client.mdx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. <Warning>
  10. The V2 API and client are beta. Method names, inputs, and outputs may change
  11. before the stable release.
  12. </Warning>
  13. ## Install
  14. ```sh
  15. bun add @opencode-ai/client@next
  16. ```
  17. ## Create a client
  18. Create a client with the server URL, then call methods grouped by API resource:
  19. ```ts
  20. import { OpenCode } from "@opencode-ai/client"
  21. const client = OpenCode.make({
  22. baseUrl: "http://localhost:4096",
  23. })
  24. const session = await client.session.create({
  25. location: { directory: "/workspace" },
  26. })
  27. await client.session.prompt({
  28. sessionID: session.id,
  29. text: "Review the current changes",
  30. })
  31. ```
  32. ## Headers and requests
  33. Pass default authentication or application headers to `OpenCode.make` with
  34. `headers`. You can also supply a custom `fetch` implementation. Each operation
  35. accepts request options as its final argument for an `AbortSignal` or
  36. per-request headers.
  37. ```ts
  38. const client = OpenCode.make({
  39. baseUrl: "https://opencode.example.com",
  40. headers: {
  41. authorization: `Bearer ${process.env.OPENCODE_TOKEN}`,
  42. },
  43. })
  44. await client.session.list(undefined, {
  45. signal: AbortSignal.timeout(10_000),
  46. })
  47. ```
  48. ## Stream events
  49. Streaming endpoints return async iterables:
  50. ```ts
  51. for await (const event of client.event.subscribe()) {
  52. console.log(event.type)
  53. }
  54. ```
  55. ## Effect
  56. OpenCode provides a first-class Effect client through the
  57. `@opencode-ai/client/effect` entrypoint. It returns typed Effects and Streams
  58. and decodes responses into OpenCode schema values.
  59. ```sh
  60. bun add @opencode-ai/client@next effect
  61. ```
  62. ### Create a client
  63. ```ts
  64. import { AbsolutePath, Location, OpenCode } from "@opencode-ai/client/effect"
  65. import { Effect } from "effect"
  66. import { FetchHttpClient } from "effect/unstable/http"
  67. const program = Effect.gen(function* () {
  68. const client = yield* OpenCode.make({ baseUrl: "http://localhost:4096" })
  69. const session = yield* client.session.create({
  70. location: Location.Ref.make({
  71. directory: AbsolutePath.make("/workspace"),
  72. }),
  73. })
  74. return yield* client.session.get({ sessionID: session.id })
  75. })
  76. const session = await Effect.runPromise(
  77. program.pipe(Effect.provide(FetchHttpClient.layer)),
  78. )
  79. ```
  80. Streaming operations, including `client.event.subscribe()` and
  81. `client.session.log(...)`, return Effect `Stream` values.
  82. ### Service
  83. `Service` discovers and manages the local OpenCode background service from a
  84. Node application:
  85. - `Service.discover()` returns a healthy registered endpoint without starting
  86. a process.
  87. - `Service.start()` reuses a compatible service or starts one when needed.
  88. - `Service.stop()` stops the registered service.
  89. - `Service.headers(endpoint)` creates the authentication headers for a client.
  90. ```sh
  91. bun add @effect/platform-node
  92. ```
  93. ```ts
  94. import { NodeFileSystem } from "@effect/platform-node"
  95. import { OpenCode, Service } from "@opencode-ai/client/effect"
  96. import { Effect } from "effect"
  97. import { FetchHttpClient } from "effect/unstable/http"
  98. const program = Effect.gen(function* () {
  99. const endpoint = yield* Service.start()
  100. const client = yield* OpenCode.make({
  101. baseUrl: endpoint.url,
  102. headers: Service.headers(endpoint),
  103. })
  104. return yield* client.health.get()
  105. })
  106. const health = await Effect.runPromise(
  107. program.pipe(
  108. Effect.provide(FetchHttpClient.layer),
  109. Effect.provide(NodeFileSystem.layer),
  110. ),
  111. )
  112. ```