@mariozechner/pi-ai 0.5.31 → 0.5.34
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 +275 -10
- package/dist/agent/agent.d.ts +5 -0
- package/dist/agent/agent.d.ts.map +1 -0
- package/dist/agent/agent.js +161 -0
- package/dist/agent/agent.js.map +1 -0
- package/dist/agent/index.d.ts +4 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +3 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/tools/calculate.d.ts +17 -0
- package/dist/agent/tools/calculate.d.ts.map +1 -0
- package/dist/agent/tools/calculate.js +23 -0
- package/dist/agent/tools/calculate.js.map +1 -0
- package/dist/agent/tools/get-current-time.d.ts +20 -0
- package/dist/agent/tools/get-current-time.d.ts.map +1 -0
- package/dist/agent/tools/get-current-time.js +36 -0
- package/dist/agent/tools/get-current-time.js.map +1 -0
- package/dist/agent/tools/index.d.ts +3 -0
- package/dist/agent/tools/index.d.ts.map +1 -0
- package/dist/agent/tools/index.js +3 -0
- package/dist/agent/tools/index.js.map +1 -0
- package/dist/agent/types.d.ts +53 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent/types.js +2 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/event-stream.d.ts +19 -0
- package/dist/event-stream.d.ts.map +1 -0
- package/dist/event-stream.js +77 -0
- package/dist/event-stream.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/models.generated.d.ts +129 -10
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +234 -115
- package/dist/models.generated.js.map +1 -1
- package/dist/providers/anthropic.d.ts +3 -3
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +108 -56
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts +3 -3
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +56 -13
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +3 -3
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +92 -150
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses.d.ts +3 -3
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +59 -9
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/{utils.d.ts → transorm-messages.d.ts} +1 -1
- package/dist/providers/transorm-messages.d.ts.map +1 -0
- package/dist/providers/{utils.js → transorm-messages.js} +1 -1
- package/dist/providers/transorm-messages.js.map +1 -0
- package/dist/stream.d.ts +10 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/{generate.js → stream.js} +3 -65
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +28 -11
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/validation.d.ts +10 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +30 -0
- package/dist/validation.js.map +1 -0
- package/package.json +7 -5
- package/dist/generate.d.ts +0 -22
- package/dist/generate.d.ts.map +0 -1
- package/dist/generate.js.map +0 -1
- package/dist/providers/utils.d.ts.map +0 -1
- package/dist/providers/utils.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export interface AnthropicOptions extends
|
|
1
|
+
import type { StreamFunction, StreamOptions } from "../types.js";
|
|
2
|
+
export interface AnthropicOptions extends StreamOptions {
|
|
3
3
|
thinkingEnabled?: boolean;
|
|
4
4
|
thinkingBudgetTokens?: number;
|
|
5
5
|
toolChoice?: "auto" | "any" | "none" | {
|
|
@@ -7,5 +7,5 @@ export interface AnthropicOptions extends GenerateOptions {
|
|
|
7
7
|
name: string;
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
export declare const streamAnthropic:
|
|
10
|
+
export declare const streamAnthropic: StreamFunction<"anthropic-messages">;
|
|
11
11
|
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAOX,cAAc,EACd,aAAa,EAMb,MAAM,aAAa,CAAC;AAIrB,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CACtE;AAED,eAAO,MAAM,eAAe,EAAE,cAAc,CAAC,oBAAoB,CAgLhE,CAAC"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import {
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
import { AssistantMessageEventStream } from "../event-stream.js";
|
|
3
4
|
import { calculateCost } from "../models.js";
|
|
4
|
-
import {
|
|
5
|
+
import { validateToolArguments } from "../validation.js";
|
|
6
|
+
import { transformMessages } from "./transorm-messages.js";
|
|
5
7
|
export const streamAnthropic = (model, context, options) => {
|
|
6
|
-
const stream = new
|
|
8
|
+
const stream = new AssistantMessageEventStream();
|
|
7
9
|
(async () => {
|
|
8
10
|
const output = {
|
|
9
11
|
role: "assistant",
|
|
@@ -25,101 +27,128 @@ export const streamAnthropic = (model, context, options) => {
|
|
|
25
27
|
const params = buildParams(model, context, isOAuthToken, options);
|
|
26
28
|
const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
|
|
27
29
|
stream.push({ type: "start", partial: output });
|
|
28
|
-
|
|
30
|
+
const blocks = output.content;
|
|
29
31
|
for await (const event of anthropicStream) {
|
|
30
32
|
if (event.type === "content_block_start") {
|
|
31
33
|
if (event.content_block.type === "text") {
|
|
32
|
-
|
|
34
|
+
const block = {
|
|
33
35
|
type: "text",
|
|
34
36
|
text: "",
|
|
37
|
+
index: event.index,
|
|
35
38
|
};
|
|
36
|
-
output.content.push(
|
|
37
|
-
stream.push({ type: "text_start", partial: output });
|
|
39
|
+
output.content.push(block);
|
|
40
|
+
stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
|
|
38
41
|
}
|
|
39
42
|
else if (event.content_block.type === "thinking") {
|
|
40
|
-
|
|
43
|
+
const block = {
|
|
41
44
|
type: "thinking",
|
|
42
45
|
thinking: "",
|
|
43
46
|
thinkingSignature: "",
|
|
47
|
+
index: event.index,
|
|
44
48
|
};
|
|
45
|
-
output.content.push(
|
|
46
|
-
stream.push({ type: "thinking_start", partial: output });
|
|
49
|
+
output.content.push(block);
|
|
50
|
+
stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
|
|
47
51
|
}
|
|
48
52
|
else if (event.content_block.type === "tool_use") {
|
|
49
|
-
|
|
50
|
-
currentBlock = {
|
|
53
|
+
const block = {
|
|
51
54
|
type: "toolCall",
|
|
52
55
|
id: event.content_block.id,
|
|
53
56
|
name: event.content_block.name,
|
|
54
57
|
arguments: event.content_block.input,
|
|
55
58
|
partialJson: "",
|
|
59
|
+
index: event.index,
|
|
56
60
|
};
|
|
61
|
+
output.content.push(block);
|
|
62
|
+
stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
57
63
|
}
|
|
58
64
|
}
|
|
59
65
|
else if (event.type === "content_block_delta") {
|
|
60
66
|
if (event.delta.type === "text_delta") {
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
68
|
+
const block = blocks[index];
|
|
69
|
+
if (block && block.type === "text") {
|
|
70
|
+
block.text += event.delta.text;
|
|
63
71
|
stream.push({
|
|
64
72
|
type: "text_delta",
|
|
73
|
+
contentIndex: index,
|
|
65
74
|
delta: event.delta.text,
|
|
66
75
|
partial: output,
|
|
67
76
|
});
|
|
68
77
|
}
|
|
69
78
|
}
|
|
70
79
|
else if (event.delta.type === "thinking_delta") {
|
|
71
|
-
|
|
72
|
-
|
|
80
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
81
|
+
const block = blocks[index];
|
|
82
|
+
if (block && block.type === "thinking") {
|
|
83
|
+
block.thinking += event.delta.thinking;
|
|
73
84
|
stream.push({
|
|
74
85
|
type: "thinking_delta",
|
|
86
|
+
contentIndex: index,
|
|
75
87
|
delta: event.delta.thinking,
|
|
76
88
|
partial: output,
|
|
77
89
|
});
|
|
78
90
|
}
|
|
79
91
|
}
|
|
80
92
|
else if (event.delta.type === "input_json_delta") {
|
|
81
|
-
|
|
82
|
-
|
|
93
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
94
|
+
const block = blocks[index];
|
|
95
|
+
if (block && block.type === "toolCall") {
|
|
96
|
+
block.partialJson += event.delta.partial_json;
|
|
97
|
+
stream.push({
|
|
98
|
+
type: "toolcall_delta",
|
|
99
|
+
contentIndex: index,
|
|
100
|
+
delta: event.delta.partial_json,
|
|
101
|
+
partial: output,
|
|
102
|
+
});
|
|
83
103
|
}
|
|
84
104
|
}
|
|
85
105
|
else if (event.delta.type === "signature_delta") {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
107
|
+
const block = blocks[index];
|
|
108
|
+
if (block && block.type === "thinking") {
|
|
109
|
+
block.thinkingSignature = block.thinkingSignature || "";
|
|
110
|
+
block.thinkingSignature += event.delta.signature;
|
|
89
111
|
}
|
|
90
112
|
}
|
|
91
113
|
}
|
|
92
114
|
else if (event.type === "content_block_stop") {
|
|
93
|
-
|
|
94
|
-
|
|
115
|
+
const index = blocks.findIndex((b) => b.index === event.index);
|
|
116
|
+
const block = blocks[index];
|
|
117
|
+
if (block) {
|
|
118
|
+
delete block.index;
|
|
119
|
+
if (block.type === "text") {
|
|
95
120
|
stream.push({
|
|
96
121
|
type: "text_end",
|
|
97
|
-
|
|
122
|
+
contentIndex: index,
|
|
123
|
+
content: block.text,
|
|
98
124
|
partial: output,
|
|
99
125
|
});
|
|
100
126
|
}
|
|
101
|
-
else if (
|
|
127
|
+
else if (block.type === "thinking") {
|
|
102
128
|
stream.push({
|
|
103
129
|
type: "thinking_end",
|
|
104
|
-
|
|
130
|
+
contentIndex: index,
|
|
131
|
+
content: block.thinking,
|
|
105
132
|
partial: output,
|
|
106
133
|
});
|
|
107
134
|
}
|
|
108
|
-
else if (
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
name
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
135
|
+
else if (block.type === "toolCall") {
|
|
136
|
+
block.arguments = JSON.parse(block.partialJson);
|
|
137
|
+
// Validate tool arguments if tool definition is available
|
|
138
|
+
if (context.tools) {
|
|
139
|
+
const tool = context.tools.find((t) => t.name === block.name);
|
|
140
|
+
if (tool) {
|
|
141
|
+
block.arguments = validateToolArguments(tool, block);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
delete block.partialJson;
|
|
116
145
|
stream.push({
|
|
117
|
-
type: "
|
|
118
|
-
|
|
146
|
+
type: "toolcall_end",
|
|
147
|
+
contentIndex: index,
|
|
148
|
+
toolCall: block,
|
|
119
149
|
partial: output,
|
|
120
150
|
});
|
|
121
151
|
}
|
|
122
|
-
currentBlock = null;
|
|
123
152
|
}
|
|
124
153
|
}
|
|
125
154
|
else if (event.type === "message_delta") {
|
|
@@ -140,6 +169,8 @@ export const streamAnthropic = (model, context, options) => {
|
|
|
140
169
|
stream.end();
|
|
141
170
|
}
|
|
142
171
|
catch (error) {
|
|
172
|
+
for (const block of output.content)
|
|
173
|
+
delete block.index;
|
|
143
174
|
output.stopReason = "error";
|
|
144
175
|
output.error = error instanceof Error ? error.message : JSON.stringify(error);
|
|
145
176
|
stream.push({ type: "error", error: output.error, partial: output });
|
|
@@ -187,7 +218,7 @@ function buildParams(model, context, isOAuthToken, options) {
|
|
|
187
218
|
const params = {
|
|
188
219
|
model: model.id,
|
|
189
220
|
messages: convertMessages(context.messages, model),
|
|
190
|
-
max_tokens: options?.maxTokens || model.maxTokens,
|
|
221
|
+
max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
|
|
191
222
|
stream: true,
|
|
192
223
|
};
|
|
193
224
|
// For OAuth tokens, we MUST include Claude Code identity
|
|
@@ -245,7 +276,8 @@ function convertMessages(messages, model) {
|
|
|
245
276
|
const params = [];
|
|
246
277
|
// Transform messages for cross-provider compatibility
|
|
247
278
|
const transformedMessages = transformMessages(messages, model);
|
|
248
|
-
for (
|
|
279
|
+
for (let i = 0; i < transformedMessages.length; i++) {
|
|
280
|
+
const msg = transformedMessages[i];
|
|
249
281
|
if (msg.role === "user") {
|
|
250
282
|
if (typeof msg.content === "string") {
|
|
251
283
|
if (msg.content.trim().length > 0) {
|
|
@@ -326,16 +358,33 @@ function convertMessages(messages, model) {
|
|
|
326
358
|
});
|
|
327
359
|
}
|
|
328
360
|
else if (msg.role === "toolResult") {
|
|
361
|
+
// Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
|
|
362
|
+
const toolResults = [];
|
|
363
|
+
// Add the current tool result
|
|
364
|
+
toolResults.push({
|
|
365
|
+
type: "tool_result",
|
|
366
|
+
tool_use_id: sanitizeToolCallId(msg.toolCallId),
|
|
367
|
+
content: msg.output,
|
|
368
|
+
is_error: msg.isError,
|
|
369
|
+
});
|
|
370
|
+
// Look ahead for consecutive toolResult messages
|
|
371
|
+
let j = i + 1;
|
|
372
|
+
while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
|
|
373
|
+
const nextMsg = transformedMessages[j]; // We know it's a toolResult
|
|
374
|
+
toolResults.push({
|
|
375
|
+
type: "tool_result",
|
|
376
|
+
tool_use_id: sanitizeToolCallId(nextMsg.toolCallId),
|
|
377
|
+
content: nextMsg.output,
|
|
378
|
+
is_error: nextMsg.isError,
|
|
379
|
+
});
|
|
380
|
+
j++;
|
|
381
|
+
}
|
|
382
|
+
// Skip the messages we've already processed
|
|
383
|
+
i = j - 1;
|
|
384
|
+
// Add a single user message with all tool results
|
|
329
385
|
params.push({
|
|
330
386
|
role: "user",
|
|
331
|
-
content:
|
|
332
|
-
{
|
|
333
|
-
type: "tool_result",
|
|
334
|
-
tool_use_id: sanitizeToolCallId(msg.toolCallId),
|
|
335
|
-
content: msg.content,
|
|
336
|
-
is_error: msg.isError,
|
|
337
|
-
},
|
|
338
|
-
],
|
|
387
|
+
content: toolResults,
|
|
339
388
|
});
|
|
340
389
|
}
|
|
341
390
|
}
|
|
@@ -344,15 +393,18 @@ function convertMessages(messages, model) {
|
|
|
344
393
|
function convertTools(tools) {
|
|
345
394
|
if (!tools)
|
|
346
395
|
return [];
|
|
347
|
-
return tools.map((tool) =>
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
396
|
+
return tools.map((tool) => {
|
|
397
|
+
const jsonSchema = zodToJsonSchema(tool.parameters, { $refStrategy: "none" });
|
|
398
|
+
return {
|
|
399
|
+
name: tool.name,
|
|
400
|
+
description: tool.description,
|
|
401
|
+
input_schema: {
|
|
402
|
+
type: "object",
|
|
403
|
+
properties: jsonSchema.properties || {},
|
|
404
|
+
required: jsonSchema.required || [],
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
});
|
|
356
408
|
}
|
|
357
409
|
function mapStopReason(reason) {
|
|
358
410
|
switch (reason) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MA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C,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IAEtB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC3B,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,YAAY,EAAE;YACb,IAAI,EAAE,QAAiB;YACvB,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,EAAE;YAC5C,QAAQ,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE;SACxC;KACD,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,aAAa,CAAC,MAAqC;IAC3D,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,UAAU;YACd,OAAO,MAAM,CAAC;QACf,KAAK,YAAY;YAChB,OAAO,QAAQ,CAAC;QACjB,KAAK,UAAU;YACd,OAAO,SAAS,CAAC;QAClB,KAAK,SAAS;YACb,OAAO,QAAQ,CAAC;QACjB,KAAK,YAAY,EAAE,kCAAkC;YACpD,OAAO,MAAM,CAAC;QACf,KAAK,eAAe;YACnB,OAAO,MAAM,CAAC,CAAC,8DAA8D;QAC9E,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,WAAW,GAAU,MAAM,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,0BAA0B,WAAW,EAAE,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n\tContentBlockParam,\n\tMessageCreateParamsStreaming,\n\tMessageParam,\n} from \"@anthropic-ai/sdk/resources/messages.js\";\nimport { QueuedGenerateStream } from \"../generate.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tGenerateFunction,\n\tGenerateOptions,\n\tGenerateStream,\n\tMessage,\n\tModel,\n\tStopReason,\n\tTextContent,\n\tThinkingContent,\n\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface AnthropicOptions extends GenerateOptions {\n\tthinkingEnabled?: boolean;\n\tthinkingBudgetTokens?: number;\n\ttoolChoice?: \"auto\" | \"any\" | \"none\" | { type: \"tool\"; name: string };\n}\n\nexport const streamAnthropic: GenerateFunction<\"anthropic-messages\"> = (\n\tmodel: Model<\"anthropic-messages\">,\n\tcontext: Context,\n\toptions?: AnthropicOptions,\n): GenerateStream => {\n\tconst stream = new QueuedGenerateStream();\n\n\t(async () => {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"anthropic-messages\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t};\n\n\t\ttry {\n\t\t\tconst { client, isOAuthToken } = createClient(model, options?.apiKey!);\n\t\t\tconst params = buildParams(model, context, isOAuthToken, options);\n\t\t\tconst anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tlet currentBlock: ThinkingContent | TextContent | (ToolCall & { partialJson: string }) | null = null;\n\n\t\t\tfor await (const event of anthropicStream) {\n\t\t\t\tif (event.type === \"content_block_start\") {\n\t\t\t\t\tif (event.content_block.type === \"text\") {\n\t\t\t\t\t\tcurrentBlock = {\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"\",\n\t\t\t\t\t\t};\n\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\tstream.push({ type: \"text_start\", partial: output });\n\t\t\t\t\t} else if (event.content_block.type === \"thinking\") {\n\t\t\t\t\t\tcurrentBlock = {\n\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\tthinking: \"\",\n\t\t\t\t\t\t\tthinkingSignature: \"\",\n\t\t\t\t\t\t};\n\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\tstream.push({ type: \"thinking_start\", partial: output });\n\t\t\t\t\t} else if (event.content_block.type === \"tool_use\") {\n\t\t\t\t\t\t// We wait for the full tool use to be streamed\n\t\t\t\t\t\tcurrentBlock = {\n\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\tid: event.content_block.id,\n\t\t\t\t\t\t\tname: event.content_block.name,\n\t\t\t\t\t\t\targuments: event.content_block.input as Record<string, any>,\n\t\t\t\t\t\t\tpartialJson: \"\",\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"content_block_delta\") {\n\t\t\t\t\tif (event.delta.type === \"text_delta\") {\n\t\t\t\t\t\tif (currentBlock && currentBlock.type === \"text\") {\n\t\t\t\t\t\t\tcurrentBlock.text += event.delta.text;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\tdelta: event.delta.text,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"thinking_delta\") {\n\t\t\t\t\t\tif (currentBlock && currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tcurrentBlock.thinking += event.delta.thinking;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\tdelta: event.delta.thinking,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"input_json_delta\") {\n\t\t\t\t\t\tif (currentBlock && currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\tcurrentBlock.partialJson += event.delta.partial_json;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"signature_delta\") {\n\t\t\t\t\t\tif (currentBlock && currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = currentBlock.thinkingSignature || \"\";\n\t\t\t\t\t\t\tcurrentBlock.thinkingSignature += event.delta.signature;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"content_block_stop\") {\n\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\tconst finalToolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: currentBlock.id,\n\t\t\t\t\t\t\t\tname: currentBlock.name,\n\t\t\t\t\t\t\t\targuments: JSON.parse(currentBlock.partialJson),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\toutput.content.push(finalToolCall);\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\ttoolCall: finalToolCall,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"message_delta\") {\n\t\t\t\t\tif (event.delta.stop_reason) {\n\t\t\t\t\t\toutput.stopReason = mapStopReason(event.delta.stop_reason);\n\t\t\t\t\t}\n\t\t\t\t\toutput.usage.input += event.usage.input_tokens || 0;\n\t\t\t\t\toutput.usage.output += event.usage.output_tokens || 0;\n\t\t\t\t\toutput.usage.cacheRead += event.usage.cache_read_input_tokens || 0;\n\t\t\t\t\toutput.usage.cacheWrite += event.usage.cache_creation_input_tokens || 0;\n\t\t\t\t\tcalculateCost(model, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\toutput.stopReason = \"error\";\n\t\t\toutput.error = error instanceof Error ? error.message : JSON.stringify(error);\n\t\t\tstream.push({ type: \"error\", error: output.error, partial: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nfunction createClient(\n\tmodel: Model<\"anthropic-messages\">,\n\tapiKey: string,\n): { client: Anthropic; isOAuthToken: boolean } {\n\tif (apiKey.includes(\"sk-ant-oat\")) {\n\t\tconst defaultHeaders = {\n\t\t\taccept: \"application/json\",\n\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\"anthropic-beta\": \"oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14\",\n\t\t};\n\n\t\t// Clear the env var if we're in Node.js to prevent SDK from using it\n\t\tif (typeof process !== \"undefined\" && process.env) {\n\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t}\n\n\t\tconst client = new Anthropic({\n\t\t\tapiKey: null,\n\t\t\tauthToken: apiKey,\n\t\t\tbaseURL: model.baseUrl,\n\t\t\tdefaultHeaders,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t});\n\n\t\treturn { client, isOAuthToken: true };\n\t} else {\n\t\tconst defaultHeaders = {\n\t\t\taccept: \"application/json\",\n\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\"anthropic-beta\": \"fine-grained-tool-streaming-2025-05-14\",\n\t\t};\n\n\t\tconst client = new Anthropic({\n\t\t\tapiKey,\n\t\t\tbaseURL: model.baseUrl,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t\tdefaultHeaders,\n\t\t});\n\n\t\treturn { client, isOAuthToken: false };\n\t}\n}\n\nfunction buildParams(\n\tmodel: Model<\"anthropic-messages\">,\n\tcontext: Context,\n\tisOAuthToken: boolean,\n\toptions?: AnthropicOptions,\n): MessageCreateParamsStreaming {\n\tconst params: MessageCreateParamsStreaming = {\n\t\tmodel: model.id,\n\t\tmessages: convertMessages(context.messages, model),\n\t\tmax_tokens: options?.maxTokens || model.maxTokens,\n\t\tstream: true,\n\t};\n\n\t// For OAuth tokens, we MUST include Claude Code identity\n\tif (isOAuthToken) {\n\t\tparams.system = [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: \"You are Claude Code, Anthropic's official CLI for Claude.\",\n\t\t\t\tcache_control: {\n\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t\tif (context.systemPrompt) {\n\t\t\tparams.system.push({\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: context.systemPrompt,\n\t\t\t\tcache_control: {\n\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t} else if (context.systemPrompt) {\n\t\tparams.system = context.systemPrompt;\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options.temperature;\n\t}\n\n\tif (context.tools) {\n\t\tparams.tools = convertTools(context.tools);\n\t}\n\n\tif (options?.thinkingEnabled && model.reasoning) {\n\t\tparams.thinking = {\n\t\t\ttype: \"enabled\",\n\t\t\tbudget_tokens: options.thinkingBudgetTokens || 1024,\n\t\t};\n\t}\n\n\tif (options?.toolChoice) {\n\t\tif (typeof options.toolChoice === \"string\") {\n\t\t\tparams.tool_choice = { type: options.toolChoice };\n\t\t} else {\n\t\t\tparams.tool_choice = options.toolChoice;\n\t\t}\n\t}\n\n\treturn params;\n}\n\n// Sanitize tool call IDs to match Anthropic's required pattern: ^[a-zA-Z0-9_-]+$\nfunction sanitizeToolCallId(id: string): string {\n\t// Replace any character that isn't alphanumeric, underscore, or hyphen with underscore\n\treturn id.replace(/[^a-zA-Z0-9_-]/g, \"_\");\n}\n\nfunction convertMessages(messages: Message[], model: Model<\"anthropic-messages\">): MessageParam[] {\n\tconst params: MessageParam[] = [];\n\n\t// Transform messages for cross-provider compatibility\n\tconst transformedMessages = transformMessages(messages, model);\n\n\tfor (const msg of transformedMessages) {\n\t\tif (msg.role === \"user\") {\n\t\t\tif (typeof msg.content === \"string\") {\n\t\t\t\tif (msg.content.trim().length > 0) {\n\t\t\t\t\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst blocks: ContentBlockParam[] = msg.content.map((item) => {\n\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\ttype: \"image\",\n\t\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\t\ttype: \"base64\",\n\t\t\t\t\t\t\t\tmedia_type: item.mimeType as \"image/jpeg\" | \"image/png\" | \"image/gif\" | \"image/webp\",\n\t\t\t\t\t\t\t\tdata: item.data,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tlet filteredBlocks = !model?.input.includes(\"image\") ? blocks.filter((b) => b.type !== \"image\") : blocks;\n\t\t\t\tfilteredBlocks = filteredBlocks.filter((b) => {\n\t\t\t\t\tif (b.type === \"text\") {\n\t\t\t\t\t\treturn b.text.trim().length > 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\t\t\t\tif (filteredBlocks.length === 0) continue;\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: filteredBlocks,\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (msg.role === \"assistant\") {\n\t\t\tconst blocks: ContentBlockParam[] = [];\n\n\t\t\tfor (const block of msg.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (block.text.trim().length === 0) continue;\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t});\n\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\tif (block.thinking.trim().length === 0) continue;\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\tthinking: block.thinking,\n\t\t\t\t\t\tsignature: block.thinkingSignature || \"\",\n\t\t\t\t\t});\n\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"tool_use\",\n\t\t\t\t\t\tid: sanitizeToolCallId(block.id),\n\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\tinput: block.arguments,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (blocks.length === 0) continue;\n\t\t\tparams.push({\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: blocks,\n\t\t\t});\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\tparams.push({\n\t\t\t\trole: \"user\",\n\t\t\t\tcontent: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\t\ttool_use_id: sanitizeToolCallId(msg.toolCallId),\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t\tis_error: msg.isError,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t}\n\t}\n\treturn params;\n}\n\nfunction convertTools(tools: Tool[]): Anthropic.Messages.Tool[] {\n\tif (!tools) return [];\n\n\treturn tools.map((tool) => ({\n\t\tname: tool.name,\n\t\tdescription: tool.description,\n\t\tinput_schema: {\n\t\t\ttype: \"object\" as const,\n\t\t\tproperties: tool.parameters.properties || {},\n\t\t\trequired: tool.parameters.required || [],\n\t\t},\n\t}));\n}\n\nfunction mapStopReason(reason: Anthropic.Messages.StopReason): StopReason {\n\tswitch (reason) {\n\t\tcase \"end_turn\":\n\t\t\treturn \"stop\";\n\t\tcase \"max_tokens\":\n\t\t\treturn \"length\";\n\t\tcase \"tool_use\":\n\t\t\treturn \"toolUse\";\n\t\tcase \"refusal\":\n\t\t\treturn \"safety\";\n\t\tcase \"pause_turn\": // Stop is good enough -> resubmit\n\t\t\treturn \"stop\";\n\t\tcase \"stop_sequence\":\n\t\t\treturn \"stop\"; // We don't supply stop sequences, so this should never happen\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = reason;\n\t\t\tthrow new Error(`Unhandled stop reason: ${_exhaustive}`);\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAM1C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAgB7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAQ3D,MAAM,CAAC,MAAM,eAAe,GAAyC,CACpE,KAAkC,EAClC,OAAgB,EAChB,OAA0B,EACI,EAAE;IAChC,MAAM,MAAM,GA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QACJ,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,YAAY,CAAC,KAAa;IAClC,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IAEtB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACzB,MAAM,UAAU,GAAG,eAAe,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,CAAQ,CAAC;QAErF,OAAO;YACN,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,YAAY,EAAE;gBACb,IAAI,EAAE,QAAiB;gBACvB,UAAU,EAAE,UAAU,CAAC,UAAU,IAAI,EAAE;gBACvC,QAAQ,EAAE,UAAU,CAAC,QAAQ,IAAI,EAAE;aACnC;SACD,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,MAAqC;IAC3D,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,UAAU;YACd,OAAO,MAAM,CAAC;QACf,KAAK,YAAY;YAChB,OAAO,QAAQ,CAAC;QACjB,KAAK,UAAU;YACd,OAAO,SAAS,CAAC;QAClB,KAAK,SAAS;YACb,OAAO,QAAQ,CAAC;QACjB,KAAK,YAAY,EAAE,kCAAkC;YACpD,OAAO,MAAM,CAAC;QACf,KAAK,eAAe;YACnB,OAAO,MAAM,CAAC,CAAC,8DAA8D;QAC9E,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,WAAW,GAAU,MAAM,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,0BAA0B,WAAW,EAAE,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n\tContentBlockParam,\n\tMessageCreateParamsStreaming,\n\tMessageParam,\n} from \"@anthropic-ai/sdk/resources/messages.js\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\nimport { AssistantMessageEventStream } from \"../event-stream.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tMessage,\n\tModel,\n\tStopReason,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingContent,\n\tTool,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.js\";\nimport { validateToolArguments } from \"../validation.js\";\nimport { transformMessages } from \"./transorm-messages.js\";\n\nexport interface AnthropicOptions extends StreamOptions {\n\tthinkingEnabled?: boolean;\n\tthinkingBudgetTokens?: number;\n\ttoolChoice?: \"auto\" | \"any\" | \"none\" | { type: \"tool\"; name: string };\n}\n\nexport const streamAnthropic: StreamFunction<\"anthropic-messages\"> = (\n\tmodel: Model<\"anthropic-messages\">,\n\tcontext: Context,\n\toptions?: AnthropicOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t(async () => {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"anthropic-messages\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t};\n\n\t\ttry {\n\t\t\tconst { client, isOAuthToken } = createClient(model, options?.apiKey!);\n\t\t\tconst params = buildParams(model, context, isOAuthToken, options);\n\t\t\tconst anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\ttype Block = (ThinkingContent | TextContent | (ToolCall & { partialJson: string })) & { index: number };\n\t\t\tconst blocks = output.content as Block[];\n\n\t\t\tfor await (const event of anthropicStream) {\n\t\t\t\tif (event.type === \"content_block_start\") {\n\t\t\t\t\tif (event.content_block.type === \"text\") {\n\t\t\t\t\t\tconst block: Block = {\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"\",\n\t\t\t\t\t\t\tindex: event.index,\n\t\t\t\t\t\t};\n\t\t\t\t\t\toutput.content.push(block);\n\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: output.content.length - 1, partial: output });\n\t\t\t\t\t} else if (event.content_block.type === \"thinking\") {\n\t\t\t\t\t\tconst block: Block = {\n\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\tthinking: \"\",\n\t\t\t\t\t\t\tthinkingSignature: \"\",\n\t\t\t\t\t\t\tindex: event.index,\n\t\t\t\t\t\t};\n\t\t\t\t\t\toutput.content.push(block);\n\t\t\t\t\t\tstream.push({ type: \"thinking_start\", contentIndex: output.content.length - 1, partial: output });\n\t\t\t\t\t} else if (event.content_block.type === \"tool_use\") {\n\t\t\t\t\t\tconst block: Block = {\n\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\tid: event.content_block.id,\n\t\t\t\t\t\t\tname: event.content_block.name,\n\t\t\t\t\t\t\targuments: event.content_block.input as Record<string, any>,\n\t\t\t\t\t\t\tpartialJson: \"\",\n\t\t\t\t\t\t\tindex: event.index,\n\t\t\t\t\t\t};\n\t\t\t\t\t\toutput.content.push(block);\n\t\t\t\t\t\tstream.push({ type: \"toolcall_start\", contentIndex: output.content.length - 1, partial: output });\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"content_block_delta\") {\n\t\t\t\t\tif (event.delta.type === \"text_delta\") {\n\t\t\t\t\t\tconst index = blocks.findIndex((b) => b.index === event.index);\n\t\t\t\t\t\tconst block = blocks[index];\n\t\t\t\t\t\tif (block && block.type === \"text\") {\n\t\t\t\t\t\t\tblock.text += event.delta.text;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\tdelta: event.delta.text,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"thinking_delta\") {\n\t\t\t\t\t\tconst index = blocks.findIndex((b) => b.index === event.index);\n\t\t\t\t\t\tconst block = blocks[index];\n\t\t\t\t\t\tif (block && block.type === \"thinking\") {\n\t\t\t\t\t\t\tblock.thinking += event.delta.thinking;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\tdelta: event.delta.thinking,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"input_json_delta\") {\n\t\t\t\t\t\tconst index = blocks.findIndex((b) => b.index === event.index);\n\t\t\t\t\t\tconst block = blocks[index];\n\t\t\t\t\t\tif (block && block.type === \"toolCall\") {\n\t\t\t\t\t\t\tblock.partialJson += event.delta.partial_json;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\tdelta: event.delta.partial_json,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (event.delta.type === \"signature_delta\") {\n\t\t\t\t\t\tconst index = blocks.findIndex((b) => b.index === event.index);\n\t\t\t\t\t\tconst block = blocks[index];\n\t\t\t\t\t\tif (block && block.type === \"thinking\") {\n\t\t\t\t\t\t\tblock.thinkingSignature = block.thinkingSignature || \"\";\n\t\t\t\t\t\t\tblock.thinkingSignature += event.delta.signature;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"content_block_stop\") {\n\t\t\t\t\tconst index = blocks.findIndex((b) => b.index === event.index);\n\t\t\t\t\tconst block = blocks[index];\n\t\t\t\t\tif (block) {\n\t\t\t\t\t\tdelete (block as any).index;\n\t\t\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\tcontent: block.text,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\tcontent: block.thinking,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\t\tblock.arguments = JSON.parse(block.partialJson);\n\n\t\t\t\t\t\t\t// Validate tool arguments if tool definition is available\n\t\t\t\t\t\t\tif (context.tools) {\n\t\t\t\t\t\t\t\tconst tool = context.tools.find((t) => t.name === block.name);\n\t\t\t\t\t\t\t\tif (tool) {\n\t\t\t\t\t\t\t\t\tblock.arguments = validateToolArguments(tool, block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tdelete (block as any).partialJson;\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\t\t\t\tcontentIndex: index,\n\t\t\t\t\t\t\t\ttoolCall: block,\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"message_delta\") {\n\t\t\t\t\tif (event.delta.stop_reason) {\n\t\t\t\t\t\toutput.stopReason = mapStopReason(event.delta.stop_reason);\n\t\t\t\t\t}\n\t\t\t\t\toutput.usage.input += event.usage.input_tokens || 0;\n\t\t\t\t\toutput.usage.output += event.usage.output_tokens || 0;\n\t\t\t\t\toutput.usage.cacheRead += event.usage.cache_read_input_tokens || 0;\n\t\t\t\t\toutput.usage.cacheWrite += event.usage.cache_creation_input_tokens || 0;\n\t\t\t\t\tcalculateCost(model, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tfor (const block of output.content) delete (block as any).index;\n\t\t\toutput.stopReason = \"error\";\n\t\t\toutput.error = error instanceof Error ? error.message : JSON.stringify(error);\n\t\t\tstream.push({ type: \"error\", error: output.error, partial: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nfunction createClient(\n\tmodel: Model<\"anthropic-messages\">,\n\tapiKey: string,\n): { client: Anthropic; isOAuthToken: boolean } {\n\tif (apiKey.includes(\"sk-ant-oat\")) {\n\t\tconst defaultHeaders = {\n\t\t\taccept: \"application/json\",\n\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\"anthropic-beta\": \"oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14\",\n\t\t};\n\n\t\t// Clear the env var if we're in Node.js to prevent SDK from using it\n\t\tif (typeof process !== \"undefined\" && process.env) {\n\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t}\n\n\t\tconst client = new Anthropic({\n\t\t\tapiKey: null,\n\t\t\tauthToken: apiKey,\n\t\t\tbaseURL: model.baseUrl,\n\t\t\tdefaultHeaders,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t});\n\n\t\treturn { client, isOAuthToken: true };\n\t} else {\n\t\tconst defaultHeaders = {\n\t\t\taccept: \"application/json\",\n\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\"anthropic-beta\": \"fine-grained-tool-streaming-2025-05-14\",\n\t\t};\n\n\t\tconst client = new Anthropic({\n\t\t\tapiKey,\n\t\t\tbaseURL: model.baseUrl,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t\tdefaultHeaders,\n\t\t});\n\n\t\treturn { client, isOAuthToken: false };\n\t}\n}\n\nfunction buildParams(\n\tmodel: Model<\"anthropic-messages\">,\n\tcontext: Context,\n\tisOAuthToken: boolean,\n\toptions?: AnthropicOptions,\n): MessageCreateParamsStreaming {\n\tconst params: MessageCreateParamsStreaming = {\n\t\tmodel: model.id,\n\t\tmessages: convertMessages(context.messages, model),\n\t\tmax_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,\n\t\tstream: true,\n\t};\n\n\t// For OAuth tokens, we MUST include Claude Code identity\n\tif (isOAuthToken) {\n\t\tparams.system = [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: \"You are Claude Code, Anthropic's official CLI for Claude.\",\n\t\t\t\tcache_control: {\n\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t\tif (context.systemPrompt) {\n\t\t\tparams.system.push({\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: context.systemPrompt,\n\t\t\t\tcache_control: {\n\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t} else if (context.systemPrompt) {\n\t\tparams.system = context.systemPrompt;\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options.temperature;\n\t}\n\n\tif (context.tools) {\n\t\tparams.tools = convertTools(context.tools);\n\t}\n\n\tif (options?.thinkingEnabled && model.reasoning) {\n\t\tparams.thinking = {\n\t\t\ttype: \"enabled\",\n\t\t\tbudget_tokens: options.thinkingBudgetTokens || 1024,\n\t\t};\n\t}\n\n\tif (options?.toolChoice) {\n\t\tif (typeof options.toolChoice === \"string\") {\n\t\t\tparams.tool_choice = { type: options.toolChoice };\n\t\t} else {\n\t\t\tparams.tool_choice = options.toolChoice;\n\t\t}\n\t}\n\n\treturn params;\n}\n\n// Sanitize tool call IDs to match Anthropic's required pattern: ^[a-zA-Z0-9_-]+$\nfunction sanitizeToolCallId(id: string): string {\n\t// Replace any character that isn't alphanumeric, underscore, or hyphen with underscore\n\treturn id.replace(/[^a-zA-Z0-9_-]/g, \"_\");\n}\n\nfunction convertMessages(messages: Message[], model: Model<\"anthropic-messages\">): MessageParam[] {\n\tconst params: MessageParam[] = [];\n\n\t// Transform messages for cross-provider compatibility\n\tconst transformedMessages = transformMessages(messages, model);\n\n\tfor (let i = 0; i < transformedMessages.length; i++) {\n\t\tconst msg = transformedMessages[i];\n\n\t\tif (msg.role === \"user\") {\n\t\t\tif (typeof msg.content === \"string\") {\n\t\t\t\tif (msg.content.trim().length > 0) {\n\t\t\t\t\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst blocks: ContentBlockParam[] = msg.content.map((item) => {\n\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\ttype: \"image\",\n\t\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\t\ttype: \"base64\",\n\t\t\t\t\t\t\t\tmedia_type: item.mimeType as \"image/jpeg\" | \"image/png\" | \"image/gif\" | \"image/webp\",\n\t\t\t\t\t\t\t\tdata: item.data,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tlet filteredBlocks = !model?.input.includes(\"image\") ? blocks.filter((b) => b.type !== \"image\") : blocks;\n\t\t\t\tfilteredBlocks = filteredBlocks.filter((b) => {\n\t\t\t\t\tif (b.type === \"text\") {\n\t\t\t\t\t\treturn b.text.trim().length > 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\t\t\t\tif (filteredBlocks.length === 0) continue;\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: filteredBlocks,\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (msg.role === \"assistant\") {\n\t\t\tconst blocks: ContentBlockParam[] = [];\n\n\t\t\tfor (const block of msg.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (block.text.trim().length === 0) continue;\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t});\n\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\tif (block.thinking.trim().length === 0) continue;\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\tthinking: block.thinking,\n\t\t\t\t\t\tsignature: block.thinkingSignature || \"\",\n\t\t\t\t\t});\n\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"tool_use\",\n\t\t\t\t\t\tid: sanitizeToolCallId(block.id),\n\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\tinput: block.arguments,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (blocks.length === 0) continue;\n\t\t\tparams.push({\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: blocks,\n\t\t\t});\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t// Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint\n\t\t\tconst toolResults: ContentBlockParam[] = [];\n\n\t\t\t// Add the current tool result\n\t\t\ttoolResults.push({\n\t\t\t\ttype: \"tool_result\",\n\t\t\t\ttool_use_id: sanitizeToolCallId(msg.toolCallId),\n\t\t\t\tcontent: msg.output,\n\t\t\t\tis_error: msg.isError,\n\t\t\t});\n\n\t\t\t// Look ahead for consecutive toolResult messages\n\t\t\tlet j = i + 1;\n\t\t\twhile (j < transformedMessages.length && transformedMessages[j].role === \"toolResult\") {\n\t\t\t\tconst nextMsg = transformedMessages[j] as ToolResultMessage; // We know it's a toolResult\n\t\t\t\ttoolResults.push({\n\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\ttool_use_id: sanitizeToolCallId(nextMsg.toolCallId),\n\t\t\t\t\tcontent: nextMsg.output,\n\t\t\t\t\tis_error: nextMsg.isError,\n\t\t\t\t});\n\t\t\t\tj++;\n\t\t\t}\n\n\t\t\t// Skip the messages we've already processed\n\t\t\ti = j - 1;\n\n\t\t\t// Add a single user message with all tool results\n\t\t\tparams.push({\n\t\t\t\trole: \"user\",\n\t\t\t\tcontent: toolResults,\n\t\t\t});\n\t\t}\n\t}\n\treturn params;\n}\n\nfunction convertTools(tools: Tool[]): Anthropic.Messages.Tool[] {\n\tif (!tools) return [];\n\n\treturn tools.map((tool) => {\n\t\tconst jsonSchema = zodToJsonSchema(tool.parameters, { $refStrategy: \"none\" }) as any;\n\n\t\treturn {\n\t\t\tname: tool.name,\n\t\t\tdescription: tool.description,\n\t\t\tinput_schema: {\n\t\t\t\ttype: \"object\" as const,\n\t\t\t\tproperties: jsonSchema.properties || {},\n\t\t\t\trequired: jsonSchema.required || [],\n\t\t\t},\n\t\t};\n\t});\n}\n\nfunction mapStopReason(reason: Anthropic.Messages.StopReason): StopReason {\n\tswitch (reason) {\n\t\tcase \"end_turn\":\n\t\t\treturn \"stop\";\n\t\tcase \"max_tokens\":\n\t\t\treturn \"length\";\n\t\tcase \"tool_use\":\n\t\t\treturn \"toolUse\";\n\t\tcase \"refusal\":\n\t\t\treturn \"safety\";\n\t\tcase \"pause_turn\": // Stop is good enough -> resubmit\n\t\t\treturn \"stop\";\n\t\tcase \"stop_sequence\":\n\t\t\treturn \"stop\"; // We don't supply stop sequences, so this should never happen\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = reason;\n\t\t\tthrow new Error(`Unhandled stop reason: ${_exhaustive}`);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export interface GoogleOptions extends
|
|
1
|
+
import type { StreamFunction, StreamOptions } from "../types.js";
|
|
2
|
+
export interface GoogleOptions extends StreamOptions {
|
|
3
3
|
toolChoice?: "auto" | "none" | "any";
|
|
4
4
|
thinking?: {
|
|
5
5
|
enabled: boolean;
|
|
6
6
|
budgetTokens?: number;
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
|
-
export declare const streamGoogle:
|
|
9
|
+
export declare const streamGoogle: StreamFunction<"google-generative-ai">;
|
|
10
10
|
//# sourceMappingURL=google.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAMX,cAAc,EACd,aAAa,EAKb,MAAM,aAAa,CAAC;AAIrB,MAAM,WAAW,aAAc,SAAQ,aAAa;IACnD,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IACrC,QAAQ,CAAC,EAAE;QACV,OAAO,EAAE,OAAO,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACF;AAKD,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,sBAAsB,CAyM/D,CAAC"}
|
package/dist/providers/google.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { FinishReason, FunctionCallingConfigMode, GoogleGenAI, } from "@google/genai";
|
|
2
|
-
import {
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
import { AssistantMessageEventStream } from "../event-stream.js";
|
|
3
4
|
import { calculateCost } from "../models.js";
|
|
4
|
-
import {
|
|
5
|
+
import { validateToolArguments } from "../validation.js";
|
|
6
|
+
import { transformMessages } from "./transorm-messages.js";
|
|
5
7
|
// Counter for generating unique tool call IDs
|
|
6
8
|
let toolCallCounter = 0;
|
|
7
9
|
export const streamGoogle = (model, context, options) => {
|
|
8
|
-
const stream = new
|
|
10
|
+
const stream = new AssistantMessageEventStream();
|
|
9
11
|
(async () => {
|
|
10
12
|
const output = {
|
|
11
13
|
role: "assistant",
|
|
@@ -28,6 +30,8 @@ export const streamGoogle = (model, context, options) => {
|
|
|
28
30
|
const googleStream = await client.models.generateContentStream(params);
|
|
29
31
|
stream.push({ type: "start", partial: output });
|
|
30
32
|
let currentBlock = null;
|
|
33
|
+
const blocks = output.content;
|
|
34
|
+
const blockIndex = () => blocks.length - 1;
|
|
31
35
|
for await (const chunk of googleStream) {
|
|
32
36
|
const candidate = chunk.candidates?.[0];
|
|
33
37
|
if (candidate?.content?.parts) {
|
|
@@ -41,6 +45,7 @@ export const streamGoogle = (model, context, options) => {
|
|
|
41
45
|
if (currentBlock.type === "text") {
|
|
42
46
|
stream.push({
|
|
43
47
|
type: "text_end",
|
|
48
|
+
contentIndex: blocks.length - 1,
|
|
44
49
|
content: currentBlock.text,
|
|
45
50
|
partial: output,
|
|
46
51
|
});
|
|
@@ -48,6 +53,7 @@ export const streamGoogle = (model, context, options) => {
|
|
|
48
53
|
else {
|
|
49
54
|
stream.push({
|
|
50
55
|
type: "thinking_end",
|
|
56
|
+
contentIndex: blockIndex(),
|
|
51
57
|
content: currentBlock.thinking,
|
|
52
58
|
partial: output,
|
|
53
59
|
});
|
|
@@ -55,11 +61,11 @@ export const streamGoogle = (model, context, options) => {
|
|
|
55
61
|
}
|
|
56
62
|
if (isThinking) {
|
|
57
63
|
currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
|
|
58
|
-
stream.push({ type: "thinking_start", partial: output });
|
|
64
|
+
stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
59
65
|
}
|
|
60
66
|
else {
|
|
61
67
|
currentBlock = { type: "text", text: "" };
|
|
62
|
-
stream.push({ type: "text_start", partial: output });
|
|
68
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
63
69
|
}
|
|
64
70
|
output.content.push(currentBlock);
|
|
65
71
|
}
|
|
@@ -68,13 +74,19 @@ export const streamGoogle = (model, context, options) => {
|
|
|
68
74
|
currentBlock.thinkingSignature = part.thoughtSignature;
|
|
69
75
|
stream.push({
|
|
70
76
|
type: "thinking_delta",
|
|
77
|
+
contentIndex: blockIndex(),
|
|
71
78
|
delta: part.text,
|
|
72
79
|
partial: output,
|
|
73
80
|
});
|
|
74
81
|
}
|
|
75
82
|
else {
|
|
76
83
|
currentBlock.text += part.text;
|
|
77
|
-
stream.push({
|
|
84
|
+
stream.push({
|
|
85
|
+
type: "text_delta",
|
|
86
|
+
contentIndex: blockIndex(),
|
|
87
|
+
delta: part.text,
|
|
88
|
+
partial: output,
|
|
89
|
+
});
|
|
78
90
|
}
|
|
79
91
|
}
|
|
80
92
|
if (part.functionCall) {
|
|
@@ -82,6 +94,7 @@ export const streamGoogle = (model, context, options) => {
|
|
|
82
94
|
if (currentBlock.type === "text") {
|
|
83
95
|
stream.push({
|
|
84
96
|
type: "text_end",
|
|
97
|
+
contentIndex: blockIndex(),
|
|
85
98
|
content: currentBlock.text,
|
|
86
99
|
partial: output,
|
|
87
100
|
});
|
|
@@ -89,6 +102,7 @@ export const streamGoogle = (model, context, options) => {
|
|
|
89
102
|
else {
|
|
90
103
|
stream.push({
|
|
91
104
|
type: "thinking_end",
|
|
105
|
+
contentIndex: blockIndex(),
|
|
92
106
|
content: currentBlock.thinking,
|
|
93
107
|
partial: output,
|
|
94
108
|
});
|
|
@@ -107,8 +121,22 @@ export const streamGoogle = (model, context, options) => {
|
|
|
107
121
|
name: part.functionCall.name || "",
|
|
108
122
|
arguments: part.functionCall.args,
|
|
109
123
|
};
|
|
124
|
+
// Validate tool arguments if tool definition is available
|
|
125
|
+
if (context.tools) {
|
|
126
|
+
const tool = context.tools.find((t) => t.name === toolCall.name);
|
|
127
|
+
if (tool) {
|
|
128
|
+
toolCall.arguments = validateToolArguments(tool, toolCall);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
110
131
|
output.content.push(toolCall);
|
|
111
|
-
stream.push({ type: "
|
|
132
|
+
stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
|
|
133
|
+
stream.push({
|
|
134
|
+
type: "toolcall_delta",
|
|
135
|
+
contentIndex: blockIndex(),
|
|
136
|
+
delta: JSON.stringify(toolCall.arguments),
|
|
137
|
+
partial: output,
|
|
138
|
+
});
|
|
139
|
+
stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
|
|
112
140
|
}
|
|
113
141
|
}
|
|
114
142
|
}
|
|
@@ -137,10 +165,20 @@ export const streamGoogle = (model, context, options) => {
|
|
|
137
165
|
}
|
|
138
166
|
if (currentBlock) {
|
|
139
167
|
if (currentBlock.type === "text") {
|
|
140
|
-
stream.push({
|
|
168
|
+
stream.push({
|
|
169
|
+
type: "text_end",
|
|
170
|
+
contentIndex: blockIndex(),
|
|
171
|
+
content: currentBlock.text,
|
|
172
|
+
partial: output,
|
|
173
|
+
});
|
|
141
174
|
}
|
|
142
175
|
else {
|
|
143
|
-
stream.push({
|
|
176
|
+
stream.push({
|
|
177
|
+
type: "thinking_end",
|
|
178
|
+
contentIndex: blockIndex(),
|
|
179
|
+
content: currentBlock.thinking,
|
|
180
|
+
partial: output,
|
|
181
|
+
});
|
|
144
182
|
}
|
|
145
183
|
}
|
|
146
184
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
@@ -176,15 +214,18 @@ function buildParams(model, context, options = {}) {
|
|
|
176
214
|
const config = {
|
|
177
215
|
...(Object.keys(generationConfig).length > 0 && generationConfig),
|
|
178
216
|
...(context.systemPrompt && { systemInstruction: context.systemPrompt }),
|
|
179
|
-
...(context.tools && { tools: convertTools(context.tools) }),
|
|
217
|
+
...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),
|
|
180
218
|
};
|
|
181
|
-
if (context.tools && options.toolChoice) {
|
|
219
|
+
if (context.tools && context.tools.length > 0 && options.toolChoice) {
|
|
182
220
|
config.toolConfig = {
|
|
183
221
|
functionCallingConfig: {
|
|
184
222
|
mode: mapToolChoice(options.toolChoice),
|
|
185
223
|
},
|
|
186
224
|
};
|
|
187
225
|
}
|
|
226
|
+
else {
|
|
227
|
+
config.toolConfig = undefined;
|
|
228
|
+
}
|
|
188
229
|
if (options.thinking?.enabled && model.reasoning) {
|
|
189
230
|
config.thinkingConfig = {
|
|
190
231
|
includeThoughts: true,
|
|
@@ -278,7 +319,7 @@ function convertMessages(model, context) {
|
|
|
278
319
|
id: msg.toolCallId,
|
|
279
320
|
name: msg.toolName,
|
|
280
321
|
response: {
|
|
281
|
-
result: msg.
|
|
322
|
+
result: msg.output,
|
|
282
323
|
isError: msg.isError,
|
|
283
324
|
},
|
|
284
325
|
},
|
|
@@ -290,12 +331,14 @@ function convertMessages(model, context) {
|
|
|
290
331
|
return contents;
|
|
291
332
|
}
|
|
292
333
|
function convertTools(tools) {
|
|
334
|
+
if (tools.length === 0)
|
|
335
|
+
return undefined;
|
|
293
336
|
return [
|
|
294
337
|
{
|
|
295
338
|
functionDeclarations: tools.map((tool) => ({
|
|
296
339
|
name: tool.name,
|
|
297
340
|
description: tool.description,
|
|
298
|
-
parameters: tool.parameters,
|
|
341
|
+
parameters: zodToJsonSchema(tool.parameters, { $refStrategy: "none" }),
|
|
299
342
|
})),
|
|
300
343
|
},
|
|
301
344
|
];
|