opencode-ext-connector 0.3.0
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 +39 -0
- package/LICENSE +29 -0
- package/README.md +207 -0
- package/THIRD_PARTY_NOTICES.md +201 -0
- package/dist/catalog/parse-ids.d.ts +2 -0
- package/dist/catalog/parse-ids.js +49 -0
- package/dist/core/adapter.d.ts +16 -0
- package/dist/core/adapter.js +20 -0
- package/dist/core/clock.d.ts +7 -0
- package/dist/core/clock.js +1 -0
- package/dist/core/deadline.d.ts +13 -0
- package/dist/core/deadline.js +54 -0
- package/dist/core/errors.d.ts +57 -0
- package/dist/core/errors.js +81 -0
- package/dist/core/health.d.ts +22 -0
- package/dist/core/health.js +30 -0
- package/dist/core/http.d.ts +28 -0
- package/dist/core/http.js +1 -0
- package/dist/core/ids.d.ts +7 -0
- package/dist/core/ids.js +23 -0
- package/dist/core/lifecycle.d.ts +4 -0
- package/dist/core/lifecycle.js +16 -0
- package/dist/core/logger.d.ts +21 -0
- package/dist/core/logger.js +61 -0
- package/dist/core/models.d.ts +41 -0
- package/dist/core/models.js +59 -0
- package/dist/core/options.d.ts +31 -0
- package/dist/core/options.js +62 -0
- package/dist/core/process.d.ts +20 -0
- package/dist/core/process.js +1 -0
- package/dist/http/fetch-transport.d.ts +2 -0
- package/dist/http/fetch-transport.js +75 -0
- package/dist/http/package-version.d.ts +7 -0
- package/dist/http/package-version.js +46 -0
- package/dist/http/read-body.d.ts +9 -0
- package/dist/http/read-body.js +22 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/logging/logger.d.ts +4 -0
- package/dist/logging/logger.js +11 -0
- package/dist/opencode/auth-store.d.ts +19 -0
- package/dist/opencode/auth-store.js +107 -0
- package/dist/opencode/beta-api.d.ts +3 -0
- package/dist/opencode/beta-api.js +1 -0
- package/dist/opencode/catalog-bridge.d.ts +3 -0
- package/dist/opencode/catalog-bridge.js +85 -0
- package/dist/opencode/health-refresh.d.ts +16 -0
- package/dist/opencode/health-refresh.js +49 -0
- package/dist/opencode/host-options.d.ts +2 -0
- package/dist/opencode/host-options.js +49 -0
- package/dist/opencode/language-factory.d.ts +13 -0
- package/dist/opencode/language-factory.js +37 -0
- package/dist/opencode/ollama-probe.d.ts +2 -0
- package/dist/opencode/ollama-probe.js +15 -0
- package/dist/opencode/ollama-production.d.ts +4 -0
- package/dist/opencode/ollama-production.js +10 -0
- package/dist/opencode/plugin.d.ts +33 -0
- package/dist/opencode/plugin.js +40 -0
- package/dist/opencode/process-exit.d.ts +4 -0
- package/dist/opencode/process-exit.js +22 -0
- package/dist/opencode/provider-entry.d.ts +25 -0
- package/dist/opencode/provider-entry.js +1 -0
- package/dist/opencode/providers.d.ts +16 -0
- package/dist/opencode/providers.js +140 -0
- package/dist/opencode/register-integrations.d.ts +3 -0
- package/dist/opencode/register-integrations.js +8 -0
- package/dist/opencode/reload.d.ts +9 -0
- package/dist/opencode/reload.js +30 -0
- package/dist/opencode/v1-anthropic-auth.d.ts +9 -0
- package/dist/opencode/v1-anthropic-auth.js +55 -0
- package/dist/opencode/v1-catalog.d.ts +13 -0
- package/dist/opencode/v1-catalog.js +80 -0
- package/dist/opencode/v1-language.d.ts +3 -0
- package/dist/opencode/v1-language.js +64 -0
- package/dist/opencode/v1-module.d.ts +23 -0
- package/dist/opencode/v1-module.js +74 -0
- package/dist/opencode/v1-session-auth.d.ts +5 -0
- package/dist/opencode/v1-session-auth.js +98 -0
- package/dist/providers/claude/adapter.d.ts +7 -0
- package/dist/providers/claude/adapter.js +37 -0
- package/dist/providers/claude/atomic-private-file.d.ts +13 -0
- package/dist/providers/claude/atomic-private-file.js +94 -0
- package/dist/providers/claude/auth-json.d.ts +2 -0
- package/dist/providers/claude/auth-json.js +14 -0
- package/dist/providers/claude/auth.d.ts +24 -0
- package/dist/providers/claude/auth.js +200 -0
- package/dist/providers/claude/cli-version.d.ts +10 -0
- package/dist/providers/claude/cli-version.js +36 -0
- package/dist/providers/claude/compat-betas.d.ts +5 -0
- package/dist/providers/claude/compat-betas.js +54 -0
- package/dist/providers/claude/compat-config.d.ts +8 -0
- package/dist/providers/claude/compat-config.js +31 -0
- package/dist/providers/claude/compat-request.d.ts +12 -0
- package/dist/providers/claude/compat-request.js +151 -0
- package/dist/providers/claude/compat-response.d.ts +1 -0
- package/dist/providers/claude/compat-response.js +54 -0
- package/dist/providers/claude/compat-signing.d.ts +9 -0
- package/dist/providers/claude/compat-signing.js +24 -0
- package/dist/providers/claude/compat-transform.d.ts +3 -0
- package/dist/providers/claude/compat-transform.js +224 -0
- package/dist/providers/claude/credentials.d.ts +7 -0
- package/dist/providers/claude/credentials.js +37 -0
- package/dist/providers/claude/emit-stream.d.ts +2 -0
- package/dist/providers/claude/emit-stream.js +62 -0
- package/dist/providers/claude/keychain-account.d.ts +1 -0
- package/dist/providers/claude/keychain-account.js +14 -0
- package/dist/providers/claude/language-model.d.ts +8 -0
- package/dist/providers/claude/language-model.js +109 -0
- package/dist/providers/claude/models.d.ts +9 -0
- package/dist/providers/claude/models.js +24 -0
- package/dist/providers/claude/prompt.d.ts +42 -0
- package/dist/providers/claude/prompt.js +134 -0
- package/dist/providers/claude/refresh.d.ts +19 -0
- package/dist/providers/claude/refresh.js +85 -0
- package/dist/providers/claude/sse-convert.d.ts +6 -0
- package/dist/providers/claude/sse-convert.js +74 -0
- package/dist/providers/claude/sse.d.ts +71 -0
- package/dist/providers/claude/sse.js +123 -0
- package/dist/providers/claude/transform.d.ts +32 -0
- package/dist/providers/claude/transform.js +52 -0
- package/dist/providers/claude/writeback.d.ts +18 -0
- package/dist/providers/claude/writeback.js +140 -0
- package/dist/providers/command-code/adapter.d.ts +7 -0
- package/dist/providers/command-code/adapter.js +37 -0
- package/dist/providers/command-code/auth.d.ts +5 -0
- package/dist/providers/command-code/auth.js +88 -0
- package/dist/providers/command-code/cli-version.d.ts +10 -0
- package/dist/providers/command-code/cli-version.js +36 -0
- package/dist/providers/command-code/emit-stream.d.ts +2 -0
- package/dist/providers/command-code/emit-stream.js +226 -0
- package/dist/providers/command-code/errors.d.ts +33 -0
- package/dist/providers/command-code/errors.js +105 -0
- package/dist/providers/command-code/language-model.d.ts +15 -0
- package/dist/providers/command-code/language-model.js +203 -0
- package/dist/providers/command-code/models.d.ts +3 -0
- package/dist/providers/command-code/models.js +18 -0
- package/dist/providers/command-code/record-stream.d.ts +1 -0
- package/dist/providers/command-code/record-stream.js +61 -0
- package/dist/providers/command-code/request-lifecycle.d.ts +8 -0
- package/dist/providers/command-code/request-lifecycle.js +33 -0
- package/dist/providers/command-code/request.d.ts +14 -0
- package/dist/providers/command-code/request.js +167 -0
- package/dist/providers/command-code/session.d.ts +5 -0
- package/dist/providers/command-code/session.js +9 -0
- package/dist/providers/cursor/adapter.d.ts +7 -0
- package/dist/providers/cursor/adapter.js +37 -0
- package/dist/providers/cursor/auth.d.ts +6 -0
- package/dist/providers/cursor/auth.js +56 -0
- package/dist/providers/cursor/blob-store.d.ts +26 -0
- package/dist/providers/cursor/blob-store.js +119 -0
- package/dist/providers/cursor/bridge-client.d.ts +33 -0
- package/dist/providers/cursor/bridge-client.js +200 -0
- package/dist/providers/cursor/bridge-event-sanitize.d.ts +3 -0
- package/dist/providers/cursor/bridge-event-sanitize.js +49 -0
- package/dist/providers/cursor/bridge-limits.d.ts +3 -0
- package/dist/providers/cursor/bridge-limits.js +10 -0
- package/dist/providers/cursor/bridge-process.d.ts +45 -0
- package/dist/providers/cursor/bridge-process.js +211 -0
- package/dist/providers/cursor/bridge-protocol.d.ts +68 -0
- package/dist/providers/cursor/bridge-protocol.js +49 -0
- package/dist/providers/cursor/bridge-serialize.d.ts +3 -0
- package/dist/providers/cursor/bridge-serialize.js +53 -0
- package/dist/providers/cursor/bridge-wire.d.ts +3 -0
- package/dist/providers/cursor/bridge-wire.js +163 -0
- package/dist/providers/cursor/checkpoint-store.d.ts +28 -0
- package/dist/providers/cursor/checkpoint-store.js +94 -0
- package/dist/providers/cursor/connect-frame-stream.d.ts +19 -0
- package/dist/providers/cursor/connect-frame-stream.js +160 -0
- package/dist/providers/cursor/connect-frame.d.ts +31 -0
- package/dist/providers/cursor/connect-frame.js +121 -0
- package/dist/providers/cursor/credentials.d.ts +5 -0
- package/dist/providers/cursor/credentials.js +22 -0
- package/dist/providers/cursor/direct-run-types.d.ts +33 -0
- package/dist/providers/cursor/direct-run-types.js +1 -0
- package/dist/providers/cursor/direct-run.d.ts +5 -0
- package/dist/providers/cursor/direct-run.js +213 -0
- package/dist/providers/cursor/direct-runtime.d.ts +23 -0
- package/dist/providers/cursor/direct-runtime.js +111 -0
- package/dist/providers/cursor/direct-stream.d.ts +28 -0
- package/dist/providers/cursor/direct-stream.js +190 -0
- package/dist/providers/cursor/exec-reply.d.ts +13 -0
- package/dist/providers/cursor/exec-reply.js +102 -0
- package/dist/providers/cursor/h2-bridge-response.d.ts +9 -0
- package/dist/providers/cursor/h2-bridge-response.js +51 -0
- package/dist/providers/cursor/h2-bridge-session.d.ts +24 -0
- package/dist/providers/cursor/h2-bridge-session.js +197 -0
- package/dist/providers/cursor/h2-bridge.d.ts +2 -0
- package/dist/providers/cursor/h2-bridge.js +12865 -0
- package/dist/providers/cursor/http2.d.ts +18 -0
- package/dist/providers/cursor/http2.js +144 -0
- package/dist/providers/cursor/interaction-reply.d.ts +6 -0
- package/dist/providers/cursor/interaction-reply.js +52 -0
- package/dist/providers/cursor/language-generate.d.ts +2 -0
- package/dist/providers/cursor/language-generate.js +35 -0
- package/dist/providers/cursor/language-model.d.ts +13 -0
- package/dist/providers/cursor/language-model.js +77 -0
- package/dist/providers/cursor/legacy-stream.d.ts +11 -0
- package/dist/providers/cursor/legacy-stream.js +115 -0
- package/dist/providers/cursor/mcp-tools.d.ts +7 -0
- package/dist/providers/cursor/mcp-tools.js +15 -0
- package/dist/providers/cursor/models.d.ts +14 -0
- package/dist/providers/cursor/models.js +116 -0
- package/dist/providers/cursor/ndjson.d.ts +3 -0
- package/dist/providers/cursor/ndjson.js +100 -0
- package/dist/providers/cursor/prompt.d.ts +4 -0
- package/dist/providers/cursor/prompt.js +142 -0
- package/dist/providers/cursor/proto/checkpoint.d.ts +35 -0
- package/dist/providers/cursor/proto/checkpoint.js +114 -0
- package/dist/providers/cursor/proto/context.d.ts +64 -0
- package/dist/providers/cursor/proto/context.js +178 -0
- package/dist/providers/cursor/proto/errors.d.ts +16 -0
- package/dist/providers/cursor/proto/errors.js +26 -0
- package/dist/providers/cursor/proto/exec-control.d.ts +20 -0
- package/dist/providers/cursor/proto/exec-control.js +54 -0
- package/dist/providers/cursor/proto/exec.d.ts +47 -0
- package/dist/providers/cursor/proto/exec.js +154 -0
- package/dist/providers/cursor/proto/fields.d.ts +22 -0
- package/dist/providers/cursor/proto/fields.js +97 -0
- package/dist/providers/cursor/proto/interaction-query.d.ts +15 -0
- package/dist/providers/cursor/proto/interaction-query.js +79 -0
- package/dist/providers/cursor/proto/interaction-tool-update.d.ts +10 -0
- package/dist/providers/cursor/proto/interaction-tool-update.js +58 -0
- package/dist/providers/cursor/proto/interaction-turn-ended.d.ts +6 -0
- package/dist/providers/cursor/proto/interaction-turn-ended.js +18 -0
- package/dist/providers/cursor/proto/interaction.d.ts +65 -0
- package/dist/providers/cursor/proto/interaction.js +185 -0
- package/dist/providers/cursor/proto/kv.d.ts +25 -0
- package/dist/providers/cursor/proto/kv.js +109 -0
- package/dist/providers/cursor/proto/mcp-result.d.ts +40 -0
- package/dist/providers/cursor/proto/mcp-result.js +150 -0
- package/dist/providers/cursor/proto/mcp.d.ts +19 -0
- package/dist/providers/cursor/proto/mcp.js +78 -0
- package/dist/providers/cursor/proto/model.d.ts +11 -0
- package/dist/providers/cursor/proto/model.js +36 -0
- package/dist/providers/cursor/proto/output-location.d.ts +7 -0
- package/dist/providers/cursor/proto/output-location.js +53 -0
- package/dist/providers/cursor/proto/prewarm.d.ts +16 -0
- package/dist/providers/cursor/proto/prewarm.js +68 -0
- package/dist/providers/cursor/proto/request-context.d.ts +16 -0
- package/dist/providers/cursor/proto/request-context.js +58 -0
- package/dist/providers/cursor/proto/request.d.ts +44 -0
- package/dist/providers/cursor/proto/request.js +111 -0
- package/dist/providers/cursor/proto/run-request.d.ts +19 -0
- package/dist/providers/cursor/proto/run-request.js +68 -0
- package/dist/providers/cursor/proto/server.d.ts +35 -0
- package/dist/providers/cursor/proto/server.js +93 -0
- package/dist/providers/cursor/proto/unknown-field.d.ts +1 -0
- package/dist/providers/cursor/proto/unknown-field.js +22 -0
- package/dist/providers/cursor/proto-value.d.ts +3 -0
- package/dist/providers/cursor/proto-value.js +170 -0
- package/dist/providers/cursor/proto-wire.d.ts +22 -0
- package/dist/providers/cursor/proto-wire.js +224 -0
- package/dist/providers/cursor/protocol-failure.d.ts +18 -0
- package/dist/providers/cursor/protocol-failure.js +45 -0
- package/dist/providers/cursor/recovery.d.ts +48 -0
- package/dist/providers/cursor/recovery.js +102 -0
- package/dist/providers/cursor/request-blob-ownership.d.ts +15 -0
- package/dist/providers/cursor/request-blob-ownership.js +39 -0
- package/dist/providers/cursor/request-build.d.ts +18 -0
- package/dist/providers/cursor/request-build.js +123 -0
- package/dist/providers/cursor/request-history.d.ts +32 -0
- package/dist/providers/cursor/request-history.js +163 -0
- package/dist/providers/cursor/request-input.d.ts +75 -0
- package/dist/providers/cursor/request-input.js +123 -0
- package/dist/providers/cursor/run-events.d.ts +15 -0
- package/dist/providers/cursor/run-events.js +43 -0
- package/dist/providers/cursor/run-session-expiry.d.ts +15 -0
- package/dist/providers/cursor/run-session-expiry.js +21 -0
- package/dist/providers/cursor/run-session.d.ts +50 -0
- package/dist/providers/cursor/run-session.js +205 -0
- package/dist/providers/cursor/run.d.ts +9 -0
- package/dist/providers/cursor/run.js +151 -0
- package/dist/providers/cursor/runner.d.ts +1 -0
- package/dist/providers/cursor/runner.js +55 -0
- package/dist/providers/cursor/server-dispatch-types.d.ts +83 -0
- package/dist/providers/cursor/server-dispatch-types.js +1 -0
- package/dist/providers/cursor/server-dispatch.d.ts +3 -0
- package/dist/providers/cursor/server-dispatch.js +178 -0
- package/dist/providers/cursor/session-state.d.ts +21 -0
- package/dist/providers/cursor/session-state.js +118 -0
- package/dist/providers/cursor/session.d.ts +1 -0
- package/dist/providers/cursor/session.js +26 -0
- package/dist/providers/cursor/settle-cleanup.d.ts +1 -0
- package/dist/providers/cursor/settle-cleanup.js +11 -0
- package/dist/providers/cursor/stream-adapter.d.ts +17 -0
- package/dist/providers/cursor/stream-adapter.js +99 -0
- package/dist/providers/cursor/tool-continuation.d.ts +14 -0
- package/dist/providers/cursor/tool-continuation.js +134 -0
- package/dist/providers/cursor/tool-stream.d.ts +2 -0
- package/dist/providers/cursor/tool-stream.js +220 -0
- package/dist/providers/cursor/usage.d.ts +3 -0
- package/dist/providers/cursor/usage.js +35 -0
- package/dist/providers/ollama/adapter.d.ts +8 -0
- package/dist/providers/ollama/adapter.js +65 -0
- package/dist/providers/ollama/catalog-state.d.ts +17 -0
- package/dist/providers/ollama/catalog-state.js +40 -0
- package/dist/providers/ollama/cloud-catalog.d.ts +3 -0
- package/dist/providers/ollama/cloud-catalog.js +82 -0
- package/dist/providers/ollama/errors.d.ts +14 -0
- package/dist/providers/ollama/errors.js +19 -0
- package/dist/providers/ollama/generate.d.ts +2 -0
- package/dist/providers/ollama/generate.js +49 -0
- package/dist/providers/ollama/html-links.d.ts +1 -0
- package/dist/providers/ollama/html-links.js +30 -0
- package/dist/providers/ollama/http.d.ts +12 -0
- package/dist/providers/ollama/http.js +67 -0
- package/dist/providers/ollama/index.d.ts +8 -0
- package/dist/providers/ollama/index.js +8 -0
- package/dist/providers/ollama/language-model.d.ts +11 -0
- package/dist/providers/ollama/language-model.js +62 -0
- package/dist/providers/ollama/local-catalog.d.ts +3 -0
- package/dist/providers/ollama/local-catalog.js +38 -0
- package/dist/providers/ollama/ndjson.d.ts +3 -0
- package/dist/providers/ollama/ndjson.js +51 -0
- package/dist/providers/ollama/prompt.d.ts +8 -0
- package/dist/providers/ollama/prompt.js +141 -0
- package/dist/providers/ollama/protocol.d.ts +56 -0
- package/dist/providers/ollama/protocol.js +26 -0
- package/dist/providers/ollama/runtime.d.ts +11 -0
- package/dist/providers/ollama/runtime.js +128 -0
- package/dist/providers/ollama/stream.d.ts +8 -0
- package/dist/providers/ollama/stream.js +110 -0
- package/dist/sdk/command-code.d.ts +5 -0
- package/dist/sdk/command-code.js +6 -0
- package/dist/sdk/cursor.d.ts +5 -0
- package/dist/sdk/cursor.js +6 -0
- package/dist/sdk/ollama.d.ts +5 -0
- package/dist/sdk/ollama.js +6 -0
- package/dist/server.d.ts +12 -0
- package/dist/server.js +94 -0
- package/docs/README.ko.md +189 -0
- package/package.json +75 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Derived from brent-weatherall/opencode-commandcode-provider@6cf3f22d4aae469db3723e589291c736285373c1 src/stream.ts.
|
|
2
|
+
// Licensed under MIT. See THIRD_PARTY_NOTICES.md.
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { commandCodeNdjsonError } from "./errors";
|
|
5
|
+
import { commandCodeRecords } from "./record-stream";
|
|
6
|
+
const tokenDetailsSchema = z
|
|
7
|
+
.object({
|
|
8
|
+
noCacheTokens: z.number().optional(),
|
|
9
|
+
cacheReadTokens: z.number().optional(),
|
|
10
|
+
cacheWriteTokens: z.number().optional(),
|
|
11
|
+
textTokens: z.number().optional(),
|
|
12
|
+
reasoningTokens: z.number().optional(),
|
|
13
|
+
})
|
|
14
|
+
.passthrough();
|
|
15
|
+
const usageSchema = z
|
|
16
|
+
.object({
|
|
17
|
+
inputTokens: z.number().optional(),
|
|
18
|
+
outputTokens: z.number().optional(),
|
|
19
|
+
inputTokenDetails: tokenDetailsSchema.optional(),
|
|
20
|
+
outputTokenDetails: tokenDetailsSchema.optional(),
|
|
21
|
+
})
|
|
22
|
+
.passthrough();
|
|
23
|
+
const providerErrorSchema = z
|
|
24
|
+
.object({
|
|
25
|
+
message: z.string(),
|
|
26
|
+
code: z.string().optional(),
|
|
27
|
+
statusCode: z.number().int().min(100).max(599).optional(),
|
|
28
|
+
isRetryable: z.boolean().optional(),
|
|
29
|
+
})
|
|
30
|
+
.passthrough()
|
|
31
|
+
.transform((error) => ({
|
|
32
|
+
...(error.code === undefined ? {} : { code: error.code }),
|
|
33
|
+
...(error.statusCode === undefined ? {} : { statusCode: error.statusCode }),
|
|
34
|
+
...(error.isRetryable === undefined ? {} : { isRetryable: error.isRetryable }),
|
|
35
|
+
}));
|
|
36
|
+
const streamEventSchema = z.discriminatedUnion("type", [
|
|
37
|
+
z.object({ type: z.literal("start") }).passthrough(),
|
|
38
|
+
z.object({ type: z.literal("abort") }).passthrough(),
|
|
39
|
+
z
|
|
40
|
+
.object({
|
|
41
|
+
type: z.enum([
|
|
42
|
+
"text-start",
|
|
43
|
+
"text-end",
|
|
44
|
+
"reasoning-start",
|
|
45
|
+
"reasoning-end",
|
|
46
|
+
"tool-input-end",
|
|
47
|
+
]),
|
|
48
|
+
id: z.string().optional(),
|
|
49
|
+
})
|
|
50
|
+
.passthrough(),
|
|
51
|
+
z
|
|
52
|
+
.object({
|
|
53
|
+
type: z.enum(["text-delta", "reasoning-delta", "tool-input-delta"]),
|
|
54
|
+
id: z.string().optional(),
|
|
55
|
+
text: z.string().optional(),
|
|
56
|
+
delta: z.string().optional(),
|
|
57
|
+
})
|
|
58
|
+
.passthrough(),
|
|
59
|
+
z
|
|
60
|
+
.object({
|
|
61
|
+
type: z.literal("tool-input-start"),
|
|
62
|
+
id: z.string().optional(),
|
|
63
|
+
toolName: z.string().optional(),
|
|
64
|
+
dynamic: z.boolean().optional(),
|
|
65
|
+
})
|
|
66
|
+
.passthrough(),
|
|
67
|
+
z
|
|
68
|
+
.object({
|
|
69
|
+
type: z.literal("tool-call"),
|
|
70
|
+
toolCallId: z.string().optional(),
|
|
71
|
+
id: z.string().optional(),
|
|
72
|
+
toolName: z.string().optional(),
|
|
73
|
+
input: z.unknown().optional(),
|
|
74
|
+
args: z.unknown().optional(),
|
|
75
|
+
arguments: z.unknown().optional(),
|
|
76
|
+
})
|
|
77
|
+
.passthrough(),
|
|
78
|
+
z
|
|
79
|
+
.object({
|
|
80
|
+
type: z.literal("finish-step"),
|
|
81
|
+
finishReason: z.string().optional(),
|
|
82
|
+
usage: usageSchema.optional(),
|
|
83
|
+
})
|
|
84
|
+
.passthrough(),
|
|
85
|
+
z
|
|
86
|
+
.object({
|
|
87
|
+
type: z.literal("finish"),
|
|
88
|
+
finishReason: z.string().optional(),
|
|
89
|
+
rawFinishReason: z.string().optional(),
|
|
90
|
+
totalUsage: usageSchema.optional(),
|
|
91
|
+
usage: usageSchema.optional(),
|
|
92
|
+
})
|
|
93
|
+
.passthrough(),
|
|
94
|
+
z
|
|
95
|
+
.object({
|
|
96
|
+
type: z.literal("response-metadata"),
|
|
97
|
+
id: z.string().optional(),
|
|
98
|
+
modelId: z.string().optional(),
|
|
99
|
+
})
|
|
100
|
+
.passthrough(),
|
|
101
|
+
z
|
|
102
|
+
.object({
|
|
103
|
+
type: z.literal("error"),
|
|
104
|
+
error: z.union([z.string(), providerErrorSchema]).optional(),
|
|
105
|
+
message: z.string().optional(),
|
|
106
|
+
})
|
|
107
|
+
.passthrough(),
|
|
108
|
+
]);
|
|
109
|
+
function finishReason(raw) {
|
|
110
|
+
switch (raw) {
|
|
111
|
+
case "stop":
|
|
112
|
+
case "end_turn":
|
|
113
|
+
return { unified: "stop", raw };
|
|
114
|
+
case "tool_calls":
|
|
115
|
+
case "tool-calls":
|
|
116
|
+
case "tool_use":
|
|
117
|
+
return { unified: "tool-calls", raw };
|
|
118
|
+
case "length":
|
|
119
|
+
case "max_tokens":
|
|
120
|
+
case "max-tokens":
|
|
121
|
+
case "max_output_tokens":
|
|
122
|
+
return { unified: "length", raw };
|
|
123
|
+
case "content_filter":
|
|
124
|
+
return { unified: "content-filter", raw };
|
|
125
|
+
default:
|
|
126
|
+
return { unified: "other", raw };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function usageFromEvent(event) {
|
|
130
|
+
const usage = event.totalUsage ?? event.usage;
|
|
131
|
+
return {
|
|
132
|
+
inputTokens: {
|
|
133
|
+
total: usage?.inputTokens,
|
|
134
|
+
noCache: usage?.inputTokenDetails?.noCacheTokens,
|
|
135
|
+
cacheRead: usage?.inputTokenDetails?.cacheReadTokens,
|
|
136
|
+
cacheWrite: usage?.inputTokenDetails?.cacheWriteTokens,
|
|
137
|
+
},
|
|
138
|
+
outputTokens: {
|
|
139
|
+
total: usage?.outputTokens,
|
|
140
|
+
text: usage?.outputTokenDetails?.textTokens,
|
|
141
|
+
reasoning: usage?.outputTokenDetails?.reasoningTokens,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function toolCallPart(event) {
|
|
146
|
+
const input = event.input ?? event.args ?? event.arguments ?? {};
|
|
147
|
+
return {
|
|
148
|
+
type: "tool-call",
|
|
149
|
+
toolCallId: event.toolCallId ?? event.id ?? "",
|
|
150
|
+
toolName: event.toolName ?? "",
|
|
151
|
+
input: typeof input === "string" ? input : JSON.stringify(input),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function streamPart(event) {
|
|
155
|
+
switch (event.type) {
|
|
156
|
+
case "start":
|
|
157
|
+
return { type: "stream-start", warnings: [] };
|
|
158
|
+
case "abort":
|
|
159
|
+
case "finish-step":
|
|
160
|
+
return null;
|
|
161
|
+
case "text-start":
|
|
162
|
+
case "text-end":
|
|
163
|
+
case "reasoning-start":
|
|
164
|
+
case "reasoning-end":
|
|
165
|
+
case "tool-input-end":
|
|
166
|
+
return { type: event.type, id: event.id ?? "" };
|
|
167
|
+
case "text-delta":
|
|
168
|
+
case "reasoning-delta":
|
|
169
|
+
case "tool-input-delta":
|
|
170
|
+
return { type: event.type, id: event.id ?? "", delta: event.text ?? event.delta ?? "" };
|
|
171
|
+
case "tool-input-start":
|
|
172
|
+
return {
|
|
173
|
+
type: event.type,
|
|
174
|
+
id: event.id ?? "",
|
|
175
|
+
toolName: event.toolName ?? "",
|
|
176
|
+
...(event.dynamic === undefined ? {} : { dynamic: event.dynamic }),
|
|
177
|
+
};
|
|
178
|
+
case "tool-call":
|
|
179
|
+
return toolCallPart(event);
|
|
180
|
+
case "finish": {
|
|
181
|
+
const raw = event.rawFinishReason ?? event.finishReason ?? "stop";
|
|
182
|
+
return { type: "finish", finishReason: finishReason(raw), usage: usageFromEvent(event) };
|
|
183
|
+
}
|
|
184
|
+
case "response-metadata":
|
|
185
|
+
return {
|
|
186
|
+
type: event.type,
|
|
187
|
+
...(event.id === undefined ? {} : { id: event.id }),
|
|
188
|
+
...(event.modelId === undefined ? {} : { modelId: event.modelId }),
|
|
189
|
+
};
|
|
190
|
+
case "error":
|
|
191
|
+
return { type: "error", error: commandCodeNdjsonError(event.error) };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function parseEvent(line) {
|
|
195
|
+
try {
|
|
196
|
+
const parsed = JSON.parse(line);
|
|
197
|
+
const result = streamEventSchema.safeParse(parsed);
|
|
198
|
+
return result.success ? result.data : null;
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export async function emitCommandCodeChunks(chunks, controller) {
|
|
205
|
+
let finished = false;
|
|
206
|
+
const consumeLine = (line) => {
|
|
207
|
+
const trimmed = line.trim();
|
|
208
|
+
if (trimmed.length === 0 || trimmed.startsWith(":") || trimmed === "[DONE]") {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const payload = trimmed.startsWith("data:") ? trimmed.slice(5).trimStart() : trimmed;
|
|
212
|
+
const event = parseEvent(payload);
|
|
213
|
+
if (event === null || (event.type === "finish" && finished)) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const part = streamPart(event);
|
|
217
|
+
if (part !== null) {
|
|
218
|
+
controller.enqueue(part);
|
|
219
|
+
}
|
|
220
|
+
if (event.type === "finish") {
|
|
221
|
+
finished = true;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
for await (const line of commandCodeRecords(chunks))
|
|
225
|
+
consumeLine(line);
|
|
226
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const COMMAND_CODE_PROVIDER_ERROR = "COMMAND_CODE_PROVIDER_ERROR";
|
|
2
|
+
export declare const commandCodeProviderErrorStages: readonly ["http-response", "ndjson-stream", "response-body"];
|
|
3
|
+
export type CommandCodeProviderErrorStage = (typeof commandCodeProviderErrorStages)[number];
|
|
4
|
+
export declare const commandCodeProviderErrorReasons: readonly ["http-status", "ndjson-event", "missing-response-body", "response-body-too-large", "stream-record-too-large"];
|
|
5
|
+
export type CommandCodeProviderErrorReason = (typeof commandCodeProviderErrorReasons)[number];
|
|
6
|
+
type CommandCodeProviderErrorOptions = {
|
|
7
|
+
readonly stage: CommandCodeProviderErrorStage;
|
|
8
|
+
readonly reason: CommandCodeProviderErrorReason;
|
|
9
|
+
readonly statusCode: number | null;
|
|
10
|
+
readonly providerCode: string | null;
|
|
11
|
+
readonly retryable: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type CommandCodeProviderMetadata = {
|
|
14
|
+
readonly code?: string;
|
|
15
|
+
readonly statusCode?: number;
|
|
16
|
+
readonly isRetryable?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare class CommandCodeProviderError extends Error {
|
|
19
|
+
readonly name = "CommandCodeProviderError";
|
|
20
|
+
readonly code: typeof COMMAND_CODE_PROVIDER_ERROR;
|
|
21
|
+
readonly stage: CommandCodeProviderErrorStage;
|
|
22
|
+
readonly reason: CommandCodeProviderErrorReason;
|
|
23
|
+
readonly statusCode: number | null;
|
|
24
|
+
readonly providerCode: string | null;
|
|
25
|
+
readonly retryable: boolean;
|
|
26
|
+
constructor(options: CommandCodeProviderErrorOptions);
|
|
27
|
+
}
|
|
28
|
+
export declare function commandCodeHttpError(statusCode: number, body: string): CommandCodeProviderError;
|
|
29
|
+
export declare function commandCodeNdjsonError(error: string | CommandCodeProviderMetadata | undefined): CommandCodeProviderError;
|
|
30
|
+
export declare function commandCodeMissingBodyError(statusCode: number): CommandCodeProviderError;
|
|
31
|
+
export declare function commandCodeResponseBodyTooLargeError(): CommandCodeProviderError;
|
|
32
|
+
export declare function commandCodeStreamRecordTooLargeError(): CommandCodeProviderError;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const COMMAND_CODE_PROVIDER_ERROR = "COMMAND_CODE_PROVIDER_ERROR";
|
|
3
|
+
export const commandCodeProviderErrorStages = [
|
|
4
|
+
"http-response",
|
|
5
|
+
"ndjson-stream",
|
|
6
|
+
"response-body",
|
|
7
|
+
];
|
|
8
|
+
export const commandCodeProviderErrorReasons = [
|
|
9
|
+
"http-status",
|
|
10
|
+
"ndjson-event",
|
|
11
|
+
"missing-response-body",
|
|
12
|
+
"response-body-too-large",
|
|
13
|
+
"stream-record-too-large",
|
|
14
|
+
];
|
|
15
|
+
const commandCodeProviderMetadataSchema = z
|
|
16
|
+
.object({
|
|
17
|
+
code: z.string().optional(),
|
|
18
|
+
statusCode: z.number().int().min(100).max(599).optional(),
|
|
19
|
+
isRetryable: z.boolean().optional(),
|
|
20
|
+
})
|
|
21
|
+
.passthrough();
|
|
22
|
+
const httpErrorBodySchema = z.union([
|
|
23
|
+
z.object({ error: commandCodeProviderMetadataSchema }).transform((value) => value.error),
|
|
24
|
+
commandCodeProviderMetadataSchema,
|
|
25
|
+
]);
|
|
26
|
+
export class CommandCodeProviderError extends Error {
|
|
27
|
+
name = "CommandCodeProviderError";
|
|
28
|
+
code = COMMAND_CODE_PROVIDER_ERROR;
|
|
29
|
+
stage;
|
|
30
|
+
reason;
|
|
31
|
+
statusCode;
|
|
32
|
+
providerCode;
|
|
33
|
+
retryable;
|
|
34
|
+
constructor(options) {
|
|
35
|
+
super("Command Code provider request failed");
|
|
36
|
+
this.stage = options.stage;
|
|
37
|
+
this.reason = options.reason;
|
|
38
|
+
this.statusCode = options.statusCode;
|
|
39
|
+
this.providerCode = options.providerCode;
|
|
40
|
+
this.retryable = options.retryable;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function parseHttpMetadata(body) {
|
|
44
|
+
try {
|
|
45
|
+
const parsed = JSON.parse(body);
|
|
46
|
+
const result = httpErrorBodySchema.safeParse(parsed);
|
|
47
|
+
if (!result.success)
|
|
48
|
+
return null;
|
|
49
|
+
return {
|
|
50
|
+
...(result.data.code === undefined ? {} : { code: result.data.code }),
|
|
51
|
+
...(result.data.statusCode === undefined ? {} : { statusCode: result.data.statusCode }),
|
|
52
|
+
...(result.data.isRetryable === undefined ? {} : { isRetryable: result.data.isRetryable }),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function commandCodeHttpError(statusCode, body) {
|
|
60
|
+
const metadata = parseHttpMetadata(body);
|
|
61
|
+
return new CommandCodeProviderError({
|
|
62
|
+
stage: "http-response",
|
|
63
|
+
reason: "http-status",
|
|
64
|
+
statusCode,
|
|
65
|
+
providerCode: metadata?.code ?? null,
|
|
66
|
+
retryable: metadata?.isRetryable ?? (statusCode === 429 || statusCode >= 500),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
export function commandCodeNdjsonError(error) {
|
|
70
|
+
const metadata = typeof error === "object" ? error : undefined;
|
|
71
|
+
return new CommandCodeProviderError({
|
|
72
|
+
stage: "ndjson-stream",
|
|
73
|
+
reason: "ndjson-event",
|
|
74
|
+
statusCode: metadata?.statusCode ?? null,
|
|
75
|
+
providerCode: metadata?.code ?? null,
|
|
76
|
+
retryable: metadata?.isRetryable ?? false,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
export function commandCodeMissingBodyError(statusCode) {
|
|
80
|
+
return new CommandCodeProviderError({
|
|
81
|
+
stage: "response-body",
|
|
82
|
+
reason: "missing-response-body",
|
|
83
|
+
statusCode,
|
|
84
|
+
providerCode: null,
|
|
85
|
+
retryable: false,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function commandCodeResponseBodyTooLargeError() {
|
|
89
|
+
return new CommandCodeProviderError({
|
|
90
|
+
stage: "response-body",
|
|
91
|
+
reason: "response-body-too-large",
|
|
92
|
+
statusCode: null,
|
|
93
|
+
providerCode: null,
|
|
94
|
+
retryable: false,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
export function commandCodeStreamRecordTooLargeError() {
|
|
98
|
+
return new CommandCodeProviderError({
|
|
99
|
+
stage: "ndjson-stream",
|
|
100
|
+
reason: "stream-record-too-large",
|
|
101
|
+
statusCode: null,
|
|
102
|
+
providerCode: null,
|
|
103
|
+
retryable: false,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { LanguageModelV3 } from "@ai-sdk/provider";
|
|
2
|
+
import type { HttpTransport } from "../../core/http";
|
|
3
|
+
import { type CommandCodeVersionResolver } from "./cli-version";
|
|
4
|
+
export type CommandCodeLanguageModelOptions = {
|
|
5
|
+
readonly modelId: string;
|
|
6
|
+
readonly transport: HttpTransport;
|
|
7
|
+
readonly readAccessToken: (signal: AbortSignal) => Promise<string | null>;
|
|
8
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
9
|
+
readonly readCliVersion?: CommandCodeVersionResolver;
|
|
10
|
+
readonly baseURL?: string;
|
|
11
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
12
|
+
readonly timeoutMs?: number;
|
|
13
|
+
readonly generateSessionId?: () => string;
|
|
14
|
+
};
|
|
15
|
+
export declare function createCommandCodeLanguageModel(options: CommandCodeLanguageModelOptions): LanguageModelV3;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Derived from brent-weatherall/opencode-commandcode-provider src/model.ts.
|
|
2
|
+
// Licensed under MIT. See THIRD_PARTY_NOTICES.md.
|
|
3
|
+
import { AdapterError, OperationCancelledError } from "../../core/errors";
|
|
4
|
+
import { parseProviderId } from "../../core/ids";
|
|
5
|
+
import { openHttpBody } from "../../http/read-body";
|
|
6
|
+
import { createCommandCodeVersionResolver } from "./cli-version";
|
|
7
|
+
import { emitCommandCodeChunks } from "./emit-stream";
|
|
8
|
+
import { commandCodeMissingBodyError } from "./errors";
|
|
9
|
+
import { buildBody, buildHeaders } from "./request";
|
|
10
|
+
import { commandCodeHttpError, createCommandCodeRequestLifecycle, readCommandCodeErrorBody, } from "./request-lifecycle";
|
|
11
|
+
import { createCommandCodeSessionId } from "./session";
|
|
12
|
+
function buildRequestOptions(options, call, token, cliVersion) {
|
|
13
|
+
const bodyOptions = {
|
|
14
|
+
modelId: options.modelId,
|
|
15
|
+
call,
|
|
16
|
+
sessionId: options.sessionId,
|
|
17
|
+
};
|
|
18
|
+
const headerOptions = {
|
|
19
|
+
token,
|
|
20
|
+
cliVersion,
|
|
21
|
+
sessionId: options.sessionId,
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
url: `${(options.baseURL ?? "https://api.commandcode.ai").replace(/\/+$/, "")}/alpha/generate`,
|
|
25
|
+
headers: {
|
|
26
|
+
...buildHeaders(headerOptions),
|
|
27
|
+
...options.headers,
|
|
28
|
+
...Object.fromEntries(Object.entries(call.headers ?? {}).filter((entry) => entry[1] !== undefined)),
|
|
29
|
+
},
|
|
30
|
+
body: new TextEncoder().encode(JSON.stringify(buildBody(bodyOptions))),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function createUsage() {
|
|
34
|
+
return {
|
|
35
|
+
inputTokens: {
|
|
36
|
+
total: undefined,
|
|
37
|
+
noCache: undefined,
|
|
38
|
+
cacheRead: undefined,
|
|
39
|
+
cacheWrite: undefined,
|
|
40
|
+
},
|
|
41
|
+
outputTokens: { total: undefined, text: undefined, reasoning: undefined },
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function createFinishReason() {
|
|
45
|
+
return { unified: "stop", raw: "stop" };
|
|
46
|
+
}
|
|
47
|
+
async function streamCommandCode(options, call) {
|
|
48
|
+
if (call.abortSignal?.aborted === true) {
|
|
49
|
+
throw new OperationCancelledError("command-code-stream");
|
|
50
|
+
}
|
|
51
|
+
const lifecycle = createCommandCodeRequestLifecycle(call.abortSignal, options.timeoutMs ?? 5 * 60 * 1_000);
|
|
52
|
+
const token = await options.readAccessToken(lifecycle.signal);
|
|
53
|
+
if (token === null) {
|
|
54
|
+
lifecycle.dispose();
|
|
55
|
+
throw new AdapterError({
|
|
56
|
+
operation: "command-code-missing-credentials",
|
|
57
|
+
retryable: false,
|
|
58
|
+
cause: null,
|
|
59
|
+
providerId: parseProviderId("command-code"),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const cliVersion = await options.readCliVersion(lifecycle.signal);
|
|
63
|
+
if (cliVersion === null) {
|
|
64
|
+
lifecycle.dispose();
|
|
65
|
+
throw new AdapterError({
|
|
66
|
+
operation: "command-code-cli-version",
|
|
67
|
+
retryable: false,
|
|
68
|
+
cause: null,
|
|
69
|
+
providerId: parseProviderId("command-code"),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const requestOptions = buildRequestOptions(options, call, token, cliVersion);
|
|
73
|
+
let opened;
|
|
74
|
+
try {
|
|
75
|
+
opened = await openHttpBody(options.transport, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
url: requestOptions.url,
|
|
78
|
+
headers: requestOptions.headers,
|
|
79
|
+
body: requestOptions.body,
|
|
80
|
+
}, lifecycle.signal);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
lifecycle.dispose();
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
if (opened.status < 200 || opened.status >= 300) {
|
|
87
|
+
let errorBody;
|
|
88
|
+
try {
|
|
89
|
+
errorBody = await readCommandCodeErrorBody(opened.chunks);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
lifecycle.abort();
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
lifecycle.dispose();
|
|
97
|
+
}
|
|
98
|
+
throw commandCodeHttpError(opened.status, errorBody);
|
|
99
|
+
}
|
|
100
|
+
if (!opened.bodyPresent) {
|
|
101
|
+
lifecycle.dispose();
|
|
102
|
+
throw commandCodeMissingBodyError(opened.status);
|
|
103
|
+
}
|
|
104
|
+
let cancelled = false;
|
|
105
|
+
const stream = new ReadableStream({
|
|
106
|
+
async start(controller) {
|
|
107
|
+
try {
|
|
108
|
+
await emitCommandCodeChunks(opened.chunks, controller);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (!cancelled) {
|
|
112
|
+
controller.enqueue({ type: "error", error });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
lifecycle.dispose();
|
|
117
|
+
if (!cancelled) {
|
|
118
|
+
controller.close();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
cancel() {
|
|
123
|
+
cancelled = true;
|
|
124
|
+
lifecycle.abort();
|
|
125
|
+
lifecycle.dispose();
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
stream,
|
|
130
|
+
request: { body: new TextDecoder().decode(requestOptions.body) },
|
|
131
|
+
response: { headers: opened.headers },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export function createCommandCodeLanguageModel(options) {
|
|
135
|
+
const provider = parseProviderId("command-code");
|
|
136
|
+
const sessionId = createCommandCodeSessionId(options.generateSessionId);
|
|
137
|
+
const readCliVersion = options.readCliVersion ??
|
|
138
|
+
createCommandCodeVersionResolver({
|
|
139
|
+
env: options.env ?? process.env,
|
|
140
|
+
transport: options.transport,
|
|
141
|
+
});
|
|
142
|
+
const runtime = { ...options, sessionId, readCliVersion };
|
|
143
|
+
return {
|
|
144
|
+
specificationVersion: "v3",
|
|
145
|
+
provider,
|
|
146
|
+
modelId: options.modelId,
|
|
147
|
+
supportedUrls: {},
|
|
148
|
+
doGenerate: async (call) => {
|
|
149
|
+
const { stream } = await streamCommandCode(runtime, call);
|
|
150
|
+
const content = [];
|
|
151
|
+
const text = [];
|
|
152
|
+
const reasoning = [];
|
|
153
|
+
let finish = createFinishReason();
|
|
154
|
+
let usage = createUsage();
|
|
155
|
+
const reader = stream.getReader();
|
|
156
|
+
try {
|
|
157
|
+
for (;;) {
|
|
158
|
+
const next = await reader.read();
|
|
159
|
+
if (next.done) {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
const part = next.value;
|
|
163
|
+
switch (part.type) {
|
|
164
|
+
case "text-delta":
|
|
165
|
+
text.push(part.delta);
|
|
166
|
+
break;
|
|
167
|
+
case "reasoning-delta":
|
|
168
|
+
reasoning.push(part.delta);
|
|
169
|
+
break;
|
|
170
|
+
case "tool-call":
|
|
171
|
+
content.push({
|
|
172
|
+
type: "tool-call",
|
|
173
|
+
toolCallId: part.toolCallId,
|
|
174
|
+
toolName: part.toolName,
|
|
175
|
+
input: part.input,
|
|
176
|
+
});
|
|
177
|
+
break;
|
|
178
|
+
case "finish":
|
|
179
|
+
finish = part.finishReason;
|
|
180
|
+
usage = part.usage;
|
|
181
|
+
break;
|
|
182
|
+
case "error":
|
|
183
|
+
throw part.error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
reader.releaseLock();
|
|
189
|
+
await stream.cancel();
|
|
190
|
+
}
|
|
191
|
+
const textValue = text.join("");
|
|
192
|
+
if (textValue.length > 0) {
|
|
193
|
+
content.unshift({ type: "text", text: textValue });
|
|
194
|
+
}
|
|
195
|
+
const reasoningValue = reasoning.join("");
|
|
196
|
+
if (reasoningValue.length > 0) {
|
|
197
|
+
content.unshift({ type: "reasoning", text: reasoningValue });
|
|
198
|
+
}
|
|
199
|
+
return { content, finishReason: finish, usage, warnings: [] };
|
|
200
|
+
},
|
|
201
|
+
doStream: (call) => streamCommandCode(runtime, call),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { parseModelIdList } from "../../catalog/parse-ids";
|
|
2
|
+
export async function listCommandCodeModels(transport, token, signal) {
|
|
3
|
+
const response = await transport.request({
|
|
4
|
+
method: "GET",
|
|
5
|
+
url: "https://api.commandcode.ai/provider/v1/models",
|
|
6
|
+
headers: { authorization: `Bearer ${token}` },
|
|
7
|
+
body: null,
|
|
8
|
+
}, signal);
|
|
9
|
+
if (response.status >= 400) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
return parseModelIdList(JSON.parse(new TextDecoder().decode(response.body)));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function commandCodeRecords(chunks: AsyncIterable<Uint8Array>): AsyncIterable<string>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { commandCodeStreamRecordTooLargeError } from "./errors";
|
|
2
|
+
const MAX_STREAM_RECORD_BYTES = 1024 * 1024;
|
|
3
|
+
const CARRIAGE_RETURN = 13;
|
|
4
|
+
const LINE_FEED = 10;
|
|
5
|
+
export async function* commandCodeRecords(chunks) {
|
|
6
|
+
const decoder = new TextDecoder();
|
|
7
|
+
let buffer = "";
|
|
8
|
+
let recordBytes = 0;
|
|
9
|
+
let pendingCarriageReturn = false;
|
|
10
|
+
const append = (segment) => {
|
|
11
|
+
recordBytes += segment.byteLength;
|
|
12
|
+
if (recordBytes > MAX_STREAM_RECORD_BYTES) {
|
|
13
|
+
throw commandCodeStreamRecordTooLargeError();
|
|
14
|
+
}
|
|
15
|
+
buffer += decoder.decode(segment, { stream: true });
|
|
16
|
+
};
|
|
17
|
+
const complete = () => {
|
|
18
|
+
const record = buffer + decoder.decode();
|
|
19
|
+
buffer = "";
|
|
20
|
+
recordBytes = 0;
|
|
21
|
+
return record;
|
|
22
|
+
};
|
|
23
|
+
for await (const chunk of chunks) {
|
|
24
|
+
let start = 0;
|
|
25
|
+
if (pendingCarriageReturn && chunk.byteLength === 0)
|
|
26
|
+
continue;
|
|
27
|
+
if (pendingCarriageReturn) {
|
|
28
|
+
pendingCarriageReturn = false;
|
|
29
|
+
if (chunk[0] === LINE_FEED) {
|
|
30
|
+
yield complete();
|
|
31
|
+
start = 1;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
append(new Uint8Array([CARRIAGE_RETURN]));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (let index = 0; index < chunk.byteLength; index += 1) {
|
|
38
|
+
if (index < start)
|
|
39
|
+
continue;
|
|
40
|
+
if (chunk[index] !== LINE_FEED)
|
|
41
|
+
continue;
|
|
42
|
+
const contentEnd = chunk[index - 1] === CARRIAGE_RETURN ? index - 1 : index;
|
|
43
|
+
append(chunk.subarray(start, contentEnd));
|
|
44
|
+
yield complete();
|
|
45
|
+
start = index + 1;
|
|
46
|
+
}
|
|
47
|
+
const trailing = chunk.subarray(start);
|
|
48
|
+
if (trailing.at(-1) === CARRIAGE_RETURN) {
|
|
49
|
+
append(trailing.subarray(0, trailing.byteLength - 1));
|
|
50
|
+
pendingCarriageReturn = true;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
append(trailing);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (pendingCarriageReturn)
|
|
57
|
+
append(new Uint8Array([CARRIAGE_RETURN]));
|
|
58
|
+
buffer += decoder.decode();
|
|
59
|
+
if (buffer.trim().length > 0)
|
|
60
|
+
yield buffer;
|
|
61
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { commandCodeHttpError } from "./errors";
|
|
2
|
+
export type CommandCodeRequestLifecycle = {
|
|
3
|
+
readonly signal: AbortSignal;
|
|
4
|
+
readonly abort: () => void;
|
|
5
|
+
readonly dispose: () => void;
|
|
6
|
+
};
|
|
7
|
+
export declare function createCommandCodeRequestLifecycle(parent: AbortSignal | undefined, timeoutMs: number): CommandCodeRequestLifecycle;
|
|
8
|
+
export declare function readCommandCodeErrorBody(chunks: AsyncIterable<Uint8Array>): Promise<string>;
|