sdk.mdx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. <Warning>
  16. The V2 SDK is beta and currently private to the OpenCode workspace. It is not
  17. published for external installation yet, and its package name and API may
  18. change before release.
  19. </Warning>
  20. ## Create a host
  21. `OpenCode.create()` creates a scoped host. Closing its Effect Scope releases
  22. the router, location services, fibers, and scoped plugin registrations.
  23. ```ts
  24. import {
  25. AbsolutePath,
  26. Location,
  27. OpenCode,
  28. } from "@opencode-ai/sdk-next"
  29. import { Effect } from "effect"
  30. const program = Effect.scoped(
  31. Effect.gen(function* () {
  32. const opencode = yield* OpenCode.create()
  33. const session = yield* opencode.sessions.create({
  34. location: Location.Ref.make({
  35. directory: AbsolutePath.make("/workspace"),
  36. }),
  37. })
  38. return yield* opencode.sessions.get({ sessionID: session.id })
  39. }),
  40. )
  41. const session = await Effect.runPromise(program)
  42. ```
  43. The embedded host uses the same routes, middleware, codecs, errors, and schema
  44. values as `@opencode-ai/client/effect`. It exposes the full generated client and
  45. adds the convenience aliases `sessions` and `events` for the session and event
  46. groups.
  47. ## Use as a service
  48. Use `OpenCode.layer` when the host should be provided through Effect dependency
  49. injection:
  50. ```ts
  51. import { OpenCode } from "@opencode-ai/sdk-next"
  52. import { Effect } from "effect"
  53. const program = Effect.gen(function* () {
  54. const opencode = yield* OpenCode.Service
  55. return yield* opencode.sessions.active()
  56. })
  57. const active = await Effect.runPromise(
  58. program.pipe(Effect.provide(OpenCode.layer)),
  59. )
  60. ```
  61. ## Register plugins
  62. Call `opencode.plugin(...)` to register an embedded V2 plugin. Embedded plugins
  63. use the same discovery and location-scoped activation path as configured
  64. plugins. The SDK also exports `Tool` for plugin-defined tools. See the
  65. [Plugins guide](/build/plugins) for the plugin shape and available hooks.