sdk.mdx 2.5 KB

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