sdk.mdx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ---
  2. title: "SDK"
  3. description: "Embed OpenCode directly in your application."
  4. ---
  5. We're working on a general-purpose SDK for embedding OpenCode directly inside
  6. your application. The regular SDK is coming soon.
  7. An Effect-native version is available now for applications built with Effect.
  8. Its current documentation is below. For other applications, run OpenCode as a
  9. server and use the [TypeScript client](/build/client) in the meantime.
  10. ## Effect
  11. `@opencode-ai/sdk-next` hosts OpenCode in-process. Unlike the
  12. [network client](/build/client), it assembles the OpenCode server and routes API
  13. calls through its HTTP router in memory. It opens no HTTP listener and adds no
  14. network hop between the client and server.
  15. <Callout type="warning">
  16. The V2 SDK is beta and currently private to the OpenCode workspace. It is not published for external installation yet,
  17. and its package name and API may change before release.
  18. </Callout>
  19. ## Create a host
  20. `OpenCode.create()` creates a scoped host. Closing its Effect Scope releases
  21. the router, location services, fibers, and scoped plugin registrations.
  22. ```ts
  23. import { AbsolutePath, Location, OpenCode } from "@opencode-ai/sdk-next"
  24. import { Effect } from "effect"
  25. const program = Effect.scoped(
  26. Effect.gen(function* () {
  27. const opencode = yield* OpenCode.create()
  28. const session = yield* opencode.sessions.create({
  29. location: Location.Ref.make({
  30. directory: AbsolutePath.make("/workspace"),
  31. }),
  32. })
  33. return yield* opencode.sessions.get({ sessionID: session.id })
  34. }),
  35. )
  36. const session = await Effect.runPromise(program)
  37. ```
  38. The embedded host uses the same routes, middleware, codecs, errors, and schema
  39. values as `@opencode-ai/client/effect`. It exposes the full generated client and
  40. adds the convenience aliases `sessions` and `events` for the session and event
  41. groups.
  42. ## Use as a service
  43. Use `OpenCode.layer` when the host should be provided through Effect dependency
  44. injection:
  45. ```ts
  46. import { OpenCode } from "@opencode-ai/sdk-next"
  47. import { Effect } from "effect"
  48. const program = Effect.gen(function* () {
  49. const opencode = yield* OpenCode.Service
  50. return yield* opencode.sessions.active()
  51. })
  52. const active = await Effect.runPromise(program.pipe(Effect.provide(OpenCode.layer)))
  53. ```
  54. ## Register plugins
  55. Call `opencode.plugin(...)` to register an embedded V2 plugin. Embedded plugins
  56. use the same discovery and location-scoped activation path as configured
  57. plugins. The SDK also exports `Tool` for plugin-defined tools. See the
  58. [Plugins guide](/build/plugins) for the plugin shape and available hooks.