@mariozechner/pi-ai 0.5.18 → 0.5.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -9
- package/dist/models.generated.d.ts +14 -14
- package/dist/models.generated.js +66 -66
- package/dist/models.generated.js.map +1 -1
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +86 -102
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts +2 -2
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +36 -54
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +1 -1
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +36 -47
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses.d.ts +1 -1
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +116 -89
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/utils.d.ts +15 -0
- package/dist/providers/utils.d.ts.map +1 -0
- package/dist/providers/utils.js +48 -0
- package/dist/providers/utils.js.map +1 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,yBAAyB,EAGzB,WAAW,GAEX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAwB7C,MAAM,OAAO,SAAS;IACb,MAAM,CAAc;IACpB,KAAK,CAAQ;IAErB,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,g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c;QACnC,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,MAAM;gBACV,OAAO,yBAAyB,CAAC,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\tUsage,\n} from \"../types.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 model: 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.model = model;\n\t}\n\n\tgetModel(): Model {\n\t\treturn this.model;\n\t}\n\n\tasync complete(context: Context, options?: GoogleLLMOptions): Promise<AssistantMessage> {\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.model.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.model.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.model.id, provider: this.model.provider });\n\n\t\t\tconst blocks: AssistantMessage[\"content\"] = [];\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tlet usage: Usage = {\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\tlet stopReason: StopReason = \"stop\";\n\n\t\t\t// Process the stream\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\t// Save and finalize current block\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\tblocks.push(currentBlock);\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}\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\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 {\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\tblocks.push(currentBlock);\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\tblocks.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\tstopReason = this.mapStopReason(candidate.finishReason);\n\t\t\t\t\t// Check if we have tool calls in blocks\n\t\t\t\t\tif (blocks.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\tstopReason = \"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\tusage = {\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}\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 {\n\t\t\t\t\toptions?.onEvent?.({ type: \"thinking_end\", content: currentBlock.thinking });\n\t\t\t\t}\n\t\t\t\tblocks.push(currentBlock);\n\t\t\t}\n\n\t\t\tcalculateCost(this.model, usage);\n\n\t\t\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: blocks,\n\t\t\t\tprovider: this.model.provider,\n\t\t\t\tmodel: this.model.id,\n\t\t\t\tusage,\n\t\t\t\tstopReason,\n\t\t\t} satisfies AssistantMessage;\n\t\t\toptions?.onEvent?.({ type: \"done\", reason: stopReason, message: output });\n\t\t\treturn output;\n\t\t} catch (error) {\n\t\t\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: [],\n\t\t\t\tprovider: this.model.provider,\n\t\t\t\tmodel: this.model.id,\n\t\t\t\tusage: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t\t},\n\t\t\t\tstopReason: \"error\",\n\t\t\t\terror: error instanceof Error ? error.message : JSON.stringify(error),\n\t\t\t} satisfies AssistantMessage;\n\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error });\n\t\t\treturn output;\n\t\t}\n\t}\n\n\tprivate convertMessages(messages: Message[]): Content[] {\n\t\tconst contents: Content[] = [];\n\n\t\tfor (const msg of messages) {\n\t\t\tif (msg.role === \"user\") {\n\t\t\t\t// Handle both string and array content\n\t\t\t\tif (typeof msg.content === \"string\") {\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts: [{ text: msg.content }],\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// Convert array content to Google format\n\t\t\t\t\tconst parts: Part[] = msg.content.map((item) => {\n\t\t\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\t\t\treturn { text: item.text };\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Image content - Google uses inlineData\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tinlineData: {\n\t\t\t\t\t\t\t\t\tmimeType: item.mimeType,\n\t\t\t\t\t\t\t\t\tdata: item.data,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tparts,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"assistant\") {\n\t\t\t\tconst parts: Part[] = [];\n\n\t\t\t\t// Process content blocks\n\t\t\t\tfor (const block of msg.content) {\n\t\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\t\tparts.push({ text: block.text });\n\t\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\t\tconst thinkingPart: Part = {\n\t\t\t\t\t\t\tthought: true,\n\t\t\t\t\t\t\tthoughtSignature: block.thinkingSignature,\n\t\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tparts.push(thinkingPart);\n\t\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\t\tparts.push({\n\t\t\t\t\t\t\tfunctionCall: {\n\t\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\t\targs: block.arguments,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (parts.length > 0) {\n\t\t\t\t\tcontents.push({\n\t\t\t\t\t\trole: \"model\",\n\t\t\t\t\t\tparts,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === \"toolResult\") {\n\t\t\t\tcontents.push({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tparts: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfunctionResponse: {\n\t\t\t\t\t\t\t\tid: msg.toolCallId,\n\t\t\t\t\t\t\t\tname: msg.toolName,\n\t\t\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\t\t\tresult: msg.content,\n\t\t\t\t\t\t\t\t\tisError: msg.isError,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn contents;\n\t}\n\n\tprivate convertTools(tools: Tool[]): any[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tfunctionDeclarations: tools.map((tool) => ({\n\t\t\t\t\tname: tool.name,\n\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\tparameters: tool.parameters,\n\t\t\t\t})),\n\t\t\t},\n\t\t];\n\t}\n\n\tprivate mapToolChoice(choice: string): FunctionCallingConfigMode {\n\t\tswitch (choice) {\n\t\t\tcase \"auto\":\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t\tcase \"none\":\n\t\t\t\treturn FunctionCallingConfigMode.NONE;\n\t\t\tcase \"any\":\n\t\t\t\treturn FunctionCallingConfigMode.ANY;\n\t\t\tdefault:\n\t\t\t\treturn FunctionCallingConfigMode.AUTO;\n\t\t}\n\t}\n\n\tprivate mapStopReason(reason: FinishReason): StopReason {\n\t\tswitch (reason) {\n\t\t\tcase \"STOP\":\n\t\t\t\treturn \"stop\";\n\t\t\tcase \"MAX_TOKENS\":\n\t\t\t\treturn \"length\";\n\t\t\tcase \"BLOCKLIST\":\n\t\t\tcase \"PROHIBITED_CONTENT\":\n\t\t\tcase \"SPII\":\n\t\t\tcase \"SAFETY\":\n\t\t\tcase \"IMAGE_SAFETY\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"RECITATION\":\n\t\t\t\treturn \"safety\";\n\t\t\tcase \"FINISH_REASON_UNSPECIFIED\":\n\t\t\tcase \"OTHER\":\n\t\t\tcase \"LANGUAGE\":\n\t\t\tcase \"MALFORMED_FUNCTION_CALL\":\n\t\t\tcase \"UNEXPECTED_TOOL_CALL\":\n\t\t\t\treturn \"error\";\n\t\t\tdefault:\n\t\t\t\treturn \"stop\";\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,yBAAyB,EAGzB,WAAW,GAEX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAc7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAU/C,MAAM,OAAO,SAAS;IACb,MAAM,CAAc;IACpB,SAAS,CAAQ;IAEzB,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CACd,gGAAgG,CAChG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC,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AAK,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"]}
|
|
@@ -13,7 +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
|
-
|
|
16
|
+
generate(request: Context, options?: OpenAICompletionsLLMOptions): Promise<AssistantMessage>;
|
|
17
17
|
private convertMessages;
|
|
18
18
|
private convertTools;
|
|
19
19
|
private mapStopReason;
|
|
@@ -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,
|
|
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;IAIX,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,2BAA2B,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAqQlG,OAAO,CAAC,eAAe;IAiGvB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,aAAa;CAerB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import { calculateCost } from "../models.js";
|
|
3
|
+
import { transformMessages } from "./utils.js";
|
|
3
4
|
export class OpenAICompletionsLLM {
|
|
4
5
|
client;
|
|
5
6
|
modelInfo;
|
|
@@ -16,7 +17,21 @@ export class OpenAICompletionsLLM {
|
|
|
16
17
|
getModel() {
|
|
17
18
|
return this.modelInfo;
|
|
18
19
|
}
|
|
19
|
-
async
|
|
20
|
+
async generate(request, options) {
|
|
21
|
+
const output = {
|
|
22
|
+
role: "assistant",
|
|
23
|
+
content: [],
|
|
24
|
+
provider: this.modelInfo.provider,
|
|
25
|
+
model: this.modelInfo.id,
|
|
26
|
+
usage: {
|
|
27
|
+
input: 0,
|
|
28
|
+
output: 0,
|
|
29
|
+
cacheRead: 0,
|
|
30
|
+
cacheWrite: 0,
|
|
31
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
32
|
+
},
|
|
33
|
+
stopReason: "stop",
|
|
34
|
+
};
|
|
20
35
|
try {
|
|
21
36
|
const messages = this.convertMessages(request.messages, request.systemPrompt);
|
|
22
37
|
const params = {
|
|
@@ -50,19 +65,10 @@ export class OpenAICompletionsLLM {
|
|
|
50
65
|
signal: options?.signal,
|
|
51
66
|
});
|
|
52
67
|
options?.onEvent?.({ type: "start", model: this.modelInfo.id, provider: this.modelInfo.provider });
|
|
53
|
-
const blocks = [];
|
|
54
68
|
let currentBlock = null;
|
|
55
|
-
let usage = {
|
|
56
|
-
input: 0,
|
|
57
|
-
output: 0,
|
|
58
|
-
cacheRead: 0,
|
|
59
|
-
cacheWrite: 0,
|
|
60
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
61
|
-
};
|
|
62
|
-
let finishReason = null;
|
|
63
69
|
for await (const chunk of stream) {
|
|
64
70
|
if (chunk.usage) {
|
|
65
|
-
usage = {
|
|
71
|
+
output.usage = {
|
|
66
72
|
input: chunk.usage.prompt_tokens || 0,
|
|
67
73
|
output: (chunk.usage.completion_tokens || 0) +
|
|
68
74
|
(chunk.usage.completion_tokens_details?.reasoning_tokens || 0),
|
|
@@ -76,10 +82,15 @@ export class OpenAICompletionsLLM {
|
|
|
76
82
|
total: 0,
|
|
77
83
|
},
|
|
78
84
|
};
|
|
85
|
+
calculateCost(this.modelInfo, output.usage);
|
|
79
86
|
}
|
|
80
87
|
const choice = chunk.choices[0];
|
|
81
88
|
if (!choice)
|
|
82
89
|
continue;
|
|
90
|
+
// Capture finish reason
|
|
91
|
+
if (choice.finish_reason) {
|
|
92
|
+
output.stopReason = this.mapStopReason(choice.finish_reason);
|
|
93
|
+
}
|
|
83
94
|
if (choice.delta) {
|
|
84
95
|
// Handle text content
|
|
85
96
|
if (choice.delta.content !== null &&
|
|
@@ -97,10 +108,10 @@ export class OpenAICompletionsLLM {
|
|
|
97
108
|
delete currentBlock.partialArgs;
|
|
98
109
|
options?.onEvent?.({ type: "toolCall", toolCall: currentBlock });
|
|
99
110
|
}
|
|
100
|
-
blocks.push(currentBlock);
|
|
101
111
|
}
|
|
102
112
|
// Start new text block
|
|
103
113
|
currentBlock = { type: "text", text: "" };
|
|
114
|
+
output.content.push(currentBlock);
|
|
104
115
|
options?.onEvent?.({ type: "text_start" });
|
|
105
116
|
}
|
|
106
117
|
// Append to text block
|
|
@@ -129,10 +140,10 @@ export class OpenAICompletionsLLM {
|
|
|
129
140
|
delete currentBlock.partialArgs;
|
|
130
141
|
options?.onEvent?.({ type: "toolCall", toolCall: currentBlock });
|
|
131
142
|
}
|
|
132
|
-
blocks.push(currentBlock);
|
|
133
143
|
}
|
|
134
144
|
// Start new thinking block
|
|
135
145
|
currentBlock = { type: "thinking", thinking: "", thinkingSignature: "reasoning_content" };
|
|
146
|
+
output.content.push(currentBlock);
|
|
136
147
|
options?.onEvent?.({ type: "thinking_start" });
|
|
137
148
|
}
|
|
138
149
|
// Append to thinking block
|
|
@@ -158,10 +169,10 @@ export class OpenAICompletionsLLM {
|
|
|
158
169
|
delete currentBlock.partialArgs;
|
|
159
170
|
options?.onEvent?.({ type: "toolCall", toolCall: currentBlock });
|
|
160
171
|
}
|
|
161
|
-
blocks.push(currentBlock);
|
|
162
172
|
}
|
|
163
173
|
// Start new thinking block
|
|
164
174
|
currentBlock = { type: "thinking", thinking: "", thinkingSignature: "reasoning" };
|
|
175
|
+
output.content.push(currentBlock);
|
|
165
176
|
options?.onEvent?.({ type: "thinking_start" });
|
|
166
177
|
}
|
|
167
178
|
// Append to thinking block
|
|
@@ -191,7 +202,6 @@ export class OpenAICompletionsLLM {
|
|
|
191
202
|
delete currentBlock.partialArgs;
|
|
192
203
|
options?.onEvent?.({ type: "toolCall", toolCall: currentBlock });
|
|
193
204
|
}
|
|
194
|
-
blocks.push(currentBlock);
|
|
195
205
|
}
|
|
196
206
|
// Start new tool call block
|
|
197
207
|
currentBlock = {
|
|
@@ -201,6 +211,7 @@ export class OpenAICompletionsLLM {
|
|
|
201
211
|
arguments: {},
|
|
202
212
|
partialArgs: "",
|
|
203
213
|
};
|
|
214
|
+
output.content.push(currentBlock);
|
|
204
215
|
}
|
|
205
216
|
// Accumulate tool call data
|
|
206
217
|
if (currentBlock.type === "toolCall") {
|
|
@@ -215,10 +226,6 @@ export class OpenAICompletionsLLM {
|
|
|
215
226
|
}
|
|
216
227
|
}
|
|
217
228
|
}
|
|
218
|
-
// Capture finish reason
|
|
219
|
-
if (choice.finish_reason) {
|
|
220
|
-
finishReason = choice.finish_reason;
|
|
221
|
-
}
|
|
222
229
|
}
|
|
223
230
|
// Save final block if exists
|
|
224
231
|
if (currentBlock) {
|
|
@@ -233,43 +240,25 @@ export class OpenAICompletionsLLM {
|
|
|
233
240
|
delete currentBlock.partialArgs;
|
|
234
241
|
options?.onEvent?.({ type: "toolCall", toolCall: currentBlock });
|
|
235
242
|
}
|
|
236
|
-
blocks.push(currentBlock);
|
|
237
243
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
role: "assistant",
|
|
242
|
-
content: blocks,
|
|
243
|
-
provider: this.modelInfo.provider,
|
|
244
|
-
model: this.modelInfo.id,
|
|
245
|
-
usage,
|
|
246
|
-
stopReason: this.mapStopReason(finishReason),
|
|
247
|
-
};
|
|
244
|
+
if (options?.signal?.aborted) {
|
|
245
|
+
throw new Error("Request was aborted");
|
|
246
|
+
}
|
|
248
247
|
options?.onEvent?.({ type: "done", reason: output.stopReason, message: output });
|
|
249
248
|
return output;
|
|
250
249
|
}
|
|
251
250
|
catch (error) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
model: this.modelInfo.id,
|
|
257
|
-
usage: {
|
|
258
|
-
input: 0,
|
|
259
|
-
output: 0,
|
|
260
|
-
cacheRead: 0,
|
|
261
|
-
cacheWrite: 0,
|
|
262
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
263
|
-
},
|
|
264
|
-
stopReason: "error",
|
|
265
|
-
error: error instanceof Error ? error.message : String(error),
|
|
266
|
-
};
|
|
267
|
-
options?.onEvent?.({ type: "error", error: output.error || "Unknown error" });
|
|
251
|
+
// Update output with error information
|
|
252
|
+
output.stopReason = "error";
|
|
253
|
+
output.error = error instanceof Error ? error.message : String(error);
|
|
254
|
+
options?.onEvent?.({ type: "error", error: output.error });
|
|
268
255
|
return output;
|
|
269
256
|
}
|
|
270
257
|
}
|
|
271
258
|
convertMessages(messages, systemPrompt) {
|
|
272
259
|
const params = [];
|
|
260
|
+
// Transform messages for cross-provider compatibility
|
|
261
|
+
const transformedMessages = transformMessages(messages, this.modelInfo);
|
|
273
262
|
// Add system prompt if provided
|
|
274
263
|
if (systemPrompt) {
|
|
275
264
|
// Cerebras/xAi don't like the "developer" role
|
|
@@ -280,7 +269,7 @@ export class OpenAICompletionsLLM {
|
|
|
280
269
|
params.push({ role: role, content: systemPrompt });
|
|
281
270
|
}
|
|
282
271
|
// Convert messages
|
|
283
|
-
for (const msg of
|
|
272
|
+
for (const msg of transformedMessages) {
|
|
284
273
|
if (msg.role === "user") {
|
|
285
274
|
// Handle both string and array content
|
|
286
275
|
if (typeof msg.content === "string") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.js","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAQ5B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAqB7C,MAAM,OAAO,oBAAoB;IACx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;iBAAM,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBACtC,MAAM,CAAC,IAAI,CAAC;oBACX,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE,GAAG,CAAC,OAAO;oBACpB,YAAY,EAAE,GAAG,CAAC,UAAU;iBAC5B,CAAC,CAAC;YACJ,CAAC;QACF,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,YAAY,CAAC,KAAa;QACjC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC3B,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACT,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,UAAU,EAAE,IAAI,CAAC,UAAU;aAC3B;SACD,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,MAA0D;QAC/E,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,MAAM;gBACV,OAAO,MAAM,CAAC;YACf,KAAK,QAAQ;gBACZ,OAAO,QAAQ,CAAC;YACjB,KAAK,eAAe,CAAC;YACrB,KAAK,YAAY;gBAChB,OAAO,SAAS,CAAC;YAClB,KAAK,gBAAgB;gBACpB,OAAO,QAAQ,CAAC;YACjB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tChatCompletionChunk,\n\tChatCompletionContentPart,\n\tChatCompletionContentPartImage,\n\tChatCompletionContentPartText,\n\tChatCompletionMessageParam,\n} from \"openai/resources/chat/completions.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tAssistantMessage,\n\tContext,\n\tLLM,\n\tLLMOptions,\n\tMessage,\n\tModel,\n\tStopReason,\n\tTextContent,\n\tThinkingContent,\n\tTool,\n\tToolCall,\n\tUsage,\n} from \"../types.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 complete(request: Context, options?: OpenAICompletionsLLMOptions): Promise<AssistantMessage> {\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\tconst blocks: AssistantMessage[\"content\"] = [];\n\t\t\tlet currentBlock: TextContent | ThinkingContent | (ToolCall & { partialArgs?: string }) | null = null;\n\t\t\tlet usage: Usage = {\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\tlet finishReason: ChatCompletionChunk.Choice[\"finish_reason\"] | null = null;\n\t\t\tfor await (const chunk of stream) {\n\t\t\t\tif (chunk.usage) {\n\t\t\t\t\tusage = {\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}\n\n\t\t\t\tconst choice = chunk.choices[0];\n\t\t\t\tif (!choice) continue;\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\tblocks.push(currentBlock);\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\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\tblocks.push(currentBlock);\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\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\tblocks.push(currentBlock);\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\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\tblocks.push(currentBlock);\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}\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\n\t\t\t\t// Capture finish reason\n\t\t\t\tif (choice.finish_reason) {\n\t\t\t\t\tfinishReason = choice.finish_reason;\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\tblocks.push(currentBlock);\n\t\t\t}\n\n\t\t\t// Calculate cost\n\t\t\tcalculateCost(this.modelInfo, usage);\n\n\t\t\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: blocks,\n\t\t\t\tprovider: this.modelInfo.provider,\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tusage,\n\t\t\t\tstopReason: this.mapStopReason(finishReason),\n\t\t\t} satisfies AssistantMessage;\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\tconst output = {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: [],\n\t\t\t\tprovider: this.modelInfo.provider,\n\t\t\t\tmodel: this.modelInfo.id,\n\t\t\t\tusage: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t\t},\n\t\t\t\tstopReason: \"error\",\n\t\t\t\terror: error instanceof Error ? error.message : String(error),\n\t\t\t} satisfies AssistantMessage;\n\t\t\toptions?.onEvent?.({ type: \"error\", error: output.error || \"Unknown 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// 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 messages) {\n\t\t\tif (msg.role === \"user\") {\n\t\t\t\t// Handle both string and array content\n\t\t\t\tif (typeof msg.content === \"string\") {\n\t\t\t\t\tparams.push({\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// Convert array content to 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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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"]}
|
|
@@ -8,7 +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
|
-
|
|
11
|
+
generate(request: Context, options?: OpenAIResponsesLLMOptions): Promise<AssistantMessage>;
|
|
12
12
|
private convertToInput;
|
|
13
13
|
private convertTools;
|
|
14
14
|
private mapStopReason;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses.d.ts","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"
|
|
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;IAIX,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAwOhG,OAAO,CAAC,cAAc;IA+FtB,OAAO,CAAC,YAAY;IAUpB,OAAO,CAAC,aAAa;CAarB"}
|