@opencode-ai/ai 0.0.0-bootstrap.0 → 0.0.0-next-15617
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/README.md +176 -1
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +108 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +9 -0
- package/dist/llm.d.ts +223 -0
- package/dist/llm.js +81 -0
- package/dist/protocols/anthropic-messages.d.ts +503 -0
- package/dist/protocols/anthropic-messages.js +745 -0
- package/dist/protocols/bedrock-converse.d.ts +476 -0
- package/dist/protocols/bedrock-converse.js +526 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +64 -0
- package/dist/protocols/gemini.d.ts +220 -0
- package/dist/protocols/gemini.js +420 -0
- package/dist/protocols/google-vertex-anthropic.d.ts +494 -0
- package/dist/protocols/google-vertex-anthropic.js +33 -0
- package/dist/protocols/google-vertex-gemini.d.ts +59 -0
- package/dist/protocols/google-vertex-gemini.js +18 -0
- package/dist/protocols/index.d.ts +9 -0
- package/dist/protocols/index.js +9 -0
- package/dist/protocols/openai-chat.d.ts +391 -0
- package/dist/protocols/openai-chat.js +423 -0
- package/dist/protocols/openai-compatible-chat.d.ts +73 -0
- package/dist/protocols/openai-compatible-chat.js +20 -0
- package/dist/protocols/openai-compatible-responses.d.ts +85 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-responses.d.ts +613 -0
- package/dist/protocols/openai-responses.js +828 -0
- package/dist/protocols/shared.d.ts +192 -0
- package/dist/protocols/shared.js +236 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +18 -0
- package/dist/protocols/utils/bedrock-auth.js +41 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +15 -0
- package/dist/protocols/utils/bedrock-cache.js +29 -0
- package/dist/protocols/utils/bedrock-media.d.ts +49 -0
- package/dist/protocols/utils/bedrock-media.js +68 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +8 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +83 -0
- package/dist/protocols/utils/lifecycle.d.ts +19 -0
- package/dist/protocols/utils/lifecycle.js +65 -0
- package/dist/protocols/utils/openai-options.d.ts +22 -0
- package/dist/protocols/utils/openai-options.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +7 -0
- package/dist/protocols/utils/tool-schema.js +79 -0
- package/dist/protocols/utils/tool-stream.d.ts +155 -0
- package/dist/protocols/utils/tool-stream.js +136 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +13 -0
- package/dist/provider-error.js +122 -0
- package/dist/provider-package.d.ts +13 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/amazon-bedrock.d.ts +150 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +183 -0
- package/dist/providers/anthropic-compatible.js +44 -0
- package/dist/providers/anthropic.d.ts +181 -0
- package/dist/providers/anthropic.js +39 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +183 -0
- package/dist/providers/azure.js +89 -0
- package/dist/providers/cloudflare.d.ts +232 -0
- package/dist/providers/cloudflare.js +87 -0
- package/dist/providers/github-copilot.d.ts +167 -0
- package/dist/providers/github-copilot.js +47 -0
- package/dist/providers/google-vertex/anthropic.d.ts +2 -0
- package/dist/providers/google-vertex/anthropic.js +1 -0
- package/dist/providers/google-vertex-anthropic.d.ts +181 -0
- package/dist/providers/google-vertex-anthropic.js +46 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +61 -0
- package/dist/providers/google-vertex.d.ts +93 -0
- package/dist/providers/google-vertex.js +49 -0
- package/dist/providers/google.d.ts +85 -0
- package/dist/providers/google.js +33 -0
- package/dist/providers/index.d.ts +15 -0
- package/dist/providers/index.js +15 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-profile.d.ts +43 -0
- package/dist/providers/openai-compatible-profile.js +12 -0
- package/dist/providers/openai-compatible-responses.d.ts +107 -0
- package/dist/providers/openai-compatible-responses.js +33 -0
- package/dist/providers/openai-compatible.d.ts +130 -0
- package/dist/providers/openai-compatible.js +55 -0
- package/dist/providers/openai-options.d.ts +30 -0
- package/dist/providers/openai-options.js +42 -0
- package/dist/providers/openai.d.ts +264 -0
- package/dist/providers/openai.js +62 -0
- package/dist/providers/openrouter.d.ts +321 -0
- package/dist/providers/openrouter.js +65 -0
- package/dist/providers/xai.d.ts +169 -0
- package/dist/providers/xai.js +43 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +49 -0
- package/dist/route/auth.js +89 -0
- package/dist/route/client.d.ts +330 -0
- package/dist/route/client.js +211 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +12 -0
- package/dist/route/executor.js +229 -0
- package/dist/route/framing.d.ts +23 -0
- package/dist/route/framing.js +4 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +76 -0
- package/dist/route/protocol.js +17 -0
- package/dist/route/transport/http.d.ts +33 -0
- package/dist/route/transport/http.js +98 -0
- package/dist/route/transport/index.d.ts +27 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket.d.ts +56 -0
- package/dist/route/transport/websocket.js +181 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +153 -0
- package/dist/schema/errors.js +150 -0
- package/dist/schema/events.d.ts +4292 -0
- package/dist/schema/events.js +482 -0
- package/dist/schema/ids.d.ts +30 -0
- package/dist/schema/ids.js +18 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +385 -0
- package/dist/schema/messages.js +246 -0
- package/dist/schema/options.d.ts +143 -0
- package/dist/schema/options.js +195 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +35 -0
- package/dist/tool.d.ts +134 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/package.json +36 -9
- package/index.js +0 -1
|
@@ -0,0 +1,745 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Route } from "../route/client";
|
|
3
|
+
import { Auth } from "../route/auth";
|
|
4
|
+
import { Endpoint } from "../route/endpoint";
|
|
5
|
+
import { Framing } from "../route/framing";
|
|
6
|
+
import { Protocol } from "../route/protocol";
|
|
7
|
+
import { LLMError, LLMEvent, Usage, } from "../schema";
|
|
8
|
+
import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared";
|
|
9
|
+
import { classifyProviderFailure } from "../provider-error";
|
|
10
|
+
import * as Cache from "./utils/cache";
|
|
11
|
+
import { Lifecycle } from "./utils/lifecycle";
|
|
12
|
+
import { ToolSchemaProjection } from "./utils/tool-schema";
|
|
13
|
+
import { ToolStream } from "./utils/tool-stream";
|
|
14
|
+
const ADAPTER = "anthropic-messages";
|
|
15
|
+
export const DEFAULT_BASE_URL = "https://api.anthropic.com/v1";
|
|
16
|
+
export const PATH = "/messages";
|
|
17
|
+
// =============================================================================
|
|
18
|
+
// Request Body Schema
|
|
19
|
+
// =============================================================================
|
|
20
|
+
const AnthropicCacheControl = Schema.Struct({
|
|
21
|
+
type: Schema.tag("ephemeral"),
|
|
22
|
+
ttl: Schema.optional(Schema.Literals(["5m", "1h"])),
|
|
23
|
+
});
|
|
24
|
+
const AnthropicTextBlock = Schema.Struct({
|
|
25
|
+
type: Schema.tag("text"),
|
|
26
|
+
text: Schema.String,
|
|
27
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
28
|
+
});
|
|
29
|
+
const AnthropicImageBlock = Schema.Struct({
|
|
30
|
+
type: Schema.tag("image"),
|
|
31
|
+
source: Schema.Struct({
|
|
32
|
+
type: Schema.tag("base64"),
|
|
33
|
+
media_type: Schema.String,
|
|
34
|
+
data: Schema.String,
|
|
35
|
+
}),
|
|
36
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
37
|
+
});
|
|
38
|
+
const AnthropicThinkingBlock = Schema.Struct({
|
|
39
|
+
type: Schema.tag("thinking"),
|
|
40
|
+
thinking: Schema.String,
|
|
41
|
+
signature: Schema.optional(Schema.String),
|
|
42
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
43
|
+
});
|
|
44
|
+
const AnthropicToolUseBlock = Schema.Struct({
|
|
45
|
+
type: Schema.tag("tool_use"),
|
|
46
|
+
id: Schema.String,
|
|
47
|
+
name: Schema.String,
|
|
48
|
+
input: Schema.Unknown,
|
|
49
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
50
|
+
});
|
|
51
|
+
const AnthropicServerToolUseBlock = Schema.Struct({
|
|
52
|
+
type: Schema.tag("server_tool_use"),
|
|
53
|
+
id: Schema.String,
|
|
54
|
+
name: Schema.String,
|
|
55
|
+
input: Schema.Unknown,
|
|
56
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
57
|
+
});
|
|
58
|
+
// Server tool result blocks: web_search_tool_result, code_execution_tool_result,
|
|
59
|
+
// and web_fetch_tool_result. The provider executes the tool and inlines the
|
|
60
|
+
// structured result into the assistant turn — there is no client tool_result
|
|
61
|
+
// round-trip. We round-trip the structured `content` payload as opaque JSON so
|
|
62
|
+
// the next request can echo it back when continuing the conversation.
|
|
63
|
+
const AnthropicServerToolResultType = Schema.Literals([
|
|
64
|
+
"web_search_tool_result",
|
|
65
|
+
"code_execution_tool_result",
|
|
66
|
+
"web_fetch_tool_result",
|
|
67
|
+
]);
|
|
68
|
+
const AnthropicServerToolResultBlock = Schema.Struct({
|
|
69
|
+
type: AnthropicServerToolResultType,
|
|
70
|
+
tool_use_id: Schema.String,
|
|
71
|
+
content: Schema.Unknown,
|
|
72
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
73
|
+
});
|
|
74
|
+
// Anthropic accepts either a plain string or an ordered array of text/image
|
|
75
|
+
// blocks inside `tool_result.content`. The array form is required when a tool
|
|
76
|
+
// returns image bytes (screenshot, image search, etc.) so they can be passed
|
|
77
|
+
// to the model as proper image inputs instead of being JSON-stringified into
|
|
78
|
+
// the prompt — which silently inflates context by megabytes and can push the
|
|
79
|
+
// conversation over the model's token limit.
|
|
80
|
+
const AnthropicToolResultContent = Schema.Union([AnthropicTextBlock, AnthropicImageBlock]);
|
|
81
|
+
const AnthropicToolResultBlock = Schema.Struct({
|
|
82
|
+
type: Schema.tag("tool_result"),
|
|
83
|
+
tool_use_id: Schema.String,
|
|
84
|
+
content: Schema.Union([Schema.String, Schema.Array(AnthropicToolResultContent)]),
|
|
85
|
+
is_error: Schema.optional(Schema.Boolean),
|
|
86
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
87
|
+
});
|
|
88
|
+
const AnthropicUserBlock = Schema.Union([AnthropicTextBlock, AnthropicImageBlock, AnthropicToolResultBlock]);
|
|
89
|
+
const AnthropicAssistantBlock = Schema.Union([
|
|
90
|
+
AnthropicTextBlock,
|
|
91
|
+
AnthropicThinkingBlock,
|
|
92
|
+
AnthropicToolUseBlock,
|
|
93
|
+
AnthropicServerToolUseBlock,
|
|
94
|
+
AnthropicServerToolResultBlock,
|
|
95
|
+
]);
|
|
96
|
+
const AnthropicMessage = Schema.Union([
|
|
97
|
+
Schema.Struct({ role: Schema.Literal("user"), content: Schema.Array(AnthropicUserBlock) }),
|
|
98
|
+
Schema.Struct({ role: Schema.Literal("assistant"), content: Schema.Array(AnthropicAssistantBlock) }),
|
|
99
|
+
Schema.Struct({ role: Schema.Literal("system"), content: Schema.Array(AnthropicTextBlock) }),
|
|
100
|
+
]).pipe(Schema.toTaggedUnion("role"));
|
|
101
|
+
const AnthropicTool = Schema.Struct({
|
|
102
|
+
name: Schema.String,
|
|
103
|
+
description: Schema.String,
|
|
104
|
+
input_schema: JsonObject,
|
|
105
|
+
cache_control: Schema.optional(AnthropicCacheControl),
|
|
106
|
+
});
|
|
107
|
+
const AnthropicToolChoice = Schema.Union([
|
|
108
|
+
Schema.Struct({ type: Schema.Literals(["auto", "any"]) }),
|
|
109
|
+
Schema.Struct({ type: Schema.tag("tool"), name: Schema.String }),
|
|
110
|
+
]);
|
|
111
|
+
const AnthropicThinking = Schema.Union([
|
|
112
|
+
Schema.Struct({
|
|
113
|
+
type: Schema.tag("enabled"),
|
|
114
|
+
budget_tokens: Schema.Number,
|
|
115
|
+
}),
|
|
116
|
+
Schema.Struct({
|
|
117
|
+
type: Schema.tag("adaptive"),
|
|
118
|
+
display: Schema.optional(Schema.Literals(["summarized", "omitted"])),
|
|
119
|
+
}),
|
|
120
|
+
Schema.Struct({
|
|
121
|
+
type: Schema.tag("disabled"),
|
|
122
|
+
}),
|
|
123
|
+
]);
|
|
124
|
+
const AnthropicOutputConfig = Schema.Struct({
|
|
125
|
+
effort: Schema.optional(Schema.String),
|
|
126
|
+
});
|
|
127
|
+
const AnthropicBodyFields = {
|
|
128
|
+
model: Schema.String,
|
|
129
|
+
system: optionalArray(AnthropicTextBlock),
|
|
130
|
+
messages: Schema.Array(AnthropicMessage),
|
|
131
|
+
tools: optionalArray(AnthropicTool),
|
|
132
|
+
tool_choice: Schema.optional(AnthropicToolChoice),
|
|
133
|
+
stream: Schema.Literal(true),
|
|
134
|
+
max_tokens: Schema.Number,
|
|
135
|
+
temperature: Schema.optional(Schema.Number),
|
|
136
|
+
top_p: Schema.optional(Schema.Number),
|
|
137
|
+
top_k: Schema.optional(Schema.Number),
|
|
138
|
+
stop_sequences: optionalArray(Schema.String),
|
|
139
|
+
thinking: Schema.optional(AnthropicThinking),
|
|
140
|
+
output_config: Schema.optional(AnthropicOutputConfig),
|
|
141
|
+
};
|
|
142
|
+
export const AnthropicMessagesBody = Schema.Struct(AnthropicBodyFields);
|
|
143
|
+
const AnthropicUsage = Schema.Struct({
|
|
144
|
+
input_tokens: Schema.optional(Schema.Number),
|
|
145
|
+
output_tokens: Schema.optional(Schema.Number),
|
|
146
|
+
cache_creation_input_tokens: optionalNull(Schema.Number),
|
|
147
|
+
cache_read_input_tokens: optionalNull(Schema.Number),
|
|
148
|
+
});
|
|
149
|
+
const AnthropicStreamBlock = Schema.Struct({
|
|
150
|
+
type: Schema.String,
|
|
151
|
+
id: Schema.optional(Schema.String),
|
|
152
|
+
name: Schema.optional(Schema.String),
|
|
153
|
+
text: Schema.optional(Schema.String),
|
|
154
|
+
thinking: Schema.optional(Schema.String),
|
|
155
|
+
signature: Schema.optional(Schema.String),
|
|
156
|
+
input: Schema.optional(Schema.Unknown),
|
|
157
|
+
// *_tool_result blocks arrive whole as content_block_start (no streaming
|
|
158
|
+
// delta) with the structured payload in `content` and the originating
|
|
159
|
+
// server_tool_use id in `tool_use_id`.
|
|
160
|
+
tool_use_id: Schema.optional(Schema.String),
|
|
161
|
+
content: Schema.optional(Schema.Unknown),
|
|
162
|
+
});
|
|
163
|
+
const AnthropicStreamDelta = Schema.Struct({
|
|
164
|
+
type: Schema.optional(Schema.String),
|
|
165
|
+
text: Schema.optional(Schema.String),
|
|
166
|
+
thinking: Schema.optional(Schema.String),
|
|
167
|
+
partial_json: Schema.optional(Schema.String),
|
|
168
|
+
signature: Schema.optional(Schema.String),
|
|
169
|
+
stop_reason: optionalNull(Schema.String),
|
|
170
|
+
stop_sequence: optionalNull(Schema.String),
|
|
171
|
+
});
|
|
172
|
+
const AnthropicEvent = Schema.Struct({
|
|
173
|
+
type: Schema.String,
|
|
174
|
+
index: Schema.optional(Schema.Number),
|
|
175
|
+
message: Schema.optional(Schema.Struct({ usage: Schema.optional(AnthropicUsage) })),
|
|
176
|
+
content_block: Schema.optional(AnthropicStreamBlock),
|
|
177
|
+
delta: Schema.optional(AnthropicStreamDelta),
|
|
178
|
+
usage: Schema.optional(AnthropicUsage),
|
|
179
|
+
// `type` and `message` are both required per Anthropic's spec, but
|
|
180
|
+
// OpenAI-compatible proxies and gateway translations occasionally drop one
|
|
181
|
+
// or the other; mark them optional so a partial payload still parses and
|
|
182
|
+
// the parser can fall back to whichever field is populated.
|
|
183
|
+
error: Schema.optional(Schema.Struct({ type: Schema.optional(Schema.String), message: Schema.optional(Schema.String) })),
|
|
184
|
+
});
|
|
185
|
+
const invalid = ProviderShared.invalidRequest;
|
|
186
|
+
// =============================================================================
|
|
187
|
+
// Request Lowering
|
|
188
|
+
// =============================================================================
|
|
189
|
+
// Anthropic accepts at most 4 explicit cache_control breakpoints per request,
|
|
190
|
+
// across `tools`, `system`, and `messages`. Beyond the cap the API returns a
|
|
191
|
+
// 400 — so the lowering layer counts emitted markers and silently drops any
|
|
192
|
+
// that exceed it.
|
|
193
|
+
const ANTHROPIC_BREAKPOINT_CAP = 4;
|
|
194
|
+
const EPHEMERAL_5M = { type: "ephemeral" };
|
|
195
|
+
const EPHEMERAL_1H = { type: "ephemeral", ttl: "1h" };
|
|
196
|
+
const cacheControl = (breakpoints, cache) => {
|
|
197
|
+
if (cache?.type !== "ephemeral" && cache?.type !== "persistent")
|
|
198
|
+
return undefined;
|
|
199
|
+
if (breakpoints.remaining <= 0) {
|
|
200
|
+
breakpoints.dropped += 1;
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
breakpoints.remaining -= 1;
|
|
204
|
+
return Cache.ttlBucket(cache.ttlSeconds) === "1h" ? EPHEMERAL_1H : EPHEMERAL_5M;
|
|
205
|
+
};
|
|
206
|
+
const anthropicMetadata = (metadata) => ({ anthropic: metadata });
|
|
207
|
+
const signatureFromMetadata = (metadata) => {
|
|
208
|
+
const anthropic = metadata?.anthropic;
|
|
209
|
+
if (!ProviderShared.isRecord(anthropic))
|
|
210
|
+
return undefined;
|
|
211
|
+
return typeof anthropic.signature === "string" ? anthropic.signature : undefined;
|
|
212
|
+
};
|
|
213
|
+
const lowerTool = (breakpoints, tool, inputSchema) => ({
|
|
214
|
+
name: tool.name,
|
|
215
|
+
description: tool.description,
|
|
216
|
+
input_schema: inputSchema,
|
|
217
|
+
cache_control: cacheControl(breakpoints, tool.cache),
|
|
218
|
+
});
|
|
219
|
+
const lowerToolChoice = (toolChoice) => ProviderShared.matchToolChoice("Anthropic Messages", toolChoice, {
|
|
220
|
+
auto: () => ({ type: "auto" }),
|
|
221
|
+
none: () => undefined,
|
|
222
|
+
required: () => ({ type: "any" }),
|
|
223
|
+
tool: (name) => ({ type: "tool", name }),
|
|
224
|
+
});
|
|
225
|
+
const lowerToolCall = (part) => ({
|
|
226
|
+
type: "tool_use",
|
|
227
|
+
id: part.id,
|
|
228
|
+
name: part.name,
|
|
229
|
+
input: part.input,
|
|
230
|
+
});
|
|
231
|
+
const lowerServerToolCall = (part) => ({
|
|
232
|
+
type: "server_tool_use",
|
|
233
|
+
id: part.id,
|
|
234
|
+
name: part.name,
|
|
235
|
+
input: part.input,
|
|
236
|
+
});
|
|
237
|
+
// Server tool result blocks are typed by name. Anthropic ships three today;
|
|
238
|
+
// extend this list when new server tools land. The block content is the
|
|
239
|
+
// structured payload returned by the provider, which we round-trip as-is.
|
|
240
|
+
const serverToolResultType = (name) => {
|
|
241
|
+
if (name === "web_search")
|
|
242
|
+
return "web_search_tool_result";
|
|
243
|
+
if (name === "code_execution")
|
|
244
|
+
return "code_execution_tool_result";
|
|
245
|
+
if (name === "web_fetch")
|
|
246
|
+
return "web_fetch_tool_result";
|
|
247
|
+
return undefined;
|
|
248
|
+
};
|
|
249
|
+
const lowerServerToolResult = Effect.fn("AnthropicMessages.lowerServerToolResult")(function* (part) {
|
|
250
|
+
const wireType = serverToolResultType(part.name);
|
|
251
|
+
if (!wireType)
|
|
252
|
+
return yield* invalid(`Anthropic Messages does not know how to round-trip server tool result for ${part.name}`);
|
|
253
|
+
return { type: wireType, tool_use_id: part.id, content: part.result.value };
|
|
254
|
+
});
|
|
255
|
+
const lowerImage = Effect.fn("AnthropicMessages.lowerImage")(function* (part) {
|
|
256
|
+
const media = yield* ProviderShared.validateMedia("Anthropic Messages", part, new Set(ProviderShared.IMAGE_MIMES));
|
|
257
|
+
return {
|
|
258
|
+
type: "image",
|
|
259
|
+
source: {
|
|
260
|
+
type: "base64",
|
|
261
|
+
media_type: media.mime,
|
|
262
|
+
data: media.base64,
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
});
|
|
266
|
+
// Tool results may carry structured text/images. Keep media as provider-native
|
|
267
|
+
// content instead of JSON-stringifying base64 into a prompt string.
|
|
268
|
+
const lowerToolResultContentItem = Effect.fn("AnthropicMessages.lowerToolResultContentItem")(function* (item) {
|
|
269
|
+
if (item.type === "text")
|
|
270
|
+
return { type: "text", text: item.text };
|
|
271
|
+
const media = yield* ProviderShared.validateToolFile("Anthropic Messages", item, new Set(ProviderShared.IMAGE_MIMES));
|
|
272
|
+
return {
|
|
273
|
+
type: "image",
|
|
274
|
+
source: {
|
|
275
|
+
type: "base64",
|
|
276
|
+
media_type: media.mime,
|
|
277
|
+
data: media.base64,
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
const lowerToolResultContent = Effect.fn("AnthropicMessages.lowerToolResultContent")(function* (part) {
|
|
282
|
+
// Text / json / error results stay as a string for backward compatibility
|
|
283
|
+
// with existing cassettes and provider expectations.
|
|
284
|
+
if (part.result.type !== "content")
|
|
285
|
+
return ProviderShared.toolResultText(part);
|
|
286
|
+
// Preserve the narrowed array element type when compiled through a consumer package.
|
|
287
|
+
const content = part.result.value;
|
|
288
|
+
return yield* Effect.forEach(content, lowerToolResultContentItem);
|
|
289
|
+
});
|
|
290
|
+
// Mid-conversation system messages are a native Claude API feature only for
|
|
291
|
+
// Opus 4.8. Other Anthropic models intentionally use the same visible wrapped-
|
|
292
|
+
// user fallback as non-Anthropic routes rather than sending a role they reject.
|
|
293
|
+
const supportsNativeSystemUpdates = (request) => String(request.model.id) === "claude-opus-4-8";
|
|
294
|
+
const endsInServerToolUse = (message) => {
|
|
295
|
+
const last = message.content.at(-1);
|
|
296
|
+
return message.role === "assistant" && last?.type === "tool-call" && last.providerExecuted === true;
|
|
297
|
+
};
|
|
298
|
+
const canUseNativeSystemUpdate = (messages, index) => {
|
|
299
|
+
const previous = messages[index - 1];
|
|
300
|
+
const next = messages[index + 1];
|
|
301
|
+
return (previous !== undefined &&
|
|
302
|
+
previous.role !== "system" &&
|
|
303
|
+
(previous.role === "user" || previous.role === "tool" || endsInServerToolUse(previous)) &&
|
|
304
|
+
next?.role !== "system" &&
|
|
305
|
+
(next === undefined || next.role === "assistant"));
|
|
306
|
+
};
|
|
307
|
+
const splitsLocalToolResults = (messages, index) => {
|
|
308
|
+
const pending = new Set();
|
|
309
|
+
for (const message of messages.slice(0, index)) {
|
|
310
|
+
for (const part of message.content) {
|
|
311
|
+
if (message.role === "assistant" && part.type === "tool-call" && part.providerExecuted !== true)
|
|
312
|
+
pending.add(part.id);
|
|
313
|
+
if (message.role === "tool" && part.type === "tool-result")
|
|
314
|
+
pending.delete(part.id);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return pending.size > 0;
|
|
318
|
+
};
|
|
319
|
+
const lowerNativeSystemUpdate = Effect.fn("AnthropicMessages.lowerNativeSystemUpdate")(function* (message, breakpoints) {
|
|
320
|
+
const content = yield* ProviderShared.systemUpdateText("Anthropic Messages", message);
|
|
321
|
+
return {
|
|
322
|
+
role: "system",
|
|
323
|
+
content: content.map((part) => ({
|
|
324
|
+
type: "text",
|
|
325
|
+
text: part.text,
|
|
326
|
+
cache_control: cacheControl(breakpoints, part.cache),
|
|
327
|
+
})),
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
const lowerMessages = Effect.fn("AnthropicMessages.lowerMessages")(function* (request, breakpoints) {
|
|
331
|
+
const messages = [];
|
|
332
|
+
for (const [index, message] of request.messages.entries()) {
|
|
333
|
+
if (message.role === "system") {
|
|
334
|
+
if (splitsLocalToolResults(request.messages, index))
|
|
335
|
+
return yield* invalid("Anthropic Messages system updates cannot split a local tool call from its tool result");
|
|
336
|
+
if (supportsNativeSystemUpdates(request) && canUseNativeSystemUpdate(request.messages, index)) {
|
|
337
|
+
messages.push(yield* lowerNativeSystemUpdate(message, breakpoints));
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const part = yield* ProviderShared.wrappedSystemUpdate("Anthropic Messages", message);
|
|
341
|
+
const block = { type: "text", text: part.text, cache_control: cacheControl(breakpoints, part.cache) };
|
|
342
|
+
const previous = messages.at(-1);
|
|
343
|
+
if (previous?.role === "user")
|
|
344
|
+
messages[messages.length - 1] = { role: "user", content: [...previous.content, block] };
|
|
345
|
+
else
|
|
346
|
+
messages.push({ role: "user", content: [block] });
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (message.role === "user") {
|
|
350
|
+
const content = [];
|
|
351
|
+
for (const part of message.content) {
|
|
352
|
+
if (part.type === "text") {
|
|
353
|
+
content.push({ type: "text", text: part.text, cache_control: cacheControl(breakpoints, part.cache) });
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (part.type === "media") {
|
|
357
|
+
content.push(yield* lowerImage(part));
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
return yield* ProviderShared.unsupportedContent("Anthropic Messages", "user", ["text", "media"]);
|
|
361
|
+
}
|
|
362
|
+
messages.push({ role: "user", content });
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (message.role === "assistant") {
|
|
366
|
+
const content = [];
|
|
367
|
+
for (const part of message.content) {
|
|
368
|
+
if (part.type === "text") {
|
|
369
|
+
content.push({ type: "text", text: part.text, cache_control: cacheControl(breakpoints, part.cache) });
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (part.type === "reasoning") {
|
|
373
|
+
content.push({
|
|
374
|
+
type: "thinking",
|
|
375
|
+
thinking: part.text,
|
|
376
|
+
signature: part.encrypted ?? signatureFromMetadata(part.providerMetadata),
|
|
377
|
+
});
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (part.type === "tool-call") {
|
|
381
|
+
content.push(part.providerExecuted ? lowerServerToolCall(part) : lowerToolCall(part));
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (part.type === "tool-result" && part.providerExecuted) {
|
|
385
|
+
content.push(yield* lowerServerToolResult(part));
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
return yield* invalid(`Anthropic Messages assistant messages only support text, reasoning, and tool-call content for now`);
|
|
389
|
+
}
|
|
390
|
+
messages.push({ role: "assistant", content });
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
const content = [];
|
|
394
|
+
for (const part of message.content) {
|
|
395
|
+
if (!ProviderShared.supportsContent(part, ["tool-result"]))
|
|
396
|
+
return yield* ProviderShared.unsupportedContent("Anthropic Messages", "tool", ["tool-result"]);
|
|
397
|
+
content.push({
|
|
398
|
+
type: "tool_result",
|
|
399
|
+
tool_use_id: part.id,
|
|
400
|
+
content: yield* lowerToolResultContent(part),
|
|
401
|
+
is_error: part.result.type === "error" ? true : undefined,
|
|
402
|
+
cache_control: cacheControl(breakpoints, part.cache),
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
messages.push({ role: "user", content });
|
|
406
|
+
}
|
|
407
|
+
return messages;
|
|
408
|
+
});
|
|
409
|
+
const anthropicOptions = (request) => request.providerOptions?.anthropic;
|
|
410
|
+
const lowerThinking = Effect.fn("AnthropicMessages.lowerThinking")(function* (request) {
|
|
411
|
+
const thinking = anthropicOptions(request)?.thinking;
|
|
412
|
+
if (!ProviderShared.isRecord(thinking))
|
|
413
|
+
return undefined;
|
|
414
|
+
if (thinking.type === "adaptive") {
|
|
415
|
+
const display = thinking.display === "summarized"
|
|
416
|
+
? "summarized"
|
|
417
|
+
: thinking.display === "omitted"
|
|
418
|
+
? "omitted"
|
|
419
|
+
: undefined;
|
|
420
|
+
return { type: "adaptive", ...(display === undefined ? {} : { display }) };
|
|
421
|
+
}
|
|
422
|
+
if (thinking.type === "disabled")
|
|
423
|
+
return { type: "disabled" };
|
|
424
|
+
if (thinking.type !== "enabled")
|
|
425
|
+
return undefined;
|
|
426
|
+
const budget = typeof thinking.budgetTokens === "number"
|
|
427
|
+
? thinking.budgetTokens
|
|
428
|
+
: typeof thinking.budget_tokens === "number"
|
|
429
|
+
? thinking.budget_tokens
|
|
430
|
+
: undefined;
|
|
431
|
+
if (budget === undefined)
|
|
432
|
+
return yield* invalid("Anthropic thinking provider option requires budgetTokens");
|
|
433
|
+
return { type: "enabled", budget_tokens: budget };
|
|
434
|
+
});
|
|
435
|
+
const outputConfig = (request) => {
|
|
436
|
+
const effort = anthropicOptions(request)?.effort;
|
|
437
|
+
return typeof effort === "string" ? { effort } : undefined;
|
|
438
|
+
};
|
|
439
|
+
const fromRequest = Effect.fn("AnthropicMessages.fromRequest")(function* (request) {
|
|
440
|
+
const toolChoice = request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined;
|
|
441
|
+
const generation = request.generation;
|
|
442
|
+
const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
|
|
443
|
+
const outputLimit = request.model.defaults?.limits?.output ?? request.model.route.defaults.limits?.output ?? 4096;
|
|
444
|
+
// Allocate the 4-breakpoint budget in invalidation order: tools → system →
|
|
445
|
+
// messages. Tools live highest in the cache hierarchy, so when callers
|
|
446
|
+
// over-mark we keep their tool hints and shed the message-tail ones first.
|
|
447
|
+
const breakpoints = Cache.newBreakpoints(ANTHROPIC_BREAKPOINT_CAP);
|
|
448
|
+
const tools = request.tools.length === 0 || request.toolChoice?.type === "none"
|
|
449
|
+
? undefined
|
|
450
|
+
: request.tools.map((tool) => lowerTool(breakpoints, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility)));
|
|
451
|
+
const system = request.system.length === 0
|
|
452
|
+
? undefined
|
|
453
|
+
: request.system.map((part) => ({
|
|
454
|
+
type: "text",
|
|
455
|
+
text: part.text,
|
|
456
|
+
cache_control: cacheControl(breakpoints, part.cache),
|
|
457
|
+
}));
|
|
458
|
+
const messages = yield* lowerMessages(request, breakpoints);
|
|
459
|
+
if (breakpoints.dropped > 0) {
|
|
460
|
+
yield* Effect.logWarning(`Anthropic Messages: dropped ${breakpoints.dropped} cache breakpoint(s); the API allows at most ${ANTHROPIC_BREAKPOINT_CAP} per request.`);
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
model: request.model.id,
|
|
464
|
+
system,
|
|
465
|
+
messages,
|
|
466
|
+
tools,
|
|
467
|
+
tool_choice: toolChoice,
|
|
468
|
+
stream: true,
|
|
469
|
+
max_tokens: generation?.maxTokens ?? outputLimit,
|
|
470
|
+
temperature: generation?.temperature,
|
|
471
|
+
top_p: generation?.topP,
|
|
472
|
+
top_k: generation?.topK,
|
|
473
|
+
stop_sequences: generation?.stop,
|
|
474
|
+
thinking: yield* lowerThinking(request),
|
|
475
|
+
output_config: outputConfig(request),
|
|
476
|
+
};
|
|
477
|
+
});
|
|
478
|
+
// =============================================================================
|
|
479
|
+
// Stream Parsing
|
|
480
|
+
// =============================================================================
|
|
481
|
+
const mapFinishReason = (reason) => {
|
|
482
|
+
if (reason === "end_turn" || reason === "stop_sequence" || reason === "pause_turn")
|
|
483
|
+
return "stop";
|
|
484
|
+
if (reason === "max_tokens")
|
|
485
|
+
return "length";
|
|
486
|
+
if (reason === "tool_use")
|
|
487
|
+
return "tool-calls";
|
|
488
|
+
if (reason === "refusal")
|
|
489
|
+
return "content-filter";
|
|
490
|
+
return "unknown";
|
|
491
|
+
};
|
|
492
|
+
// Anthropic reports the non-overlapping breakdown natively — its
|
|
493
|
+
// `input_tokens` is the *non-cached* count per the Messages API docs, with
|
|
494
|
+
// cache reads and writes as separate fields. We sum them to derive the
|
|
495
|
+
// inclusive `inputTokens` the rest of the contract expects. Extended
|
|
496
|
+
// thinking tokens are *not* broken out by Anthropic — they're billed as
|
|
497
|
+
// part of `output_tokens`, so `reasoningTokens` stays `undefined` and
|
|
498
|
+
// `outputTokens` carries the combined total.
|
|
499
|
+
const mapUsage = (usage) => {
|
|
500
|
+
if (!usage)
|
|
501
|
+
return undefined;
|
|
502
|
+
const nonCached = usage.input_tokens;
|
|
503
|
+
const cacheRead = usage.cache_read_input_tokens ?? undefined;
|
|
504
|
+
const cacheWrite = usage.cache_creation_input_tokens ?? undefined;
|
|
505
|
+
const inputTokens = ProviderShared.sumTokens(nonCached, cacheRead, cacheWrite);
|
|
506
|
+
return new Usage({
|
|
507
|
+
inputTokens,
|
|
508
|
+
outputTokens: usage.output_tokens,
|
|
509
|
+
nonCachedInputTokens: nonCached,
|
|
510
|
+
cacheReadInputTokens: cacheRead,
|
|
511
|
+
cacheWriteInputTokens: cacheWrite,
|
|
512
|
+
totalTokens: ProviderShared.totalTokens(inputTokens, usage.output_tokens, undefined),
|
|
513
|
+
providerMetadata: { anthropic: usage },
|
|
514
|
+
});
|
|
515
|
+
};
|
|
516
|
+
// Anthropic emits usage on `message_start` and again on `message_delta` — the
|
|
517
|
+
// final delta carries the authoritative totals. Right-biased merge: each
|
|
518
|
+
// field prefers `right` when defined, falls back to `left`. `inputTokens` is
|
|
519
|
+
// recomputed from the merged breakdown so the inclusive total stays
|
|
520
|
+
// consistent with `nonCached + cacheRead + cacheWrite`.
|
|
521
|
+
const mergeUsage = (left, right) => {
|
|
522
|
+
if (!left)
|
|
523
|
+
return right;
|
|
524
|
+
if (!right)
|
|
525
|
+
return left;
|
|
526
|
+
const nonCachedInputTokens = right.nonCachedInputTokens ?? left.nonCachedInputTokens;
|
|
527
|
+
const cacheReadInputTokens = right.cacheReadInputTokens ?? left.cacheReadInputTokens;
|
|
528
|
+
const cacheWriteInputTokens = right.cacheWriteInputTokens ?? left.cacheWriteInputTokens;
|
|
529
|
+
const inputTokens = ProviderShared.sumTokens(nonCachedInputTokens, cacheReadInputTokens, cacheWriteInputTokens);
|
|
530
|
+
const outputTokens = right.outputTokens ?? left.outputTokens;
|
|
531
|
+
return new Usage({
|
|
532
|
+
inputTokens,
|
|
533
|
+
outputTokens,
|
|
534
|
+
nonCachedInputTokens,
|
|
535
|
+
cacheReadInputTokens,
|
|
536
|
+
cacheWriteInputTokens,
|
|
537
|
+
totalTokens: ProviderShared.totalTokens(inputTokens, outputTokens, undefined),
|
|
538
|
+
providerMetadata: {
|
|
539
|
+
anthropic: {
|
|
540
|
+
...left.providerMetadata?.["anthropic"],
|
|
541
|
+
...right.providerMetadata?.["anthropic"],
|
|
542
|
+
},
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
};
|
|
546
|
+
// Server tool result blocks come whole in `content_block_start` (no streaming
|
|
547
|
+
// delta sequence). We convert the payload to a `tool-result` event with
|
|
548
|
+
// `providerExecuted: true`. The runtime appends it to the assistant message
|
|
549
|
+
// for round-trip; downstream consumers can inspect `result.value` for the
|
|
550
|
+
// structured payload.
|
|
551
|
+
const SERVER_TOOL_RESULT_NAMES = {
|
|
552
|
+
web_search_tool_result: "web_search",
|
|
553
|
+
code_execution_tool_result: "code_execution",
|
|
554
|
+
web_fetch_tool_result: "web_fetch",
|
|
555
|
+
};
|
|
556
|
+
const isServerToolResultType = (type) => type in SERVER_TOOL_RESULT_NAMES;
|
|
557
|
+
const serverToolResultEvent = (block) => {
|
|
558
|
+
if (!block.type || !isServerToolResultType(block.type))
|
|
559
|
+
return undefined;
|
|
560
|
+
const errorPayload = typeof block.content === "object" && block.content !== null && "type" in block.content
|
|
561
|
+
? String(block.content.type)
|
|
562
|
+
: "";
|
|
563
|
+
const isError = errorPayload.endsWith("_tool_result_error");
|
|
564
|
+
return LLMEvent.toolResult({
|
|
565
|
+
id: block.tool_use_id ?? "",
|
|
566
|
+
name: SERVER_TOOL_RESULT_NAMES[block.type],
|
|
567
|
+
result: isError ? { type: "error", value: block.content } : { type: "json", value: block.content },
|
|
568
|
+
providerExecuted: true,
|
|
569
|
+
providerMetadata: anthropicMetadata({ blockType: block.type }),
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
const NO_EVENTS = [];
|
|
573
|
+
const onMessageStart = (state, event) => {
|
|
574
|
+
const usage = mapUsage(event.message?.usage);
|
|
575
|
+
return [usage ? { ...state, usage: mergeUsage(state.usage, usage) } : state, NO_EVENTS];
|
|
576
|
+
};
|
|
577
|
+
const onContentBlockStart = (state, event) => {
|
|
578
|
+
const block = event.content_block;
|
|
579
|
+
if (!block)
|
|
580
|
+
return [state, NO_EVENTS];
|
|
581
|
+
if ((block.type === "tool_use" || block.type === "server_tool_use") && event.index !== undefined) {
|
|
582
|
+
const events = [];
|
|
583
|
+
const lifecycle = Lifecycle.stepStart(state.lifecycle, events);
|
|
584
|
+
return [
|
|
585
|
+
{
|
|
586
|
+
...state,
|
|
587
|
+
lifecycle,
|
|
588
|
+
tools: ToolStream.start(state.tools, event.index, {
|
|
589
|
+
id: block.id ?? String(event.index),
|
|
590
|
+
name: block.name ?? "",
|
|
591
|
+
providerExecuted: block.type === "server_tool_use",
|
|
592
|
+
}),
|
|
593
|
+
},
|
|
594
|
+
[...events, LLMEvent.toolInputStart({ id: block.id ?? String(event.index), name: block.name ?? "" })],
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
if (block.type === "text" && block.text) {
|
|
598
|
+
const events = [];
|
|
599
|
+
return [
|
|
600
|
+
{ ...state, lifecycle: Lifecycle.textDelta(state.lifecycle, events, `text-${event.index ?? 0}`, block.text) },
|
|
601
|
+
events,
|
|
602
|
+
];
|
|
603
|
+
}
|
|
604
|
+
if (block.type === "thinking" && block.thinking) {
|
|
605
|
+
const events = [];
|
|
606
|
+
return [
|
|
607
|
+
{
|
|
608
|
+
...state,
|
|
609
|
+
lifecycle: Lifecycle.reasoningDelta(state.lifecycle, events, `reasoning-${event.index ?? 0}`, block.thinking),
|
|
610
|
+
},
|
|
611
|
+
events,
|
|
612
|
+
];
|
|
613
|
+
}
|
|
614
|
+
const result = serverToolResultEvent(block);
|
|
615
|
+
if (!result)
|
|
616
|
+
return [state, NO_EVENTS];
|
|
617
|
+
const events = [];
|
|
618
|
+
return [{ ...state, lifecycle: Lifecycle.stepStart(state.lifecycle, events) }, [...events, result]];
|
|
619
|
+
};
|
|
620
|
+
const onContentBlockDelta = Effect.fn("AnthropicMessages.onContentBlockDelta")(function* (state, event) {
|
|
621
|
+
const delta = event.delta;
|
|
622
|
+
if (delta?.type === "text_delta" && delta.text) {
|
|
623
|
+
const events = [];
|
|
624
|
+
return [
|
|
625
|
+
{ ...state, lifecycle: Lifecycle.textDelta(state.lifecycle, events, `text-${event.index ?? 0}`, delta.text) },
|
|
626
|
+
events,
|
|
627
|
+
];
|
|
628
|
+
}
|
|
629
|
+
if (delta?.type === "thinking_delta" && delta.thinking) {
|
|
630
|
+
const events = [];
|
|
631
|
+
return [
|
|
632
|
+
{
|
|
633
|
+
...state,
|
|
634
|
+
lifecycle: Lifecycle.reasoningDelta(state.lifecycle, events, `reasoning-${event.index ?? 0}`, delta.thinking),
|
|
635
|
+
},
|
|
636
|
+
events,
|
|
637
|
+
];
|
|
638
|
+
}
|
|
639
|
+
if (delta?.type === "signature_delta" && delta.signature) {
|
|
640
|
+
const events = [];
|
|
641
|
+
return [
|
|
642
|
+
{
|
|
643
|
+
...state,
|
|
644
|
+
lifecycle: Lifecycle.reasoningEnd(state.lifecycle, events, `reasoning-${event.index ?? 0}`, anthropicMetadata({ signature: delta.signature })),
|
|
645
|
+
},
|
|
646
|
+
events,
|
|
647
|
+
];
|
|
648
|
+
}
|
|
649
|
+
if (delta?.type === "input_json_delta" && event.index !== undefined) {
|
|
650
|
+
if (!delta.partial_json)
|
|
651
|
+
return [state, NO_EVENTS];
|
|
652
|
+
const result = ToolStream.appendExisting(ADAPTER, state.tools, event.index, delta.partial_json, "Anthropic Messages tool argument delta is missing its tool call");
|
|
653
|
+
if (ToolStream.isError(result))
|
|
654
|
+
return yield* result;
|
|
655
|
+
const events = [];
|
|
656
|
+
const lifecycle = result.events.length ? Lifecycle.stepStart(state.lifecycle, events) : state.lifecycle;
|
|
657
|
+
events.push(...result.events);
|
|
658
|
+
return [{ ...state, lifecycle, tools: result.tools }, events];
|
|
659
|
+
}
|
|
660
|
+
return [state, NO_EVENTS];
|
|
661
|
+
});
|
|
662
|
+
const onContentBlockStop = Effect.fn("AnthropicMessages.onContentBlockStop")(function* (state, event) {
|
|
663
|
+
if (event.index === undefined)
|
|
664
|
+
return [state, NO_EVENTS];
|
|
665
|
+
const result = yield* ToolStream.finish(ADAPTER, state.tools, event.index);
|
|
666
|
+
const events = [];
|
|
667
|
+
const resultEvents = result.events ?? [];
|
|
668
|
+
const lifecycle = resultEvents.length
|
|
669
|
+
? Lifecycle.stepStart(state.lifecycle, events)
|
|
670
|
+
: Lifecycle.reasoningEnd(Lifecycle.textEnd(state.lifecycle, events, `text-${event.index}`), events, `reasoning-${event.index}`);
|
|
671
|
+
events.push(...resultEvents);
|
|
672
|
+
return [{ ...state, lifecycle, tools: result.tools }, events];
|
|
673
|
+
});
|
|
674
|
+
const onMessageDelta = (state, event) => {
|
|
675
|
+
const usage = mergeUsage(state.usage, mapUsage(event.usage));
|
|
676
|
+
const events = [];
|
|
677
|
+
const lifecycle = Lifecycle.finish(state.lifecycle, events, {
|
|
678
|
+
reason: mapFinishReason(event.delta?.stop_reason),
|
|
679
|
+
usage,
|
|
680
|
+
providerMetadata: event.delta?.stop_sequence
|
|
681
|
+
? anthropicMetadata({ stopSequence: event.delta.stop_sequence })
|
|
682
|
+
: undefined,
|
|
683
|
+
});
|
|
684
|
+
return [{ ...state, lifecycle, usage }, events];
|
|
685
|
+
};
|
|
686
|
+
// Prefix `error.type` so overloads, rate limits, and quota errors are visible
|
|
687
|
+
// even when the provider message is generic or empty.
|
|
688
|
+
const providerErrorMessage = (event) => {
|
|
689
|
+
const type = event.error?.type;
|
|
690
|
+
const message = event.error?.message;
|
|
691
|
+
if (type && message)
|
|
692
|
+
return `${type}: ${message}`;
|
|
693
|
+
return message || type || "Anthropic Messages stream error";
|
|
694
|
+
};
|
|
695
|
+
const onError = (event) => new LLMError({
|
|
696
|
+
module: ADAPTER,
|
|
697
|
+
method: "stream",
|
|
698
|
+
reason: classifyProviderFailure({ message: providerErrorMessage(event), code: event.error?.type }),
|
|
699
|
+
});
|
|
700
|
+
const step = (state, event) => {
|
|
701
|
+
if (event.type === "message_start")
|
|
702
|
+
return Effect.succeed(onMessageStart(state, event));
|
|
703
|
+
if (event.type === "content_block_start")
|
|
704
|
+
return Effect.succeed(onContentBlockStart(state, event));
|
|
705
|
+
if (event.type === "content_block_delta")
|
|
706
|
+
return onContentBlockDelta(state, event);
|
|
707
|
+
if (event.type === "content_block_stop")
|
|
708
|
+
return onContentBlockStop(state, event);
|
|
709
|
+
if (event.type === "message_delta")
|
|
710
|
+
return Effect.succeed(onMessageDelta(state, event));
|
|
711
|
+
if (event.type === "error")
|
|
712
|
+
return onError(event);
|
|
713
|
+
return Effect.succeed([state, NO_EVENTS]);
|
|
714
|
+
};
|
|
715
|
+
// =============================================================================
|
|
716
|
+
// Protocol And Anthropic Route
|
|
717
|
+
// =============================================================================
|
|
718
|
+
/**
|
|
719
|
+
* The Anthropic Messages protocol — request body construction, body schema,
|
|
720
|
+
* and the streaming-event state machine. Used by native Anthropic Cloud and
|
|
721
|
+
* (once registered) Vertex Anthropic / Bedrock-hosted Anthropic passthrough.
|
|
722
|
+
*/
|
|
723
|
+
export const protocol = Protocol.make({
|
|
724
|
+
id: ADAPTER,
|
|
725
|
+
body: {
|
|
726
|
+
schema: AnthropicMessagesBody,
|
|
727
|
+
from: fromRequest,
|
|
728
|
+
},
|
|
729
|
+
stream: {
|
|
730
|
+
event: Protocol.jsonEvent(AnthropicEvent),
|
|
731
|
+
initial: () => ({ tools: ToolStream.empty(), lifecycle: Lifecycle.initial() }),
|
|
732
|
+
step,
|
|
733
|
+
},
|
|
734
|
+
});
|
|
735
|
+
export const route = Route.make({
|
|
736
|
+
id: ADAPTER,
|
|
737
|
+
provider: "anthropic",
|
|
738
|
+
providerMetadataKey: "anthropic",
|
|
739
|
+
protocol,
|
|
740
|
+
endpoint: Endpoint.path(PATH, { baseURL: DEFAULT_BASE_URL }),
|
|
741
|
+
auth: Auth.none,
|
|
742
|
+
framing: Framing.sse,
|
|
743
|
+
headers: () => ({ "anthropic-version": "2023-06-01" }),
|
|
744
|
+
});
|
|
745
|
+
export * as AnthropicMessages from "./anthropic-messages";
|