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,109 @@
|
|
|
1
|
+
// Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
|
|
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 { emitClaudeSseChunks } from "./emit-stream";
|
|
7
|
+
import { buildRequestBody, emptyUsage, parseAssistantText, promptToAnthropicMessages, } from "./prompt";
|
|
8
|
+
export function createClaudeLanguageModel(options) {
|
|
9
|
+
const provider = parseProviderId("claude");
|
|
10
|
+
return {
|
|
11
|
+
specificationVersion: "v3",
|
|
12
|
+
provider,
|
|
13
|
+
modelId: options.modelId,
|
|
14
|
+
supportedUrls: {},
|
|
15
|
+
doGenerate: async (call) => {
|
|
16
|
+
const signal = call.abortSignal ?? new AbortController().signal;
|
|
17
|
+
if (signal.aborted) {
|
|
18
|
+
throw new OperationCancelledError("claude-generate");
|
|
19
|
+
}
|
|
20
|
+
const token = await options.readAccessToken(signal);
|
|
21
|
+
if (token === null) {
|
|
22
|
+
throw new AdapterError({
|
|
23
|
+
operation: "claude-missing-credentials",
|
|
24
|
+
retryable: false,
|
|
25
|
+
cause: null,
|
|
26
|
+
providerId: provider,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const mapped = promptToAnthropicMessages(call.prompt);
|
|
30
|
+
const requestBody = buildRequestBody(options, mapped, false);
|
|
31
|
+
const response = await options.transport.request({
|
|
32
|
+
method: "POST",
|
|
33
|
+
url: "https://api.anthropic.com/v1/messages",
|
|
34
|
+
headers: {
|
|
35
|
+
"anthropic-version": "2023-06-01",
|
|
36
|
+
authorization: `Bearer ${token}`,
|
|
37
|
+
"content-type": "application/json",
|
|
38
|
+
"user-agent": "claude-cli/2.1.6 (external, sdk-cli)",
|
|
39
|
+
},
|
|
40
|
+
body: new TextEncoder().encode(JSON.stringify(requestBody)),
|
|
41
|
+
}, signal);
|
|
42
|
+
if (response.status >= 400) {
|
|
43
|
+
throw new AdapterError({
|
|
44
|
+
operation: "claude-http",
|
|
45
|
+
retryable: response.status >= 500,
|
|
46
|
+
cause: null,
|
|
47
|
+
providerId: provider,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const text = parseAssistantText(response.body);
|
|
51
|
+
return {
|
|
52
|
+
content: [{ type: "text", text }],
|
|
53
|
+
finishReason: { unified: "stop", raw: "end_turn" },
|
|
54
|
+
usage: emptyUsage(),
|
|
55
|
+
warnings: [],
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
doStream: async (call) => {
|
|
59
|
+
const signal = call.abortSignal ?? new AbortController().signal;
|
|
60
|
+
if (signal.aborted) {
|
|
61
|
+
throw new OperationCancelledError("claude-stream");
|
|
62
|
+
}
|
|
63
|
+
const token = await options.readAccessToken(signal);
|
|
64
|
+
if (token === null) {
|
|
65
|
+
throw new AdapterError({
|
|
66
|
+
operation: "claude-missing-credentials",
|
|
67
|
+
retryable: false,
|
|
68
|
+
cause: null,
|
|
69
|
+
providerId: provider,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const mapped = promptToAnthropicMessages(call.prompt);
|
|
73
|
+
const requestBody = buildRequestBody(options, mapped, true);
|
|
74
|
+
const opened = await openHttpBody(options.transport, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
url: "https://api.anthropic.com/v1/messages",
|
|
77
|
+
headers: {
|
|
78
|
+
"anthropic-version": "2023-06-01",
|
|
79
|
+
authorization: `Bearer ${token}`,
|
|
80
|
+
"content-type": "application/json",
|
|
81
|
+
"user-agent": "claude-cli/2.1.6 (external, sdk-cli)",
|
|
82
|
+
},
|
|
83
|
+
body: new TextEncoder().encode(JSON.stringify(requestBody)),
|
|
84
|
+
}, signal);
|
|
85
|
+
if (opened.status >= 400) {
|
|
86
|
+
throw new AdapterError({
|
|
87
|
+
operation: "claude-http",
|
|
88
|
+
retryable: opened.status >= 500,
|
|
89
|
+
cause: null,
|
|
90
|
+
providerId: provider,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const stream = new ReadableStream({
|
|
94
|
+
async start(controller) {
|
|
95
|
+
try {
|
|
96
|
+
await emitClaudeSseChunks(opened.chunks, controller);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
controller.error(error);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
controller.close();
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
return { stream };
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HttpTransport } from "../../core/http";
|
|
2
|
+
import type { AdapterModel } from "../../core/models";
|
|
3
|
+
export type ClaudeModelListOptions = {
|
|
4
|
+
readonly transport: HttpTransport;
|
|
5
|
+
readonly token: string;
|
|
6
|
+
readonly version: string;
|
|
7
|
+
readonly signal: AbortSignal;
|
|
8
|
+
};
|
|
9
|
+
export declare function listClaudeModels(options: ClaudeModelListOptions): Promise<readonly AdapterModel[]>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parseModelIdList } from "../../catalog/parse-ids";
|
|
2
|
+
import { createClaudeCompatibilityHeaders } from "./compat-request";
|
|
3
|
+
export async function listClaudeModels(options) {
|
|
4
|
+
const headers = createClaudeCompatibilityHeaders({
|
|
5
|
+
accessToken: options.token,
|
|
6
|
+
modelId: "unknown",
|
|
7
|
+
version: options.version,
|
|
8
|
+
});
|
|
9
|
+
const response = await options.transport.request({
|
|
10
|
+
method: "GET",
|
|
11
|
+
url: "https://api.anthropic.com/v1/models",
|
|
12
|
+
headers: Object.fromEntries(headers.entries()),
|
|
13
|
+
body: null,
|
|
14
|
+
}, options.signal);
|
|
15
|
+
if (response.status >= 400) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return parseModelIdList(JSON.parse(new TextDecoder().decode(response.body)));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { LanguageModelV3CallOptions } from "@ai-sdk/provider";
|
|
2
|
+
import { type RequestBody } from "./transform";
|
|
3
|
+
type AnthropicContentPart = {
|
|
4
|
+
readonly type: "text";
|
|
5
|
+
readonly text: string;
|
|
6
|
+
} | {
|
|
7
|
+
readonly type: "tool_use";
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly input: Record<string, unknown>;
|
|
11
|
+
} | {
|
|
12
|
+
readonly type: "tool_result";
|
|
13
|
+
readonly tool_use_id: string;
|
|
14
|
+
readonly content: string;
|
|
15
|
+
readonly is_error: boolean;
|
|
16
|
+
};
|
|
17
|
+
export declare function promptToAnthropicMessages(prompt: LanguageModelV3CallOptions["prompt"]): {
|
|
18
|
+
readonly system: string;
|
|
19
|
+
readonly messages: readonly {
|
|
20
|
+
readonly role: "user" | "assistant";
|
|
21
|
+
readonly content: readonly AnthropicContentPart[];
|
|
22
|
+
}[];
|
|
23
|
+
};
|
|
24
|
+
export declare function buildRequestBody(options: {
|
|
25
|
+
readonly modelId: string;
|
|
26
|
+
readonly maxOutputTokens?: number;
|
|
27
|
+
}, mapped: ReturnType<typeof promptToAnthropicMessages>, stream: boolean): RequestBody;
|
|
28
|
+
export declare function parseAssistantText(body: Uint8Array): string;
|
|
29
|
+
export declare function emptyUsage(): {
|
|
30
|
+
readonly inputTokens: {
|
|
31
|
+
readonly total: undefined;
|
|
32
|
+
readonly noCache: undefined;
|
|
33
|
+
readonly cacheRead: undefined;
|
|
34
|
+
readonly cacheWrite: undefined;
|
|
35
|
+
};
|
|
36
|
+
readonly outputTokens: {
|
|
37
|
+
readonly total: undefined;
|
|
38
|
+
readonly text: undefined;
|
|
39
|
+
readonly reasoning: undefined;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
|
|
2
|
+
// Licensed under MIT. See THIRD_PARTY_NOTICES.md.
|
|
3
|
+
import { repairOrphanToolUses } from "./transform";
|
|
4
|
+
const CLAUDE_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude.";
|
|
5
|
+
function isTextPart(part) {
|
|
6
|
+
return (typeof part === "object" &&
|
|
7
|
+
part !== null &&
|
|
8
|
+
"type" in part &&
|
|
9
|
+
Reflect.get(part, "type") === "text");
|
|
10
|
+
}
|
|
11
|
+
function isToolCallPart(part) {
|
|
12
|
+
return (typeof part === "object" &&
|
|
13
|
+
part !== null &&
|
|
14
|
+
"type" in part &&
|
|
15
|
+
Reflect.get(part, "type") === "tool-call");
|
|
16
|
+
}
|
|
17
|
+
function isToolResultPart(part) {
|
|
18
|
+
return (typeof part === "object" &&
|
|
19
|
+
part !== null &&
|
|
20
|
+
"type" in part &&
|
|
21
|
+
Reflect.get(part, "type") === "tool-result");
|
|
22
|
+
}
|
|
23
|
+
function toolResultOutputToString(output) {
|
|
24
|
+
if (output.type === "text") {
|
|
25
|
+
return output.value;
|
|
26
|
+
}
|
|
27
|
+
return JSON.stringify(output);
|
|
28
|
+
}
|
|
29
|
+
function toRecord(value) {
|
|
30
|
+
if (typeof value === "object" && value !== null) {
|
|
31
|
+
const result = {};
|
|
32
|
+
for (const key of Object.keys(value)) {
|
|
33
|
+
result[key] = Reflect.get(value, key);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
function contentPartToAnthropic(part) {
|
|
40
|
+
if (isTextPart(part)) {
|
|
41
|
+
return { type: "text", text: part.text };
|
|
42
|
+
}
|
|
43
|
+
if (isToolCallPart(part)) {
|
|
44
|
+
return {
|
|
45
|
+
type: "tool_use",
|
|
46
|
+
id: part.toolCallId,
|
|
47
|
+
name: part.toolName,
|
|
48
|
+
input: toRecord(part.input),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (isToolResultPart(part)) {
|
|
52
|
+
return {
|
|
53
|
+
type: "tool_result",
|
|
54
|
+
tool_use_id: part.toolCallId,
|
|
55
|
+
content: toolResultOutputToString(part.output),
|
|
56
|
+
is_error: false,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { type: "text", text: "" };
|
|
60
|
+
}
|
|
61
|
+
export function promptToAnthropicMessages(prompt) {
|
|
62
|
+
const messages = [];
|
|
63
|
+
const systems = [CLAUDE_IDENTITY];
|
|
64
|
+
for (const message of prompt) {
|
|
65
|
+
switch (message.role) {
|
|
66
|
+
case "system":
|
|
67
|
+
systems.push(message.content);
|
|
68
|
+
break;
|
|
69
|
+
case "user": {
|
|
70
|
+
const content = [];
|
|
71
|
+
for (const part of message.content) {
|
|
72
|
+
if (isTextPart(part) || isToolCallPart(part) || isToolResultPart(part)) {
|
|
73
|
+
content.push(contentPartToAnthropic(part));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
messages.push({ role: "user", content });
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case "assistant": {
|
|
80
|
+
const content = [];
|
|
81
|
+
for (const part of message.content) {
|
|
82
|
+
if (isTextPart(part) || isToolCallPart(part) || isToolResultPart(part)) {
|
|
83
|
+
content.push(contentPartToAnthropic(part));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
messages.push({ role: "assistant", content });
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "tool":
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { system: systems.join("\n"), messages };
|
|
94
|
+
}
|
|
95
|
+
export function buildRequestBody(options, mapped, stream) {
|
|
96
|
+
const baseBody = {
|
|
97
|
+
model: options.modelId,
|
|
98
|
+
max_tokens: options.maxOutputTokens ?? 4096,
|
|
99
|
+
system: mapped.system,
|
|
100
|
+
messages: mapped.messages,
|
|
101
|
+
};
|
|
102
|
+
const requestBody = stream ? { ...baseBody, stream: true } : baseBody;
|
|
103
|
+
return repairOrphanToolUses(requestBody);
|
|
104
|
+
}
|
|
105
|
+
export function parseAssistantText(body) {
|
|
106
|
+
const parsed = JSON.parse(new TextDecoder().decode(body));
|
|
107
|
+
if (typeof parsed !== "object" || parsed === null || !("content" in parsed)) {
|
|
108
|
+
return "";
|
|
109
|
+
}
|
|
110
|
+
const content = parsed.content;
|
|
111
|
+
if (!Array.isArray(content)) {
|
|
112
|
+
return "";
|
|
113
|
+
}
|
|
114
|
+
const texts = [];
|
|
115
|
+
for (const part of content) {
|
|
116
|
+
if (typeof part === "object" && part !== null && "type" in part && "text" in part) {
|
|
117
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
118
|
+
texts.push(part.text);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return texts.join("");
|
|
123
|
+
}
|
|
124
|
+
export function emptyUsage() {
|
|
125
|
+
return {
|
|
126
|
+
inputTokens: {
|
|
127
|
+
total: undefined,
|
|
128
|
+
noCache: undefined,
|
|
129
|
+
cacheRead: undefined,
|
|
130
|
+
cacheWrite: undefined,
|
|
131
|
+
},
|
|
132
|
+
outputTokens: { total: undefined, text: undefined, reasoning: undefined },
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Clock } from "../../core/clock";
|
|
2
|
+
import type { HttpTransport } from "../../core/http";
|
|
3
|
+
import type { ClaudeCredentials } from "./credentials";
|
|
4
|
+
export type RefreshClaudeOptions = {
|
|
5
|
+
readonly transport: HttpTransport;
|
|
6
|
+
readonly clock: Clock;
|
|
7
|
+
readonly refreshToken: string;
|
|
8
|
+
readonly signal: AbortSignal;
|
|
9
|
+
};
|
|
10
|
+
export type ClaudeRefreshResult = {
|
|
11
|
+
readonly ok: true;
|
|
12
|
+
readonly credentials: ClaudeCredentials;
|
|
13
|
+
} | {
|
|
14
|
+
readonly ok: false;
|
|
15
|
+
readonly kind: "transient" | "terminal";
|
|
16
|
+
readonly retryAfterMs: number | null;
|
|
17
|
+
};
|
|
18
|
+
export declare function refreshClaudeAccessTokenResult(options: RefreshClaudeOptions): Promise<ClaudeRefreshResult>;
|
|
19
|
+
export declare function refreshClaudeAccessToken(options: RefreshClaudeOptions): Promise<ClaudeCredentials | null>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
|
|
2
|
+
// Licensed under MIT. See THIRD_PARTY_NOTICES.md.
|
|
3
|
+
const OAUTH_TOKEN_URL = "https://claude.ai/v1/oauth/token";
|
|
4
|
+
const OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
|
5
|
+
function stringField(value, key) {
|
|
6
|
+
if (!(key in value)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const field = Reflect.get(value, key);
|
|
10
|
+
return typeof field === "string" && field.length > 0 ? field : null;
|
|
11
|
+
}
|
|
12
|
+
function numberField(value, key) {
|
|
13
|
+
if (!(key in value)) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const field = Reflect.get(value, key);
|
|
17
|
+
return typeof field === "number" && Number.isFinite(field) ? field : null;
|
|
18
|
+
}
|
|
19
|
+
function retryAfterMs(headers) {
|
|
20
|
+
const seconds = Number.parseInt(headers["retry-after"] ?? "", 10);
|
|
21
|
+
return Number.isFinite(seconds) && seconds > 0 ? seconds * 1_000 : null;
|
|
22
|
+
}
|
|
23
|
+
export async function refreshClaudeAccessTokenResult(options) {
|
|
24
|
+
const body = new TextEncoder().encode(new URLSearchParams({
|
|
25
|
+
grant_type: "refresh_token",
|
|
26
|
+
client_id: OAUTH_CLIENT_ID,
|
|
27
|
+
refresh_token: options.refreshToken,
|
|
28
|
+
}).toString());
|
|
29
|
+
const response = await options.transport.request({
|
|
30
|
+
method: "POST",
|
|
31
|
+
url: OAUTH_TOKEN_URL,
|
|
32
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
33
|
+
body,
|
|
34
|
+
}, options.signal);
|
|
35
|
+
let parsed;
|
|
36
|
+
try {
|
|
37
|
+
parsed = JSON.parse(new TextDecoder().decode(response.body));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return {
|
|
41
|
+
ok: false,
|
|
42
|
+
kind: "transient",
|
|
43
|
+
retryAfterMs: retryAfterMs(response.headers),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
47
|
+
return { ok: false, kind: "transient", retryAfterMs: null };
|
|
48
|
+
}
|
|
49
|
+
if (response.status < 200 || response.status >= 300) {
|
|
50
|
+
const oauthError = stringField(parsed, "error");
|
|
51
|
+
const terminal = new Set([
|
|
52
|
+
"invalid_grant",
|
|
53
|
+
"invalid_client",
|
|
54
|
+
"unauthorized_client",
|
|
55
|
+
"unsupported_grant_type",
|
|
56
|
+
]);
|
|
57
|
+
return {
|
|
58
|
+
ok: false,
|
|
59
|
+
kind: oauthError !== null && terminal.has(oauthError) ? "terminal" : "transient",
|
|
60
|
+
retryAfterMs: retryAfterMs(response.headers),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const accessToken = stringField(parsed, "access_token");
|
|
64
|
+
if (accessToken === null) {
|
|
65
|
+
return { ok: false, kind: "transient", retryAfterMs: null };
|
|
66
|
+
}
|
|
67
|
+
const nowMs = options.clock.nowMs();
|
|
68
|
+
const expiresAt = numberField(parsed, "expires_at");
|
|
69
|
+
const expiresIn = numberField(parsed, "expires_in");
|
|
70
|
+
const expiresAtMs = expiresAt !== null && expiresAt > nowMs
|
|
71
|
+
? Math.trunc(expiresAt)
|
|
72
|
+
: Math.trunc(nowMs + (expiresIn ?? 36_000) * 1000);
|
|
73
|
+
return {
|
|
74
|
+
ok: true,
|
|
75
|
+
credentials: {
|
|
76
|
+
accessToken,
|
|
77
|
+
refreshToken: stringField(parsed, "refresh_token") ?? options.refreshToken,
|
|
78
|
+
expiresAtMs,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export async function refreshClaudeAccessToken(options) {
|
|
83
|
+
const result = await refreshClaudeAccessTokenResult(options);
|
|
84
|
+
return result.ok ? result.credentials : null;
|
|
85
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AnthropicSseEvent, SseParseResult } from "./sse";
|
|
2
|
+
export declare function convertEventToPart(event: AnthropicSseEvent): SseParseResult | null;
|
|
3
|
+
export declare function mapStopReason(reason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use" | null): {
|
|
4
|
+
unified: "stop" | "length" | "content-filter" | "tool-calls" | "error" | "other";
|
|
5
|
+
raw: string;
|
|
6
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
|
|
2
|
+
// Licensed under MIT. See THIRD_PARTY_NOTICES.md.
|
|
3
|
+
export function convertEventToPart(event) {
|
|
4
|
+
switch (event.type) {
|
|
5
|
+
case "message_start": {
|
|
6
|
+
return { kind: "part", part: { type: "stream-start", warnings: [] } };
|
|
7
|
+
}
|
|
8
|
+
case "content_block_start": {
|
|
9
|
+
const block = event.content_block;
|
|
10
|
+
if (block.type === "text") {
|
|
11
|
+
return { kind: "part", part: { type: "text-start", id: `text-${event.index}` } };
|
|
12
|
+
}
|
|
13
|
+
if (block.type === "tool_use" && block.id !== undefined && block.name !== undefined) {
|
|
14
|
+
return {
|
|
15
|
+
kind: "part",
|
|
16
|
+
part: {
|
|
17
|
+
type: "tool-input-start",
|
|
18
|
+
id: block.id,
|
|
19
|
+
toolName: block.name,
|
|
20
|
+
providerExecuted: false,
|
|
21
|
+
dynamic: false,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
case "content_block_delta": {
|
|
28
|
+
const delta = event.delta;
|
|
29
|
+
if (delta.type === "text_delta" && delta.text !== undefined) {
|
|
30
|
+
return {
|
|
31
|
+
kind: "part",
|
|
32
|
+
part: { type: "text-delta", id: `text-${event.index}`, delta: delta.text },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (delta.type === "input_json_delta" && delta.partial_json !== undefined) {
|
|
36
|
+
return {
|
|
37
|
+
kind: "part",
|
|
38
|
+
part: { type: "tool-input-delta", id: `tool-${event.index}`, delta: delta.partial_json },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
case "content_block_stop": {
|
|
44
|
+
return { kind: "part", part: { type: "text-end", id: `text-${event.index}` } };
|
|
45
|
+
}
|
|
46
|
+
case "message_delta": {
|
|
47
|
+
const stopReason = event.delta.stop_reason;
|
|
48
|
+
if (stopReason !== null) {
|
|
49
|
+
return { kind: "finish", stopReason };
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
case "message_stop": {
|
|
54
|
+
return { kind: "finish", stopReason: "end_turn" };
|
|
55
|
+
}
|
|
56
|
+
case "error": {
|
|
57
|
+
return { kind: "error", error: new Error(`Anthropic API error: ${event.error.message}`) };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function mapStopReason(reason) {
|
|
62
|
+
switch (reason) {
|
|
63
|
+
case "end_turn":
|
|
64
|
+
return { unified: "stop", raw: "end_turn" };
|
|
65
|
+
case "max_tokens":
|
|
66
|
+
return { unified: "length", raw: "max_tokens" };
|
|
67
|
+
case "stop_sequence":
|
|
68
|
+
return { unified: "stop", raw: "stop_sequence" };
|
|
69
|
+
case "tool_use":
|
|
70
|
+
return { unified: "tool-calls", raw: "tool_use" };
|
|
71
|
+
default:
|
|
72
|
+
return { unified: "other", raw: "unknown" };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { LanguageModelV3StreamPart } from "@ai-sdk/provider";
|
|
2
|
+
export type AnthropicSseEvent = {
|
|
3
|
+
readonly type: "message_start";
|
|
4
|
+
readonly message: {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly type: "message";
|
|
7
|
+
readonly role: "assistant";
|
|
8
|
+
readonly content: readonly unknown[];
|
|
9
|
+
readonly model: string;
|
|
10
|
+
readonly stop_reason: null;
|
|
11
|
+
readonly stop_sequence: null;
|
|
12
|
+
readonly usage: {
|
|
13
|
+
readonly input_tokens: number;
|
|
14
|
+
readonly output_tokens: number;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
} | {
|
|
18
|
+
readonly type: "content_block_start";
|
|
19
|
+
readonly index: number;
|
|
20
|
+
readonly content_block: {
|
|
21
|
+
readonly type: "text" | "tool_use";
|
|
22
|
+
readonly id?: string;
|
|
23
|
+
readonly name?: string;
|
|
24
|
+
readonly input?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
} | {
|
|
27
|
+
readonly type: "content_block_delta";
|
|
28
|
+
readonly index: number;
|
|
29
|
+
readonly delta: {
|
|
30
|
+
readonly type: "text_delta" | "input_json_delta";
|
|
31
|
+
readonly text?: string;
|
|
32
|
+
readonly partial_json?: string;
|
|
33
|
+
};
|
|
34
|
+
} | {
|
|
35
|
+
readonly type: "content_block_stop";
|
|
36
|
+
readonly index: number;
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: "message_delta";
|
|
39
|
+
readonly delta: {
|
|
40
|
+
readonly stop_reason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use" | null;
|
|
41
|
+
readonly stop_sequence: string | null;
|
|
42
|
+
};
|
|
43
|
+
} | {
|
|
44
|
+
readonly type: "message_stop";
|
|
45
|
+
} | {
|
|
46
|
+
readonly type: "error";
|
|
47
|
+
readonly error: {
|
|
48
|
+
readonly type: string;
|
|
49
|
+
readonly message: string;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export type SseParseResult = {
|
|
53
|
+
readonly kind: "part";
|
|
54
|
+
readonly part: LanguageModelV3StreamPart;
|
|
55
|
+
} | {
|
|
56
|
+
readonly kind: "finish";
|
|
57
|
+
readonly stopReason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use" | null;
|
|
58
|
+
} | {
|
|
59
|
+
readonly kind: "error";
|
|
60
|
+
readonly error: Error;
|
|
61
|
+
};
|
|
62
|
+
export type SseParseState = {
|
|
63
|
+
readonly buffer: string;
|
|
64
|
+
readonly event: string | null;
|
|
65
|
+
};
|
|
66
|
+
export declare function createSseParseState(): SseParseState;
|
|
67
|
+
export declare function parseAnthropicSse(chunk: Uint8Array, state: SseParseState): {
|
|
68
|
+
readonly events: readonly SseParseResult[];
|
|
69
|
+
readonly state: SseParseState;
|
|
70
|
+
};
|
|
71
|
+
export { convertEventToPart, mapStopReason } from "./sse-convert";
|