@absolutejs/ai 0.0.1

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.
Files changed (42) hide show
  1. package/README.md +6 -0
  2. package/dist/ai/client/index.js +682 -0
  3. package/dist/ai/client/index.js.map +15 -0
  4. package/dist/ai/index.js +3317 -0
  5. package/dist/ai/index.js.map +28 -0
  6. package/dist/ai/providers/anthropic.js +371 -0
  7. package/dist/ai/providers/anthropic.js.map +10 -0
  8. package/dist/ai/providers/gemini.js +260 -0
  9. package/dist/ai/providers/gemini.js.map +10 -0
  10. package/dist/ai/providers/ollama.js +221 -0
  11. package/dist/ai/providers/ollama.js.map +10 -0
  12. package/dist/ai/providers/openai.js +322 -0
  13. package/dist/ai/providers/openai.js.map +10 -0
  14. package/dist/ai/providers/openaiCompatible.js +360 -0
  15. package/dist/ai/providers/openaiCompatible.js.map +11 -0
  16. package/dist/ai/providers/openaiResponses.js +381 -0
  17. package/dist/ai/providers/openaiResponses.js.map +10 -0
  18. package/dist/src/ai/client/actions.d.ts +186 -0
  19. package/dist/src/ai/client/connection.d.ts +9 -0
  20. package/dist/src/ai/client/createAIStream.d.ts +12 -0
  21. package/dist/src/ai/client/index.d.ts +4 -0
  22. package/dist/src/ai/client/messageStore.d.ts +12 -0
  23. package/dist/src/ai/conversationManager.d.ts +19 -0
  24. package/dist/src/ai/htmxRenderers.d.ts +3 -0
  25. package/dist/src/ai/index.d.ts +19 -0
  26. package/dist/src/ai/memoryStore.d.ts +2 -0
  27. package/dist/src/ai/protocol.d.ts +4 -0
  28. package/dist/src/ai/providers/anthropic.d.ts +3 -0
  29. package/dist/src/ai/providers/gemini.d.ts +8 -0
  30. package/dist/src/ai/providers/ollama.d.ts +6 -0
  31. package/dist/src/ai/providers/openai.d.ts +7 -0
  32. package/dist/src/ai/providers/openaiCompatible.d.ts +30 -0
  33. package/dist/src/ai/providers/openaiResponses.d.ts +33 -0
  34. package/dist/src/ai/streamAI.d.ts +2 -0
  35. package/dist/src/ai/streamAIToSSE.d.ts +6 -0
  36. package/dist/src/constants.d.ts +60 -0
  37. package/dist/src/plugins/aiChat.d.ts +239 -0
  38. package/dist/types/ai.d.ts +4773 -0
  39. package/dist/types/anthropic.d.ts +20 -0
  40. package/dist/types/session.d.ts +16 -0
  41. package/dist/types/typeGuards.d.ts +3 -0
  42. package/package.json +59 -0
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/ai/providers/openai.ts"],
4
+ "sourcesContent": [
5
+ "import type {\n\tAIProviderConfig,\n\tAIProviderContentBlock,\n\tAIProviderMessage,\n\tAIProviderStreamParams,\n\tAIProviderToolDefinition,\n\tAIUsage\n} from '../../../types/ai';\n\ntype OpenAIConfig = {\n\tapiKey: string;\n\tbaseUrl?: string;\n};\n\ntype OpenAIMessage = {\n\tcontent: string | Array<Record<string, unknown>> | null;\n\trole: 'user' | 'assistant' | 'system' | 'tool';\n\ttool_call_id?: string;\n\ttool_calls?: Array<{\n\t\tfunction: { arguments: string; name: string };\n\t\tid: string;\n\t\ttype: 'function';\n\t}>;\n};\n\ntype PendingToolCall = {\n\targuments: string;\n\tid: string;\n\tname: string;\n};\n\ntype UsageRef = {\n\tcurrent: AIUsage | undefined;\n};\n\ntype StreamState = {\n\tbuffer: string;\n\tpendingToolCalls: Map<number, PendingToolCall>;\n\tusageRef: UsageRef;\n};\n\nconst DEFAULT_BASE_URL = 'https://api.openai.com';\nconst SSE_DATA_PREFIX_LENGTH = 6;\nconst DONE_SENTINEL = '[DONE]';\nconst NOT_FOUND = -1;\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n\ttypeof value === 'object' && value !== null;\n\nconst isRecordArray = (\n\tvalue: unknown\n): value is Array<Record<string, unknown>> =>\n\tArray.isArray(value) && value.length > 0 && isRecord(value[0]);\n\nconst hasArrayContent = (\n\tmsg: AIProviderMessage\n): msg is AIProviderMessage & { content: AIProviderContentBlock[] } =>\n\ttypeof msg.content !== 'string' && Array.isArray(msg.content);\n\nconst buildToolMessages = (blocks: AIProviderContentBlock[]) => {\n\tconst toolUseBlocks = blocks.filter((block) => block.type === 'tool_use');\n\tconst toolResultBlocks = blocks.filter(\n\t\t(block) => block.type === 'tool_result'\n\t);\n\tconst messages: OpenAIMessage[] = [];\n\n\tif (toolUseBlocks.length > 0) {\n\t\tmessages.push({\n\t\t\tcontent: null,\n\t\t\trole: 'assistant',\n\t\t\ttool_calls: toolUseBlocks.map((block) => ({\n\t\t\t\tfunction: {\n\t\t\t\t\targuments:\n\t\t\t\t\t\ttypeof block.input === 'string'\n\t\t\t\t\t\t\t? block.input\n\t\t\t\t\t\t\t: JSON.stringify(block.input),\n\t\t\t\t\tname: block.name\n\t\t\t\t},\n\t\t\t\tid: block.id,\n\t\t\t\ttype: 'function' as const\n\t\t\t}))\n\t\t});\n\t}\n\n\tfor (const result of toolResultBlocks) {\n\t\tmessages.push({\n\t\t\tcontent: typeof result.content === 'string' ? result.content : '',\n\t\t\trole: 'tool',\n\t\t\ttool_call_id: result.tool_use_id\n\t\t});\n\t}\n\n\treturn messages;\n};\n\nconst processMessageAtIndex = (\n\tresult: OpenAIMessage[],\n\tmsg: AIProviderMessage,\n\tidx: number\n) => {\n\tif (!hasArrayContent(msg)) {\n\t\treturn;\n\t}\n\n\tconst hasToolBlocks = msg.content.some(\n\t\t(block) => block.type === 'tool_use' || block.type === 'tool_result'\n\t);\n\n\tif (!hasToolBlocks) {\n\t\treturn;\n\t}\n\n\tconst toolMessages = buildToolMessages(msg.content);\n\tresult.splice(idx, 1, ...toolMessages);\n};\n\nconst convertSingleMessage = (\n\tresult: OpenAIMessage[],\n\tmsg: AIProviderMessage | undefined,\n\tidx: number\n) => {\n\tif (!msg) {\n\t\treturn;\n\t}\n\n\tprocessMessageAtIndex(result, msg, idx);\n};\n\nconst convertToolResultMessages = (\n\tmessages: OpenAIMessage[],\n\tparams: AIProviderStreamParams\n) => {\n\tconst result = [...messages];\n\n\tfor (let idx = 0; idx < params.messages.length; idx++) {\n\t\tconvertSingleMessage(result, params.messages[idx], idx);\n\t}\n\n\treturn result;\n};\n\nconst mapToolDefinitions = (tools: AIProviderToolDefinition[]) =>\n\ttools.map((tool) => ({\n\t\tfunction: {\n\t\t\tdescription: tool.description,\n\t\t\tname: tool.name,\n\t\t\tparameters: tool.input_schema\n\t\t},\n\t\ttype: 'function'\n\t}));\n\nconst mapContentBlockToOpenAI = (block: AIProviderContentBlock) => {\n\tif (block.type === 'image') {\n\t\treturn {\n\t\t\timage_url: {\n\t\t\t\turl: `data:${block.source.media_type};base64,${block.source.data}`\n\t\t\t},\n\t\t\ttype: 'image_url'\n\t\t};\n\t}\n\n\tif (block.type === 'document') {\n\t\treturn {\n\t\t\tfile: {\n\t\t\t\tfile_data: `data:${block.source.media_type};base64,${block.source.data}`,\n\t\t\t\tfilename: block.name ?? 'document.pdf'\n\t\t\t},\n\t\t\ttype: 'file'\n\t\t};\n\t}\n\n\tif (block.type === 'text') {\n\t\treturn { text: block.content, type: 'text' };\n\t}\n\n\treturn null;\n};\n\nconst mapOpenAIContent = (msg: AIProviderStreamParams['messages'][number]) => {\n\tif (typeof msg.content === 'string') {\n\t\treturn msg.content;\n\t}\n\n\tconst hasMedia = msg.content.some(\n\t\t(block) => block.type === 'image' || block.type === 'document'\n\t);\n\n\tif (!hasMedia) {\n\t\treturn null;\n\t}\n\n\treturn msg.content\n\t\t.map(mapContentBlockToOpenAI)\n\t\t.filter((mapped) => mapped !== null);\n};\n\nconst buildRequestBody = (params: AIProviderStreamParams) => {\n\tconst messages = convertToolResultMessages(\n\t\tparams.messages.map((msg) => ({\n\t\t\tcontent: mapOpenAIContent(msg),\n\t\t\trole: msg.role\n\t\t})),\n\t\tparams\n\t);\n\n\tconst body: Record<string, unknown> = {\n\t\tmessages,\n\t\tmodel: params.model,\n\t\tstream: true,\n\t\tstream_options: { include_usage: true }\n\t};\n\n\tif (params.tools && params.tools.length > 0) {\n\t\tbody.tools = mapToolDefinitions(params.tools);\n\t}\n\n\treturn body;\n};\n\nconst parseToolInput = (rawArguments: string) => {\n\ttry {\n\t\treturn JSON.parse(rawArguments);\n\t} catch {\n\t\treturn rawArguments;\n\t}\n};\n\nconst flushPendingToolCalls = function* (\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tfor (const [, tool] of pendingToolCalls) {\n\t\tconst input = parseToolInput(tool.arguments);\n\t\tyield {\n\t\t\tid: tool.id,\n\t\t\tinput,\n\t\t\tname: tool.name,\n\t\t\ttype: 'tool_use' as const\n\t\t};\n\t}\n\n\tpendingToolCalls.clear();\n};\n\nconst extractUsage = (parsedUsage: Record<string, number>) => ({\n\tinputTokens: parsedUsage.prompt_tokens ?? 0,\n\toutputTokens: parsedUsage.completion_tokens ?? 0\n});\n\nconst resolveToolCallIndex = (toolCall: Record<string, unknown>) => {\n\tconst raw = typeof toolCall.index === 'number' ? toolCall.index : NOT_FOUND;\n\n\treturn raw < 0 ? undefined : raw;\n};\n\nconst initPendingToolCall = (\n\ttoolCall: Record<string, unknown>,\n\tfunc: Record<string, unknown> | null,\n\tindex: number,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tif (pendingToolCalls.has(index)) {\n\t\treturn;\n\t}\n\n\tconst toolId = typeof toolCall.id === 'string' ? toolCall.id : '';\n\tconst toolName = func && typeof func.name === 'string' ? func.name : '';\n\n\tpendingToolCalls.set(index, {\n\t\targuments: '',\n\t\tid: toolId,\n\t\tname: toolName\n\t});\n};\n\nconst updatePendingToolCall = (\n\ttoolCall: Record<string, unknown>,\n\tfunc: Record<string, unknown> | null,\n\tpending: PendingToolCall\n) => {\n\tif (typeof toolCall.id === 'string') {\n\t\tpending.id = toolCall.id;\n\t}\n\n\tif (func && typeof func.name === 'string') {\n\t\tpending.name = func.name;\n\t}\n\n\tif (func && typeof func.arguments === 'string') {\n\t\tpending.arguments += func.arguments;\n\t}\n};\n\nconst processToolCallDelta = (\n\ttoolCall: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tconst index = resolveToolCallIndex(toolCall);\n\tif (index === undefined) {\n\t\treturn;\n\t}\n\n\tconst func = isRecord(toolCall.function) ? toolCall.function : null;\n\tinitPendingToolCall(toolCall, func, index, pendingToolCalls);\n\n\tconst pending = pendingToolCalls.get(index);\n\tif (!pending) {\n\t\treturn;\n\t}\n\n\tupdatePendingToolCall(toolCall, func, pending);\n};\n\nconst processToolCallDeltas = (\n\ttoolCalls: Array<Record<string, unknown>>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tfor (const toolCall of toolCalls) {\n\t\tprocessToolCallDelta(toolCall, pendingToolCalls);\n\t}\n};\n\nconst processDelta = function* (\n\tdelta: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tif (typeof delta.content === 'string') {\n\t\tyield { content: delta.content, type: 'text' as const };\n\t}\n\n\tif (isRecordArray(delta.tool_calls)) {\n\t\tprocessToolCallDeltas(delta.tool_calls, pendingToolCalls);\n\t}\n};\n\nconst processChoice = function* (\n\tchoice: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tconst delta = isRecord(choice.delta) ? choice.delta : null;\n\tif (delta) {\n\t\tyield* processDelta(delta, pendingToolCalls);\n\t}\n\n\tif (choice.finish_reason === 'tool_calls') {\n\t\tyield* flushPendingToolCalls(pendingToolCalls);\n\t}\n};\n\nconst narrowUsageRecord = (parsed: Record<string, unknown>) => {\n\tif (!isRecord(parsed.usage)) {\n\t\treturn undefined;\n\t}\n\n\tconst { usage } = parsed;\n\tconst promptTokens =\n\t\ttypeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0;\n\tconst completionTokens =\n\t\ttypeof usage.completion_tokens === 'number'\n\t\t\t? usage.completion_tokens\n\t\t\t: 0;\n\n\treturn extractUsage({\n\t\tcompletion_tokens: completionTokens,\n\t\tprompt_tokens: promptTokens\n\t});\n};\n\nconst processSSELine = function* (\n\tline: string,\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tcurrentUsage: AIUsage | undefined\n) {\n\tconst trimmed = line.trim();\n\tif (!trimmed || !trimmed.startsWith('data: ')) {\n\t\treturn;\n\t}\n\n\tconst data = trimmed.slice(SSE_DATA_PREFIX_LENGTH);\n\tif (data === DONE_SENTINEL) {\n\t\tyield* flushPendingToolCalls(pendingToolCalls);\n\t\tyield { type: 'done' as const, usage: currentUsage };\n\n\t\treturn;\n\t}\n\n\tlet parsed: Record<string, unknown>;\n\ttry {\n\t\tparsed = JSON.parse(data);\n\t} catch {\n\t\treturn;\n\t}\n\n\tconst usageUpdate = narrowUsageRecord(parsed);\n\tif (usageUpdate) {\n\t\tyield { type: 'usage_update' as const, usage: usageUpdate };\n\t}\n\n\tconst { choices } = parsed;\n\tif (!isRecordArray(choices)) {\n\t\treturn;\n\t}\n\n\tconst [firstChoice] = choices;\n\tif (!firstChoice) {\n\t\treturn;\n\t}\n\n\tyield* processChoice(firstChoice, pendingToolCalls);\n};\n\nconst isUsageUpdate = (chunk: {\n\ttype: string;\n\tusage?: AIUsage;\n}): chunk is { type: 'usage_update'; usage: AIUsage } =>\n\tchunk.type === 'usage_update';\n\nconst collectYieldableChunks = (\n\tline: string,\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tusageRef: UsageRef\n) => {\n\tconst allChunks = Array.from(\n\t\tprocessSSELine(line, pendingToolCalls, usageRef.current)\n\t);\n\tconst usageChunks = allChunks.filter(isUsageUpdate);\n\tconst lastUsage = usageChunks.at(NOT_FOUND);\n\n\tif (lastUsage) {\n\t\tusageRef.current = lastUsage.usage;\n\t}\n\n\treturn allChunks.filter((chunk) => !isUsageUpdate(chunk));\n};\n\nconst processSSELines = function* (\n\tlines: string[],\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tusageRef: UsageRef\n) {\n\tfor (const line of lines) {\n\t\tyield* collectYieldableChunks(line, pendingToolCalls, usageRef);\n\t}\n};\n\nconst processStreamValue = (\n\tvalue: Uint8Array,\n\tdecoder: TextDecoder,\n\tstate: StreamState\n) => {\n\tstate.buffer += decoder.decode(value, { stream: true });\n\tconst lines = state.buffer.split('\\n');\n\tstate.buffer = lines.pop() ?? '';\n\n\treturn lines;\n};\n\nconst drainReader = async function* (\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n\tdecoder: TextDecoder,\n\tstate: StreamState,\n\tsignal?: AbortSignal\n) {\n\t/* eslint-disable no-await-in-loop */\n\tfor (\n\t\tlet result = await reader.read();\n\t\t!result.done && !signal?.aborted;\n\t\tresult = await reader.read()\n\t) {\n\t\t/* eslint-enable no-await-in-loop */\n\t\tconst lines = processStreamValue(result.value, decoder, state);\n\t\tyield* processSSELines(lines, state.pendingToolCalls, state.usageRef);\n\t}\n};\n\nconst parseSSEStream = async function* (\n\tbody: ReadableStream<Uint8Array>,\n\tsignal?: AbortSignal\n) {\n\tconst reader = body.getReader();\n\tconst decoder = new TextDecoder();\n\tconst state: StreamState = {\n\t\tbuffer: '',\n\t\tpendingToolCalls: new Map<number, PendingToolCall>(),\n\t\tusageRef: { current: undefined }\n\t};\n\n\ttry {\n\t\tyield* drainReader(reader, decoder, state, signal);\n\t\tyield { type: 'done' as const, usage: state.usageRef.current };\n\t} finally {\n\t\treader.releaseLock();\n\t}\n};\n\nconst fetchOpenAIStream = async function* (\n\tbaseUrl: string,\n\tapiKey: string,\n\tbody: Record<string, unknown>,\n\tsignal?: AbortSignal\n) {\n\tconst response = await fetch(`${baseUrl}/v1/chat/completions`, {\n\t\tbody: JSON.stringify(body),\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t'Content-Type': 'application/json'\n\t\t},\n\t\tmethod: 'POST',\n\t\tsignal\n\t});\n\n\tif (!response.ok) {\n\t\tconst errorText = await response.text();\n\t\tthrow new Error(`OpenAI API error ${response.status}: ${errorText}`);\n\t}\n\n\tif (!response.body) {\n\t\tthrow new Error('OpenAI API returned no response body');\n\t}\n\n\tyield* parseSSEStream(response.body, signal);\n};\n\nexport const openai = (config: OpenAIConfig): AIProviderConfig => {\n\tconst baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;\n\n\treturn {\n\t\tstream: (params: AIProviderStreamParams) => {\n\t\t\tconst body = buildRequestBody(params);\n\n\t\t\treturn fetchOpenAIStream(\n\t\t\t\tbaseUrl,\n\t\t\t\tconfig.apiKey,\n\t\t\t\tbody,\n\t\t\t\tparams.signal\n\t\t\t);\n\t\t}\n\t};\n};\n"
6
+ ],
7
+ "mappings": ";;AAyCA,IAAM,mBAAmB;AACzB,IAAM,yBAAyB;AAC/B,IAAM,gBAAgB;AACtB,IAAM,YAAY;AAElB,IAAM,WAAW,CAAC,UACjB,OAAO,UAAU,YAAY,UAAU;AAExC,IAAM,gBAAgB,CACrB,UAEA,MAAM,QAAQ,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS,MAAM,EAAE;AAE9D,IAAM,kBAAkB,CACvB,QAEA,OAAO,IAAI,YAAY,YAAY,MAAM,QAAQ,IAAI,OAAO;AAE7D,IAAM,oBAAoB,CAAC,WAAqC;AAAA,EAC/D,MAAM,gBAAgB,OAAO,OAAO,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EACxE,MAAM,mBAAmB,OAAO,OAC/B,CAAC,UAAU,MAAM,SAAS,aAC3B;AAAA,EACA,MAAM,WAA4B,CAAC;AAAA,EAEnC,IAAI,cAAc,SAAS,GAAG;AAAA,IAC7B,SAAS,KAAK;AAAA,MACb,SAAS;AAAA,MACT,MAAM;AAAA,MACN,YAAY,cAAc,IAAI,CAAC,WAAW;AAAA,QACzC,UAAU;AAAA,UACT,WACC,OAAO,MAAM,UAAU,WACpB,MAAM,QACN,KAAK,UAAU,MAAM,KAAK;AAAA,UAC9B,MAAM,MAAM;AAAA,QACb;AAAA,QACA,IAAI,MAAM;AAAA,QACV,MAAM;AAAA,MACP,EAAE;AAAA,IACH,CAAC;AAAA,EACF;AAAA,EAEA,WAAW,UAAU,kBAAkB;AAAA,IACtC,SAAS,KAAK;AAAA,MACb,SAA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M,oBAAoB,gBAAgB,CACzC,SACA,QACA,MACA,QACC;AAAA,EACD,MAAM,WAAW,MAAM,MAAM,GAAG,+BAA+B;AAAA,IAC9D,MAAM,KAAK,UAAU,IAAI;AAAA,IACzB,SAAS;AAAA,MACR,eAAe,UAAU;AAAA,MACzB,gBAAgB;AAAA,IACjB;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,EACD,CAAC;AAAA,EAED,IAAI,CAAC,SAAS,IAAI;AAAA,IACjB,MAAM,YAAY,MAAM,SAAS,KAAK;AAAA,IACtC,MAAM,IAAI,MAAM,oBAAoB,SAAS,WAAW,WAAW;AAAA,EACpE;AAAA,EAEA,IAAI,CAAC,SAAS,MAAM;AAAA,IACnB,MAAM,IAAI,MAAM,sCAAsC;AAAA,EACvD;AAAA,EAEA,OAAO,eAAe,SAAS,MAAM,MAAM;AAAA;AAGrC,IAAM,SAAS,CAAC,WAA2C;AAAA,EACjE,MAAM,UAAU,OAAO,WAAW;AAAA,EAElC,OAAO;AAAA,IACN,QAAQ,CAAC,WAAmC;AAAA,MAC3C,MAAM,OAAO,iBAAiB,MAAM;AAAA,MAEpC,OAAO,kBACN,SACA,OAAO,QACP,MACA,OAAO,MACR;AAAA;AAAA,EAEF;AAAA;",
8
+ "debugId": "B928538A3EDE03A364756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,360 @@
1
+ // @bun
2
+ // src/ai/providers/openai.ts
3
+ var DEFAULT_BASE_URL = "https://api.openai.com";
4
+ var SSE_DATA_PREFIX_LENGTH = 6;
5
+ var DONE_SENTINEL = "[DONE]";
6
+ var NOT_FOUND = -1;
7
+ var isRecord = (value) => typeof value === "object" && value !== null;
8
+ var isRecordArray = (value) => Array.isArray(value) && value.length > 0 && isRecord(value[0]);
9
+ var hasArrayContent = (msg) => typeof msg.content !== "string" && Array.isArray(msg.content);
10
+ var buildToolMessages = (blocks) => {
11
+ const toolUseBlocks = blocks.filter((block) => block.type === "tool_use");
12
+ const toolResultBlocks = blocks.filter((block) => block.type === "tool_result");
13
+ const messages = [];
14
+ if (toolUseBlocks.length > 0) {
15
+ messages.push({
16
+ content: null,
17
+ role: "assistant",
18
+ tool_calls: toolUseBlocks.map((block) => ({
19
+ function: {
20
+ arguments: typeof block.input === "string" ? block.input : JSON.stringify(block.input),
21
+ name: block.name
22
+ },
23
+ id: block.id,
24
+ type: "function"
25
+ }))
26
+ });
27
+ }
28
+ for (const result of toolResultBlocks) {
29
+ messages.push({
30
+ content: typeof result.content === "string" ? result.content : "",
31
+ role: "tool",
32
+ tool_call_id: result.tool_use_id
33
+ });
34
+ }
35
+ return messages;
36
+ };
37
+ var processMessageAtIndex = (result, msg, idx) => {
38
+ if (!hasArrayContent(msg)) {
39
+ return;
40
+ }
41
+ const hasToolBlocks = msg.content.some((block) => block.type === "tool_use" || block.type === "tool_result");
42
+ if (!hasToolBlocks) {
43
+ return;
44
+ }
45
+ const toolMessages = buildToolMessages(msg.content);
46
+ result.splice(idx, 1, ...toolMessages);
47
+ };
48
+ var convertSingleMessage = (result, msg, idx) => {
49
+ if (!msg) {
50
+ return;
51
+ }
52
+ processMessageAtIndex(result, msg, idx);
53
+ };
54
+ var convertToolResultMessages = (messages, params) => {
55
+ const result = [...messages];
56
+ for (let idx = 0;idx < params.messages.length; idx++) {
57
+ convertSingleMessage(result, params.messages[idx], idx);
58
+ }
59
+ return result;
60
+ };
61
+ var mapToolDefinitions = (tools) => tools.map((tool) => ({
62
+ function: {
63
+ description: tool.description,
64
+ name: tool.name,
65
+ parameters: tool.input_schema
66
+ },
67
+ type: "function"
68
+ }));
69
+ var mapContentBlockToOpenAI = (block) => {
70
+ if (block.type === "image") {
71
+ return {
72
+ image_url: {
73
+ url: `data:${block.source.media_type};base64,${block.source.data}`
74
+ },
75
+ type: "image_url"
76
+ };
77
+ }
78
+ if (block.type === "document") {
79
+ return {
80
+ file: {
81
+ file_data: `data:${block.source.media_type};base64,${block.source.data}`,
82
+ filename: block.name ?? "document.pdf"
83
+ },
84
+ type: "file"
85
+ };
86
+ }
87
+ if (block.type === "text") {
88
+ return { text: block.content, type: "text" };
89
+ }
90
+ return null;
91
+ };
92
+ var mapOpenAIContent = (msg) => {
93
+ if (typeof msg.content === "string") {
94
+ return msg.content;
95
+ }
96
+ const hasMedia = msg.content.some((block) => block.type === "image" || block.type === "document");
97
+ if (!hasMedia) {
98
+ return null;
99
+ }
100
+ return msg.content.map(mapContentBlockToOpenAI).filter((mapped) => mapped !== null);
101
+ };
102
+ var buildRequestBody = (params) => {
103
+ const messages = convertToolResultMessages(params.messages.map((msg) => ({
104
+ content: mapOpenAIContent(msg),
105
+ role: msg.role
106
+ })), params);
107
+ const body = {
108
+ messages,
109
+ model: params.model,
110
+ stream: true,
111
+ stream_options: { include_usage: true }
112
+ };
113
+ if (params.tools && params.tools.length > 0) {
114
+ body.tools = mapToolDefinitions(params.tools);
115
+ }
116
+ return body;
117
+ };
118
+ var parseToolInput = (rawArguments) => {
119
+ try {
120
+ return JSON.parse(rawArguments);
121
+ } catch {
122
+ return rawArguments;
123
+ }
124
+ };
125
+ var flushPendingToolCalls = function* (pendingToolCalls) {
126
+ for (const [, tool] of pendingToolCalls) {
127
+ const input = parseToolInput(tool.arguments);
128
+ yield {
129
+ id: tool.id,
130
+ input,
131
+ name: tool.name,
132
+ type: "tool_use"
133
+ };
134
+ }
135
+ pendingToolCalls.clear();
136
+ };
137
+ var extractUsage = (parsedUsage) => ({
138
+ inputTokens: parsedUsage.prompt_tokens ?? 0,
139
+ outputTokens: parsedUsage.completion_tokens ?? 0
140
+ });
141
+ var resolveToolCallIndex = (toolCall) => {
142
+ const raw = typeof toolCall.index === "number" ? toolCall.index : NOT_FOUND;
143
+ return raw < 0 ? undefined : raw;
144
+ };
145
+ var initPendingToolCall = (toolCall, func, index, pendingToolCalls) => {
146
+ if (pendingToolCalls.has(index)) {
147
+ return;
148
+ }
149
+ const toolId = typeof toolCall.id === "string" ? toolCall.id : "";
150
+ const toolName = func && typeof func.name === "string" ? func.name : "";
151
+ pendingToolCalls.set(index, {
152
+ arguments: "",
153
+ id: toolId,
154
+ name: toolName
155
+ });
156
+ };
157
+ var updatePendingToolCall = (toolCall, func, pending) => {
158
+ if (typeof toolCall.id === "string") {
159
+ pending.id = toolCall.id;
160
+ }
161
+ if (func && typeof func.name === "string") {
162
+ pending.name = func.name;
163
+ }
164
+ if (func && typeof func.arguments === "string") {
165
+ pending.arguments += func.arguments;
166
+ }
167
+ };
168
+ var processToolCallDelta = (toolCall, pendingToolCalls) => {
169
+ const index = resolveToolCallIndex(toolCall);
170
+ if (index === undefined) {
171
+ return;
172
+ }
173
+ const func = isRecord(toolCall.function) ? toolCall.function : null;
174
+ initPendingToolCall(toolCall, func, index, pendingToolCalls);
175
+ const pending = pendingToolCalls.get(index);
176
+ if (!pending) {
177
+ return;
178
+ }
179
+ updatePendingToolCall(toolCall, func, pending);
180
+ };
181
+ var processToolCallDeltas = (toolCalls, pendingToolCalls) => {
182
+ for (const toolCall of toolCalls) {
183
+ processToolCallDelta(toolCall, pendingToolCalls);
184
+ }
185
+ };
186
+ var processDelta = function* (delta, pendingToolCalls) {
187
+ if (typeof delta.content === "string") {
188
+ yield { content: delta.content, type: "text" };
189
+ }
190
+ if (isRecordArray(delta.tool_calls)) {
191
+ processToolCallDeltas(delta.tool_calls, pendingToolCalls);
192
+ }
193
+ };
194
+ var processChoice = function* (choice, pendingToolCalls) {
195
+ const delta = isRecord(choice.delta) ? choice.delta : null;
196
+ if (delta) {
197
+ yield* processDelta(delta, pendingToolCalls);
198
+ }
199
+ if (choice.finish_reason === "tool_calls") {
200
+ yield* flushPendingToolCalls(pendingToolCalls);
201
+ }
202
+ };
203
+ var narrowUsageRecord = (parsed) => {
204
+ if (!isRecord(parsed.usage)) {
205
+ return;
206
+ }
207
+ const { usage } = parsed;
208
+ const promptTokens = typeof usage.prompt_tokens === "number" ? usage.prompt_tokens : 0;
209
+ const completionTokens = typeof usage.completion_tokens === "number" ? usage.completion_tokens : 0;
210
+ return extractUsage({
211
+ completion_tokens: completionTokens,
212
+ prompt_tokens: promptTokens
213
+ });
214
+ };
215
+ var processSSELine = function* (line, pendingToolCalls, currentUsage) {
216
+ const trimmed = line.trim();
217
+ if (!trimmed || !trimmed.startsWith("data: ")) {
218
+ return;
219
+ }
220
+ const data = trimmed.slice(SSE_DATA_PREFIX_LENGTH);
221
+ if (data === DONE_SENTINEL) {
222
+ yield* flushPendingToolCalls(pendingToolCalls);
223
+ yield { type: "done", usage: currentUsage };
224
+ return;
225
+ }
226
+ let parsed;
227
+ try {
228
+ parsed = JSON.parse(data);
229
+ } catch {
230
+ return;
231
+ }
232
+ const usageUpdate = narrowUsageRecord(parsed);
233
+ if (usageUpdate) {
234
+ yield { type: "usage_update", usage: usageUpdate };
235
+ }
236
+ const { choices } = parsed;
237
+ if (!isRecordArray(choices)) {
238
+ return;
239
+ }
240
+ const [firstChoice] = choices;
241
+ if (!firstChoice) {
242
+ return;
243
+ }
244
+ yield* processChoice(firstChoice, pendingToolCalls);
245
+ };
246
+ var isUsageUpdate = (chunk) => chunk.type === "usage_update";
247
+ var collectYieldableChunks = (line, pendingToolCalls, usageRef) => {
248
+ const allChunks = Array.from(processSSELine(line, pendingToolCalls, usageRef.current));
249
+ const usageChunks = allChunks.filter(isUsageUpdate);
250
+ const lastUsage = usageChunks.at(NOT_FOUND);
251
+ if (lastUsage) {
252
+ usageRef.current = lastUsage.usage;
253
+ }
254
+ return allChunks.filter((chunk) => !isUsageUpdate(chunk));
255
+ };
256
+ var processSSELines = function* (lines, pendingToolCalls, usageRef) {
257
+ for (const line of lines) {
258
+ yield* collectYieldableChunks(line, pendingToolCalls, usageRef);
259
+ }
260
+ };
261
+ var processStreamValue = (value, decoder, state) => {
262
+ state.buffer += decoder.decode(value, { stream: true });
263
+ const lines = state.buffer.split(`
264
+ `);
265
+ state.buffer = lines.pop() ?? "";
266
+ return lines;
267
+ };
268
+ var drainReader = async function* (reader, decoder, state, signal) {
269
+ for (let result = await reader.read();!result.done && !signal?.aborted; result = await reader.read()) {
270
+ const lines = processStreamValue(result.value, decoder, state);
271
+ yield* processSSELines(lines, state.pendingToolCalls, state.usageRef);
272
+ }
273
+ };
274
+ var parseSSEStream = async function* (body, signal) {
275
+ const reader = body.getReader();
276
+ const decoder = new TextDecoder;
277
+ const state = {
278
+ buffer: "",
279
+ pendingToolCalls: new Map,
280
+ usageRef: { current: undefined }
281
+ };
282
+ try {
283
+ yield* drainReader(reader, decoder, state, signal);
284
+ yield { type: "done", usage: state.usageRef.current };
285
+ } finally {
286
+ reader.releaseLock();
287
+ }
288
+ };
289
+ var fetchOpenAIStream = async function* (baseUrl, apiKey, body, signal) {
290
+ const response = await fetch(`${baseUrl}/v1/chat/completions`, {
291
+ body: JSON.stringify(body),
292
+ headers: {
293
+ Authorization: `Bearer ${apiKey}`,
294
+ "Content-Type": "application/json"
295
+ },
296
+ method: "POST",
297
+ signal
298
+ });
299
+ if (!response.ok) {
300
+ const errorText = await response.text();
301
+ throw new Error(`OpenAI API error ${response.status}: ${errorText}`);
302
+ }
303
+ if (!response.body) {
304
+ throw new Error("OpenAI API returned no response body");
305
+ }
306
+ yield* parseSSEStream(response.body, signal);
307
+ };
308
+ var openai = (config) => {
309
+ const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
310
+ return {
311
+ stream: (params) => {
312
+ const body = buildRequestBody(params);
313
+ return fetchOpenAIStream(baseUrl, config.apiKey, body, params.signal);
314
+ }
315
+ };
316
+ };
317
+
318
+ // src/ai/providers/openaiCompatible.ts
319
+ var alibaba = (config) => openaiCompatible({
320
+ apiKey: config.apiKey,
321
+ baseUrl: "https://dashscope-intl.aliyuncs.com/compatible-mode"
322
+ });
323
+ var deepseek = (config) => openaiCompatible({
324
+ apiKey: config.apiKey,
325
+ baseUrl: "https://api.deepseek.com"
326
+ });
327
+ var google = (config) => openaiCompatible({
328
+ apiKey: config.apiKey,
329
+ baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai"
330
+ });
331
+ var meta = (config) => openaiCompatible({
332
+ apiKey: config.apiKey,
333
+ baseUrl: "https://api.llama.com/compat/v1"
334
+ });
335
+ var mistralai = (config) => openaiCompatible({
336
+ apiKey: config.apiKey,
337
+ baseUrl: "https://api.mistral.ai"
338
+ });
339
+ var moonshot = (config) => openaiCompatible({
340
+ apiKey: config.apiKey,
341
+ baseUrl: "https://api.moonshot.ai"
342
+ });
343
+ var openaiCompatible = (config) => openai({ apiKey: config.apiKey, baseUrl: config.baseUrl });
344
+ var xai = (config) => openaiCompatible({
345
+ apiKey: config.apiKey,
346
+ baseUrl: "https://api.x.ai"
347
+ });
348
+ export {
349
+ xai,
350
+ openaiCompatible,
351
+ moonshot,
352
+ mistralai,
353
+ meta,
354
+ google,
355
+ deepseek,
356
+ alibaba
357
+ };
358
+
359
+ //# debugId=C1002ED4459EEC0F64756E2164756E21
360
+ //# sourceMappingURL=openaiCompatible.js.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/ai/providers/openai.ts", "../src/ai/providers/openaiCompatible.ts"],
4
+ "sourcesContent": [
5
+ "import type {\n\tAIProviderConfig,\n\tAIProviderContentBlock,\n\tAIProviderMessage,\n\tAIProviderStreamParams,\n\tAIProviderToolDefinition,\n\tAIUsage\n} from '../../../types/ai';\n\ntype OpenAIConfig = {\n\tapiKey: string;\n\tbaseUrl?: string;\n};\n\ntype OpenAIMessage = {\n\tcontent: string | Array<Record<string, unknown>> | null;\n\trole: 'user' | 'assistant' | 'system' | 'tool';\n\ttool_call_id?: string;\n\ttool_calls?: Array<{\n\t\tfunction: { arguments: string; name: string };\n\t\tid: string;\n\t\ttype: 'function';\n\t}>;\n};\n\ntype PendingToolCall = {\n\targuments: string;\n\tid: string;\n\tname: string;\n};\n\ntype UsageRef = {\n\tcurrent: AIUsage | undefined;\n};\n\ntype StreamState = {\n\tbuffer: string;\n\tpendingToolCalls: Map<number, PendingToolCall>;\n\tusageRef: UsageRef;\n};\n\nconst DEFAULT_BASE_URL = 'https://api.openai.com';\nconst SSE_DATA_PREFIX_LENGTH = 6;\nconst DONE_SENTINEL = '[DONE]';\nconst NOT_FOUND = -1;\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n\ttypeof value === 'object' && value !== null;\n\nconst isRecordArray = (\n\tvalue: unknown\n): value is Array<Record<string, unknown>> =>\n\tArray.isArray(value) && value.length > 0 && isRecord(value[0]);\n\nconst hasArrayContent = (\n\tmsg: AIProviderMessage\n): msg is AIProviderMessage & { content: AIProviderContentBlock[] } =>\n\ttypeof msg.content !== 'string' && Array.isArray(msg.content);\n\nconst buildToolMessages = (blocks: AIProviderContentBlock[]) => {\n\tconst toolUseBlocks = blocks.filter((block) => block.type === 'tool_use');\n\tconst toolResultBlocks = blocks.filter(\n\t\t(block) => block.type === 'tool_result'\n\t);\n\tconst messages: OpenAIMessage[] = [];\n\n\tif (toolUseBlocks.length > 0) {\n\t\tmessages.push({\n\t\t\tcontent: null,\n\t\t\trole: 'assistant',\n\t\t\ttool_calls: toolUseBlocks.map((block) => ({\n\t\t\t\tfunction: {\n\t\t\t\t\targuments:\n\t\t\t\t\t\ttypeof block.input === 'string'\n\t\t\t\t\t\t\t? block.input\n\t\t\t\t\t\t\t: JSON.stringify(block.input),\n\t\t\t\t\tname: block.name\n\t\t\t\t},\n\t\t\t\tid: block.id,\n\t\t\t\ttype: 'function' as const\n\t\t\t}))\n\t\t});\n\t}\n\n\tfor (const result of toolResultBlocks) {\n\t\tmessages.push({\n\t\t\tcontent: typeof result.content === 'string' ? result.content : '',\n\t\t\trole: 'tool',\n\t\t\ttool_call_id: result.tool_use_id\n\t\t});\n\t}\n\n\treturn messages;\n};\n\nconst processMessageAtIndex = (\n\tresult: OpenAIMessage[],\n\tmsg: AIProviderMessage,\n\tidx: number\n) => {\n\tif (!hasArrayContent(msg)) {\n\t\treturn;\n\t}\n\n\tconst hasToolBlocks = msg.content.some(\n\t\t(block) => block.type === 'tool_use' || block.type === 'tool_result'\n\t);\n\n\tif (!hasToolBlocks) {\n\t\treturn;\n\t}\n\n\tconst toolMessages = buildToolMessages(msg.content);\n\tresult.splice(idx, 1, ...toolMessages);\n};\n\nconst convertSingleMessage = (\n\tresult: OpenAIMessage[],\n\tmsg: AIProviderMessage | undefined,\n\tidx: number\n) => {\n\tif (!msg) {\n\t\treturn;\n\t}\n\n\tprocessMessageAtIndex(result, msg, idx);\n};\n\nconst convertToolResultMessages = (\n\tmessages: OpenAIMessage[],\n\tparams: AIProviderStreamParams\n) => {\n\tconst result = [...messages];\n\n\tfor (let idx = 0; idx < params.messages.length; idx++) {\n\t\tconvertSingleMessage(result, params.messages[idx], idx);\n\t}\n\n\treturn result;\n};\n\nconst mapToolDefinitions = (tools: AIProviderToolDefinition[]) =>\n\ttools.map((tool) => ({\n\t\tfunction: {\n\t\t\tdescription: tool.description,\n\t\t\tname: tool.name,\n\t\t\tparameters: tool.input_schema\n\t\t},\n\t\ttype: 'function'\n\t}));\n\nconst mapContentBlockToOpenAI = (block: AIProviderContentBlock) => {\n\tif (block.type === 'image') {\n\t\treturn {\n\t\t\timage_url: {\n\t\t\t\turl: `data:${block.source.media_type};base64,${block.source.data}`\n\t\t\t},\n\t\t\ttype: 'image_url'\n\t\t};\n\t}\n\n\tif (block.type === 'document') {\n\t\treturn {\n\t\t\tfile: {\n\t\t\t\tfile_data: `data:${block.source.media_type};base64,${block.source.data}`,\n\t\t\t\tfilename: block.name ?? 'document.pdf'\n\t\t\t},\n\t\t\ttype: 'file'\n\t\t};\n\t}\n\n\tif (block.type === 'text') {\n\t\treturn { text: block.content, type: 'text' };\n\t}\n\n\treturn null;\n};\n\nconst mapOpenAIContent = (msg: AIProviderStreamParams['messages'][number]) => {\n\tif (typeof msg.content === 'string') {\n\t\treturn msg.content;\n\t}\n\n\tconst hasMedia = msg.content.some(\n\t\t(block) => block.type === 'image' || block.type === 'document'\n\t);\n\n\tif (!hasMedia) {\n\t\treturn null;\n\t}\n\n\treturn msg.content\n\t\t.map(mapContentBlockToOpenAI)\n\t\t.filter((mapped) => mapped !== null);\n};\n\nconst buildRequestBody = (params: AIProviderStreamParams) => {\n\tconst messages = convertToolResultMessages(\n\t\tparams.messages.map((msg) => ({\n\t\t\tcontent: mapOpenAIContent(msg),\n\t\t\trole: msg.role\n\t\t})),\n\t\tparams\n\t);\n\n\tconst body: Record<string, unknown> = {\n\t\tmessages,\n\t\tmodel: params.model,\n\t\tstream: true,\n\t\tstream_options: { include_usage: true }\n\t};\n\n\tif (params.tools && params.tools.length > 0) {\n\t\tbody.tools = mapToolDefinitions(params.tools);\n\t}\n\n\treturn body;\n};\n\nconst parseToolInput = (rawArguments: string) => {\n\ttry {\n\t\treturn JSON.parse(rawArguments);\n\t} catch {\n\t\treturn rawArguments;\n\t}\n};\n\nconst flushPendingToolCalls = function* (\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tfor (const [, tool] of pendingToolCalls) {\n\t\tconst input = parseToolInput(tool.arguments);\n\t\tyield {\n\t\t\tid: tool.id,\n\t\t\tinput,\n\t\t\tname: tool.name,\n\t\t\ttype: 'tool_use' as const\n\t\t};\n\t}\n\n\tpendingToolCalls.clear();\n};\n\nconst extractUsage = (parsedUsage: Record<string, number>) => ({\n\tinputTokens: parsedUsage.prompt_tokens ?? 0,\n\toutputTokens: parsedUsage.completion_tokens ?? 0\n});\n\nconst resolveToolCallIndex = (toolCall: Record<string, unknown>) => {\n\tconst raw = typeof toolCall.index === 'number' ? toolCall.index : NOT_FOUND;\n\n\treturn raw < 0 ? undefined : raw;\n};\n\nconst initPendingToolCall = (\n\ttoolCall: Record<string, unknown>,\n\tfunc: Record<string, unknown> | null,\n\tindex: number,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tif (pendingToolCalls.has(index)) {\n\t\treturn;\n\t}\n\n\tconst toolId = typeof toolCall.id === 'string' ? toolCall.id : '';\n\tconst toolName = func && typeof func.name === 'string' ? func.name : '';\n\n\tpendingToolCalls.set(index, {\n\t\targuments: '',\n\t\tid: toolId,\n\t\tname: toolName\n\t});\n};\n\nconst updatePendingToolCall = (\n\ttoolCall: Record<string, unknown>,\n\tfunc: Record<string, unknown> | null,\n\tpending: PendingToolCall\n) => {\n\tif (typeof toolCall.id === 'string') {\n\t\tpending.id = toolCall.id;\n\t}\n\n\tif (func && typeof func.name === 'string') {\n\t\tpending.name = func.name;\n\t}\n\n\tif (func && typeof func.arguments === 'string') {\n\t\tpending.arguments += func.arguments;\n\t}\n};\n\nconst processToolCallDelta = (\n\ttoolCall: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tconst index = resolveToolCallIndex(toolCall);\n\tif (index === undefined) {\n\t\treturn;\n\t}\n\n\tconst func = isRecord(toolCall.function) ? toolCall.function : null;\n\tinitPendingToolCall(toolCall, func, index, pendingToolCalls);\n\n\tconst pending = pendingToolCalls.get(index);\n\tif (!pending) {\n\t\treturn;\n\t}\n\n\tupdatePendingToolCall(toolCall, func, pending);\n};\n\nconst processToolCallDeltas = (\n\ttoolCalls: Array<Record<string, unknown>>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) => {\n\tfor (const toolCall of toolCalls) {\n\t\tprocessToolCallDelta(toolCall, pendingToolCalls);\n\t}\n};\n\nconst processDelta = function* (\n\tdelta: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tif (typeof delta.content === 'string') {\n\t\tyield { content: delta.content, type: 'text' as const };\n\t}\n\n\tif (isRecordArray(delta.tool_calls)) {\n\t\tprocessToolCallDeltas(delta.tool_calls, pendingToolCalls);\n\t}\n};\n\nconst processChoice = function* (\n\tchoice: Record<string, unknown>,\n\tpendingToolCalls: Map<number, PendingToolCall>\n) {\n\tconst delta = isRecord(choice.delta) ? choice.delta : null;\n\tif (delta) {\n\t\tyield* processDelta(delta, pendingToolCalls);\n\t}\n\n\tif (choice.finish_reason === 'tool_calls') {\n\t\tyield* flushPendingToolCalls(pendingToolCalls);\n\t}\n};\n\nconst narrowUsageRecord = (parsed: Record<string, unknown>) => {\n\tif (!isRecord(parsed.usage)) {\n\t\treturn undefined;\n\t}\n\n\tconst { usage } = parsed;\n\tconst promptTokens =\n\t\ttypeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0;\n\tconst completionTokens =\n\t\ttypeof usage.completion_tokens === 'number'\n\t\t\t? usage.completion_tokens\n\t\t\t: 0;\n\n\treturn extractUsage({\n\t\tcompletion_tokens: completionTokens,\n\t\tprompt_tokens: promptTokens\n\t});\n};\n\nconst processSSELine = function* (\n\tline: string,\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tcurrentUsage: AIUsage | undefined\n) {\n\tconst trimmed = line.trim();\n\tif (!trimmed || !trimmed.startsWith('data: ')) {\n\t\treturn;\n\t}\n\n\tconst data = trimmed.slice(SSE_DATA_PREFIX_LENGTH);\n\tif (data === DONE_SENTINEL) {\n\t\tyield* flushPendingToolCalls(pendingToolCalls);\n\t\tyield { type: 'done' as const, usage: currentUsage };\n\n\t\treturn;\n\t}\n\n\tlet parsed: Record<string, unknown>;\n\ttry {\n\t\tparsed = JSON.parse(data);\n\t} catch {\n\t\treturn;\n\t}\n\n\tconst usageUpdate = narrowUsageRecord(parsed);\n\tif (usageUpdate) {\n\t\tyield { type: 'usage_update' as const, usage: usageUpdate };\n\t}\n\n\tconst { choices } = parsed;\n\tif (!isRecordArray(choices)) {\n\t\treturn;\n\t}\n\n\tconst [firstChoice] = choices;\n\tif (!firstChoice) {\n\t\treturn;\n\t}\n\n\tyield* processChoice(firstChoice, pendingToolCalls);\n};\n\nconst isUsageUpdate = (chunk: {\n\ttype: string;\n\tusage?: AIUsage;\n}): chunk is { type: 'usage_update'; usage: AIUsage } =>\n\tchunk.type === 'usage_update';\n\nconst collectYieldableChunks = (\n\tline: string,\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tusageRef: UsageRef\n) => {\n\tconst allChunks = Array.from(\n\t\tprocessSSELine(line, pendingToolCalls, usageRef.current)\n\t);\n\tconst usageChunks = allChunks.filter(isUsageUpdate);\n\tconst lastUsage = usageChunks.at(NOT_FOUND);\n\n\tif (lastUsage) {\n\t\tusageRef.current = lastUsage.usage;\n\t}\n\n\treturn allChunks.filter((chunk) => !isUsageUpdate(chunk));\n};\n\nconst processSSELines = function* (\n\tlines: string[],\n\tpendingToolCalls: Map<number, PendingToolCall>,\n\tusageRef: UsageRef\n) {\n\tfor (const line of lines) {\n\t\tyield* collectYieldableChunks(line, pendingToolCalls, usageRef);\n\t}\n};\n\nconst processStreamValue = (\n\tvalue: Uint8Array,\n\tdecoder: TextDecoder,\n\tstate: StreamState\n) => {\n\tstate.buffer += decoder.decode(value, { stream: true });\n\tconst lines = state.buffer.split('\\n');\n\tstate.buffer = lines.pop() ?? '';\n\n\treturn lines;\n};\n\nconst drainReader = async function* (\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n\tdecoder: TextDecoder,\n\tstate: StreamState,\n\tsignal?: AbortSignal\n) {\n\t/* eslint-disable no-await-in-loop */\n\tfor (\n\t\tlet result = await reader.read();\n\t\t!result.done && !signal?.aborted;\n\t\tresult = await reader.read()\n\t) {\n\t\t/* eslint-enable no-await-in-loop */\n\t\tconst lines = processStreamValue(result.value, decoder, state);\n\t\tyield* processSSELines(lines, state.pendingToolCalls, state.usageRef);\n\t}\n};\n\nconst parseSSEStream = async function* (\n\tbody: ReadableStream<Uint8Array>,\n\tsignal?: AbortSignal\n) {\n\tconst reader = body.getReader();\n\tconst decoder = new TextDecoder();\n\tconst state: StreamState = {\n\t\tbuffer: '',\n\t\tpendingToolCalls: new Map<number, PendingToolCall>(),\n\t\tusageRef: { current: undefined }\n\t};\n\n\ttry {\n\t\tyield* drainReader(reader, decoder, state, signal);\n\t\tyield { type: 'done' as const, usage: state.usageRef.current };\n\t} finally {\n\t\treader.releaseLock();\n\t}\n};\n\nconst fetchOpenAIStream = async function* (\n\tbaseUrl: string,\n\tapiKey: string,\n\tbody: Record<string, unknown>,\n\tsignal?: AbortSignal\n) {\n\tconst response = await fetch(`${baseUrl}/v1/chat/completions`, {\n\t\tbody: JSON.stringify(body),\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t'Content-Type': 'application/json'\n\t\t},\n\t\tmethod: 'POST',\n\t\tsignal\n\t});\n\n\tif (!response.ok) {\n\t\tconst errorText = await response.text();\n\t\tthrow new Error(`OpenAI API error ${response.status}: ${errorText}`);\n\t}\n\n\tif (!response.body) {\n\t\tthrow new Error('OpenAI API returned no response body');\n\t}\n\n\tyield* parseSSEStream(response.body, signal);\n};\n\nexport const openai = (config: OpenAIConfig): AIProviderConfig => {\n\tconst baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;\n\n\treturn {\n\t\tstream: (params: AIProviderStreamParams) => {\n\t\t\tconst body = buildRequestBody(params);\n\n\t\t\treturn fetchOpenAIStream(\n\t\t\t\tbaseUrl,\n\t\t\t\tconfig.apiKey,\n\t\t\t\tbody,\n\t\t\t\tparams.signal\n\t\t\t);\n\t\t}\n\t};\n};\n",
6
+ "import { openai } from './openai';\n\n/**\n * Creates a provider for any OpenAI-compatible API.\n * Many providers (Google, xAI, DeepSeek, Mistral, etc.)\n * expose OpenAI-compatible chat completion endpoints.\n */\nexport const alibaba = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://dashscope-intl.aliyuncs.com/compatible-mode'\n\t});\nexport const deepseek = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://api.deepseek.com'\n\t});\nexport const google = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai'\n\t});\nexport const meta = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://api.llama.com/compat/v1'\n\t});\nexport const mistralai = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://api.mistral.ai'\n\t});\nexport const moonshot = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://api.moonshot.ai'\n\t});\nexport const openaiCompatible = (config: { apiKey: string; baseUrl: string }) =>\n\topenai({ apiKey: config.apiKey, baseUrl: config.baseUrl });\nexport const xai = (config: { apiKey: string }) =>\n\topenaiCompatible({\n\t\tapiKey: config.apiKey,\n\t\tbaseUrl: 'https://api.x.ai'\n\t});\n"
7
+ ],
8
+ "mappings": ";;AAyCA,IAAM,mBAAmB;AACzB,IAAM,yBAAyB;AAC/B,IAAM,gBAAgB;AACtB,IAAM,YAAY;AAElB,IAAM,WAAW,CAAC,UACjB,OAAO,UAAU,YAAY,UAAU;AAExC,IAAM,gBAAgB,CACrB,UAEA,MAAM,QAAQ,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS,MAAM,EAAE;AAE9D,IAAM,kBAAkB,CACvB,QAEA,OAAO,IAAI,YAAY,YAAY,MAAM,QAAQ,IAAI,OAAO;AAE7D,IAAM,oBAAoB,CAAC,WAAqC;AAAA,EAC/D,MAAM,gBAAgB,OAAO,OAAO,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EACxE,MAAM,mBAAmB,OAAO,OAC/B,CAAC,UAAU,MAAM,SAAS,aAC3B;AAAA,EACA,MAAM,WAA4B,CAAC;AAAA,EAEnC,IAAI,cAAc,SAAS,GAAG;AAAA,IAC7B,SAAS,KAAK;AAAA,MACb,SAAS;AAAA,MACT,MAAM;AAAA,MACN,YAAY,cAAc,IAAI,CAAC,WAAW;AAAA,QACzC,UAAU;AAAA,UACT,WACC,OAAO,MAAM,UAAU,WACpB,MAAM,QACN,KAAK,UAAU,MAAM,KAAK;AAAA,UAC9B,MAAM,MAAM;AAAA,QACb;AAAA,QACA,IAAI,MAAM;AAAA,QACV,MAAM;AAAA,MACP,EAAE;AAAA,IACH,CAAC;AAAA,EACF;AAAA,EAEA,W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