@ai-sdk/google 3.0.89 → 3.0.91

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.
@@ -277,6 +277,8 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId) {
277
277
  });
278
278
  break;
279
279
  case "image-data":
280
+ case "file-data": {
281
+ const topLevelMediaType = String(contentPart.mediaType).split("/")[0];
280
282
  parts.push(
281
283
  {
282
284
  inlineData: {
@@ -285,10 +287,11 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId) {
285
287
  }
286
288
  },
287
289
  {
288
- text: "Tool executed successfully and returned this image as a response"
290
+ text: `Tool executed successfully and returned this ${topLevelMediaType === "image" ? "image" : "file"} as a response`
289
291
  }
290
292
  );
291
293
  break;
294
+ }
292
295
  default:
293
296
  parts.push({ text: JSON.stringify(contentPart) });
294
297
  break;
@@ -490,7 +493,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
490
493
  name: part.toolName,
491
494
  response: {
492
495
  name: part.toolName,
493
- content: output.type === "execution-denied" ? (_e = output.reason) != null ? _e : "Tool execution denied." : output.value
496
+ content: output.type === "execution-denied" ? (_e = output.reason) != null ? _e : "Tool call execution denied." : output.value
494
497
  }
495
498
  }
496
499
  });
@@ -1228,6 +1231,12 @@ function mapGoogleGenerativeAIFinishReason({
1228
1231
  }
1229
1232
 
1230
1233
  // src/google-generative-ai-language-model.ts
1234
+ var configurableSafetySettingCategories = [
1235
+ "HARM_CATEGORY_HATE_SPEECH",
1236
+ "HARM_CATEGORY_DANGEROUS_CONTENT",
1237
+ "HARM_CATEGORY_HARASSMENT",
1238
+ "HARM_CATEGORY_SEXUALLY_EXPLICIT"
1239
+ ];
1231
1240
  var GoogleGenerativeAILanguageModel = class {
1232
1241
  constructor(modelId, config) {
1233
1242
  this.specificationVersion = "v3";
@@ -1258,7 +1267,7 @@ var GoogleGenerativeAILanguageModel = class {
1258
1267
  toolChoice,
1259
1268
  providerOptions
1260
1269
  }, { isStreaming = false } = {}) {
1261
- var _a, _b;
1270
+ var _a, _b, _c;
1262
1271
  const warnings = [];
1263
1272
  const providerOptionsName = this.config.provider.includes("vertex") ? "vertex" : "google";
1264
1273
  let googleOptions = await parseProviderOptions({
@@ -1333,6 +1342,11 @@ var GoogleGenerativeAILanguageModel = class {
1333
1342
  isVertexProvider
1334
1343
  });
1335
1344
  const streamFunctionCallArguments = isStreaming && isVertexProvider ? (_a = googleOptions == null ? void 0 : googleOptions.streamFunctionCallArguments) != null ? _a : false : void 0;
1345
+ const safetyThreshold = googleOptions == null ? void 0 : googleOptions.threshold;
1346
+ const safetySettings = (_b = googleOptions == null ? void 0 : googleOptions.safetySettings) != null ? _b : safetyThreshold != null ? configurableSafetySettingCategories.map((category) => ({
1347
+ category,
1348
+ threshold: safetyThreshold
1349
+ })) : void 0;
1336
1350
  const toolConfig = googleToolConfig || streamFunctionCallArguments || (googleOptions == null ? void 0 : googleOptions.retrievalConfig) ? {
1337
1351
  ...googleToolConfig,
1338
1352
  ...streamFunctionCallArguments && {
@@ -1362,7 +1376,7 @@ var GoogleGenerativeAILanguageModel = class {
1362
1376
  responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
1363
1377
  // so this is needed as an escape hatch:
1364
1378
  // TODO convert into provider option
1365
- ((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
1379
+ ((_c = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _c : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
1366
1380
  ...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
1367
1381
  audioTimestamp: googleOptions.audioTimestamp
1368
1382
  },
@@ -1378,7 +1392,7 @@ var GoogleGenerativeAILanguageModel = class {
1378
1392
  },
1379
1393
  contents,
1380
1394
  systemInstruction: isGemmaModel ? void 0 : systemInstruction,
1381
- safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
1395
+ safetySettings,
1382
1396
  tools: googleTools2,
1383
1397
  toolConfig,
1384
1398
  cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
@@ -2456,8 +2470,2935 @@ var googleTools = {
2456
2470
  */
2457
2471
  vertexRagStore
2458
2472
  };
2473
+
2474
+ // src/interactions/google-interactions-language-model.ts
2475
+ import {
2476
+ combineHeaders as combineHeaders3,
2477
+ createEventSourceResponseHandler as createEventSourceResponseHandler3,
2478
+ createJsonResponseHandler as createJsonResponseHandler3,
2479
+ generateId as defaultGenerateId,
2480
+ parseProviderOptions as parseProviderOptions2,
2481
+ postJsonToApi as postJsonToApi2,
2482
+ resolve as resolve2
2483
+ } from "@ai-sdk/provider-utils";
2484
+
2485
+ // src/interactions/convert-google-interactions-usage.ts
2486
+ function convertGoogleInteractionsUsage(usage) {
2487
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2488
+ if (usage == null) {
2489
+ return {
2490
+ inputTokens: {
2491
+ total: void 0,
2492
+ noCache: void 0,
2493
+ cacheRead: void 0,
2494
+ cacheWrite: void 0
2495
+ },
2496
+ outputTokens: {
2497
+ total: void 0,
2498
+ text: void 0,
2499
+ reasoning: void 0
2500
+ },
2501
+ raw: void 0
2502
+ };
2503
+ }
2504
+ const totalInput = (_a = usage.total_input_tokens) != null ? _a : 0;
2505
+ const totalOutput = (_b = usage.total_output_tokens) != null ? _b : 0;
2506
+ const totalThought = (_c = usage.total_thought_tokens) != null ? _c : 0;
2507
+ const totalCached = (_d = usage.total_cached_tokens) != null ? _d : 0;
2508
+ return {
2509
+ inputTokens: {
2510
+ total: (_e = usage.total_input_tokens) != null ? _e : void 0,
2511
+ noCache: usage.total_input_tokens == null ? void 0 : totalInput - totalCached,
2512
+ cacheRead: (_f = usage.total_cached_tokens) != null ? _f : void 0,
2513
+ cacheWrite: void 0
2514
+ },
2515
+ outputTokens: {
2516
+ total: usage.total_output_tokens == null && usage.total_thought_tokens == null ? void 0 : totalOutput + totalThought,
2517
+ text: (_g = usage.total_output_tokens) != null ? _g : void 0,
2518
+ reasoning: (_h = usage.total_thought_tokens) != null ? _h : void 0
2519
+ },
2520
+ raw: usage
2521
+ };
2522
+ }
2523
+ function getGoogleInteractionsOutputTokensByModality(usage) {
2524
+ const byModality = usage == null ? void 0 : usage.output_tokens_by_modality;
2525
+ if (byModality == null) {
2526
+ return void 0;
2527
+ }
2528
+ const result = {};
2529
+ for (const entry of byModality) {
2530
+ if ((entry == null ? void 0 : entry.modality) != null && entry.tokens != null) {
2531
+ result[entry.modality] = entry.tokens;
2532
+ }
2533
+ }
2534
+ return Object.keys(result).length > 0 ? result : void 0;
2535
+ }
2536
+
2537
+ // src/interactions/extract-google-interactions-sources.ts
2538
+ var KNOWN_DOC_EXTENSIONS = {
2539
+ pdf: "application/pdf",
2540
+ txt: "text/plain",
2541
+ md: "text/markdown",
2542
+ markdown: "text/markdown",
2543
+ doc: "application/msword",
2544
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
2545
+ };
2546
+ function inferDocMediaType(uriOrName) {
2547
+ const lower = uriOrName.toLowerCase();
2548
+ for (const [ext, media] of Object.entries(KNOWN_DOC_EXTENSIONS)) {
2549
+ if (lower.endsWith(`.${ext}`)) return media;
2550
+ }
2551
+ return "application/octet-stream";
2552
+ }
2553
+ function basename(uriOrName) {
2554
+ const parts = uriOrName.split("/");
2555
+ const last = parts[parts.length - 1];
2556
+ return last && last.length > 0 ? last : void 0;
2557
+ }
2558
+ function annotationToSource({
2559
+ annotation,
2560
+ generateId: generateId2
2561
+ }) {
2562
+ var _a, _b, _c, _d, _e;
2563
+ switch (annotation.type) {
2564
+ case "url_citation": {
2565
+ const a = annotation;
2566
+ if (a.url == null || a.url.length === 0) return void 0;
2567
+ return {
2568
+ type: "source",
2569
+ sourceType: "url",
2570
+ id: generateId2(),
2571
+ url: a.url,
2572
+ ...a.title != null ? { title: a.title } : {}
2573
+ };
2574
+ }
2575
+ case "file_citation": {
2576
+ const a = annotation;
2577
+ const uri = (_b = (_a = a.url) != null ? _a : a.document_uri) != null ? _b : a.file_name;
2578
+ if (uri == null || uri.length === 0) return void 0;
2579
+ if (uri.startsWith("http://") || uri.startsWith("https://")) {
2580
+ return {
2581
+ type: "source",
2582
+ sourceType: "url",
2583
+ id: generateId2(),
2584
+ url: uri,
2585
+ ...a.file_name != null ? { title: a.file_name } : {}
2586
+ };
2587
+ }
2588
+ const filename = (_c = a.file_name) != null ? _c : basename(uri);
2589
+ const mediaType = inferDocMediaType(uri);
2590
+ return {
2591
+ type: "source",
2592
+ sourceType: "document",
2593
+ id: generateId2(),
2594
+ mediaType,
2595
+ title: (_e = (_d = a.file_name) != null ? _d : filename) != null ? _e : uri,
2596
+ ...filename != null ? { filename } : {}
2597
+ };
2598
+ }
2599
+ case "place_citation": {
2600
+ const a = annotation;
2601
+ if (a.url == null || a.url.length === 0) return void 0;
2602
+ return {
2603
+ type: "source",
2604
+ sourceType: "url",
2605
+ id: generateId2(),
2606
+ url: a.url,
2607
+ ...a.name != null ? { title: a.name } : {}
2608
+ };
2609
+ }
2610
+ default:
2611
+ return void 0;
2612
+ }
2613
+ }
2614
+ function builtinToolResultToSources({
2615
+ block,
2616
+ generateId: generateId2
2617
+ }) {
2618
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2619
+ const sources = [];
2620
+ switch (block.type) {
2621
+ case "url_context_result": {
2622
+ const result = (_a = block.result) != null ? _a : [];
2623
+ for (const entry of result) {
2624
+ if ((entry == null ? void 0 : entry.url) == null || entry.url.length === 0) continue;
2625
+ if (entry.status != null && entry.status !== "success") continue;
2626
+ sources.push({
2627
+ type: "source",
2628
+ sourceType: "url",
2629
+ id: generateId2(),
2630
+ url: entry.url
2631
+ });
2632
+ }
2633
+ break;
2634
+ }
2635
+ case "google_search_result": {
2636
+ const result = (_b = block.result) != null ? _b : [];
2637
+ for (const entry of result) {
2638
+ const url = entry == null ? void 0 : entry.url;
2639
+ if (url == null || url.length === 0) continue;
2640
+ sources.push({
2641
+ type: "source",
2642
+ sourceType: "url",
2643
+ id: generateId2(),
2644
+ url,
2645
+ ...entry.title != null ? { title: entry.title } : {}
2646
+ });
2647
+ }
2648
+ break;
2649
+ }
2650
+ case "google_maps_result": {
2651
+ const result = (_c = block.result) != null ? _c : [];
2652
+ for (const entry of result) {
2653
+ for (const place of (_d = entry.places) != null ? _d : []) {
2654
+ if (place.url == null || place.url.length === 0) continue;
2655
+ sources.push({
2656
+ type: "source",
2657
+ sourceType: "url",
2658
+ id: generateId2(),
2659
+ url: place.url,
2660
+ ...place.name != null ? { title: place.name } : {}
2661
+ });
2662
+ }
2663
+ }
2664
+ break;
2665
+ }
2666
+ case "file_search_result": {
2667
+ const result = (_e = block.result) != null ? _e : [];
2668
+ for (const raw of result) {
2669
+ if (raw == null || typeof raw !== "object") continue;
2670
+ const entry = raw;
2671
+ const uri = (_g = (_f = entry.url) != null ? _f : entry.document_uri) != null ? _g : entry.file_name;
2672
+ if (uri == null || uri.length === 0) continue;
2673
+ if (uri.startsWith("http://") || uri.startsWith("https://")) {
2674
+ sources.push({
2675
+ type: "source",
2676
+ sourceType: "url",
2677
+ id: generateId2(),
2678
+ url: uri,
2679
+ ...entry.title != null ? { title: entry.title } : {}
2680
+ });
2681
+ continue;
2682
+ }
2683
+ const filename = (_h = entry.file_name) != null ? _h : basename(uri);
2684
+ const mediaType = inferDocMediaType(uri);
2685
+ sources.push({
2686
+ type: "source",
2687
+ sourceType: "document",
2688
+ id: generateId2(),
2689
+ mediaType,
2690
+ title: (_k = (_j = (_i = entry.title) != null ? _i : entry.file_name) != null ? _j : filename) != null ? _k : uri,
2691
+ ...filename != null ? { filename } : {}
2692
+ });
2693
+ }
2694
+ break;
2695
+ }
2696
+ default:
2697
+ break;
2698
+ }
2699
+ return sources;
2700
+ }
2701
+ function annotationsToSources({
2702
+ annotations,
2703
+ generateId: generateId2
2704
+ }) {
2705
+ var _a;
2706
+ if (annotations == null) return [];
2707
+ const seen = /* @__PURE__ */ new Set();
2708
+ const sources = [];
2709
+ for (const annotation of annotations) {
2710
+ const source = annotationToSource({ annotation, generateId: generateId2 });
2711
+ if (source == null) continue;
2712
+ const key = source.sourceType === "url" ? `url:${source.url}` : `doc:${(_a = source.filename) != null ? _a : source.title}`;
2713
+ if (seen.has(key)) continue;
2714
+ seen.add(key);
2715
+ sources.push(source);
2716
+ }
2717
+ return sources;
2718
+ }
2719
+
2720
+ // src/interactions/map-google-interactions-finish-reason.ts
2721
+ function mapGoogleInteractionsFinishReason({
2722
+ status,
2723
+ hasFunctionCall
2724
+ }) {
2725
+ switch (status) {
2726
+ case "completed":
2727
+ return hasFunctionCall ? "tool-calls" : "stop";
2728
+ case "requires_action":
2729
+ return "tool-calls";
2730
+ case "failed":
2731
+ return "error";
2732
+ case "incomplete":
2733
+ return "length";
2734
+ case "cancelled":
2735
+ return "other";
2736
+ case "in_progress":
2737
+ default:
2738
+ return "other";
2739
+ }
2740
+ }
2741
+
2742
+ // src/interactions/build-google-interactions-stream-transform.ts
2743
+ var BUILTIN_TOOL_CALL_TYPES = /* @__PURE__ */ new Set([
2744
+ "google_search_call",
2745
+ "code_execution_call",
2746
+ "url_context_call",
2747
+ "file_search_call",
2748
+ "google_maps_call",
2749
+ "mcp_server_tool_call"
2750
+ ]);
2751
+ var BUILTIN_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
2752
+ "google_search_result",
2753
+ "code_execution_result",
2754
+ "url_context_result",
2755
+ "file_search_result",
2756
+ "google_maps_result",
2757
+ "mcp_server_tool_result"
2758
+ ]);
2759
+ function builtinToolNameFromCallType(type) {
2760
+ return type.replace(/_call$/, "");
2761
+ }
2762
+ function builtinToolNameFromResultType(type) {
2763
+ return type.replace(/_result$/, "");
2764
+ }
2765
+ function buildGoogleInteractionsStreamTransform({
2766
+ warnings,
2767
+ generateId: generateId2,
2768
+ includeRawChunks,
2769
+ serviceTier: headerServiceTier
2770
+ }) {
2771
+ let interactionId;
2772
+ let usage;
2773
+ let serviceTier = headerServiceTier;
2774
+ let finishStatus;
2775
+ let hasFunctionCall = false;
2776
+ const openBlocks = /* @__PURE__ */ new Map();
2777
+ const emittedSourceKeys = /* @__PURE__ */ new Set();
2778
+ function sourceKey(source) {
2779
+ var _a;
2780
+ return source.sourceType === "url" ? `url:${source.url}` : `doc:${(_a = source.filename) != null ? _a : source.title}`;
2781
+ }
2782
+ return new TransformStream({
2783
+ start(controller) {
2784
+ controller.enqueue({ type: "stream-start", warnings });
2785
+ },
2786
+ transform(chunk, controller) {
2787
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
2788
+ if (includeRawChunks) {
2789
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2790
+ }
2791
+ if (!chunk.success) {
2792
+ finishStatus = "failed";
2793
+ controller.enqueue({ type: "error", error: chunk.error });
2794
+ return;
2795
+ }
2796
+ const value = chunk.value;
2797
+ const eventType = value.event_type;
2798
+ switch (eventType) {
2799
+ case "interaction.created": {
2800
+ const event = value;
2801
+ const interaction = event.interaction;
2802
+ interactionId = (interaction == null ? void 0 : interaction.id) != null && interaction.id.length > 0 ? interaction.id : void 0;
2803
+ const created = interaction == null ? void 0 : interaction.created;
2804
+ let timestamp;
2805
+ if (typeof created === "string") {
2806
+ const parsed = new Date(created);
2807
+ if (!Number.isNaN(parsed.getTime())) {
2808
+ timestamp = parsed;
2809
+ }
2810
+ }
2811
+ controller.enqueue({
2812
+ type: "response-metadata",
2813
+ ...interactionId != null ? { id: interactionId } : {},
2814
+ modelId: interaction == null ? void 0 : interaction.model,
2815
+ ...timestamp ? { timestamp } : {}
2816
+ });
2817
+ break;
2818
+ }
2819
+ case "step.start": {
2820
+ const event = value;
2821
+ const step = event.step;
2822
+ const index = event.index;
2823
+ const blockId = `${interactionId != null ? interactionId : "interaction"}:${index}`;
2824
+ const stepType = step == null ? void 0 : step.type;
2825
+ if (stepType === "model_output") {
2826
+ const initial = (_a = step == null ? void 0 : step.content) == null ? void 0 : _a[0];
2827
+ if ((initial == null ? void 0 : initial.type) === "text") {
2828
+ openBlocks.set(index, {
2829
+ kind: "text",
2830
+ id: blockId,
2831
+ emittedSourceKeys: /* @__PURE__ */ new Set()
2832
+ });
2833
+ controller.enqueue({ type: "text-start", id: blockId });
2834
+ const initialSources = annotationsToSources({
2835
+ annotations: initial.annotations,
2836
+ generateId: generateId2
2837
+ });
2838
+ for (const source of initialSources) {
2839
+ const key = sourceKey(source);
2840
+ if (emittedSourceKeys.has(key)) continue;
2841
+ emittedSourceKeys.add(key);
2842
+ controller.enqueue(source);
2843
+ }
2844
+ } else if ((initial == null ? void 0 : initial.type) === "image") {
2845
+ openBlocks.set(index, {
2846
+ kind: "image",
2847
+ id: blockId,
2848
+ ...initial.data != null ? { data: initial.data } : {},
2849
+ ...initial.mime_type != null ? { mimeType: initial.mime_type } : {},
2850
+ ...initial.uri != null ? { uri: initial.uri } : {}
2851
+ });
2852
+ } else {
2853
+ openBlocks.set(index, {
2854
+ kind: "pending_model_output",
2855
+ id: blockId
2856
+ });
2857
+ }
2858
+ } else if (stepType === "thought") {
2859
+ const signature = step == null ? void 0 : step.signature;
2860
+ openBlocks.set(index, {
2861
+ kind: "reasoning",
2862
+ id: blockId,
2863
+ ...signature != null ? { signature } : {}
2864
+ });
2865
+ controller.enqueue({ type: "reasoning-start", id: blockId });
2866
+ if (Array.isArray(step == null ? void 0 : step.summary)) {
2867
+ for (const item of step.summary) {
2868
+ if ((item == null ? void 0 : item.type) === "text" && typeof item.text === "string") {
2869
+ controller.enqueue({
2870
+ type: "reasoning-delta",
2871
+ id: blockId,
2872
+ delta: item.text
2873
+ });
2874
+ }
2875
+ }
2876
+ }
2877
+ } else if (stepType === "function_call") {
2878
+ const toolCallId = (_b = step == null ? void 0 : step.id) != null ? _b : blockId;
2879
+ const toolName = (_c = step == null ? void 0 : step.name) != null ? _c : "unknown";
2880
+ hasFunctionCall = true;
2881
+ const state = {
2882
+ kind: "function_call",
2883
+ id: blockId,
2884
+ toolCallId,
2885
+ toolName,
2886
+ argumentsAccum: "",
2887
+ ...(step == null ? void 0 : step.signature) != null ? { signature: step.signature } : {}
2888
+ };
2889
+ openBlocks.set(index, state);
2890
+ controller.enqueue({
2891
+ type: "tool-input-start",
2892
+ id: toolCallId,
2893
+ toolName
2894
+ });
2895
+ } else if (stepType != null && BUILTIN_TOOL_CALL_TYPES.has(stepType)) {
2896
+ const toolName = stepType === "mcp_server_tool_call" ? (_d = step == null ? void 0 : step.name) != null ? _d : "mcp_server_tool" : builtinToolNameFromCallType(stepType);
2897
+ const toolCallId = (_e = step == null ? void 0 : step.id) != null ? _e : blockId;
2898
+ const state = {
2899
+ kind: "builtin_tool_call",
2900
+ id: blockId,
2901
+ blockType: stepType,
2902
+ toolCallId,
2903
+ toolName,
2904
+ arguments: (_f = step == null ? void 0 : step.arguments) != null ? _f : {},
2905
+ callEmitted: false
2906
+ };
2907
+ openBlocks.set(index, state);
2908
+ } else if (stepType != null && BUILTIN_TOOL_RESULT_TYPES.has(stepType)) {
2909
+ const toolName = stepType === "mcp_server_tool_result" ? (_g = step == null ? void 0 : step.name) != null ? _g : "mcp_server_tool" : builtinToolNameFromResultType(stepType);
2910
+ const callId = (_h = step == null ? void 0 : step.call_id) != null ? _h : blockId;
2911
+ const state = {
2912
+ kind: "builtin_tool_result",
2913
+ id: blockId,
2914
+ blockType: stepType,
2915
+ callId,
2916
+ toolName,
2917
+ result: (_i = step == null ? void 0 : step.result) != null ? _i : null,
2918
+ ...(step == null ? void 0 : step.is_error) != null ? { isError: step.is_error } : {},
2919
+ resultEmitted: false
2920
+ };
2921
+ openBlocks.set(index, state);
2922
+ } else {
2923
+ openBlocks.set(index, { kind: "unknown", id: blockId });
2924
+ }
2925
+ break;
2926
+ }
2927
+ case "step.delta": {
2928
+ const event = value;
2929
+ let open = openBlocks.get(event.index);
2930
+ if (open == null) break;
2931
+ const dtype = (_j = event.delta) == null ? void 0 : _j.type;
2932
+ if (open.kind === "pending_model_output") {
2933
+ if (dtype === "text" || dtype === "text_annotation" || dtype === "text_annotation_delta") {
2934
+ const promoted = {
2935
+ kind: "text",
2936
+ id: open.id,
2937
+ emittedSourceKeys: /* @__PURE__ */ new Set()
2938
+ };
2939
+ openBlocks.set(event.index, promoted);
2940
+ open = promoted;
2941
+ controller.enqueue({ type: "text-start", id: promoted.id });
2942
+ }
2943
+ }
2944
+ if (dtype === "image" && (open.kind === "pending_model_output" || open.kind === "text" || open.kind === "image")) {
2945
+ const img = event.delta;
2946
+ const google = {};
2947
+ if (interactionId != null) google.interactionId = interactionId;
2948
+ const providerMetadata = Object.keys(google).length > 0 ? { google } : void 0;
2949
+ if ((img == null ? void 0 : img.data) != null && img.data.length > 0) {
2950
+ controller.enqueue({
2951
+ type: "file",
2952
+ mediaType: (_k = img.mime_type) != null ? _k : "image/png",
2953
+ data: img.data,
2954
+ ...providerMetadata ? { providerMetadata } : {}
2955
+ });
2956
+ } else if ((img == null ? void 0 : img.uri) != null && img.uri.length > 0) {
2957
+ const uriProviderMetadata = {
2958
+ google: {
2959
+ ...interactionId != null ? { interactionId } : {},
2960
+ imageUri: img.uri
2961
+ }
2962
+ };
2963
+ controller.enqueue({
2964
+ type: "file",
2965
+ mediaType: (_l = img.mime_type) != null ? _l : "image/png",
2966
+ data: "",
2967
+ providerMetadata: uriProviderMetadata
2968
+ });
2969
+ }
2970
+ if (open.kind === "image") {
2971
+ open.data = void 0;
2972
+ open.uri = void 0;
2973
+ }
2974
+ break;
2975
+ }
2976
+ if (dtype === "video" && (open.kind === "pending_model_output" || open.kind === "text")) {
2977
+ const videoDelta = event.delta;
2978
+ const google = {};
2979
+ if (interactionId != null) google.interactionId = interactionId;
2980
+ const providerMetadata = Object.keys(google).length > 0 ? { google } : void 0;
2981
+ if ((videoDelta == null ? void 0 : videoDelta.data) != null && videoDelta.data.length > 0) {
2982
+ controller.enqueue({
2983
+ type: "file",
2984
+ mediaType: (_m = videoDelta.mime_type) != null ? _m : "video/mp4",
2985
+ data: videoDelta.data,
2986
+ ...providerMetadata ? { providerMetadata } : {}
2987
+ });
2988
+ } else if ((videoDelta == null ? void 0 : videoDelta.uri) != null && videoDelta.uri.length > 0) {
2989
+ const uriProviderMetadata = {
2990
+ google: {
2991
+ ...interactionId != null ? { interactionId } : {},
2992
+ videoUri: videoDelta.uri
2993
+ }
2994
+ };
2995
+ controller.enqueue({
2996
+ type: "file",
2997
+ mediaType: (_n = videoDelta.mime_type) != null ? _n : "video/mp4",
2998
+ data: "",
2999
+ providerMetadata: uriProviderMetadata
3000
+ });
3001
+ }
3002
+ break;
3003
+ }
3004
+ const delta = event.delta;
3005
+ if (open.kind === "text" && (delta == null ? void 0 : delta.type) === "text") {
3006
+ const text = (_o = delta.text) != null ? _o : "";
3007
+ if (text.length > 0) {
3008
+ controller.enqueue({
3009
+ type: "text-delta",
3010
+ id: open.id,
3011
+ delta: text
3012
+ });
3013
+ }
3014
+ } else if (open.kind === "text" && ((delta == null ? void 0 : delta.type) === "text_annotation" || (delta == null ? void 0 : delta.type) === "text_annotation_delta")) {
3015
+ const sources = annotationsToSources({
3016
+ annotations: delta.annotations,
3017
+ generateId: generateId2
3018
+ });
3019
+ for (const source of sources) {
3020
+ const key = sourceKey(source);
3021
+ if (emittedSourceKeys.has(key)) continue;
3022
+ emittedSourceKeys.add(key);
3023
+ open.emittedSourceKeys.add(key);
3024
+ controller.enqueue(source);
3025
+ }
3026
+ } else if (open.kind === "image" && (delta == null ? void 0 : delta.type) === "image") {
3027
+ if (delta.data != null) open.data = delta.data;
3028
+ if (delta.mime_type != null) open.mimeType = delta.mime_type;
3029
+ if (delta.uri != null) open.uri = delta.uri;
3030
+ } else if (open.kind === "reasoning") {
3031
+ if ((delta == null ? void 0 : delta.type) === "thought_summary") {
3032
+ const item = delta.content;
3033
+ if ((item == null ? void 0 : item.type) === "text" && typeof item.text === "string") {
3034
+ controller.enqueue({
3035
+ type: "reasoning-delta",
3036
+ id: open.id,
3037
+ delta: item.text
3038
+ });
3039
+ }
3040
+ } else if ((delta == null ? void 0 : delta.type) === "thought_signature") {
3041
+ const signature = delta.signature;
3042
+ if (signature != null) {
3043
+ open.signature = signature;
3044
+ }
3045
+ }
3046
+ } else if (open.kind === "function_call" && (delta == null ? void 0 : delta.type) === "arguments_delta") {
3047
+ const slice = typeof delta.arguments === "string" ? delta.arguments : "";
3048
+ if (slice.length > 0) {
3049
+ open.argumentsAccum += slice;
3050
+ controller.enqueue({
3051
+ type: "tool-input-delta",
3052
+ id: open.toolCallId,
3053
+ delta: slice
3054
+ });
3055
+ }
3056
+ if (delta.id != null) {
3057
+ open.toolCallId = delta.id;
3058
+ }
3059
+ if (delta.signature != null) {
3060
+ open.signature = delta.signature;
3061
+ }
3062
+ hasFunctionCall = true;
3063
+ } else if (open.kind === "builtin_tool_call" && (delta == null ? void 0 : delta.type) === open.blockType) {
3064
+ if (delta.id != null) open.toolCallId = delta.id;
3065
+ if (delta.arguments != null && typeof delta.arguments === "object") {
3066
+ open.arguments = delta.arguments;
3067
+ }
3068
+ if (delta.name != null && open.blockType === "mcp_server_tool_call") {
3069
+ open.toolName = delta.name;
3070
+ }
3071
+ } else if (open.kind === "builtin_tool_result" && (delta == null ? void 0 : delta.type) === open.blockType) {
3072
+ if (delta.call_id != null) open.callId = delta.call_id;
3073
+ if (delta.result !== void 0) open.result = delta.result;
3074
+ if (delta.is_error != null) open.isError = delta.is_error;
3075
+ if (delta.name != null && open.blockType === "mcp_server_tool_result") {
3076
+ open.toolName = delta.name;
3077
+ }
3078
+ }
3079
+ break;
3080
+ }
3081
+ case "step.stop": {
3082
+ const event = value;
3083
+ const open = openBlocks.get(event.index);
3084
+ if (open == null) break;
3085
+ if (open.kind === "text") {
3086
+ const textProviderMetadata = interactionId != null ? { google: { interactionId } } : void 0;
3087
+ controller.enqueue({
3088
+ type: "text-end",
3089
+ id: open.id,
3090
+ ...textProviderMetadata ? { providerMetadata: textProviderMetadata } : {}
3091
+ });
3092
+ } else if (open.kind === "reasoning") {
3093
+ const google = {};
3094
+ if (open.signature != null) google.signature = open.signature;
3095
+ if (interactionId != null) google.interactionId = interactionId;
3096
+ const providerMetadata = Object.keys(google).length > 0 ? { google } : void 0;
3097
+ controller.enqueue({
3098
+ type: "reasoning-end",
3099
+ id: open.id,
3100
+ ...providerMetadata ? { providerMetadata } : {}
3101
+ });
3102
+ } else if (open.kind === "image") {
3103
+ const google = {};
3104
+ if (interactionId != null) google.interactionId = interactionId;
3105
+ const providerMetadata = Object.keys(google).length > 0 ? { google } : void 0;
3106
+ if (open.data != null && open.data.length > 0) {
3107
+ controller.enqueue({
3108
+ type: "file",
3109
+ mediaType: (_p = open.mimeType) != null ? _p : "image/png",
3110
+ data: open.data,
3111
+ ...providerMetadata ? { providerMetadata } : {}
3112
+ });
3113
+ } else if (open.uri != null && open.uri.length > 0) {
3114
+ const uriProviderMetadata = {
3115
+ google: {
3116
+ ...interactionId != null ? { interactionId } : {},
3117
+ imageUri: open.uri
3118
+ }
3119
+ };
3120
+ controller.enqueue({
3121
+ type: "file",
3122
+ mediaType: (_q = open.mimeType) != null ? _q : "image/png",
3123
+ data: "",
3124
+ providerMetadata: uriProviderMetadata
3125
+ });
3126
+ }
3127
+ } else if (open.kind === "function_call") {
3128
+ const accumulated = open.argumentsAccum.length > 0 ? open.argumentsAccum : "{}";
3129
+ controller.enqueue({
3130
+ type: "tool-input-end",
3131
+ id: open.toolCallId
3132
+ });
3133
+ const google = {};
3134
+ if (open.signature != null) google.signature = open.signature;
3135
+ if (interactionId != null) google.interactionId = interactionId;
3136
+ const providerMetadata = Object.keys(google).length > 0 ? { google } : void 0;
3137
+ controller.enqueue({
3138
+ type: "tool-call",
3139
+ toolCallId: open.toolCallId,
3140
+ toolName: open.toolName,
3141
+ input: accumulated,
3142
+ ...providerMetadata ? { providerMetadata } : {}
3143
+ });
3144
+ } else if (open.kind === "builtin_tool_call" && !open.callEmitted) {
3145
+ controller.enqueue({
3146
+ type: "tool-call",
3147
+ toolCallId: open.toolCallId,
3148
+ toolName: open.toolName,
3149
+ input: JSON.stringify((_r = open.arguments) != null ? _r : {}),
3150
+ providerExecuted: true
3151
+ });
3152
+ open.callEmitted = true;
3153
+ } else if (open.kind === "builtin_tool_result" && !open.resultEmitted) {
3154
+ controller.enqueue({
3155
+ type: "tool-result",
3156
+ toolCallId: open.callId,
3157
+ toolName: open.toolName,
3158
+ result: (_s = open.result) != null ? _s : null
3159
+ });
3160
+ open.resultEmitted = true;
3161
+ const sources = builtinToolResultToSources({
3162
+ block: {
3163
+ type: open.blockType,
3164
+ call_id: open.callId,
3165
+ result: open.result
3166
+ },
3167
+ generateId: generateId2
3168
+ });
3169
+ for (const source of sources) {
3170
+ const key = sourceKey(source);
3171
+ if (emittedSourceKeys.has(key)) continue;
3172
+ emittedSourceKeys.add(key);
3173
+ controller.enqueue(source);
3174
+ }
3175
+ }
3176
+ openBlocks.delete(event.index);
3177
+ break;
3178
+ }
3179
+ case "interaction.status_update":
3180
+ case "interaction.in_progress":
3181
+ case "interaction.requires_action": {
3182
+ const event = value;
3183
+ if (event.status != null) {
3184
+ finishStatus = event.status;
3185
+ } else if (eventType === "interaction.requires_action") {
3186
+ finishStatus = "requires_action";
3187
+ } else {
3188
+ finishStatus = "in_progress";
3189
+ }
3190
+ break;
3191
+ }
3192
+ case "interaction.completed": {
3193
+ const event = value;
3194
+ const interaction = event.interaction;
3195
+ if ((interaction == null ? void 0 : interaction.id) != null && interaction.id.length > 0) {
3196
+ interactionId = interaction.id;
3197
+ }
3198
+ if ((interaction == null ? void 0 : interaction.status) != null) {
3199
+ finishStatus = interaction.status;
3200
+ }
3201
+ if ((interaction == null ? void 0 : interaction.usage) != null) {
3202
+ usage = interaction.usage;
3203
+ }
3204
+ if ((interaction == null ? void 0 : interaction.service_tier) != null) {
3205
+ serviceTier = interaction.service_tier;
3206
+ }
3207
+ break;
3208
+ }
3209
+ case "error": {
3210
+ const event = value;
3211
+ finishStatus = "failed";
3212
+ const errorPayload = (_t = event.error) != null ? _t : {
3213
+ message: "Unknown interaction error"
3214
+ };
3215
+ controller.enqueue({ type: "error", error: errorPayload });
3216
+ break;
3217
+ }
3218
+ default:
3219
+ break;
3220
+ }
3221
+ },
3222
+ flush(controller) {
3223
+ const finishReason = {
3224
+ unified: mapGoogleInteractionsFinishReason({
3225
+ status: finishStatus,
3226
+ hasFunctionCall
3227
+ }),
3228
+ raw: finishStatus
3229
+ };
3230
+ const outputTokensByModality = getGoogleInteractionsOutputTokensByModality(usage);
3231
+ const providerMetadata = {
3232
+ google: {
3233
+ ...interactionId != null ? { interactionId } : {},
3234
+ ...serviceTier != null ? { serviceTier } : {},
3235
+ ...outputTokensByModality != null ? { outputTokensByModality } : {}
3236
+ }
3237
+ };
3238
+ controller.enqueue({
3239
+ type: "finish",
3240
+ finishReason,
3241
+ usage: convertGoogleInteractionsUsage(usage),
3242
+ providerMetadata
3243
+ });
3244
+ }
3245
+ });
3246
+ }
3247
+
3248
+ // src/interactions/convert-to-google-interactions-input.ts
3249
+ import { convertToBase64 as convertToBase642, secureJsonParse as secureJsonParse2 } from "@ai-sdk/provider-utils";
3250
+ function getTopLevelMediaType(mediaType) {
3251
+ const slashIndex = mediaType.indexOf("/");
3252
+ return slashIndex === -1 ? mediaType : mediaType.substring(0, slashIndex);
3253
+ }
3254
+ function isFullMediaType(mediaType) {
3255
+ const slashIndex = mediaType.indexOf("/");
3256
+ if (slashIndex === -1) {
3257
+ return false;
3258
+ }
3259
+ const subtype = mediaType.substring(slashIndex + 1);
3260
+ return subtype.length > 0 && subtype !== "*";
3261
+ }
3262
+ function convertToGoogleInteractionsInput({
3263
+ prompt,
3264
+ previousInteractionId,
3265
+ store,
3266
+ mediaResolution
3267
+ }) {
3268
+ var _a, _b, _c, _d, _e, _f, _g;
3269
+ const warnings = [];
3270
+ const incoherentCombo = previousInteractionId != null && store === false;
3271
+ const shouldCompact = previousInteractionId != null && store !== false;
3272
+ if (incoherentCombo) {
3273
+ warnings.push({
3274
+ type: "other",
3275
+ message: "google.interactions: providerOptions.google.previousInteractionId was set together with store: false. These are incoherent (the prior interaction cannot be referenced when nothing was stored on the server); the full history will be sent and previous_interaction_id will still be emitted."
3276
+ });
3277
+ }
3278
+ const compactedPrompt = shouldCompact ? compactPromptForPreviousInteraction({
3279
+ prompt,
3280
+ previousInteractionId
3281
+ }) : prompt;
3282
+ const systemTexts = [];
3283
+ const steps = [];
3284
+ for (const message of compactedPrompt) {
3285
+ switch (message.role) {
3286
+ case "system": {
3287
+ systemTexts.push(message.content);
3288
+ break;
3289
+ }
3290
+ case "user": {
3291
+ const content = [];
3292
+ for (const part of message.content) {
3293
+ if (part.type === "text") {
3294
+ content.push({ type: "text", text: part.text });
3295
+ } else if (part.type === "file") {
3296
+ const fileBlock = convertFilePartToContent({
3297
+ part,
3298
+ warnings,
3299
+ mediaResolution
3300
+ });
3301
+ if (fileBlock != null) {
3302
+ content.push(fileBlock);
3303
+ }
3304
+ }
3305
+ }
3306
+ const merged = mergeAdjacentTextContent(content);
3307
+ if (merged.length > 0) {
3308
+ steps.push({ type: "user_input", content: merged });
3309
+ }
3310
+ break;
3311
+ }
3312
+ case "assistant": {
3313
+ let pendingModelOutput = [];
3314
+ const flushModelOutput = () => {
3315
+ if (pendingModelOutput.length > 0) {
3316
+ steps.push({ type: "model_output", content: pendingModelOutput });
3317
+ pendingModelOutput = [];
3318
+ }
3319
+ };
3320
+ for (const part of message.content) {
3321
+ if (part.type === "text") {
3322
+ pendingModelOutput.push({ type: "text", text: part.text });
3323
+ } else if (part.type === "reasoning") {
3324
+ flushModelOutput();
3325
+ const signature = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.signature;
3326
+ steps.push({
3327
+ type: "thought",
3328
+ ...signature != null ? { signature } : {},
3329
+ summary: part.text.length > 0 ? [{ type: "text", text: part.text }] : void 0
3330
+ });
3331
+ } else if (part.type === "file") {
3332
+ const fileBlock = convertFilePartToContent({
3333
+ part,
3334
+ warnings,
3335
+ mediaResolution
3336
+ });
3337
+ if (fileBlock != null) {
3338
+ pendingModelOutput.push(fileBlock);
3339
+ }
3340
+ } else if (part.type === "tool-call") {
3341
+ flushModelOutput();
3342
+ const signature = (_d = (_c = part.providerOptions) == null ? void 0 : _c.google) == null ? void 0 : _d.signature;
3343
+ const args = typeof part.input === "string" ? safeParseToolArgs(part.input) : (_e = part.input) != null ? _e : {};
3344
+ steps.push({
3345
+ type: "function_call",
3346
+ id: part.toolCallId,
3347
+ name: part.toolName,
3348
+ arguments: args,
3349
+ ...signature != null ? { signature } : {}
3350
+ });
3351
+ } else {
3352
+ warnings.push({
3353
+ type: "other",
3354
+ message: `google.interactions: unsupported assistant content part type "${part.type}"; part dropped.`
3355
+ });
3356
+ }
3357
+ }
3358
+ flushModelOutput();
3359
+ break;
3360
+ }
3361
+ case "tool": {
3362
+ const content = [];
3363
+ for (const part of message.content) {
3364
+ if (part.type !== "tool-result") {
3365
+ warnings.push({
3366
+ type: "other",
3367
+ message: `google.interactions: unsupported tool message part type "${part.type}"; part dropped.`
3368
+ });
3369
+ continue;
3370
+ }
3371
+ const block = convertToolResultPart({
3372
+ toolCallId: part.toolCallId,
3373
+ toolName: part.toolName,
3374
+ output: part.output,
3375
+ signature: (_g = (_f = part.providerOptions) == null ? void 0 : _f.google) == null ? void 0 : _g.signature,
3376
+ warnings
3377
+ });
3378
+ content.push(block);
3379
+ }
3380
+ if (content.length > 0) {
3381
+ steps.push({ type: "user_input", content });
3382
+ }
3383
+ break;
3384
+ }
3385
+ }
3386
+ }
3387
+ const systemInstruction = systemTexts.length > 0 ? systemTexts.join("\n\n") : void 0;
3388
+ return { input: steps, systemInstruction, warnings };
3389
+ }
3390
+ function convertFilePartToContent({
3391
+ part,
3392
+ warnings,
3393
+ mediaResolution
3394
+ }) {
3395
+ const topLevel = getTopLevelMediaType(part.mediaType);
3396
+ let kind;
3397
+ switch (topLevel) {
3398
+ case "image":
3399
+ kind = "image";
3400
+ break;
3401
+ case "audio":
3402
+ kind = "audio";
3403
+ break;
3404
+ case "video":
3405
+ kind = "video";
3406
+ break;
3407
+ case "application":
3408
+ kind = "document";
3409
+ break;
3410
+ default:
3411
+ kind = void 0;
3412
+ }
3413
+ if (kind == null) {
3414
+ warnings.push({
3415
+ type: "other",
3416
+ message: `google.interactions: unsupported file media type "${part.mediaType}"; part dropped.`
3417
+ });
3418
+ return void 0;
3419
+ }
3420
+ const resolutionField = mediaResolution != null && (kind === "image" || kind === "video") ? { resolution: mediaResolution } : {};
3421
+ if (part.data instanceof URL) {
3422
+ return {
3423
+ type: kind,
3424
+ uri: part.data.toString(),
3425
+ ...isFullMediaType(part.mediaType) ? { mime_type: part.mediaType } : {},
3426
+ ...resolutionField
3427
+ };
3428
+ }
3429
+ if (!isFullMediaType(part.mediaType)) {
3430
+ warnings.push({
3431
+ type: "other",
3432
+ message: `google.interactions: inline file data requires a full IANA media type (e.g. "image/png"), got "${part.mediaType}"; part dropped.`
3433
+ });
3434
+ return void 0;
3435
+ }
3436
+ return {
3437
+ type: kind,
3438
+ data: convertToBase642(part.data),
3439
+ mime_type: part.mediaType,
3440
+ ...resolutionField
3441
+ };
3442
+ }
3443
+ function compactPromptForPreviousInteraction({
3444
+ prompt,
3445
+ previousInteractionId
3446
+ }) {
3447
+ const out = [];
3448
+ const droppedToolCallIds = /* @__PURE__ */ new Set();
3449
+ for (const message of prompt) {
3450
+ if (message.role === "assistant") {
3451
+ const matchesLinkedInteraction = message.content.some((part) => {
3452
+ var _a, _b;
3453
+ const partInteractionId = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.interactionId;
3454
+ return partInteractionId === previousInteractionId;
3455
+ });
3456
+ if (matchesLinkedInteraction) {
3457
+ for (const part of message.content) {
3458
+ if (part.type === "tool-call") {
3459
+ droppedToolCallIds.add(part.toolCallId);
3460
+ }
3461
+ }
3462
+ continue;
3463
+ }
3464
+ out.push(message);
3465
+ continue;
3466
+ }
3467
+ if (message.role === "tool") {
3468
+ const remaining = message.content.filter((part) => {
3469
+ if (part.type !== "tool-result") {
3470
+ return true;
3471
+ }
3472
+ return !droppedToolCallIds.has(part.toolCallId);
3473
+ });
3474
+ if (remaining.length === 0) {
3475
+ continue;
3476
+ }
3477
+ out.push({
3478
+ ...message,
3479
+ content: remaining
3480
+ });
3481
+ continue;
3482
+ }
3483
+ out.push(message);
3484
+ }
3485
+ return out;
3486
+ }
3487
+ function safeParseToolArgs(input) {
3488
+ try {
3489
+ const parsed = secureJsonParse2(input);
3490
+ if (parsed != null && typeof parsed === "object" && !Array.isArray(parsed)) {
3491
+ return parsed;
3492
+ }
3493
+ return { value: parsed };
3494
+ } catch (e) {
3495
+ return { value: input };
3496
+ }
3497
+ }
3498
+ function convertToolResultPart({
3499
+ toolCallId,
3500
+ toolName,
3501
+ output,
3502
+ signature,
3503
+ warnings
3504
+ }) {
3505
+ var _a;
3506
+ const base = {
3507
+ type: "function_result",
3508
+ call_id: toolCallId,
3509
+ name: toolName,
3510
+ ...signature != null ? { signature } : {}
3511
+ };
3512
+ switch (output.type) {
3513
+ case "text":
3514
+ return { ...base, result: output.value };
3515
+ case "json":
3516
+ return { ...base, result: JSON.stringify(output.value) };
3517
+ case "error-text":
3518
+ return { ...base, is_error: true, result: output.value };
3519
+ case "error-json":
3520
+ return { ...base, is_error: true, result: JSON.stringify(output.value) };
3521
+ case "execution-denied":
3522
+ return {
3523
+ ...base,
3524
+ is_error: true,
3525
+ result: (_a = output.reason) != null ? _a : "Tool execution denied by user."
3526
+ };
3527
+ case "content": {
3528
+ const blocks = [];
3529
+ for (const item of output.value) {
3530
+ if (item.type === "text") {
3531
+ blocks.push({ type: "text", text: item.text });
3532
+ } else if (item.type === "image-data") {
3533
+ const imageBlock = filePartToImageBlock({
3534
+ part: {
3535
+ type: "file",
3536
+ mediaType: item.mediaType,
3537
+ data: item.data
3538
+ },
3539
+ warnings
3540
+ });
3541
+ if (imageBlock != null) {
3542
+ blocks.push(imageBlock);
3543
+ }
3544
+ } else if (item.type === "image-url") {
3545
+ const imageBlock = filePartToImageBlock({
3546
+ part: {
3547
+ type: "file",
3548
+ mediaType: "image/*",
3549
+ data: new URL(item.url)
3550
+ },
3551
+ warnings
3552
+ });
3553
+ if (imageBlock != null) {
3554
+ blocks.push(imageBlock);
3555
+ }
3556
+ } else if (item.type === "file-data" || item.type === "file-url") {
3557
+ const mediaType = item.type === "file-data" ? item.mediaType : "application/*";
3558
+ const topLevel = getTopLevelMediaType(mediaType);
3559
+ if (topLevel !== "image") {
3560
+ warnings.push({
3561
+ type: "other",
3562
+ message: `google.interactions: tool-result file with mediaType "${mediaType}" is not supported (Interactions \`function_result.result\` accepts only text and image content); part dropped.`
3563
+ });
3564
+ continue;
3565
+ }
3566
+ const imageBlock = filePartToImageBlock({
3567
+ part: item.type === "file-data" ? {
3568
+ type: "file",
3569
+ mediaType: item.mediaType,
3570
+ data: item.data
3571
+ } : {
3572
+ type: "file",
3573
+ mediaType,
3574
+ data: new URL(item.url)
3575
+ },
3576
+ warnings
3577
+ });
3578
+ if (imageBlock != null) {
3579
+ blocks.push(imageBlock);
3580
+ }
3581
+ } else {
3582
+ warnings.push({
3583
+ type: "other",
3584
+ message: `google.interactions: tool-result content part type "${item.type}" is not supported; part dropped.`
3585
+ });
3586
+ }
3587
+ }
3588
+ return { ...base, result: blocks };
3589
+ }
3590
+ }
3591
+ }
3592
+ function filePartToImageBlock({
3593
+ part,
3594
+ warnings
3595
+ }) {
3596
+ if (part.data instanceof URL) {
3597
+ return {
3598
+ type: "image",
3599
+ uri: part.data.toString(),
3600
+ ...isFullMediaType(part.mediaType) ? { mime_type: part.mediaType } : {}
3601
+ };
3602
+ }
3603
+ if (!isFullMediaType(part.mediaType)) {
3604
+ warnings.push({
3605
+ type: "other",
3606
+ message: `google.interactions: tool-result image part requires a full IANA media type (e.g. "image/png"), got "${part.mediaType}"; part dropped.`
3607
+ });
3608
+ return void 0;
3609
+ }
3610
+ return {
3611
+ type: "image",
3612
+ data: convertToBase642(part.data),
3613
+ mime_type: part.mediaType
3614
+ };
3615
+ }
3616
+ function mergeAdjacentTextContent(content) {
3617
+ if (content.length < 2) {
3618
+ return content;
3619
+ }
3620
+ const result = [];
3621
+ for (const block of content) {
3622
+ const last = result[result.length - 1];
3623
+ if (block.type === "text" && last != null && last.type === "text" && last.annotations == null && block.annotations == null) {
3624
+ const merged = {
3625
+ type: "text",
3626
+ text: `${last.text}
3627
+
3628
+ ${block.text}`
3629
+ };
3630
+ result[result.length - 1] = merged;
3631
+ continue;
3632
+ }
3633
+ result.push(block);
3634
+ }
3635
+ return result;
3636
+ }
3637
+
3638
+ // src/interactions/google-interactions-api.ts
3639
+ import {
3640
+ lazySchema as lazySchema9,
3641
+ zodSchema as zodSchema9
3642
+ } from "@ai-sdk/provider-utils";
3643
+ import { z as z11 } from "zod/v4";
3644
+ var tokenByModalitySchema = () => z11.object({
3645
+ modality: z11.string().nullish(),
3646
+ tokens: z11.number().nullish()
3647
+ }).loose();
3648
+ var usageSchema2 = () => z11.object({
3649
+ total_input_tokens: z11.number().nullish(),
3650
+ total_output_tokens: z11.number().nullish(),
3651
+ total_thought_tokens: z11.number().nullish(),
3652
+ total_cached_tokens: z11.number().nullish(),
3653
+ total_tool_use_tokens: z11.number().nullish(),
3654
+ total_tokens: z11.number().nullish(),
3655
+ input_tokens_by_modality: z11.array(tokenByModalitySchema()).nullish(),
3656
+ output_tokens_by_modality: z11.array(tokenByModalitySchema()).nullish(),
3657
+ cached_tokens_by_modality: z11.array(tokenByModalitySchema()).nullish(),
3658
+ tool_use_tokens_by_modality: z11.array(tokenByModalitySchema()).nullish(),
3659
+ grounding_tool_count: z11.array(
3660
+ z11.object({
3661
+ type: z11.string().nullish(),
3662
+ count: z11.number().nullish()
3663
+ }).loose()
3664
+ ).nullish()
3665
+ }).loose();
3666
+ var interactionStatusSchema = () => z11.enum([
3667
+ "in_progress",
3668
+ "requires_action",
3669
+ "completed",
3670
+ "failed",
3671
+ "cancelled",
3672
+ "incomplete"
3673
+ ]);
3674
+ var annotationSchema = () => {
3675
+ const urlCitation = z11.object({
3676
+ type: z11.literal("url_citation"),
3677
+ url: z11.string().nullish(),
3678
+ title: z11.string().nullish(),
3679
+ start_index: z11.number().nullish(),
3680
+ end_index: z11.number().nullish()
3681
+ }).loose();
3682
+ const fileCitation = z11.object({
3683
+ type: z11.literal("file_citation"),
3684
+ file_name: z11.string().nullish(),
3685
+ document_uri: z11.string().nullish(),
3686
+ url: z11.string().nullish(),
3687
+ page_number: z11.number().nullish(),
3688
+ media_id: z11.string().nullish(),
3689
+ start_index: z11.number().nullish(),
3690
+ end_index: z11.number().nullish(),
3691
+ custom_metadata: z11.record(z11.string(), z11.unknown()).nullish()
3692
+ }).loose();
3693
+ const placeCitation = z11.object({
3694
+ type: z11.literal("place_citation"),
3695
+ name: z11.string().nullish(),
3696
+ url: z11.string().nullish(),
3697
+ place_id: z11.string().nullish(),
3698
+ start_index: z11.number().nullish(),
3699
+ end_index: z11.number().nullish()
3700
+ }).loose();
3701
+ return z11.union([
3702
+ urlCitation,
3703
+ fileCitation,
3704
+ placeCitation,
3705
+ z11.object({ type: z11.string() }).loose()
3706
+ ]);
3707
+ };
3708
+ var thoughtSummaryItemSchema = () => z11.object({
3709
+ type: z11.string(),
3710
+ text: z11.string().nullish(),
3711
+ data: z11.string().nullish(),
3712
+ mime_type: z11.string().nullish()
3713
+ }).loose();
3714
+ var contentBlockSchema = () => {
3715
+ const textContent = z11.object({
3716
+ type: z11.literal("text"),
3717
+ text: z11.string(),
3718
+ annotations: z11.array(annotationSchema()).nullish()
3719
+ }).loose();
3720
+ const imageContent = z11.object({
3721
+ type: z11.literal("image"),
3722
+ data: z11.string().nullish(),
3723
+ mime_type: z11.string().nullish(),
3724
+ resolution: z11.enum(["low", "medium", "high", "ultra_high"]).nullish(),
3725
+ uri: z11.string().nullish()
3726
+ }).loose();
3727
+ const videoContent = z11.object({
3728
+ type: z11.literal("video"),
3729
+ data: z11.string().nullish(),
3730
+ mime_type: z11.string().nullish(),
3731
+ uri: z11.string().nullish()
3732
+ }).loose();
3733
+ return z11.union([
3734
+ textContent,
3735
+ imageContent,
3736
+ videoContent,
3737
+ z11.object({ type: z11.string() }).loose()
3738
+ ]);
3739
+ };
3740
+ var BUILTIN_TOOL_CALL_STEP_TYPES = [
3741
+ "google_search_call",
3742
+ "code_execution_call",
3743
+ "url_context_call",
3744
+ "file_search_call",
3745
+ "google_maps_call",
3746
+ "mcp_server_tool_call"
3747
+ ];
3748
+ var BUILTIN_TOOL_RESULT_STEP_TYPES = [
3749
+ "google_search_result",
3750
+ "code_execution_result",
3751
+ "url_context_result",
3752
+ "file_search_result",
3753
+ "google_maps_result",
3754
+ "mcp_server_tool_result"
3755
+ ];
3756
+ var stepSchema = () => {
3757
+ const userInputStep = z11.object({
3758
+ type: z11.literal("user_input"),
3759
+ content: z11.array(contentBlockSchema()).nullish()
3760
+ }).loose();
3761
+ const modelOutputStep = z11.object({
3762
+ type: z11.literal("model_output"),
3763
+ content: z11.array(contentBlockSchema()).nullish()
3764
+ }).loose();
3765
+ const functionCallStep = z11.object({
3766
+ type: z11.literal("function_call"),
3767
+ id: z11.string(),
3768
+ name: z11.string(),
3769
+ arguments: z11.record(z11.string(), z11.unknown()).nullish(),
3770
+ signature: z11.string().nullish()
3771
+ }).loose();
3772
+ const thoughtStep = z11.object({
3773
+ type: z11.literal("thought"),
3774
+ signature: z11.string().nullish(),
3775
+ summary: z11.array(thoughtSummaryItemSchema()).nullish()
3776
+ }).loose();
3777
+ const builtinToolCallStep = z11.object({
3778
+ type: z11.enum(BUILTIN_TOOL_CALL_STEP_TYPES),
3779
+ id: z11.string(),
3780
+ arguments: z11.record(z11.string(), z11.unknown()).nullish(),
3781
+ name: z11.string().nullish(),
3782
+ server_name: z11.string().nullish(),
3783
+ search_type: z11.string().nullish(),
3784
+ signature: z11.string().nullish()
3785
+ }).loose();
3786
+ const builtinToolResultStep = z11.object({
3787
+ type: z11.enum(BUILTIN_TOOL_RESULT_STEP_TYPES),
3788
+ call_id: z11.string(),
3789
+ result: z11.unknown().nullish(),
3790
+ is_error: z11.boolean().nullish(),
3791
+ name: z11.string().nullish(),
3792
+ server_name: z11.string().nullish(),
3793
+ signature: z11.string().nullish()
3794
+ }).loose();
3795
+ return z11.union([
3796
+ userInputStep,
3797
+ modelOutputStep,
3798
+ functionCallStep,
3799
+ thoughtStep,
3800
+ builtinToolCallStep,
3801
+ builtinToolResultStep,
3802
+ z11.object({ type: z11.string() }).loose()
3803
+ ]);
3804
+ };
3805
+ var googleInteractionsResponseSchema = lazySchema9(
3806
+ () => zodSchema9(
3807
+ z11.object({
3808
+ /*
3809
+ * `id` is omitted from the response body when `store: false` (fully
3810
+ * stateless mode) — there is no server-side interaction record for the
3811
+ * client to reference. `nullish` lets the schema accept that shape.
3812
+ */
3813
+ id: z11.string().nullish(),
3814
+ created: z11.string().nullish(),
3815
+ updated: z11.string().nullish(),
3816
+ status: interactionStatusSchema(),
3817
+ model: z11.string().nullish(),
3818
+ agent: z11.string().nullish(),
3819
+ steps: z11.array(stepSchema()).nullish(),
3820
+ usage: usageSchema2().nullish(),
3821
+ service_tier: z11.string().nullish(),
3822
+ previous_interaction_id: z11.string().nullish(),
3823
+ response_modalities: z11.array(z11.string()).nullish()
3824
+ }).loose()
3825
+ )
3826
+ );
3827
+ var googleInteractionsEventSchema = lazySchema9(
3828
+ () => zodSchema9(
3829
+ (() => {
3830
+ const status = interactionStatusSchema();
3831
+ const annotation = annotationSchema();
3832
+ const thoughtSummaryItem = thoughtSummaryItemSchema();
3833
+ const interactionCreatedEvent = z11.object({
3834
+ event_type: z11.literal("interaction.created"),
3835
+ event_id: z11.string().nullish(),
3836
+ interaction: z11.object({
3837
+ /*
3838
+ * `id` is omitted when `store: false` (fully stateless mode);
3839
+ * see the matching note on `googleInteractionsResponseSchema.id`.
3840
+ */
3841
+ id: z11.string().nullish(),
3842
+ created: z11.string().nullish(),
3843
+ model: z11.string().nullish(),
3844
+ agent: z11.string().nullish(),
3845
+ status: status.nullish()
3846
+ }).loose()
3847
+ }).loose();
3848
+ const stepStartEvent = z11.object({
3849
+ event_type: z11.literal("step.start"),
3850
+ event_id: z11.string().nullish(),
3851
+ index: z11.number(),
3852
+ step: stepSchema()
3853
+ }).loose();
3854
+ const stepDeltaText = z11.object({
3855
+ type: z11.literal("text"),
3856
+ text: z11.string()
3857
+ }).loose();
3858
+ const stepDeltaThoughtSummary = z11.object({
3859
+ type: z11.literal("thought_summary"),
3860
+ content: thoughtSummaryItem.nullish()
3861
+ }).loose();
3862
+ const stepDeltaThoughtSignature = z11.object({
3863
+ type: z11.literal("thought_signature"),
3864
+ signature: z11.string().nullish()
3865
+ }).loose();
3866
+ const stepDeltaArgumentsDelta = z11.object({
3867
+ type: z11.literal("arguments_delta"),
3868
+ arguments: z11.string().nullish(),
3869
+ id: z11.string().nullish(),
3870
+ signature: z11.string().nullish()
3871
+ }).loose();
3872
+ const stepDeltaTextAnnotation = z11.object({
3873
+ type: z11.enum(["text_annotation_delta", "text_annotation"]),
3874
+ annotations: z11.array(annotation).nullish()
3875
+ }).loose();
3876
+ const stepDeltaImage = z11.object({
3877
+ type: z11.literal("image"),
3878
+ data: z11.string().nullish(),
3879
+ mime_type: z11.string().nullish(),
3880
+ resolution: z11.enum(["low", "medium", "high", "ultra_high"]).nullish(),
3881
+ uri: z11.string().nullish()
3882
+ }).loose();
3883
+ const stepDeltaVideo = z11.object({
3884
+ type: z11.literal("video"),
3885
+ data: z11.string().nullish(),
3886
+ mime_type: z11.string().nullish(),
3887
+ uri: z11.string().nullish()
3888
+ }).loose();
3889
+ const stepDeltaBuiltinToolCall = z11.object({
3890
+ type: z11.enum(BUILTIN_TOOL_CALL_STEP_TYPES),
3891
+ id: z11.string().nullish(),
3892
+ arguments: z11.record(z11.string(), z11.unknown()).nullish(),
3893
+ name: z11.string().nullish(),
3894
+ server_name: z11.string().nullish(),
3895
+ search_type: z11.string().nullish(),
3896
+ signature: z11.string().nullish()
3897
+ }).loose();
3898
+ const stepDeltaBuiltinToolResult = z11.object({
3899
+ type: z11.enum(BUILTIN_TOOL_RESULT_STEP_TYPES),
3900
+ call_id: z11.string().nullish(),
3901
+ result: z11.unknown().nullish(),
3902
+ is_error: z11.boolean().nullish(),
3903
+ name: z11.string().nullish(),
3904
+ server_name: z11.string().nullish(),
3905
+ signature: z11.string().nullish()
3906
+ }).loose();
3907
+ const stepDeltaUnknown = z11.object({ type: z11.string() }).loose();
3908
+ const stepDeltaUnion = z11.union([
3909
+ stepDeltaText,
3910
+ stepDeltaImage,
3911
+ stepDeltaVideo,
3912
+ stepDeltaThoughtSummary,
3913
+ stepDeltaThoughtSignature,
3914
+ stepDeltaArgumentsDelta,
3915
+ stepDeltaTextAnnotation,
3916
+ stepDeltaBuiltinToolCall,
3917
+ stepDeltaBuiltinToolResult,
3918
+ stepDeltaUnknown
3919
+ ]);
3920
+ const stepDeltaEvent = z11.object({
3921
+ event_type: z11.literal("step.delta"),
3922
+ event_id: z11.string().nullish(),
3923
+ index: z11.number(),
3924
+ delta: stepDeltaUnion
3925
+ }).loose();
3926
+ const stepStopEvent = z11.object({
3927
+ event_type: z11.literal("step.stop"),
3928
+ event_id: z11.string().nullish(),
3929
+ index: z11.number()
3930
+ }).loose();
3931
+ const interactionStatusUpdateEvent = z11.object({
3932
+ event_type: z11.literal("interaction.status_update"),
3933
+ event_id: z11.string().nullish(),
3934
+ interaction_id: z11.string().nullish(),
3935
+ status: status.nullish()
3936
+ }).loose();
3937
+ const interactionInProgressEvent = z11.object({
3938
+ event_type: z11.literal("interaction.in_progress"),
3939
+ event_id: z11.string().nullish(),
3940
+ interaction_id: z11.string().nullish(),
3941
+ status: status.nullish()
3942
+ }).loose();
3943
+ const interactionRequiresActionEvent = z11.object({
3944
+ event_type: z11.literal("interaction.requires_action"),
3945
+ event_id: z11.string().nullish(),
3946
+ interaction_id: z11.string().nullish(),
3947
+ status: status.nullish()
3948
+ }).loose();
3949
+ const interactionCompletedEvent = z11.object({
3950
+ event_type: z11.literal("interaction.completed"),
3951
+ event_id: z11.string().nullish(),
3952
+ interaction: z11.object({
3953
+ id: z11.string().nullish(),
3954
+ status: status.nullish(),
3955
+ usage: usageSchema2().nullish(),
3956
+ service_tier: z11.string().nullish()
3957
+ }).loose()
3958
+ }).loose();
3959
+ const errorEvent = z11.object({
3960
+ event_type: z11.literal("error"),
3961
+ event_id: z11.string().nullish(),
3962
+ error: z11.object({
3963
+ code: z11.string().nullish(),
3964
+ message: z11.string().nullish()
3965
+ }).loose().nullish()
3966
+ }).loose();
3967
+ const unknownEvent = z11.object({ event_type: z11.string() }).loose();
3968
+ return z11.union([
3969
+ interactionCreatedEvent,
3970
+ stepStartEvent,
3971
+ stepDeltaEvent,
3972
+ stepStopEvent,
3973
+ interactionStatusUpdateEvent,
3974
+ interactionInProgressEvent,
3975
+ interactionRequiresActionEvent,
3976
+ interactionCompletedEvent,
3977
+ errorEvent,
3978
+ unknownEvent
3979
+ ]);
3980
+ })()
3981
+ )
3982
+ );
3983
+
3984
+ // src/interactions/google-interactions-language-model-options.ts
3985
+ import {
3986
+ lazySchema as lazySchema10,
3987
+ zodSchema as zodSchema10
3988
+ } from "@ai-sdk/provider-utils";
3989
+ import { z as z12 } from "zod/v4";
3990
+ var googleInteractionsLanguageModelOptions = lazySchema10(
3991
+ () => zodSchema10(
3992
+ z12.object({
3993
+ previousInteractionId: z12.string().nullish(),
3994
+ store: z12.boolean().nullish(),
3995
+ agent: z12.string().nullish(),
3996
+ agentConfig: z12.union([
3997
+ z12.object({
3998
+ type: z12.literal("dynamic")
3999
+ }).loose(),
4000
+ z12.object({
4001
+ type: z12.literal("deep-research"),
4002
+ thinkingSummaries: z12.enum(["auto", "none"]).nullish(),
4003
+ visualization: z12.enum(["off", "auto"]).nullish(),
4004
+ collaborativePlanning: z12.boolean().nullish()
4005
+ })
4006
+ ]).nullish(),
4007
+ thinkingLevel: z12.enum(["minimal", "low", "medium", "high"]).nullish(),
4008
+ thinkingSummaries: z12.enum(["auto", "none"]).nullish(),
4009
+ /**
4010
+ * Output-format entries that map directly to the API's `response_format`
4011
+ * array. Use this to request image, audio, or non-JSON text outputs
4012
+ * with full control over `mime_type`, `aspect_ratio`, and `image_size`.
4013
+ *
4014
+ * Entries are sent in order. The AI SDK call-level `responseFormat: {
4015
+ * type: 'json', schema }` still drives JSON-mode and adds a matching
4016
+ * text entry automatically; entries listed here are appended.
4017
+ */
4018
+ responseFormat: z12.array(
4019
+ z12.union([
4020
+ z12.object({
4021
+ type: z12.literal("text"),
4022
+ mimeType: z12.string().nullish(),
4023
+ schema: z12.unknown().nullish()
4024
+ }).loose(),
4025
+ z12.object({
4026
+ type: z12.literal("image"),
4027
+ mimeType: z12.string().nullish(),
4028
+ aspectRatio: z12.enum([
4029
+ "1:1",
4030
+ "2:3",
4031
+ "3:2",
4032
+ "3:4",
4033
+ "4:3",
4034
+ "4:5",
4035
+ "5:4",
4036
+ "9:16",
4037
+ "16:9",
4038
+ "21:9",
4039
+ "1:8",
4040
+ "8:1",
4041
+ "1:4",
4042
+ "4:1"
4043
+ ]).nullish(),
4044
+ imageSize: z12.enum(["1K", "2K", "4K", "512"]).nullish()
4045
+ }).loose(),
4046
+ z12.object({
4047
+ type: z12.literal("audio"),
4048
+ mimeType: z12.string().nullish()
4049
+ }).loose()
4050
+ ])
4051
+ ).nullish(),
4052
+ /**
4053
+ * @deprecated Use `responseFormat` with a `{ type: 'image', ... }`
4054
+ * entry instead. Retained for backwards compatibility; the SDK
4055
+ * translates it into a matching `response_format` image entry and
4056
+ * emits a warning when set.
4057
+ */
4058
+ imageConfig: z12.object({
4059
+ aspectRatio: z12.enum([
4060
+ "1:1",
4061
+ "2:3",
4062
+ "3:2",
4063
+ "3:4",
4064
+ "4:3",
4065
+ "4:5",
4066
+ "5:4",
4067
+ "9:16",
4068
+ "16:9",
4069
+ "21:9",
4070
+ "1:8",
4071
+ "8:1",
4072
+ "1:4",
4073
+ "4:1"
4074
+ ]).nullish(),
4075
+ imageSize: z12.enum(["1K", "2K", "4K", "512"]).nullish()
4076
+ }).nullish(),
4077
+ mediaResolution: z12.enum(["low", "medium", "high", "ultra_high"]).nullish(),
4078
+ responseModalities: z12.array(z12.enum(["text", "image", "audio", "video", "document"])).nullish(),
4079
+ serviceTier: z12.enum(["flex", "standard", "priority"]).nullish(),
4080
+ /**
4081
+ * Alternative to AI SDK `system` message. If both are set, the AI SDK
4082
+ * `system` message wins and a warning is emitted.
4083
+ */
4084
+ systemInstruction: z12.string().nullish(),
4085
+ /**
4086
+ * Per-block signature for round-tripping `thought.signature` and
4087
+ * `function_call.signature` blocks. Set by the SDK on output reasoning /
4088
+ * tool-call parts; passed back unchanged on input parts so the API
4089
+ * accepts the prior turn.
4090
+ */
4091
+ signature: z12.string().nullish(),
4092
+ /**
4093
+ * Set by the SDK on output assistant messages. The converter uses it to
4094
+ * decide which messages to drop when compacting under
4095
+ * `previousInteractionId`.
4096
+ */
4097
+ interactionId: z12.string().nullish(),
4098
+ /**
4099
+ * Maximum time, in milliseconds, to poll a background interaction (agent
4100
+ * call) before giving up. Defaults to 30 minutes. Long-running agents
4101
+ * such as deep research can take tens of minutes — increase if needed.
4102
+ */
4103
+ pollingTimeoutMs: z12.number().int().positive().nullish(),
4104
+ /**
4105
+ * Run the interaction in the background. Required for agents whose
4106
+ * server-side workflow cannot complete within a single request/response.
4107
+ * When `true`, the POST returns with a non-terminal status and the SDK
4108
+ * polls `GET /interactions/{id}` until the work completes. Some agents
4109
+ * reject `true`; see the agent's documentation for which mode it
4110
+ * requires.
4111
+ */
4112
+ background: z12.boolean().nullish(),
4113
+ /**
4114
+ * Environment configuration for the agent sandbox. Only applies to agent
4115
+ * calls (`google.interactions({ agent })`); ignored on model-id calls.
4116
+ *
4117
+ * - `"remote"`: provision a fresh sandbox for this call.
4118
+ * - any other string: an existing `environment_id` to reuse.
4119
+ * - object: provision a fresh sandbox and optionally preload `sources`
4120
+ * and/or constrain outbound traffic via `network`.
4121
+ */
4122
+ environment: z12.union([
4123
+ z12.string(),
4124
+ z12.object({
4125
+ type: z12.literal("remote"),
4126
+ sources: z12.array(
4127
+ z12.union([
4128
+ z12.object({
4129
+ type: z12.literal("gcs"),
4130
+ source: z12.string(),
4131
+ target: z12.string().nullish()
4132
+ }),
4133
+ z12.object({
4134
+ type: z12.literal("repository"),
4135
+ source: z12.string(),
4136
+ target: z12.string().nullish()
4137
+ }),
4138
+ z12.object({
4139
+ type: z12.literal("inline"),
4140
+ content: z12.string(),
4141
+ target: z12.string()
4142
+ })
4143
+ ])
4144
+ ).nullish(),
4145
+ network: z12.union([
4146
+ z12.literal("disabled"),
4147
+ z12.object({
4148
+ allowlist: z12.array(
4149
+ z12.object({
4150
+ domain: z12.string(),
4151
+ transform: z12.array(z12.record(z12.string(), z12.string())).nullish()
4152
+ })
4153
+ )
4154
+ })
4155
+ ]).nullish()
4156
+ })
4157
+ ]).nullish()
4158
+ })
4159
+ )
4160
+ );
4161
+
4162
+ // src/interactions/parse-google-interactions-outputs.ts
4163
+ function googleProviderMetadata({
4164
+ signature,
4165
+ interactionId
4166
+ }) {
4167
+ const google = {};
4168
+ if (signature != null) {
4169
+ google.signature = signature;
4170
+ }
4171
+ if (interactionId != null) {
4172
+ google.interactionId = interactionId;
4173
+ }
4174
+ return Object.keys(google).length > 0 ? { providerMetadata: { google } } : {};
4175
+ }
4176
+ var BUILTIN_TOOL_CALL_TYPES2 = /* @__PURE__ */ new Set([
4177
+ "google_search_call",
4178
+ "code_execution_call",
4179
+ "url_context_call",
4180
+ "file_search_call",
4181
+ "google_maps_call",
4182
+ "mcp_server_tool_call"
4183
+ ]);
4184
+ var BUILTIN_TOOL_RESULT_TYPES2 = /* @__PURE__ */ new Set([
4185
+ "google_search_result",
4186
+ "code_execution_result",
4187
+ "url_context_result",
4188
+ "file_search_result",
4189
+ "google_maps_result",
4190
+ "mcp_server_tool_result"
4191
+ ]);
4192
+ function builtinToolNameFromCallType2(type) {
4193
+ return type.replace(/_call$/, "");
4194
+ }
4195
+ function builtinToolNameFromResultType2(type) {
4196
+ return type.replace(/_result$/, "");
4197
+ }
4198
+ function parseGoogleInteractionsOutputs({
4199
+ steps,
4200
+ generateId: generateId2,
4201
+ interactionId
4202
+ }) {
4203
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
4204
+ const content = [];
4205
+ let hasFunctionCall = false;
4206
+ if (steps == null) {
4207
+ return { content, hasFunctionCall };
4208
+ }
4209
+ for (const step of steps) {
4210
+ if (step == null || typeof step !== "object") continue;
4211
+ const type = step.type;
4212
+ if (typeof type !== "string") continue;
4213
+ switch (type) {
4214
+ case "user_input": {
4215
+ break;
4216
+ }
4217
+ case "model_output": {
4218
+ const blocks = (_a = step.content) != null ? _a : [];
4219
+ for (const block of blocks) {
4220
+ if (block == null || typeof block !== "object") continue;
4221
+ const blockType = block.type;
4222
+ if (blockType === "text") {
4223
+ const text = (_b = block.text) != null ? _b : "";
4224
+ const annotations = block.annotations;
4225
+ content.push({
4226
+ type: "text",
4227
+ text,
4228
+ ...googleProviderMetadata({ interactionId })
4229
+ });
4230
+ const sources = annotationsToSources({ annotations, generateId: generateId2 });
4231
+ for (const source of sources) {
4232
+ content.push(source);
4233
+ }
4234
+ } else if (blockType === "image") {
4235
+ const image = block;
4236
+ if (image.data != null && image.data.length > 0) {
4237
+ content.push({
4238
+ type: "file",
4239
+ mediaType: (_c = image.mime_type) != null ? _c : "image/png",
4240
+ data: image.data,
4241
+ ...googleProviderMetadata({ interactionId })
4242
+ });
4243
+ } else if (image.uri != null && image.uri.length > 0) {
4244
+ content.push({
4245
+ type: "file",
4246
+ mediaType: (_d = image.mime_type) != null ? _d : "image/png",
4247
+ data: "",
4248
+ providerMetadata: {
4249
+ google: {
4250
+ ...interactionId != null ? { interactionId } : {},
4251
+ imageUri: image.uri
4252
+ }
4253
+ }
4254
+ });
4255
+ }
4256
+ } else if (blockType === "video") {
4257
+ const video = block;
4258
+ if (video.data != null && video.data.length > 0) {
4259
+ content.push({
4260
+ type: "file",
4261
+ mediaType: (_e = video.mime_type) != null ? _e : "video/mp4",
4262
+ data: video.data,
4263
+ ...googleProviderMetadata({ interactionId })
4264
+ });
4265
+ } else if (video.uri != null && video.uri.length > 0) {
4266
+ content.push({
4267
+ type: "file",
4268
+ mediaType: (_f = video.mime_type) != null ? _f : "video/mp4",
4269
+ data: "",
4270
+ providerMetadata: {
4271
+ google: {
4272
+ ...interactionId != null ? { interactionId } : {},
4273
+ videoUri: video.uri
4274
+ }
4275
+ }
4276
+ });
4277
+ }
4278
+ }
4279
+ }
4280
+ break;
4281
+ }
4282
+ case "thought": {
4283
+ const thought = step;
4284
+ const summary = Array.isArray(thought.summary) ? thought.summary : [];
4285
+ const text = summary.filter(
4286
+ (item) => (item == null ? void 0 : item.type) === "text" && typeof item.text === "string"
4287
+ ).map((item) => item.text).join("\n");
4288
+ content.push({
4289
+ type: "reasoning",
4290
+ text,
4291
+ ...googleProviderMetadata({
4292
+ signature: thought.signature,
4293
+ interactionId
4294
+ })
4295
+ });
4296
+ break;
4297
+ }
4298
+ case "function_call": {
4299
+ hasFunctionCall = true;
4300
+ const call = step;
4301
+ content.push({
4302
+ type: "tool-call",
4303
+ toolCallId: call.id,
4304
+ toolName: call.name,
4305
+ input: JSON.stringify((_g = call.arguments) != null ? _g : {}),
4306
+ ...googleProviderMetadata({
4307
+ signature: call.signature,
4308
+ interactionId
4309
+ })
4310
+ });
4311
+ break;
4312
+ }
4313
+ default: {
4314
+ if (BUILTIN_TOOL_CALL_TYPES2.has(type)) {
4315
+ const call = step;
4316
+ const toolName = type === "mcp_server_tool_call" ? (_h = call.name) != null ? _h : "mcp_server_tool" : builtinToolNameFromCallType2(type);
4317
+ const input = JSON.stringify((_i = call.arguments) != null ? _i : {});
4318
+ content.push({
4319
+ type: "tool-call",
4320
+ toolCallId: (_j = call.id) != null ? _j : generateId2(),
4321
+ toolName,
4322
+ input,
4323
+ providerExecuted: true
4324
+ });
4325
+ } else if (BUILTIN_TOOL_RESULT_TYPES2.has(type)) {
4326
+ const result = step;
4327
+ const toolName = type === "mcp_server_tool_result" ? (_k = result.name) != null ? _k : "mcp_server_tool" : builtinToolNameFromResultType2(type);
4328
+ content.push({
4329
+ type: "tool-result",
4330
+ toolCallId: (_l = result.call_id) != null ? _l : generateId2(),
4331
+ toolName,
4332
+ result: (_m = result.result) != null ? _m : null
4333
+ });
4334
+ const sources = builtinToolResultToSources({
4335
+ block: step,
4336
+ generateId: generateId2
4337
+ });
4338
+ for (const source of sources) {
4339
+ content.push(source);
4340
+ }
4341
+ }
4342
+ break;
4343
+ }
4344
+ }
4345
+ }
4346
+ return { content, hasFunctionCall };
4347
+ }
4348
+
4349
+ // src/interactions/poll-google-interactions.ts
4350
+ import {
4351
+ createJsonResponseHandler as createJsonResponseHandler2,
4352
+ delay,
4353
+ getFromApi,
4354
+ isAbortError
4355
+ } from "@ai-sdk/provider-utils";
4356
+
4357
+ // src/interactions/cancel-google-interaction.ts
4358
+ import {
4359
+ combineHeaders as combineHeaders2,
4360
+ getRuntimeEnvironmentUserAgent,
4361
+ withUserAgentSuffix
4362
+ } from "@ai-sdk/provider-utils";
4363
+ var getOriginalFetch = () => globalThis.fetch;
4364
+ async function cancelGoogleInteraction({
4365
+ baseURL,
4366
+ interactionId,
4367
+ headers,
4368
+ fetch = getOriginalFetch()
4369
+ }) {
4370
+ if (interactionId == null || interactionId.length === 0) {
4371
+ return;
4372
+ }
4373
+ const url = `${baseURL}/interactions/${encodeURIComponent(interactionId)}/cancel`;
4374
+ try {
4375
+ const response = await fetch(url, {
4376
+ method: "POST",
4377
+ headers: withUserAgentSuffix(
4378
+ combineHeaders2({ "Content-Type": "application/json" }, headers),
4379
+ getRuntimeEnvironmentUserAgent()
4380
+ ),
4381
+ body: "{}"
4382
+ });
4383
+ try {
4384
+ await response.text();
4385
+ } catch (e) {
4386
+ }
4387
+ } catch (e) {
4388
+ }
4389
+ }
4390
+
4391
+ // src/interactions/poll-google-interactions.ts
4392
+ var TERMINAL_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "incomplete"]);
4393
+ function isTerminalStatus(status) {
4394
+ return status != null && TERMINAL_STATUSES.has(status);
4395
+ }
4396
+ var DEFAULT_INITIAL_DELAY_MS = 1e3;
4397
+ var DEFAULT_MAX_DELAY_MS = 1e4;
4398
+ var DEFAULT_TIMEOUT_MS = 30 * 60 * 1e3;
4399
+ async function pollGoogleInteractionUntilTerminal({
4400
+ baseURL,
4401
+ interactionId,
4402
+ headers,
4403
+ fetch,
4404
+ abortSignal,
4405
+ initialDelayMs = DEFAULT_INITIAL_DELAY_MS,
4406
+ maxDelayMs = DEFAULT_MAX_DELAY_MS,
4407
+ timeoutMs = DEFAULT_TIMEOUT_MS
4408
+ }) {
4409
+ if (interactionId == null || interactionId.length === 0) {
4410
+ throw new Error(
4411
+ "google.interactions: cannot poll a background interaction without an id. The POST response did not include an interaction id."
4412
+ );
4413
+ }
4414
+ const startedAt = Date.now();
4415
+ let nextDelayMs = initialDelayMs;
4416
+ const url = `${baseURL}/interactions/${encodeURIComponent(interactionId)}`;
4417
+ const cancelOnServer = () => cancelGoogleInteraction({ baseURL, interactionId, headers, fetch });
4418
+ try {
4419
+ while (true) {
4420
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
4421
+ await cancelOnServer();
4422
+ throw new DOMException("Polling was aborted", "AbortError");
4423
+ }
4424
+ if (Date.now() - startedAt > timeoutMs) {
4425
+ throw new Error(
4426
+ `google.interactions: timed out polling interaction ${interactionId} after ${timeoutMs}ms.`
4427
+ );
4428
+ }
4429
+ await delay(nextDelayMs, { abortSignal });
4430
+ const {
4431
+ value: response,
4432
+ rawValue: rawResponse,
4433
+ responseHeaders
4434
+ } = await getFromApi({
4435
+ url,
4436
+ headers,
4437
+ failedResponseHandler: googleFailedResponseHandler,
4438
+ successfulResponseHandler: createJsonResponseHandler2(
4439
+ googleInteractionsResponseSchema
4440
+ ),
4441
+ abortSignal,
4442
+ fetch
4443
+ });
4444
+ if (isTerminalStatus(response.status)) {
4445
+ return { response, rawResponse, responseHeaders };
4446
+ }
4447
+ nextDelayMs = Math.min(nextDelayMs * 2, maxDelayMs);
4448
+ }
4449
+ } catch (error) {
4450
+ if (isAbortError(error)) {
4451
+ await cancelOnServer();
4452
+ }
4453
+ throw error;
4454
+ }
4455
+ }
4456
+
4457
+ // src/interactions/prepare-google-interactions-tools.ts
4458
+ function prepareGoogleInteractionsTools({
4459
+ tools,
4460
+ toolChoice
4461
+ }) {
4462
+ var _a, _b, _c, _d;
4463
+ const toolWarnings = [];
4464
+ const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
4465
+ if (normalized == null) {
4466
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
4467
+ }
4468
+ const interactionsTools = [];
4469
+ for (const tool of normalized) {
4470
+ if (tool.type === "function") {
4471
+ interactionsTools.push({
4472
+ type: "function",
4473
+ name: tool.name,
4474
+ description: (_a = tool.description) != null ? _a : "",
4475
+ parameters: tool.inputSchema
4476
+ });
4477
+ continue;
4478
+ }
4479
+ if (tool.type === "provider") {
4480
+ const args = (_b = tool.args) != null ? _b : {};
4481
+ switch (tool.id) {
4482
+ case "google.google_search": {
4483
+ const searchTypesArg = args.searchTypes;
4484
+ let search_types;
4485
+ if (searchTypesArg != null && typeof searchTypesArg === "object") {
4486
+ const list = [];
4487
+ if (searchTypesArg.webSearch != null) list.push("web_search");
4488
+ if (searchTypesArg.imageSearch != null) list.push("image_search");
4489
+ if (list.length > 0) {
4490
+ search_types = list;
4491
+ }
4492
+ }
4493
+ interactionsTools.push({
4494
+ type: "google_search",
4495
+ ...search_types != null ? { search_types } : {}
4496
+ });
4497
+ break;
4498
+ }
4499
+ case "google.code_execution": {
4500
+ interactionsTools.push({ type: "code_execution" });
4501
+ break;
4502
+ }
4503
+ case "google.url_context": {
4504
+ interactionsTools.push({ type: "url_context" });
4505
+ break;
4506
+ }
4507
+ case "google.file_search": {
4508
+ interactionsTools.push({
4509
+ type: "file_search",
4510
+ ...args.fileSearchStoreNames != null ? {
4511
+ file_search_store_names: args.fileSearchStoreNames
4512
+ } : {},
4513
+ ...args.topK != null ? { top_k: args.topK } : {},
4514
+ ...args.metadataFilter != null ? { metadata_filter: args.metadataFilter } : {}
4515
+ });
4516
+ break;
4517
+ }
4518
+ case "google.google_maps": {
4519
+ interactionsTools.push({
4520
+ type: "google_maps",
4521
+ ...args.latitude != null ? { latitude: args.latitude } : {},
4522
+ ...args.longitude != null ? { longitude: args.longitude } : {},
4523
+ ...args.enableWidget != null ? { enable_widget: args.enableWidget } : {}
4524
+ });
4525
+ break;
4526
+ }
4527
+ case "google.computer_use": {
4528
+ interactionsTools.push({
4529
+ type: "computer_use",
4530
+ environment: (_c = args.environment) != null ? _c : "browser",
4531
+ ...args.excludedPredefinedFunctions != null ? {
4532
+ excludedPredefinedFunctions: args.excludedPredefinedFunctions
4533
+ } : {}
4534
+ });
4535
+ break;
4536
+ }
4537
+ case "google.mcp_server": {
4538
+ interactionsTools.push({
4539
+ type: "mcp_server",
4540
+ ...args.name != null ? { name: args.name } : {},
4541
+ ...args.url != null ? { url: args.url } : {},
4542
+ ...args.headers != null ? { headers: args.headers } : {},
4543
+ ...args.allowedTools != null ? { allowed_tools: args.allowedTools } : {}
4544
+ });
4545
+ break;
4546
+ }
4547
+ case "google.retrieval": {
4548
+ const vertexAiSearchConfig = (_d = args.vertexAiSearchConfig) != null ? _d : void 0;
4549
+ interactionsTools.push({
4550
+ type: "retrieval",
4551
+ ...args.retrievalTypes != null ? {
4552
+ retrieval_types: args.retrievalTypes
4553
+ } : { retrieval_types: ["vertex_ai_search"] },
4554
+ ...vertexAiSearchConfig != null ? { vertex_ai_search_config: vertexAiSearchConfig } : {}
4555
+ });
4556
+ break;
4557
+ }
4558
+ default: {
4559
+ toolWarnings.push({
4560
+ type: "unsupported",
4561
+ feature: `provider-defined tool ${tool.id}`,
4562
+ details: `provider-defined tool ${tool.id} is not supported by google.interactions; tool dropped.`
4563
+ });
4564
+ break;
4565
+ }
4566
+ }
4567
+ continue;
4568
+ }
4569
+ toolWarnings.push({
4570
+ type: "unsupported",
4571
+ feature: `tool of type ${tool.type}`,
4572
+ details: "Only function tools and google.* provider-defined tools are supported by google.interactions; tool dropped."
4573
+ });
4574
+ }
4575
+ const hasFunctionTool = interactionsTools.some((t) => t.type === "function");
4576
+ let mappedToolChoice;
4577
+ if (toolChoice != null && hasFunctionTool) {
4578
+ switch (toolChoice.type) {
4579
+ case "auto":
4580
+ mappedToolChoice = "auto";
4581
+ break;
4582
+ case "required":
4583
+ mappedToolChoice = "any";
4584
+ break;
4585
+ case "none":
4586
+ mappedToolChoice = "none";
4587
+ break;
4588
+ case "tool":
4589
+ mappedToolChoice = {
4590
+ allowed_tools: {
4591
+ mode: "validated",
4592
+ tools: [toolChoice.toolName]
4593
+ }
4594
+ };
4595
+ break;
4596
+ }
4597
+ }
4598
+ return {
4599
+ tools: interactionsTools.length > 0 ? interactionsTools : void 0,
4600
+ toolChoice: mappedToolChoice,
4601
+ toolWarnings
4602
+ };
4603
+ }
4604
+
4605
+ // src/interactions/stream-google-interactions.ts
4606
+ import {
4607
+ createEventSourceResponseHandler as createEventSourceResponseHandler2,
4608
+ delay as delay2,
4609
+ getFromApi as getFromApi2,
4610
+ isAbortError as isAbortError2
4611
+ } from "@ai-sdk/provider-utils";
4612
+ var DEFAULT_MAX_RETRIES = 3;
4613
+ var DEFAULT_RETRY_DELAY_MS = 500;
4614
+ function streamGoogleInteractionEvents({
4615
+ baseURL,
4616
+ interactionId,
4617
+ headers,
4618
+ fetch,
4619
+ abortSignal,
4620
+ maxRetries = DEFAULT_MAX_RETRIES,
4621
+ retryDelayMs = DEFAULT_RETRY_DELAY_MS
4622
+ }) {
4623
+ if (interactionId.length === 0) {
4624
+ throw new Error(
4625
+ "google.interactions: cannot stream a background interaction without an id."
4626
+ );
4627
+ }
4628
+ const eventSourceHeaders = {
4629
+ ...headers,
4630
+ accept: "text/event-stream"
4631
+ };
4632
+ let lastEventId;
4633
+ let complete = false;
4634
+ let attempt = 0;
4635
+ let receivedAnyEventThisAttempt = false;
4636
+ let currentReader;
4637
+ const internalAbort = new AbortController();
4638
+ const upstreamAbortHandler = () => internalAbort.abort();
4639
+ if (abortSignal != null) {
4640
+ if (abortSignal.aborted) {
4641
+ internalAbort.abort();
4642
+ } else {
4643
+ abortSignal.addEventListener("abort", upstreamAbortHandler, {
4644
+ once: true
4645
+ });
4646
+ }
4647
+ }
4648
+ const effectiveSignal = internalAbort.signal;
4649
+ function buildUrl() {
4650
+ const base = `${baseURL}/interactions/${encodeURIComponent(interactionId)}`;
4651
+ const params = new URLSearchParams({ stream: "true" });
4652
+ if (lastEventId != null) {
4653
+ params.set("last_event_id", lastEventId);
4654
+ }
4655
+ return `${base}?${params.toString()}`;
4656
+ }
4657
+ async function openReader() {
4658
+ const { value: stream } = await getFromApi2({
4659
+ url: buildUrl(),
4660
+ headers: eventSourceHeaders,
4661
+ failedResponseHandler: googleFailedResponseHandler,
4662
+ successfulResponseHandler: createEventSourceResponseHandler2(
4663
+ googleInteractionsEventSchema
4664
+ ),
4665
+ abortSignal: effectiveSignal,
4666
+ fetch
4667
+ });
4668
+ return stream.getReader();
4669
+ }
4670
+ return new ReadableStream({
4671
+ async start(controller) {
4672
+ try {
4673
+ while (!complete && !effectiveSignal.aborted) {
4674
+ if (currentReader == null) {
4675
+ try {
4676
+ currentReader = await openReader();
4677
+ receivedAnyEventThisAttempt = false;
4678
+ } catch (error) {
4679
+ if (isAbortError2(error) || effectiveSignal.aborted) {
4680
+ controller.error(error);
4681
+ return;
4682
+ }
4683
+ attempt++;
4684
+ if (attempt >= maxRetries) {
4685
+ controller.error(error);
4686
+ return;
4687
+ }
4688
+ await delay2(retryDelayMs * attempt, {
4689
+ abortSignal: effectiveSignal
4690
+ });
4691
+ continue;
4692
+ }
4693
+ }
4694
+ try {
4695
+ const { done, value } = await currentReader.read();
4696
+ if (done) {
4697
+ currentReader = void 0;
4698
+ if (complete) break;
4699
+ if (!receivedAnyEventThisAttempt) {
4700
+ attempt++;
4701
+ if (attempt >= maxRetries) {
4702
+ controller.error(
4703
+ new Error(
4704
+ "google.interactions: SSE stream closed without producing any events."
4705
+ )
4706
+ );
4707
+ return;
4708
+ }
4709
+ await delay2(retryDelayMs * attempt, {
4710
+ abortSignal: effectiveSignal
4711
+ });
4712
+ } else {
4713
+ attempt = 0;
4714
+ }
4715
+ continue;
4716
+ }
4717
+ receivedAnyEventThisAttempt = true;
4718
+ if (value.success) {
4719
+ const ev = value.value;
4720
+ if (typeof ev.event_id === "string" && ev.event_id.length > 0) {
4721
+ lastEventId = ev.event_id;
4722
+ }
4723
+ if (ev.event_type === "interaction.completed" || ev.event_type === "error") {
4724
+ complete = true;
4725
+ }
4726
+ }
4727
+ controller.enqueue(value);
4728
+ } catch (error) {
4729
+ if (isAbortError2(error) || effectiveSignal.aborted) {
4730
+ controller.error(error);
4731
+ return;
4732
+ }
4733
+ currentReader = void 0;
4734
+ attempt++;
4735
+ if (attempt >= maxRetries) {
4736
+ controller.error(error);
4737
+ return;
4738
+ }
4739
+ await delay2(retryDelayMs * attempt, {
4740
+ abortSignal: effectiveSignal
4741
+ });
4742
+ }
4743
+ }
4744
+ controller.close();
4745
+ } catch (error) {
4746
+ controller.error(error);
4747
+ } finally {
4748
+ if (abortSignal != null) {
4749
+ abortSignal.removeEventListener("abort", upstreamAbortHandler);
4750
+ }
4751
+ currentReader == null ? void 0 : currentReader.cancel().catch(() => {
4752
+ });
4753
+ currentReader = void 0;
4754
+ if (effectiveSignal.aborted && !complete) {
4755
+ await cancelGoogleInteraction({
4756
+ baseURL,
4757
+ interactionId,
4758
+ headers,
4759
+ fetch
4760
+ });
4761
+ }
4762
+ }
4763
+ },
4764
+ cancel() {
4765
+ internalAbort.abort();
4766
+ currentReader == null ? void 0 : currentReader.cancel().catch(() => {
4767
+ });
4768
+ currentReader = void 0;
4769
+ }
4770
+ });
4771
+ }
4772
+
4773
+ // src/interactions/synthesize-google-interactions-agent-stream.ts
4774
+ function synthesizeGoogleInteractionsAgentStream({
4775
+ response,
4776
+ warnings,
4777
+ generateId: generateId2,
4778
+ includeRawChunks,
4779
+ headerServiceTier
4780
+ }) {
4781
+ return new ReadableStream({
4782
+ start(controller) {
4783
+ var _a, _b, _c;
4784
+ controller.enqueue({ type: "stream-start", warnings });
4785
+ const interactionId = typeof response.id === "string" && response.id.length > 0 ? response.id : void 0;
4786
+ let timestamp;
4787
+ const created = response.created;
4788
+ if (typeof created === "string") {
4789
+ const parsed = new Date(created);
4790
+ if (!Number.isNaN(parsed.getTime())) {
4791
+ timestamp = parsed;
4792
+ }
4793
+ }
4794
+ controller.enqueue({
4795
+ type: "response-metadata",
4796
+ ...interactionId != null ? { id: interactionId } : {},
4797
+ modelId: (_a = response.model) != null ? _a : void 0,
4798
+ ...timestamp ? { timestamp } : {}
4799
+ });
4800
+ if (includeRawChunks) {
4801
+ controller.enqueue({ type: "raw", rawValue: response });
4802
+ }
4803
+ const { content, hasFunctionCall } = parseGoogleInteractionsOutputs({
4804
+ steps: (_b = response.steps) != null ? _b : null,
4805
+ generateId: generateId2,
4806
+ interactionId
4807
+ });
4808
+ let blockCounter = 0;
4809
+ const nextBlockId = () => `${interactionId != null ? interactionId : "agent"}:${blockCounter++}`;
4810
+ for (const part of content) {
4811
+ switch (part.type) {
4812
+ case "text": {
4813
+ const id = nextBlockId();
4814
+ const providerMetadata2 = part.providerMetadata;
4815
+ controller.enqueue({ type: "text-start", id });
4816
+ if (part.text.length > 0) {
4817
+ controller.enqueue({ type: "text-delta", id, delta: part.text });
4818
+ }
4819
+ controller.enqueue({
4820
+ type: "text-end",
4821
+ id,
4822
+ ...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
4823
+ });
4824
+ break;
4825
+ }
4826
+ case "reasoning": {
4827
+ const id = nextBlockId();
4828
+ const providerMetadata2 = part.providerMetadata;
4829
+ controller.enqueue({ type: "reasoning-start", id });
4830
+ if (part.text.length > 0) {
4831
+ controller.enqueue({
4832
+ type: "reasoning-delta",
4833
+ id,
4834
+ delta: part.text
4835
+ });
4836
+ }
4837
+ controller.enqueue({
4838
+ type: "reasoning-end",
4839
+ id,
4840
+ ...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
4841
+ });
4842
+ break;
4843
+ }
4844
+ case "tool-call": {
4845
+ const providerMetadata2 = part.providerMetadata;
4846
+ controller.enqueue({
4847
+ type: "tool-input-start",
4848
+ id: part.toolCallId,
4849
+ toolName: part.toolName,
4850
+ ...part.providerExecuted ? { providerExecuted: part.providerExecuted } : {}
4851
+ });
4852
+ controller.enqueue({
4853
+ type: "tool-input-delta",
4854
+ id: part.toolCallId,
4855
+ delta: part.input
4856
+ });
4857
+ controller.enqueue({
4858
+ type: "tool-input-end",
4859
+ id: part.toolCallId
4860
+ });
4861
+ controller.enqueue({
4862
+ type: "tool-call",
4863
+ toolCallId: part.toolCallId,
4864
+ toolName: part.toolName,
4865
+ input: part.input,
4866
+ ...part.providerExecuted ? { providerExecuted: part.providerExecuted } : {},
4867
+ ...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
4868
+ });
4869
+ break;
4870
+ }
4871
+ case "tool-result": {
4872
+ controller.enqueue({
4873
+ type: "tool-result",
4874
+ toolCallId: part.toolCallId,
4875
+ toolName: part.toolName,
4876
+ result: part.result
4877
+ });
4878
+ break;
4879
+ }
4880
+ case "source":
4881
+ case "file": {
4882
+ controller.enqueue(part);
4883
+ break;
4884
+ }
4885
+ default:
4886
+ break;
4887
+ }
4888
+ }
4889
+ const serviceTier = (_c = response.service_tier) != null ? _c : headerServiceTier;
4890
+ const finishReason = {
4891
+ unified: mapGoogleInteractionsFinishReason({
4892
+ status: response.status,
4893
+ hasFunctionCall
4894
+ }),
4895
+ raw: response.status
4896
+ };
4897
+ const providerMetadata = {
4898
+ google: {
4899
+ ...interactionId != null ? { interactionId } : {},
4900
+ ...serviceTier != null ? { serviceTier } : {}
4901
+ }
4902
+ };
4903
+ controller.enqueue({
4904
+ type: "finish",
4905
+ finishReason,
4906
+ usage: convertGoogleInteractionsUsage(response.usage),
4907
+ providerMetadata
4908
+ });
4909
+ controller.close();
4910
+ }
4911
+ });
4912
+ }
4913
+
4914
+ // src/interactions/google-interactions-language-model.ts
4915
+ var GoogleInteractionsLanguageModel = class {
4916
+ constructor(modelOrAgent, config) {
4917
+ this.specificationVersion = "v3";
4918
+ if (typeof modelOrAgent === "string") {
4919
+ this.modelId = modelOrAgent;
4920
+ this.agent = void 0;
4921
+ } else if ("managedAgent" in modelOrAgent) {
4922
+ this.modelId = modelOrAgent.managedAgent;
4923
+ this.agent = modelOrAgent.managedAgent;
4924
+ } else {
4925
+ this.modelId = modelOrAgent.agent;
4926
+ this.agent = modelOrAgent.agent;
4927
+ }
4928
+ this.config = config;
4929
+ }
4930
+ get provider() {
4931
+ return this.config.provider;
4932
+ }
4933
+ get supportedUrls() {
4934
+ if (this.config.supportedUrls) {
4935
+ return this.config.supportedUrls();
4936
+ }
4937
+ return {
4938
+ "image/*": [/^https?:\/\/.+/],
4939
+ "application/pdf": [/^https?:\/\/.+/],
4940
+ "audio/*": [/^https?:\/\/.+/],
4941
+ "video/*": [
4942
+ /^https?:\/\/(www\.)?youtube\.com\/watch\?v=.+/,
4943
+ /^https?:\/\/youtu\.be\/.+/,
4944
+ /^gs:\/\/.+/
4945
+ ]
4946
+ };
4947
+ }
4948
+ async getArgs(options) {
4949
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
4950
+ const warnings = [];
4951
+ const opts = await parseProviderOptions2({
4952
+ provider: "google",
4953
+ providerOptions: options.providerOptions,
4954
+ schema: googleInteractionsLanguageModelOptions
4955
+ });
4956
+ const isAgent = this.agent != null;
4957
+ const hasTools = options.tools != null && options.tools.length > 0;
4958
+ let toolsForBody;
4959
+ let toolChoiceForBody;
4960
+ if (hasTools && isAgent) {
4961
+ warnings.push({
4962
+ type: "other",
4963
+ message: "google.interactions: tools are not supported when an agent is set; tools will be omitted from the request body."
4964
+ });
4965
+ } else if (hasTools) {
4966
+ const prepared = prepareGoogleInteractionsTools({
4967
+ tools: options.tools,
4968
+ toolChoice: options.toolChoice
4969
+ });
4970
+ toolsForBody = prepared.tools;
4971
+ toolChoiceForBody = prepared.toolChoice;
4972
+ warnings.push(...prepared.toolWarnings);
4973
+ }
4974
+ const responseFormatEntries = [];
4975
+ if (((_a = options.responseFormat) == null ? void 0 : _a.type) === "json") {
4976
+ if (isAgent) {
4977
+ warnings.push({
4978
+ type: "other",
4979
+ message: "google.interactions: structured output (responseFormat) is not supported when an agent is set; responseFormat will be ignored."
4980
+ });
4981
+ } else {
4982
+ const entry = {
4983
+ type: "text",
4984
+ mime_type: "application/json",
4985
+ ...options.responseFormat.schema != null ? { schema: options.responseFormat.schema } : {}
4986
+ };
4987
+ responseFormatEntries.push(entry);
4988
+ }
4989
+ }
4990
+ if ((opts == null ? void 0 : opts.responseFormat) != null) {
4991
+ for (const entry of opts.responseFormat) {
4992
+ if (entry.type === "text") {
4993
+ responseFormatEntries.push(
4994
+ pruneUndefined({
4995
+ type: "text",
4996
+ mime_type: (_b = entry.mimeType) != null ? _b : void 0,
4997
+ schema: (_c = entry.schema) != null ? _c : void 0
4998
+ })
4999
+ );
5000
+ } else if (entry.type === "image") {
5001
+ responseFormatEntries.push(
5002
+ pruneUndefined({
5003
+ type: "image",
5004
+ mime_type: (_d = entry.mimeType) != null ? _d : void 0,
5005
+ aspect_ratio: (_e = entry.aspectRatio) != null ? _e : void 0,
5006
+ image_size: (_f = entry.imageSize) != null ? _f : void 0
5007
+ })
5008
+ );
5009
+ } else if (entry.type === "audio") {
5010
+ responseFormatEntries.push(
5011
+ pruneUndefined({
5012
+ type: "audio",
5013
+ mime_type: (_g = entry.mimeType) != null ? _g : void 0
5014
+ })
5015
+ );
5016
+ }
5017
+ }
5018
+ }
5019
+ const {
5020
+ input,
5021
+ systemInstruction: convertedSystemInstruction,
5022
+ warnings: convWarnings
5023
+ } = convertToGoogleInteractionsInput({
5024
+ prompt: options.prompt,
5025
+ previousInteractionId: (_h = opts == null ? void 0 : opts.previousInteractionId) != null ? _h : void 0,
5026
+ store: (_i = opts == null ? void 0 : opts.store) != null ? _i : void 0,
5027
+ mediaResolution: (_j = opts == null ? void 0 : opts.mediaResolution) != null ? _j : void 0
5028
+ });
5029
+ warnings.push(...convWarnings);
5030
+ let systemInstruction = convertedSystemInstruction;
5031
+ const optionSystemInstruction = (_k = opts == null ? void 0 : opts.systemInstruction) != null ? _k : void 0;
5032
+ if (systemInstruction != null && optionSystemInstruction != null) {
5033
+ warnings.push({
5034
+ type: "other",
5035
+ message: "google.interactions: both AI SDK system message and providerOptions.google.systemInstruction were set; using the AI SDK system message."
5036
+ });
5037
+ } else if (systemInstruction == null && optionSystemInstruction != null) {
5038
+ systemInstruction = optionSystemInstruction;
5039
+ }
5040
+ let generationConfig;
5041
+ if (isAgent) {
5042
+ const droppedFields = [];
5043
+ if (options.temperature != null) droppedFields.push("temperature");
5044
+ if (options.topP != null) droppedFields.push("topP");
5045
+ if (options.seed != null) droppedFields.push("seed");
5046
+ if (options.stopSequences != null && options.stopSequences.length > 0) {
5047
+ droppedFields.push("stopSequences");
5048
+ }
5049
+ if (options.maxOutputTokens != null)
5050
+ droppedFields.push("maxOutputTokens");
5051
+ if ((opts == null ? void 0 : opts.thinkingLevel) != null) droppedFields.push("thinkingLevel");
5052
+ if ((opts == null ? void 0 : opts.thinkingSummaries) != null) {
5053
+ droppedFields.push("thinkingSummaries");
5054
+ }
5055
+ if ((opts == null ? void 0 : opts.imageConfig) != null) droppedFields.push("imageConfig");
5056
+ if (droppedFields.length > 0) {
5057
+ warnings.push({
5058
+ type: "other",
5059
+ message: `google.interactions: ${droppedFields.join(", ")} ${droppedFields.length === 1 ? "is" : "are"} not supported when an agent is set; use providerOptions.google.agentConfig instead. Dropped from the request body.`
5060
+ });
5061
+ }
5062
+ generationConfig = void 0;
5063
+ } else {
5064
+ generationConfig = pruneUndefined({
5065
+ temperature: (_l = options.temperature) != null ? _l : void 0,
5066
+ top_p: (_m = options.topP) != null ? _m : void 0,
5067
+ seed: (_n = options.seed) != null ? _n : void 0,
5068
+ stop_sequences: options.stopSequences != null && options.stopSequences.length > 0 ? options.stopSequences : void 0,
5069
+ max_output_tokens: (_o = options.maxOutputTokens) != null ? _o : void 0,
5070
+ thinking_level: (_p = opts == null ? void 0 : opts.thinkingLevel) != null ? _p : void 0,
5071
+ thinking_summaries: (_q = opts == null ? void 0 : opts.thinkingSummaries) != null ? _q : void 0,
5072
+ tool_choice: toolChoiceForBody
5073
+ });
5074
+ if ((opts == null ? void 0 : opts.imageConfig) != null) {
5075
+ const alreadyHasImageEntry = responseFormatEntries.some(
5076
+ (entry) => entry.type === "image"
5077
+ );
5078
+ warnings.push({
5079
+ type: "other",
5080
+ message: alreadyHasImageEntry ? "google.interactions: providerOptions.google.imageConfig is deprecated and was ignored because providerOptions.google.responseFormat already supplies an image entry. Use responseFormat exclusively." : 'google.interactions: providerOptions.google.imageConfig is deprecated. Use providerOptions.google.responseFormat with a { type: "image", ... } entry instead.'
5081
+ });
5082
+ if (!alreadyHasImageEntry) {
5083
+ responseFormatEntries.push({
5084
+ type: "image",
5085
+ mime_type: "image/png",
5086
+ ...opts.imageConfig.aspectRatio != null ? { aspect_ratio: opts.imageConfig.aspectRatio } : {},
5087
+ ...opts.imageConfig.imageSize != null ? { image_size: opts.imageConfig.imageSize } : {}
5088
+ });
5089
+ }
5090
+ }
5091
+ }
5092
+ let agentConfig;
5093
+ if (isAgent && (opts == null ? void 0 : opts.agentConfig) != null) {
5094
+ const ac = opts.agentConfig;
5095
+ if (ac.type === "deep-research") {
5096
+ agentConfig = pruneUndefined({
5097
+ type: "deep-research",
5098
+ thinking_summaries: (_r = ac.thinkingSummaries) != null ? _r : void 0,
5099
+ visualization: (_s = ac.visualization) != null ? _s : void 0,
5100
+ collaborative_planning: (_t = ac.collaborativePlanning) != null ? _t : void 0
5101
+ });
5102
+ } else if (ac.type === "dynamic") {
5103
+ agentConfig = { type: "dynamic" };
5104
+ }
5105
+ }
5106
+ let environment;
5107
+ if ((opts == null ? void 0 : opts.environment) != null) {
5108
+ if (!isAgent) {
5109
+ warnings.push({
5110
+ type: "other",
5111
+ message: "google.interactions: environment is only supported when an agent is set; environment will be omitted from the request body."
5112
+ });
5113
+ } else if (typeof opts.environment === "string") {
5114
+ environment = opts.environment;
5115
+ } else {
5116
+ const env = opts.environment;
5117
+ const sources = (_u = env.sources) == null ? void 0 : _u.map((s) => {
5118
+ var _a2;
5119
+ if (s.type === "inline") {
5120
+ return {
5121
+ type: "inline",
5122
+ content: s.content,
5123
+ target: s.target
5124
+ };
5125
+ }
5126
+ return pruneUndefined({
5127
+ type: s.type,
5128
+ source: s.source,
5129
+ target: (_a2 = s.target) != null ? _a2 : void 0
5130
+ });
5131
+ });
5132
+ let network;
5133
+ if (env.network === "disabled") {
5134
+ network = "disabled";
5135
+ } else if (env.network != null) {
5136
+ network = {
5137
+ allowlist: env.network.allowlist.map(
5138
+ (entry) => {
5139
+ var _a2;
5140
+ return pruneUndefined({
5141
+ domain: entry.domain,
5142
+ transform: (_a2 = entry.transform) != null ? _a2 : void 0
5143
+ });
5144
+ }
5145
+ )
5146
+ };
5147
+ }
5148
+ environment = pruneUndefined({
5149
+ type: "remote",
5150
+ sources: sources != null && sources.length > 0 ? sources : void 0,
5151
+ network
5152
+ });
5153
+ }
5154
+ }
5155
+ const args = pruneUndefined({
5156
+ ...isAgent ? { agent: this.agent } : { model: this.modelId },
5157
+ input,
5158
+ system_instruction: systemInstruction,
5159
+ tools: toolsForBody,
5160
+ response_format: responseFormatEntries.length > 0 ? responseFormatEntries : void 0,
5161
+ response_modalities: (opts == null ? void 0 : opts.responseModalities) != null ? opts.responseModalities : void 0,
5162
+ previous_interaction_id: (_v = opts == null ? void 0 : opts.previousInteractionId) != null ? _v : void 0,
5163
+ service_tier: (_w = opts == null ? void 0 : opts.serviceTier) != null ? _w : void 0,
5164
+ store: (_x = opts == null ? void 0 : opts.store) != null ? _x : void 0,
5165
+ generation_config: generationConfig != null && Object.keys(generationConfig).length > 0 ? generationConfig : void 0,
5166
+ agent_config: agentConfig,
5167
+ environment,
5168
+ background: (_y = opts == null ? void 0 : opts.background) != null ? _y : void 0
5169
+ });
5170
+ return {
5171
+ args,
5172
+ warnings,
5173
+ isAgent,
5174
+ isBackground: (opts == null ? void 0 : opts.background) === true,
5175
+ pollingTimeoutMs: (_z = opts == null ? void 0 : opts.pollingTimeoutMs) != null ? _z : void 0
5176
+ };
5177
+ }
5178
+ async doGenerate(options) {
5179
+ var _a, _b, _c, _d, _e, _f;
5180
+ const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
5181
+ const url = `${this.config.baseURL}/interactions`;
5182
+ const mergedHeaders = combineHeaders3(
5183
+ INTERACTIONS_API_REVISION_HEADER,
5184
+ this.config.headers ? await resolve2(this.config.headers) : void 0,
5185
+ options.headers
5186
+ );
5187
+ const postResult = await postJsonToApi2({
5188
+ url,
5189
+ headers: mergedHeaders,
5190
+ body: args,
5191
+ failedResponseHandler: googleFailedResponseHandler,
5192
+ successfulResponseHandler: createJsonResponseHandler3(
5193
+ googleInteractionsResponseSchema
5194
+ ),
5195
+ abortSignal: options.abortSignal,
5196
+ fetch: this.config.fetch
5197
+ });
5198
+ let {
5199
+ responseHeaders,
5200
+ value: response,
5201
+ rawValue: rawResponse
5202
+ } = postResult;
5203
+ if (isAgent && !isTerminalStatus(response.status)) {
5204
+ const polled = await pollGoogleInteractionUntilTerminal({
5205
+ baseURL: this.config.baseURL,
5206
+ interactionId: response.id,
5207
+ headers: mergedHeaders,
5208
+ fetch: this.config.fetch,
5209
+ abortSignal: options.abortSignal,
5210
+ timeoutMs: pollingTimeoutMs
5211
+ });
5212
+ response = polled.response;
5213
+ rawResponse = polled.rawResponse;
5214
+ responseHeaders = (_a = polled.responseHeaders) != null ? _a : responseHeaders;
5215
+ }
5216
+ const interactionId = typeof response.id === "string" && response.id.length > 0 ? response.id : void 0;
5217
+ const { content, hasFunctionCall } = parseGoogleInteractionsOutputs({
5218
+ steps: (_b = response.steps) != null ? _b : null,
5219
+ generateId: (_c = this.config.generateId) != null ? _c : defaultGenerateId,
5220
+ interactionId
5221
+ });
5222
+ const finishReason = {
5223
+ unified: mapGoogleInteractionsFinishReason({
5224
+ status: response.status,
5225
+ hasFunctionCall
5226
+ }),
5227
+ raw: response.status
5228
+ };
5229
+ const serviceTier = (_e = (_d = response.service_tier) != null ? _d : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _e : void 0;
5230
+ const outputTokensByModality = getGoogleInteractionsOutputTokensByModality(
5231
+ response.usage
5232
+ );
5233
+ const providerMetadata = {
5234
+ google: {
5235
+ ...interactionId != null ? { interactionId } : {},
5236
+ ...serviceTier != null ? { serviceTier } : {},
5237
+ ...outputTokensByModality != null ? { outputTokensByModality } : {}
5238
+ }
5239
+ };
5240
+ let timestamp;
5241
+ if (typeof response.created === "string") {
5242
+ const parsed = new Date(response.created);
5243
+ if (!Number.isNaN(parsed.getTime())) {
5244
+ timestamp = parsed;
5245
+ }
5246
+ }
5247
+ return {
5248
+ content,
5249
+ finishReason,
5250
+ usage: convertGoogleInteractionsUsage(response.usage),
5251
+ warnings,
5252
+ providerMetadata,
5253
+ request: { body: args },
5254
+ response: {
5255
+ headers: responseHeaders,
5256
+ body: rawResponse,
5257
+ ...interactionId != null ? { id: interactionId } : {},
5258
+ ...timestamp ? { timestamp } : {},
5259
+ modelId: (_f = response.model) != null ? _f : void 0
5260
+ }
5261
+ };
5262
+ }
5263
+ async doStream(options) {
5264
+ var _a;
5265
+ const { args, warnings, isBackground, pollingTimeoutMs } = await this.getArgs(options);
5266
+ const url = `${this.config.baseURL}/interactions`;
5267
+ const mergedHeaders = combineHeaders3(
5268
+ INTERACTIONS_API_REVISION_HEADER,
5269
+ this.config.headers ? await resolve2(this.config.headers) : void 0,
5270
+ options.headers
5271
+ );
5272
+ if (isBackground) {
5273
+ return this.doStreamBackground({
5274
+ args,
5275
+ warnings,
5276
+ url,
5277
+ mergedHeaders,
5278
+ options,
5279
+ pollingTimeoutMs
5280
+ });
5281
+ }
5282
+ const body = { ...args, stream: true };
5283
+ const { responseHeaders, value: response } = await postJsonToApi2({
5284
+ url,
5285
+ headers: mergedHeaders,
5286
+ body,
5287
+ failedResponseHandler: googleFailedResponseHandler,
5288
+ successfulResponseHandler: createEventSourceResponseHandler3(
5289
+ googleInteractionsEventSchema
5290
+ ),
5291
+ abortSignal: options.abortSignal,
5292
+ fetch: this.config.fetch
5293
+ });
5294
+ const headerServiceTier = responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"];
5295
+ const transform = buildGoogleInteractionsStreamTransform({
5296
+ warnings,
5297
+ generateId: (_a = this.config.generateId) != null ? _a : defaultGenerateId,
5298
+ includeRawChunks: options.includeRawChunks,
5299
+ serviceTier: headerServiceTier
5300
+ });
5301
+ return {
5302
+ stream: response.pipeThrough(transform),
5303
+ request: { body },
5304
+ response: { headers: responseHeaders }
5305
+ };
5306
+ }
5307
+ /*
5308
+ * Drive the streaming surface for agent calls. Agents require
5309
+ * `background: true`, which is incompatible with `stream: true` on POST.
5310
+ *
5311
+ * Approach:
5312
+ * 1. POST `/interactions` with `background: true`. The response includes
5313
+ * the interaction id and an initial (usually non-terminal) status.
5314
+ * 2. If the POST status is already terminal (rare), synthesize a stream
5315
+ * from the polled outputs and we're done.
5316
+ * 3. Otherwise open `GET /interactions/{id}?stream=true` and pipe the
5317
+ * SSE events through `buildGoogleInteractionsStreamTransform` so the
5318
+ * consumer receives text deltas / thinking summaries / tool events as
5319
+ * they happen instead of all at once at the end.
5320
+ *
5321
+ * The SSE connection can drop while the agent idles between events
5322
+ * (`UND_ERR_BODY_TIMEOUT`); `streamGoogleInteractionEvents` handles the
5323
+ * reconnect-with-`last_event_id` loop transparently.
5324
+ */
5325
+ async doStreamBackground({
5326
+ args,
5327
+ warnings,
5328
+ url,
5329
+ mergedHeaders,
5330
+ options,
5331
+ pollingTimeoutMs
5332
+ }) {
5333
+ var _a, _b;
5334
+ const postResult = await postJsonToApi2({
5335
+ url,
5336
+ headers: mergedHeaders,
5337
+ body: args,
5338
+ failedResponseHandler: googleFailedResponseHandler,
5339
+ successfulResponseHandler: createJsonResponseHandler3(
5340
+ googleInteractionsResponseSchema
5341
+ ),
5342
+ abortSignal: options.abortSignal,
5343
+ fetch: this.config.fetch
5344
+ });
5345
+ const { responseHeaders: postHeaders, value: postResponse } = postResult;
5346
+ const interactionId = postResponse.id;
5347
+ if (interactionId == null || interactionId.length === 0) {
5348
+ throw new Error(
5349
+ "google.interactions: background POST response did not include an interaction id; cannot stream the result."
5350
+ );
5351
+ }
5352
+ const headerServiceTier = postHeaders == null ? void 0 : postHeaders["x-gemini-service-tier"];
5353
+ if (isTerminalStatus(postResponse.status)) {
5354
+ const synthesized = synthesizeGoogleInteractionsAgentStream({
5355
+ response: postResponse,
5356
+ warnings,
5357
+ generateId: (_a = this.config.generateId) != null ? _a : defaultGenerateId,
5358
+ includeRawChunks: options.includeRawChunks,
5359
+ headerServiceTier
5360
+ });
5361
+ return {
5362
+ stream: synthesized,
5363
+ request: { body: args },
5364
+ response: { headers: postHeaders }
5365
+ };
5366
+ }
5367
+ void pollingTimeoutMs;
5368
+ const events = streamGoogleInteractionEvents({
5369
+ baseURL: this.config.baseURL,
5370
+ interactionId,
5371
+ headers: mergedHeaders,
5372
+ fetch: this.config.fetch,
5373
+ abortSignal: options.abortSignal
5374
+ });
5375
+ const transform = buildGoogleInteractionsStreamTransform({
5376
+ warnings,
5377
+ generateId: (_b = this.config.generateId) != null ? _b : defaultGenerateId,
5378
+ includeRawChunks: options.includeRawChunks,
5379
+ serviceTier: headerServiceTier
5380
+ });
5381
+ return {
5382
+ stream: events.pipeThrough(transform),
5383
+ request: { body: args },
5384
+ response: { headers: postHeaders }
5385
+ };
5386
+ }
5387
+ };
5388
+ var INTERACTIONS_API_REVISION_HEADER = {
5389
+ "Api-Revision": "2026-05-20"
5390
+ };
5391
+ function pruneUndefined(obj) {
5392
+ const result = {};
5393
+ for (const [key, value] of Object.entries(obj)) {
5394
+ if (value === void 0) continue;
5395
+ result[key] = value;
5396
+ }
5397
+ return result;
5398
+ }
2459
5399
  export {
2460
5400
  GoogleGenerativeAILanguageModel,
5401
+ GoogleInteractionsLanguageModel,
2461
5402
  getGroundingMetadataSchema,
2462
5403
  getUrlContextMetadataSchema,
2463
5404
  googleTools