@providerprotocol/ai 0.0.12 → 0.0.14

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 (44) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +80 -29
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
  5. package/dist/chunk-MOU4U3PO.js.map +1 -0
  6. package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
  7. package/dist/chunk-MSR5P65T.js.map +1 -0
  8. package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
  9. package/dist/chunk-SVYROCLD.js.map +1 -0
  10. package/dist/chunk-U4JJC2YX.js +234 -0
  11. package/dist/chunk-U4JJC2YX.js.map +1 -0
  12. package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
  13. package/dist/chunk-Z7RBRCRN.js.map +1 -0
  14. package/dist/google/index.d.ts +376 -7
  15. package/dist/google/index.js +149 -21
  16. package/dist/google/index.js.map +1 -1
  17. package/dist/http/index.d.ts +222 -25
  18. package/dist/http/index.js +3 -3
  19. package/dist/index.d.ts +1484 -198
  20. package/dist/index.js +233 -47
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +31 -7
  24. package/dist/ollama/index.js.map +1 -1
  25. package/dist/openai/index.d.ts +340 -61
  26. package/dist/openai/index.js +105 -31
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +84 -24
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-Bi0nyNhA.d.ts +505 -0
  32. package/dist/retry-BatS2hjD.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +129 -45
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -3
  37. package/dist/chunk-CUCRF5W6.js +0 -136
  38. package/dist/chunk-CUCRF5W6.js.map +0 -1
  39. package/dist/chunk-SUNYWHTH.js.map +0 -1
  40. package/dist/chunk-W4BB4BG2.js.map +0 -1
  41. package/dist/chunk-X5G4EHL7.js.map +0 -1
  42. package/dist/chunk-Y6Q7JCNP.js.map +0 -1
  43. package/dist/provider-CUJWjgNl.d.ts +0 -192
  44. package/dist/retry-I2661_rv.d.ts +0 -118
@@ -3,17 +3,17 @@ import {
3
3
  isAssistantMessage,
4
4
  isToolResultMessage,
5
5
  isUserMessage
6
- } from "../chunk-W4BB4BG2.js";
6
+ } from "../chunk-SVYROCLD.js";
7
7
  import {
8
8
  parseSSEStream
9
- } from "../chunk-X5G4EHL7.js";
9
+ } from "../chunk-Z7RBRCRN.js";
10
10
  import {
11
11
  UPPError,
12
12
  doFetch,
13
13
  doStreamFetch,
14
14
  normalizeHttpError,
15
15
  resolveApiKey
16
- } from "../chunk-SUNYWHTH.js";
16
+ } from "../chunk-MOU4U3PO.js";
17
17
 
18
18
  // src/providers/openai/transform.completions.ts
19
19
  function transformRequest(request, modelId) {
@@ -48,12 +48,18 @@ function transformRequest(request, modelId) {
48
48
  }
49
49
  return openaiRequest;
50
50
  }
51
+ function normalizeSystem(system) {
52
+ if (!system) return void 0;
53
+ if (typeof system === "string") return system;
54
+ return system.map((block) => block.text ?? "").filter((text) => text.length > 0).join("\n\n");
55
+ }
51
56
  function transformMessages(messages, system) {
52
57
  const result = [];
53
- if (system) {
58
+ const normalizedSystem = normalizeSystem(system);
59
+ if (normalizedSystem) {
54
60
  result.push({
55
61
  role: "system",
56
- content: system
62
+ content: normalizedSystem
57
63
  });
58
64
  }
59
65
  for (const message of messages) {
@@ -154,7 +160,12 @@ function transformContentBlock(block) {
154
160
  throw new Error(`Unsupported content type: ${block.type}`);
155
161
  }
156
162
  }
163
+ function extractToolOptions(tool) {
164
+ const openaiMeta = tool.metadata?.openai;
165
+ return { strict: openaiMeta?.strict };
166
+ }
157
167
  function transformTool(tool) {
168
+ const { strict } = extractToolOptions(tool);
158
169
  return {
159
170
  type: "function",
160
171
  function: {
@@ -165,7 +176,8 @@ function transformTool(tool) {
165
176
  properties: tool.parameters.properties,
166
177
  required: tool.parameters.required,
167
178
  ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
168
- }
179
+ },
180
+ ...strict !== void 0 ? { strict } : {}
169
181
  }
170
182
  };
171
183
  }
@@ -221,7 +233,9 @@ function transformResponse(data) {
221
233
  const usage = {
222
234
  inputTokens: data.usage.prompt_tokens,
223
235
  outputTokens: data.usage.completion_tokens,
224
- totalTokens: data.usage.total_tokens
236
+ totalTokens: data.usage.total_tokens,
237
+ cacheReadTokens: data.usage.prompt_tokens_details?.cached_tokens ?? 0,
238
+ cacheWriteTokens: 0
225
239
  };
226
240
  let stopReason = "end_turn";
227
241
  switch (choice.finish_reason) {
@@ -257,6 +271,7 @@ function createStreamState() {
257
271
  finishReason: null,
258
272
  inputTokens: 0,
259
273
  outputTokens: 0,
274
+ cacheReadTokens: 0,
260
275
  hadRefusal: false
261
276
  };
262
277
  }
@@ -324,6 +339,7 @@ function transformStreamEvent(chunk, state) {
324
339
  if (chunk.usage) {
325
340
  state.inputTokens = chunk.usage.prompt_tokens;
326
341
  state.outputTokens = chunk.usage.completion_tokens;
342
+ state.cacheReadTokens = chunk.usage.prompt_tokens_details?.cached_tokens ?? 0;
327
343
  }
328
344
  return events;
329
345
  }
@@ -368,7 +384,9 @@ function buildResponseFromState(state) {
368
384
  const usage = {
369
385
  inputTokens: state.inputTokens,
370
386
  outputTokens: state.outputTokens,
371
- totalTokens: state.inputTokens + state.outputTokens
387
+ totalTokens: state.inputTokens + state.outputTokens,
388
+ cacheReadTokens: state.cacheReadTokens,
389
+ cacheWriteTokens: 0
372
390
  };
373
391
  let stopReason = "end_turn";
374
392
  switch (state.finishReason) {
@@ -436,14 +454,22 @@ function createCompletionsLLMHandler() {
436
454
  );
437
455
  const baseUrl = request.config.baseUrl ?? OPENAI_API_URL;
438
456
  const body = transformRequest(request, modelId);
457
+ const headers = {
458
+ "Content-Type": "application/json",
459
+ Authorization: `Bearer ${apiKey}`
460
+ };
461
+ if (request.config.headers) {
462
+ for (const [key, value] of Object.entries(request.config.headers)) {
463
+ if (value !== void 0) {
464
+ headers[key] = value;
465
+ }
466
+ }
467
+ }
439
468
  const response = await doFetch(
440
469
  baseUrl,
441
470
  {
442
471
  method: "POST",
443
- headers: {
444
- "Content-Type": "application/json",
445
- Authorization: `Bearer ${apiKey}`
446
- },
472
+ headers,
447
473
  body: JSON.stringify(body),
448
474
  signal: request.signal
449
475
  },
@@ -474,14 +500,22 @@ function createCompletionsLLMHandler() {
474
500
  const body = transformRequest(request, modelId);
475
501
  body.stream = true;
476
502
  body.stream_options = { include_usage: true };
503
+ const headers = {
504
+ "Content-Type": "application/json",
505
+ Authorization: `Bearer ${apiKey}`
506
+ };
507
+ if (request.config.headers) {
508
+ for (const [key, value] of Object.entries(request.config.headers)) {
509
+ if (value !== void 0) {
510
+ headers[key] = value;
511
+ }
512
+ }
513
+ }
477
514
  const response = await doStreamFetch(
478
515
  baseUrl,
479
516
  {
480
517
  method: "POST",
481
- headers: {
482
- "Content-Type": "application/json",
483
- Authorization: `Bearer ${apiKey}`
484
- },
518
+ headers,
485
519
  body: JSON.stringify(body),
486
520
  signal: request.signal
487
521
  },
@@ -583,13 +617,19 @@ function transformRequest2(request, modelId) {
583
617
  }
584
618
  return openaiRequest;
585
619
  }
620
+ function normalizeSystem2(system) {
621
+ if (!system) return void 0;
622
+ if (typeof system === "string") return system;
623
+ return system.map((block) => block.text ?? "").filter((text) => text.length > 0).join("\n\n");
624
+ }
586
625
  function transformInputItems(messages, system) {
587
626
  const result = [];
588
- if (system) {
627
+ const normalizedSystem = normalizeSystem2(system);
628
+ if (normalizedSystem) {
589
629
  result.push({
590
630
  type: "message",
591
631
  role: "system",
592
- content: system
632
+ content: normalizedSystem
593
633
  });
594
634
  }
595
635
  for (const message of messages) {
@@ -708,7 +748,12 @@ function transformContentPart(block) {
708
748
  throw new Error(`Unsupported content type: ${block.type}`);
709
749
  }
710
750
  }
751
+ function extractToolOptions2(tool) {
752
+ const openaiMeta = tool.metadata?.openai;
753
+ return { strict: openaiMeta?.strict };
754
+ }
711
755
  function transformTool2(tool) {
756
+ const { strict } = extractToolOptions2(tool);
712
757
  return {
713
758
  type: "function",
714
759
  name: tool.name,
@@ -718,7 +763,8 @@ function transformTool2(tool) {
718
763
  properties: tool.parameters.properties,
719
764
  required: tool.parameters.required,
720
765
  ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
721
- }
766
+ },
767
+ ...strict !== void 0 ? { strict } : {}
722
768
  };
723
769
  }
724
770
  function transformResponse2(data) {
@@ -791,7 +837,9 @@ function transformResponse2(data) {
791
837
  const usage = {
792
838
  inputTokens: data.usage.input_tokens,
793
839
  outputTokens: data.usage.output_tokens,
794
- totalTokens: data.usage.total_tokens
840
+ totalTokens: data.usage.total_tokens,
841
+ cacheReadTokens: data.usage.input_tokens_details?.cached_tokens ?? 0,
842
+ cacheWriteTokens: 0
795
843
  };
796
844
  let stopReason = "end_turn";
797
845
  if (data.status === "completed") {
@@ -821,6 +869,7 @@ function createStreamState2() {
821
869
  status: "in_progress",
822
870
  inputTokens: 0,
823
871
  outputTokens: 0,
872
+ cacheReadTokens: 0,
824
873
  hadRefusal: false
825
874
  };
826
875
  }
@@ -840,6 +889,7 @@ function transformStreamEvent2(event, state) {
840
889
  if (event.response.usage) {
841
890
  state.inputTokens = event.response.usage.input_tokens;
842
891
  state.outputTokens = event.response.usage.output_tokens;
892
+ state.cacheReadTokens = event.response.usage.input_tokens_details?.cached_tokens ?? 0;
843
893
  }
844
894
  events.push({ type: "message_stop", index: 0, delta: {} });
845
895
  break;
@@ -892,7 +942,7 @@ function transformStreamEvent2(event, state) {
892
942
  delta: {}
893
943
  });
894
944
  break;
895
- case "response.output_text.delta":
945
+ case "response.output_text.delta": {
896
946
  const currentText = state.textByIndex.get(event.output_index) ?? "";
897
947
  state.textByIndex.set(event.output_index, currentText + event.delta);
898
948
  events.push({
@@ -901,6 +951,7 @@ function transformStreamEvent2(event, state) {
901
951
  delta: { text: event.delta }
902
952
  });
903
953
  break;
954
+ }
904
955
  case "response.output_text.done":
905
956
  state.textByIndex.set(event.output_index, event.text);
906
957
  break;
@@ -1023,7 +1074,6 @@ function buildResponseFromState2(state) {
1023
1074
  openai: {
1024
1075
  model: state.model,
1025
1076
  status: state.status,
1026
- // Store response_id for multi-turn tool calling
1027
1077
  response_id: state.id,
1028
1078
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
1029
1079
  }
@@ -1033,7 +1083,9 @@ function buildResponseFromState2(state) {
1033
1083
  const usage = {
1034
1084
  inputTokens: state.inputTokens,
1035
1085
  outputTokens: state.outputTokens,
1036
- totalTokens: state.inputTokens + state.outputTokens
1086
+ totalTokens: state.inputTokens + state.outputTokens,
1087
+ cacheReadTokens: state.cacheReadTokens,
1088
+ cacheWriteTokens: 0
1037
1089
  };
1038
1090
  let stopReason = "end_turn";
1039
1091
  if (state.status === "completed") {
@@ -1092,14 +1144,22 @@ function createResponsesLLMHandler() {
1092
1144
  );
1093
1145
  const baseUrl = request.config.baseUrl ?? OPENAI_RESPONSES_API_URL;
1094
1146
  const body = transformRequest2(request, modelId);
1147
+ const headers = {
1148
+ "Content-Type": "application/json",
1149
+ Authorization: `Bearer ${apiKey}`
1150
+ };
1151
+ if (request.config.headers) {
1152
+ for (const [key, value] of Object.entries(request.config.headers)) {
1153
+ if (value !== void 0) {
1154
+ headers[key] = value;
1155
+ }
1156
+ }
1157
+ }
1095
1158
  const response = await doFetch(
1096
1159
  baseUrl,
1097
1160
  {
1098
1161
  method: "POST",
1099
- headers: {
1100
- "Content-Type": "application/json",
1101
- Authorization: `Bearer ${apiKey}`
1102
- },
1162
+ headers,
1103
1163
  body: JSON.stringify(body),
1104
1164
  signal: request.signal
1105
1165
  },
@@ -1137,14 +1197,22 @@ function createResponsesLLMHandler() {
1137
1197
  const baseUrl = request.config.baseUrl ?? OPENAI_RESPONSES_API_URL;
1138
1198
  const body = transformRequest2(request, modelId);
1139
1199
  body.stream = true;
1200
+ const headers = {
1201
+ "Content-Type": "application/json",
1202
+ Authorization: `Bearer ${apiKey}`
1203
+ };
1204
+ if (request.config.headers) {
1205
+ for (const [key, value] of Object.entries(request.config.headers)) {
1206
+ if (value !== void 0) {
1207
+ headers[key] = value;
1208
+ }
1209
+ }
1210
+ }
1140
1211
  const response = await doStreamFetch(
1141
1212
  baseUrl,
1142
1213
  {
1143
1214
  method: "POST",
1144
- headers: {
1145
- "Content-Type": "application/json",
1146
- Authorization: `Bearer ${apiKey}`
1147
- },
1215
+ headers,
1148
1216
  body: JSON.stringify(body),
1149
1217
  signal: request.signal
1150
1218
  },
@@ -1262,11 +1330,17 @@ function mcpTool(options) {
1262
1330
  };
1263
1331
  }
1264
1332
  var tools = {
1333
+ /** Creates a web search tool configuration */
1265
1334
  webSearch: webSearchTool,
1335
+ /** Creates a file search tool configuration */
1266
1336
  fileSearch: fileSearchTool,
1337
+ /** Creates a code interpreter tool configuration */
1267
1338
  codeInterpreter: codeInterpreterTool,
1339
+ /** Creates a computer tool configuration */
1268
1340
  computer: computerTool,
1341
+ /** Creates an image generation tool configuration */
1269
1342
  imageGeneration: imageGenerationTool,
1343
+ /** Creates an MCP tool configuration */
1270
1344
  mcp: mcpTool
1271
1345
  };
1272
1346