sdk.mdx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ---
  2. title: "SDK"
  3. description: "Embed an OpenCode host in an Effect application."
  4. ---
  5. `@opencode-ai/sdk-next` is the Effect-native SDK for applications that need to
  6. host OpenCode in-process. Unlike the [network client](/docs/build/client), it assembles the
  7. OpenCode server and routes API calls through its HTTP router in memory. It opens
  8. no HTTP listener and adds no network hop between the client and server.
  9. <Warning>
  10. The V2 SDK is beta and currently private to the OpenCode workspace. It is not
  11. published for external installation yet, and its package name and API may
  12. change before release.
  13. </Warning>
  14. ## Create a host
  15. `OpenCode.create()` creates a scoped host. Closing its Effect Scope releases
  16. the router, location services, fibers, and scoped plugin registrations.
  17. ```ts
  18. import {
  19. AbsolutePath,
  20. Location,
  21. OpenCode,
  22. } 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(
  52. program.pipe(Effect.provide(OpenCode.layer)),
  53. )
  54. ```
  55. ## Register plugins
  56. Call `opencode.plugin(...)` to register an embedded V2 plugin. Embedded plugins
  57. use the same discovery and location-scoped activation path as configured
  58. plugins. The SDK also exports `Tool` for plugin-defined tools. See the
  59. [Plugins guide](/docs/build/plugins) for the plugin shape and available hooks.