@mariozechner/pi-ai 0.5.24 → 0.5.25
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/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 +9 -2
- 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,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAKL,MAAM,aAAa,CAAC;AAGrB,MAAM,W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AAC,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 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAA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G,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,gBAAgB,EAAE,yDAAyD;aAC3E,CAAC;YAEF,qEAAqE;YACrE,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,C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iB;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":"
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAWA,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":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,IAAI,CAAC;YACvC,KAAK,MAAM;gBACV,OAAO,yBAAyB,CAAC,IAAI,CAAC;YACvC,KAAK,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\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,GAGX,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;QACP,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED,MAAM;QACL,OAAO,sBAAsB,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,OAAgB,EAAE,OAA0B;QAC1D,MAAM,MAAM,GAAqB;YAChC,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,EAAE;YACX,GAAG,EAAE,IAAI,CAAC,MAAM,EAAE;YAClB,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ;YACjC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE;YACxB,KAAK,EAAE;gBACN,KAAK,EAAE,CAAC;gBACR,MAAM,EAAE,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C,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\tsetDefaultBaseUrls,\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
|
-
{"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,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\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,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,EAAE,CAAC,CAAC;QAC5F,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,QAAQ;QACP,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;I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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,GAAG,EACH,UAAU,EAEV,KAAK,EAKL,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,yBAA0B,SAAQ,UAAU;IAC5D,eAAe,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACxD,gBAAgB,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;CAC1D;AAED,qBAAa,kBAAmB,YAAW,GAAG,CAAC,yBAAyB,CAAC;IACxE,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-responses.d.ts","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAKL,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,yBAA0B,SAAQ,UAAU;IAC5D,eAAe,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACxD,gBAAgB,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;CAC1D;AAED,qBAAa,kBAAmB,YAAW,GAAG,CAAC,yBAAyB,CAAC;IACxE,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,yBAAyB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAyOhG,OAAO,CAAC,cAAc;IAkGtB,OAAO,CAAC,YAAY;IAUpB,OAAO,CAAC,aAAa;CAarB"}
|
|
@@ -17,10 +17,14 @@ export class OpenAIResponsesLLM {
|
|
|
17
17
|
getModel() {
|
|
18
18
|
return this.modelInfo;
|
|
19
19
|
}
|
|
20
|
+
getApi() {
|
|
21
|
+
return "openai-responses";
|
|
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: {
|
|
@@ -248,7 +252,7 @@ export class OpenAIResponsesLLM {
|
|
|
248
252
|
convertToInput(messages, systemPrompt) {
|
|
249
253
|
const input = [];
|
|
250
254
|
// Transform messages for cross-provider compatibility
|
|
251
|
-
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
255
|
+
const transformedMessages = transformMessages(messages, this.modelInfo, this.getApi());
|
|
252
256
|
// Add system prompt if provided
|
|
253
257
|
if (systemPrompt) {
|
|
254
258
|
const role = this.modelInfo?.reasoning ? "developer" : "system";
|
|
@@ -285,9 +289,12 @@ export class OpenAIResponsesLLM {
|
|
|
285
289
|
};
|
|
286
290
|
}
|
|
287
291
|
});
|
|
292
|
+
const filteredContent = !this.modelInfo?.input.includes("image")
|
|
293
|
+
? content.filter((c) => c.type !== "input_image")
|
|
294
|
+
: content;
|
|
288
295
|
input.push({
|
|
289
296
|
role: "user",
|
|
290
|
-
content,
|
|
297
|
+
content: filteredContent,
|
|
291
298
|
});
|
|
292
299
|
}
|
|
293
300
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAY5B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAa7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAO/C,MAAM,OAAO,kBAAkB;IACtB,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,EAAE,CAAC,CAAC;QAC5F,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,QAAQ;QACP,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,QAAQ,CA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ACL,CAAC;IAEO,aAAa,CAAC,MAA0B;QAC/C,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,WAAW;gBACf,OAAO,MAAM,CAAC;YACf,KAAK,YAAY;gBAChB,OAAO,QAAQ,CAAC;YACjB,KAAK,QAAQ,CAAC;YACd,KAAK,WAAW;gBACf,OAAO,OAAO,CAAC;YAChB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tTool as OpenAITool,\n\tResponseCreateParamsStreaming,\n\tResponseFunctionToolCall,\n\tResponseInput,\n\tResponseInputContent,\n\tResponseInputImage,\n\tResponseInputText,\n\tResponseOutputMessage,\n\tResponseReasoningItem,\n} from \"openai/resources/responses/responses.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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface OpenAIResponsesLLMOptions extends LLMOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n}\n\nexport class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {\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?: OpenAIResponsesLLMOptions): 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 input = this.convertToInput(request.messages, request.systemPrompt);\n\n\t\t\tconst params: ResponseCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tinput,\n\t\t\t\tstream: true,\n\t\t\t};\n\n\t\t\tif (options?.maxTokens) {\n\t\t\t\tparams.max_output_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\t// Add reasoning options for models that support it\n\t\t\tif (this.modelInfo?.reasoning && (options?.reasoningEffort || options?.reasoningSummary)) {\n\t\t\t\tparams.reasoning = {\n\t\t\t\t\teffort: options?.reasoningEffort || \"medium\",\n\t\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t\t};\n\t\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t\t}\n\n\t\t\tconst stream = await this.client.responses.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\tconst outputItems: (ResponseReasoningItem | ResponseOutputMessage | ResponseFunctionToolCall)[] = [];\n\t\t\tlet currentItem: ResponseReasoningItem | ResponseOutputMessage | ResponseFunctionToolCall | null = null;\n\n\t\t\tfor await (const event of stream) {\n\t\t\t\t// Handle output item start\n\t\t\t\tif (event.type === \"response.output_item.added\") {\n\t\t\t\t\tconst item = event.item;\n\t\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t\tcurrentItem = item;\n\t\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t\tcurrentItem = item;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle reasoning summary deltas\n\t\t\t\telse if (event.type === \"response.reasoning_summary_part.added\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tcurrentItem.summary.push(event.part);\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.reasoning_summary_text.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tconst lastPart = currentItem.summary[currentItem.summary.length - 1];\n\t\t\t\t\t\tif (lastPart) {\n\t\t\t\t\t\t\tlastPart.text += event.delta;\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: currentItem.summary.map((s) => s.text).join(\"\\n\\n\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\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\t// Add a new line between summary parts (hack...)\n\t\t\t\telse if (event.type === \"response.reasoning_summary_part.done\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tconst lastPart = currentItem.summary[currentItem.summary.length - 1];\n\t\t\t\t\t\tif (lastPart) {\n\t\t\t\t\t\t\tlastPart.text += \"\\n\\n\";\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: currentItem.summary.map((s) => s.text).join(\"\\n\\n\"),\n\t\t\t\t\t\t\t\tdelta: \"\\n\\n\",\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\t// Handle text output deltas\n\t\t\t\telse if (event.type === \"response.content_part.added\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tcurrentItem.content = currentItem.content || [];\n\t\t\t\t\t\tcurrentItem.content.push(event.part);\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.output_text.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tconst lastPart = currentItem.content[currentItem.content.length - 1];\n\t\t\t\t\t\tif (lastPart && lastPart.type === \"output_text\") {\n\t\t\t\t\t\t\tlastPart.text += event.delta;\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: currentItem.content\n\t\t\t\t\t\t\t\t\t.map((c) => (c.type === \"output_text\" ? c.text : c.refusal))\n\t\t\t\t\t\t\t\t\t.join(\"\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.refusal.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tconst lastPart = currentItem.content[currentItem.content.length - 1];\n\t\t\t\t\t\tif (lastPart && lastPart.type === \"refusal\") {\n\t\t\t\t\t\t\tlastPart.refusal += event.delta;\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: currentItem.content\n\t\t\t\t\t\t\t\t\t.map((c) => (c.type === \"output_text\" ? c.text : c.refusal))\n\t\t\t\t\t\t\t\t\t.join(\"\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\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\t// Handle output item completion\n\t\t\t\telse if (event.type === \"response.output_item.done\") {\n\t\t\t\t\tconst item = event.item;\n\n\t\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\t\toutputItems[outputItems.length - 1] = item; // Update with final item\n\t\t\t\t\t\tconst thinkingContent = item.summary?.map((s) => s.text).join(\"\\n\\n\") || \"\";\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: thinkingContent });\n\t\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\t\toutputItems[outputItems.length - 1] = item; // Update with final item\n\t\t\t\t\t\tconst textContent = item.content.map((c) => (c.type === \"output_text\" ? c.text : c.refusal)).join(\"\");\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: textContent });\n\t\t\t\t\t} else if (item.type === \"function_call\") {\n\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\tid: item.call_id + \"|\" + item.id,\n\t\t\t\t\t\t\tname: item.name,\n\t\t\t\t\t\t\targuments: JSON.parse(item.arguments),\n\t\t\t\t\t\t};\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle completion\n\t\t\t\telse if (event.type === \"response.completed\") {\n\t\t\t\t\tconst response = event.response;\n\t\t\t\t\tif (response?.usage) {\n\t\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\t\tinput: response.usage.input_tokens || 0,\n\t\t\t\t\t\t\toutput: response.usage.output_tokens || 0,\n\t\t\t\t\t\t\tcacheRead: response.usage.input_tokens_details?.cached_tokens || 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 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\t// Map status to stop reason\n\t\t\t\t\toutput.stopReason = this.mapStopReason(response?.status);\n\t\t\t\t\tif (outputItems.some((b) => b.type === \"function_call\") && output.stopReason === \"stop\") {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle errors\n\t\t\t\telse if (event.type === \"error\") {\n\t\t\t\t\toutput.stopReason = \"error\";\n\t\t\t\t\toutput.error = `Code ${event.code}: ${event.message}` || \"Unknown error\";\n\t\t\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\t\t\treturn output;\n\t\t\t\t} else if (event.type === \"response.failed\") {\n\t\t\t\t\toutput.stopReason = \"error\";\n\t\t\t\t\toutput.error = \"Unknown error\";\n\t\t\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\t\t\treturn output;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Convert output items to blocks\n\t\t\tfor (const item of outputItems) {\n\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\tthinking: item.summary?.map((s: any) => s.text).join(\"\\n\\n\") || \"\",\n\t\t\t\t\t\tthinkingSignature: JSON.stringify(item), // Full item for resubmission\n\t\t\t\t\t});\n\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: item.content.map((c) => (c.type === \"output_text\" ? c.text : c.refusal)).join(\"\"),\n\t\t\t\t\t\ttextSignature: item.id, // ID for resubmission\n\t\t\t\t\t});\n\t\t\t\t} else if (item.type === \"function_call\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\tid: item.call_id + \"|\" + item.id,\n\t\t\t\t\t\tname: item.name,\n\t\t\t\t\t\targuments: JSON.parse(item.arguments),\n\t\t\t\t\t});\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\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 convertToInput(messages: Message[], systemPrompt?: string): ResponseInput {\n\t\tconst input: ResponseInput = [];\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\tconst role = this.modelInfo?.reasoning ? \"developer\" : \"system\";\n\t\t\tinput.push({\n\t\t\t\trole,\n\t\t\t\tcontent: systemPrompt,\n\t\t\t});\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\tinput.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"input_text\", 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 OpenAI Responses format\n\t\t\t\t\tconst content: ResponseInputContent[] = msg.content.map((item): ResponseInputContent => {\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: \"input_text\",\n\t\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t\t} satisfies ResponseInputText;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - OpenAI Responses uses data URLs\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"input_image\",\n\t\t\t\t\t\t\t\tdetail: \"auto\",\n\t\t\t\t\t\t\t\timage_url: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t\t\t\t\t} satisfies ResponseInputImage;\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tinput.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\t// Process content blocks in order\n\t\t\t\tconst output: ResponseInput = [];\n\n\t\t\t\tfor (const block of msg.content) {\n\t\t\t\t\t// Do not submit thinking blocks if the completion had an error (i.e. abort)\n\t\t\t\t\tif (block.type === \"thinking\" && msg.stopReason !== \"error\") {\n\t\t\t\t\t\t// Push the full reasoning item(s) from signature\n\t\t\t\t\t\tif (block.thinkingSignature) {\n\t\t\t\t\t\t\tconst reasoningItem = JSON.parse(block.thinkingSignature);\n\t\t\t\t\t\t\toutput.push(reasoningItem);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (block.type === \"text\") {\n\t\t\t\t\t\tconst textBlock = block as TextContent;\n\t\t\t\t\t\toutput.push({\n\t\t\t\t\t\t\ttype: \"message\",\n\t\t\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\t\t\tcontent: [{ type: \"output_text\", text: textBlock.text, annotations: [] }],\n\t\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\t\tid: textBlock.textSignature || \"msg_\" + Math.random().toString(36).substring(2, 15),\n\t\t\t\t\t\t} satisfies ResponseOutputMessage);\n\t\t\t\t\t\t// Do not submit thinking blocks if the completion had an error (i.e. abort)\n\t\t\t\t\t} else if (block.type === \"toolCall\" && msg.stopReason !== \"error\") {\n\t\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\t\toutput.push({\n\t\t\t\t\t\t\ttype: \"function_call\",\n\t\t\t\t\t\t\tid: toolCall.id.split(\"|\")[1], // Extract original ID\n\t\t\t\t\t\t\tcall_id: toolCall.id.split(\"|\")[0], // Extract call session ID\n\t\t\t\t\t\t\tname: toolCall.name,\n\t\t\t\t\t\t\targuments: JSON.stringify(toolCall.arguments),\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Add all output items to input\n\t\t\t\tinput.push(...output);\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\t// Tool results are sent as function_call_output\n\t\t\t\tinput.push({\n\t\t\t\t\ttype: \"function_call_output\",\n\t\t\t\t\tcall_id: msg.toolCallId.split(\"|\")[0], // Extract call session ID\n\t\t\t\t\toutput: msg.content,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn input;\n\t}\n\n\tprivate convertTools(tools: Tool[]): OpenAITool[] {\n\t\treturn tools.map((tool) => ({\n\t\t\ttype: \"function\",\n\t\t\tname: tool.name,\n\t\t\tdescription: tool.description,\n\t\t\tparameters: tool.parameters,\n\t\t\tstrict: null,\n\t\t}));\n\t}\n\n\tprivate mapStopReason(status: string | undefined): StopReason {\n\t\tswitch (status) {\n\t\t\tcase \"completed\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"incomplete\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"failed\":\n\t\t\tcase \"cancelled\":\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":"openai-responses.js","sourceRoot":"","sources":["../../src/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AAM,CAAC;YACf,KAAK,YAAY;gBAChB,OAAO,QAAQ,CAAC;YACjB,KAAK,QAAQ,CAAC;YACd,KAAK,WAAW;gBACf,OAAO,OAAO,CAAC;YAChB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tTool as OpenAITool,\n\tResponseCreateParamsStreaming,\n\tResponseFunctionToolCall,\n\tResponseInput,\n\tResponseInputContent,\n\tResponseInputImage,\n\tResponseInputText,\n\tResponseOutputMessage,\n\tResponseReasoningItem,\n} from \"openai/resources/responses/responses.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\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { transformMessages } from \"./utils.js\";\n\nexport interface OpenAIResponsesLLMOptions extends LLMOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n}\n\nexport class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {\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-responses\";\n\t}\n\n\tasync generate(request: Context, options?: OpenAIResponsesLLMOptions): 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 input = this.convertToInput(request.messages, request.systemPrompt);\n\n\t\t\tconst params: ResponseCreateParamsStreaming = {\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tinput,\n\t\t\t\tstream: true,\n\t\t\t};\n\n\t\t\tif (options?.maxTokens) {\n\t\t\t\tparams.max_output_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\t// Add reasoning options for models that support it\n\t\t\tif (this.modelInfo?.reasoning && (options?.reasoningEffort || options?.reasoningSummary)) {\n\t\t\t\tparams.reasoning = {\n\t\t\t\t\teffort: options?.reasoningEffort || \"medium\",\n\t\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t\t};\n\t\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t\t}\n\n\t\t\tconst stream = await this.client.responses.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\tconst outputItems: (ResponseReasoningItem | ResponseOutputMessage | ResponseFunctionToolCall)[] = [];\n\t\t\tlet currentItem: ResponseReasoningItem | ResponseOutputMessage | ResponseFunctionToolCall | null = null;\n\n\t\t\tfor await (const event of stream) {\n\t\t\t\t// Handle output item start\n\t\t\t\tif (event.type === \"response.output_item.added\") {\n\t\t\t\t\tconst item = event.item;\n\t\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_start\" });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t\tcurrentItem = item;\n\t\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_start\" });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t\tcurrentItem = item;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle reasoning summary deltas\n\t\t\t\telse if (event.type === \"response.reasoning_summary_part.added\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tcurrentItem.summary.push(event.part);\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.reasoning_summary_text.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tconst lastPart = currentItem.summary[currentItem.summary.length - 1];\n\t\t\t\t\t\tif (lastPart) {\n\t\t\t\t\t\t\tlastPart.text += event.delta;\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: currentItem.summary.map((s) => s.text).join(\"\\n\\n\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\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\t// Add a new line between summary parts (hack...)\n\t\t\t\telse if (event.type === \"response.reasoning_summary_part.done\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"reasoning\") {\n\t\t\t\t\t\tcurrentItem.summary = currentItem.summary || [];\n\t\t\t\t\t\tconst lastPart = currentItem.summary[currentItem.summary.length - 1];\n\t\t\t\t\t\tif (lastPart) {\n\t\t\t\t\t\t\tlastPart.text += \"\\n\\n\";\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: currentItem.summary.map((s) => s.text).join(\"\\n\\n\"),\n\t\t\t\t\t\t\t\tdelta: \"\\n\\n\",\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\t// Handle text output deltas\n\t\t\t\telse if (event.type === \"response.content_part.added\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tcurrentItem.content = currentItem.content || [];\n\t\t\t\t\t\tcurrentItem.content.push(event.part);\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.output_text.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tconst lastPart = currentItem.content[currentItem.content.length - 1];\n\t\t\t\t\t\tif (lastPart && lastPart.type === \"output_text\") {\n\t\t\t\t\t\t\tlastPart.text += event.delta;\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: currentItem.content\n\t\t\t\t\t\t\t\t\t.map((c) => (c.type === \"output_text\" ? c.text : c.refusal))\n\t\t\t\t\t\t\t\t\t.join(\"\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (event.type === \"response.refusal.delta\") {\n\t\t\t\t\tif (currentItem && currentItem.type === \"message\") {\n\t\t\t\t\t\tconst lastPart = currentItem.content[currentItem.content.length - 1];\n\t\t\t\t\t\tif (lastPart && lastPart.type === \"refusal\") {\n\t\t\t\t\t\t\tlastPart.refusal += event.delta;\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: currentItem.content\n\t\t\t\t\t\t\t\t\t.map((c) => (c.type === \"output_text\" ? c.text : c.refusal))\n\t\t\t\t\t\t\t\t\t.join(\"\"),\n\t\t\t\t\t\t\t\tdelta: event.delta,\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\t// Handle output item completion\n\t\t\t\telse if (event.type === \"response.output_item.done\") {\n\t\t\t\t\tconst item = event.item;\n\n\t\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\t\toutputItems[outputItems.length - 1] = item; // Update with final item\n\t\t\t\t\t\tconst thinkingContent = item.summary?.map((s) => s.text).join(\"\\n\\n\") || \"\";\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: thinkingContent });\n\t\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\t\toutputItems[outputItems.length - 1] = item; // Update with final item\n\t\t\t\t\t\tconst textContent = item.content.map((c) => (c.type === \"output_text\" ? c.text : c.refusal)).join(\"\");\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"text_end\", content: textContent });\n\t\t\t\t\t} else if (item.type === \"function_call\") {\n\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\tid: item.call_id + \"|\" + item.id,\n\t\t\t\t\t\t\tname: item.name,\n\t\t\t\t\t\t\targuments: JSON.parse(item.arguments),\n\t\t\t\t\t\t};\n\t\t\t\t\t\toptions?.onEvent?.({ type: \"toolCall\", toolCall });\n\t\t\t\t\t\toutputItems.push(item);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle completion\n\t\t\t\telse if (event.type === \"response.completed\") {\n\t\t\t\t\tconst response = event.response;\n\t\t\t\t\tif (response?.usage) {\n\t\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\t\tinput: response.usage.input_tokens || 0,\n\t\t\t\t\t\t\toutput: response.usage.output_tokens || 0,\n\t\t\t\t\t\t\tcacheRead: response.usage.input_tokens_details?.cached_tokens || 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 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\t// Map status to stop reason\n\t\t\t\t\toutput.stopReason = this.mapStopReason(response?.status);\n\t\t\t\t\tif (outputItems.some((b) => b.type === \"function_call\") && output.stopReason === \"stop\") {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// Handle errors\n\t\t\t\telse if (event.type === \"error\") {\n\t\t\t\t\toutput.stopReason = \"error\";\n\t\t\t\t\toutput.error = `Code ${event.code}: ${event.message}` || \"Unknown error\";\n\t\t\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\t\t\treturn output;\n\t\t\t\t} else if (event.type === \"response.failed\") {\n\t\t\t\t\toutput.stopReason = \"error\";\n\t\t\t\t\toutput.error = \"Unknown error\";\n\t\t\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\t\t\treturn output;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Convert output items to blocks\n\t\t\tfor (const item of outputItems) {\n\t\t\t\tif (item.type === \"reasoning\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\tthinking: item.summary?.map((s: any) => s.text).join(\"\\n\\n\") || \"\",\n\t\t\t\t\t\tthinkingSignature: JSON.stringify(item), // Full item for resubmission\n\t\t\t\t\t});\n\t\t\t\t} else if (item.type === \"message\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: item.content.map((c) => (c.type === \"output_text\" ? c.text : c.refusal)).join(\"\"),\n\t\t\t\t\t\ttextSignature: item.id, // ID for resubmission\n\t\t\t\t\t});\n\t\t\t\t} else if (item.type === \"function_call\") {\n\t\t\t\t\toutput.content.push({\n\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\tid: item.call_id + \"|\" + item.id,\n\t\t\t\t\t\tname: item.name,\n\t\t\t\t\t\targuments: JSON.parse(item.arguments),\n\t\t\t\t\t});\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\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 convertToInput(messages: Message[], systemPrompt?: string): ResponseInput {\n\t\tconst input: ResponseInput = [];\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\tconst role = this.modelInfo?.reasoning ? \"developer\" : \"system\";\n\t\t\tinput.push({\n\t\t\t\trole,\n\t\t\t\tcontent: systemPrompt,\n\t\t\t});\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\tinput.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"input_text\", 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 OpenAI Responses format\n\t\t\t\t\tconst content: ResponseInputContent[] = msg.content.map((item): ResponseInputContent => {\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: \"input_text\",\n\t\t\t\t\t\t\t\ttext: item.text,\n\t\t\t\t\t\t\t} satisfies ResponseInputText;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - OpenAI Responses uses data URLs\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\ttype: \"input_image\",\n\t\t\t\t\t\t\t\tdetail: \"auto\",\n\t\t\t\t\t\t\t\timage_url: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t\t\t\t\t} satisfies ResponseInputImage;\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 !== \"input_image\")\n\t\t\t\t\t\t: content;\n\t\t\t\t\tinput.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\t// Process content blocks in order\n\t\t\t\tconst output: ResponseInput = [];\n\n\t\t\t\tfor (const block of msg.content) {\n\t\t\t\t\t// Do not submit thinking blocks if the completion had an error (i.e. abort)\n\t\t\t\t\tif (block.type === \"thinking\" && msg.stopReason !== \"error\") {\n\t\t\t\t\t\t// Push the full reasoning item(s) from signature\n\t\t\t\t\t\tif (block.thinkingSignature) {\n\t\t\t\t\t\t\tconst reasoningItem = JSON.parse(block.thinkingSignature);\n\t\t\t\t\t\t\toutput.push(reasoningItem);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (block.type === \"text\") {\n\t\t\t\t\t\tconst textBlock = block as TextContent;\n\t\t\t\t\t\toutput.push({\n\t\t\t\t\t\t\ttype: \"message\",\n\t\t\t\t\t\t\trole: \"assistant\",\n\t\t\t\t\t\t\tcontent: [{ type: \"output_text\", text: textBlock.text, annotations: [] }],\n\t\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\t\tid: textBlock.textSignature || \"msg_\" + Math.random().toString(36).substring(2, 15),\n\t\t\t\t\t\t} satisfies ResponseOutputMessage);\n\t\t\t\t\t\t// Do not submit thinking blocks if the completion had an error (i.e. abort)\n\t\t\t\t\t} else if (block.type === \"toolCall\" && msg.stopReason !== \"error\") {\n\t\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\t\toutput.push({\n\t\t\t\t\t\t\ttype: \"function_call\",\n\t\t\t\t\t\t\tid: toolCall.id.split(\"|\")[1], // Extract original ID\n\t\t\t\t\t\t\tcall_id: toolCall.id.split(\"|\")[0], // Extract call session ID\n\t\t\t\t\t\t\tname: toolCall.name,\n\t\t\t\t\t\t\targuments: JSON.stringify(toolCall.arguments),\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Add all output items to input\n\t\t\t\tinput.push(...output);\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\t// Tool results are sent as function_call_output\n\t\t\t\tinput.push({\n\t\t\t\t\ttype: \"function_call_output\",\n\t\t\t\t\tcall_id: msg.toolCallId.split(\"|\")[0], // Extract call session ID\n\t\t\t\t\toutput: msg.content,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn input;\n\t}\n\n\tprivate convertTools(tools: Tool[]): OpenAITool[] {\n\t\treturn tools.map((tool) => ({\n\t\t\ttype: \"function\",\n\t\t\tname: tool.name,\n\t\t\tdescription: tool.description,\n\t\t\tparameters: tool.parameters,\n\t\t\tstrict: null,\n\t\t}));\n\t}\n\n\tprivate mapStopReason(status: string | undefined): StopReason {\n\t\tswitch (status) {\n\t\t\tcase \"completed\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"incomplete\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"failed\":\n\t\t\tcase \"cancelled\":\n\t\t\t\treturn \"error\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
@@ -11,5 +11,5 @@ import type { Message, Model } from "../types.js";
|
|
|
11
11
|
* @param model The target model that will process these messages
|
|
12
12
|
* @returns A copy of the messages array with transformations applied
|
|
13
13
|
*/
|
|
14
|
-
export declare function transformMessages(messages: Message[], model: Model): Message[];
|
|
14
|
+
export declare function transformMessages(messages: Message[], model: Model, api: string): Message[];
|
|
15
15
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpE;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpE;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,EAAE,CAuC3F"}
|
package/dist/providers/utils.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @param model The target model that will process these messages
|
|
11
11
|
* @returns A copy of the messages array with transformations applied
|
|
12
12
|
*/
|
|
13
|
-
export function transformMessages(messages, model) {
|
|
13
|
+
export function transformMessages(messages, model, api) {
|
|
14
14
|
return messages.map((msg) => {
|
|
15
15
|
// User and toolResult messages pass through unchanged
|
|
16
16
|
if (msg.role === "user" || msg.role === "toolResult") {
|
|
@@ -19,8 +19,8 @@ export function transformMessages(messages, model) {
|
|
|
19
19
|
// Assistant messages need transformation check
|
|
20
20
|
if (msg.role === "assistant") {
|
|
21
21
|
const assistantMsg = msg;
|
|
22
|
-
// If message is from the same provider and
|
|
23
|
-
if (assistantMsg.provider === model.provider && assistantMsg.
|
|
22
|
+
// If message is from the same provider and API, keep as is
|
|
23
|
+
if (assistantMsg.provider === model.provider && assistantMsg.api === api) {
|
|
24
24
|
return msg;
|
|
25
25
|
}
|
|
26
26
|
// Transform message from different provider/model
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAAmB,EAAE,KAAY;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAAmB,EAAE,KAAY,EAAE,GAAW;IAC/E,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QAC3B,sDAAsD;QACtD,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YACtD,OAAO,GAAG,CAAC;QACZ,CAAC;QAED,+CAA+C;QAC/C,IAAI,GAAG,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YAC9B,MAAM,YAAY,GAAG,GAAuB,CAAC;YAE7C,2DAA2D;YAC3D,IAAI,YAAY,CAAC,QAAQ,KAAK,KAAK,CAAC,QAAQ,IAAI,YAAY,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;gBAC1E,OAAO,GAAG,CAAC;YACZ,CAAC;YAED,kDAAkD;YAClD,MAAM,kBAAkB,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;gBAC7D,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;oBAC/B,4DAA4D;oBAC5D,OAAO;wBACN,IAAI,EAAE,MAAe;wBACrB,IAAI,EAAE,eAAe,KAAK,CAAC,QAAQ,eAAe;qBAClD,CAAC;gBACH,CAAC;gBACD,2DAA2D;gBAC3D,OAAO,KAAK,CAAC;YACd,CAAC,CAAC,CAAC;YAEH,uCAAuC;YACvC,OAAO;gBACN,GAAG,YAAY;gBACf,OAAO,EAAE,kBAAkB;aAC3B,CAAC;QACH,CAAC;QAED,qDAAqD;QACrD,OAAO,GAAG,CAAC;IACZ,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { AssistantMessage, Message, Model } from \"../types.js\";\n\n/**\n * Transform messages for cross-provider compatibility.\n *\n * - User and toolResult messages are copied verbatim\n * - Assistant messages:\n * - If from the same provider/model, copied as-is\n * - If from different provider/model, thinking blocks are converted to text blocks with <thinking></thinking> tags\n *\n * @param messages The messages to transform\n * @param model The target model that will process these messages\n * @returns A copy of the messages array with transformations applied\n */\nexport function transformMessages(messages: Message[], model: Model, api: string): Message[] {\n\treturn messages.map((msg) => {\n\t\t// User and toolResult messages pass through unchanged\n\t\tif (msg.role === \"user\" || msg.role === \"toolResult\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\n\t\t\t// If message is from the same provider and API, keep as is\n\t\t\tif (assistantMsg.provider === model.provider && assistantMsg.api === api) {\n\t\t\t\treturn msg;\n\t\t\t}\n\n\t\t\t// Transform message from different provider/model\n\t\t\tconst transformedContent = assistantMsg.content.map((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Convert thinking block to text block with <thinking> tags\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: `<thinking>\\n${block.thinking}\\n</thinking>`,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\t// All other blocks (text, toolCall) pass through unchanged\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\t// Return transformed assistant message\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\n\t\t// Should not reach here, but return as-is for safety\n\t\treturn msg;\n\t});\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface LLMOptions {
|
|
|
7
7
|
export interface LLM<T extends LLMOptions> {
|
|
8
8
|
generate(request: Context, options?: T): Promise<AssistantMessage>;
|
|
9
9
|
getModel(): Model;
|
|
10
|
+
getApi(): string;
|
|
10
11
|
}
|
|
11
12
|
export interface TextContent {
|
|
12
13
|
type: "text";
|
|
@@ -50,6 +51,7 @@ export interface UserMessage {
|
|
|
50
51
|
export interface AssistantMessage {
|
|
51
52
|
role: "assistant";
|
|
52
53
|
content: (TextContent | ThinkingContent | ToolCall)[];
|
|
54
|
+
api: string;
|
|
53
55
|
provider: string;
|
|
54
56
|
model: string;
|
|
55
57
|
usage: Usage;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,IAAI,CAAC;IACjD,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,GAAG,CAAC,CAAC,SAAS,UAAU;IACxC,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACnE,QAAQ,IAAI,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"m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
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export interface LLMOptions {\n\ttemperature?: number;\n\tmaxTokens?: number;\n\tonEvent?: (event: AssistantMessageEvent) => void;\n\tsignal?: AbortSignal;\n}\n\nexport interface LLM<T extends LLMOptions> {\n\tgenerate(request: Context, options?: T): Promise<AssistantMessage>;\n\tgetModel(): Model;\n}\n\nexport interface TextContent {\n\ttype: \"text\";\n\ttext: string;\n\ttextSignature?: string; // e.g., for OpenAI responses, the message ID\n}\n\nexport interface ThinkingContent {\n\ttype: \"thinking\";\n\tthinking: string;\n\tthinkingSignature?: string; // e.g., for OpenAI responses, the reasoning item ID\n}\n\nexport interface ImageContent {\n\ttype: \"image\";\n\tdata: string; // base64 encoded image data\n\tmimeType: string; // e.g., \"image/jpeg\", \"image/png\"\n}\n\nexport interface ToolCall {\n\ttype: \"toolCall\";\n\tid: string;\n\tname: string;\n\targuments: Record<string, any>;\n}\n\nexport interface Usage {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n}\n\nexport type StopReason = \"stop\" | \"length\" | \"toolUse\" | \"safety\" | \"error\";\n\nexport interface UserMessage {\n\trole: \"user\";\n\tcontent: string | (TextContent | ImageContent)[];\n}\n\nexport interface AssistantMessage {\n\trole: \"assistant\";\n\tcontent: (TextContent | ThinkingContent | ToolCall)[];\n\tprovider: string;\n\tmodel: string;\n\tusage: Usage;\n\n\tstopReason: StopReason;\n\terror?: string | Error;\n}\n\nexport interface ToolResultMessage {\n\trole: \"toolResult\";\n\ttoolCallId: string;\n\ttoolName: string;\n\tcontent: string;\n\tisError: boolean;\n}\n\nexport type Message = UserMessage | AssistantMessage | ToolResultMessage;\n\nexport interface Tool {\n\tname: string;\n\tdescription: string;\n\tparameters: Record<string, any>; // JSON Schema\n}\n\nexport interface Context {\n\tsystemPrompt?: string;\n\tmessages: Message[];\n\ttools?: Tool[];\n}\n\nexport type AssistantMessageEvent =\n\t| { type: \"start\"; model: string; provider: string }\n\t| { type: \"text_start\" }\n\t| { type: \"text_delta\"; content: string; delta: string }\n\t| { type: \"text_end\"; content: string }\n\t| { type: \"thinking_start\" }\n\t| { type: \"thinking_delta\"; content: string; delta: string }\n\t| { type: \"thinking_end\"; content: string }\n\t| { type: \"toolCall\"; toolCall: ToolCall }\n\t| { type: \"done\"; reason: StopReason; message: AssistantMessage }\n\t| { type: \"error\"; error: string };\n\n// Model interface for the unified model system\nexport interface Model {\n\tid: string;\n\tname: string;\n\tprovider: string;\n\tbaseUrl?: string;\n\treasoning: boolean;\n\tinput: (\"text\" | \"image\")[];\n\tcost: {\n\t\tinput: number; // $/million tokens\n\t\toutput: number; // $/million tokens\n\t\tcacheRead: number; // $/million tokens\n\t\tcacheWrite: number; // $/million tokens\n\t};\n\tcontextWindow: number;\n\tmaxTokens: number;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export interface LLMOptions {\n\ttemperature?: number;\n\tmaxTokens?: number;\n\tonEvent?: (event: AssistantMessageEvent) => void;\n\tsignal?: AbortSignal;\n}\n\nexport interface LLM<T extends LLMOptions> {\n\tgenerate(request: Context, options?: T): Promise<AssistantMessage>;\n\tgetModel(): Model;\n\tgetApi(): string;\n}\n\nexport interface TextContent {\n\ttype: \"text\";\n\ttext: string;\n\ttextSignature?: string; // e.g., for OpenAI responses, the message ID\n}\n\nexport interface ThinkingContent {\n\ttype: \"thinking\";\n\tthinking: string;\n\tthinkingSignature?: string; // e.g., for OpenAI responses, the reasoning item ID\n}\n\nexport interface ImageContent {\n\ttype: \"image\";\n\tdata: string; // base64 encoded image data\n\tmimeType: string; // e.g., \"image/jpeg\", \"image/png\"\n}\n\nexport interface ToolCall {\n\ttype: \"toolCall\";\n\tid: string;\n\tname: string;\n\targuments: Record<string, any>;\n}\n\nexport interface Usage {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n}\n\nexport type StopReason = \"stop\" | \"length\" | \"toolUse\" | \"safety\" | \"error\";\n\nexport interface UserMessage {\n\trole: \"user\";\n\tcontent: string | (TextContent | ImageContent)[];\n}\n\nexport interface AssistantMessage {\n\trole: \"assistant\";\n\tcontent: (TextContent | ThinkingContent | ToolCall)[];\n\tapi: string;\n\tprovider: string;\n\tmodel: string;\n\tusage: Usage;\n\n\tstopReason: StopReason;\n\terror?: string | Error;\n}\n\nexport interface ToolResultMessage {\n\trole: \"toolResult\";\n\ttoolCallId: string;\n\ttoolName: string;\n\tcontent: string;\n\tisError: boolean;\n}\n\nexport type Message = UserMessage | AssistantMessage | ToolResultMessage;\n\nexport interface Tool {\n\tname: string;\n\tdescription: string;\n\tparameters: Record<string, any>; // JSON Schema\n}\n\nexport interface Context {\n\tsystemPrompt?: string;\n\tmessages: Message[];\n\ttools?: Tool[];\n}\n\nexport type AssistantMessageEvent =\n\t| { type: \"start\"; model: string; provider: string }\n\t| { type: \"text_start\" }\n\t| { type: \"text_delta\"; content: string; delta: string }\n\t| { type: \"text_end\"; content: string }\n\t| { type: \"thinking_start\" }\n\t| { type: \"thinking_delta\"; content: string; delta: string }\n\t| { type: \"thinking_end\"; content: string }\n\t| { type: \"toolCall\"; toolCall: ToolCall }\n\t| { type: \"done\"; reason: StopReason; message: AssistantMessage }\n\t| { type: \"error\"; error: string };\n\n// Model interface for the unified model system\nexport interface Model {\n\tid: string;\n\tname: string;\n\tprovider: string;\n\tbaseUrl?: string;\n\treasoning: boolean;\n\tinput: (\"text\" | \"image\")[];\n\tcost: {\n\t\tinput: number; // $/million tokens\n\t\toutput: number; // $/million tokens\n\t\tcacheRead: number; // $/million tokens\n\t\tcacheWrite: number; // $/million tokens\n\t};\n\tcontextWindow: number;\n\tmaxTokens: number;\n}\n"]}
|