@providerprotocol/ai 0.0.21 → 0.0.23

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 (53) hide show
  1. package/README.md +188 -6
  2. package/dist/anthropic/index.d.ts +1 -1
  3. package/dist/anthropic/index.js +115 -39
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/{chunk-Y3GBJNA2.js → chunk-55X3W2MN.js} +4 -3
  6. package/dist/chunk-55X3W2MN.js.map +1 -0
  7. package/dist/chunk-73IIE3QT.js +120 -0
  8. package/dist/chunk-73IIE3QT.js.map +1 -0
  9. package/dist/{chunk-M4BMM5IB.js → chunk-MF5ETY5O.js} +13 -4
  10. package/dist/chunk-MF5ETY5O.js.map +1 -0
  11. package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
  12. package/dist/chunk-MKDLXV4O.js.map +1 -0
  13. package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
  14. package/dist/chunk-NWS5IKNR.js.map +1 -0
  15. package/dist/{chunk-EDENPF3E.js → chunk-QNJO7DSD.js} +152 -53
  16. package/dist/chunk-QNJO7DSD.js.map +1 -0
  17. package/dist/{chunk-Z4ILICF5.js → chunk-SBCATNHA.js} +43 -14
  18. package/dist/chunk-SBCATNHA.js.map +1 -0
  19. package/dist/chunk-Z6DKC37J.js +50 -0
  20. package/dist/chunk-Z6DKC37J.js.map +1 -0
  21. package/dist/google/index.d.ts +22 -7
  22. package/dist/google/index.js +286 -85
  23. package/dist/google/index.js.map +1 -1
  24. package/dist/http/index.d.ts +3 -3
  25. package/dist/http/index.js +4 -4
  26. package/dist/index.d.ts +10 -6
  27. package/dist/index.js +331 -204
  28. package/dist/index.js.map +1 -1
  29. package/dist/ollama/index.d.ts +5 -2
  30. package/dist/ollama/index.js +87 -28
  31. package/dist/ollama/index.js.map +1 -1
  32. package/dist/openai/index.d.ts +1 -1
  33. package/dist/openai/index.js +226 -81
  34. package/dist/openai/index.js.map +1 -1
  35. package/dist/openrouter/index.d.ts +1 -1
  36. package/dist/openrouter/index.js +199 -64
  37. package/dist/openrouter/index.js.map +1 -1
  38. package/dist/{provider-DGQHYE6I.d.ts → provider-DR1yins0.d.ts} +159 -53
  39. package/dist/proxy/index.d.ts +2 -2
  40. package/dist/proxy/index.js +178 -17
  41. package/dist/proxy/index.js.map +1 -1
  42. package/dist/{retry-Pcs3hnbu.d.ts → retry-DJiqAslw.d.ts} +11 -2
  43. package/dist/{stream-Di9acos2.d.ts → stream-BuTrqt_j.d.ts} +103 -41
  44. package/dist/xai/index.d.ts +1 -1
  45. package/dist/xai/index.js +189 -75
  46. package/dist/xai/index.js.map +1 -1
  47. package/package.json +1 -1
  48. package/dist/chunk-EDENPF3E.js.map +0 -1
  49. package/dist/chunk-M4BMM5IB.js.map +0 -1
  50. package/dist/chunk-SKY2JLA7.js.map +0 -1
  51. package/dist/chunk-Y3GBJNA2.js.map +0 -1
  52. package/dist/chunk-Z4ILICF5.js.map +0 -1
  53. package/dist/chunk-Z7RBRCRN.js.map +0 -1
@@ -1,25 +1,34 @@
1
1
  import {
2
2
  Image
3
3
  } from "../chunk-WAKD3OO5.js";
4
+ import {
5
+ parseJsonResponse
6
+ } from "../chunk-Z6DKC37J.js";
7
+ import {
8
+ StreamEventType
9
+ } from "../chunk-73IIE3QT.js";
4
10
  import {
5
11
  AssistantMessage,
6
12
  createProvider,
7
13
  isAssistantMessage,
8
14
  isToolResultMessage,
9
15
  isUserMessage
10
- } from "../chunk-M4BMM5IB.js";
16
+ } from "../chunk-MF5ETY5O.js";
11
17
  import {
12
18
  parseSSEStream
13
- } from "../chunk-Z7RBRCRN.js";
19
+ } from "../chunk-NWS5IKNR.js";
14
20
  import {
15
21
  resolveApiKey
16
- } from "../chunk-Y3GBJNA2.js";
22
+ } from "../chunk-55X3W2MN.js";
17
23
  import {
24
+ ErrorCode,
25
+ ModalityType,
18
26
  UPPError,
19
27
  doFetch,
20
28
  doStreamFetch,
21
- normalizeHttpError
22
- } from "../chunk-EDENPF3E.js";
29
+ normalizeHttpError,
30
+ toError
31
+ } from "../chunk-QNJO7DSD.js";
23
32
 
24
33
  // src/providers/google/transform.ts
25
34
  function transformRequest(request, modelId) {
@@ -28,14 +37,15 @@ function transformRequest(request, modelId) {
28
37
  const googleRequest = {
29
38
  contents: transformMessages(request.messages)
30
39
  };
31
- if (request.system) {
32
- if (typeof request.system === "string") {
40
+ const normalizedSystem = normalizeSystem(request.system);
41
+ if (normalizedSystem !== void 0) {
42
+ if (typeof normalizedSystem === "string") {
33
43
  googleRequest.systemInstruction = {
34
- parts: [{ text: request.system }]
44
+ parts: [{ text: normalizedSystem }]
35
45
  };
36
- } else {
46
+ } else if (normalizedSystem.length > 0) {
37
47
  googleRequest.systemInstruction = {
38
- parts: request.system
48
+ parts: normalizedSystem
39
49
  };
40
50
  }
41
51
  }
@@ -69,6 +79,56 @@ function transformRequest(request, modelId) {
69
79
  }
70
80
  return googleRequest;
71
81
  }
82
+ function normalizeSystem(system) {
83
+ if (system === void 0 || system === null) return void 0;
84
+ if (typeof system === "string") return system;
85
+ if (!Array.isArray(system)) {
86
+ throw new UPPError(
87
+ "System prompt must be a string or an array of text parts",
88
+ ErrorCode.InvalidRequest,
89
+ "google",
90
+ ModalityType.LLM
91
+ );
92
+ }
93
+ const parts = [];
94
+ for (const part of system) {
95
+ if (!part || typeof part !== "object" || !("text" in part)) {
96
+ throw new UPPError(
97
+ "Google system prompt array must contain text parts",
98
+ ErrorCode.InvalidRequest,
99
+ "google",
100
+ ModalityType.LLM
101
+ );
102
+ }
103
+ const textValue = part.text;
104
+ if (typeof textValue !== "string") {
105
+ throw new UPPError(
106
+ "Google system prompt text must be a string",
107
+ ErrorCode.InvalidRequest,
108
+ "google",
109
+ ModalityType.LLM
110
+ );
111
+ }
112
+ parts.push(part);
113
+ }
114
+ return parts.length > 0 ? parts : void 0;
115
+ }
116
+ var GOOGLE_TOOLCALL_PREFIX = "google_toolcall";
117
+ function createGoogleToolCallId(name, index) {
118
+ return `${GOOGLE_TOOLCALL_PREFIX}:${index}:${name}`;
119
+ }
120
+ function extractGoogleToolName(toolCallId) {
121
+ const prefix = `${GOOGLE_TOOLCALL_PREFIX}:`;
122
+ if (!toolCallId.startsWith(prefix)) {
123
+ return toolCallId;
124
+ }
125
+ const rest = toolCallId.slice(prefix.length);
126
+ const separatorIndex = rest.indexOf(":");
127
+ if (separatorIndex === -1) {
128
+ return toolCallId;
129
+ }
130
+ return rest.slice(separatorIndex + 1);
131
+ }
72
132
  function filterValidContent(content) {
73
133
  return content.filter((c) => c && typeof c.type === "string");
74
134
  }
@@ -124,7 +184,7 @@ function transformMessages(messages) {
124
184
  role: "user",
125
185
  parts: msg.results.map((result) => ({
126
186
  functionResponse: {
127
- name: result.toolCallId,
187
+ name: extractGoogleToolName(result.toolCallId),
128
188
  response: typeof result.result === "object" ? result.result : { result: result.result }
129
189
  }
130
190
  }))
@@ -191,8 +251,9 @@ function transformResponse(data) {
191
251
  }
192
252
  } else if ("functionCall" in part) {
193
253
  const fc = part;
254
+ const toolCallId = createGoogleToolCallId(fc.functionCall.name, toolCalls.length);
194
255
  toolCalls.push({
195
- toolCallId: fc.functionCall.name,
256
+ toolCallId,
196
257
  toolName: fc.functionCall.name,
197
258
  arguments: fc.functionCall.args
198
259
  });
@@ -234,7 +295,7 @@ ${codeResult.codeExecutionResult.output}\`\`\`
234
295
  return {
235
296
  message,
236
297
  usage,
237
- stopReason: candidate.finishReason ?? "STOP",
298
+ stopReason: normalizeStopReason(candidate.finishReason),
238
299
  data: structuredData
239
300
  };
240
301
  }
@@ -252,7 +313,7 @@ function createStreamState() {
252
313
  function transformStreamChunk(chunk, state) {
253
314
  const events = [];
254
315
  if (state.isFirstChunk) {
255
- events.push({ type: "message_start", index: 0, delta: {} });
316
+ events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
256
317
  state.isFirstChunk = false;
257
318
  }
258
319
  if (chunk.usageMetadata) {
@@ -268,22 +329,24 @@ function transformStreamChunk(chunk, state) {
268
329
  if ("text" in part) {
269
330
  state.content += part.text;
270
331
  events.push({
271
- type: "text_delta",
332
+ type: StreamEventType.TextDelta,
272
333
  index: 0,
273
334
  delta: { text: part.text }
274
335
  });
275
336
  } else if ("functionCall" in part) {
276
337
  const fc = part;
338
+ const toolCallId = createGoogleToolCallId(fc.functionCall.name, state.toolCalls.length);
277
339
  state.toolCalls.push({
340
+ id: toolCallId,
278
341
  name: fc.functionCall.name,
279
342
  args: fc.functionCall.args,
280
343
  thoughtSignature: fc.thoughtSignature
281
344
  });
282
345
  events.push({
283
- type: "tool_call_delta",
346
+ type: StreamEventType.ToolCallDelta,
284
347
  index: state.toolCalls.length - 1,
285
348
  delta: {
286
- toolCallId: fc.functionCall.name,
349
+ toolCallId,
287
350
  toolName: fc.functionCall.name,
288
351
  argumentsJson: JSON.stringify(fc.functionCall.args)
289
352
  }
@@ -297,7 +360,7 @@ ${codeResult.codeExecutionResult.output}\`\`\`
297
360
  `;
298
361
  state.content += outputText;
299
362
  events.push({
300
- type: "text_delta",
363
+ type: StreamEventType.TextDelta,
301
364
  index: 0,
302
365
  delta: { text: outputText }
303
366
  });
@@ -306,7 +369,7 @@ ${codeResult.codeExecutionResult.output}\`\`\`
306
369
  }
307
370
  if (candidate.finishReason) {
308
371
  state.finishReason = candidate.finishReason;
309
- events.push({ type: "message_stop", index: 0, delta: {} });
372
+ events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
310
373
  }
311
374
  return events;
312
375
  }
@@ -323,8 +386,9 @@ function buildResponseFromState(state) {
323
386
  }
324
387
  }
325
388
  for (const tc of state.toolCalls) {
389
+ const toolCallId = tc.id || createGoogleToolCallId(tc.name, toolCalls.length);
326
390
  toolCalls.push({
327
- toolCallId: tc.name,
391
+ toolCallId,
328
392
  toolName: tc.name,
329
393
  arguments: tc.args
330
394
  });
@@ -356,10 +420,25 @@ function buildResponseFromState(state) {
356
420
  return {
357
421
  message,
358
422
  usage,
359
- stopReason: state.finishReason ?? "STOP",
423
+ stopReason: normalizeStopReason(state.finishReason),
360
424
  data: structuredData
361
425
  };
362
426
  }
427
+ function normalizeStopReason(reason) {
428
+ switch (reason) {
429
+ case "STOP":
430
+ return "end_turn";
431
+ case "MAX_TOKENS":
432
+ return "max_tokens";
433
+ case "SAFETY":
434
+ case "RECITATION":
435
+ return "content_filter";
436
+ case "OTHER":
437
+ return "end_turn";
438
+ default:
439
+ return "end_turn";
440
+ }
441
+ }
363
442
 
364
443
  // src/providers/google/llm.ts
365
444
  var GOOGLE_API_BASE = "https://generativelanguage.googleapis.com/v1beta";
@@ -371,9 +450,8 @@ var GOOGLE_CAPABILITIES = {
371
450
  videoInput: true,
372
451
  audioInput: true
373
452
  };
374
- function buildUrl(modelId, action, apiKey) {
375
- const base = `${GOOGLE_API_BASE}/models/${modelId}:${action}`;
376
- return `${base}?key=${apiKey}`;
453
+ function buildUrl(modelId, action) {
454
+ return `${GOOGLE_API_BASE}/models/${modelId}:${action}`;
377
455
  }
378
456
  function createLLMHandler() {
379
457
  let providerRef = null;
@@ -385,9 +463,9 @@ function createLLMHandler() {
385
463
  if (!providerRef) {
386
464
  throw new UPPError(
387
465
  "Provider reference not set. Handler must be used with createProvider().",
388
- "INVALID_REQUEST",
466
+ ErrorCode.InvalidRequest,
389
467
  "google",
390
- "llm"
468
+ ModalityType.LLM
391
469
  );
392
470
  }
393
471
  const model = {
@@ -403,10 +481,11 @@ function createLLMHandler() {
403
481
  "google",
404
482
  "llm"
405
483
  );
406
- const url = request.config.baseUrl ? `${request.config.baseUrl}/models/${modelId}:generateContent?key=${apiKey}` : buildUrl(modelId, "generateContent", apiKey);
484
+ const url = request.config.baseUrl ? `${request.config.baseUrl}/models/${modelId}:generateContent` : buildUrl(modelId, "generateContent");
407
485
  const body = transformRequest(request, modelId);
408
486
  const headers = {
409
- "Content-Type": "application/json"
487
+ "Content-Type": "application/json",
488
+ "x-goog-api-key": apiKey
410
489
  };
411
490
  if (request.config.headers) {
412
491
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -427,7 +506,7 @@ function createLLMHandler() {
427
506
  "google",
428
507
  "llm"
429
508
  );
430
- const data = await response.json();
509
+ const data = await parseJsonResponse(response, "google", "llm");
431
510
  return transformResponse(data);
432
511
  },
433
512
  stream(request) {
@@ -446,10 +525,12 @@ function createLLMHandler() {
446
525
  "google",
447
526
  "llm"
448
527
  );
449
- const url = request.config.baseUrl ? `${request.config.baseUrl}/models/${modelId}:streamGenerateContent?alt=sse&key=${apiKey}` : `${buildUrl(modelId, "streamGenerateContent", apiKey)}&alt=sse`;
528
+ const url = request.config.baseUrl ? `${request.config.baseUrl}/models/${modelId}:streamGenerateContent?alt=sse` : `${buildUrl(modelId, "streamGenerateContent")}?alt=sse`;
450
529
  const body = transformRequest(request, modelId);
451
530
  const headers = {
452
- "Content-Type": "application/json"
531
+ "Content-Type": "application/json",
532
+ Accept: "text/event-stream",
533
+ "x-goog-api-key": apiKey
453
534
  };
454
535
  if (request.config.headers) {
455
536
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -478,9 +559,9 @@ function createLLMHandler() {
478
559
  if (!response.body) {
479
560
  const error = new UPPError(
480
561
  "No response body for streaming request",
481
- "PROVIDER_ERROR",
562
+ ErrorCode.ProviderError,
482
563
  "google",
483
- "llm"
564
+ ModalityType.LLM
484
565
  );
485
566
  responseReject(error);
486
567
  throw error;
@@ -491,9 +572,9 @@ function createLLMHandler() {
491
572
  if (chunk.error) {
492
573
  const error = new UPPError(
493
574
  chunk.error.message,
494
- "PROVIDER_ERROR",
575
+ ErrorCode.ProviderError,
495
576
  "google",
496
- "llm"
577
+ ModalityType.LLM
497
578
  );
498
579
  responseReject(error);
499
580
  throw error;
@@ -506,8 +587,9 @@ function createLLMHandler() {
506
587
  }
507
588
  responseResolve(buildResponseFromState(state));
508
589
  } catch (error) {
509
- responseReject(error);
510
- throw error;
590
+ const err = toError(error);
591
+ responseReject(err);
592
+ throw err;
511
593
  }
512
594
  }
513
595
  return {
@@ -536,9 +618,9 @@ function createEmbeddingHandler() {
536
618
  if (!providerRef) {
537
619
  throw new UPPError(
538
620
  "Provider reference not set. Handler must be used with createProvider().",
539
- "INVALID_REQUEST",
621
+ ErrorCode.InvalidRequest,
540
622
  "google",
541
- "embedding"
623
+ ModalityType.Embedding
542
624
  );
543
625
  }
544
626
  const model = {
@@ -562,9 +644,9 @@ function createEmbeddingHandler() {
562
644
  if (!text) {
563
645
  throw new UPPError(
564
646
  "Google embeddings only support text input",
565
- "INVALID_REQUEST",
647
+ ErrorCode.InvalidRequest,
566
648
  "google",
567
- "embedding"
649
+ ModalityType.Embedding
568
650
  );
569
651
  }
570
652
  const embedRequest = {
@@ -574,9 +656,10 @@ function createEmbeddingHandler() {
574
656
  };
575
657
  return embedRequest;
576
658
  });
577
- const url = `${baseUrl}/models/${modelId}:batchEmbedContents?key=${apiKey}`;
659
+ const url = `${baseUrl}/models/${modelId}:batchEmbedContents`;
578
660
  const headers = {
579
- "Content-Type": "application/json"
661
+ "Content-Type": "application/json",
662
+ "x-goog-api-key": apiKey
580
663
  };
581
664
  if (request.config.headers) {
582
665
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -591,7 +674,7 @@ function createEmbeddingHandler() {
591
674
  body: JSON.stringify({ requests }),
592
675
  signal: request.signal
593
676
  }, request.config, "google", "embedding");
594
- const data = await response.json();
677
+ const data = await parseJsonResponse(response, "google", "embedding");
595
678
  let totalTokens = 0;
596
679
  for (const emb of data.embeddings) {
597
680
  totalTokens += emb.statistics?.tokenCount ?? 0;
@@ -635,9 +718,9 @@ function createImageHandler() {
635
718
  if (!providerRef) {
636
719
  throw new UPPError(
637
720
  "Provider reference not set. Handler must be used with createProvider().",
638
- "INVALID_REQUEST",
721
+ ErrorCode.InvalidRequest,
639
722
  "google",
640
- "image"
723
+ ModalityType.Image
641
724
  );
642
725
  }
643
726
  const capabilities = getCapabilities();
@@ -687,7 +770,7 @@ async function executeGenerate(modelId, request) {
687
770
  body: JSON.stringify(body),
688
771
  signal: request.signal
689
772
  }, request.config, "google", "image");
690
- const data = await response.json();
773
+ const data = await parseJsonResponse(response, "google", "image");
691
774
  return transformResponse2(data);
692
775
  }
693
776
  function buildParameters(params) {
@@ -697,9 +780,9 @@ function transformResponse2(data) {
697
780
  if (!data.predictions || data.predictions.length === 0) {
698
781
  throw new UPPError(
699
782
  "No images in response",
700
- "PROVIDER_ERROR",
783
+ ErrorCode.ProviderError,
701
784
  "google",
702
- "image"
785
+ ModalityType.Image
703
786
  );
704
787
  }
705
788
  const images = data.predictions.map((prediction) => {
@@ -716,10 +799,19 @@ function transformResponse2(data) {
716
799
  }
717
800
 
718
801
  // src/providers/google/cache.ts
719
- var CACHE_API_BASE = "https://generativelanguage.googleapis.com/v1beta/cachedContents";
802
+ var DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
803
+ function resolveBaseUrl(config) {
804
+ if (config?.baseUrl) {
805
+ const trimmed = config.baseUrl.replace(/\/$/, "");
806
+ return trimmed.endsWith("/v1beta") ? trimmed : `${trimmed}/v1beta`;
807
+ }
808
+ return DEFAULT_BASE_URL;
809
+ }
720
810
  async function create(options) {
721
811
  const {
722
812
  apiKey,
813
+ config,
814
+ signal,
723
815
  model,
724
816
  displayName,
725
817
  contents,
@@ -728,6 +820,8 @@ async function create(options) {
728
820
  ttl,
729
821
  expireTime
730
822
  } = options;
823
+ const baseUrl = resolveBaseUrl(config);
824
+ const requestConfig = { ...config, apiKey };
731
825
  const requestBody = {
732
826
  model: model.startsWith("models/") ? model : `models/${model}`
733
827
  };
@@ -750,56 +844,163 @@ async function create(options) {
750
844
  } else if (expireTime) {
751
845
  requestBody.expireTime = expireTime;
752
846
  }
753
- const response = await fetch(`${CACHE_API_BASE}?key=${apiKey}`, {
754
- method: "POST",
755
- headers: { "Content-Type": "application/json" },
756
- body: JSON.stringify(requestBody)
757
- });
758
- if (!response.ok) {
759
- throw await normalizeHttpError(response, "google", "llm");
847
+ const headers = {
848
+ "Content-Type": "application/json",
849
+ "x-goog-api-key": apiKey
850
+ };
851
+ if (config?.headers) {
852
+ for (const [key, value] of Object.entries(config.headers)) {
853
+ if (value !== void 0) {
854
+ headers[key] = value;
855
+ }
856
+ }
760
857
  }
761
- return response.json();
858
+ const response = await doFetch(
859
+ `${baseUrl}/cachedContents`,
860
+ {
861
+ method: "POST",
862
+ headers,
863
+ body: JSON.stringify(requestBody),
864
+ signal
865
+ },
866
+ requestConfig,
867
+ "google",
868
+ "llm"
869
+ );
870
+ return parseJsonResponse(response, "google", "llm");
762
871
  }
763
- async function get(name, apiKey) {
872
+ async function get(name, apiKey, config, signal) {
764
873
  const cacheName = name.startsWith("cachedContents/") ? name : `cachedContents/${name}`;
765
- const url = `https://generativelanguage.googleapis.com/v1beta/${cacheName}?key=${apiKey}`;
766
- const response = await fetch(url, { method: "GET" });
767
- if (!response.ok) {
768
- throw await normalizeHttpError(response, "google", "llm");
874
+ const baseUrl = resolveBaseUrl(config);
875
+ const url = `${baseUrl}/${cacheName}`;
876
+ const requestConfig = { ...config, apiKey };
877
+ const headers = { "x-goog-api-key": apiKey };
878
+ if (config?.headers) {
879
+ for (const [key, value] of Object.entries(config.headers)) {
880
+ if (value !== void 0) {
881
+ headers[key] = value;
882
+ }
883
+ }
769
884
  }
770
- return response.json();
885
+ const response = await doFetch(
886
+ url,
887
+ {
888
+ method: "GET",
889
+ headers,
890
+ signal
891
+ },
892
+ requestConfig,
893
+ "google",
894
+ "llm"
895
+ );
896
+ return parseJsonResponse(response, "google", "llm");
771
897
  }
772
898
  async function list(options) {
773
- const { apiKey, pageSize, pageToken } = options;
774
- const params = new URLSearchParams({ key: apiKey });
899
+ const { apiKey, config, signal, pageSize, pageToken } = options;
900
+ const baseUrl = resolveBaseUrl(config);
901
+ const requestConfig = { ...config, apiKey };
902
+ const params = new URLSearchParams();
775
903
  if (pageSize) params.set("pageSize", String(pageSize));
776
904
  if (pageToken) params.set("pageToken", pageToken);
777
- const response = await fetch(`${CACHE_API_BASE}?${params}`, { method: "GET" });
778
- if (!response.ok) {
779
- throw await normalizeHttpError(response, "google", "llm");
905
+ const headers = { "x-goog-api-key": apiKey };
906
+ if (config?.headers) {
907
+ for (const [key, value] of Object.entries(config.headers)) {
908
+ if (value !== void 0) {
909
+ headers[key] = value;
910
+ }
911
+ }
780
912
  }
781
- return response.json();
913
+ const response = await doFetch(
914
+ `${baseUrl}/cachedContents?${params}`,
915
+ {
916
+ method: "GET",
917
+ headers,
918
+ signal
919
+ },
920
+ requestConfig,
921
+ "google",
922
+ "llm"
923
+ );
924
+ return parseJsonResponse(response, "google", "llm");
782
925
  }
783
- async function update(name, updateRequest, apiKey) {
926
+ async function update(name, updateRequest, apiKey, config, signal) {
927
+ if (updateRequest.expireTime && updateRequest.ttl) {
928
+ throw new UPPError(
929
+ "Provide either expireTime or ttl (not both)",
930
+ ErrorCode.InvalidRequest,
931
+ "google",
932
+ ModalityType.LLM
933
+ );
934
+ }
935
+ const updateMaskParts = [];
936
+ if (updateRequest.expireTime) {
937
+ updateMaskParts.push("expireTime");
938
+ }
939
+ if (updateRequest.ttl) {
940
+ updateMaskParts.push("ttl");
941
+ }
942
+ if (updateMaskParts.length === 0) {
943
+ throw new UPPError(
944
+ "Update request must include expireTime or ttl",
945
+ ErrorCode.InvalidRequest,
946
+ "google",
947
+ ModalityType.LLM
948
+ );
949
+ }
784
950
  const cacheName = name.startsWith("cachedContents/") ? name : `cachedContents/${name}`;
785
- const url = `https://generativelanguage.googleapis.com/v1beta/${cacheName}?key=${apiKey}`;
786
- const response = await fetch(url, {
787
- method: "PATCH",
788
- headers: { "Content-Type": "application/json" },
789
- body: JSON.stringify(updateRequest)
790
- });
791
- if (!response.ok) {
792
- throw await normalizeHttpError(response, "google", "llm");
951
+ const baseUrl = resolveBaseUrl(config);
952
+ const params = new URLSearchParams({ updateMask: updateMaskParts.join(",") });
953
+ const url = `${baseUrl}/${cacheName}?${params.toString()}`;
954
+ const requestConfig = { ...config, apiKey };
955
+ const headers = {
956
+ "Content-Type": "application/json",
957
+ "x-goog-api-key": apiKey
958
+ };
959
+ if (config?.headers) {
960
+ for (const [key, value] of Object.entries(config.headers)) {
961
+ if (value !== void 0) {
962
+ headers[key] = value;
963
+ }
964
+ }
793
965
  }
794
- return response.json();
966
+ const response = await doFetch(
967
+ url,
968
+ {
969
+ method: "PATCH",
970
+ headers,
971
+ body: JSON.stringify(updateRequest),
972
+ signal
973
+ },
974
+ requestConfig,
975
+ "google",
976
+ "llm"
977
+ );
978
+ return parseJsonResponse(response, "google", "llm");
795
979
  }
796
- async function deleteCache(name, apiKey) {
980
+ async function deleteCache(name, apiKey, config, signal) {
797
981
  const cacheName = name.startsWith("cachedContents/") ? name : `cachedContents/${name}`;
798
- const url = `https://generativelanguage.googleapis.com/v1beta/${cacheName}?key=${apiKey}`;
799
- const response = await fetch(url, { method: "DELETE" });
800
- if (!response.ok) {
801
- throw await normalizeHttpError(response, "google", "llm");
982
+ const baseUrl = resolveBaseUrl(config);
983
+ const url = `${baseUrl}/${cacheName}`;
984
+ const requestConfig = { ...config, apiKey };
985
+ const headers = { "x-goog-api-key": apiKey };
986
+ if (config?.headers) {
987
+ for (const [key, value] of Object.entries(config.headers)) {
988
+ if (value !== void 0) {
989
+ headers[key] = value;
990
+ }
991
+ }
802
992
  }
993
+ const response = await doFetch(
994
+ url,
995
+ {
996
+ method: "DELETE",
997
+ headers,
998
+ signal
999
+ },
1000
+ requestConfig,
1001
+ "google",
1002
+ "llm"
1003
+ );
803
1004
  }
804
1005
  var cache = {
805
1006
  create,