@opencode-ai/sdk 0.1.0-alpha.1 → 0.1.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +198 -4
  3. package/README.md +20 -6
  4. package/client.d.mts +15 -56
  5. package/client.d.mts.map +1 -1
  6. package/client.d.ts +15 -56
  7. package/client.d.ts.map +1 -1
  8. package/client.js +14 -67
  9. package/client.js.map +1 -1
  10. package/client.mjs +14 -67
  11. package/client.mjs.map +1 -1
  12. package/core/streaming.d.mts +31 -0
  13. package/core/streaming.d.mts.map +1 -0
  14. package/core/streaming.d.ts +31 -0
  15. package/core/streaming.d.ts.map +1 -0
  16. package/core/streaming.js +257 -0
  17. package/core/streaming.js.map +1 -0
  18. package/core/streaming.mjs +252 -0
  19. package/core/streaming.mjs.map +1 -0
  20. package/internal/decoders/line.d.mts +17 -0
  21. package/internal/decoders/line.d.mts.map +1 -0
  22. package/internal/decoders/line.d.ts +17 -0
  23. package/internal/decoders/line.d.ts.map +1 -0
  24. package/internal/decoders/line.js +113 -0
  25. package/internal/decoders/line.js.map +1 -0
  26. package/internal/decoders/line.mjs +108 -0
  27. package/internal/decoders/line.mjs.map +1 -0
  28. package/internal/parse.d.mts.map +1 -1
  29. package/internal/parse.d.ts.map +1 -1
  30. package/internal/parse.js +10 -0
  31. package/internal/parse.js.map +1 -1
  32. package/internal/parse.mjs +10 -0
  33. package/internal/parse.mjs.map +1 -1
  34. package/internal/request-options.d.mts +2 -0
  35. package/internal/request-options.d.mts.map +1 -1
  36. package/internal/request-options.d.ts +2 -0
  37. package/internal/request-options.d.ts.map +1 -1
  38. package/internal/request-options.js.map +1 -1
  39. package/internal/request-options.mjs.map +1 -1
  40. package/internal/tslib.js +6 -6
  41. package/internal/types.d.mts +8 -6
  42. package/internal/types.d.mts.map +1 -1
  43. package/internal/types.d.ts +8 -6
  44. package/internal/types.d.ts.map +1 -1
  45. package/internal/uploads.js +1 -1
  46. package/internal/uploads.js.map +1 -1
  47. package/internal/uploads.mjs +1 -1
  48. package/internal/uploads.mjs.map +1 -1
  49. package/internal/utils/log.js +1 -1
  50. package/internal/utils/log.js.map +1 -1
  51. package/internal/utils/log.mjs +1 -1
  52. package/internal/utils/log.mjs.map +1 -1
  53. package/internal/utils/path.d.mts.map +1 -1
  54. package/internal/utils/path.d.ts.map +1 -1
  55. package/internal/utils/path.js +26 -5
  56. package/internal/utils/path.js.map +1 -1
  57. package/internal/utils/path.mjs +26 -5
  58. package/internal/utils/path.mjs.map +1 -1
  59. package/package.json +13 -3
  60. package/resources/app.d.mts +37 -0
  61. package/resources/app.d.mts.map +1 -0
  62. package/resources/app.d.ts +37 -0
  63. package/resources/app.d.ts.map +1 -0
  64. package/resources/app.js +21 -0
  65. package/resources/app.js.map +1 -0
  66. package/resources/app.mjs +17 -0
  67. package/resources/app.mjs.map +1 -0
  68. package/resources/config.d.mts +314 -0
  69. package/resources/config.d.mts.map +1 -0
  70. package/resources/config.d.ts +314 -0
  71. package/resources/config.d.ts.map +1 -0
  72. package/resources/config.js +21 -0
  73. package/resources/config.js.map +1 -0
  74. package/resources/config.mjs +17 -0
  75. package/resources/config.mjs.map +1 -0
  76. package/resources/event.d.mts +61 -27
  77. package/resources/event.d.mts.map +1 -1
  78. package/resources/event.d.ts +61 -27
  79. package/resources/event.d.ts.map +1 -1
  80. package/resources/event.js +1 -1
  81. package/resources/event.js.map +1 -1
  82. package/resources/event.mjs +1 -1
  83. package/resources/event.mjs.map +1 -1
  84. package/resources/file.d.mts +17 -0
  85. package/resources/file.d.mts.map +1 -0
  86. package/resources/file.d.ts +17 -0
  87. package/resources/file.d.ts.map +1 -0
  88. package/resources/{file-search.js → file.js} +6 -6
  89. package/resources/file.js.map +1 -0
  90. package/resources/{file-search.mjs → file.mjs} +4 -4
  91. package/resources/file.mjs.map +1 -0
  92. package/resources/index.d.mts +5 -16
  93. package/resources/index.d.mts.map +1 -1
  94. package/resources/index.d.ts +5 -16
  95. package/resources/index.d.ts.map +1 -1
  96. package/resources/index.js +11 -33
  97. package/resources/index.js.map +1 -1
  98. package/resources/index.mjs +5 -16
  99. package/resources/index.mjs.map +1 -1
  100. package/resources/session.d.mts +207 -0
  101. package/resources/session.d.mts.map +1 -0
  102. package/resources/session.d.ts +207 -0
  103. package/resources/session.d.ts.map +1 -0
  104. package/resources/session.js +70 -0
  105. package/resources/session.js.map +1 -0
  106. package/resources/session.mjs +66 -0
  107. package/resources/session.mjs.map +1 -0
  108. package/resources/shared.d.mts +20 -0
  109. package/resources/shared.d.mts.map +1 -0
  110. package/resources/shared.d.ts +20 -0
  111. package/resources/shared.d.ts.map +1 -0
  112. package/resources/shared.js +4 -0
  113. package/resources/shared.js.map +1 -0
  114. package/resources/shared.mjs +3 -0
  115. package/resources/shared.mjs.map +1 -0
  116. package/src/client.ts +79 -156
  117. package/src/core/streaming.ts +301 -0
  118. package/src/internal/decoders/line.ts +135 -0
  119. package/src/internal/parse.ts +14 -0
  120. package/src/internal/request-options.ts +2 -0
  121. package/src/internal/types.ts +9 -6
  122. package/src/internal/uploads.ts +1 -1
  123. package/src/internal/utils/log.ts +1 -1
  124. package/src/internal/utils/path.ts +32 -7
  125. package/src/resources/app.ts +57 -0
  126. package/src/resources/config.ts +404 -0
  127. package/src/resources/event.ts +84 -36
  128. package/src/resources/file.ts +24 -0
  129. package/src/resources/index.ts +35 -33
  130. package/src/resources/session.ts +355 -0
  131. package/src/resources/shared.ts +27 -0
  132. package/src/streaming.ts +2 -0
  133. package/src/version.ts +1 -1
  134. package/streaming.d.mts +2 -0
  135. package/streaming.d.mts.map +1 -0
  136. package/streaming.d.ts +2 -0
  137. package/streaming.d.ts.map +1 -0
  138. package/streaming.js +6 -0
  139. package/streaming.js.map +1 -0
  140. package/streaming.mjs +2 -0
  141. package/streaming.mjs.map +1 -0
  142. package/version.d.mts +1 -1
  143. package/version.d.mts.map +1 -1
  144. package/version.d.ts +1 -1
  145. package/version.d.ts.map +1 -1
  146. package/version.js +1 -1
  147. package/version.js.map +1 -1
  148. package/version.mjs +1 -1
  149. package/version.mjs.map +1 -1
  150. package/resources/app-info.d.mts +0 -31
  151. package/resources/app-info.d.mts.map +0 -1
  152. package/resources/app-info.d.ts +0 -31
  153. package/resources/app-info.d.ts.map +0 -1
  154. package/resources/app-info.js +0 -15
  155. package/resources/app-info.js.map +0 -1
  156. package/resources/app-info.mjs +0 -11
  157. package/resources/app-info.mjs.map +0 -1
  158. package/resources/app-initialize.d.mts +0 -14
  159. package/resources/app-initialize.d.mts.map +0 -1
  160. package/resources/app-initialize.d.ts +0 -14
  161. package/resources/app-initialize.d.ts.map +0 -1
  162. package/resources/app-initialize.js +0 -15
  163. package/resources/app-initialize.js.map +0 -1
  164. package/resources/app-initialize.mjs +0 -11
  165. package/resources/app-initialize.mjs.map +0 -1
  166. package/resources/config-get.d.mts +0 -236
  167. package/resources/config-get.d.mts.map +0 -1
  168. package/resources/config-get.d.ts +0 -236
  169. package/resources/config-get.d.ts.map +0 -1
  170. package/resources/config-get.js +0 -15
  171. package/resources/config-get.js.map +0 -1
  172. package/resources/config-get.mjs +0 -11
  173. package/resources/config-get.mjs.map +0 -1
  174. package/resources/file-search.d.mts +0 -17
  175. package/resources/file-search.d.mts.map +0 -1
  176. package/resources/file-search.d.ts +0 -17
  177. package/resources/file-search.d.ts.map +0 -1
  178. package/resources/file-search.js.map +0 -1
  179. package/resources/file-search.mjs.map +0 -1
  180. package/resources/installation-info.d.mts +0 -17
  181. package/resources/installation-info.d.mts.map +0 -1
  182. package/resources/installation-info.d.ts +0 -17
  183. package/resources/installation-info.d.ts.map +0 -1
  184. package/resources/installation-info.js +0 -15
  185. package/resources/installation-info.js.map +0 -1
  186. package/resources/installation-info.mjs +0 -11
  187. package/resources/installation-info.mjs.map +0 -1
  188. package/resources/path-get.d.mts +0 -19
  189. package/resources/path-get.d.mts.map +0 -1
  190. package/resources/path-get.d.ts +0 -19
  191. package/resources/path-get.d.ts.map +0 -1
  192. package/resources/path-get.js +0 -15
  193. package/resources/path-get.js.map +0 -1
  194. package/resources/path-get.mjs +0 -11
  195. package/resources/path-get.mjs.map +0 -1
  196. package/resources/provider-list.d.mts +0 -58
  197. package/resources/provider-list.d.mts.map +0 -1
  198. package/resources/provider-list.d.ts +0 -58
  199. package/resources/provider-list.d.ts.map +0 -1
  200. package/resources/provider-list.js +0 -15
  201. package/resources/provider-list.js.map +0 -1
  202. package/resources/provider-list.mjs +0 -11
  203. package/resources/provider-list.mjs.map +0 -1
  204. package/resources/session-abort.d.mts +0 -17
  205. package/resources/session-abort.d.mts.map +0 -1
  206. package/resources/session-abort.d.ts +0 -17
  207. package/resources/session-abort.d.ts.map +0 -1
  208. package/resources/session-abort.js +0 -15
  209. package/resources/session-abort.js.map +0 -1
  210. package/resources/session-abort.mjs +0 -11
  211. package/resources/session-abort.mjs.map +0 -1
  212. package/resources/session-chat.d.mts +0 -160
  213. package/resources/session-chat.d.mts.map +0 -1
  214. package/resources/session-chat.d.ts +0 -160
  215. package/resources/session-chat.d.ts.map +0 -1
  216. package/resources/session-chat.js +0 -15
  217. package/resources/session-chat.js.map +0 -1
  218. package/resources/session-chat.mjs +0 -11
  219. package/resources/session-chat.mjs.map +0 -1
  220. package/resources/session-create.d.mts +0 -30
  221. package/resources/session-create.d.mts.map +0 -1
  222. package/resources/session-create.d.ts +0 -30
  223. package/resources/session-create.d.ts.map +0 -1
  224. package/resources/session-create.js +0 -15
  225. package/resources/session-create.js.map +0 -1
  226. package/resources/session-create.mjs +0 -11
  227. package/resources/session-create.mjs.map +0 -1
  228. package/resources/session-initialize.d.mts +0 -19
  229. package/resources/session-initialize.d.mts.map +0 -1
  230. package/resources/session-initialize.d.ts +0 -19
  231. package/resources/session-initialize.d.ts.map +0 -1
  232. package/resources/session-initialize.js +0 -15
  233. package/resources/session-initialize.js.map +0 -1
  234. package/resources/session-initialize.mjs +0 -11
  235. package/resources/session-initialize.mjs.map +0 -1
  236. package/resources/session-list.d.mts +0 -15
  237. package/resources/session-list.d.mts.map +0 -1
  238. package/resources/session-list.d.ts +0 -15
  239. package/resources/session-list.d.ts.map +0 -1
  240. package/resources/session-list.js +0 -15
  241. package/resources/session-list.js.map +0 -1
  242. package/resources/session-list.mjs +0 -11
  243. package/resources/session-list.mjs.map +0 -1
  244. package/resources/session-messages.d.mts +0 -18
  245. package/resources/session-messages.d.mts.map +0 -1
  246. package/resources/session-messages.d.ts +0 -18
  247. package/resources/session-messages.d.ts.map +0 -1
  248. package/resources/session-messages.js +0 -15
  249. package/resources/session-messages.js.map +0 -1
  250. package/resources/session-messages.mjs +0 -11
  251. package/resources/session-messages.mjs.map +0 -1
  252. package/resources/session-share.d.mts +0 -17
  253. package/resources/session-share.d.mts.map +0 -1
  254. package/resources/session-share.d.ts +0 -17
  255. package/resources/session-share.d.ts.map +0 -1
  256. package/resources/session-share.js +0 -15
  257. package/resources/session-share.js.map +0 -1
  258. package/resources/session-share.mjs +0 -11
  259. package/resources/session-share.mjs.map +0 -1
  260. package/resources/session-summarize.d.mts +0 -19
  261. package/resources/session-summarize.d.mts.map +0 -1
  262. package/resources/session-summarize.d.ts +0 -19
  263. package/resources/session-summarize.d.ts.map +0 -1
  264. package/resources/session-summarize.js +0 -15
  265. package/resources/session-summarize.js.map +0 -1
  266. package/resources/session-summarize.mjs +0 -11
  267. package/resources/session-summarize.mjs.map +0 -1
  268. package/resources/session-unshare.d.mts +0 -17
  269. package/resources/session-unshare.d.mts.map +0 -1
  270. package/resources/session-unshare.d.ts +0 -17
  271. package/resources/session-unshare.d.ts.map +0 -1
  272. package/resources/session-unshare.js +0 -15
  273. package/resources/session-unshare.js.map +0 -1
  274. package/resources/session-unshare.mjs +0 -11
  275. package/resources/session-unshare.mjs.map +0 -1
  276. package/src/resources/app-info.ts +0 -46
  277. package/src/resources/app-initialize.ts +0 -20
  278. package/src/resources/config-get.ts +0 -294
  279. package/src/resources/file-search.ts +0 -27
  280. package/src/resources/installation-info.ts +0 -24
  281. package/src/resources/path-get.ts +0 -28
  282. package/src/resources/provider-list.ts +0 -80
  283. package/src/resources/session-abort.ts +0 -27
  284. package/src/resources/session-chat.ts +0 -250
  285. package/src/resources/session-create.ts +0 -44
  286. package/src/resources/session-initialize.ts +0 -34
  287. package/src/resources/session-list.ts +0 -21
  288. package/src/resources/session-messages.ts +0 -31
  289. package/src/resources/session-share.ts +0 -23
  290. package/src/resources/session-summarize.ts +0 -34
  291. package/src/resources/session-unshare.ts +0 -26
package/src/client.ts CHANGED
@@ -16,42 +16,43 @@ import * as Errors from './core/error';
16
16
  import * as Uploads from './core/uploads';
17
17
  import * as API from './resources/index';
18
18
  import { APIPromise } from './core/api-promise';
19
- import { AppInfo, AppInfoCreateResponse } from './resources/app-info';
20
- import { AppInitialize, AppInitializeInitializeResponse } from './resources/app-initialize';
21
- import { ConfigGet, ConfigGetRetrieveResponse } from './resources/config-get';
19
+ import { App, AppInitResponse, AppResource } from './resources/app';
20
+ import {
21
+ Config,
22
+ ConfigProvidersResponse,
23
+ ConfigResource,
24
+ Keybinds,
25
+ McpLocal,
26
+ McpRemote,
27
+ Model,
28
+ Provider,
29
+ } from './resources/config';
22
30
  import { Event, EventListResponse } from './resources/event';
23
- import { FileSearch, FileSearchSearchParams, FileSearchSearchResponse } from './resources/file-search';
24
- import { InstallationInfo, InstallationInfoCreateResponse } from './resources/installation-info';
25
- import { PathGet, PathGetCreateResponse } from './resources/path-get';
26
- import { ProviderList, ProviderListCreateResponse } from './resources/provider-list';
27
- import { SessionAbort, SessionAbortAbortParams, SessionAbortAbortResponse } from './resources/session-abort';
31
+ import { File, FileSearchParams, FileSearchResponse } from './resources/file';
28
32
  import {
29
- MessageInfo,
33
+ FilePart,
34
+ Message,
30
35
  MessagePart,
31
- ProviderAuthError,
32
- SessionChat,
33
- SessionChatCreateParams,
34
- UnknownError,
35
- } from './resources/session-chat';
36
- import { SessionCreate, SessionInfo } from './resources/session-create';
37
- import {
38
- SessionInitialize,
39
- SessionInitializeCreateParams,
40
- SessionInitializeCreateResponse,
41
- } from './resources/session-initialize';
42
- import { SessionList, SessionListCreateResponse } from './resources/session-list';
43
- import {
44
- SessionMessageCreateParams,
45
- SessionMessageCreateResponse,
46
- SessionMessages,
47
- } from './resources/session-messages';
48
- import { SessionShare, SessionShareCreateParams } from './resources/session-share';
49
- import {
50
- SessionSummarize,
51
- SessionSummarizeSummarizeParams,
52
- SessionSummarizeSummarizeResponse,
53
- } from './resources/session-summarize';
54
- import { SessionUnshare, SessionUnshareUnshareParams } from './resources/session-unshare';
36
+ ReasoningPart,
37
+ Session,
38
+ SessionAbortResponse,
39
+ SessionChatParams,
40
+ SessionDeleteResponse,
41
+ SessionInitParams,
42
+ SessionInitResponse,
43
+ SessionListResponse,
44
+ SessionMessagesResponse,
45
+ SessionResource,
46
+ SessionSummarizeParams,
47
+ SessionSummarizeResponse,
48
+ SourceURLPart,
49
+ StepStartPart,
50
+ TextPart,
51
+ ToolCall,
52
+ ToolInvocationPart,
53
+ ToolPartialCall,
54
+ ToolResult,
55
+ } from './resources/session';
55
56
  import { type Fetch } from './internal/builtin-types';
56
57
  import { HeadersLike, NullableHeaders, buildHeaders } from './internal/headers';
57
58
  import { FinalRequestOptions, RequestOptions } from './internal/request-options';
@@ -66,11 +67,6 @@ import {
66
67
  import { isEmptyObj } from './internal/utils/values';
67
68
 
68
69
  export interface ClientOptions {
69
- /**
70
- * Defaults to process.env['OPENCODE_API_KEY'].
71
- */
72
- apiKey?: string | null | undefined;
73
-
74
70
  /**
75
71
  * Override the default base URL for the API, e.g., "https://api.example.com/v2/"
76
72
  *
@@ -142,8 +138,6 @@ export interface ClientOptions {
142
138
  * API Client for interfacing with the Opencode API.
143
139
  */
144
140
  export class Opencode {
145
- apiKey: string | null;
146
-
147
141
  baseURL: string;
148
142
  maxRetries: number;
149
143
  timeout: number;
@@ -159,7 +153,6 @@ export class Opencode {
159
153
  /**
160
154
  * API Client for interfacing with the Opencode API.
161
155
  *
162
- * @param {string | null | undefined} [opts.apiKey=process.env['OPENCODE_API_KEY'] ?? null]
163
156
  * @param {string} [opts.baseURL=process.env['OPENCODE_BASE_URL'] ?? http://localhost:54321] - Override the default base URL for the API.
164
157
  * @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
165
158
  * @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
@@ -168,13 +161,8 @@ export class Opencode {
168
161
  * @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
169
162
  * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
170
163
  */
171
- constructor({
172
- baseURL = readEnv('OPENCODE_BASE_URL'),
173
- apiKey = readEnv('OPENCODE_API_KEY') ?? null,
174
- ...opts
175
- }: ClientOptions = {}) {
164
+ constructor({ baseURL = readEnv('OPENCODE_BASE_URL'), ...opts }: ClientOptions = {}) {
176
165
  const options: ClientOptions = {
177
- apiKey,
178
166
  ...opts,
179
167
  baseURL: baseURL || `http://localhost:54321`,
180
168
  };
@@ -195,8 +183,6 @@ export class Opencode {
195
183
  this.#encoder = Opts.FallbackEncoder;
196
184
 
197
185
  this._options = options;
198
-
199
- this.apiKey = apiKey;
200
186
  }
201
187
 
202
188
  /**
@@ -212,7 +198,6 @@ export class Opencode {
212
198
  logLevel: this.logLevel,
213
199
  fetch: this.fetch,
214
200
  fetchOptions: this.fetchOptions,
215
- apiKey: this.apiKey,
216
201
  ...options,
217
202
  });
218
203
  }
@@ -229,23 +214,7 @@ export class Opencode {
229
214
  }
230
215
 
231
216
  protected validateHeaders({ values, nulls }: NullableHeaders) {
232
- if (this.apiKey && values.get('authorization')) {
233
- return;
234
- }
235
- if (nulls.has('authorization')) {
236
- return;
237
- }
238
-
239
- throw new Error(
240
- 'Could not resolve authentication method. Expected the apiKey to be set. Or for the "Authorization" headers to be explicitly omitted',
241
- );
242
- }
243
-
244
- protected authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
245
- if (this.apiKey == null) {
246
- return undefined;
247
- }
248
- return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
217
+ return;
249
218
  }
250
219
 
251
220
  /**
@@ -683,7 +652,6 @@ export class Opencode {
683
652
  ...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
684
653
  ...getPlatformHeaders(),
685
654
  },
686
- this.authHeaders(options),
687
655
  this._options.defaultHeaders,
688
656
  bodyHeaders,
689
657
  options.headers,
@@ -751,110 +719,65 @@ export class Opencode {
751
719
  static toFile = Uploads.toFile;
752
720
 
753
721
  event: API.Event = new API.Event(this);
754
- appInfo: API.AppInfo = new API.AppInfo(this);
755
- configGet: API.ConfigGet = new API.ConfigGet(this);
756
- appInitialize: API.AppInitialize = new API.AppInitialize(this);
757
- sessionInitialize: API.SessionInitialize = new API.SessionInitialize(this);
758
- pathGet: API.PathGet = new API.PathGet(this);
759
- sessionCreate: API.SessionCreate = new API.SessionCreate(this);
760
- sessionShare: API.SessionShare = new API.SessionShare(this);
761
- sessionUnshare: API.SessionUnshare = new API.SessionUnshare(this);
762
- sessionMessages: API.SessionMessages = new API.SessionMessages(this);
763
- sessionList: API.SessionList = new API.SessionList(this);
764
- sessionAbort: API.SessionAbort = new API.SessionAbort(this);
765
- sessionSummarize: API.SessionSummarize = new API.SessionSummarize(this);
766
- sessionChat: API.SessionChat = new API.SessionChat(this);
767
- providerList: API.ProviderList = new API.ProviderList(this);
768
- fileSearch: API.FileSearch = new API.FileSearch(this);
769
- installationInfo: API.InstallationInfo = new API.InstallationInfo(this);
722
+ app: API.AppResource = new API.AppResource(this);
723
+ file: API.File = new API.File(this);
724
+ config: API.ConfigResource = new API.ConfigResource(this);
725
+ session: API.SessionResource = new API.SessionResource(this);
770
726
  }
771
727
  Opencode.Event = Event;
772
- Opencode.AppInfo = AppInfo;
773
- Opencode.ConfigGet = ConfigGet;
774
- Opencode.AppInitialize = AppInitialize;
775
- Opencode.SessionInitialize = SessionInitialize;
776
- Opencode.PathGet = PathGet;
777
- Opencode.SessionCreate = SessionCreate;
778
- Opencode.SessionShare = SessionShare;
779
- Opencode.SessionUnshare = SessionUnshare;
780
- Opencode.SessionMessages = SessionMessages;
781
- Opencode.SessionList = SessionList;
782
- Opencode.SessionAbort = SessionAbort;
783
- Opencode.SessionSummarize = SessionSummarize;
784
- Opencode.SessionChat = SessionChat;
785
- Opencode.ProviderList = ProviderList;
786
- Opencode.FileSearch = FileSearch;
787
- Opencode.InstallationInfo = InstallationInfo;
728
+ Opencode.AppResource = AppResource;
729
+ Opencode.File = File;
730
+ Opencode.ConfigResource = ConfigResource;
731
+ Opencode.SessionResource = SessionResource;
788
732
  export declare namespace Opencode {
789
733
  export type RequestOptions = Opts.RequestOptions;
790
734
 
791
735
  export { Event as Event, type EventListResponse as EventListResponse };
792
736
 
793
- export { AppInfo as AppInfo, type AppInfoCreateResponse as AppInfoCreateResponse };
794
-
795
- export { ConfigGet as ConfigGet, type ConfigGetRetrieveResponse as ConfigGetRetrieveResponse };
737
+ export { AppResource as AppResource, type App as App, type AppInitResponse as AppInitResponse };
796
738
 
797
739
  export {
798
- AppInitialize as AppInitialize,
799
- type AppInitializeInitializeResponse as AppInitializeInitializeResponse,
740
+ File as File,
741
+ type FileSearchResponse as FileSearchResponse,
742
+ type FileSearchParams as FileSearchParams,
800
743
  };
801
744
 
802
745
  export {
803
- SessionInitialize as SessionInitialize,
804
- type SessionInitializeCreateResponse as SessionInitializeCreateResponse,
805
- type SessionInitializeCreateParams as SessionInitializeCreateParams,
746
+ ConfigResource as ConfigResource,
747
+ type Config as Config,
748
+ type Keybinds as Keybinds,
749
+ type McpLocal as McpLocal,
750
+ type McpRemote as McpRemote,
751
+ type Model as Model,
752
+ type Provider as Provider,
753
+ type ConfigProvidersResponse as ConfigProvidersResponse,
806
754
  };
807
755
 
808
- export { PathGet as PathGet, type PathGetCreateResponse as PathGetCreateResponse };
809
-
810
- export { SessionCreate as SessionCreate, type SessionInfo as SessionInfo };
811
-
812
- export { SessionShare as SessionShare, type SessionShareCreateParams as SessionShareCreateParams };
813
-
814
756
  export {
815
- SessionUnshare as SessionUnshare,
816
- type SessionUnshareUnshareParams as SessionUnshareUnshareParams,
817
- };
818
-
819
- export {
820
- SessionMessages as SessionMessages,
821
- type SessionMessageCreateResponse as SessionMessageCreateResponse,
822
- type SessionMessageCreateParams as SessionMessageCreateParams,
823
- };
824
-
825
- export { SessionList as SessionList, type SessionListCreateResponse as SessionListCreateResponse };
826
-
827
- export {
828
- SessionAbort as SessionAbort,
829
- type SessionAbortAbortResponse as SessionAbortAbortResponse,
830
- type SessionAbortAbortParams as SessionAbortAbortParams,
831
- };
832
-
833
- export {
834
- SessionSummarize as SessionSummarize,
835
- type SessionSummarizeSummarizeResponse as SessionSummarizeSummarizeResponse,
836
- type SessionSummarizeSummarizeParams as SessionSummarizeSummarizeParams,
837
- };
838
-
839
- export {
840
- SessionChat as SessionChat,
841
- type MessageInfo as MessageInfo,
757
+ SessionResource as SessionResource,
758
+ type FilePart as FilePart,
759
+ type Message as Message,
842
760
  type MessagePart as MessagePart,
843
- type ProviderAuthError as ProviderAuthError,
844
- type UnknownError as UnknownError,
845
- type SessionChatCreateParams as SessionChatCreateParams,
761
+ type ReasoningPart as ReasoningPart,
762
+ type Session as Session,
763
+ type SourceURLPart as SourceURLPart,
764
+ type StepStartPart as StepStartPart,
765
+ type TextPart as TextPart,
766
+ type ToolCall as ToolCall,
767
+ type ToolInvocationPart as ToolInvocationPart,
768
+ type ToolPartialCall as ToolPartialCall,
769
+ type ToolResult as ToolResult,
770
+ type SessionListResponse as SessionListResponse,
771
+ type SessionDeleteResponse as SessionDeleteResponse,
772
+ type SessionAbortResponse as SessionAbortResponse,
773
+ type SessionInitResponse as SessionInitResponse,
774
+ type SessionMessagesResponse as SessionMessagesResponse,
775
+ type SessionSummarizeResponse as SessionSummarizeResponse,
776
+ type SessionChatParams as SessionChatParams,
777
+ type SessionInitParams as SessionInitParams,
778
+ type SessionSummarizeParams as SessionSummarizeParams,
846
779
  };
847
780
 
848
- export { ProviderList as ProviderList, type ProviderListCreateResponse as ProviderListCreateResponse };
849
-
850
- export {
851
- FileSearch as FileSearch,
852
- type FileSearchSearchResponse as FileSearchSearchResponse,
853
- type FileSearchSearchParams as FileSearchSearchParams,
854
- };
855
-
856
- export {
857
- InstallationInfo as InstallationInfo,
858
- type InstallationInfoCreateResponse as InstallationInfoCreateResponse,
859
- };
781
+ export type ProviderAuthError = API.ProviderAuthError;
782
+ export type UnknownError = API.UnknownError;
860
783
  }
@@ -0,0 +1,301 @@
1
+ import { OpencodeError } from './error';
2
+ import { type ReadableStream } from '../internal/shim-types';
3
+ import { makeReadableStream } from '../internal/shims';
4
+ import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
5
+ import { ReadableStreamToAsyncIterable } from '../internal/shims';
6
+ import { isAbortError } from '../internal/errors';
7
+ import { encodeUTF8 } from '../internal/utils/bytes';
8
+
9
+ type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
10
+
11
+ export type ServerSentEvent = {
12
+ event: string | null;
13
+ data: string;
14
+ raw: string[];
15
+ };
16
+
17
+ export class Stream<Item> implements AsyncIterable<Item> {
18
+ controller: AbortController;
19
+
20
+ constructor(
21
+ private iterator: () => AsyncIterator<Item>,
22
+ controller: AbortController,
23
+ ) {
24
+ this.controller = controller;
25
+ }
26
+
27
+ static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {
28
+ let consumed = false;
29
+
30
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
31
+ if (consumed) {
32
+ throw new OpencodeError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
33
+ }
34
+ consumed = true;
35
+ let done = false;
36
+ try {
37
+ for await (const sse of _iterSSEMessages(response, controller)) {
38
+ try {
39
+ yield JSON.parse(sse.data);
40
+ } catch (e) {
41
+ console.error(`Could not parse message into JSON:`, sse.data);
42
+ console.error(`From chunk:`, sse.raw);
43
+ throw e;
44
+ }
45
+ }
46
+ done = true;
47
+ } catch (e) {
48
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
49
+ if (isAbortError(e)) return;
50
+ throw e;
51
+ } finally {
52
+ // If the user `break`s, abort the ongoing request.
53
+ if (!done) controller.abort();
54
+ }
55
+ }
56
+
57
+ return new Stream(iterator, controller);
58
+ }
59
+
60
+ /**
61
+ * Generates a Stream from a newline-separated ReadableStream
62
+ * where each item is a JSON value.
63
+ */
64
+ static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {
65
+ let consumed = false;
66
+
67
+ async function* iterLines(): AsyncGenerator<string, void, unknown> {
68
+ const lineDecoder = new LineDecoder();
69
+
70
+ const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);
71
+ for await (const chunk of iter) {
72
+ for (const line of lineDecoder.decode(chunk)) {
73
+ yield line;
74
+ }
75
+ }
76
+
77
+ for (const line of lineDecoder.flush()) {
78
+ yield line;
79
+ }
80
+ }
81
+
82
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
83
+ if (consumed) {
84
+ throw new OpencodeError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
85
+ }
86
+ consumed = true;
87
+ let done = false;
88
+ try {
89
+ for await (const line of iterLines()) {
90
+ if (done) continue;
91
+ if (line) yield JSON.parse(line);
92
+ }
93
+ done = true;
94
+ } catch (e) {
95
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
96
+ if (isAbortError(e)) return;
97
+ throw e;
98
+ } finally {
99
+ // If the user `break`s, abort the ongoing request.
100
+ if (!done) controller.abort();
101
+ }
102
+ }
103
+
104
+ return new Stream(iterator, controller);
105
+ }
106
+
107
+ [Symbol.asyncIterator](): AsyncIterator<Item> {
108
+ return this.iterator();
109
+ }
110
+
111
+ /**
112
+ * Splits the stream into two streams which can be
113
+ * independently read from at different speeds.
114
+ */
115
+ tee(): [Stream<Item>, Stream<Item>] {
116
+ const left: Array<Promise<IteratorResult<Item>>> = [];
117
+ const right: Array<Promise<IteratorResult<Item>>> = [];
118
+ const iterator = this.iterator();
119
+
120
+ const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
121
+ return {
122
+ next: () => {
123
+ if (queue.length === 0) {
124
+ const result = iterator.next();
125
+ left.push(result);
126
+ right.push(result);
127
+ }
128
+ return queue.shift()!;
129
+ },
130
+ };
131
+ };
132
+
133
+ return [
134
+ new Stream(() => teeIterator(left), this.controller),
135
+ new Stream(() => teeIterator(right), this.controller),
136
+ ];
137
+ }
138
+
139
+ /**
140
+ * Converts this stream to a newline-separated ReadableStream of
141
+ * JSON stringified values in the stream
142
+ * which can be turned back into a Stream with `Stream.fromReadableStream()`.
143
+ */
144
+ toReadableStream(): ReadableStream {
145
+ const self = this;
146
+ let iter: AsyncIterator<Item>;
147
+
148
+ return makeReadableStream({
149
+ async start() {
150
+ iter = self[Symbol.asyncIterator]();
151
+ },
152
+ async pull(ctrl: any) {
153
+ try {
154
+ const { value, done } = await iter.next();
155
+ if (done) return ctrl.close();
156
+
157
+ const bytes = encodeUTF8(JSON.stringify(value) + '\n');
158
+
159
+ ctrl.enqueue(bytes);
160
+ } catch (err) {
161
+ ctrl.error(err);
162
+ }
163
+ },
164
+ async cancel() {
165
+ await iter.return?.();
166
+ },
167
+ });
168
+ }
169
+ }
170
+
171
+ export async function* _iterSSEMessages(
172
+ response: Response,
173
+ controller: AbortController,
174
+ ): AsyncGenerator<ServerSentEvent, void, unknown> {
175
+ if (!response.body) {
176
+ controller.abort();
177
+ if (
178
+ typeof (globalThis as any).navigator !== 'undefined' &&
179
+ (globalThis as any).navigator.product === 'ReactNative'
180
+ ) {
181
+ throw new OpencodeError(
182
+ `The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
183
+ );
184
+ }
185
+ throw new OpencodeError(`Attempted to iterate over a response with no body`);
186
+ }
187
+
188
+ const sseDecoder = new SSEDecoder();
189
+ const lineDecoder = new LineDecoder();
190
+
191
+ const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);
192
+ for await (const sseChunk of iterSSEChunks(iter)) {
193
+ for (const line of lineDecoder.decode(sseChunk)) {
194
+ const sse = sseDecoder.decode(line);
195
+ if (sse) yield sse;
196
+ }
197
+ }
198
+
199
+ for (const line of lineDecoder.flush()) {
200
+ const sse = sseDecoder.decode(line);
201
+ if (sse) yield sse;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Given an async iterable iterator, iterates over it and yields full
207
+ * SSE chunks, i.e. yields when a double new-line is encountered.
208
+ */
209
+ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
210
+ let data = new Uint8Array();
211
+
212
+ for await (const chunk of iterator) {
213
+ if (chunk == null) {
214
+ continue;
215
+ }
216
+
217
+ const binaryChunk =
218
+ chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
219
+ : typeof chunk === 'string' ? encodeUTF8(chunk)
220
+ : chunk;
221
+
222
+ let newData = new Uint8Array(data.length + binaryChunk.length);
223
+ newData.set(data);
224
+ newData.set(binaryChunk, data.length);
225
+ data = newData;
226
+
227
+ let patternIndex;
228
+ while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
229
+ yield data.slice(0, patternIndex);
230
+ data = data.slice(patternIndex);
231
+ }
232
+ }
233
+
234
+ if (data.length > 0) {
235
+ yield data;
236
+ }
237
+ }
238
+
239
+ class SSEDecoder {
240
+ private data: string[];
241
+ private event: string | null;
242
+ private chunks: string[];
243
+
244
+ constructor() {
245
+ this.event = null;
246
+ this.data = [];
247
+ this.chunks = [];
248
+ }
249
+
250
+ decode(line: string) {
251
+ if (line.endsWith('\r')) {
252
+ line = line.substring(0, line.length - 1);
253
+ }
254
+
255
+ if (!line) {
256
+ // empty line and we didn't previously encounter any messages
257
+ if (!this.event && !this.data.length) return null;
258
+
259
+ const sse: ServerSentEvent = {
260
+ event: this.event,
261
+ data: this.data.join('\n'),
262
+ raw: this.chunks,
263
+ };
264
+
265
+ this.event = null;
266
+ this.data = [];
267
+ this.chunks = [];
268
+
269
+ return sse;
270
+ }
271
+
272
+ this.chunks.push(line);
273
+
274
+ if (line.startsWith(':')) {
275
+ return null;
276
+ }
277
+
278
+ let [fieldname, _, value] = partition(line, ':');
279
+
280
+ if (value.startsWith(' ')) {
281
+ value = value.substring(1);
282
+ }
283
+
284
+ if (fieldname === 'event') {
285
+ this.event = value;
286
+ } else if (fieldname === 'data') {
287
+ this.data.push(value);
288
+ }
289
+
290
+ return null;
291
+ }
292
+ }
293
+
294
+ function partition(str: string, delimiter: string): [string, string, string] {
295
+ const index = str.indexOf(delimiter);
296
+ if (index !== -1) {
297
+ return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
298
+ }
299
+
300
+ return [str, '', ''];
301
+ }