@mariozechner/pi-ai 0.5.18 → 0.5.20
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 +127 -9
- package/dist/models.generated.d.ts +14 -14
- package/dist/models.generated.js +66 -66
- package/dist/models.generated.js.map +1 -1
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +86 -102
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts +2 -2
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +36 -54
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +1 -1
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +36 -47
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses.d.ts +1 -1
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +116 -89
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/utils.d.ts +15 -0
- package/dist/providers/utils.d.ts.map +1 -0
- package/dist/providers/utils.js +48 -0
- package/dist/providers/utils.js.map +1 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -15,7 +15,7 @@ export declare class AnthropicLLM implements LLM<AnthropicLLMOptions> {
|
|
|
15
15
|
private isOAuthToken;
|
|
16
16
|
constructor(model: Model, apiKey?: string);
|
|
17
17
|
getModel(): Model;
|
|
18
|
-
|
|
18
|
+
generate(context: Context, options?: AnthropicLLMOptions): Promise<AssistantMessage>;
|
|
19
19
|
private convertMessages;
|
|
20
20
|
private convertTools;
|
|
21
21
|
private mapStopReason;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAKL,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,mBAAoB,SAAQ,UAAU;IACtD,QAAQ,CAAC,EAAE;QACV,OAAO,EAAE,OAAO,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;IACF,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CACtE;AAED,qBAAa,YAAa,YAAW,GAAG,CAAC,mBAAmB,CAAC;IAC5D,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,YAAY,CAAkB;gBAE1B,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM;IAyBzC,QAAQ,IAAI,KAAK;IAIX,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAmL1F,OAAO,CAAC,eAAe;IAqFvB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,aAAa;CAkBrB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
2
|
import { calculateCost } from "../models.js";
|
|
3
|
+
import { transformMessages } from "./utils.js";
|
|
3
4
|
export class AnthropicLLM {
|
|
4
5
|
client;
|
|
5
6
|
modelInfo;
|
|
@@ -29,7 +30,21 @@ export class AnthropicLLM {
|
|
|
29
30
|
getModel() {
|
|
30
31
|
return this.modelInfo;
|
|
31
32
|
}
|
|
32
|
-
async
|
|
33
|
+
async generate(context, options) {
|
|
34
|
+
const output = {
|
|
35
|
+
role: "assistant",
|
|
36
|
+
content: [],
|
|
37
|
+
provider: this.modelInfo.provider,
|
|
38
|
+
model: this.modelInfo.id,
|
|
39
|
+
usage: {
|
|
40
|
+
input: 0,
|
|
41
|
+
output: 0,
|
|
42
|
+
cacheRead: 0,
|
|
43
|
+
cacheWrite: 0,
|
|
44
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
45
|
+
},
|
|
46
|
+
stopReason: "stop",
|
|
47
|
+
};
|
|
33
48
|
try {
|
|
34
49
|
const messages = this.convertMessages(context.messages);
|
|
35
50
|
const params = {
|
|
@@ -90,144 +105,113 @@ export class AnthropicLLM {
|
|
|
90
105
|
signal: options?.signal,
|
|
91
106
|
});
|
|
92
107
|
options?.onEvent?.({ type: "start", model: this.modelInfo.id, provider: this.modelInfo.provider });
|
|
93
|
-
let
|
|
94
|
-
let blockContent = "";
|
|
95
|
-
let toolCall = null;
|
|
108
|
+
let currentBlock = null;
|
|
96
109
|
for await (const event of stream) {
|
|
97
110
|
if (event.type === "content_block_start") {
|
|
98
111
|
if (event.content_block.type === "text") {
|
|
99
|
-
|
|
100
|
-
|
|
112
|
+
currentBlock = {
|
|
113
|
+
type: "text",
|
|
114
|
+
text: "",
|
|
115
|
+
};
|
|
116
|
+
output.content.push(currentBlock);
|
|
101
117
|
options?.onEvent?.({ type: "text_start" });
|
|
102
118
|
}
|
|
103
119
|
else if (event.content_block.type === "thinking") {
|
|
104
|
-
|
|
105
|
-
|
|
120
|
+
currentBlock = {
|
|
121
|
+
type: "thinking",
|
|
122
|
+
thinking: "",
|
|
123
|
+
thinkingSignature: "",
|
|
124
|
+
};
|
|
125
|
+
output.content.push(currentBlock);
|
|
106
126
|
options?.onEvent?.({ type: "thinking_start" });
|
|
107
127
|
}
|
|
108
128
|
else if (event.content_block.type === "tool_use") {
|
|
109
129
|
// We wait for the full tool use to be streamed to send the event
|
|
110
|
-
|
|
130
|
+
currentBlock = {
|
|
111
131
|
type: "toolCall",
|
|
112
132
|
id: event.content_block.id,
|
|
113
133
|
name: event.content_block.name,
|
|
114
134
|
arguments: event.content_block.input,
|
|
115
135
|
partialJson: "",
|
|
116
136
|
};
|
|
117
|
-
blockType = "toolUse";
|
|
118
|
-
blockContent = "";
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
blockType = "other";
|
|
122
|
-
blockContent = "";
|
|
123
137
|
}
|
|
124
138
|
}
|
|
125
|
-
if (event.type === "content_block_delta") {
|
|
139
|
+
else if (event.type === "content_block_delta") {
|
|
126
140
|
if (event.delta.type === "text_delta") {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
options?.onEvent?.({ type: "thinking_delta", content: blockContent, delta: event.delta.thinking });
|
|
132
|
-
blockContent += event.delta.thinking;
|
|
133
|
-
}
|
|
134
|
-
if (event.delta.type === "input_json_delta") {
|
|
135
|
-
toolCall.partialJson += event.delta.partial_json;
|
|
141
|
+
if (currentBlock && currentBlock.type === "text") {
|
|
142
|
+
currentBlock.text += event.delta.text;
|
|
143
|
+
options?.onEvent?.({ type: "text_delta", content: currentBlock.text, delta: event.delta.text });
|
|
144
|
+
}
|
|
136
145
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
146
|
+
else if (event.delta.type === "thinking_delta") {
|
|
147
|
+
if (currentBlock && currentBlock.type === "thinking") {
|
|
148
|
+
currentBlock.thinking += event.delta.thinking;
|
|
149
|
+
options?.onEvent?.({
|
|
150
|
+
type: "thinking_delta",
|
|
151
|
+
content: currentBlock.thinking,
|
|
152
|
+
delta: event.delta.thinking,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
141
155
|
}
|
|
142
|
-
else if (
|
|
143
|
-
|
|
156
|
+
else if (event.delta.type === "input_json_delta") {
|
|
157
|
+
if (currentBlock && currentBlock.type === "toolCall") {
|
|
158
|
+
currentBlock.partialJson += event.delta.partial_json;
|
|
159
|
+
}
|
|
144
160
|
}
|
|
145
|
-
else if (
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
arguments: toolCall.partialJson ? JSON.parse(toolCall.partialJson) : toolCall.arguments,
|
|
151
|
-
};
|
|
152
|
-
toolCall = null;
|
|
153
|
-
options?.onEvent?.({ type: "toolCall", toolCall: finalToolCall });
|
|
161
|
+
else if (event.delta.type === "signature_delta") {
|
|
162
|
+
if (currentBlock && currentBlock.type === "thinking") {
|
|
163
|
+
currentBlock.thinkingSignature = currentBlock.thinkingSignature || "";
|
|
164
|
+
currentBlock.thinkingSignature += event.delta.signature;
|
|
165
|
+
}
|
|
154
166
|
}
|
|
155
|
-
blockType = "other";
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const msg = await stream.finalMessage();
|
|
159
|
-
const blocks = [];
|
|
160
|
-
for (const block of msg.content) {
|
|
161
|
-
if (block.type === "text" && block.text) {
|
|
162
|
-
blocks.push({
|
|
163
|
-
type: "text",
|
|
164
|
-
text: block.text,
|
|
165
|
-
});
|
|
166
167
|
}
|
|
167
|
-
else if (
|
|
168
|
-
|
|
169
|
-
type
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
168
|
+
else if (event.type === "content_block_stop") {
|
|
169
|
+
if (currentBlock) {
|
|
170
|
+
if (currentBlock.type === "text") {
|
|
171
|
+
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
|
|
172
|
+
}
|
|
173
|
+
else if (currentBlock.type === "thinking") {
|
|
174
|
+
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
|
|
175
|
+
}
|
|
176
|
+
else if (currentBlock.type === "toolCall") {
|
|
177
|
+
const finalToolCall = {
|
|
178
|
+
type: "toolCall",
|
|
179
|
+
id: currentBlock.id,
|
|
180
|
+
name: currentBlock.name,
|
|
181
|
+
arguments: JSON.parse(currentBlock.partialJson),
|
|
182
|
+
};
|
|
183
|
+
output.content.push(finalToolCall);
|
|
184
|
+
options?.onEvent?.({ type: "toolCall", toolCall: finalToolCall });
|
|
185
|
+
}
|
|
186
|
+
currentBlock = null;
|
|
187
|
+
}
|
|
173
188
|
}
|
|
174
|
-
else if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
189
|
+
else if (event.type === "message_delta") {
|
|
190
|
+
if (event.delta.stop_reason) {
|
|
191
|
+
output.stopReason = this.mapStopReason(event.delta.stop_reason);
|
|
192
|
+
}
|
|
193
|
+
output.usage.input += event.usage.input_tokens || 0;
|
|
194
|
+
output.usage.output += event.usage.output_tokens || 0;
|
|
195
|
+
output.usage.cacheRead += event.usage.cache_read_input_tokens || 0;
|
|
196
|
+
output.usage.cacheWrite += event.usage.cache_creation_input_tokens || 0;
|
|
197
|
+
calculateCost(this.modelInfo, output.usage);
|
|
181
198
|
}
|
|
182
199
|
}
|
|
183
|
-
const usage = {
|
|
184
|
-
input: msg.usage.input_tokens,
|
|
185
|
-
output: msg.usage.output_tokens,
|
|
186
|
-
cacheRead: msg.usage.cache_read_input_tokens || 0,
|
|
187
|
-
cacheWrite: msg.usage.cache_creation_input_tokens || 0,
|
|
188
|
-
cost: {
|
|
189
|
-
input: 0,
|
|
190
|
-
output: 0,
|
|
191
|
-
cacheRead: 0,
|
|
192
|
-
cacheWrite: 0,
|
|
193
|
-
total: 0,
|
|
194
|
-
},
|
|
195
|
-
};
|
|
196
|
-
calculateCost(this.modelInfo, usage);
|
|
197
|
-
const output = {
|
|
198
|
-
role: "assistant",
|
|
199
|
-
content: blocks,
|
|
200
|
-
provider: this.modelInfo.provider,
|
|
201
|
-
model: this.modelInfo.id,
|
|
202
|
-
usage,
|
|
203
|
-
stopReason: this.mapStopReason(msg.stop_reason),
|
|
204
|
-
};
|
|
205
200
|
options?.onEvent?.({ type: "done", reason: output.stopReason, message: output });
|
|
206
201
|
return output;
|
|
207
202
|
}
|
|
208
203
|
catch (error) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
content: [],
|
|
212
|
-
provider: this.modelInfo.provider,
|
|
213
|
-
model: this.modelInfo.id,
|
|
214
|
-
usage: {
|
|
215
|
-
input: 0,
|
|
216
|
-
output: 0,
|
|
217
|
-
cacheRead: 0,
|
|
218
|
-
cacheWrite: 0,
|
|
219
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
220
|
-
},
|
|
221
|
-
stopReason: "error",
|
|
222
|
-
error: error instanceof Error ? error.message : JSON.stringify(error),
|
|
223
|
-
};
|
|
204
|
+
output.stopReason = "error";
|
|
205
|
+
output.error = error instanceof Error ? error.message : JSON.stringify(error);
|
|
224
206
|
options?.onEvent?.({ type: "error", error: output.error });
|
|
225
207
|
return output;
|
|
226
208
|
}
|
|
227
209
|
}
|
|
228
210
|
convertMessages(messages) {
|
|
229
211
|
const params = [];
|
|
230
|
-
|
|
212
|
+
// Transform messages for cross-provider compatibility
|
|
213
|
+
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
214
|
+
for (const msg of transformedMessages) {
|
|
231
215
|
if (msg.role === "user") {
|
|
232
216
|
// Handle both string and array content
|
|
233
217
|
if (typeof msg.content === "string") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAO1C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAqB7C,MAAM,OAAO,YAAY;IAChB,MAAM,CAAY;IAClB,SAAS,CAAQ;IACjB,YAAY,GAAY,KAAK,CAAC;IAEtC,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,CAAC;gBACpC,MAAM,IAAI,KAAK,CACd,sGAAsG,CACtG,CAAC;YACH,CAAC;Y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AAM,EAAE,CAAC;YAChB,KAAK,UAAU;gBACd,OAAO,MAAM,CAAC;YACf,KAAK,YAAY;gBAChB,OAAO,QAAQ,CAAC;YACjB,KAAK,UAAU;gBACd,OAAO,SAAS,CAAC;YAClB,KAAK,SAAS;gBACb,OAAO,QAAQ,CAAC;YACjB,KAAK,YAAY,EAAE,kCAAkC;gBACpD,OAAO,MAAM,CAAC;YACf,KAAK,eAAe;gBACnB,OAAO,MAAM,CAAC,CAAC,8DAA8D;YAC9E;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n\tContentBlockParam,\n\tMessageCreateParamsStreaming,\n\tMessageParam,\n\tTool,\n} from \"@anthropic-ai/sdk/resources/messages.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tAssistantMessage,\n\tContext,\n\tLLM,\n\tLLMOptions,\n\tMessage,\n\tModel,\n\tStopReason,\n\tToolCall,\n\tUsage,\n} from \"../types.js\";\n\nexport interface AnthropicLLMOptions extends LLMOptions {\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number;\n\t};\n\ttoolChoice?: \"auto\" | \"any\" | \"none\" | { type: \"tool\"; name: string };\n}\n\nexport class AnthropicLLM implements LLM<AnthropicLLMOptions> {\n\tprivate client: Anthropic;\n\tprivate modelInfo: Model;\n\tprivate isOAuthToken: boolean = false;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.ANTHROPIC_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Anthropic API key is required. Set ANTHROPIC_API_KEY environment variable or pass it as an argument.\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tapiKey = process.env.ANTHROPIC_API_KEY;\n\t\t}\n\t\tif (apiKey.includes(\"sk-ant-oat\")) {\n\t\t\tconst defaultHeaders = {\n\t\t\t\taccept: \"application/json\",\n\t\t\t\t\"anthropic-beta\": \"oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14\",\n\t\t\t};\n\n\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t\tthis.client = new Anthropic({ apiKey: null, authToken: apiKey, baseURL: model.baseUrl, defaultHeaders });\n\t\t\tthis.isOAuthToken = true;\n\t\t} else {\n\t\t\tthis.client = new Anthropic({ apiKey, baseURL: model.baseUrl });\n\t\t\tthis.isOAuthToken = false;\n\t\t}\n\t\tthis.modelInfo = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.modelInfo;\n\t}\n\n\tasync complete(context: Context, options?: AnthropicLLMOptions): Promise<AssistantMessage> {\n\t\ttry {\n\t\t\tconst messages = this.convertMessages(context.messages);\n\n\t\t\tconst params: MessageCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tmessages,\n\t\t\t\tmax_tokens: options?.maxTokens || 4096,\n\t\t\t\tstream: true,\n\t\t\t};\n\n\t\t\t// For OAuth tokens, we MUST include Claude Code identity\n\t\t\tif (this.isOAuthToken) {\n\t\t\t\tparams.system = [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: \"You are Claude Code, Anthropic's official CLI for Claude.\",\n\t\t\t\t\t\tcache_control: {\n\t\t\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t];\n\t\t\t\tif (context.systemPrompt) {\n\t\t\t\t\tparams.system.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: context.systemPrompt,\n\t\t\t\t\t\tcache_control: {\n\t\t\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (context.systemPrompt) {\n\t\t\t\tparams.system = context.systemPrompt;\n\t\t\t}\n\n\t\t\tif (options?.temperature !== undefined) {\n\t\t\t\tparams.temperature = options?.temperature;\n\t\t\t}\n\n\t\t\tif (context.tools) {\n\t\t\t\tparams.tools = this.convertTools(context.tools);\n\t\t\t}\n\n\t\t\t// Only enable thinking if the model supports it\n\t\t\tif (options?.thinking?.enabled && this.modelInfo.reasoning) {\n\t\t\t\tparams.thinking = {\n\t\t\t\t\ttype: \"enabled\",\n\t\t\t\t\tbudget_tokens: options.thinking.budgetTokens || 1024,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (options?.toolChoice) {\n\t\t\t\tif (typeof options.toolChoice === \"string\") {\n\t\t\t\t\tparams.tool_choice = { type: options.toolChoice };\n\t\t\t\t} else {\n\t\t\t\t\tparams.tool_choice = options.toolChoice;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst stream = this.client.messages.stream(\n\t\t\t\t{\n\t\t\t\t\t...params,\n\t\t\t\t\tstream: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\n\t\t\toptions?.onEvent?.({ type: \"start\", model: this.modelInfo.id, provider: this.modelInfo.provider });\n\n\t\t\tlet blockType: \"text\" | \"thinking\" | \"toolUse\" | \"other\" = \"other\";\n\t\t\tlet blockContent = \"\";\n\t\t\tlet toolCall: (ToolCall & { partialJson: string }) | null = null;\n\t\t\tfor await (const event of stream) {\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\tblockType = \"text\";\n\t\t\t\t\t\tblockContent = \"\";\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t} else if (event.content_block.type === \"thinking\") {\n\t\t\t\t\t\tblockType = \"thinking\";\n\t\t\t\t\t\tblockContent = \"\";\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\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 to send the event\n\t\t\t\t\t\ttoolCall = {\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\tblockType = \"toolUse\";\n\t\t\t\t\t\tblockContent = \"\";\n\t\t\t\t\t} else {\n\t\t\t\t\t\tblockType = \"other\";\n\t\t\t\t\t\tblockContent = \"\";\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (event.type === \"content_block_delta\") {\n\t\t\t\t\tif (event.delta.type === \"text_delta\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_delta\", content: blockContent, delta: event.delta.text });\n\t\t\t\t\t\tblockContent += event.delta.text;\n\t\t\t\t\t}\n\t\t\t\t\tif (event.delta.type === \"thinking_delta\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_delta\", content: blockContent, delta: event.delta.thinking });\n\t\t\t\t\t\tblockContent += event.delta.thinking;\n\t\t\t\t\t}\n\t\t\t\t\tif (event.delta.type === \"input_json_delta\") {\n\t\t\t\t\t\ttoolCall!.partialJson += event.delta.partial_json;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (event.type === \"content_block_stop\") {\n\t\t\t\t\tif (blockType === \"text\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: blockContent });\n\t\t\t\t\t} else if (blockType === \"thinking\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: blockContent });\n\t\t\t\t\t} else if (blockType === \"toolUse\") {\n\t\t\t\t\t\tconst finalToolCall: ToolCall = {\n\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\tid: toolCall!.id,\n\t\t\t\t\t\t\tname: toolCall!.name,\n\t\t\t\t\t\t\targuments: toolCall!.partialJson ? JSON.parse(toolCall!.partialJson) : toolCall!.arguments,\n\t\t\t\t\t\t};\n\t\t\t\t\t\ttoolCall = null;\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: finalToolCall });\n\t\t\t\t\t}\n\t\t\t\t\tblockType = \"other\";\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst msg = await stream.finalMessage();\n\t\t\tconst blocks: AssistantMessage[\"content\"] = [];\n\t\t\tfor (const block of msg.content) {\n\t\t\t\tif (block.type === \"text\" && block.text) {\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\" && block.thinking) {\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\tthinkingSignature: block.signature,\n\t\t\t\t\t});\n\t\t\t\t} else if (block.type === \"tool_use\") {\n\t\t\t\t\tblocks.push({\n\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\targuments: block.input as Record<string, any>,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst usage: Usage = {\n\t\t\t\tinput: msg.usage.input_tokens,\n\t\t\t\toutput: msg.usage.output_tokens,\n\t\t\t\tcacheRead: msg.usage.cache_read_input_tokens || 0,\n\t\t\t\tcacheWrite: msg.usage.cache_creation_input_tokens || 0,\n\t\t\t\tcost: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\ttotal: 0,\n\t\t\t\t},\n\t\t\t};\n\t\t\tcalculateCost(this.modelInfo, usage);\n\n\t\t\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: blocks,\n\t\t\t\tprovider: this.modelInfo.provider,\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tusage,\n\t\t\t\tstopReason: this.mapStopReason(msg.stop_reason),\n\t\t\t} satisfies AssistantMessage;\n\t\t\toptions?.onEvent?.({ type: \"done\", reason: output.stopReason, message: output });\n\n\t\t\treturn output;\n\t\t} catch (error) {\n\t\t\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: [],\n\t\t\t\tprovider: this.modelInfo.provider,\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tusage: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t\t},\n\t\t\t\tstopReason: \"error\",\n\t\t\t\terror: error instanceof Error ? error.message : JSON.stringify(error),\n\t\t\t} satisfies AssistantMessage;\n\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\treturn output;\n\t\t}\n\t}\n\n\tprivate convertMessages(messages: Message[]): MessageParam[] {\n\t\tconst params: MessageParam[] = [];\n\n\t\tfor (const msg of messages) {\n\t\t\tif (msg.role === \"user\") {\n\t\t\t\t// Handle both string and array content\n\t\t\t\tif (typeof msg.content === \"string\") {\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} else {\n\t\t\t\t\t// Convert array content to Anthropic format\n\t\t\t\t\tconst blocks: ContentBlockParam[] = msg.content.map((item) => {\n\t\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"image\",\n\t\t\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\t\t\ttype: \"base64\",\n\t\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\t\tdata: item.data,\n\t\t\t\t\t\t\t\t},\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\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: blocks,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"assistant\") {\n\t\t\t\tconst blocks: ContentBlockParam[] = [];\n\n\t\t\t\tfor (const block of msg.content) {\n\t\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t});\n\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\tthinking: block.thinking,\n\t\t\t\t\t\t\tsignature: block.thinkingSignature || \"\",\n\t\t\t\t\t\t});\n\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"tool_use\",\n\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\tinput: block.arguments,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\tcontent: blocks,\n\t\t\t\t});\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\t\t\ttool_use_id: msg.toolCallId,\n\t\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t\t\tis_error: msg.isError,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn params;\n\t}\n\n\tprivate convertTools(tools: Context[\"tools\"]): Tool[] {\n\t\tif (!tools) return [];\n\n\t\treturn tools.map((tool) => ({\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: tool.parameters.properties || {},\n\t\t\t\trequired: tool.parameters.required || [],\n\t\t\t},\n\t\t}));\n\t}\n\n\tprivate mapStopReason(reason: Anthropic.Messages.StopReason | null): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"end_turn\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"max_tokens\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"tool_use\":\n\t\t\t\treturn \"toolUse\";\n\t\t\tcase \"refusal\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"pause_turn\": // Stop is good enough -> resubmit\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"stop_sequence\":\n\t\t\t\treturn \"stop\"; // We don't supply stop sequences, so this should never happen\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\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;AAO1C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAa7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAU/C,MAAM,OAAO,YAAY;IAChB,MAAM,CAAY;IAClB,SAAS,CAAQ;IACjB,YAAY,GAAY,KAAK,CAAC;IAEtC,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,C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QAAQ,CAAC;YACjB,KAAK,UAAU;gBACd,OAAO,SAAS,CAAC;YAClB,KAAK,SAAS;gBACb,OAAO,QAAQ,CAAC;YACjB,KAAK,YAAY,EAAE,kCAAkC;gBACpD,OAAO,MAAM,CAAC;YACf,KAAK,eAAe;gBACnB,OAAO,MAAM,CAAC,CAAC,8DAA8D;YAC9E;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n\tContentBlockParam,\n\tMessageCreateParamsStreaming,\n\tMessageParam,\n\tTool,\n} from \"@anthropic-ai/sdk/resources/messages.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tAssistantMessage,\n\tContext,\n\tLLM,\n\tLLMOptions,\n\tMessage,\n\tModel,\n\tStopReason,\n\tTextContent,\n\tThinkingContent,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface AnthropicLLMOptions extends LLMOptions {\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number;\n\t};\n\ttoolChoice?: \"auto\" | \"any\" | \"none\" | { type: \"tool\"; name: string };\n}\n\nexport class AnthropicLLM implements LLM<AnthropicLLMOptions> {\n\tprivate client: Anthropic;\n\tprivate modelInfo: Model;\n\tprivate isOAuthToken: boolean = false;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.ANTHROPIC_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Anthropic API key is required. Set ANTHROPIC_API_KEY environment variable or pass it as an argument.\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tapiKey = process.env.ANTHROPIC_API_KEY;\n\t\t}\n\t\tif (apiKey.includes(\"sk-ant-oat\")) {\n\t\t\tconst defaultHeaders = {\n\t\t\t\taccept: \"application/json\",\n\t\t\t\t\"anthropic-beta\": \"oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14\",\n\t\t\t};\n\n\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t\tthis.client = new Anthropic({ apiKey: null, authToken: apiKey, baseURL: model.baseUrl, defaultHeaders });\n\t\t\tthis.isOAuthToken = true;\n\t\t} else {\n\t\t\tthis.client = new Anthropic({ apiKey, baseURL: model.baseUrl });\n\t\t\tthis.isOAuthToken = false;\n\t\t}\n\t\tthis.modelInfo = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.modelInfo;\n\t}\n\n\tasync generate(context: Context, options?: AnthropicLLMOptions): Promise<AssistantMessage> {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tprovider: this.modelInfo.provider,\n\t\t\tmodel: this.modelInfo.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 messages = this.convertMessages(context.messages);\n\n\t\t\tconst params: MessageCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tmessages,\n\t\t\t\tmax_tokens: options?.maxTokens || 4096,\n\t\t\t\tstream: true,\n\t\t\t};\n\n\t\t\t// For OAuth tokens, we MUST include Claude Code identity\n\t\t\tif (this.isOAuthToken) {\n\t\t\t\tparams.system = [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: \"You are Claude Code, Anthropic's official CLI for Claude.\",\n\t\t\t\t\t\tcache_control: {\n\t\t\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t];\n\t\t\t\tif (context.systemPrompt) {\n\t\t\t\t\tparams.system.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: context.systemPrompt,\n\t\t\t\t\t\tcache_control: {\n\t\t\t\t\t\t\ttype: \"ephemeral\",\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (context.systemPrompt) {\n\t\t\t\tparams.system = context.systemPrompt;\n\t\t\t}\n\n\t\t\tif (options?.temperature !== undefined) {\n\t\t\t\tparams.temperature = options?.temperature;\n\t\t\t}\n\n\t\t\tif (context.tools) {\n\t\t\t\tparams.tools = this.convertTools(context.tools);\n\t\t\t}\n\n\t\t\t// Only enable thinking if the model supports it\n\t\t\tif (options?.thinking?.enabled && this.modelInfo.reasoning) {\n\t\t\t\tparams.thinking = {\n\t\t\t\t\ttype: \"enabled\",\n\t\t\t\t\tbudget_tokens: options.thinking.budgetTokens || 1024,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (options?.toolChoice) {\n\t\t\t\tif (typeof options.toolChoice === \"string\") {\n\t\t\t\t\tparams.tool_choice = { type: options.toolChoice };\n\t\t\t\t} else {\n\t\t\t\t\tparams.tool_choice = options.toolChoice;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst stream = this.client.messages.stream(\n\t\t\t\t{\n\t\t\t\t\t...params,\n\t\t\t\t\tstream: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\n\t\t\toptions?.onEvent?.({ type: \"start\", model: this.modelInfo.id, provider: this.modelInfo.provider });\n\n\t\t\tlet currentBlock: ThinkingContent | TextContent | (ToolCall & { partialJson: string }) | null = null;\n\t\t\tfor await (const event of stream) {\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\toptions?.onEvent?.({ type: \"text_start\" });\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\toptions?.onEvent?.({ type: \"thinking_start\" });\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 to send the event\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\toptions?.onEvent?.({ type: \"text_delta\", content: currentBlock.text, delta: event.delta.text });\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\toptions?.onEvent?.({\n\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\tdelta: event.delta.thinking,\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\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\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\toptions?.onEvent?.({ type: \"toolCall\", toolCall: finalToolCall });\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 = this.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(this.modelInfo, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\toptions?.onEvent?.({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\treturn output;\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\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\treturn output;\n\t\t}\n\t}\n\n\tprivate convertMessages(messages: Message[]): MessageParam[] {\n\t\tconst params: MessageParam[] = [];\n\n\t\t// Transform messages for cross-provider compatibility\n\t\tconst transformedMessages = transformMessages(messages, this.modelInfo);\n\n\t\tfor (const msg of transformedMessages) {\n\t\t\tif (msg.role === \"user\") {\n\t\t\t\t// Handle both string and array content\n\t\t\t\tif (typeof msg.content === \"string\") {\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} else {\n\t\t\t\t\t// Convert array content to Anthropic format\n\t\t\t\t\tconst blocks: ContentBlockParam[] = msg.content.map((item) => {\n\t\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"image\",\n\t\t\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\t\t\ttype: \"base64\",\n\t\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\t\tdata: item.data,\n\t\t\t\t\t\t\t\t},\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\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: blocks,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"assistant\") {\n\t\t\t\tconst blocks: ContentBlockParam[] = [];\n\n\t\t\t\tfor (const block of msg.content) {\n\t\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t});\n\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\tthinking: block.thinking,\n\t\t\t\t\t\t\tsignature: block.thinkingSignature || \"\",\n\t\t\t\t\t\t});\n\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\tblocks.push({\n\t\t\t\t\t\t\ttype: \"tool_use\",\n\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\tinput: block.arguments,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\tcontent: blocks,\n\t\t\t\t});\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\t\t\ttool_use_id: msg.toolCallId,\n\t\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t\t\tis_error: msg.isError,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn params;\n\t}\n\n\tprivate convertTools(tools: Context[\"tools\"]): Tool[] {\n\t\tif (!tools) return [];\n\n\t\treturn tools.map((tool) => ({\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: tool.parameters.properties || {},\n\t\t\t\trequired: tool.parameters.required || [],\n\t\t\t},\n\t\t}));\n\t}\n\n\tprivate mapStopReason(reason: Anthropic.Messages.StopReason | null): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"end_turn\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"max_tokens\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"tool_use\":\n\t\t\t\treturn \"toolUse\";\n\t\t\tcase \"refusal\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"pause_turn\": // Stop is good enough -> resubmit\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"stop_sequence\":\n\t\t\t\treturn \"stop\"; // We don't supply stop sequences, so this should never happen\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
@@ -8,10 +8,10 @@ export interface GoogleLLMOptions extends LLMOptions {
|
|
|
8
8
|
}
|
|
9
9
|
export declare class GoogleLLM implements LLM<GoogleLLMOptions> {
|
|
10
10
|
private client;
|
|
11
|
-
private
|
|
11
|
+
private modelInfo;
|
|
12
12
|
constructor(model: Model, apiKey?: string);
|
|
13
13
|
getModel(): Model;
|
|
14
|
-
|
|
14
|
+
generate(context: Context, options?: GoogleLLMOptions): Promise<AssistantMessage>;
|
|
15
15
|
private convertMessages;
|
|
16
16
|
private convertTools;
|
|
17
17
|
private mapToolChoice;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAML,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,gBAAiB,SAAQ,UAAU;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;AAED,qBAAa,SAAU,YAAW,GAAG,CAAC,gBAAgB,CAAC;IACtD,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,SAAS,CAAQ;gBAEb,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM;IAazC,QAAQ,IAAI,KAAK;IAIX,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA+LvF,OAAO,CAAC,eAAe;IAuFvB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,aAAa;CAwBrB"}
|
package/dist/providers/google.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { FunctionCallingConfigMode, GoogleGenAI, } from "@google/genai";
|
|
2
2
|
import { calculateCost } from "../models.js";
|
|
3
|
+
import { transformMessages } from "./utils.js";
|
|
3
4
|
export class GoogleLLM {
|
|
4
5
|
client;
|
|
5
|
-
|
|
6
|
+
modelInfo;
|
|
6
7
|
constructor(model, apiKey) {
|
|
7
8
|
if (!apiKey) {
|
|
8
9
|
if (!process.env.GEMINI_API_KEY) {
|
|
@@ -11,12 +12,26 @@ export class GoogleLLM {
|
|
|
11
12
|
apiKey = process.env.GEMINI_API_KEY;
|
|
12
13
|
}
|
|
13
14
|
this.client = new GoogleGenAI({ apiKey });
|
|
14
|
-
this.
|
|
15
|
+
this.modelInfo = model;
|
|
15
16
|
}
|
|
16
17
|
getModel() {
|
|
17
|
-
return this.
|
|
18
|
+
return this.modelInfo;
|
|
18
19
|
}
|
|
19
|
-
async
|
|
20
|
+
async generate(context, options) {
|
|
21
|
+
const output = {
|
|
22
|
+
role: "assistant",
|
|
23
|
+
content: [],
|
|
24
|
+
provider: this.modelInfo.provider,
|
|
25
|
+
model: this.modelInfo.id,
|
|
26
|
+
usage: {
|
|
27
|
+
input: 0,
|
|
28
|
+
output: 0,
|
|
29
|
+
cacheRead: 0,
|
|
30
|
+
cacheWrite: 0,
|
|
31
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
32
|
+
},
|
|
33
|
+
stopReason: "stop",
|
|
34
|
+
};
|
|
20
35
|
try {
|
|
21
36
|
const contents = this.convertMessages(context.messages);
|
|
22
37
|
// Build generation config
|
|
@@ -42,7 +57,7 @@ export class GoogleLLM {
|
|
|
42
57
|
};
|
|
43
58
|
}
|
|
44
59
|
// Add thinking config if enabled and model supports it
|
|
45
|
-
if (options?.thinking?.enabled && this.
|
|
60
|
+
if (options?.thinking?.enabled && this.modelInfo.reasoning) {
|
|
46
61
|
config.thinkingConfig = {
|
|
47
62
|
includeThoughts: true,
|
|
48
63
|
...(options.thinking.budgetTokens !== undefined && { thinkingBudget: options.thinking.budgetTokens }),
|
|
@@ -57,23 +72,13 @@ export class GoogleLLM {
|
|
|
57
72
|
}
|
|
58
73
|
// Build the request parameters
|
|
59
74
|
const params = {
|
|
60
|
-
model: this.
|
|
75
|
+
model: this.modelInfo.id,
|
|
61
76
|
contents,
|
|
62
77
|
config,
|
|
63
78
|
};
|
|
64
79
|
const stream = await this.client.models.generateContentStream(params);
|
|
65
|
-
options?.onEvent?.({ type: "start", model: this.
|
|
66
|
-
const blocks = [];
|
|
80
|
+
options?.onEvent?.({ type: "start", model: this.modelInfo.id, provider: this.modelInfo.provider });
|
|
67
81
|
let currentBlock = null;
|
|
68
|
-
let usage = {
|
|
69
|
-
input: 0,
|
|
70
|
-
output: 0,
|
|
71
|
-
cacheRead: 0,
|
|
72
|
-
cacheWrite: 0,
|
|
73
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
74
|
-
};
|
|
75
|
-
let stopReason = "stop";
|
|
76
|
-
// Process the stream
|
|
77
82
|
for await (const chunk of stream) {
|
|
78
83
|
// Extract parts from the chunk
|
|
79
84
|
const candidate = chunk.candidates?.[0];
|
|
@@ -85,7 +90,6 @@ export class GoogleLLM {
|
|
|
85
90
|
if (!currentBlock ||
|
|
86
91
|
(isThinking && currentBlock.type !== "thinking") ||
|
|
87
92
|
(!isThinking && currentBlock.type !== "text")) {
|
|
88
|
-
// Save and finalize current block
|
|
89
93
|
if (currentBlock) {
|
|
90
94
|
if (currentBlock.type === "text") {
|
|
91
95
|
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
|
|
@@ -93,7 +97,6 @@ export class GoogleLLM {
|
|
|
93
97
|
else {
|
|
94
98
|
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
|
|
95
99
|
}
|
|
96
|
-
blocks.push(currentBlock);
|
|
97
100
|
}
|
|
98
101
|
// Start new block
|
|
99
102
|
if (isThinking) {
|
|
@@ -104,6 +107,7 @@ export class GoogleLLM {
|
|
|
104
107
|
currentBlock = { type: "text", text: "" };
|
|
105
108
|
options?.onEvent?.({ type: "text_start" });
|
|
106
109
|
}
|
|
110
|
+
output.content.push(currentBlock);
|
|
107
111
|
}
|
|
108
112
|
// Append content to current block
|
|
109
113
|
if (currentBlock.type === "thinking") {
|
|
@@ -122,7 +126,6 @@ export class GoogleLLM {
|
|
|
122
126
|
}
|
|
123
127
|
// Handle function calls
|
|
124
128
|
if (part.functionCall) {
|
|
125
|
-
// Save current block if exists
|
|
126
129
|
if (currentBlock) {
|
|
127
130
|
if (currentBlock.type === "text") {
|
|
128
131
|
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
|
|
@@ -130,7 +133,6 @@ export class GoogleLLM {
|
|
|
130
133
|
else {
|
|
131
134
|
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
|
|
132
135
|
}
|
|
133
|
-
blocks.push(currentBlock);
|
|
134
136
|
currentBlock = null;
|
|
135
137
|
}
|
|
136
138
|
// Add tool call
|
|
@@ -141,22 +143,22 @@ export class GoogleLLM {
|
|
|
141
143
|
name: part.functionCall.name || "",
|
|
142
144
|
arguments: part.functionCall.args,
|
|
143
145
|
};
|
|
144
|
-
|
|
146
|
+
output.content.push(toolCall);
|
|
145
147
|
options?.onEvent?.({ type: "toolCall", toolCall });
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
151
|
// Map finish reason
|
|
150
152
|
if (candidate?.finishReason) {
|
|
151
|
-
stopReason = this.mapStopReason(candidate.finishReason);
|
|
153
|
+
output.stopReason = this.mapStopReason(candidate.finishReason);
|
|
152
154
|
// Check if we have tool calls in blocks
|
|
153
|
-
if (
|
|
154
|
-
stopReason = "toolUse";
|
|
155
|
+
if (output.content.some((b) => b.type === "toolCall")) {
|
|
156
|
+
output.stopReason = "toolUse";
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
159
|
// Capture usage metadata if available
|
|
158
160
|
if (chunk.usageMetadata) {
|
|
159
|
-
usage = {
|
|
161
|
+
output.usage = {
|
|
160
162
|
input: chunk.usageMetadata.promptTokenCount || 0,
|
|
161
163
|
output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
|
|
162
164
|
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
|
|
@@ -169,9 +171,10 @@ export class GoogleLLM {
|
|
|
169
171
|
total: 0,
|
|
170
172
|
},
|
|
171
173
|
};
|
|
174
|
+
calculateCost(this.modelInfo, output.usage);
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
|
-
//
|
|
177
|
+
// Finalize last block
|
|
175
178
|
if (currentBlock) {
|
|
176
179
|
if (currentBlock.type === "text") {
|
|
177
180
|
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
|
|
@@ -179,43 +182,22 @@ export class GoogleLLM {
|
|
|
179
182
|
else {
|
|
180
183
|
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
|
|
181
184
|
}
|
|
182
|
-
blocks.push(currentBlock);
|
|
183
185
|
}
|
|
184
|
-
|
|
185
|
-
const output = {
|
|
186
|
-
role: "assistant",
|
|
187
|
-
content: blocks,
|
|
188
|
-
provider: this.model.provider,
|
|
189
|
-
model: this.model.id,
|
|
190
|
-
usage,
|
|
191
|
-
stopReason,
|
|
192
|
-
};
|
|
193
|
-
options?.onEvent?.({ type: "done", reason: stopReason, message: output });
|
|
186
|
+
options?.onEvent?.({ type: "done", reason: output.stopReason, message: output });
|
|
194
187
|
return output;
|
|
195
188
|
}
|
|
196
189
|
catch (error) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
content: [],
|
|
200
|
-
provider: this.model.provider,
|
|
201
|
-
model: this.model.id,
|
|
202
|
-
usage: {
|
|
203
|
-
input: 0,
|
|
204
|
-
output: 0,
|
|
205
|
-
cacheRead: 0,
|
|
206
|
-
cacheWrite: 0,
|
|
207
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
208
|
-
},
|
|
209
|
-
stopReason: "error",
|
|
210
|
-
error: error instanceof Error ? error.message : JSON.stringify(error),
|
|
211
|
-
};
|
|
190
|
+
output.stopReason = "error";
|
|
191
|
+
output.error = error instanceof Error ? error.message : JSON.stringify(error);
|
|
212
192
|
options?.onEvent?.({ type: "error", error: output.error });
|
|
213
193
|
return output;
|
|
214
194
|
}
|
|
215
195
|
}
|
|
216
196
|
convertMessages(messages) {
|
|
217
197
|
const contents = [];
|
|
218
|
-
|
|
198
|
+
// Transform messages for cross-provider compatibility
|
|
199
|
+
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
200
|
+
for (const msg of transformedMessages) {
|
|
219
201
|
if (msg.role === "user") {
|
|
220
202
|
// Handle both string and array content
|
|
221
203
|
if (typeof msg.content === "string") {
|