@draht/ai 2026.3.2-2
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 +1185 -0
- package/dist/api-registry.d.ts +20 -0
- package/dist/api-registry.d.ts.map +1 -0
- package/dist/api-registry.js +44 -0
- package/dist/api-registry.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/env-api-keys.d.ts +9 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +99 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +24 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +13133 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +12939 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +55 -0
- package/dist/models.js.map +1 -0
- package/dist/providers/amazon-bedrock.d.ts +15 -0
- package/dist/providers/amazon-bedrock.d.ts.map +1 -0
- package/dist/providers/amazon-bedrock.js +585 -0
- package/dist/providers/amazon-bedrock.js.map +1 -0
- package/dist/providers/anthropic.d.ts +33 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +729 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/azure-openai-responses.d.ts +15 -0
- package/dist/providers/azure-openai-responses.d.ts.map +1 -0
- package/dist/providers/azure-openai-responses.js +184 -0
- package/dist/providers/azure-openai-responses.js.map +1 -0
- package/dist/providers/github-copilot-headers.d.ts +8 -0
- package/dist/providers/github-copilot-headers.d.ts.map +1 -0
- package/dist/providers/github-copilot-headers.js +29 -0
- package/dist/providers/github-copilot-headers.js.map +1 -0
- package/dist/providers/google-gemini-cli.d.ts +74 -0
- package/dist/providers/google-gemini-cli.d.ts.map +1 -0
- package/dist/providers/google-gemini-cli.js +735 -0
- package/dist/providers/google-gemini-cli.js.map +1 -0
- package/dist/providers/google-shared.d.ts +65 -0
- package/dist/providers/google-shared.d.ts.map +1 -0
- package/dist/providers/google-shared.js +306 -0
- package/dist/providers/google-shared.js.map +1 -0
- package/dist/providers/google-vertex.d.ts +15 -0
- package/dist/providers/google-vertex.d.ts.map +1 -0
- package/dist/providers/google-vertex.js +371 -0
- package/dist/providers/google-vertex.js.map +1 -0
- package/dist/providers/google.d.ts +13 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +352 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/openai-codex-responses.d.ts +9 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -0
- package/dist/providers/openai-codex-responses.js +699 -0
- package/dist/providers/openai-codex-responses.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +15 -0
- package/dist/providers/openai-completions.d.ts.map +1 -0
- package/dist/providers/openai-completions.js +712 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses-shared.d.ts +17 -0
- package/dist/providers/openai-responses-shared.d.ts.map +1 -0
- package/dist/providers/openai-responses-shared.js +427 -0
- package/dist/providers/openai-responses-shared.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +13 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +198 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/register-builtins.d.ts +3 -0
- package/dist/providers/register-builtins.d.ts.map +1 -0
- package/dist/providers/register-builtins.js +63 -0
- package/dist/providers/register-builtins.js.map +1 -0
- package/dist/providers/simple-options.d.ts +8 -0
- package/dist/providers/simple-options.d.ts.map +1 -0
- package/dist/providers/simple-options.js +35 -0
- package/dist/providers/simple-options.js.map +1 -0
- package/dist/providers/transform-messages.d.ts +8 -0
- package/dist/providers/transform-messages.d.ts.map +1 -0
- package/dist/providers/transform-messages.js +155 -0
- package/dist/providers/transform-messages.js.map +1 -0
- package/dist/stream.d.ts +9 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +28 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +279 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/event-stream.d.ts +21 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +81 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/http-proxy.d.ts +2 -0
- package/dist/utils/http-proxy.d.ts.map +1 -0
- package/dist/utils/http-proxy.js +15 -0
- package/dist/utils/http-proxy.js.map +1 -0
- package/dist/utils/json-parse.d.ts +9 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +29 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/oauth/anthropic.d.ts +17 -0
- package/dist/utils/oauth/anthropic.d.ts.map +1 -0
- package/dist/utils/oauth/anthropic.js +104 -0
- package/dist/utils/oauth/anthropic.js.map +1 -0
- package/dist/utils/oauth/github-copilot.d.ts +30 -0
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
- package/dist/utils/oauth/github-copilot.js +281 -0
- package/dist/utils/oauth/github-copilot.js.map +1 -0
- package/dist/utils/oauth/google-antigravity.d.ts +26 -0
- package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
- package/dist/utils/oauth/google-antigravity.js +373 -0
- package/dist/utils/oauth/google-antigravity.js.map +1 -0
- package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
- package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
- package/dist/utils/oauth/google-gemini-cli.js +478 -0
- package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
- package/dist/utils/oauth/index.d.ts +62 -0
- package/dist/utils/oauth/index.d.ts.map +1 -0
- package/dist/utils/oauth/index.js +133 -0
- package/dist/utils/oauth/index.js.map +1 -0
- package/dist/utils/oauth/openai-codex.d.ts +34 -0
- package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
- package/dist/utils/oauth/openai-codex.js +380 -0
- package/dist/utils/oauth/openai-codex.js.map +1 -0
- package/dist/utils/oauth/pkce.d.ts +13 -0
- package/dist/utils/oauth/pkce.d.ts.map +1 -0
- package/dist/utils/oauth/pkce.js +31 -0
- package/dist/utils/oauth/pkce.js.map +1 -0
- package/dist/utils/oauth/types.d.ts +47 -0
- package/dist/utils/oauth/types.d.ts.map +1 -0
- package/dist/utils/oauth/types.js +2 -0
- package/dist/utils/oauth/types.js.map +1 -0
- package/dist/utils/overflow.d.ts +52 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +115 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/validation.d.ts +18 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +72 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import { getEnvApiKey } from "../env-api-keys.js";
|
|
3
|
+
import { calculateCost } from "../models.js";
|
|
4
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
|
+
import { parseStreamingJson } from "../utils/json-parse.js";
|
|
6
|
+
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
7
|
+
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
|
|
8
|
+
import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
|
|
9
|
+
import { transformMessages } from "./transform-messages.js";
|
|
10
|
+
/**
|
|
11
|
+
* Resolve cache retention preference.
|
|
12
|
+
* Defaults to "short" and uses DRAHT_CACHE_RETENTION for backward compatibility.
|
|
13
|
+
*/
|
|
14
|
+
function resolveCacheRetention(cacheRetention) {
|
|
15
|
+
if (cacheRetention) {
|
|
16
|
+
return cacheRetention;
|
|
17
|
+
}
|
|
18
|
+
if (typeof process !== "undefined" && process.env.DRAHT_CACHE_RETENTION === "long") {
|
|
19
|
+
return "long";
|
|
20
|
+
}
|
|
21
|
+
return "short";
|
|
22
|
+
}
|
|
23
|
+
function getCacheControl(baseUrl, cacheRetention) {
|
|
24
|
+
const retention = resolveCacheRetention(cacheRetention);
|
|
25
|
+
if (retention === "none") {
|
|
26
|
+
return { retention };
|
|
27
|
+
}
|
|
28
|
+
const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : undefined;
|
|
29
|
+
return {
|
|
30
|
+
retention,
|
|
31
|
+
cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// Stealth mode: Mimic Claude Code's tool naming exactly
|
|
35
|
+
const claudeCodeVersion = "2.1.62";
|
|
36
|
+
// Claude Code 2.x tool names (canonical casing)
|
|
37
|
+
// Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
|
|
38
|
+
// To update: https://github.com/badlogic/cchistory
|
|
39
|
+
const claudeCodeTools = [
|
|
40
|
+
"Read",
|
|
41
|
+
"Write",
|
|
42
|
+
"Edit",
|
|
43
|
+
"Bash",
|
|
44
|
+
"Grep",
|
|
45
|
+
"Glob",
|
|
46
|
+
"AskUserQuestion",
|
|
47
|
+
"EnterPlanMode",
|
|
48
|
+
"ExitPlanMode",
|
|
49
|
+
"KillShell",
|
|
50
|
+
"NotebookEdit",
|
|
51
|
+
"Skill",
|
|
52
|
+
"Task",
|
|
53
|
+
"TaskOutput",
|
|
54
|
+
"TodoWrite",
|
|
55
|
+
"WebFetch",
|
|
56
|
+
"WebSearch",
|
|
57
|
+
];
|
|
58
|
+
const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
|
|
59
|
+
// Convert tool name to CC canonical casing if it matches (case-insensitive)
|
|
60
|
+
const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
|
|
61
|
+
const fromClaudeCodeName = (name, tools) => {
|
|
62
|
+
if (tools && tools.length > 0) {
|
|
63
|
+
const lowerName = name.toLowerCase();
|
|
64
|
+
const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
|
|
65
|
+
if (matchedTool)
|
|
66
|
+
return matchedTool.name;
|
|
67
|
+
}
|
|
68
|
+
return name;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Convert content blocks to Anthropic API format
|
|
72
|
+
*/
|
|
73
|
+
function convertContentBlocks(content) {
|
|
74
|
+
// If only text blocks, return as concatenated string for simplicity
|
|
75
|
+
const hasImages = content.some((c) => c.type === "image");
|
|
76
|
+
if (!hasImages) {
|
|
77
|
+
return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
|
|
78
|
+
}
|
|
79
|
+
// If we have images, convert to content block array
|
|
80
|
+
const blocks = content.map((block) => {
|
|
81
|
+
if (block.type === "text") {
|
|
82
|
+
return {
|
|
83
|
+
type: "text",
|
|
84
|
+
text: sanitizeSurrogates(block.text),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
type: "image",
|
|
89
|
+
source: {
|
|
90
|
+
type: "base64",
|
|
91
|
+
media_type: block.mimeType,
|
|
92
|
+
data: block.data,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
// If only images (no text), add placeholder text block
|
|
97
|
+
const hasText = blocks.some((b) => b.type === "text");
|
|
98
|
+
if (!hasText) {
|
|
99
|
+
blocks.unshift({
|
|
100
|
+
type: "text",
|
|
101
|
+
text: "(see attached image)",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return blocks;
|
|
105
|
+
}
|
|
106
|
+
function mergeHeaders(...headerSources) {
|
|
107
|
+
const merged = {};
|
|
108
|
+
for (const headers of headerSources) {
|
|
109
|
+
if (headers) {
|
|
110
|
+
Object.assign(merged, headers);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return merged;
|
|
114
|
+
}
|
|
115
|
+
export const streamAnthropic = (model, context, options) => {
|
|
116
|
+
const stream = new AssistantMessageEventStream();
|
|
117
|
+
(async () => {
|
|
118
|
+
const output = {
|
|
119
|
+
role: "assistant",
|
|
120
|
+
content: [],
|
|
121
|
+
api: model.api,
|
|
122
|
+
provider: model.provider,
|
|
123
|
+
model: model.id,
|
|
124
|
+
usage: {
|
|
125
|
+
input: 0,
|
|
126
|
+
output: 0,
|
|
127
|
+
cacheRead: 0,
|
|
128
|
+
cacheWrite: 0,
|
|
129
|
+
totalTokens: 0,
|
|
130
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
131
|
+
},
|
|
132
|
+
stopReason: "stop",
|
|
133
|
+
timestamp: Date.now(),
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
|
|
137
|
+
let copilotDynamicHeaders;
|
|
138
|
+
if (model.provider === "github-copilot") {
|
|
139
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
140
|
+
copilotDynamicHeaders = buildCopilotDynamicHeaders({
|
|
141
|
+
messages: context.messages,
|
|
142
|
+
hasImages,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const { client, isOAuthToken } = createClient(model, apiKey, options?.interleavedThinking ?? true, options?.headers, copilotDynamicHeaders);
|
|
146
|
+
const params = buildParams(model, context, isOAuthToken, options);
|
|
147
|
+
options?.onPayload?.(params);
|
|
148
|
+
const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
|
|
149
|
+
stream.push({ type: "start", partial: output });
|
|
150
|
+
const blocks = output.content;
|
|
151
|
+
for await (const event of anthropicStream) {
|
|
152
|
+
if (event.type === "message_start") {
|
|
153
|
+
// Capture initial token usage from message_start event
|
|
154
|
+
// This ensures we have input token counts even if the stream is aborted early
|
|
155
|
+
output.usage.input = event.message.usage.input_tokens || 0;
|
|
156
|
+
output.usage.output = event.message.usage.output_tokens || 0;
|
|
157
|
+
output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
|
|
158
|
+
output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
|
|
159
|
+
// Anthropic doesn't provide total_tokens, compute from components
|
|
160
|
+
output.usage.totalTokens =
|
|
161
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
162
|
+
calculateCost(model, output.usage);
|
|
163
|
+
}
|
|
164
|
+
else if (event.type === "content_block_start") {
|
|
165
|
+
if (event.content_block.type === "text") {
|
|
166
|
+
const block = {
|
|
167
|
+
type: "text",
|
|
168
|
+
text: "",
|
|
169
|
+
index: event.index,
|
|
170
|
+
};
|
|
171
|
+
output.content.push(block);
|
|
172
|
+
stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
|
|
173
|
+
}
|
|
174
|
+
else if (event.content_block.type === "thinking") {
|
|
175
|
+
const block = {
|
|
176
|
+
type: "thinking",
|
|
177
|
+
thinking: "",
|
|
178
|
+
thinkingSignature: "",
|
|
179
|
+
index: event.index,
|
|
180
|
+
};
|
|
181
|
+
output.content.push(block);
|
|
182
|
+
stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
|
|
183
|
+
}
|
|
184
|
+
else if (event.content_block.type === "redacted_thinking") {
|
|
185
|
+
const block = {
|
|
186
|
+
type: "thinking",
|
|
187
|
+
thinking: "[Reasoning redacted]",
|
|
188
|
+
thinkingSignature: event.content_block.data,
|
|
189
|
+
redacted: true,
|
|
190
|
+
index: event.index,
|
|
191
|
+
};
|
|
192
|
+
output.content.push(block);
|
|
193
|
+
stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
|
|
194
|
+
}
|
|
195
|
+
else if (event.content_block.type === "tool_use") {
|
|
196
|
+
const block = {
|
|
197
|
+
type: "toolCall",
|
|
198
|
+
id: event.content_block.id,
|
|
199
|
+
name: isOAuthToken
|
|
200
|
+
? fromClaudeCodeName(event.content_block.name, context.tools)
|
|
201
|
+
: event.content_block.name,
|
|
202
|
+
arguments: event.content_block.input ?? {},
|
|
203
|
+
partialJson: "",
|
|
204
|
+
index: event.index,
|
|
205
|
+
};
|
|
206
|
+
output.content.push(block);
|
|
207
|
+
stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else if (event.type === "content_block_delta") {
|
|
211
|
+
if (event.delta.type === "text_delta") {
|
|
212
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
213
|
+
const block = blocks[index];
|
|
214
|
+
if (block && block.type === "text") {
|
|
215
|
+
block.text += event.delta.text;
|
|
216
|
+
stream.push({
|
|
217
|
+
type: "text_delta",
|
|
218
|
+
contentIndex: index,
|
|
219
|
+
delta: event.delta.text,
|
|
220
|
+
partial: output,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (event.delta.type === "thinking_delta") {
|
|
225
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
226
|
+
const block = blocks[index];
|
|
227
|
+
if (block && block.type === "thinking") {
|
|
228
|
+
block.thinking += event.delta.thinking;
|
|
229
|
+
stream.push({
|
|
230
|
+
type: "thinking_delta",
|
|
231
|
+
contentIndex: index,
|
|
232
|
+
delta: event.delta.thinking,
|
|
233
|
+
partial: output,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
else if (event.delta.type === "input_json_delta") {
|
|
238
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
239
|
+
const block = blocks[index];
|
|
240
|
+
if (block && block.type === "toolCall") {
|
|
241
|
+
block.partialJson += event.delta.partial_json;
|
|
242
|
+
block.arguments = parseStreamingJson(block.partialJson);
|
|
243
|
+
stream.push({
|
|
244
|
+
type: "toolcall_delta",
|
|
245
|
+
contentIndex: index,
|
|
246
|
+
delta: event.delta.partial_json,
|
|
247
|
+
partial: output,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else if (event.delta.type === "signature_delta") {
|
|
252
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
253
|
+
const block = blocks[index];
|
|
254
|
+
if (block && block.type === "thinking") {
|
|
255
|
+
block.thinkingSignature = block.thinkingSignature || "";
|
|
256
|
+
block.thinkingSignature += event.delta.signature;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
else if (event.type === "content_block_stop") {
|
|
261
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
262
|
+
const block = blocks[index];
|
|
263
|
+
if (block) {
|
|
264
|
+
delete block.index;
|
|
265
|
+
if (block.type === "text") {
|
|
266
|
+
stream.push({
|
|
267
|
+
type: "text_end",
|
|
268
|
+
contentIndex: index,
|
|
269
|
+
content: block.text,
|
|
270
|
+
partial: output,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
else if (block.type === "thinking") {
|
|
274
|
+
stream.push({
|
|
275
|
+
type: "thinking_end",
|
|
276
|
+
contentIndex: index,
|
|
277
|
+
content: block.thinking,
|
|
278
|
+
partial: output,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
else if (block.type === "toolCall") {
|
|
282
|
+
block.arguments = parseStreamingJson(block.partialJson);
|
|
283
|
+
delete block.partialJson;
|
|
284
|
+
stream.push({
|
|
285
|
+
type: "toolcall_end",
|
|
286
|
+
contentIndex: index,
|
|
287
|
+
toolCall: block,
|
|
288
|
+
partial: output,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else if (event.type === "message_delta") {
|
|
294
|
+
if (event.delta.stop_reason) {
|
|
295
|
+
output.stopReason = mapStopReason(event.delta.stop_reason);
|
|
296
|
+
}
|
|
297
|
+
// Only update usage fields if present (not null).
|
|
298
|
+
// Preserves input_tokens from message_start when proxies omit it in message_delta.
|
|
299
|
+
if (event.usage.input_tokens != null) {
|
|
300
|
+
output.usage.input = event.usage.input_tokens;
|
|
301
|
+
}
|
|
302
|
+
if (event.usage.output_tokens != null) {
|
|
303
|
+
output.usage.output = event.usage.output_tokens;
|
|
304
|
+
}
|
|
305
|
+
if (event.usage.cache_read_input_tokens != null) {
|
|
306
|
+
output.usage.cacheRead = event.usage.cache_read_input_tokens;
|
|
307
|
+
}
|
|
308
|
+
if (event.usage.cache_creation_input_tokens != null) {
|
|
309
|
+
output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
|
|
310
|
+
}
|
|
311
|
+
// Anthropic doesn't provide total_tokens, compute from components
|
|
312
|
+
output.usage.totalTokens =
|
|
313
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
314
|
+
calculateCost(model, output.usage);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (options?.signal?.aborted) {
|
|
318
|
+
throw new Error("Request was aborted");
|
|
319
|
+
}
|
|
320
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
321
|
+
throw new Error("An unknown error occurred");
|
|
322
|
+
}
|
|
323
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
324
|
+
stream.end();
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
for (const block of output.content)
|
|
328
|
+
delete block.index;
|
|
329
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
330
|
+
output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
|
|
331
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
332
|
+
stream.end();
|
|
333
|
+
}
|
|
334
|
+
})();
|
|
335
|
+
return stream;
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* Check if a model supports adaptive thinking (Opus 4.6 and Sonnet 4.6)
|
|
339
|
+
*/
|
|
340
|
+
function supportsAdaptiveThinking(modelId) {
|
|
341
|
+
// Opus 4.6 and Sonnet 4.6 model IDs (with or without date suffix)
|
|
342
|
+
return (modelId.includes("opus-4-6") ||
|
|
343
|
+
modelId.includes("opus-4.6") ||
|
|
344
|
+
modelId.includes("sonnet-4-6") ||
|
|
345
|
+
modelId.includes("sonnet-4.6"));
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
|
|
349
|
+
* Note: effort "max" is only valid on Opus 4.6.
|
|
350
|
+
*/
|
|
351
|
+
function mapThinkingLevelToEffort(level, modelId) {
|
|
352
|
+
switch (level) {
|
|
353
|
+
case "minimal":
|
|
354
|
+
return "low";
|
|
355
|
+
case "low":
|
|
356
|
+
return "low";
|
|
357
|
+
case "medium":
|
|
358
|
+
return "medium";
|
|
359
|
+
case "high":
|
|
360
|
+
return "high";
|
|
361
|
+
case "xhigh":
|
|
362
|
+
return modelId.includes("opus-4-6") || modelId.includes("opus-4.6") ? "max" : "high";
|
|
363
|
+
default:
|
|
364
|
+
return "high";
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
export const streamSimpleAnthropic = (model, context, options) => {
|
|
368
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
369
|
+
if (!apiKey) {
|
|
370
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
371
|
+
}
|
|
372
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
373
|
+
if (!options?.reasoning) {
|
|
374
|
+
return streamAnthropic(model, context, { ...base, thinkingEnabled: false });
|
|
375
|
+
}
|
|
376
|
+
// For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
|
|
377
|
+
// For older models: use budget-based thinking
|
|
378
|
+
if (supportsAdaptiveThinking(model.id)) {
|
|
379
|
+
const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
|
|
380
|
+
return streamAnthropic(model, context, {
|
|
381
|
+
...base,
|
|
382
|
+
thinkingEnabled: true,
|
|
383
|
+
effort,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
const adjusted = adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
|
|
387
|
+
return streamAnthropic(model, context, {
|
|
388
|
+
...base,
|
|
389
|
+
maxTokens: adjusted.maxTokens,
|
|
390
|
+
thinkingEnabled: true,
|
|
391
|
+
thinkingBudgetTokens: adjusted.thinkingBudget,
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
function isOAuthToken(apiKey) {
|
|
395
|
+
return apiKey.includes("sk-ant-oat");
|
|
396
|
+
}
|
|
397
|
+
function createClient(model, apiKey, interleavedThinking, optionsHeaders, dynamicHeaders) {
|
|
398
|
+
// Adaptive thinking models (Opus 4.6, Sonnet 4.6) have interleaved thinking built-in.
|
|
399
|
+
// The beta header is deprecated on Opus 4.6 and redundant on Sonnet 4.6, so skip it.
|
|
400
|
+
const needsInterleavedBeta = interleavedThinking && !supportsAdaptiveThinking(model.id);
|
|
401
|
+
// Copilot: Bearer auth, selective betas (no fine-grained-tool-streaming)
|
|
402
|
+
if (model.provider === "github-copilot") {
|
|
403
|
+
const betaFeatures = [];
|
|
404
|
+
if (needsInterleavedBeta) {
|
|
405
|
+
betaFeatures.push("interleaved-thinking-2025-05-14");
|
|
406
|
+
}
|
|
407
|
+
const client = new Anthropic({
|
|
408
|
+
apiKey: null,
|
|
409
|
+
authToken: apiKey,
|
|
410
|
+
baseURL: model.baseUrl,
|
|
411
|
+
dangerouslyAllowBrowser: true,
|
|
412
|
+
defaultHeaders: mergeHeaders({
|
|
413
|
+
accept: "application/json",
|
|
414
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
415
|
+
...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
|
|
416
|
+
}, model.headers, dynamicHeaders, optionsHeaders),
|
|
417
|
+
});
|
|
418
|
+
return { client, isOAuthToken: false };
|
|
419
|
+
}
|
|
420
|
+
const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
|
|
421
|
+
if (needsInterleavedBeta) {
|
|
422
|
+
betaFeatures.push("interleaved-thinking-2025-05-14");
|
|
423
|
+
}
|
|
424
|
+
// OAuth: Bearer auth, Claude Code identity headers
|
|
425
|
+
if (isOAuthToken(apiKey)) {
|
|
426
|
+
const client = new Anthropic({
|
|
427
|
+
apiKey: null,
|
|
428
|
+
authToken: apiKey,
|
|
429
|
+
baseURL: model.baseUrl,
|
|
430
|
+
dangerouslyAllowBrowser: true,
|
|
431
|
+
defaultHeaders: mergeHeaders({
|
|
432
|
+
accept: "application/json",
|
|
433
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
434
|
+
"anthropic-beta": `claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")}`,
|
|
435
|
+
"user-agent": `claude-cli/${claudeCodeVersion}`,
|
|
436
|
+
"x-app": "cli",
|
|
437
|
+
}, model.headers, optionsHeaders),
|
|
438
|
+
});
|
|
439
|
+
return { client, isOAuthToken: true };
|
|
440
|
+
}
|
|
441
|
+
// API key auth
|
|
442
|
+
const client = new Anthropic({
|
|
443
|
+
apiKey,
|
|
444
|
+
baseURL: model.baseUrl,
|
|
445
|
+
dangerouslyAllowBrowser: true,
|
|
446
|
+
defaultHeaders: mergeHeaders({
|
|
447
|
+
accept: "application/json",
|
|
448
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
449
|
+
"anthropic-beta": betaFeatures.join(","),
|
|
450
|
+
}, model.headers, optionsHeaders),
|
|
451
|
+
});
|
|
452
|
+
return { client, isOAuthToken: false };
|
|
453
|
+
}
|
|
454
|
+
function buildParams(model, context, isOAuthToken, options) {
|
|
455
|
+
const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
|
|
456
|
+
const params = {
|
|
457
|
+
model: model.id,
|
|
458
|
+
messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
|
|
459
|
+
max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
|
|
460
|
+
stream: true,
|
|
461
|
+
};
|
|
462
|
+
// For OAuth tokens, we MUST include Claude Code identity
|
|
463
|
+
if (isOAuthToken) {
|
|
464
|
+
params.system = [
|
|
465
|
+
{
|
|
466
|
+
type: "text",
|
|
467
|
+
text: "You are Claude Code, Anthropic's official CLI for Claude.",
|
|
468
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
469
|
+
},
|
|
470
|
+
];
|
|
471
|
+
if (context.systemPrompt) {
|
|
472
|
+
params.system.push({
|
|
473
|
+
type: "text",
|
|
474
|
+
text: sanitizeSurrogates(context.systemPrompt),
|
|
475
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
else if (context.systemPrompt) {
|
|
480
|
+
// Add cache control to system prompt for non-OAuth tokens
|
|
481
|
+
params.system = [
|
|
482
|
+
{
|
|
483
|
+
type: "text",
|
|
484
|
+
text: sanitizeSurrogates(context.systemPrompt),
|
|
485
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
486
|
+
},
|
|
487
|
+
];
|
|
488
|
+
}
|
|
489
|
+
// Temperature is incompatible with extended thinking (adaptive or budget-based).
|
|
490
|
+
if (options?.temperature !== undefined && !options?.thinkingEnabled) {
|
|
491
|
+
params.temperature = options.temperature;
|
|
492
|
+
}
|
|
493
|
+
if (context.tools) {
|
|
494
|
+
params.tools = convertTools(context.tools, isOAuthToken);
|
|
495
|
+
}
|
|
496
|
+
// Configure thinking mode: adaptive (Opus 4.6 and Sonnet 4.6) or budget-based (older models)
|
|
497
|
+
if (options?.thinkingEnabled && model.reasoning) {
|
|
498
|
+
if (supportsAdaptiveThinking(model.id)) {
|
|
499
|
+
// Adaptive thinking: Claude decides when and how much to think
|
|
500
|
+
params.thinking = { type: "adaptive" };
|
|
501
|
+
if (options.effort) {
|
|
502
|
+
params.output_config = { effort: options.effort };
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
// Budget-based thinking for older models
|
|
507
|
+
params.thinking = {
|
|
508
|
+
type: "enabled",
|
|
509
|
+
budget_tokens: options.thinkingBudgetTokens || 1024,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (options?.metadata) {
|
|
514
|
+
const userId = options.metadata.user_id;
|
|
515
|
+
if (typeof userId === "string") {
|
|
516
|
+
params.metadata = { user_id: userId };
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (options?.toolChoice) {
|
|
520
|
+
if (typeof options.toolChoice === "string") {
|
|
521
|
+
params.tool_choice = { type: options.toolChoice };
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
params.tool_choice = options.toolChoice;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return params;
|
|
528
|
+
}
|
|
529
|
+
// Normalize tool call IDs to match Anthropic's required pattern and length
|
|
530
|
+
function normalizeToolCallId(id) {
|
|
531
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
532
|
+
}
|
|
533
|
+
function convertMessages(messages, model, isOAuthToken, cacheControl) {
|
|
534
|
+
const params = [];
|
|
535
|
+
// Transform messages for cross-provider compatibility
|
|
536
|
+
const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
|
|
537
|
+
for (let i = 0; i < transformedMessages.length; i++) {
|
|
538
|
+
const msg = transformedMessages[i];
|
|
539
|
+
if (msg.role === "user") {
|
|
540
|
+
if (typeof msg.content === "string") {
|
|
541
|
+
if (msg.content.trim().length > 0) {
|
|
542
|
+
params.push({
|
|
543
|
+
role: "user",
|
|
544
|
+
content: sanitizeSurrogates(msg.content),
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
const blocks = msg.content.map((item) => {
|
|
550
|
+
if (item.type === "text") {
|
|
551
|
+
return {
|
|
552
|
+
type: "text",
|
|
553
|
+
text: sanitizeSurrogates(item.text),
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
return {
|
|
558
|
+
type: "image",
|
|
559
|
+
source: {
|
|
560
|
+
type: "base64",
|
|
561
|
+
media_type: item.mimeType,
|
|
562
|
+
data: item.data,
|
|
563
|
+
},
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
let filteredBlocks = !model?.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
|
|
568
|
+
filteredBlocks = filteredBlocks.filter((b) => {
|
|
569
|
+
if (b.type === "text") {
|
|
570
|
+
return b.text.trim().length > 0;
|
|
571
|
+
}
|
|
572
|
+
return true;
|
|
573
|
+
});
|
|
574
|
+
if (filteredBlocks.length === 0)
|
|
575
|
+
continue;
|
|
576
|
+
params.push({
|
|
577
|
+
role: "user",
|
|
578
|
+
content: filteredBlocks,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
else if (msg.role === "assistant") {
|
|
583
|
+
const blocks = [];
|
|
584
|
+
for (const block of msg.content) {
|
|
585
|
+
if (block.type === "text") {
|
|
586
|
+
if (block.text.trim().length === 0)
|
|
587
|
+
continue;
|
|
588
|
+
blocks.push({
|
|
589
|
+
type: "text",
|
|
590
|
+
text: sanitizeSurrogates(block.text),
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
else if (block.type === "thinking") {
|
|
594
|
+
// Redacted thinking: pass the opaque payload back as redacted_thinking
|
|
595
|
+
if (block.redacted) {
|
|
596
|
+
blocks.push({
|
|
597
|
+
type: "redacted_thinking",
|
|
598
|
+
data: block.thinkingSignature,
|
|
599
|
+
});
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
if (block.thinking.trim().length === 0)
|
|
603
|
+
continue;
|
|
604
|
+
// If thinking signature is missing/empty (e.g., from aborted stream),
|
|
605
|
+
// convert to plain text block without <thinking> tags to avoid API rejection
|
|
606
|
+
// and prevent Claude from mimicking the tags in responses
|
|
607
|
+
if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
|
|
608
|
+
blocks.push({
|
|
609
|
+
type: "text",
|
|
610
|
+
text: sanitizeSurrogates(block.thinking),
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
blocks.push({
|
|
615
|
+
type: "thinking",
|
|
616
|
+
thinking: sanitizeSurrogates(block.thinking),
|
|
617
|
+
signature: block.thinkingSignature,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
else if (block.type === "toolCall") {
|
|
622
|
+
blocks.push({
|
|
623
|
+
type: "tool_use",
|
|
624
|
+
id: block.id,
|
|
625
|
+
name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
|
|
626
|
+
input: block.arguments ?? {},
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (blocks.length === 0)
|
|
631
|
+
continue;
|
|
632
|
+
params.push({
|
|
633
|
+
role: "assistant",
|
|
634
|
+
content: blocks,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
else if (msg.role === "toolResult") {
|
|
638
|
+
// Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
|
|
639
|
+
const toolResults = [];
|
|
640
|
+
// Add the current tool result
|
|
641
|
+
toolResults.push({
|
|
642
|
+
type: "tool_result",
|
|
643
|
+
tool_use_id: msg.toolCallId,
|
|
644
|
+
content: convertContentBlocks(msg.content),
|
|
645
|
+
is_error: msg.isError,
|
|
646
|
+
});
|
|
647
|
+
// Look ahead for consecutive toolResult messages
|
|
648
|
+
let j = i + 1;
|
|
649
|
+
while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
|
|
650
|
+
const nextMsg = transformedMessages[j]; // We know it's a toolResult
|
|
651
|
+
toolResults.push({
|
|
652
|
+
type: "tool_result",
|
|
653
|
+
tool_use_id: nextMsg.toolCallId,
|
|
654
|
+
content: convertContentBlocks(nextMsg.content),
|
|
655
|
+
is_error: nextMsg.isError,
|
|
656
|
+
});
|
|
657
|
+
j++;
|
|
658
|
+
}
|
|
659
|
+
// Skip the messages we've already processed
|
|
660
|
+
i = j - 1;
|
|
661
|
+
// Add a single user message with all tool results
|
|
662
|
+
params.push({
|
|
663
|
+
role: "user",
|
|
664
|
+
content: toolResults,
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
// Add cache_control to the last user message to cache conversation history
|
|
669
|
+
if (cacheControl && params.length > 0) {
|
|
670
|
+
const lastMessage = params[params.length - 1];
|
|
671
|
+
if (lastMessage.role === "user") {
|
|
672
|
+
if (Array.isArray(lastMessage.content)) {
|
|
673
|
+
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
|
674
|
+
if (lastBlock &&
|
|
675
|
+
(lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")) {
|
|
676
|
+
lastBlock.cache_control = cacheControl;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
else if (typeof lastMessage.content === "string") {
|
|
680
|
+
lastMessage.content = [
|
|
681
|
+
{
|
|
682
|
+
type: "text",
|
|
683
|
+
text: lastMessage.content,
|
|
684
|
+
cache_control: cacheControl,
|
|
685
|
+
},
|
|
686
|
+
];
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
return params;
|
|
691
|
+
}
|
|
692
|
+
function convertTools(tools, isOAuthToken) {
|
|
693
|
+
if (!tools)
|
|
694
|
+
return [];
|
|
695
|
+
return tools.map((tool) => {
|
|
696
|
+
const jsonSchema = tool.parameters; // TypeBox already generates JSON Schema
|
|
697
|
+
return {
|
|
698
|
+
name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
|
|
699
|
+
description: tool.description,
|
|
700
|
+
input_schema: {
|
|
701
|
+
type: "object",
|
|
702
|
+
properties: jsonSchema.properties || {},
|
|
703
|
+
required: jsonSchema.required || [],
|
|
704
|
+
},
|
|
705
|
+
};
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
function mapStopReason(reason) {
|
|
709
|
+
switch (reason) {
|
|
710
|
+
case "end_turn":
|
|
711
|
+
return "stop";
|
|
712
|
+
case "max_tokens":
|
|
713
|
+
return "length";
|
|
714
|
+
case "tool_use":
|
|
715
|
+
return "toolUse";
|
|
716
|
+
case "refusal":
|
|
717
|
+
return "error";
|
|
718
|
+
case "pause_turn": // Stop is good enough -> resubmit
|
|
719
|
+
return "stop";
|
|
720
|
+
case "stop_sequence":
|
|
721
|
+
return "stop"; // We don't supply stop sequences, so this should never happen
|
|
722
|
+
case "sensitive": // Content flagged by safety filters (not yet in SDK types)
|
|
723
|
+
return "error";
|
|
724
|
+
default:
|
|
725
|
+
// Handle unknown stop reasons gracefully (API may add new values)
|
|
726
|
+
throw new Error(`Unhandled stop reason: ${reason}`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
//# sourceMappingURL=anthropic.js.map
|