@mariozechner/pi-ai 0.5.24 → 0.5.26
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/dist/models.generated.d.ts +398 -623
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +1529 -1754
- package/dist/models.generated.js.map +1 -1
- package/dist/providers/anthropic.d.ts +1 -0
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +9 -2
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts +1 -0
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +9 -2
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +1 -0
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +9 -2
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses.d.ts +1 -0
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +35 -8
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/utils.d.ts +1 -1
- package/dist/providers/utils.d.ts.map +1 -1
- package/dist/providers/utils.js +3 -3
- package/dist/providers/utils.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -15,6 +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
|
+
getApi(): string;
|
|
18
19
|
generate(context: Context, options?: AnthropicLLMOptions): Promise<AssistantMessage>;
|
|
19
20
|
private convertMessages;
|
|
20
21
|
private convertTools;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAQA,OAAO,KA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AK,EAAE,MAAM,CAAC,EAAE,MAAM;IAwCzC,QAAQ,IAAI,KAAK;IAIjB,MAAM,IAAI,MAAM;IAIV,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAoL1F,OAAO,CAAC,eAAe;IAwFvB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,aAAa;CAkBrB"}
|
|
@@ -45,10 +45,14 @@ export class AnthropicLLM {
|
|
|
45
45
|
getModel() {
|
|
46
46
|
return this.modelInfo;
|
|
47
47
|
}
|
|
48
|
+
getApi() {
|
|
49
|
+
return "anthropic-messages";
|
|
50
|
+
}
|
|
48
51
|
async generate(context, options) {
|
|
49
52
|
const output = {
|
|
50
53
|
role: "assistant",
|
|
51
54
|
content: [],
|
|
55
|
+
api: this.getApi(),
|
|
52
56
|
provider: this.modelInfo.provider,
|
|
53
57
|
model: this.modelInfo.id,
|
|
54
58
|
usage: {
|
|
@@ -225,7 +229,7 @@ export class AnthropicLLM {
|
|
|
225
229
|
convertMessages(messages) {
|
|
226
230
|
const params = [];
|
|
227
231
|
// Transform messages for cross-provider compatibility
|
|
228
|
-
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
232
|
+
const transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());
|
|
229
233
|
for (const msg of transformedMessages) {
|
|
230
234
|
if (msg.role === "user") {
|
|
231
235
|
// Handle both string and array content
|
|
@@ -256,9 +260,12 @@ export class AnthropicLLM {
|
|
|
256
260
|
};
|
|
257
261
|
}
|
|
258
262
|
});
|
|
263
|
+
const filteredBlocks = !this.modelInfo?.input.includes("image")
|
|
264
|
+
? blocks.filter((b) => b.type !== "image")
|
|
265
|
+
: blocks;
|
|
259
266
|
params.push({
|
|
260
267
|
role: "user",
|
|
261
|
-
content:
|
|
268
|
+
content: filteredBlocks,
|
|
262
269
|
});
|
|
263
270
|
}
|
|
264
271
|
}
|
|
@@ -1 +1 @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;4BACnB,WAAW,EAAE,GAAG,CAAC,UAAU;4BAC3B,OAAO,EAAE,GAAG,CAAC,OAAO;4BACpB,QAAQ,EAAE,GAAG,CAAC,OAAO;yBACrB;qBACD;iBACD,CAAC,CAAC;YACJ,CAAC;QACF,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,YAAY,CAAC,KAAuB;QAC3C,IAAI,CAAC,KAAK;YAAE,OAAO,EAAE,CAAC;QAEtB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC3B,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,YAAY,EAAE;gBACb,IAAI,EAAE,QAAiB;gBACvB,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,EAAE;gBAC5C,QAAQ,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE;aACxC;SACD,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,MAA4C;QACjE,QAAQ,MAAM,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\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-dangerous-direct-browser-access\": \"true\",\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\t// Clear the env var if we're in Node.js to prevent SDK from using it\n\t\t\tif (typeof process !== \"undefined\" && process.env) {\n\t\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t\t}\n\t\t\tthis.client = new Anthropic({\n\t\t\t\tapiKey: null,\n\t\t\t\tauthToken: apiKey,\n\t\t\t\tbaseURL: model.baseUrl,\n\t\t\t\tdefaultHeaders,\n\t\t\t\tdangerouslyAllowBrowser: true,\n\t\t\t});\n\t\t\tthis.isOAuthToken = true;\n\t\t} else {\n\t\t\tconst defaultHeaders = {\n\t\t\t\taccept: \"application/json\",\n\t\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\t\"anthropic-beta\": \"fine-grained-tool-streaming-2025-05-14\",\n\t\t\t};\n\t\t\tthis.client = new Anthropic({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true, defaultHeaders });\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"]}
|
|
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,CAAC;gBACpC,MAAM,IAAI,KAAK,CACd,sGAAsG,CACtG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;QACxC,CAAC;QACD,IAAI,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;YACnC,MAAM,cAAc,GAAG;gBACtB,MAAM,EAAE,kBAAkB;gBAC1B,2CAA2C,EAAE,MAAM;gBACnD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,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,YAAY,EAAE;gBACb,IAAI,EAAE,QAAiB;gBACvB,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,EAAE;gBAC5C,QAAQ,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE;aACxC;SACD,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,MAA4C;QACjE,QAAQ,MAAM,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\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-dangerous-direct-browser-access\": \"true\",\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\t// Clear the env var if we're in Node.js to prevent SDK from using it\n\t\t\tif (typeof process !== \"undefined\" && process.env) {\n\t\t\t\tprocess.env.ANTHROPIC_API_KEY = undefined;\n\t\t\t}\n\t\t\tthis.client = new Anthropic({\n\t\t\t\tapiKey: null,\n\t\t\t\tauthToken: apiKey,\n\t\t\t\tbaseURL: model.baseUrl,\n\t\t\t\tdefaultHeaders,\n\t\t\t\tdangerouslyAllowBrowser: true,\n\t\t\t});\n\t\t\tthis.isOAuthToken = true;\n\t\t} else {\n\t\t\tconst defaultHeaders = {\n\t\t\t\taccept: \"application/json\",\n\t\t\t\t\"anthropic-dangerous-direct-browser-access\": \"true\",\n\t\t\t\t\"anthropic-beta\": \"fine-grained-tool-streaming-2025-05-14\",\n\t\t\t};\n\t\t\tthis.client = new Anthropic({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true, defaultHeaders });\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\tgetApi(): string {\n\t\treturn \"anthropic-messages\";\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\tapi: this.getApi(),\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, this.getApi());\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\tconst filteredBlocks = !this.modelInfo?.input.includes(\"image\")\n\t\t\t\t\t\t? blocks.filter((b) => b.type !== \"image\")\n\t\t\t\t\t\t: blocks;\n\t\t\t\t\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: filteredBlocks,\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"]}
|
|
@@ -11,6 +11,7 @@ export declare class GoogleLLM implements LLM<GoogleLLMOptions> {
|
|
|
11
11
|
private modelInfo;
|
|
12
12
|
constructor(model: Model, apiKey?: string);
|
|
13
13
|
getModel(): Model;
|
|
14
|
+
getApi(): string;
|
|
14
15
|
generate(context: Context, options?: GoogleLLMOptions): Promise<AssistantMessage>;
|
|
15
16
|
private convertMessages;
|
|
16
17
|
private convertTools;
|
|
@@ -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,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;
|
|
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;IAIjB,MAAM,IAAI,MAAM;IAIV,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAgMvF,OAAO,CAAC,eAAe;IA0FvB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,aAAa;CAwBrB"}
|
package/dist/providers/google.js
CHANGED
|
@@ -17,10 +17,14 @@ export class GoogleLLM {
|
|
|
17
17
|
getModel() {
|
|
18
18
|
return this.modelInfo;
|
|
19
19
|
}
|
|
20
|
+
getApi() {
|
|
21
|
+
return "google-generative-ai";
|
|
22
|
+
}
|
|
20
23
|
async generate(context, options) {
|
|
21
24
|
const output = {
|
|
22
25
|
role: "assistant",
|
|
23
26
|
content: [],
|
|
27
|
+
api: this.getApi(),
|
|
24
28
|
provider: this.modelInfo.provider,
|
|
25
29
|
model: this.modelInfo.id,
|
|
26
30
|
usage: {
|
|
@@ -196,7 +200,7 @@ export class GoogleLLM {
|
|
|
196
200
|
convertMessages(messages) {
|
|
197
201
|
const contents = [];
|
|
198
202
|
// Transform messages for cross-provider compatibility
|
|
199
|
-
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
203
|
+
const transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());
|
|
200
204
|
for (const msg of transformedMessages) {
|
|
201
205
|
if (msg.role === "user") {
|
|
202
206
|
// Handle both string and array content
|
|
@@ -222,9 +226,12 @@ export class GoogleLLM {
|
|
|
222
226
|
};
|
|
223
227
|
}
|
|
224
228
|
});
|
|
229
|
+
const filteredParts = !this.modelInfo?.input.includes("image")
|
|
230
|
+
? parts.filter((p) => p.text !== undefined)
|
|
231
|
+
: parts;
|
|
225
232
|
contents.push({
|
|
226
233
|
role: "user",
|
|
227
|
-
parts,
|
|
234
|
+
parts: filteredParts,
|
|
228
235
|
});
|
|
229
236
|
}
|
|
230
237
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,yBAAyB,EAGzB,WAAW,GAEX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAc7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAU/C,MAAM,OAAO,SAAS;IACb,MAAM,CAAc;IACpB,SAAS,CAAQ;IAEzB,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CACd,gGAAgG,CAChG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QAC1C,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,QAAQ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B,KAAK,sBAAsB;gBAC1B,OAAO,OAAO,CAAC;YAChB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import {\n\ttype Content,\n\ttype FinishReason,\n\tFunctionCallingConfigMode,\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\tGoogleGenAI,\n\ttype Part,\n} from \"@google/genai\";\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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface GoogleLLMOptions extends LLMOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\";\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number; // -1 for dynamic, 0 to disable\n\t};\n}\n\nexport class GoogleLLM implements LLM<GoogleLLMOptions> {\n\tprivate client: GoogleGenAI;\n\tprivate modelInfo: Model;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.GEMINI_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Gemini API key is required. Set GEMINI_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.GEMINI_API_KEY;\n\t\t}\n\t\tthis.client = new GoogleGenAI({ apiKey });\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?: GoogleLLMOptions): 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\t\ttry {\n\t\t\tconst contents = this.convertMessages(context.messages);\n\n\t\t\t// Build generation config\n\t\t\tconst generationConfig: GenerateContentConfig = {};\n\t\t\tif (options?.temperature !== undefined) {\n\t\t\t\tgenerationConfig.temperature = options.temperature;\n\t\t\t}\n\t\t\tif (options?.maxTokens !== undefined) {\n\t\t\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t\t\t}\n\n\t\t\t// Build the config object\n\t\t\tconst config: GenerateContentConfig = {\n\t\t\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t\t\t...(context.systemPrompt && { systemInstruction: context.systemPrompt }),\n\t\t\t\t...(context.tools && { tools: this.convertTools(context.tools) }),\n\t\t\t};\n\n\t\t\t// Add tool config if needed\n\t\t\tif (context.tools && options?.toolChoice) {\n\t\t\t\tconfig.toolConfig = {\n\t\t\t\t\tfunctionCallingConfig: {\n\t\t\t\t\t\tmode: this.mapToolChoice(options.toolChoice),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Add thinking config if enabled and model supports it\n\t\t\tif (options?.thinking?.enabled && this.modelInfo.reasoning) {\n\t\t\t\tconfig.thinkingConfig = {\n\t\t\t\t\tincludeThoughts: true,\n\t\t\t\t\t...(options.thinking.budgetTokens !== undefined && { thinkingBudget: options.thinking.budgetTokens }),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Abort signal\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tthrow new Error(\"Request aborted\");\n\t\t\t\t}\n\t\t\t\tconfig.abortSignal = options.signal;\n\t\t\t}\n\n\t\t\t// Build the request parameters\n\t\t\tconst params: GenerateContentParameters = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tcontents,\n\t\t\t\tconfig,\n\t\t\t};\n\n\t\t\tconst stream = await this.client.models.generateContentStream(params);\n\n\t\t\toptions?.onEvent?.({ type: \"start\", model: this.modelInfo.id, provider: this.modelInfo.provider });\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tfor await (const chunk of stream) {\n\t\t\t\t// Extract parts from the chunk\n\t\t\t\tconst candidate = chunk.candidates?.[0];\n\t\t\t\tif (candidate?.content?.parts) {\n\t\t\t\t\tfor (const part of candidate.content.parts) {\n\t\t\t\t\t\tif (part.text !== undefined) {\n\t\t\t\t\t\t\tconst isThinking = part.thought === true;\n\n\t\t\t\t\t\t\t// Check if we need to switch blocks\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\t(isThinking && currentBlock.type !== \"thinking\") ||\n\t\t\t\t\t\t\t\t(!isThinking && currentBlock.type !== \"text\")\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// Start new block\n\t\t\t\t\t\t\t\tif (isThinking) {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Append content to current block\n\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\tcurrentBlock.thinking += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = part.thoughtSignature;\n\t\t\t\t\t\t\t\toptions?.onEvent?.({\n\t\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcurrentBlock.text += part.text;\n\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_delta\", content: currentBlock.text, delta: part.text });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Handle function calls\n\t\t\t\t\t\tif (part.functionCall) {\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Add tool call\n\t\t\t\t\t\t\tconst toolCallId = part.functionCall.id || `${part.functionCall.name}_${Date.now()}`;\n\t\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: toolCallId,\n\t\t\t\t\t\t\t\tname: part.functionCall.name || \"\",\n\t\t\t\t\t\t\t\targuments: part.functionCall.args as Record<string, any>,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\toutput.content.push(toolCall);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Map finish reason\n\t\t\t\tif (candidate?.finishReason) {\n\t\t\t\t\toutput.stopReason = this.mapStopReason(candidate.finishReason);\n\t\t\t\t\t// Check if we have tool calls in blocks\n\t\t\t\t\tif (output.content.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Capture usage metadata if available\n\t\t\t\tif (chunk.usageMetadata) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput: chunk.usageMetadata.promptTokenCount || 0,\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\t\t\t\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(this.modelInfo, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Finalize last block\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t} else {\n\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\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[]): Content[] {\n\t\tconst contents: Content[] = [];\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\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts: [{ text: msg.content }],\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// Convert array content to Google format\n\t\t\t\t\tconst parts: Part[] = msg.content.map((item) => {\n\t\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\t\treturn { text: item.text };\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - Google uses inlineData\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tinlineData: {\n\t\t\t\t\t\t\t\t\tmimeType: item.mimeType,\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\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts,\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 parts: Part[] = [];\n\n\t\t\t\t// Process content blocks\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\tparts.push({ text: block.text });\n\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\tconst thinkingPart: Part = {\n\t\t\t\t\t\t\tthought: true,\n\t\t\t\t\t\t\tthoughtSignature: block.thinkingSignature,\n\t\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tparts.push(thinkingPart);\n\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\tparts.push({\n\t\t\t\t\t\t\tfunctionCall: {\n\t\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\t\targs: block.arguments,\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\n\t\t\t\tif (parts.length > 0) {\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"model\",\n\t\t\t\t\t\tparts,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tcontents.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tparts: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfunctionResponse: {\n\t\t\t\t\t\t\t\tid: msg.toolCallId,\n\t\t\t\t\t\t\t\tname: msg.toolName,\n\t\t\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\t\t\tresult: msg.content,\n\t\t\t\t\t\t\t\t\tisError: msg.isError,\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});\n\t\t\t}\n\t\t}\n\n\t\treturn contents;\n\t}\n\n\tprivate convertTools(tools: Tool[]): any[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tfunctionDeclarations: tools.map((tool) => ({\n\t\t\t\t\tname: tool.name,\n\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\tparameters: tool.parameters,\n\t\t\t\t})),\n\t\t\t},\n\t\t];\n\t}\n\n\tprivate mapToolChoice(choice: string): FunctionCallingConfigMode {\n\t\tswitch (choice) {\n\t\t\tcase \"auto\":\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t\tcase \"none\":\n\t\t\t\treturn FunctionCallingConfigMode.NONE;\n\t\t\tcase \"any\":\n\t\t\t\treturn FunctionCallingConfigMode.ANY;\n\t\t\tdefault:\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t}\n\t}\n\n\tprivate mapStopReason(reason: FinishReason): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"STOP\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"MAX_TOKENS\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"BLOCKLIST\":\n\t\t\tcase \"PROHIBITED_CONTENT\":\n\t\t\tcase \"SPII\":\n\t\t\tcase \"SAFETY\":\n\t\t\tcase \"IMAGE_SAFETY\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"RECITATION\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"FINISH_REASON_UNSPECIFIED\":\n\t\t\tcase \"OTHER\":\n\t\t\tcase \"LANGUAGE\":\n\t\t\tcase \"MALFORMED_FUNCTION_CALL\":\n\t\t\tcase \"UNEXPECTED_TOOL_CALL\":\n\t\t\t\treturn \"error\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,yBAAyB,EAGzB,WAAW,GAEX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAc7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAU/C,MAAM,OAAO,SAAS;IACb,MAAM,CAAc;IACpB,SAAS,CAAQ;IAEzB,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CACd,gGAAgG,CAChG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC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K,KAAK;gBACT,OAAO,yBAAyB,CAAC,GAAG,CAAC;YACtC;gBACC,OAAO,yBAAyB,CAAC,IAAI,CAAC;QACxC,CAAC;IACF,CAAC;IAEO,aAAa,CAAC,MAAoB;QACzC,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,MAAM;gBACV,OAAO,MAAM,CAAC;YACf,KAAK,YAAY;gBAChB,OAAO,QAAQ,CAAC;YACjB,KAAK,WAAW,CAAC;YACjB,KAAK,oBAAoB,CAAC;YAC1B,KAAK,MAAM,CAAC;YACZ,KAAK,QAAQ,CAAC;YACd,KAAK,cAAc;gBAClB,OAAO,QAAQ,CAAC;YACjB,KAAK,YAAY;gBAChB,OAAO,QAAQ,CAAC;YACjB,KAAK,2BAA2B,CAAC;YACjC,KAAK,OAAO,CAAC;YACb,KAAK,UAAU,CAAC;YAChB,KAAK,yBAAyB,CAAC;YAC/B,KAAK,sBAAsB;gBAC1B,OAAO,OAAO,CAAC;YAChB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import {\n\ttype Content,\n\ttype FinishReason,\n\tFunctionCallingConfigMode,\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\tGoogleGenAI,\n\ttype Part,\n} from \"@google/genai\";\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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface GoogleLLMOptions extends LLMOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\";\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number; // -1 for dynamic, 0 to disable\n\t};\n}\n\nexport class GoogleLLM implements LLM<GoogleLLMOptions> {\n\tprivate client: GoogleGenAI;\n\tprivate modelInfo: Model;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.GEMINI_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Gemini API key is required. Set GEMINI_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.GEMINI_API_KEY;\n\t\t}\n\t\tthis.client = new GoogleGenAI({ apiKey });\n\t\tthis.modelInfo = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.modelInfo;\n\t}\n\n\tgetApi(): string {\n\t\treturn \"google-generative-ai\";\n\t}\n\n\tasync generate(context: Context, options?: GoogleLLMOptions): Promise<AssistantMessage> {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: this.getApi(),\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\t\ttry {\n\t\t\tconst contents = this.convertMessages(context.messages);\n\n\t\t\t// Build generation config\n\t\t\tconst generationConfig: GenerateContentConfig = {};\n\t\t\tif (options?.temperature !== undefined) {\n\t\t\t\tgenerationConfig.temperature = options.temperature;\n\t\t\t}\n\t\t\tif (options?.maxTokens !== undefined) {\n\t\t\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t\t\t}\n\n\t\t\t// Build the config object\n\t\t\tconst config: GenerateContentConfig = {\n\t\t\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t\t\t...(context.systemPrompt && { systemInstruction: context.systemPrompt }),\n\t\t\t\t...(context.tools && { tools: this.convertTools(context.tools) }),\n\t\t\t};\n\n\t\t\t// Add tool config if needed\n\t\t\tif (context.tools && options?.toolChoice) {\n\t\t\t\tconfig.toolConfig = {\n\t\t\t\t\tfunctionCallingConfig: {\n\t\t\t\t\t\tmode: this.mapToolChoice(options.toolChoice),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Add thinking config if enabled and model supports it\n\t\t\tif (options?.thinking?.enabled && this.modelInfo.reasoning) {\n\t\t\t\tconfig.thinkingConfig = {\n\t\t\t\t\tincludeThoughts: true,\n\t\t\t\t\t...(options.thinking.budgetTokens !== undefined && { thinkingBudget: options.thinking.budgetTokens }),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Abort signal\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tthrow new Error(\"Request aborted\");\n\t\t\t\t}\n\t\t\t\tconfig.abortSignal = options.signal;\n\t\t\t}\n\n\t\t\t// Build the request parameters\n\t\t\tconst params: GenerateContentParameters = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tcontents,\n\t\t\t\tconfig,\n\t\t\t};\n\n\t\t\tconst stream = await this.client.models.generateContentStream(params);\n\n\t\t\toptions?.onEvent?.({ type: \"start\", model: this.modelInfo.id, provider: this.modelInfo.provider });\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tfor await (const chunk of stream) {\n\t\t\t\t// Extract parts from the chunk\n\t\t\t\tconst candidate = chunk.candidates?.[0];\n\t\t\t\tif (candidate?.content?.parts) {\n\t\t\t\t\tfor (const part of candidate.content.parts) {\n\t\t\t\t\t\tif (part.text !== undefined) {\n\t\t\t\t\t\t\tconst isThinking = part.thought === true;\n\n\t\t\t\t\t\t\t// Check if we need to switch blocks\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\t(isThinking && currentBlock.type !== \"thinking\") ||\n\t\t\t\t\t\t\t\t(!isThinking && currentBlock.type !== \"text\")\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// Start new block\n\t\t\t\t\t\t\t\tif (isThinking) {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Append content to current block\n\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\tcurrentBlock.thinking += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = part.thoughtSignature;\n\t\t\t\t\t\t\t\toptions?.onEvent?.({\n\t\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcurrentBlock.text += part.text;\n\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_delta\", content: currentBlock.text, delta: part.text });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Handle function calls\n\t\t\t\t\t\tif (part.functionCall) {\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Add tool call\n\t\t\t\t\t\t\tconst toolCallId = part.functionCall.id || `${part.functionCall.name}_${Date.now()}`;\n\t\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: toolCallId,\n\t\t\t\t\t\t\t\tname: part.functionCall.name || \"\",\n\t\t\t\t\t\t\t\targuments: part.functionCall.args as Record<string, any>,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\toutput.content.push(toolCall);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Map finish reason\n\t\t\t\tif (candidate?.finishReason) {\n\t\t\t\t\toutput.stopReason = this.mapStopReason(candidate.finishReason);\n\t\t\t\t\t// Check if we have tool calls in blocks\n\t\t\t\t\tif (output.content.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Capture usage metadata if available\n\t\t\t\tif (chunk.usageMetadata) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput: chunk.usageMetadata.promptTokenCount || 0,\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\t\t\t\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(this.modelInfo, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Finalize last block\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t} else {\n\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\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[]): Content[] {\n\t\tconst contents: Content[] = [];\n\n\t\t// Transform messages for cross-provider compatibility\n\t\tconst transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());\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\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts: [{ text: msg.content }],\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// Convert array content to Google format\n\t\t\t\t\tconst parts: Part[] = msg.content.map((item) => {\n\t\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\t\treturn { text: item.text };\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - Google uses inlineData\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tinlineData: {\n\t\t\t\t\t\t\t\t\tmimeType: item.mimeType,\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\tconst filteredParts = !this.modelInfo?.input.includes(\"image\")\n\t\t\t\t\t\t? parts.filter((p) => p.text !== undefined)\n\t\t\t\t\t\t: parts;\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts: filteredParts,\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 parts: Part[] = [];\n\n\t\t\t\t// Process content blocks\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\tparts.push({ text: block.text });\n\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\tconst thinkingPart: Part = {\n\t\t\t\t\t\t\tthought: true,\n\t\t\t\t\t\t\tthoughtSignature: block.thinkingSignature,\n\t\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tparts.push(thinkingPart);\n\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\tparts.push({\n\t\t\t\t\t\t\tfunctionCall: {\n\t\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\t\targs: block.arguments,\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\n\t\t\t\tif (parts.length > 0) {\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"model\",\n\t\t\t\t\t\tparts,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tcontents.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tparts: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfunctionResponse: {\n\t\t\t\t\t\t\t\tid: msg.toolCallId,\n\t\t\t\t\t\t\t\tname: msg.toolName,\n\t\t\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\t\t\tresult: msg.content,\n\t\t\t\t\t\t\t\t\tisError: msg.isError,\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});\n\t\t\t}\n\t\t}\n\n\t\treturn contents;\n\t}\n\n\tprivate convertTools(tools: Tool[]): any[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tfunctionDeclarations: tools.map((tool) => ({\n\t\t\t\t\tname: tool.name,\n\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\tparameters: tool.parameters,\n\t\t\t\t})),\n\t\t\t},\n\t\t];\n\t}\n\n\tprivate mapToolChoice(choice: string): FunctionCallingConfigMode {\n\t\tswitch (choice) {\n\t\t\tcase \"auto\":\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t\tcase \"none\":\n\t\t\t\treturn FunctionCallingConfigMode.NONE;\n\t\t\tcase \"any\":\n\t\t\t\treturn FunctionCallingConfigMode.ANY;\n\t\t\tdefault:\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t}\n\t}\n\n\tprivate mapStopReason(reason: FinishReason): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"STOP\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"MAX_TOKENS\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"BLOCKLIST\":\n\t\t\tcase \"PROHIBITED_CONTENT\":\n\t\t\tcase \"SPII\":\n\t\t\tcase \"SAFETY\":\n\t\t\tcase \"IMAGE_SAFETY\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"RECITATION\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"FINISH_REASON_UNSPECIFIED\":\n\t\t\tcase \"OTHER\":\n\t\t\tcase \"LANGUAGE\":\n\t\t\tcase \"MALFORMED_FUNCTION_CALL\":\n\t\t\tcase \"UNEXPECTED_TOOL_CALL\":\n\t\t\t\treturn \"error\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
@@ -13,6 +13,7 @@ export declare class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOpt
|
|
|
13
13
|
private modelInfo;
|
|
14
14
|
constructor(model: Model, apiKey?: string);
|
|
15
15
|
getModel(): Model;
|
|
16
|
+
getApi(): string;
|
|
16
17
|
generate(request: Context, options?: OpenAICompletionsLLMOptions): Promise<AssistantMessage>;
|
|
17
18
|
private convertMessages;
|
|
18
19
|
private convertTools;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAML,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,2BAA4B,SAAQ,UAAU;IAC9D,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG;QAAE,IAAI,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IAC7F,eAAe,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;CAC5C;AAED,qBAAa,oBAAqB,YAAW,GAAG,CAAC,2BAA2B,CAAC;IAC5E,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAQ;gBAEb,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM;IAazC,QAAQ,IAAI,KAAK;
|
|
1
|
+
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAML,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,2BAA4B,SAAQ,UAAU;IAC9D,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG;QAAE,IAAI,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IAC7F,eAAe,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;CAC5C;AAED,qBAAa,oBAAqB,YAAW,GAAG,CAAC,2BAA2B,CAAC;IAC5E,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAQ;gBAEb,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM;IAazC,QAAQ,IAAI,KAAK;IAIjB,MAAM,IAAI,MAAM;IAIV,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,2BAA2B,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAsQlG,OAAO,CAAC,eAAe;IAoGvB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,aAAa;CAerB"}
|
|
@@ -17,10 +17,14 @@ export class OpenAICompletionsLLM {
|
|
|
17
17
|
getModel() {
|
|
18
18
|
return this.modelInfo;
|
|
19
19
|
}
|
|
20
|
+
getApi() {
|
|
21
|
+
return "openai-completions";
|
|
22
|
+
}
|
|
20
23
|
async generate(request, options) {
|
|
21
24
|
const output = {
|
|
22
25
|
role: "assistant",
|
|
23
26
|
content: [],
|
|
27
|
+
api: this.getApi(),
|
|
24
28
|
provider: this.modelInfo.provider,
|
|
25
29
|
model: this.modelInfo.id,
|
|
26
30
|
usage: {
|
|
@@ -258,7 +262,7 @@ export class OpenAICompletionsLLM {
|
|
|
258
262
|
convertMessages(messages, systemPrompt) {
|
|
259
263
|
const params = [];
|
|
260
264
|
// Transform messages for cross-provider compatibility
|
|
261
|
-
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
265
|
+
const transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());
|
|
262
266
|
// Add system prompt if provided
|
|
263
267
|
if (systemPrompt) {
|
|
264
268
|
// Cerebras/xAi don't like the "developer" role
|
|
@@ -297,9 +301,12 @@ export class OpenAICompletionsLLM {
|
|
|
297
301
|
};
|
|
298
302
|
}
|
|
299
303
|
});
|
|
304
|
+
const filteredContent = !this.modelInfo?.input.includes("image")
|
|
305
|
+
? content.filter((c) => c.type !== "image_url")
|
|
306
|
+
: content;
|
|
300
307
|
params.push({
|
|
301
308
|
role: "user",
|
|
302
|
-
content,
|
|
309
|
+
content: filteredContent,
|
|
303
310
|
});
|
|
304
311
|
}
|
|
305
312
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.js","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAQ5B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAc7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAO/C,MAAM,OAAO,oBAAoB;IACxB,MAAM,CAAS;IACf,SAAS,CAAQ;IAEzB,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CACd,gGAAgG,CAChG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,IAAI,EAA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;YACjB,KAAK,eAAe,CAAC;YACrB,KAAK,YAAY;gBAChB,OAAO,SAAS,CAAC;YAClB,KAAK,gBAAgB;gBACpB,OAAO,QAAQ,CAAC;YACjB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tChatCompletionChunk,\n\tChatCompletionContentPart,\n\tChatCompletionContentPartImage,\n\tChatCompletionContentPartText,\n\tChatCompletionMessageParam,\n} from \"openai/resources/chat/completions.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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface OpenAICompletionsLLMOptions extends LLMOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"required\" | { type: \"function\"; function: { name: string } };\n\treasoningEffort?: \"low\" | \"medium\" | \"high\";\n}\n\nexport class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOptions> {\n\tprivate client: OpenAI;\n\tprivate modelInfo: Model;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.OPENAI_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"OpenAI API key is required. Set OPENAI_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.OPENAI_API_KEY;\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true });\n\t\tthis.modelInfo = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.modelInfo;\n\t}\n\n\tasync generate(request: Context, options?: OpenAICompletionsLLMOptions): 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(request.messages, request.systemPrompt);\n\n\t\t\tconst params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tmessages,\n\t\t\t\tstream: true,\n\t\t\t\tstream_options: { include_usage: true },\n\t\t\t};\n\n\t\t\t// Cerebras/xAI dont like the \"store\" field\n\t\t\tif (!this.modelInfo.baseUrl?.includes(\"cerebras.ai\") && !this.modelInfo.baseUrl?.includes(\"api.x.ai\")) {\n\t\t\t\tparams.store = false;\n\t\t\t}\n\n\t\t\tif (options?.maxTokens) {\n\t\t\t\tparams.max_completion_tokens = options?.maxTokens;\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 (request.tools) {\n\t\t\t\tparams.tools = this.convertTools(request.tools);\n\t\t\t}\n\n\t\t\tif (options?.toolChoice) {\n\t\t\t\tparams.tool_choice = options.toolChoice;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\toptions?.reasoningEffort &&\n\t\t\t\tthis.modelInfo.reasoning &&\n\t\t\t\t!this.modelInfo.id.toLowerCase().includes(\"grok\")\n\t\t\t) {\n\t\t\t\tparams.reasoning_effort = options.reasoningEffort;\n\t\t\t}\n\n\t\t\tconst stream = await this.client.chat.completions.create(params, {\n\t\t\t\tsignal: options?.signal,\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: TextContent | ThinkingContent | (ToolCall & { partialArgs?: string }) | null = null;\n\t\t\tfor await (const chunk of stream) {\n\t\t\t\tif (chunk.usage) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput: chunk.usage.prompt_tokens || 0,\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usage.completion_tokens || 0) +\n\t\t\t\t\t\t\t(chunk.usage.completion_tokens_details?.reasoning_tokens || 0),\n\t\t\t\t\t\tcacheRead: chunk.usage.prompt_tokens_details?.cached_tokens || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(this.modelInfo, output.usage);\n\t\t\t\t}\n\n\t\t\t\tconst choice = chunk.choices[0];\n\t\t\t\tif (!choice) continue;\n\n\t\t\t\t// Capture finish reason\n\t\t\t\tif (choice.finish_reason) {\n\t\t\t\t\toutput.stopReason = this.mapStopReason(choice.finish_reason);\n\t\t\t\t}\n\n\t\t\t\tif (choice.delta) {\n\t\t\t\t\t// Handle text content\n\t\t\t\t\tif (\n\t\t\t\t\t\tchoice.delta.content !== null &&\n\t\t\t\t\t\tchoice.delta.content !== undefined &&\n\t\t\t\t\t\tchoice.delta.content.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to text block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"text\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new text block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to text block\n\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\tcurrentBlock.text += choice.delta.content;\n\t\t\t\t\t\t\toptions?.onEvent?.({\n\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\tdelta: choice.delta.content,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle reasoning_content field\n\t\t\t\t\tif (\n\t\t\t\t\t\t(choice.delta as any).reasoning_content !== null &&\n\t\t\t\t\t\t(choice.delta as any).reasoning_content !== undefined &&\n\t\t\t\t\t\t(choice.delta as any).reasoning_content.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to thinking block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"thinking\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new thinking block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: \"reasoning_content\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to thinking block\n\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tconst delta = (choice.delta as any).reasoning_content;\n\t\t\t\t\t\t\tcurrentBlock.thinking += delta;\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_delta\", content: currentBlock.thinking, delta });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle reasoning field\n\t\t\t\t\tif (\n\t\t\t\t\t\t(choice.delta as any).reasoning !== null &&\n\t\t\t\t\t\t(choice.delta as any).reasoning !== undefined &&\n\t\t\t\t\t\t(choice.delta as any).reasoning.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to thinking block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"thinking\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new thinking block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: \"reasoning\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to thinking block\n\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tconst delta = (choice.delta as any).reasoning;\n\t\t\t\t\t\t\tcurrentBlock.thinking += delta;\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_delta\", content: currentBlock.thinking, delta });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle tool calls\n\t\t\t\t\tif (choice?.delta?.tool_calls) {\n\t\t\t\t\t\tfor (const toolCall of choice.delta.tool_calls) {\n\t\t\t\t\t\t\t// Check if we need a new tool call block\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\tcurrentBlock.type !== \"toolCall\" ||\n\t\t\t\t\t\t\t\t(toolCall.id && currentBlock.id !== toolCall.id)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// Start new tool call block\n\t\t\t\t\t\t\t\tcurrentBlock = {\n\t\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\t\tid: toolCall.id || \"\",\n\t\t\t\t\t\t\t\t\tname: toolCall.function?.name || \"\",\n\t\t\t\t\t\t\t\t\targuments: {},\n\t\t\t\t\t\t\t\t\tpartialArgs: \"\",\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Accumulate tool call data\n\t\t\t\t\t\t\tif (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\tif (toolCall.id) currentBlock.id = toolCall.id;\n\t\t\t\t\t\t\t\tif (toolCall.function?.name) currentBlock.name = toolCall.function.name;\n\t\t\t\t\t\t\t\tif (toolCall.function?.arguments) {\n\t\t\t\t\t\t\t\t\tcurrentBlock.partialArgs += toolCall.function.arguments;\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}\n\t\t\t}\n\n\t\t\t// Save final block if exists\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\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\toptions?.onEvent?.({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\treturn output;\n\t\t} catch (error) {\n\t\t\t// Update output with error information\n\t\t\toutput.stopReason = \"error\";\n\t\t\toutput.error = error instanceof Error ? error.message : String(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[], systemPrompt?: string): ChatCompletionMessageParam[] {\n\t\tconst params: ChatCompletionMessageParam[] = [];\n\n\t\t// Transform messages for cross-provider compatibility\n\t\tconst transformedMessages = transformMessages(messages, this.modelInfo);\n\n\t\t// Add system prompt if provided\n\t\tif (systemPrompt) {\n\t\t\t// Cerebras/xAi don't like the \"developer\" role\n\t\t\tconst useDeveloperRole =\n\t\t\t\tthis.modelInfo.reasoning &&\n\t\t\t\t!this.modelInfo.baseUrl?.includes(\"cerebras.ai\") &&\n\t\t\t\t!this.modelInfo.baseUrl?.includes(\"api.x.ai\");\n\t\t\tconst role = useDeveloperRole ? \"developer\" : \"system\";\n\t\t\tparams.push({ role: role, content: systemPrompt });\n\t\t}\n\n\t\t// Convert messages\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 OpenAI format\n\t\t\t\t\tconst content: ChatCompletionContentPart[] = msg.content.map((item): ChatCompletionContentPart => {\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} satisfies ChatCompletionContentPartText;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - OpenAI uses data URLs\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"image_url\",\n\t\t\t\t\t\t\t\timage_url: {\n\t\t\t\t\t\t\t\t\turl: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t} satisfies ChatCompletionContentPartImage;\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,\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 assistantMsg: ChatCompletionMessageParam = {\n\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\tcontent: null,\n\t\t\t\t};\n\n\t\t\t\t// Build content from blocks\n\t\t\t\tconst textBlocks = msg.content.filter((b) => b.type === \"text\") as TextContent[];\n\t\t\t\tif (textBlocks.length > 0) {\n\t\t\t\t\tassistantMsg.content = textBlocks.map((b) => b.text).join(\"\");\n\t\t\t\t}\n\n\t\t\t\t// Handle thinking blocks for llama.cpp server + gpt-oss\n\t\t\t\tconst thinkingBlocks = msg.content.filter((b) => b.type === \"thinking\") as ThinkingContent[];\n\t\t\t\tif (thinkingBlocks.length > 0) {\n\t\t\t\t\t// Use the signature from the first thinking block if available\n\t\t\t\t\tconst signature = thinkingBlocks[0].thinkingSignature;\n\t\t\t\t\tif (signature && signature.length > 0) {\n\t\t\t\t\t\t(assistantMsg as any)[signature] = thinkingBlocks.map((b) => b.thinking).join(\"\");\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Handle tool calls\n\t\t\t\tconst toolCalls = msg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\t\tif (toolCalls.length > 0) {\n\t\t\t\t\tassistantMsg.tool_calls = toolCalls.map((tc) => ({\n\t\t\t\t\t\tid: tc.id,\n\t\t\t\t\t\ttype: \"function\" as const,\n\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\tname: tc.name,\n\t\t\t\t\t\t\targuments: JSON.stringify(tc.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(assistantMsg);\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"tool\",\n\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\ttool_call_id: msg.toolCallId,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn params;\n\t}\n\n\tprivate convertTools(tools: Tool[]): OpenAI.Chat.Completions.ChatCompletionTool[] {\n\t\treturn tools.map((tool) => ({\n\t\t\ttype: \"function\",\n\t\t\tfunction: {\n\t\t\t\tname: tool.name,\n\t\t\t\tdescription: tool.description,\n\t\t\t\tparameters: tool.parameters,\n\t\t\t},\n\t\t}));\n\t}\n\n\tprivate mapStopReason(reason: ChatCompletionChunk.Choice[\"finish_reason\"] | null): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"stop\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"length\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"function_call\":\n\t\t\tcase \"tool_calls\":\n\t\t\t\treturn \"toolUse\";\n\t\t\tcase \"content_filter\":\n\t\t\t\treturn \"safety\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"openai-completions.js","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAQ5B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAc7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,Y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E,CAAC,SAAS,CAAC;yBACvC;qBACD,CAAC,CAAC,CAAC;gBACL,CAAC;gBAED,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;YAC3B,CAAC;iBAAM,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBACtC,MAAM,CAAC,IAAI,CAAC;oBACX,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE,GAAG,CAAC,OAAO;oBACpB,YAAY,EAAE,GAAG,CAAC,UAAU;iBAC5B,CAAC,CAAC;YACJ,CAAC;QACF,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,YAAY,CAAC,KAAa;QACjC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC3B,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACT,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,UAAU,EAAE,IAAI,CAAC,UAAU;aAC3B;SACD,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,MAA0D;QAC/E,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,MAAM;gBACV,OAAO,MAAM,CAAC;YACf,KAAK,QAAQ;gBACZ,OAAO,QAAQ,CAAC;YACjB,KAAK,eAAe,CAAC;YACrB,KAAK,YAAY;gBAChB,OAAO,SAAS,CAAC;YAClB,KAAK,gBAAgB;gBACpB,OAAO,QAAQ,CAAC;YACjB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tChatCompletionChunk,\n\tChatCompletionContentPart,\n\tChatCompletionContentPartImage,\n\tChatCompletionContentPartText,\n\tChatCompletionMessageParam,\n} from \"openai/resources/chat/completions.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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface OpenAICompletionsLLMOptions extends LLMOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"required\" | { type: \"function\"; function: { name: string } };\n\treasoningEffort?: \"low\" | \"medium\" | \"high\";\n}\n\nexport class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOptions> {\n\tprivate client: OpenAI;\n\tprivate modelInfo: Model;\n\n\tconstructor(model: Model, apiKey?: string) {\n\t\tif (!apiKey) {\n\t\t\tif (!process.env.OPENAI_API_KEY) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"OpenAI API key is required. Set OPENAI_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.OPENAI_API_KEY;\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true });\n\t\tthis.modelInfo = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.modelInfo;\n\t}\n\n\tgetApi(): string {\n\t\treturn \"openai-completions\";\n\t}\n\n\tasync generate(request: Context, options?: OpenAICompletionsLLMOptions): Promise<AssistantMessage> {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: this.getApi(),\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(request.messages, request.systemPrompt);\n\n\t\t\tconst params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tmessages,\n\t\t\t\tstream: true,\n\t\t\t\tstream_options: { include_usage: true },\n\t\t\t};\n\n\t\t\t// Cerebras/xAI dont like the \"store\" field\n\t\t\tif (!this.modelInfo.baseUrl?.includes(\"cerebras.ai\") && !this.modelInfo.baseUrl?.includes(\"api.x.ai\")) {\n\t\t\t\tparams.store = false;\n\t\t\t}\n\n\t\t\tif (options?.maxTokens) {\n\t\t\t\tparams.max_completion_tokens = options?.maxTokens;\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 (request.tools) {\n\t\t\t\tparams.tools = this.convertTools(request.tools);\n\t\t\t}\n\n\t\t\tif (options?.toolChoice) {\n\t\t\t\tparams.tool_choice = options.toolChoice;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\toptions?.reasoningEffort &&\n\t\t\t\tthis.modelInfo.reasoning &&\n\t\t\t\t!this.modelInfo.id.toLowerCase().includes(\"grok\")\n\t\t\t) {\n\t\t\t\tparams.reasoning_effort = options.reasoningEffort;\n\t\t\t}\n\n\t\t\tconst stream = await this.client.chat.completions.create(params, {\n\t\t\t\tsignal: options?.signal,\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: TextContent | ThinkingContent | (ToolCall & { partialArgs?: string }) | null = null;\n\t\t\tfor await (const chunk of stream) {\n\t\t\t\tif (chunk.usage) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput: chunk.usage.prompt_tokens || 0,\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usage.completion_tokens || 0) +\n\t\t\t\t\t\t\t(chunk.usage.completion_tokens_details?.reasoning_tokens || 0),\n\t\t\t\t\t\tcacheRead: chunk.usage.prompt_tokens_details?.cached_tokens || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(this.modelInfo, output.usage);\n\t\t\t\t}\n\n\t\t\t\tconst choice = chunk.choices[0];\n\t\t\t\tif (!choice) continue;\n\n\t\t\t\t// Capture finish reason\n\t\t\t\tif (choice.finish_reason) {\n\t\t\t\t\toutput.stopReason = this.mapStopReason(choice.finish_reason);\n\t\t\t\t}\n\n\t\t\t\tif (choice.delta) {\n\t\t\t\t\t// Handle text content\n\t\t\t\t\tif (\n\t\t\t\t\t\tchoice.delta.content !== null &&\n\t\t\t\t\t\tchoice.delta.content !== undefined &&\n\t\t\t\t\t\tchoice.delta.content.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to text block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"text\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new text block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to text block\n\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\tcurrentBlock.text += choice.delta.content;\n\t\t\t\t\t\t\toptions?.onEvent?.({\n\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\tdelta: choice.delta.content,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle reasoning_content field\n\t\t\t\t\tif (\n\t\t\t\t\t\t(choice.delta as any).reasoning_content !== null &&\n\t\t\t\t\t\t(choice.delta as any).reasoning_content !== undefined &&\n\t\t\t\t\t\t(choice.delta as any).reasoning_content.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to thinking block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"thinking\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new thinking block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: \"reasoning_content\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to thinking block\n\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tconst delta = (choice.delta as any).reasoning_content;\n\t\t\t\t\t\t\tcurrentBlock.thinking += delta;\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_delta\", content: currentBlock.thinking, delta });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle reasoning field\n\t\t\t\t\tif (\n\t\t\t\t\t\t(choice.delta as any).reasoning !== null &&\n\t\t\t\t\t\t(choice.delta as any).reasoning !== undefined &&\n\t\t\t\t\t\t(choice.delta as any).reasoning.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Check if we need to switch to thinking block\n\t\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"thinking\") {\n\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Start new thinking block\n\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: \"reasoning\" };\n\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Append to thinking block\n\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\tconst delta = (choice.delta as any).reasoning;\n\t\t\t\t\t\t\tcurrentBlock.thinking += delta;\n\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_delta\", content: currentBlock.thinking, delta });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle tool calls\n\t\t\t\t\tif (choice?.delta?.tool_calls) {\n\t\t\t\t\t\tfor (const toolCall of choice.delta.tool_calls) {\n\t\t\t\t\t\t\t// Check if we need a new tool call block\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\tcurrentBlock.type !== \"toolCall\" ||\n\t\t\t\t\t\t\t\t(toolCall.id && currentBlock.id !== toolCall.id)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t// Save current block if exists\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t\t\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\t\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t// Start new tool call block\n\t\t\t\t\t\t\t\tcurrentBlock = {\n\t\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\t\tid: toolCall.id || \"\",\n\t\t\t\t\t\t\t\t\tname: toolCall.function?.name || \"\",\n\t\t\t\t\t\t\t\t\targuments: {},\n\t\t\t\t\t\t\t\t\tpartialArgs: \"\",\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Accumulate tool call data\n\t\t\t\t\t\t\tif (currentBlock.type === \"toolCall\") {\n\t\t\t\t\t\t\t\tif (toolCall.id) currentBlock.id = toolCall.id;\n\t\t\t\t\t\t\t\tif (toolCall.function?.name) currentBlock.name = toolCall.function.name;\n\t\t\t\t\t\t\t\tif (toolCall.function?.arguments) {\n\t\t\t\t\t\t\t\t\tcurrentBlock.partialArgs += toolCall.function.arguments;\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}\n\t\t\t}\n\n\t\t\t// Save final block if exists\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: currentBlock.text });\n\t\t\t\t} else if (currentBlock.type === \"thinking\") {\n\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t} else if (currentBlock.type === \"toolCall\") {\n\t\t\t\t\tcurrentBlock.arguments = JSON.parse(currentBlock.partialArgs || \"{}\");\n\t\t\t\t\tdelete currentBlock.partialArgs;\n\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall: currentBlock as ToolCall });\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\toptions?.onEvent?.({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\treturn output;\n\t\t} catch (error) {\n\t\t\t// Update output with error information\n\t\t\toutput.stopReason = \"error\";\n\t\t\toutput.error = error instanceof Error ? error.message : String(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[], systemPrompt?: string): ChatCompletionMessageParam[] {\n\t\tconst params: ChatCompletionMessageParam[] = [];\n\n\t\t// Transform messages for cross-provider compatibility\n\t\tconst transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());\n\n\t\t// Add system prompt if provided\n\t\tif (systemPrompt) {\n\t\t\t// Cerebras/xAi don't like the \"developer\" role\n\t\t\tconst useDeveloperRole =\n\t\t\t\tthis.modelInfo.reasoning &&\n\t\t\t\t!this.modelInfo.baseUrl?.includes(\"cerebras.ai\") &&\n\t\t\t\t!this.modelInfo.baseUrl?.includes(\"api.x.ai\");\n\t\t\tconst role = useDeveloperRole ? \"developer\" : \"system\";\n\t\t\tparams.push({ role: role, content: systemPrompt });\n\t\t}\n\n\t\t// Convert messages\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 OpenAI format\n\t\t\t\t\tconst content: ChatCompletionContentPart[] = msg.content.map((item): ChatCompletionContentPart => {\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} satisfies ChatCompletionContentPartText;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - OpenAI uses data URLs\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"image_url\",\n\t\t\t\t\t\t\t\timage_url: {\n\t\t\t\t\t\t\t\t\turl: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t} satisfies ChatCompletionContentPartImage;\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tconst filteredContent = !this.modelInfo?.input.includes(\"image\")\n\t\t\t\t\t\t? content.filter((c) => c.type !== \"image_url\")\n\t\t\t\t\t\t: content;\n\t\t\t\t\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: filteredContent,\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 assistantMsg: ChatCompletionMessageParam = {\n\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\tcontent: null,\n\t\t\t\t};\n\n\t\t\t\t// Build content from blocks\n\t\t\t\tconst textBlocks = msg.content.filter((b) => b.type === \"text\") as TextContent[];\n\t\t\t\tif (textBlocks.length > 0) {\n\t\t\t\t\tassistantMsg.content = textBlocks.map((b) => b.text).join(\"\");\n\t\t\t\t}\n\n\t\t\t\t// Handle thinking blocks for llama.cpp server + gpt-oss\n\t\t\t\tconst thinkingBlocks = msg.content.filter((b) => b.type === \"thinking\") as ThinkingContent[];\n\t\t\t\tif (thinkingBlocks.length > 0) {\n\t\t\t\t\t// Use the signature from the first thinking block if available\n\t\t\t\t\tconst signature = thinkingBlocks[0].thinkingSignature;\n\t\t\t\t\tif (signature && signature.length > 0) {\n\t\t\t\t\t\t(assistantMsg as any)[signature] = thinkingBlocks.map((b) => b.thinking).join(\"\");\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Handle tool calls\n\t\t\t\tconst toolCalls = msg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\t\tif (toolCalls.length > 0) {\n\t\t\t\t\tassistantMsg.tool_calls = toolCalls.map((tc) => ({\n\t\t\t\t\t\tid: tc.id,\n\t\t\t\t\t\ttype: \"function\" as const,\n\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\tname: tc.name,\n\t\t\t\t\t\t\targuments: JSON.stringify(tc.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(assistantMsg);\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tparams.push({\n\t\t\t\t\trole: \"tool\",\n\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\ttool_call_id: msg.toolCallId,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn params;\n\t}\n\n\tprivate convertTools(tools: Tool[]): OpenAI.Chat.Completions.ChatCompletionTool[] {\n\t\treturn tools.map((tool) => ({\n\t\t\ttype: \"function\",\n\t\t\tfunction: {\n\t\t\t\tname: tool.name,\n\t\t\t\tdescription: tool.description,\n\t\t\t\tparameters: tool.parameters,\n\t\t\t},\n\t\t}));\n\t}\n\n\tprivate mapStopReason(reason: ChatCompletionChunk.Choice[\"finish_reason\"] | null): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"stop\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"length\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"function_call\":\n\t\t\tcase \"tool_calls\":\n\t\t\t\treturn \"toolUse\";\n\t\t\tcase \"content_filter\":\n\t\t\t\treturn \"safety\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
@@ -8,6 +8,7 @@ export declare class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions
|
|
|
8
8
|
private modelInfo;
|
|
9
9
|
constructor(model: Model, apiKey?: string);
|
|
10
10
|
getModel(): Model;
|
|
11
|
+
getApi(): string;
|
|
11
12
|
generate(request: Context, options?: OpenAIResponsesLLMOptions): Promise<AssistantMessage>;
|
|
12
13
|
private convertToInput;
|
|
13
14
|
private convertTools;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses.d.ts","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP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