@providerprotocol/ai 0.0.34 → 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 +82 -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,33 +1,37 @@
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
+ } from "../chunk-BRP5XJ6Q.js";
19
20
  import {
20
- resolveApiKey
21
- } from "../chunk-3D6XGGVG.js";
22
- 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/anthropic/types.ts
33
37
  var betas = {
@@ -154,59 +158,14 @@ var tools = {
154
158
  };
155
159
 
156
160
  // src/providers/anthropic/transform.ts
157
- function transformRequest(request, modelId, useNativeStructuredOutput = false) {
158
- const params = request.params ?? {};
159
- const { tools: builtInTools, ...restParams } = params;
160
- const anthropicRequest = {
161
- ...restParams,
162
- model: modelId,
163
- messages: request.messages.map(transformMessage)
164
- };
165
- const normalizedSystem = normalizeSystem(request.system);
166
- if (normalizedSystem !== void 0) {
167
- anthropicRequest.system = normalizedSystem;
168
- }
169
- const allTools = [];
170
- if (request.tools && request.tools.length > 0) {
171
- allTools.push(...request.tools.map(transformTool));
172
- }
173
- if (builtInTools && builtInTools.length > 0) {
174
- allTools.push(...builtInTools);
175
- }
176
- if (allTools.length > 0) {
177
- anthropicRequest.tools = allTools;
178
- if (!anthropicRequest.tool_choice) {
179
- anthropicRequest.tool_choice = { type: "auto" };
180
- }
181
- }
182
- if (request.structure) {
183
- if (useNativeStructuredOutput) {
184
- const outputFormat = {
185
- type: "json_schema",
186
- schema: {
187
- type: "object",
188
- properties: request.structure.properties,
189
- required: request.structure.required,
190
- additionalProperties: false
191
- }
192
- };
193
- anthropicRequest.output_format = outputFormat;
194
- } else {
195
- const structuredTool = {
196
- name: "json_response",
197
- description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
198
- input_schema: {
199
- type: "object",
200
- properties: request.structure.properties,
201
- required: request.structure.required,
202
- ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
203
- }
204
- };
205
- anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
206
- anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
207
- }
161
+ function isValidCacheControl(value) {
162
+ if (!value || typeof value !== "object") return false;
163
+ const candidate = value;
164
+ if (candidate.type !== "ephemeral") return false;
165
+ if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
166
+ return false;
208
167
  }
209
- return anthropicRequest;
168
+ return true;
210
169
  }
211
170
  function normalizeSystem(system) {
212
171
  if (system === void 0 || system === null) return void 0;
@@ -250,15 +209,6 @@ function normalizeSystem(system) {
250
209
  }
251
210
  return blocks.length > 0 ? blocks : void 0;
252
211
  }
253
- function isValidCacheControl(value) {
254
- if (!value || typeof value !== "object") return false;
255
- const candidate = value;
256
- if (candidate.type !== "ephemeral") return false;
257
- if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
258
- return false;
259
- }
260
- return true;
261
- }
262
212
  function filterValidContent(content) {
263
213
  return content.filter((c) => c && typeof c.type === "string");
264
214
  }
@@ -266,72 +216,6 @@ function extractCacheControl(message) {
266
216
  const anthropicMeta = message.metadata?.anthropic;
267
217
  return anthropicMeta?.cache_control;
268
218
  }
269
- function transformMessage(message) {
270
- const cacheControl = extractCacheControl(message);
271
- if (isUserMessage(message)) {
272
- const validContent = filterValidContent(message.content);
273
- const contentBlocks = validContent.map(
274
- (block, index, arr) => transformContentBlock(block, index === arr.length - 1 ? cacheControl : void 0)
275
- );
276
- return {
277
- role: "user",
278
- content: contentBlocks
279
- };
280
- }
281
- if (isAssistantMessage(message)) {
282
- const validContent = filterValidContent(message.content);
283
- const content = [];
284
- const anthropicMeta = message.metadata?.anthropic;
285
- const thinkingSignatures = anthropicMeta?.thinkingSignatures;
286
- let reasoningIndex = 0;
287
- for (let i = 0; i < validContent.length; i++) {
288
- const block = validContent[i];
289
- const isLastNonToolBlock = i === validContent.length - 1 && !message.toolCalls?.length;
290
- if (block.type === "reasoning") {
291
- const signatureFromArray = thinkingSignatures?.[reasoningIndex];
292
- const signature = Array.isArray(thinkingSignatures) ? typeof signatureFromArray === "string" ? signatureFromArray : void 0 : anthropicMeta?.thinkingSignature;
293
- reasoningIndex += 1;
294
- content.push({
295
- type: "thinking",
296
- thinking: block.text,
297
- ...signature ? { signature } : {}
298
- });
299
- } else {
300
- content.push(transformContentBlock(block, isLastNonToolBlock ? cacheControl : void 0));
301
- }
302
- }
303
- if (message.toolCalls) {
304
- for (let i = 0; i < message.toolCalls.length; i++) {
305
- const call = message.toolCalls[i];
306
- const isLast = i === message.toolCalls.length - 1;
307
- content.push({
308
- type: "tool_use",
309
- id: call.toolCallId,
310
- name: call.toolName,
311
- input: call.arguments,
312
- ...isLast && cacheControl ? { cache_control: cacheControl } : {}
313
- });
314
- }
315
- }
316
- return {
317
- role: "assistant",
318
- content
319
- };
320
- }
321
- if (isToolResultMessage(message)) {
322
- return {
323
- role: "user",
324
- content: message.results.map((result, index, arr) => ({
325
- type: "tool_result",
326
- tool_use_id: result.toolCallId,
327
- content: typeof result.result === "string" ? result.result : JSON.stringify(result.result),
328
- is_error: result.isError,
329
- ...index === arr.length - 1 && cacheControl ? { cache_control: cacheControl } : {}
330
- }))
331
- };
332
- }
333
- throw new Error(`Unknown message type: ${message.type}`);
334
- }
335
219
  function transformContentBlock(block, cacheControl) {
336
220
  switch (block.type) {
337
221
  case "text":
@@ -417,6 +301,72 @@ function transformContentBlock(block, cacheControl) {
417
301
  throw new Error(`Unsupported content type: ${block.type}`);
418
302
  }
419
303
  }
304
+ function transformMessage(message) {
305
+ const cacheControl = extractCacheControl(message);
306
+ if (isUserMessage(message)) {
307
+ const validContent = filterValidContent(message.content);
308
+ const contentBlocks = validContent.map(
309
+ (block, index, arr) => transformContentBlock(block, index === arr.length - 1 ? cacheControl : void 0)
310
+ );
311
+ return {
312
+ role: "user",
313
+ content: contentBlocks
314
+ };
315
+ }
316
+ if (isAssistantMessage(message)) {
317
+ const validContent = filterValidContent(message.content);
318
+ const content = [];
319
+ const anthropicMeta = message.metadata?.anthropic;
320
+ const thinkingSignatures = anthropicMeta?.thinkingSignatures;
321
+ let reasoningIndex = 0;
322
+ for (let i = 0; i < validContent.length; i++) {
323
+ const block = validContent[i];
324
+ const isLastNonToolBlock = i === validContent.length - 1 && !message.toolCalls?.length;
325
+ if (block.type === "reasoning") {
326
+ const signatureFromArray = thinkingSignatures?.[reasoningIndex];
327
+ const signature = Array.isArray(thinkingSignatures) ? typeof signatureFromArray === "string" ? signatureFromArray : void 0 : anthropicMeta?.thinkingSignature;
328
+ reasoningIndex += 1;
329
+ content.push({
330
+ type: "thinking",
331
+ thinking: block.text,
332
+ ...signature ? { signature } : {}
333
+ });
334
+ } else {
335
+ content.push(transformContentBlock(block, isLastNonToolBlock ? cacheControl : void 0));
336
+ }
337
+ }
338
+ if (message.toolCalls) {
339
+ for (let i = 0; i < message.toolCalls.length; i++) {
340
+ const call = message.toolCalls[i];
341
+ const isLast = i === message.toolCalls.length - 1;
342
+ content.push({
343
+ type: "tool_use",
344
+ id: call.toolCallId,
345
+ name: call.toolName,
346
+ input: call.arguments,
347
+ ...isLast && cacheControl ? { cache_control: cacheControl } : {}
348
+ });
349
+ }
350
+ }
351
+ return {
352
+ role: "assistant",
353
+ content
354
+ };
355
+ }
356
+ if (isToolResultMessage(message)) {
357
+ return {
358
+ role: "user",
359
+ content: message.results.map((result, index, arr) => ({
360
+ type: "tool_result",
361
+ tool_use_id: result.toolCallId,
362
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result),
363
+ is_error: result.isError,
364
+ ...index === arr.length - 1 && cacheControl ? { cache_control: cacheControl } : {}
365
+ }))
366
+ };
367
+ }
368
+ throw new Error(`Unknown message type: ${message.type}`);
369
+ }
420
370
  function extractToolCacheControl(tool) {
421
371
  const anthropicMeta = tool.metadata?.anthropic;
422
372
  return anthropicMeta?.cache_control;
@@ -435,6 +385,60 @@ function transformTool(tool) {
435
385
  ...cacheControl ? { cache_control: cacheControl } : {}
436
386
  };
437
387
  }
388
+ function transformRequest(request, modelId, useNativeStructuredOutput = false) {
389
+ const params = request.params ?? {};
390
+ const { tools: builtInTools, ...restParams } = params;
391
+ const anthropicRequest = {
392
+ ...restParams,
393
+ model: modelId,
394
+ messages: request.messages.map(transformMessage)
395
+ };
396
+ const normalizedSystem = normalizeSystem(request.system);
397
+ if (normalizedSystem !== void 0) {
398
+ anthropicRequest.system = normalizedSystem;
399
+ }
400
+ const allTools = [];
401
+ if (request.tools && request.tools.length > 0) {
402
+ allTools.push(...request.tools.map(transformTool));
403
+ }
404
+ if (builtInTools && builtInTools.length > 0) {
405
+ allTools.push(...builtInTools);
406
+ }
407
+ if (allTools.length > 0) {
408
+ anthropicRequest.tools = allTools;
409
+ if (!anthropicRequest.tool_choice) {
410
+ anthropicRequest.tool_choice = { type: "auto" };
411
+ }
412
+ }
413
+ if (request.structure) {
414
+ if (useNativeStructuredOutput) {
415
+ const outputFormat = {
416
+ type: "json_schema",
417
+ schema: {
418
+ type: "object",
419
+ properties: request.structure.properties,
420
+ required: request.structure.required,
421
+ additionalProperties: false
422
+ }
423
+ };
424
+ anthropicRequest.output_format = outputFormat;
425
+ } else {
426
+ const structuredTool = {
427
+ name: "json_response",
428
+ description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
429
+ input_schema: {
430
+ type: "object",
431
+ properties: request.structure.properties,
432
+ required: request.structure.required,
433
+ ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
434
+ }
435
+ };
436
+ anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
437
+ anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
438
+ }
439
+ }
440
+ return anthropicRequest;
441
+ }
438
442
  function transformResponse(data, useNativeStructuredOutput = false) {
439
443
  const reasoningContent = [];
440
444
  const textContent = [];
@@ -883,10 +887,12 @@ function createLLMHandler() {
883
887
  }
884
888
  const uppEvents = transformStreamEvent(event, state);
885
889
  for (const uppEvent of uppEvents) {
890
+ yield uppEvent;
886
891
  if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
887
892
  yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
888
- } else {
889
- yield uppEvent;
893
+ }
894
+ if (request.structure && uppEvent.type === StreamEventType.ToolCallDelta && uppEvent.delta.argumentsJson) {
895
+ yield objectDelta(uppEvent.delta.argumentsJson, uppEvent.index);
890
896
  }
891
897
  }
892
898
  }