client.mdx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: "Client"
  3. description: "Connect an application to the OpenCode HTTP API."
  4. ---
  5. `@opencode-ai/client` is the generated TypeScript client for the OpenCode HTTP
  6. API. Use it when your application connects to an OpenCode server over the
  7. network. Its types and methods are generated from the same contract as the
  8. [API reference](/api).
  9. <Callout type="warning">
  10. The V2 API and client are beta. Method names, inputs, and outputs may change before the stable release.
  11. </Callout>
  12. ## Install
  13. ```sh
  14. bun add @opencode-ai/client@next
  15. ```
  16. ## Create a client
  17. Create a client with the server URL, then call methods grouped by API resource:
  18. ```ts
  19. import { OpenCode } from "@opencode-ai/client"
  20. const client = OpenCode.make({
  21. baseUrl: "http://localhost:4096",
  22. })
  23. const session = await client.session.create({
  24. location: { directory: "/workspace" },
  25. })
  26. await client.session.prompt({
  27. sessionID: session.id,
  28. text: "Review the current changes",
  29. })
  30. ```
  31. ## Headers and requests
  32. Pass default authentication or application headers to `OpenCode.make` with
  33. `headers`. You can also supply a custom `fetch` implementation. Each operation
  34. accepts request options as its final argument for an `AbortSignal` or
  35. per-request headers.
  36. ```ts
  37. const client = OpenCode.make({
  38. baseUrl: "https://opencode.example.com",
  39. headers: {
  40. authorization: `Bearer ${process.env.OPENCODE_TOKEN}`,
  41. },
  42. })
  43. await client.session.list(undefined, {
  44. signal: AbortSignal.timeout(10_000),
  45. })
  46. ```
  47. ## Stream events
  48. Streaming endpoints return async iterables:
  49. ```ts
  50. for await (const event of client.event.subscribe()) {
  51. console.log(event.type)
  52. }
  53. ```
  54. ## Local background service
  55. The main client entrypoints are browser-compatible and do not include local
  56. process management. In a Node application, import the native Promise service
  57. API from `@opencode-ai/client/service`.
  58. - `Service.discover()` returns a healthy registered endpoint without starting
  59. a process.
  60. - `Service.ensure()` returns a compatible service, starting one when needed.
  61. - `Service.stop()` stops the exact registered service instance.
  62. - `Service.headers(endpoint)` creates the authentication headers for a client.
  63. ```ts
  64. import { OpenCode } from "@opencode-ai/client"
  65. import { Service } from "@opencode-ai/client/service"
  66. const endpoint = await Service.ensure()
  67. const client = OpenCode.make({
  68. baseUrl: endpoint.url,
  69. headers: Service.headers(endpoint),
  70. })
  71. const health = await client.health.get()
  72. ```
  73. `Service.ensure()` accepts an optional registration file, required version,
  74. service command, and `onStart` callback:
  75. ```ts
  76. const endpoint = await Service.ensure({
  77. file: "/var/run/opencode/service.json",
  78. version: "2.0.0",
  79. command: ["opencode", "serve", "--service"],
  80. onStart(reason, previousVersion) {
  81. console.log(reason, previousVersion)
  82. },
  83. })
  84. ```
  85. Omit these options to use the standard registration path and
  86. `opencode serve --service` command.
  87. ## Effect
  88. OpenCode provides a first-class Effect client through the
  89. `@opencode-ai/client/effect` entrypoint. It returns typed Effects and Streams
  90. and decodes responses into OpenCode schema values.
  91. ```sh
  92. bun add @opencode-ai/client@next effect
  93. ```
  94. ### Create a client
  95. ```ts
  96. import { AbsolutePath, Location, OpenCode } from "@opencode-ai/client/effect"
  97. import { Effect } from "effect"
  98. import { FetchHttpClient } from "effect/unstable/http"
  99. const program = Effect.gen(function* () {
  100. const client = yield* OpenCode.make({ baseUrl: "http://localhost:4096" })
  101. const session = yield* client.session.create({
  102. location: Location.Ref.make({
  103. directory: AbsolutePath.make("/workspace"),
  104. }),
  105. })
  106. return yield* client.session.get({ sessionID: session.id })
  107. })
  108. const session = await Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)))
  109. ```
  110. Streaming operations, including `client.event.subscribe()` and
  111. `client.session.log(...)`, return Effect `Stream` values.
  112. ### Local background service
  113. The Node-only `@opencode-ai/client/effect/service` entrypoint exposes the same
  114. operations as Effect values. Add `@effect/platform-node` and provide its
  115. filesystem layer when running them.
  116. ```sh
  117. bun add @effect/platform-node
  118. ```
  119. ```ts
  120. import { NodeFileSystem } from "@effect/platform-node"
  121. import { OpenCode } from "@opencode-ai/client/effect"
  122. import { Service } from "@opencode-ai/client/effect/service"
  123. import { Effect } from "effect"
  124. import { FetchHttpClient } from "effect/unstable/http"
  125. const program = Effect.gen(function* () {
  126. const endpoint = yield* Service.ensure()
  127. const client = yield* OpenCode.make({
  128. baseUrl: endpoint.url,
  129. headers: Service.headers(endpoint),
  130. })
  131. return yield* client.health.get()
  132. })
  133. const health = await Effect.runPromise(
  134. program.pipe(Effect.provide(FetchHttpClient.layer), Effect.provide(NodeFileSystem.layer)),
  135. )
  136. ```