@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.
- package/CHANGELOG.md +87 -0
- package/LICENSE +198 -4
- package/README.md +20 -6
- package/client.d.mts +15 -56
- package/client.d.mts.map +1 -1
- package/client.d.ts +15 -56
- package/client.d.ts.map +1 -1
- package/client.js +14 -67
- package/client.js.map +1 -1
- package/client.mjs +14 -67
- package/client.mjs.map +1 -1
- package/core/streaming.d.mts +31 -0
- package/core/streaming.d.mts.map +1 -0
- package/core/streaming.d.ts +31 -0
- package/core/streaming.d.ts.map +1 -0
- package/core/streaming.js +257 -0
- package/core/streaming.js.map +1 -0
- package/core/streaming.mjs +252 -0
- package/core/streaming.mjs.map +1 -0
- package/internal/decoders/line.d.mts +17 -0
- package/internal/decoders/line.d.mts.map +1 -0
- package/internal/decoders/line.d.ts +17 -0
- package/internal/decoders/line.d.ts.map +1 -0
- package/internal/decoders/line.js +113 -0
- package/internal/decoders/line.js.map +1 -0
- package/internal/decoders/line.mjs +108 -0
- package/internal/decoders/line.mjs.map +1 -0
- package/internal/parse.d.mts.map +1 -1
- package/internal/parse.d.ts.map +1 -1
- package/internal/parse.js +10 -0
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs +10 -0
- package/internal/parse.mjs.map +1 -1
- package/internal/request-options.d.mts +2 -0
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts +2 -0
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs.map +1 -1
- package/internal/tslib.js +6 -6
- package/internal/types.d.mts +8 -6
- package/internal/types.d.mts.map +1 -1
- package/internal/types.d.ts +8 -6
- package/internal/types.d.ts.map +1 -1
- package/internal/uploads.js +1 -1
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +1 -1
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/log.js +1 -1
- package/internal/utils/log.js.map +1 -1
- package/internal/utils/log.mjs +1 -1
- package/internal/utils/log.mjs.map +1 -1
- package/internal/utils/path.d.mts.map +1 -1
- package/internal/utils/path.d.ts.map +1 -1
- package/internal/utils/path.js +26 -5
- package/internal/utils/path.js.map +1 -1
- package/internal/utils/path.mjs +26 -5
- package/internal/utils/path.mjs.map +1 -1
- package/package.json +13 -3
- package/resources/app.d.mts +37 -0
- package/resources/app.d.mts.map +1 -0
- package/resources/app.d.ts +37 -0
- package/resources/app.d.ts.map +1 -0
- package/resources/app.js +21 -0
- package/resources/app.js.map +1 -0
- package/resources/app.mjs +17 -0
- package/resources/app.mjs.map +1 -0
- package/resources/config.d.mts +314 -0
- package/resources/config.d.mts.map +1 -0
- package/resources/config.d.ts +314 -0
- package/resources/config.d.ts.map +1 -0
- package/resources/config.js +21 -0
- package/resources/config.js.map +1 -0
- package/resources/config.mjs +17 -0
- package/resources/config.mjs.map +1 -0
- package/resources/event.d.mts +61 -27
- package/resources/event.d.mts.map +1 -1
- package/resources/event.d.ts +61 -27
- package/resources/event.d.ts.map +1 -1
- package/resources/event.js +1 -1
- package/resources/event.js.map +1 -1
- package/resources/event.mjs +1 -1
- package/resources/event.mjs.map +1 -1
- package/resources/file.d.mts +17 -0
- package/resources/file.d.mts.map +1 -0
- package/resources/file.d.ts +17 -0
- package/resources/file.d.ts.map +1 -0
- package/resources/{file-search.js → file.js} +6 -6
- package/resources/file.js.map +1 -0
- package/resources/{file-search.mjs → file.mjs} +4 -4
- package/resources/file.mjs.map +1 -0
- package/resources/index.d.mts +5 -16
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +5 -16
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +11 -33
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +5 -16
- package/resources/index.mjs.map +1 -1
- package/resources/session.d.mts +207 -0
- package/resources/session.d.mts.map +1 -0
- package/resources/session.d.ts +207 -0
- package/resources/session.d.ts.map +1 -0
- package/resources/session.js +70 -0
- package/resources/session.js.map +1 -0
- package/resources/session.mjs +66 -0
- package/resources/session.mjs.map +1 -0
- package/resources/shared.d.mts +20 -0
- package/resources/shared.d.mts.map +1 -0
- package/resources/shared.d.ts +20 -0
- package/resources/shared.d.ts.map +1 -0
- package/resources/shared.js +4 -0
- package/resources/shared.js.map +1 -0
- package/resources/shared.mjs +3 -0
- package/resources/shared.mjs.map +1 -0
- package/src/client.ts +79 -156
- package/src/core/streaming.ts +301 -0
- package/src/internal/decoders/line.ts +135 -0
- package/src/internal/parse.ts +14 -0
- package/src/internal/request-options.ts +2 -0
- package/src/internal/types.ts +9 -6
- package/src/internal/uploads.ts +1 -1
- package/src/internal/utils/log.ts +1 -1
- package/src/internal/utils/path.ts +32 -7
- package/src/resources/app.ts +57 -0
- package/src/resources/config.ts +404 -0
- package/src/resources/event.ts +84 -36
- package/src/resources/file.ts +24 -0
- package/src/resources/index.ts +35 -33
- package/src/resources/session.ts +355 -0
- package/src/resources/shared.ts +27 -0
- package/src/streaming.ts +2 -0
- package/src/version.ts +1 -1
- package/streaming.d.mts +2 -0
- package/streaming.d.mts.map +1 -0
- package/streaming.d.ts +2 -0
- package/streaming.d.ts.map +1 -0
- package/streaming.js +6 -0
- package/streaming.js.map +1 -0
- package/streaming.mjs +2 -0
- package/streaming.mjs.map +1 -0
- package/version.d.mts +1 -1
- package/version.d.mts.map +1 -1
- package/version.d.ts +1 -1
- package/version.d.ts.map +1 -1
- package/version.js +1 -1
- package/version.js.map +1 -1
- package/version.mjs +1 -1
- package/version.mjs.map +1 -1
- package/resources/app-info.d.mts +0 -31
- package/resources/app-info.d.mts.map +0 -1
- package/resources/app-info.d.ts +0 -31
- package/resources/app-info.d.ts.map +0 -1
- package/resources/app-info.js +0 -15
- package/resources/app-info.js.map +0 -1
- package/resources/app-info.mjs +0 -11
- package/resources/app-info.mjs.map +0 -1
- package/resources/app-initialize.d.mts +0 -14
- package/resources/app-initialize.d.mts.map +0 -1
- package/resources/app-initialize.d.ts +0 -14
- package/resources/app-initialize.d.ts.map +0 -1
- package/resources/app-initialize.js +0 -15
- package/resources/app-initialize.js.map +0 -1
- package/resources/app-initialize.mjs +0 -11
- package/resources/app-initialize.mjs.map +0 -1
- package/resources/config-get.d.mts +0 -236
- package/resources/config-get.d.mts.map +0 -1
- package/resources/config-get.d.ts +0 -236
- package/resources/config-get.d.ts.map +0 -1
- package/resources/config-get.js +0 -15
- package/resources/config-get.js.map +0 -1
- package/resources/config-get.mjs +0 -11
- package/resources/config-get.mjs.map +0 -1
- package/resources/file-search.d.mts +0 -17
- package/resources/file-search.d.mts.map +0 -1
- package/resources/file-search.d.ts +0 -17
- package/resources/file-search.d.ts.map +0 -1
- package/resources/file-search.js.map +0 -1
- package/resources/file-search.mjs.map +0 -1
- package/resources/installation-info.d.mts +0 -17
- package/resources/installation-info.d.mts.map +0 -1
- package/resources/installation-info.d.ts +0 -17
- package/resources/installation-info.d.ts.map +0 -1
- package/resources/installation-info.js +0 -15
- package/resources/installation-info.js.map +0 -1
- package/resources/installation-info.mjs +0 -11
- package/resources/installation-info.mjs.map +0 -1
- package/resources/path-get.d.mts +0 -19
- package/resources/path-get.d.mts.map +0 -1
- package/resources/path-get.d.ts +0 -19
- package/resources/path-get.d.ts.map +0 -1
- package/resources/path-get.js +0 -15
- package/resources/path-get.js.map +0 -1
- package/resources/path-get.mjs +0 -11
- package/resources/path-get.mjs.map +0 -1
- package/resources/provider-list.d.mts +0 -58
- package/resources/provider-list.d.mts.map +0 -1
- package/resources/provider-list.d.ts +0 -58
- package/resources/provider-list.d.ts.map +0 -1
- package/resources/provider-list.js +0 -15
- package/resources/provider-list.js.map +0 -1
- package/resources/provider-list.mjs +0 -11
- package/resources/provider-list.mjs.map +0 -1
- package/resources/session-abort.d.mts +0 -17
- package/resources/session-abort.d.mts.map +0 -1
- package/resources/session-abort.d.ts +0 -17
- package/resources/session-abort.d.ts.map +0 -1
- package/resources/session-abort.js +0 -15
- package/resources/session-abort.js.map +0 -1
- package/resources/session-abort.mjs +0 -11
- package/resources/session-abort.mjs.map +0 -1
- package/resources/session-chat.d.mts +0 -160
- package/resources/session-chat.d.mts.map +0 -1
- package/resources/session-chat.d.ts +0 -160
- package/resources/session-chat.d.ts.map +0 -1
- package/resources/session-chat.js +0 -15
- package/resources/session-chat.js.map +0 -1
- package/resources/session-chat.mjs +0 -11
- package/resources/session-chat.mjs.map +0 -1
- package/resources/session-create.d.mts +0 -30
- package/resources/session-create.d.mts.map +0 -1
- package/resources/session-create.d.ts +0 -30
- package/resources/session-create.d.ts.map +0 -1
- package/resources/session-create.js +0 -15
- package/resources/session-create.js.map +0 -1
- package/resources/session-create.mjs +0 -11
- package/resources/session-create.mjs.map +0 -1
- package/resources/session-initialize.d.mts +0 -19
- package/resources/session-initialize.d.mts.map +0 -1
- package/resources/session-initialize.d.ts +0 -19
- package/resources/session-initialize.d.ts.map +0 -1
- package/resources/session-initialize.js +0 -15
- package/resources/session-initialize.js.map +0 -1
- package/resources/session-initialize.mjs +0 -11
- package/resources/session-initialize.mjs.map +0 -1
- package/resources/session-list.d.mts +0 -15
- package/resources/session-list.d.mts.map +0 -1
- package/resources/session-list.d.ts +0 -15
- package/resources/session-list.d.ts.map +0 -1
- package/resources/session-list.js +0 -15
- package/resources/session-list.js.map +0 -1
- package/resources/session-list.mjs +0 -11
- package/resources/session-list.mjs.map +0 -1
- package/resources/session-messages.d.mts +0 -18
- package/resources/session-messages.d.mts.map +0 -1
- package/resources/session-messages.d.ts +0 -18
- package/resources/session-messages.d.ts.map +0 -1
- package/resources/session-messages.js +0 -15
- package/resources/session-messages.js.map +0 -1
- package/resources/session-messages.mjs +0 -11
- package/resources/session-messages.mjs.map +0 -1
- package/resources/session-share.d.mts +0 -17
- package/resources/session-share.d.mts.map +0 -1
- package/resources/session-share.d.ts +0 -17
- package/resources/session-share.d.ts.map +0 -1
- package/resources/session-share.js +0 -15
- package/resources/session-share.js.map +0 -1
- package/resources/session-share.mjs +0 -11
- package/resources/session-share.mjs.map +0 -1
- package/resources/session-summarize.d.mts +0 -19
- package/resources/session-summarize.d.mts.map +0 -1
- package/resources/session-summarize.d.ts +0 -19
- package/resources/session-summarize.d.ts.map +0 -1
- package/resources/session-summarize.js +0 -15
- package/resources/session-summarize.js.map +0 -1
- package/resources/session-summarize.mjs +0 -11
- package/resources/session-summarize.mjs.map +0 -1
- package/resources/session-unshare.d.mts +0 -17
- package/resources/session-unshare.d.mts.map +0 -1
- package/resources/session-unshare.d.ts +0 -17
- package/resources/session-unshare.d.ts.map +0 -1
- package/resources/session-unshare.js +0 -15
- package/resources/session-unshare.js.map +0 -1
- package/resources/session-unshare.mjs +0 -11
- package/resources/session-unshare.mjs.map +0 -1
- package/src/resources/app-info.ts +0 -46
- package/src/resources/app-initialize.ts +0 -20
- package/src/resources/config-get.ts +0 -294
- package/src/resources/file-search.ts +0 -27
- package/src/resources/installation-info.ts +0 -24
- package/src/resources/path-get.ts +0 -28
- package/src/resources/provider-list.ts +0 -80
- package/src/resources/session-abort.ts +0 -27
- package/src/resources/session-chat.ts +0 -250
- package/src/resources/session-create.ts +0 -44
- package/src/resources/session-initialize.ts +0 -34
- package/src/resources/session-list.ts +0 -21
- package/src/resources/session-messages.ts +0 -31
- package/src/resources/session-share.ts +0 -23
- package/src/resources/session-summarize.ts +0 -34
- 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 {
|
|
20
|
-
import {
|
|
21
|
-
|
|
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 {
|
|
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
|
-
|
|
33
|
+
FilePart,
|
|
34
|
+
Message,
|
|
30
35
|
MessagePart,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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.
|
|
773
|
-
Opencode.
|
|
774
|
-
Opencode.
|
|
775
|
-
Opencode.
|
|
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 {
|
|
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
|
-
|
|
799
|
-
type
|
|
740
|
+
File as File,
|
|
741
|
+
type FileSearchResponse as FileSearchResponse,
|
|
742
|
+
type FileSearchParams as FileSearchParams,
|
|
800
743
|
};
|
|
801
744
|
|
|
802
745
|
export {
|
|
803
|
-
|
|
804
|
-
type
|
|
805
|
-
type
|
|
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
|
-
|
|
816
|
-
type
|
|
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
|
|
844
|
-
type
|
|
845
|
-
type
|
|
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
|
|
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
|
+
}
|