@providerprotocol/ai 0.0.33 → 0.0.35

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 (133) hide show
  1. package/README.md +542 -3
  2. package/dist/anthropic/index.d.ts +2 -1
  3. package/dist/anthropic/index.js +151 -145
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/cerebras/index.d.ts +392 -0
  6. package/dist/cerebras/index.js +648 -0
  7. package/dist/cerebras/index.js.map +1 -0
  8. package/dist/chunk-3GWM5GR3.js +153 -0
  9. package/dist/chunk-3GWM5GR3.js.map +1 -0
  10. package/dist/chunk-4OGB7JZA.js +157 -0
  11. package/dist/chunk-4OGB7JZA.js.map +1 -0
  12. package/dist/chunk-7DXVRILR.js +49 -0
  13. package/dist/chunk-7DXVRILR.js.map +1 -0
  14. package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
  15. package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
  16. package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
  17. package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
  18. package/dist/chunk-AY55T37A.js.map +1 -0
  19. package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
  20. package/dist/chunk-BRP5XJ6Q.js.map +1 -0
  21. package/dist/chunk-C4JP64VW.js +298 -0
  22. package/dist/chunk-C4JP64VW.js.map +1 -0
  23. package/dist/chunk-COS4ON4G.js +111 -0
  24. package/dist/chunk-COS4ON4G.js.map +1 -0
  25. package/dist/chunk-ETBFOLQN.js +34 -0
  26. package/dist/chunk-ETBFOLQN.js.map +1 -0
  27. package/dist/chunk-HB4ZIH3T.js +31 -0
  28. package/dist/chunk-HB4ZIH3T.js.map +1 -0
  29. package/dist/chunk-I53CI6ZZ.js +142 -0
  30. package/dist/chunk-I53CI6ZZ.js.map +1 -0
  31. package/dist/chunk-IDZOVWP3.js +29 -0
  32. package/dist/chunk-IDZOVWP3.js.map +1 -0
  33. package/dist/chunk-JA3UZALR.js +88 -0
  34. package/dist/chunk-JA3UZALR.js.map +1 -0
  35. package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
  36. package/dist/chunk-N5DX5JW3.js.map +1 -0
  37. package/dist/chunk-OIEWDFQU.js +97 -0
  38. package/dist/chunk-OIEWDFQU.js.map +1 -0
  39. package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
  40. package/dist/chunk-PMK5LZ5Z.js.map +1 -0
  41. package/dist/chunk-UFFJDYCE.js +94 -0
  42. package/dist/chunk-UFFJDYCE.js.map +1 -0
  43. package/dist/chunk-VGKZIGVI.js +222 -0
  44. package/dist/chunk-VGKZIGVI.js.map +1 -0
  45. package/dist/chunk-VOEWHQUB.js +31 -0
  46. package/dist/chunk-VOEWHQUB.js.map +1 -0
  47. package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
  48. package/dist/chunk-ZI67WIQS.js +30 -0
  49. package/dist/chunk-ZI67WIQS.js.map +1 -0
  50. package/dist/{embedding-D2BYIehX.d.ts → embedding-CW6SaOOz.d.ts} +1 -1
  51. package/dist/google/index.d.ts +2 -1
  52. package/dist/google/index.js +202 -199
  53. package/dist/google/index.js.map +1 -1
  54. package/dist/groq/index.d.ts +410 -0
  55. package/dist/groq/index.js +649 -0
  56. package/dist/groq/index.js.map +1 -0
  57. package/dist/http/index.d.ts +3 -2
  58. package/dist/http/index.js +5 -4
  59. package/dist/image-stream-C0ciACM2.d.ts +11 -0
  60. package/dist/index.d.ts +8 -118
  61. package/dist/index.js +518 -767
  62. package/dist/index.js.map +1 -1
  63. package/dist/{llm-BQJZj3cD.d.ts → llm-DwbUK7un.d.ts} +12 -1632
  64. package/dist/middleware/logging/index.d.ts +76 -0
  65. package/dist/middleware/logging/index.js +74 -0
  66. package/dist/middleware/logging/index.js.map +1 -0
  67. package/dist/middleware/parsed-object/index.d.ts +45 -0
  68. package/dist/middleware/parsed-object/index.js +73 -0
  69. package/dist/middleware/parsed-object/index.js.map +1 -0
  70. package/dist/middleware/pubsub/index.d.ts +104 -0
  71. package/dist/middleware/pubsub/index.js +230 -0
  72. package/dist/middleware/pubsub/index.js.map +1 -0
  73. package/dist/middleware/pubsub/server/express/index.d.ts +52 -0
  74. package/dist/middleware/pubsub/server/express/index.js +11 -0
  75. package/dist/middleware/pubsub/server/express/index.js.map +1 -0
  76. package/dist/middleware/pubsub/server/fastify/index.d.ts +53 -0
  77. package/dist/middleware/pubsub/server/fastify/index.js +11 -0
  78. package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
  79. package/dist/middleware/pubsub/server/h3/index.d.ts +56 -0
  80. package/dist/middleware/pubsub/server/h3/index.js +11 -0
  81. package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
  82. package/dist/middleware/pubsub/server/index.d.ts +78 -0
  83. package/dist/middleware/pubsub/server/index.js +34 -0
  84. package/dist/middleware/pubsub/server/index.js.map +1 -0
  85. package/dist/middleware/pubsub/server/webapi/index.d.ts +53 -0
  86. package/dist/middleware/pubsub/server/webapi/index.js +11 -0
  87. package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
  88. package/dist/ollama/index.d.ts +2 -1
  89. package/dist/ollama/index.js +48 -45
  90. package/dist/ollama/index.js.map +1 -1
  91. package/dist/openai/index.d.ts +2 -1
  92. package/dist/openai/index.js +319 -313
  93. package/dist/openai/index.js.map +1 -1
  94. package/dist/openrouter/index.d.ts +2 -1
  95. package/dist/openrouter/index.js +379 -383
  96. package/dist/openrouter/index.js.map +1 -1
  97. package/dist/proxy/index.d.ts +10 -914
  98. package/dist/proxy/index.js +275 -1007
  99. package/dist/proxy/index.js.map +1 -1
  100. package/dist/proxy/server/express/index.d.ts +161 -0
  101. package/dist/proxy/server/express/index.js +24 -0
  102. package/dist/proxy/server/express/index.js.map +1 -0
  103. package/dist/proxy/server/fastify/index.d.ts +162 -0
  104. package/dist/proxy/server/fastify/index.js +24 -0
  105. package/dist/proxy/server/fastify/index.js.map +1 -0
  106. package/dist/proxy/server/h3/index.d.ts +189 -0
  107. package/dist/proxy/server/h3/index.js +28 -0
  108. package/dist/proxy/server/h3/index.js.map +1 -0
  109. package/dist/proxy/server/index.d.ts +151 -0
  110. package/dist/proxy/server/index.js +48 -0
  111. package/dist/proxy/server/index.js.map +1 -0
  112. package/dist/proxy/server/webapi/index.d.ts +278 -0
  113. package/dist/proxy/server/webapi/index.js +32 -0
  114. package/dist/proxy/server/webapi/index.js.map +1 -0
  115. package/dist/responses/index.d.ts +650 -0
  116. package/dist/responses/index.js +930 -0
  117. package/dist/responses/index.js.map +1 -0
  118. package/dist/{retry-8Ch-WWgX.d.ts → retry-YayV42GV.d.ts} +1 -1
  119. package/dist/stream-CecfVCPO.d.ts +1632 -0
  120. package/dist/types-C8Gciizr.d.ts +168 -0
  121. package/dist/utils/index.d.ts +53 -0
  122. package/dist/utils/index.js +7 -0
  123. package/dist/utils/index.js.map +1 -0
  124. package/dist/xai/index.d.ts +2 -1
  125. package/dist/xai/index.js +310 -310
  126. package/dist/xai/index.js.map +1 -1
  127. package/package.json +94 -4
  128. package/dist/chunk-4J6OFUKX.js.map +0 -1
  129. package/dist/chunk-ILR2D5PN.js.map +0 -1
  130. package/dist/chunk-TOJCZMVU.js.map +0 -1
  131. package/dist/chunk-WAKD3OO5.js.map +0 -1
  132. /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
  133. /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
@@ -1,97 +1,45 @@
1
+ import {
2
+ parseSSEStream
3
+ } from "../chunk-PMK5LZ5Z.js";
1
4
  import {
2
5
  parseJsonResponse
3
- } from "../chunk-3C7O2RNO.js";
6
+ } from "../chunk-A2IM7PGT.js";
4
7
  import {
5
- StreamEventType,
6
- objectDelta
7
- } from "../chunk-6S222DHN.js";
8
+ resolveApiKey
9
+ } from "../chunk-ARVM24K2.js";
10
+ import {
11
+ createProvider
12
+ } from "../chunk-JA3UZALR.js";
8
13
  import {
9
14
  AssistantMessage,
10
- createProvider,
11
15
  generateId,
12
16
  isAssistantMessage,
13
17
  isToolResultMessage,
14
18
  isUserMessage
15
- } from "../chunk-ILR2D5PN.js";
16
- import {
17
- parseSSEStream
18
- } from "../chunk-TOJCZMVU.js";
19
- import {
20
- resolveApiKey
21
- } from "../chunk-3D6XGGVG.js";
19
+ } from "../chunk-BRP5XJ6Q.js";
22
20
  import {
23
- ErrorCode,
24
- ModalityType,
25
- UPPError,
26
21
  doFetch,
27
22
  doStreamFetch,
28
23
  normalizeHttpError,
29
24
  toError
30
- } from "../chunk-4J6OFUKX.js";
25
+ } from "../chunk-AY55T37A.js";
26
+ import {
27
+ ErrorCode,
28
+ ModalityType,
29
+ UPPError
30
+ } from "../chunk-COS4ON4G.js";
31
+ import {
32
+ StreamEventType,
33
+ objectDelta
34
+ } from "../chunk-6S222DHN.js";
31
35
 
32
36
  // src/providers/openrouter/transform.completions.ts
33
- function transformRequest(request, modelId) {
34
- const params = request.params ?? {};
35
- const openrouterRequest = {
36
- ...params,
37
- model: modelId,
38
- messages: transformMessages(request.messages, request.system)
39
- };
40
- if (request.tools && request.tools.length > 0) {
41
- openrouterRequest.tools = request.tools.map(transformTool);
42
- }
43
- if (request.structure) {
44
- const schema = {
45
- type: "object",
46
- properties: request.structure.properties,
47
- required: request.structure.required,
48
- ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
49
- };
50
- if (request.structure.description) {
51
- schema.description = request.structure.description;
52
- }
53
- openrouterRequest.response_format = {
54
- type: "json_schema",
55
- json_schema: {
56
- name: "json_response",
57
- description: request.structure.description,
58
- schema,
59
- strict: true
60
- }
61
- };
62
- }
63
- return openrouterRequest;
64
- }
65
- function transformMessages(messages, system) {
66
- const result = [];
67
- if (system !== void 0 && system !== null) {
68
- const normalizedSystem = normalizeSystem(system);
69
- if (typeof normalizedSystem === "string") {
70
- if (normalizedSystem.length > 0) {
71
- result.push({
72
- role: "system",
73
- content: normalizedSystem
74
- });
75
- }
76
- } else if (normalizedSystem.length > 0) {
77
- result.push({
78
- role: "system",
79
- content: normalizedSystem
80
- });
81
- }
82
- }
83
- for (const message of messages) {
84
- if (isToolResultMessage(message)) {
85
- const toolMessages = transformToolResults(message);
86
- result.push(...toolMessages);
87
- } else {
88
- const transformed = transformMessage(message);
89
- if (transformed) {
90
- result.push(transformed);
91
- }
92
- }
93
- }
94
- return result;
37
+ function isValidCacheControl(value) {
38
+ if (!value || typeof value !== "object") return false;
39
+ const candidate = value;
40
+ if (candidate.type !== "ephemeral") return false;
41
+ if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
42
+ return true;
95
43
  }
96
44
  function normalizeSystem(system) {
97
45
  if (typeof system === "string") return system;
@@ -134,13 +82,6 @@ function normalizeSystem(system) {
134
82
  }
135
83
  return blocks;
136
84
  }
137
- function isValidCacheControl(value) {
138
- if (!value || typeof value !== "object") return false;
139
- const candidate = value;
140
- if (candidate.type !== "ephemeral") return false;
141
- if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
142
- return true;
143
- }
144
85
  function filterValidContent(content) {
145
86
  return content.filter((c) => c && typeof c.type === "string");
146
87
  }
@@ -148,77 +89,6 @@ function extractCacheControl(message) {
148
89
  const openrouterMeta = message.metadata?.openrouter;
149
90
  return openrouterMeta?.cache_control;
150
91
  }
151
- function transformMessage(message) {
152
- if (isUserMessage(message)) {
153
- const validContent = filterValidContent(message.content);
154
- const cacheControl = extractCacheControl(message);
155
- if (cacheControl) {
156
- const content = validContent.map(transformContentBlock);
157
- for (let i = content.length - 1; i >= 0; i--) {
158
- const block = content[i];
159
- if (block?.type === "text") {
160
- content[i] = { type: "text", text: block.text, cache_control: cacheControl };
161
- break;
162
- }
163
- }
164
- return { role: "user", content };
165
- }
166
- if (validContent.length === 1 && validContent[0]?.type === "text") {
167
- return {
168
- role: "user",
169
- content: validContent[0].text
170
- };
171
- }
172
- return {
173
- role: "user",
174
- content: validContent.map(transformContentBlock)
175
- };
176
- }
177
- if (isAssistantMessage(message)) {
178
- const validContent = filterValidContent(message.content);
179
- const nonReasoningContent = validContent.filter((c) => c.type !== "reasoning");
180
- const textContent = nonReasoningContent.filter((c) => c.type === "text").map((c) => c.text).join("");
181
- const assistantMessage = {
182
- role: "assistant",
183
- content: textContent || null
184
- };
185
- if (message.toolCalls && message.toolCalls.length > 0) {
186
- assistantMessage.tool_calls = message.toolCalls.map((call) => ({
187
- id: call.toolCallId,
188
- type: "function",
189
- function: {
190
- name: call.toolName,
191
- arguments: JSON.stringify(call.arguments)
192
- }
193
- }));
194
- }
195
- const openrouterMeta = message.metadata?.openrouter;
196
- if (openrouterMeta?.reasoning_details && openrouterMeta.reasoning_details.length > 0) {
197
- assistantMessage.reasoning_details = openrouterMeta.reasoning_details;
198
- }
199
- return assistantMessage;
200
- }
201
- if (isToolResultMessage(message)) {
202
- const results = message.results.map((result) => ({
203
- role: "tool",
204
- tool_call_id: result.toolCallId,
205
- content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
206
- }));
207
- return results[0] ?? null;
208
- }
209
- return null;
210
- }
211
- function transformToolResults(message) {
212
- if (!isToolResultMessage(message)) {
213
- const single = transformMessage(message);
214
- return single ? [single] : [];
215
- }
216
- return message.results.map((result) => ({
217
- role: "tool",
218
- tool_call_id: result.toolCallId,
219
- content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
220
- }));
221
- }
222
92
  function transformContentBlock(block) {
223
93
  switch (block.type) {
224
94
  case "text":
@@ -308,60 +178,209 @@ function transformContentBlock(block) {
308
178
  throw new Error(`Unsupported content type: ${block.type}`);
309
179
  }
310
180
  }
311
- function transformTool(tool) {
312
- return {
313
- type: "function",
314
- function: {
315
- name: tool.name,
316
- description: tool.description,
317
- parameters: {
318
- type: "object",
319
- properties: tool.parameters.properties,
320
- required: tool.parameters.required,
321
- ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
181
+ function transformMessage(message) {
182
+ if (isUserMessage(message)) {
183
+ const validContent = filterValidContent(message.content);
184
+ const cacheControl = extractCacheControl(message);
185
+ if (cacheControl) {
186
+ const content = validContent.map(transformContentBlock);
187
+ for (let i = content.length - 1; i >= 0; i--) {
188
+ const block = content[i];
189
+ if (block?.type === "text") {
190
+ content[i] = { type: "text", text: block.text, cache_control: cacheControl };
191
+ break;
192
+ }
322
193
  }
194
+ return { role: "user", content };
323
195
  }
324
- };
325
- }
326
- function transformResponse(data) {
327
- const choice = data.choices[0];
328
- if (!choice) {
329
- throw new Error("No choices in OpenRouter response");
330
- }
331
- const reasoningContent = [];
332
- const textContent = [];
333
- let structuredData;
334
- if (choice.message.reasoning_details && choice.message.reasoning_details.length > 0) {
335
- for (const detail of choice.message.reasoning_details) {
336
- if (detail.type === "reasoning.text" && detail.text) {
337
- reasoningContent.push({ type: "reasoning", text: detail.text });
338
- } else if (detail.type === "reasoning.summary" && detail.summary) {
339
- reasoningContent.push({ type: "reasoning", text: detail.summary });
340
- }
196
+ if (validContent.length === 1 && validContent[0]?.type === "text") {
197
+ return {
198
+ role: "user",
199
+ content: validContent[0].text
200
+ };
341
201
  }
202
+ return {
203
+ role: "user",
204
+ content: validContent.map(transformContentBlock)
205
+ };
342
206
  }
343
- if (choice.message.content) {
344
- textContent.push({ type: "text", text: choice.message.content });
345
- try {
346
- structuredData = JSON.parse(choice.message.content);
347
- } catch {
207
+ if (isAssistantMessage(message)) {
208
+ const validContent = filterValidContent(message.content);
209
+ const nonReasoningContent = validContent.filter((c) => c.type !== "reasoning");
210
+ const textContent = nonReasoningContent.filter((c) => c.type === "text").map((c) => c.text).join("");
211
+ const assistantMessage = {
212
+ role: "assistant",
213
+ content: textContent || null
214
+ };
215
+ if (message.toolCalls && message.toolCalls.length > 0) {
216
+ assistantMessage.tool_calls = message.toolCalls.map((call) => ({
217
+ id: call.toolCallId,
218
+ type: "function",
219
+ function: {
220
+ name: call.toolName,
221
+ arguments: JSON.stringify(call.arguments)
222
+ }
223
+ }));
348
224
  }
349
- }
350
- if (choice.message.images && choice.message.images.length > 0) {
351
- for (const image of choice.message.images) {
352
- const imageBlock = parseGeneratedImage(image.image_url.url);
353
- if (imageBlock) {
354
- textContent.push(imageBlock);
355
- }
225
+ const openrouterMeta = message.metadata?.openrouter;
226
+ if (openrouterMeta?.reasoning_details && openrouterMeta.reasoning_details.length > 0) {
227
+ assistantMessage.reasoning_details = openrouterMeta.reasoning_details;
356
228
  }
229
+ return assistantMessage;
357
230
  }
358
- const content = [...reasoningContent, ...textContent];
359
- const toolCalls = [];
360
- if (choice.message.tool_calls) {
361
- for (const call of choice.message.tool_calls) {
362
- let args = {};
363
- try {
364
- args = JSON.parse(call.function.arguments);
231
+ if (isToolResultMessage(message)) {
232
+ const results = message.results.map((result) => ({
233
+ role: "tool",
234
+ tool_call_id: result.toolCallId,
235
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
236
+ }));
237
+ return results[0] ?? null;
238
+ }
239
+ return null;
240
+ }
241
+ function transformToolResults(message) {
242
+ if (!isToolResultMessage(message)) {
243
+ const single = transformMessage(message);
244
+ return single ? [single] : [];
245
+ }
246
+ return message.results.map((result) => ({
247
+ role: "tool",
248
+ tool_call_id: result.toolCallId,
249
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
250
+ }));
251
+ }
252
+ function transformMessages(messages, system) {
253
+ const result = [];
254
+ if (system !== void 0 && system !== null) {
255
+ const normalizedSystem = normalizeSystem(system);
256
+ if (typeof normalizedSystem === "string") {
257
+ if (normalizedSystem.length > 0) {
258
+ result.push({
259
+ role: "system",
260
+ content: normalizedSystem
261
+ });
262
+ }
263
+ } else if (normalizedSystem.length > 0) {
264
+ result.push({
265
+ role: "system",
266
+ content: normalizedSystem
267
+ });
268
+ }
269
+ }
270
+ for (const message of messages) {
271
+ if (isToolResultMessage(message)) {
272
+ const toolMessages = transformToolResults(message);
273
+ result.push(...toolMessages);
274
+ } else {
275
+ const transformed = transformMessage(message);
276
+ if (transformed) {
277
+ result.push(transformed);
278
+ }
279
+ }
280
+ }
281
+ return result;
282
+ }
283
+ function transformTool(tool) {
284
+ return {
285
+ type: "function",
286
+ function: {
287
+ name: tool.name,
288
+ description: tool.description,
289
+ parameters: {
290
+ type: "object",
291
+ properties: tool.parameters.properties,
292
+ required: tool.parameters.required,
293
+ ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
294
+ }
295
+ }
296
+ };
297
+ }
298
+ function transformRequest(request, modelId) {
299
+ const params = request.params ?? {};
300
+ const openrouterRequest = {
301
+ ...params,
302
+ model: modelId,
303
+ messages: transformMessages(request.messages, request.system)
304
+ };
305
+ if (request.tools && request.tools.length > 0) {
306
+ openrouterRequest.tools = request.tools.map(transformTool);
307
+ }
308
+ if (request.structure) {
309
+ const schema = {
310
+ type: "object",
311
+ properties: request.structure.properties,
312
+ required: request.structure.required,
313
+ ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
314
+ };
315
+ if (request.structure.description) {
316
+ schema.description = request.structure.description;
317
+ }
318
+ openrouterRequest.response_format = {
319
+ type: "json_schema",
320
+ json_schema: {
321
+ name: "json_response",
322
+ description: request.structure.description,
323
+ schema,
324
+ strict: true
325
+ }
326
+ };
327
+ }
328
+ return openrouterRequest;
329
+ }
330
+ function parseGeneratedImage(dataUrl) {
331
+ const match = dataUrl.match(/^data:(image\/[^;]+);base64,(.+)$/);
332
+ if (!match) {
333
+ return null;
334
+ }
335
+ const [, mimeType, data] = match;
336
+ if (!mimeType || !data) {
337
+ return null;
338
+ }
339
+ return {
340
+ type: "image",
341
+ mimeType,
342
+ source: { type: "base64", data }
343
+ };
344
+ }
345
+ function transformResponse(data) {
346
+ const choice = data.choices[0];
347
+ if (!choice) {
348
+ throw new Error("No choices in OpenRouter response");
349
+ }
350
+ const reasoningContent = [];
351
+ const textContent = [];
352
+ let structuredData;
353
+ if (choice.message.reasoning_details && choice.message.reasoning_details.length > 0) {
354
+ for (const detail of choice.message.reasoning_details) {
355
+ if (detail.type === "reasoning.text" && detail.text) {
356
+ reasoningContent.push({ type: "reasoning", text: detail.text });
357
+ } else if (detail.type === "reasoning.summary" && detail.summary) {
358
+ reasoningContent.push({ type: "reasoning", text: detail.summary });
359
+ }
360
+ }
361
+ }
362
+ if (choice.message.content) {
363
+ textContent.push({ type: "text", text: choice.message.content });
364
+ try {
365
+ structuredData = JSON.parse(choice.message.content);
366
+ } catch {
367
+ }
368
+ }
369
+ if (choice.message.images && choice.message.images.length > 0) {
370
+ for (const image of choice.message.images) {
371
+ const imageBlock = parseGeneratedImage(image.image_url.url);
372
+ if (imageBlock) {
373
+ textContent.push(imageBlock);
374
+ }
375
+ }
376
+ }
377
+ const content = [...reasoningContent, ...textContent];
378
+ const toolCalls = [];
379
+ if (choice.message.tool_calls) {
380
+ for (const call of choice.message.tool_calls) {
381
+ let args = {};
382
+ try {
383
+ args = JSON.parse(call.function.arguments);
365
384
  } catch {
366
385
  }
367
386
  toolCalls.push({
@@ -382,7 +401,6 @@ function transformResponse(data) {
382
401
  model: data.model,
383
402
  finish_reason: choice.finish_reason,
384
403
  system_fingerprint: data.system_fingerprint,
385
- // Store reasoning_details for multi-turn context preservation
386
404
  reasoning_details: choice.message.reasoning_details
387
405
  }
388
406
  }
@@ -417,21 +435,6 @@ function transformResponse(data) {
417
435
  data: structuredData
418
436
  };
419
437
  }
420
- function parseGeneratedImage(dataUrl) {
421
- const match = dataUrl.match(/^data:(image\/[^;]+);base64,(.+)$/);
422
- if (!match) {
423
- return null;
424
- }
425
- const [, mimeType, data] = match;
426
- if (!mimeType || !data) {
427
- return null;
428
- }
429
- return {
430
- type: "image",
431
- mimeType,
432
- source: { type: "base64", data }
433
- };
434
- }
435
438
  function createStreamState() {
436
439
  return {
437
440
  id: "",
@@ -577,7 +580,6 @@ function buildResponseFromState(state) {
577
580
  openrouter: {
578
581
  model: state.model,
579
582
  finish_reason: state.finishReason,
580
- // Store reasoning_details for multi-turn context preservation
581
583
  reasoning_details: state.reasoningDetails.length > 0 ? state.reasoningDetails : void 0
582
584
  }
583
585
  }
@@ -759,10 +761,9 @@ function createCompletionsLLMHandler() {
759
761
  }
760
762
  const uppEvents = transformStreamEvent(chunk, state);
761
763
  for (const event of uppEvents) {
764
+ yield event;
762
765
  if (request.structure && event.type === StreamEventType.TextDelta) {
763
766
  yield objectDelta(event.delta.text ?? "", event.index);
764
- } else {
765
- yield event;
766
767
  }
767
768
  }
768
769
  }
@@ -788,69 +789,12 @@ function createCompletionsLLMHandler() {
788
789
  }
789
790
 
790
791
  // src/providers/openrouter/transform.responses.ts
791
- function transformRequest2(request, modelId) {
792
- const params = request.params ?? {};
793
- const openrouterRequest = {
794
- ...params,
795
- model: modelId,
796
- input: transformInputItems(request.messages, request.system)
797
- };
798
- if (request.tools && request.tools.length > 0) {
799
- openrouterRequest.tools = request.tools.map(transformTool2);
800
- }
801
- if (request.structure) {
802
- const schema = {
803
- type: "object",
804
- properties: request.structure.properties,
805
- required: request.structure.required,
806
- ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
807
- };
808
- if (request.structure.description) {
809
- schema.description = request.structure.description;
810
- }
811
- openrouterRequest.text = {
812
- format: {
813
- type: "json_schema",
814
- name: "json_response",
815
- description: request.structure.description,
816
- schema,
817
- strict: true
818
- }
819
- };
820
- }
821
- return openrouterRequest;
822
- }
823
- function transformInputItems(messages, system) {
824
- const result = [];
825
- if (system !== void 0 && system !== null) {
826
- const normalizedSystem = normalizeSystem2(system);
827
- if (typeof normalizedSystem === "string") {
828
- if (normalizedSystem.length > 0) {
829
- result.push({
830
- type: "message",
831
- role: "system",
832
- content: normalizedSystem
833
- });
834
- }
835
- } else if (normalizedSystem.length > 0) {
836
- result.push({
837
- type: "message",
838
- role: "system",
839
- content: normalizedSystem
840
- });
841
- }
842
- }
843
- for (const message of messages) {
844
- const items = transformMessage2(message);
845
- result.push(...items);
846
- }
847
- if (result.length === 1 && result[0]?.type === "message") {
848
- const item = result[0];
849
- if (item.role === "user" && typeof item.content === "string") {
850
- return item.content;
851
- }
852
- }
853
- return result;
792
+ function isValidCacheControl2(value) {
793
+ if (!value || typeof value !== "object") return false;
794
+ const candidate = value;
795
+ if (candidate.type !== "ephemeral") return false;
796
+ if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
797
+ return true;
854
798
  }
855
799
  function normalizeSystem2(system) {
856
800
  if (typeof system === "string") return system;
@@ -893,101 +837,9 @@ function normalizeSystem2(system) {
893
837
  }
894
838
  return blocks;
895
839
  }
896
- function isValidCacheControl2(value) {
897
- if (!value || typeof value !== "object") return false;
898
- const candidate = value;
899
- if (candidate.type !== "ephemeral") return false;
900
- if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
901
- return true;
902
- }
903
840
  function filterValidContent2(content) {
904
841
  return content.filter((c) => c && typeof c.type === "string");
905
842
  }
906
- function transformMessage2(message) {
907
- if (isUserMessage(message)) {
908
- const validContent = filterValidContent2(message.content);
909
- if (validContent.length === 1 && validContent[0]?.type === "text") {
910
- return [
911
- {
912
- type: "message",
913
- role: "user",
914
- content: validContent[0].text
915
- }
916
- ];
917
- }
918
- return [
919
- {
920
- type: "message",
921
- role: "user",
922
- content: validContent.map(transformContentPart)
923
- }
924
- ];
925
- }
926
- if (isAssistantMessage(message)) {
927
- const validContent = filterValidContent2(message.content);
928
- const items = [];
929
- const contentParts = validContent.filter((c) => c.type === "text").map((c) => ({
930
- type: "output_text",
931
- text: c.text,
932
- annotations: []
933
- }));
934
- const messageId = message.id || generateId();
935
- if (contentParts.length > 0) {
936
- items.push({
937
- type: "message",
938
- role: "assistant",
939
- id: messageId,
940
- status: "completed",
941
- content: contentParts
942
- });
943
- }
944
- const openrouterMeta = message.metadata?.openrouter;
945
- const functionCallItems = openrouterMeta?.functionCallItems;
946
- if (openrouterMeta?.reasoningEncryptedContent) {
947
- try {
948
- const reasoningData = JSON.parse(openrouterMeta.reasoningEncryptedContent);
949
- items.push({
950
- type: "reasoning",
951
- id: reasoningData.id,
952
- summary: reasoningData.summary,
953
- encrypted_content: reasoningData.encrypted_content
954
- });
955
- } catch {
956
- }
957
- }
958
- if (functionCallItems && functionCallItems.length > 0) {
959
- for (const fc of functionCallItems) {
960
- items.push({
961
- type: "function_call",
962
- id: fc.id,
963
- call_id: fc.call_id,
964
- name: fc.name,
965
- arguments: fc.arguments
966
- });
967
- }
968
- } else if (message.toolCalls && message.toolCalls.length > 0) {
969
- for (const call of message.toolCalls) {
970
- items.push({
971
- type: "function_call",
972
- id: `fc_${call.toolCallId}`,
973
- call_id: call.toolCallId,
974
- name: call.toolName,
975
- arguments: JSON.stringify(call.arguments)
976
- });
977
- }
978
- }
979
- return items;
980
- }
981
- if (isToolResultMessage(message)) {
982
- return message.results.map((result, index) => ({
983
- type: "function_call_output",
984
- id: `fco_${result.toolCallId}_${index}`,
985
- call_id: result.toolCallId,
986
- output: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
987
- }));
988
- }
989
- return [];
990
- }
991
843
  function transformContentPart(block) {
992
844
  switch (block.type) {
993
845
  case "text":
@@ -1081,6 +933,123 @@ function transformContentPart(block) {
1081
933
  throw new Error(`Unsupported content type: ${block.type}`);
1082
934
  }
1083
935
  }
936
+ function transformMessage2(message) {
937
+ if (isUserMessage(message)) {
938
+ const validContent = filterValidContent2(message.content);
939
+ if (validContent.length === 1 && validContent[0]?.type === "text") {
940
+ return [
941
+ {
942
+ type: "message",
943
+ role: "user",
944
+ content: validContent[0].text
945
+ }
946
+ ];
947
+ }
948
+ return [
949
+ {
950
+ type: "message",
951
+ role: "user",
952
+ content: validContent.map(transformContentPart)
953
+ }
954
+ ];
955
+ }
956
+ if (isAssistantMessage(message)) {
957
+ const validContent = filterValidContent2(message.content);
958
+ const items = [];
959
+ const contentParts = validContent.filter((c) => c.type === "text").map((c) => ({
960
+ type: "output_text",
961
+ text: c.text,
962
+ annotations: []
963
+ }));
964
+ const messageId = message.id || generateId();
965
+ if (contentParts.length > 0) {
966
+ items.push({
967
+ type: "message",
968
+ role: "assistant",
969
+ id: messageId,
970
+ status: "completed",
971
+ content: contentParts
972
+ });
973
+ }
974
+ const openrouterMeta = message.metadata?.openrouter;
975
+ const functionCallItems = openrouterMeta?.functionCallItems;
976
+ if (openrouterMeta?.reasoningEncryptedContent) {
977
+ try {
978
+ const reasoningData = JSON.parse(openrouterMeta.reasoningEncryptedContent);
979
+ items.push({
980
+ type: "reasoning",
981
+ id: reasoningData.id,
982
+ summary: reasoningData.summary,
983
+ encrypted_content: reasoningData.encrypted_content
984
+ });
985
+ } catch {
986
+ }
987
+ }
988
+ if (functionCallItems && functionCallItems.length > 0) {
989
+ for (const fc of functionCallItems) {
990
+ items.push({
991
+ type: "function_call",
992
+ id: fc.id,
993
+ call_id: fc.call_id,
994
+ name: fc.name,
995
+ arguments: fc.arguments
996
+ });
997
+ }
998
+ } else if (message.toolCalls && message.toolCalls.length > 0) {
999
+ for (const call of message.toolCalls) {
1000
+ items.push({
1001
+ type: "function_call",
1002
+ id: `fc_${call.toolCallId}`,
1003
+ call_id: call.toolCallId,
1004
+ name: call.toolName,
1005
+ arguments: JSON.stringify(call.arguments)
1006
+ });
1007
+ }
1008
+ }
1009
+ return items;
1010
+ }
1011
+ if (isToolResultMessage(message)) {
1012
+ return message.results.map((result, index) => ({
1013
+ type: "function_call_output",
1014
+ id: `fco_${result.toolCallId}_${index}`,
1015
+ call_id: result.toolCallId,
1016
+ output: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
1017
+ }));
1018
+ }
1019
+ return [];
1020
+ }
1021
+ function transformInputItems(messages, system) {
1022
+ const result = [];
1023
+ if (system !== void 0 && system !== null) {
1024
+ const normalizedSystem = normalizeSystem2(system);
1025
+ if (typeof normalizedSystem === "string") {
1026
+ if (normalizedSystem.length > 0) {
1027
+ result.push({
1028
+ type: "message",
1029
+ role: "system",
1030
+ content: normalizedSystem
1031
+ });
1032
+ }
1033
+ } else if (normalizedSystem.length > 0) {
1034
+ result.push({
1035
+ type: "message",
1036
+ role: "system",
1037
+ content: normalizedSystem
1038
+ });
1039
+ }
1040
+ }
1041
+ for (const message of messages) {
1042
+ const items = transformMessage2(message);
1043
+ result.push(...items);
1044
+ }
1045
+ if (result.length === 1 && result[0]?.type === "message") {
1046
+ const item = result[0];
1047
+ if (item.role === "user" && typeof item.content === "string") {
1048
+ return item.content;
1049
+ }
1050
+ }
1051
+ return result;
1052
+ }
1084
1053
  function transformTool2(tool) {
1085
1054
  return {
1086
1055
  type: "function",
@@ -1094,6 +1063,38 @@ function transformTool2(tool) {
1094
1063
  }
1095
1064
  };
1096
1065
  }
1066
+ function transformRequest2(request, modelId) {
1067
+ const params = request.params ?? {};
1068
+ const openrouterRequest = {
1069
+ ...params,
1070
+ model: modelId,
1071
+ input: transformInputItems(request.messages, request.system)
1072
+ };
1073
+ if (request.tools && request.tools.length > 0) {
1074
+ openrouterRequest.tools = request.tools.map(transformTool2);
1075
+ }
1076
+ if (request.structure) {
1077
+ const schema = {
1078
+ type: "object",
1079
+ properties: request.structure.properties,
1080
+ required: request.structure.required,
1081
+ ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
1082
+ };
1083
+ if (request.structure.description) {
1084
+ schema.description = request.structure.description;
1085
+ }
1086
+ openrouterRequest.text = {
1087
+ format: {
1088
+ type: "json_schema",
1089
+ name: "json_response",
1090
+ description: request.structure.description,
1091
+ schema,
1092
+ strict: true
1093
+ }
1094
+ };
1095
+ }
1096
+ return openrouterRequest;
1097
+ }
1097
1098
  function transformResponse2(data) {
1098
1099
  const reasoningContent = [];
1099
1100
  const textContent = [];
@@ -1170,10 +1171,8 @@ function transformResponse2(data) {
1170
1171
  openrouter: {
1171
1172
  model: data.model,
1172
1173
  status: data.status,
1173
- // Store response_id for multi-turn tool calling
1174
1174
  response_id: responseId,
1175
1175
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0,
1176
- // Store encrypted reasoning content for multi-turn context (stateless mode)
1177
1176
  reasoningEncryptedContent
1178
1177
  }
1179
1178
  }
@@ -1484,10 +1483,8 @@ function buildResponseFromState2(state) {
1484
1483
  openrouter: {
1485
1484
  model: state.model,
1486
1485
  status: state.status,
1487
- // Store response_id for multi-turn tool calling
1488
1486
  response_id: responseId,
1489
1487
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0,
1490
- // Store encrypted reasoning content for multi-turn context (stateless mode)
1491
1488
  reasoningEncryptedContent: state.reasoningEncryptedContent
1492
1489
  }
1493
1490
  }
@@ -1672,10 +1669,9 @@ function createResponsesLLMHandler() {
1672
1669
  }
1673
1670
  const uppEvents = transformStreamEvent2(event, state);
1674
1671
  for (const uppEvent of uppEvents) {
1672
+ yield uppEvent;
1675
1673
  if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
1676
1674
  yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
1677
- } else {
1678
- yield uppEvent;
1679
1675
  }
1680
1676
  }
1681
1677
  }