@ai-sdk/google 3.0.66 → 3.0.68
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.
- package/CHANGELOG.md +13 -0
- package/dist/index.d.mts +90 -1
- package/dist/index.d.ts +90 -1
- package/dist/index.js +2383 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2353 -1
- package/dist/index.mjs.map +1 -1
- package/docs/15-google-generative-ai.mdx +396 -0
- package/package.json +2 -2
- package/src/google-provider.ts +34 -0
- package/src/index.ts +6 -0
- package/src/interactions/build-google-interactions-stream-transform.ts +711 -0
- package/src/interactions/convert-google-interactions-usage.ts +47 -0
- package/src/interactions/convert-to-google-interactions-input.ts +630 -0
- package/src/interactions/extract-google-interactions-sources.ts +245 -0
- package/src/interactions/google-interactions-agent.ts +16 -0
- package/src/interactions/google-interactions-api.ts +466 -0
- package/src/interactions/google-interactions-language-model-options.ts +136 -0
- package/src/interactions/google-interactions-language-model.ts +609 -0
- package/src/interactions/google-interactions-prompt.ts +457 -0
- package/src/interactions/google-interactions-provider-metadata.ts +23 -0
- package/src/interactions/map-google-interactions-finish-reason.ts +33 -0
- package/src/interactions/parse-google-interactions-outputs.ts +257 -0
- package/src/interactions/poll-google-interactions.ts +110 -0
- package/src/interactions/prepare-google-interactions-tools.ts +245 -0
- package/src/interactions/synthesize-google-interactions-agent-stream.ts +185 -0
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "@ai-sdk/provider-utils";
|
|
8
8
|
|
|
9
9
|
// src/version.ts
|
|
10
|
-
var VERSION = true ? "3.0.
|
|
10
|
+
var VERSION = true ? "3.0.68" : "0.0.0-test";
|
|
11
11
|
|
|
12
12
|
// src/google-generative-ai-embedding-model.ts
|
|
13
13
|
import {
|
|
@@ -3049,6 +3049,2344 @@ var googleVideoModelOptionsSchema = lazySchema12(
|
|
|
3049
3049
|
)
|
|
3050
3050
|
);
|
|
3051
3051
|
|
|
3052
|
+
// src/interactions/google-interactions-language-model.ts
|
|
3053
|
+
import {
|
|
3054
|
+
combineHeaders as combineHeaders5,
|
|
3055
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
3056
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
3057
|
+
generateId as defaultGenerateId2,
|
|
3058
|
+
parseProviderOptions as parseProviderOptions5,
|
|
3059
|
+
postJsonToApi as postJsonToApi5,
|
|
3060
|
+
resolve as resolve5
|
|
3061
|
+
} from "@ai-sdk/provider-utils";
|
|
3062
|
+
|
|
3063
|
+
// src/interactions/convert-google-interactions-usage.ts
|
|
3064
|
+
function convertGoogleInteractionsUsage(usage) {
|
|
3065
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
3066
|
+
if (usage == null) {
|
|
3067
|
+
return {
|
|
3068
|
+
inputTokens: {
|
|
3069
|
+
total: void 0,
|
|
3070
|
+
noCache: void 0,
|
|
3071
|
+
cacheRead: void 0,
|
|
3072
|
+
cacheWrite: void 0
|
|
3073
|
+
},
|
|
3074
|
+
outputTokens: {
|
|
3075
|
+
total: void 0,
|
|
3076
|
+
text: void 0,
|
|
3077
|
+
reasoning: void 0
|
|
3078
|
+
},
|
|
3079
|
+
raw: void 0
|
|
3080
|
+
};
|
|
3081
|
+
}
|
|
3082
|
+
const totalInput = (_a = usage.total_input_tokens) != null ? _a : 0;
|
|
3083
|
+
const totalOutput = (_b = usage.total_output_tokens) != null ? _b : 0;
|
|
3084
|
+
const totalThought = (_c = usage.total_thought_tokens) != null ? _c : 0;
|
|
3085
|
+
const totalCached = (_d = usage.total_cached_tokens) != null ? _d : 0;
|
|
3086
|
+
return {
|
|
3087
|
+
inputTokens: {
|
|
3088
|
+
total: (_e = usage.total_input_tokens) != null ? _e : void 0,
|
|
3089
|
+
noCache: usage.total_input_tokens == null ? void 0 : totalInput - totalCached,
|
|
3090
|
+
cacheRead: (_f = usage.total_cached_tokens) != null ? _f : void 0,
|
|
3091
|
+
cacheWrite: void 0
|
|
3092
|
+
},
|
|
3093
|
+
outputTokens: {
|
|
3094
|
+
total: usage.total_output_tokens == null && usage.total_thought_tokens == null ? void 0 : totalOutput + totalThought,
|
|
3095
|
+
text: (_g = usage.total_output_tokens) != null ? _g : void 0,
|
|
3096
|
+
reasoning: (_h = usage.total_thought_tokens) != null ? _h : void 0
|
|
3097
|
+
},
|
|
3098
|
+
raw: usage
|
|
3099
|
+
};
|
|
3100
|
+
}
|
|
3101
|
+
|
|
3102
|
+
// src/interactions/extract-google-interactions-sources.ts
|
|
3103
|
+
var KNOWN_DOC_EXTENSIONS = {
|
|
3104
|
+
pdf: "application/pdf",
|
|
3105
|
+
txt: "text/plain",
|
|
3106
|
+
md: "text/markdown",
|
|
3107
|
+
markdown: "text/markdown",
|
|
3108
|
+
doc: "application/msword",
|
|
3109
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
3110
|
+
};
|
|
3111
|
+
function inferDocMediaType(uriOrName) {
|
|
3112
|
+
const lower = uriOrName.toLowerCase();
|
|
3113
|
+
for (const [ext, media] of Object.entries(KNOWN_DOC_EXTENSIONS)) {
|
|
3114
|
+
if (lower.endsWith(`.${ext}`)) return media;
|
|
3115
|
+
}
|
|
3116
|
+
return "application/octet-stream";
|
|
3117
|
+
}
|
|
3118
|
+
function basename(uriOrName) {
|
|
3119
|
+
const parts = uriOrName.split("/");
|
|
3120
|
+
const last = parts[parts.length - 1];
|
|
3121
|
+
return last && last.length > 0 ? last : void 0;
|
|
3122
|
+
}
|
|
3123
|
+
function annotationToSource({
|
|
3124
|
+
annotation,
|
|
3125
|
+
generateId: generateId3
|
|
3126
|
+
}) {
|
|
3127
|
+
var _a, _b, _c, _d, _e;
|
|
3128
|
+
switch (annotation.type) {
|
|
3129
|
+
case "url_citation": {
|
|
3130
|
+
const a = annotation;
|
|
3131
|
+
if (a.url == null || a.url.length === 0) return void 0;
|
|
3132
|
+
return {
|
|
3133
|
+
type: "source",
|
|
3134
|
+
sourceType: "url",
|
|
3135
|
+
id: generateId3(),
|
|
3136
|
+
url: a.url,
|
|
3137
|
+
...a.title != null ? { title: a.title } : {}
|
|
3138
|
+
};
|
|
3139
|
+
}
|
|
3140
|
+
case "file_citation": {
|
|
3141
|
+
const a = annotation;
|
|
3142
|
+
const uri = (_b = (_a = a.document_uri) != null ? _a : a.source) != null ? _b : a.file_name;
|
|
3143
|
+
if (uri == null || uri.length === 0) return void 0;
|
|
3144
|
+
if (uri.startsWith("http://") || uri.startsWith("https://")) {
|
|
3145
|
+
return {
|
|
3146
|
+
type: "source",
|
|
3147
|
+
sourceType: "url",
|
|
3148
|
+
id: generateId3(),
|
|
3149
|
+
url: uri,
|
|
3150
|
+
...a.file_name != null ? { title: a.file_name } : {}
|
|
3151
|
+
};
|
|
3152
|
+
}
|
|
3153
|
+
const filename = (_c = a.file_name) != null ? _c : basename(uri);
|
|
3154
|
+
const mediaType = inferDocMediaType(uri);
|
|
3155
|
+
return {
|
|
3156
|
+
type: "source",
|
|
3157
|
+
sourceType: "document",
|
|
3158
|
+
id: generateId3(),
|
|
3159
|
+
mediaType,
|
|
3160
|
+
title: (_e = (_d = a.file_name) != null ? _d : filename) != null ? _e : uri,
|
|
3161
|
+
...filename != null ? { filename } : {}
|
|
3162
|
+
};
|
|
3163
|
+
}
|
|
3164
|
+
case "place_citation": {
|
|
3165
|
+
const a = annotation;
|
|
3166
|
+
if (a.url == null || a.url.length === 0) return void 0;
|
|
3167
|
+
return {
|
|
3168
|
+
type: "source",
|
|
3169
|
+
sourceType: "url",
|
|
3170
|
+
id: generateId3(),
|
|
3171
|
+
url: a.url,
|
|
3172
|
+
...a.name != null ? { title: a.name } : {}
|
|
3173
|
+
};
|
|
3174
|
+
}
|
|
3175
|
+
default:
|
|
3176
|
+
return void 0;
|
|
3177
|
+
}
|
|
3178
|
+
}
|
|
3179
|
+
function builtinToolResultToSources({
|
|
3180
|
+
block,
|
|
3181
|
+
generateId: generateId3
|
|
3182
|
+
}) {
|
|
3183
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
3184
|
+
const sources = [];
|
|
3185
|
+
switch (block.type) {
|
|
3186
|
+
case "url_context_result": {
|
|
3187
|
+
const result = (_a = block.result) != null ? _a : [];
|
|
3188
|
+
for (const entry of result) {
|
|
3189
|
+
if ((entry == null ? void 0 : entry.url) == null || entry.url.length === 0) continue;
|
|
3190
|
+
if (entry.status != null && entry.status !== "success") continue;
|
|
3191
|
+
sources.push({
|
|
3192
|
+
type: "source",
|
|
3193
|
+
sourceType: "url",
|
|
3194
|
+
id: generateId3(),
|
|
3195
|
+
url: entry.url
|
|
3196
|
+
});
|
|
3197
|
+
}
|
|
3198
|
+
break;
|
|
3199
|
+
}
|
|
3200
|
+
case "google_search_result": {
|
|
3201
|
+
const result = (_b = block.result) != null ? _b : [];
|
|
3202
|
+
for (const entry of result) {
|
|
3203
|
+
const url = entry == null ? void 0 : entry.url;
|
|
3204
|
+
if (url == null || url.length === 0) continue;
|
|
3205
|
+
sources.push({
|
|
3206
|
+
type: "source",
|
|
3207
|
+
sourceType: "url",
|
|
3208
|
+
id: generateId3(),
|
|
3209
|
+
url,
|
|
3210
|
+
...entry.title != null ? { title: entry.title } : {}
|
|
3211
|
+
});
|
|
3212
|
+
}
|
|
3213
|
+
break;
|
|
3214
|
+
}
|
|
3215
|
+
case "google_maps_result": {
|
|
3216
|
+
const result = (_c = block.result) != null ? _c : [];
|
|
3217
|
+
for (const entry of result) {
|
|
3218
|
+
for (const place of (_d = entry.places) != null ? _d : []) {
|
|
3219
|
+
if (place.url == null || place.url.length === 0) continue;
|
|
3220
|
+
sources.push({
|
|
3221
|
+
type: "source",
|
|
3222
|
+
sourceType: "url",
|
|
3223
|
+
id: generateId3(),
|
|
3224
|
+
url: place.url,
|
|
3225
|
+
...place.name != null ? { title: place.name } : {}
|
|
3226
|
+
});
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
break;
|
|
3230
|
+
}
|
|
3231
|
+
case "file_search_result": {
|
|
3232
|
+
const result = (_e = block.result) != null ? _e : [];
|
|
3233
|
+
for (const raw of result) {
|
|
3234
|
+
if (raw == null || typeof raw !== "object") continue;
|
|
3235
|
+
const entry = raw;
|
|
3236
|
+
const uri = (_g = (_f = entry.document_uri) != null ? _f : entry.source) != null ? _g : entry.file_name;
|
|
3237
|
+
if (uri == null || uri.length === 0) continue;
|
|
3238
|
+
if (uri.startsWith("http://") || uri.startsWith("https://")) {
|
|
3239
|
+
sources.push({
|
|
3240
|
+
type: "source",
|
|
3241
|
+
sourceType: "url",
|
|
3242
|
+
id: generateId3(),
|
|
3243
|
+
url: uri,
|
|
3244
|
+
...entry.title != null ? { title: entry.title } : {}
|
|
3245
|
+
});
|
|
3246
|
+
continue;
|
|
3247
|
+
}
|
|
3248
|
+
const filename = (_h = entry.file_name) != null ? _h : basename(uri);
|
|
3249
|
+
const mediaType = inferDocMediaType(uri);
|
|
3250
|
+
sources.push({
|
|
3251
|
+
type: "source",
|
|
3252
|
+
sourceType: "document",
|
|
3253
|
+
id: generateId3(),
|
|
3254
|
+
mediaType,
|
|
3255
|
+
title: (_k = (_j = (_i = entry.title) != null ? _i : entry.file_name) != null ? _j : filename) != null ? _k : uri,
|
|
3256
|
+
...filename != null ? { filename } : {}
|
|
3257
|
+
});
|
|
3258
|
+
}
|
|
3259
|
+
break;
|
|
3260
|
+
}
|
|
3261
|
+
default:
|
|
3262
|
+
break;
|
|
3263
|
+
}
|
|
3264
|
+
return sources;
|
|
3265
|
+
}
|
|
3266
|
+
function annotationsToSources({
|
|
3267
|
+
annotations,
|
|
3268
|
+
generateId: generateId3
|
|
3269
|
+
}) {
|
|
3270
|
+
var _a;
|
|
3271
|
+
if (annotations == null) return [];
|
|
3272
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3273
|
+
const sources = [];
|
|
3274
|
+
for (const annotation of annotations) {
|
|
3275
|
+
const source = annotationToSource({ annotation, generateId: generateId3 });
|
|
3276
|
+
if (source == null) continue;
|
|
3277
|
+
const key = source.sourceType === "url" ? `url:${source.url}` : `doc:${(_a = source.filename) != null ? _a : source.title}`;
|
|
3278
|
+
if (seen.has(key)) continue;
|
|
3279
|
+
seen.add(key);
|
|
3280
|
+
sources.push(source);
|
|
3281
|
+
}
|
|
3282
|
+
return sources;
|
|
3283
|
+
}
|
|
3284
|
+
|
|
3285
|
+
// src/interactions/map-google-interactions-finish-reason.ts
|
|
3286
|
+
function mapGoogleInteractionsFinishReason({
|
|
3287
|
+
status,
|
|
3288
|
+
hasFunctionCall
|
|
3289
|
+
}) {
|
|
3290
|
+
switch (status) {
|
|
3291
|
+
case "completed":
|
|
3292
|
+
return hasFunctionCall ? "tool-calls" : "stop";
|
|
3293
|
+
case "requires_action":
|
|
3294
|
+
return "tool-calls";
|
|
3295
|
+
case "failed":
|
|
3296
|
+
return "error";
|
|
3297
|
+
case "incomplete":
|
|
3298
|
+
return "length";
|
|
3299
|
+
case "cancelled":
|
|
3300
|
+
return "other";
|
|
3301
|
+
case "in_progress":
|
|
3302
|
+
default:
|
|
3303
|
+
return "other";
|
|
3304
|
+
}
|
|
3305
|
+
}
|
|
3306
|
+
|
|
3307
|
+
// src/interactions/build-google-interactions-stream-transform.ts
|
|
3308
|
+
var BUILTIN_TOOL_CALL_TYPES = /* @__PURE__ */ new Set([
|
|
3309
|
+
"google_search_call",
|
|
3310
|
+
"code_execution_call",
|
|
3311
|
+
"url_context_call",
|
|
3312
|
+
"file_search_call",
|
|
3313
|
+
"google_maps_call",
|
|
3314
|
+
"mcp_server_tool_call"
|
|
3315
|
+
]);
|
|
3316
|
+
var BUILTIN_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
|
|
3317
|
+
"google_search_result",
|
|
3318
|
+
"code_execution_result",
|
|
3319
|
+
"url_context_result",
|
|
3320
|
+
"file_search_result",
|
|
3321
|
+
"google_maps_result",
|
|
3322
|
+
"mcp_server_tool_result"
|
|
3323
|
+
]);
|
|
3324
|
+
function builtinToolNameFromCallType(type) {
|
|
3325
|
+
return type.replace(/_call$/, "");
|
|
3326
|
+
}
|
|
3327
|
+
function builtinToolNameFromResultType(type) {
|
|
3328
|
+
return type.replace(/_result$/, "");
|
|
3329
|
+
}
|
|
3330
|
+
function buildGoogleInteractionsStreamTransform({
|
|
3331
|
+
warnings,
|
|
3332
|
+
generateId: generateId3,
|
|
3333
|
+
includeRawChunks,
|
|
3334
|
+
serviceTier: headerServiceTier
|
|
3335
|
+
}) {
|
|
3336
|
+
let interactionId;
|
|
3337
|
+
let usage;
|
|
3338
|
+
let serviceTier = headerServiceTier;
|
|
3339
|
+
let finishStatus;
|
|
3340
|
+
let hasFunctionCall = false;
|
|
3341
|
+
const openBlocks = /* @__PURE__ */ new Map();
|
|
3342
|
+
const emittedSourceKeys = /* @__PURE__ */ new Set();
|
|
3343
|
+
function sourceKey(source) {
|
|
3344
|
+
var _a;
|
|
3345
|
+
return source.sourceType === "url" ? `url:${source.url}` : `doc:${(_a = source.filename) != null ? _a : source.title}`;
|
|
3346
|
+
}
|
|
3347
|
+
return new TransformStream({
|
|
3348
|
+
start(controller) {
|
|
3349
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
3350
|
+
},
|
|
3351
|
+
transform(chunk, controller) {
|
|
3352
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
3353
|
+
if (includeRawChunks) {
|
|
3354
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
3355
|
+
}
|
|
3356
|
+
if (!chunk.success) {
|
|
3357
|
+
finishStatus = "failed";
|
|
3358
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
3359
|
+
return;
|
|
3360
|
+
}
|
|
3361
|
+
const value = chunk.value;
|
|
3362
|
+
const eventType = value.event_type;
|
|
3363
|
+
switch (eventType) {
|
|
3364
|
+
case "interaction.start": {
|
|
3365
|
+
const event = value;
|
|
3366
|
+
const interaction = event.interaction;
|
|
3367
|
+
interactionId = (interaction == null ? void 0 : interaction.id) != null && interaction.id.length > 0 ? interaction.id : void 0;
|
|
3368
|
+
const created = interaction == null ? void 0 : interaction.created;
|
|
3369
|
+
let timestamp;
|
|
3370
|
+
if (typeof created === "string") {
|
|
3371
|
+
const parsed = new Date(created);
|
|
3372
|
+
if (!Number.isNaN(parsed.getTime())) {
|
|
3373
|
+
timestamp = parsed;
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
controller.enqueue({
|
|
3377
|
+
type: "response-metadata",
|
|
3378
|
+
...interactionId != null ? { id: interactionId } : {},
|
|
3379
|
+
modelId: interaction == null ? void 0 : interaction.model,
|
|
3380
|
+
...timestamp ? { timestamp } : {}
|
|
3381
|
+
});
|
|
3382
|
+
break;
|
|
3383
|
+
}
|
|
3384
|
+
case "content.start": {
|
|
3385
|
+
const event = value;
|
|
3386
|
+
const block = event.content;
|
|
3387
|
+
const index = event.index;
|
|
3388
|
+
const blockId = `${interactionId != null ? interactionId : "interaction"}:${index}`;
|
|
3389
|
+
if ((block == null ? void 0 : block.type) === "text") {
|
|
3390
|
+
openBlocks.set(index, {
|
|
3391
|
+
kind: "text",
|
|
3392
|
+
id: blockId,
|
|
3393
|
+
emittedSourceKeys: /* @__PURE__ */ new Set()
|
|
3394
|
+
});
|
|
3395
|
+
controller.enqueue({ type: "text-start", id: blockId });
|
|
3396
|
+
const initialSources = annotationsToSources({
|
|
3397
|
+
annotations: block.annotations,
|
|
3398
|
+
generateId: generateId3
|
|
3399
|
+
});
|
|
3400
|
+
for (const source of initialSources) {
|
|
3401
|
+
const key = sourceKey(source);
|
|
3402
|
+
if (emittedSourceKeys.has(key)) continue;
|
|
3403
|
+
emittedSourceKeys.add(key);
|
|
3404
|
+
controller.enqueue(source);
|
|
3405
|
+
}
|
|
3406
|
+
} else if ((block == null ? void 0 : block.type) === "image") {
|
|
3407
|
+
const img = block;
|
|
3408
|
+
openBlocks.set(index, {
|
|
3409
|
+
kind: "image",
|
|
3410
|
+
id: blockId,
|
|
3411
|
+
...img.data != null ? { data: img.data } : {},
|
|
3412
|
+
...img.mime_type != null ? { mimeType: img.mime_type } : {},
|
|
3413
|
+
...img.uri != null ? { uri: img.uri } : {}
|
|
3414
|
+
});
|
|
3415
|
+
} else if ((block == null ? void 0 : block.type) === "thought") {
|
|
3416
|
+
const signature = block.signature;
|
|
3417
|
+
openBlocks.set(index, {
|
|
3418
|
+
kind: "reasoning",
|
|
3419
|
+
id: blockId,
|
|
3420
|
+
...signature != null ? { signature } : {}
|
|
3421
|
+
});
|
|
3422
|
+
controller.enqueue({ type: "reasoning-start", id: blockId });
|
|
3423
|
+
} else if ((block == null ? void 0 : block.type) === "function_call") {
|
|
3424
|
+
const fc = block;
|
|
3425
|
+
const toolCallId = (_a = fc.id) != null ? _a : blockId;
|
|
3426
|
+
hasFunctionCall = true;
|
|
3427
|
+
const state = {
|
|
3428
|
+
kind: "function_call",
|
|
3429
|
+
id: blockId,
|
|
3430
|
+
toolCallId,
|
|
3431
|
+
toolName: fc.name,
|
|
3432
|
+
arguments: (_b = fc.arguments) != null ? _b : {},
|
|
3433
|
+
...fc.signature != null ? { signature: fc.signature } : {},
|
|
3434
|
+
startEmitted: false
|
|
3435
|
+
};
|
|
3436
|
+
openBlocks.set(index, state);
|
|
3437
|
+
if (state.toolName != null) {
|
|
3438
|
+
controller.enqueue({
|
|
3439
|
+
type: "tool-input-start",
|
|
3440
|
+
id: toolCallId,
|
|
3441
|
+
toolName: state.toolName
|
|
3442
|
+
});
|
|
3443
|
+
state.startEmitted = true;
|
|
3444
|
+
}
|
|
3445
|
+
} else if ((block == null ? void 0 : block.type) != null && BUILTIN_TOOL_CALL_TYPES.has(block.type)) {
|
|
3446
|
+
const toolName = block.type === "mcp_server_tool_call" ? (_c = block.name) != null ? _c : "mcp_server_tool" : builtinToolNameFromCallType(block.type);
|
|
3447
|
+
const toolCallId = (_d = block.id) != null ? _d : blockId;
|
|
3448
|
+
const state = {
|
|
3449
|
+
kind: "builtin_tool_call",
|
|
3450
|
+
id: blockId,
|
|
3451
|
+
blockType: block.type,
|
|
3452
|
+
toolCallId,
|
|
3453
|
+
toolName,
|
|
3454
|
+
arguments: (_e = block.arguments) != null ? _e : {},
|
|
3455
|
+
callEmitted: false
|
|
3456
|
+
};
|
|
3457
|
+
openBlocks.set(index, state);
|
|
3458
|
+
} else if ((block == null ? void 0 : block.type) != null && BUILTIN_TOOL_RESULT_TYPES.has(block.type)) {
|
|
3459
|
+
const toolName = block.type === "mcp_server_tool_result" ? (_f = block.name) != null ? _f : "mcp_server_tool" : builtinToolNameFromResultType(block.type);
|
|
3460
|
+
const callId = (_g = block.call_id) != null ? _g : blockId;
|
|
3461
|
+
const state = {
|
|
3462
|
+
kind: "builtin_tool_result",
|
|
3463
|
+
id: blockId,
|
|
3464
|
+
blockType: block.type,
|
|
3465
|
+
callId,
|
|
3466
|
+
toolName,
|
|
3467
|
+
result: (_h = block.result) != null ? _h : null,
|
|
3468
|
+
...block.is_error != null ? { isError: block.is_error } : {},
|
|
3469
|
+
resultEmitted: false
|
|
3470
|
+
};
|
|
3471
|
+
openBlocks.set(index, state);
|
|
3472
|
+
} else {
|
|
3473
|
+
openBlocks.set(index, { kind: "unknown", id: blockId });
|
|
3474
|
+
}
|
|
3475
|
+
break;
|
|
3476
|
+
}
|
|
3477
|
+
case "content.delta": {
|
|
3478
|
+
const event = value;
|
|
3479
|
+
const open = openBlocks.get(event.index);
|
|
3480
|
+
if (open == null) break;
|
|
3481
|
+
const delta = event.delta;
|
|
3482
|
+
if (open.kind === "text" && (delta == null ? void 0 : delta.type) === "text") {
|
|
3483
|
+
const text = (_i = delta.text) != null ? _i : "";
|
|
3484
|
+
if (text.length > 0) {
|
|
3485
|
+
controller.enqueue({
|
|
3486
|
+
type: "text-delta",
|
|
3487
|
+
id: open.id,
|
|
3488
|
+
delta: text
|
|
3489
|
+
});
|
|
3490
|
+
}
|
|
3491
|
+
} else if (open.kind === "text" && (delta == null ? void 0 : delta.type) === "text_annotation") {
|
|
3492
|
+
const sources = annotationsToSources({
|
|
3493
|
+
annotations: delta.annotations,
|
|
3494
|
+
generateId: generateId3
|
|
3495
|
+
});
|
|
3496
|
+
for (const source of sources) {
|
|
3497
|
+
const key = sourceKey(source);
|
|
3498
|
+
if (emittedSourceKeys.has(key)) continue;
|
|
3499
|
+
emittedSourceKeys.add(key);
|
|
3500
|
+
open.emittedSourceKeys.add(key);
|
|
3501
|
+
controller.enqueue(source);
|
|
3502
|
+
}
|
|
3503
|
+
} else if (open.kind === "image" && (delta == null ? void 0 : delta.type) === "image") {
|
|
3504
|
+
if (delta.data != null) open.data = delta.data;
|
|
3505
|
+
if (delta.mime_type != null) open.mimeType = delta.mime_type;
|
|
3506
|
+
if (delta.uri != null) open.uri = delta.uri;
|
|
3507
|
+
} else if (open.kind === "reasoning") {
|
|
3508
|
+
if ((delta == null ? void 0 : delta.type) === "thought_summary") {
|
|
3509
|
+
const item = delta.content;
|
|
3510
|
+
if ((item == null ? void 0 : item.type) === "text" && typeof item.text === "string") {
|
|
3511
|
+
controller.enqueue({
|
|
3512
|
+
type: "reasoning-delta",
|
|
3513
|
+
id: open.id,
|
|
3514
|
+
delta: item.text
|
|
3515
|
+
});
|
|
3516
|
+
}
|
|
3517
|
+
} else if ((delta == null ? void 0 : delta.type) === "thought_signature") {
|
|
3518
|
+
const signature = delta.signature;
|
|
3519
|
+
if (signature != null) {
|
|
3520
|
+
open.signature = signature;
|
|
3521
|
+
}
|
|
3522
|
+
}
|
|
3523
|
+
} else if (open.kind === "function_call" && (delta == null ? void 0 : delta.type) === "function_call") {
|
|
3524
|
+
if (delta.id != null) {
|
|
3525
|
+
open.toolCallId = delta.id;
|
|
3526
|
+
}
|
|
3527
|
+
if (delta.name != null) {
|
|
3528
|
+
open.toolName = delta.name;
|
|
3529
|
+
}
|
|
3530
|
+
if (delta.arguments != null) {
|
|
3531
|
+
open.arguments = delta.arguments;
|
|
3532
|
+
}
|
|
3533
|
+
if (delta.signature != null) {
|
|
3534
|
+
open.signature = delta.signature;
|
|
3535
|
+
}
|
|
3536
|
+
if (!open.startEmitted && open.toolName != null) {
|
|
3537
|
+
controller.enqueue({
|
|
3538
|
+
type: "tool-input-start",
|
|
3539
|
+
id: open.toolCallId,
|
|
3540
|
+
toolName: open.toolName
|
|
3541
|
+
});
|
|
3542
|
+
open.startEmitted = true;
|
|
3543
|
+
}
|
|
3544
|
+
hasFunctionCall = true;
|
|
3545
|
+
} else if (open.kind === "builtin_tool_call" && (delta == null ? void 0 : delta.type) === open.blockType) {
|
|
3546
|
+
if (delta.id != null) open.toolCallId = delta.id;
|
|
3547
|
+
if (delta.arguments != null) open.arguments = delta.arguments;
|
|
3548
|
+
if (delta.name != null && open.blockType === "mcp_server_tool_call") {
|
|
3549
|
+
open.toolName = delta.name;
|
|
3550
|
+
}
|
|
3551
|
+
} else if (open.kind === "builtin_tool_result" && (delta == null ? void 0 : delta.type) === open.blockType) {
|
|
3552
|
+
if (delta.call_id != null) open.callId = delta.call_id;
|
|
3553
|
+
if (delta.result !== void 0) open.result = delta.result;
|
|
3554
|
+
if (delta.is_error != null) open.isError = delta.is_error;
|
|
3555
|
+
if (delta.name != null && open.blockType === "mcp_server_tool_result") {
|
|
3556
|
+
open.toolName = delta.name;
|
|
3557
|
+
}
|
|
3558
|
+
}
|
|
3559
|
+
break;
|
|
3560
|
+
}
|
|
3561
|
+
case "content.stop": {
|
|
3562
|
+
const event = value;
|
|
3563
|
+
const open = openBlocks.get(event.index);
|
|
3564
|
+
if (open == null) break;
|
|
3565
|
+
if (open.kind === "text") {
|
|
3566
|
+
const textProviderMetadata = interactionId != null ? { google: { interactionId } } : void 0;
|
|
3567
|
+
controller.enqueue({
|
|
3568
|
+
type: "text-end",
|
|
3569
|
+
id: open.id,
|
|
3570
|
+
...textProviderMetadata ? { providerMetadata: textProviderMetadata } : {}
|
|
3571
|
+
});
|
|
3572
|
+
} else if (open.kind === "reasoning") {
|
|
3573
|
+
const google2 = {};
|
|
3574
|
+
if (open.signature != null) google2.signature = open.signature;
|
|
3575
|
+
if (interactionId != null) google2.interactionId = interactionId;
|
|
3576
|
+
const providerMetadata = Object.keys(google2).length > 0 ? { google: google2 } : void 0;
|
|
3577
|
+
controller.enqueue({
|
|
3578
|
+
type: "reasoning-end",
|
|
3579
|
+
id: open.id,
|
|
3580
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
3581
|
+
});
|
|
3582
|
+
} else if (open.kind === "image") {
|
|
3583
|
+
const google2 = {};
|
|
3584
|
+
if (interactionId != null) google2.interactionId = interactionId;
|
|
3585
|
+
const providerMetadata = Object.keys(google2).length > 0 ? { google: google2 } : void 0;
|
|
3586
|
+
if (open.data != null && open.data.length > 0) {
|
|
3587
|
+
controller.enqueue({
|
|
3588
|
+
type: "file",
|
|
3589
|
+
mediaType: (_j = open.mimeType) != null ? _j : "image/png",
|
|
3590
|
+
data: open.data,
|
|
3591
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
3592
|
+
});
|
|
3593
|
+
} else if (open.uri != null && open.uri.length > 0) {
|
|
3594
|
+
const uriProviderMetadata = {
|
|
3595
|
+
google: {
|
|
3596
|
+
...interactionId != null ? { interactionId } : {},
|
|
3597
|
+
imageUri: open.uri
|
|
3598
|
+
}
|
|
3599
|
+
};
|
|
3600
|
+
controller.enqueue({
|
|
3601
|
+
type: "file",
|
|
3602
|
+
mediaType: (_k = open.mimeType) != null ? _k : "image/png",
|
|
3603
|
+
data: "",
|
|
3604
|
+
providerMetadata: uriProviderMetadata
|
|
3605
|
+
});
|
|
3606
|
+
}
|
|
3607
|
+
} else if (open.kind === "function_call") {
|
|
3608
|
+
const toolName = (_l = open.toolName) != null ? _l : "unknown";
|
|
3609
|
+
const argsJson = JSON.stringify((_m = open.arguments) != null ? _m : {});
|
|
3610
|
+
if (!open.startEmitted) {
|
|
3611
|
+
controller.enqueue({
|
|
3612
|
+
type: "tool-input-start",
|
|
3613
|
+
id: open.toolCallId,
|
|
3614
|
+
toolName
|
|
3615
|
+
});
|
|
3616
|
+
}
|
|
3617
|
+
controller.enqueue({
|
|
3618
|
+
type: "tool-input-delta",
|
|
3619
|
+
id: open.toolCallId,
|
|
3620
|
+
delta: argsJson
|
|
3621
|
+
});
|
|
3622
|
+
controller.enqueue({
|
|
3623
|
+
type: "tool-input-end",
|
|
3624
|
+
id: open.toolCallId
|
|
3625
|
+
});
|
|
3626
|
+
const google2 = {};
|
|
3627
|
+
if (open.signature != null) google2.signature = open.signature;
|
|
3628
|
+
if (interactionId != null) google2.interactionId = interactionId;
|
|
3629
|
+
const providerMetadata = Object.keys(google2).length > 0 ? { google: google2 } : void 0;
|
|
3630
|
+
controller.enqueue({
|
|
3631
|
+
type: "tool-call",
|
|
3632
|
+
toolCallId: open.toolCallId,
|
|
3633
|
+
toolName,
|
|
3634
|
+
input: argsJson,
|
|
3635
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
3636
|
+
});
|
|
3637
|
+
} else if (open.kind === "builtin_tool_call" && !open.callEmitted) {
|
|
3638
|
+
controller.enqueue({
|
|
3639
|
+
type: "tool-call",
|
|
3640
|
+
toolCallId: open.toolCallId,
|
|
3641
|
+
toolName: open.toolName,
|
|
3642
|
+
input: JSON.stringify((_n = open.arguments) != null ? _n : {}),
|
|
3643
|
+
providerExecuted: true
|
|
3644
|
+
});
|
|
3645
|
+
open.callEmitted = true;
|
|
3646
|
+
} else if (open.kind === "builtin_tool_result" && !open.resultEmitted) {
|
|
3647
|
+
controller.enqueue({
|
|
3648
|
+
type: "tool-result",
|
|
3649
|
+
toolCallId: open.callId,
|
|
3650
|
+
toolName: open.toolName,
|
|
3651
|
+
result: (_o = open.result) != null ? _o : null
|
|
3652
|
+
});
|
|
3653
|
+
open.resultEmitted = true;
|
|
3654
|
+
const sources = builtinToolResultToSources({
|
|
3655
|
+
block: {
|
|
3656
|
+
type: open.blockType,
|
|
3657
|
+
call_id: open.callId,
|
|
3658
|
+
result: open.result
|
|
3659
|
+
},
|
|
3660
|
+
generateId: generateId3
|
|
3661
|
+
});
|
|
3662
|
+
for (const source of sources) {
|
|
3663
|
+
const key = sourceKey(source);
|
|
3664
|
+
if (emittedSourceKeys.has(key)) continue;
|
|
3665
|
+
emittedSourceKeys.add(key);
|
|
3666
|
+
controller.enqueue(source);
|
|
3667
|
+
}
|
|
3668
|
+
}
|
|
3669
|
+
openBlocks.delete(event.index);
|
|
3670
|
+
break;
|
|
3671
|
+
}
|
|
3672
|
+
case "interaction.status_update": {
|
|
3673
|
+
const event = value;
|
|
3674
|
+
finishStatus = event.status;
|
|
3675
|
+
break;
|
|
3676
|
+
}
|
|
3677
|
+
case "interaction.complete": {
|
|
3678
|
+
const event = value;
|
|
3679
|
+
const interaction = event.interaction;
|
|
3680
|
+
if ((interaction == null ? void 0 : interaction.id) != null && interaction.id.length > 0) {
|
|
3681
|
+
interactionId = interaction.id;
|
|
3682
|
+
}
|
|
3683
|
+
if ((interaction == null ? void 0 : interaction.status) != null) {
|
|
3684
|
+
finishStatus = interaction.status;
|
|
3685
|
+
}
|
|
3686
|
+
if ((interaction == null ? void 0 : interaction.usage) != null) {
|
|
3687
|
+
usage = interaction.usage;
|
|
3688
|
+
}
|
|
3689
|
+
if ((interaction == null ? void 0 : interaction.service_tier) != null) {
|
|
3690
|
+
serviceTier = interaction.service_tier;
|
|
3691
|
+
}
|
|
3692
|
+
break;
|
|
3693
|
+
}
|
|
3694
|
+
case "error": {
|
|
3695
|
+
const event = value;
|
|
3696
|
+
finishStatus = "failed";
|
|
3697
|
+
const errorPayload = (_p = event.error) != null ? _p : {
|
|
3698
|
+
message: "Unknown interaction error"
|
|
3699
|
+
};
|
|
3700
|
+
controller.enqueue({ type: "error", error: errorPayload });
|
|
3701
|
+
break;
|
|
3702
|
+
}
|
|
3703
|
+
default:
|
|
3704
|
+
break;
|
|
3705
|
+
}
|
|
3706
|
+
},
|
|
3707
|
+
flush(controller) {
|
|
3708
|
+
const finishReason = {
|
|
3709
|
+
unified: mapGoogleInteractionsFinishReason({
|
|
3710
|
+
status: finishStatus,
|
|
3711
|
+
hasFunctionCall
|
|
3712
|
+
}),
|
|
3713
|
+
raw: finishStatus
|
|
3714
|
+
};
|
|
3715
|
+
const providerMetadata = {
|
|
3716
|
+
google: {
|
|
3717
|
+
...interactionId != null ? { interactionId } : {},
|
|
3718
|
+
...serviceTier != null ? { serviceTier } : {}
|
|
3719
|
+
}
|
|
3720
|
+
};
|
|
3721
|
+
controller.enqueue({
|
|
3722
|
+
type: "finish",
|
|
3723
|
+
finishReason,
|
|
3724
|
+
usage: convertGoogleInteractionsUsage(usage),
|
|
3725
|
+
providerMetadata
|
|
3726
|
+
});
|
|
3727
|
+
}
|
|
3728
|
+
});
|
|
3729
|
+
}
|
|
3730
|
+
|
|
3731
|
+
// src/interactions/convert-to-google-interactions-input.ts
|
|
3732
|
+
import { convertToBase64 as convertToBase643 } from "@ai-sdk/provider-utils";
|
|
3733
|
+
function getTopLevelMediaType(mediaType) {
|
|
3734
|
+
const slashIndex = mediaType.indexOf("/");
|
|
3735
|
+
return slashIndex === -1 ? mediaType : mediaType.substring(0, slashIndex);
|
|
3736
|
+
}
|
|
3737
|
+
function isFullMediaType(mediaType) {
|
|
3738
|
+
const slashIndex = mediaType.indexOf("/");
|
|
3739
|
+
if (slashIndex === -1) {
|
|
3740
|
+
return false;
|
|
3741
|
+
}
|
|
3742
|
+
const subtype = mediaType.substring(slashIndex + 1);
|
|
3743
|
+
return subtype.length > 0 && subtype !== "*";
|
|
3744
|
+
}
|
|
3745
|
+
function convertToGoogleInteractionsInput({
|
|
3746
|
+
prompt,
|
|
3747
|
+
previousInteractionId,
|
|
3748
|
+
store,
|
|
3749
|
+
mediaResolution
|
|
3750
|
+
}) {
|
|
3751
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
3752
|
+
const warnings = [];
|
|
3753
|
+
const incoherentCombo = previousInteractionId != null && store === false;
|
|
3754
|
+
const shouldCompact = previousInteractionId != null && store !== false;
|
|
3755
|
+
if (incoherentCombo) {
|
|
3756
|
+
warnings.push({
|
|
3757
|
+
type: "other",
|
|
3758
|
+
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."
|
|
3759
|
+
});
|
|
3760
|
+
}
|
|
3761
|
+
const compactedPrompt = shouldCompact ? compactPromptForPreviousInteraction({
|
|
3762
|
+
prompt,
|
|
3763
|
+
previousInteractionId
|
|
3764
|
+
}) : prompt;
|
|
3765
|
+
const systemTexts = [];
|
|
3766
|
+
const turns = [];
|
|
3767
|
+
for (const message of compactedPrompt) {
|
|
3768
|
+
switch (message.role) {
|
|
3769
|
+
case "system": {
|
|
3770
|
+
systemTexts.push(message.content);
|
|
3771
|
+
break;
|
|
3772
|
+
}
|
|
3773
|
+
case "user": {
|
|
3774
|
+
const content = [];
|
|
3775
|
+
for (const part of message.content) {
|
|
3776
|
+
if (part.type === "text") {
|
|
3777
|
+
const block = {
|
|
3778
|
+
type: "text",
|
|
3779
|
+
text: part.text
|
|
3780
|
+
};
|
|
3781
|
+
content.push(block);
|
|
3782
|
+
} else if (part.type === "file") {
|
|
3783
|
+
const fileBlock = convertFilePartToContent({
|
|
3784
|
+
part,
|
|
3785
|
+
warnings,
|
|
3786
|
+
mediaResolution
|
|
3787
|
+
});
|
|
3788
|
+
if (fileBlock != null) {
|
|
3789
|
+
content.push(fileBlock);
|
|
3790
|
+
}
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
const merged = mergeAdjacentTextContent(content);
|
|
3794
|
+
if (merged.length > 0) {
|
|
3795
|
+
turns.push({ role: "user", content: merged });
|
|
3796
|
+
}
|
|
3797
|
+
break;
|
|
3798
|
+
}
|
|
3799
|
+
case "assistant": {
|
|
3800
|
+
const content = [];
|
|
3801
|
+
for (const part of message.content) {
|
|
3802
|
+
if (part.type === "text") {
|
|
3803
|
+
content.push({ type: "text", text: part.text });
|
|
3804
|
+
} else if (part.type === "reasoning") {
|
|
3805
|
+
const signature = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.signature;
|
|
3806
|
+
content.push({
|
|
3807
|
+
type: "thought",
|
|
3808
|
+
...signature != null ? { signature } : {},
|
|
3809
|
+
summary: part.text.length > 0 ? [{ type: "text", text: part.text }] : void 0
|
|
3810
|
+
});
|
|
3811
|
+
} else if (part.type === "tool-call") {
|
|
3812
|
+
const signature = (_d = (_c = part.providerOptions) == null ? void 0 : _c.google) == null ? void 0 : _d.signature;
|
|
3813
|
+
const args = typeof part.input === "string" ? safeParseToolArgs(part.input) : (_e = part.input) != null ? _e : {};
|
|
3814
|
+
content.push({
|
|
3815
|
+
type: "function_call",
|
|
3816
|
+
id: part.toolCallId,
|
|
3817
|
+
name: part.toolName,
|
|
3818
|
+
arguments: args,
|
|
3819
|
+
...signature != null ? { signature } : {}
|
|
3820
|
+
});
|
|
3821
|
+
} else {
|
|
3822
|
+
warnings.push({
|
|
3823
|
+
type: "other",
|
|
3824
|
+
message: `google.interactions: unsupported assistant content part type "${part.type}"; part dropped.`
|
|
3825
|
+
});
|
|
3826
|
+
}
|
|
3827
|
+
}
|
|
3828
|
+
if (content.length > 0) {
|
|
3829
|
+
turns.push({ role: "model", content });
|
|
3830
|
+
}
|
|
3831
|
+
break;
|
|
3832
|
+
}
|
|
3833
|
+
case "tool": {
|
|
3834
|
+
const content = [];
|
|
3835
|
+
for (const part of message.content) {
|
|
3836
|
+
if (part.type !== "tool-result") {
|
|
3837
|
+
warnings.push({
|
|
3838
|
+
type: "other",
|
|
3839
|
+
message: `google.interactions: unsupported tool message part type "${part.type}"; part dropped.`
|
|
3840
|
+
});
|
|
3841
|
+
continue;
|
|
3842
|
+
}
|
|
3843
|
+
const block = convertToolResultPart({
|
|
3844
|
+
toolCallId: part.toolCallId,
|
|
3845
|
+
toolName: part.toolName,
|
|
3846
|
+
output: part.output,
|
|
3847
|
+
signature: (_g = (_f = part.providerOptions) == null ? void 0 : _f.google) == null ? void 0 : _g.signature,
|
|
3848
|
+
warnings
|
|
3849
|
+
});
|
|
3850
|
+
content.push(block);
|
|
3851
|
+
}
|
|
3852
|
+
if (content.length > 0) {
|
|
3853
|
+
turns.push({ role: "user", content });
|
|
3854
|
+
}
|
|
3855
|
+
break;
|
|
3856
|
+
}
|
|
3857
|
+
}
|
|
3858
|
+
}
|
|
3859
|
+
const systemInstruction = systemTexts.length > 0 ? systemTexts.join("\n\n") : void 0;
|
|
3860
|
+
let input;
|
|
3861
|
+
if (turns.length === 0) {
|
|
3862
|
+
input = "";
|
|
3863
|
+
} else if (turns.length === 1 && turns[0].role === "user" && Array.isArray(turns[0].content)) {
|
|
3864
|
+
input = turns[0].content;
|
|
3865
|
+
} else {
|
|
3866
|
+
input = turns;
|
|
3867
|
+
}
|
|
3868
|
+
return { input, systemInstruction, warnings };
|
|
3869
|
+
}
|
|
3870
|
+
function convertFilePartToContent({
|
|
3871
|
+
part,
|
|
3872
|
+
warnings,
|
|
3873
|
+
mediaResolution
|
|
3874
|
+
}) {
|
|
3875
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
3876
|
+
let kind;
|
|
3877
|
+
switch (topLevel) {
|
|
3878
|
+
case "image":
|
|
3879
|
+
kind = "image";
|
|
3880
|
+
break;
|
|
3881
|
+
case "audio":
|
|
3882
|
+
kind = "audio";
|
|
3883
|
+
break;
|
|
3884
|
+
case "video":
|
|
3885
|
+
kind = "video";
|
|
3886
|
+
break;
|
|
3887
|
+
case "application":
|
|
3888
|
+
kind = "document";
|
|
3889
|
+
break;
|
|
3890
|
+
default:
|
|
3891
|
+
kind = void 0;
|
|
3892
|
+
}
|
|
3893
|
+
if (kind == null) {
|
|
3894
|
+
warnings.push({
|
|
3895
|
+
type: "other",
|
|
3896
|
+
message: `google.interactions: unsupported file media type "${part.mediaType}"; part dropped.`
|
|
3897
|
+
});
|
|
3898
|
+
return void 0;
|
|
3899
|
+
}
|
|
3900
|
+
const resolutionField = mediaResolution != null && (kind === "image" || kind === "video") ? { resolution: mediaResolution } : {};
|
|
3901
|
+
if (part.data instanceof URL) {
|
|
3902
|
+
return {
|
|
3903
|
+
type: kind,
|
|
3904
|
+
uri: part.data.toString(),
|
|
3905
|
+
...isFullMediaType(part.mediaType) ? { mime_type: part.mediaType } : {},
|
|
3906
|
+
...resolutionField
|
|
3907
|
+
};
|
|
3908
|
+
}
|
|
3909
|
+
if (!isFullMediaType(part.mediaType)) {
|
|
3910
|
+
warnings.push({
|
|
3911
|
+
type: "other",
|
|
3912
|
+
message: `google.interactions: inline file data requires a full IANA media type (e.g. "image/png"), got "${part.mediaType}"; part dropped.`
|
|
3913
|
+
});
|
|
3914
|
+
return void 0;
|
|
3915
|
+
}
|
|
3916
|
+
return {
|
|
3917
|
+
type: kind,
|
|
3918
|
+
data: convertToBase643(part.data),
|
|
3919
|
+
mime_type: part.mediaType,
|
|
3920
|
+
...resolutionField
|
|
3921
|
+
};
|
|
3922
|
+
}
|
|
3923
|
+
function compactPromptForPreviousInteraction({
|
|
3924
|
+
prompt,
|
|
3925
|
+
previousInteractionId
|
|
3926
|
+
}) {
|
|
3927
|
+
const out = [];
|
|
3928
|
+
const droppedToolCallIds = /* @__PURE__ */ new Set();
|
|
3929
|
+
for (const message of prompt) {
|
|
3930
|
+
if (message.role === "assistant") {
|
|
3931
|
+
const matchesLinkedInteraction = message.content.some((part) => {
|
|
3932
|
+
var _a, _b;
|
|
3933
|
+
const partInteractionId = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.interactionId;
|
|
3934
|
+
return partInteractionId === previousInteractionId;
|
|
3935
|
+
});
|
|
3936
|
+
if (matchesLinkedInteraction) {
|
|
3937
|
+
for (const part of message.content) {
|
|
3938
|
+
if (part.type === "tool-call") {
|
|
3939
|
+
droppedToolCallIds.add(part.toolCallId);
|
|
3940
|
+
}
|
|
3941
|
+
}
|
|
3942
|
+
continue;
|
|
3943
|
+
}
|
|
3944
|
+
out.push(message);
|
|
3945
|
+
continue;
|
|
3946
|
+
}
|
|
3947
|
+
if (message.role === "tool") {
|
|
3948
|
+
const remaining = message.content.filter((part) => {
|
|
3949
|
+
if (part.type !== "tool-result") {
|
|
3950
|
+
return true;
|
|
3951
|
+
}
|
|
3952
|
+
return !droppedToolCallIds.has(part.toolCallId);
|
|
3953
|
+
});
|
|
3954
|
+
if (remaining.length === 0) {
|
|
3955
|
+
continue;
|
|
3956
|
+
}
|
|
3957
|
+
out.push({
|
|
3958
|
+
...message,
|
|
3959
|
+
content: remaining
|
|
3960
|
+
});
|
|
3961
|
+
continue;
|
|
3962
|
+
}
|
|
3963
|
+
out.push(message);
|
|
3964
|
+
}
|
|
3965
|
+
return out;
|
|
3966
|
+
}
|
|
3967
|
+
function safeParseToolArgs(input) {
|
|
3968
|
+
try {
|
|
3969
|
+
const parsed = JSON.parse(input);
|
|
3970
|
+
if (parsed != null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
3971
|
+
return parsed;
|
|
3972
|
+
}
|
|
3973
|
+
return { value: parsed };
|
|
3974
|
+
} catch (e) {
|
|
3975
|
+
return { value: input };
|
|
3976
|
+
}
|
|
3977
|
+
}
|
|
3978
|
+
function convertToolResultPart({
|
|
3979
|
+
toolCallId,
|
|
3980
|
+
toolName,
|
|
3981
|
+
output,
|
|
3982
|
+
signature,
|
|
3983
|
+
warnings
|
|
3984
|
+
}) {
|
|
3985
|
+
var _a;
|
|
3986
|
+
const base = {
|
|
3987
|
+
type: "function_result",
|
|
3988
|
+
call_id: toolCallId,
|
|
3989
|
+
name: toolName,
|
|
3990
|
+
...signature != null ? { signature } : {}
|
|
3991
|
+
};
|
|
3992
|
+
switch (output.type) {
|
|
3993
|
+
case "text":
|
|
3994
|
+
return { ...base, result: output.value };
|
|
3995
|
+
case "json":
|
|
3996
|
+
return { ...base, result: JSON.stringify(output.value) };
|
|
3997
|
+
case "error-text":
|
|
3998
|
+
return { ...base, is_error: true, result: output.value };
|
|
3999
|
+
case "error-json":
|
|
4000
|
+
return { ...base, is_error: true, result: JSON.stringify(output.value) };
|
|
4001
|
+
case "execution-denied":
|
|
4002
|
+
return {
|
|
4003
|
+
...base,
|
|
4004
|
+
is_error: true,
|
|
4005
|
+
result: (_a = output.reason) != null ? _a : "Tool execution denied by user."
|
|
4006
|
+
};
|
|
4007
|
+
case "content": {
|
|
4008
|
+
const blocks = [];
|
|
4009
|
+
for (const item of output.value) {
|
|
4010
|
+
if (item.type === "text") {
|
|
4011
|
+
blocks.push({ type: "text", text: item.text });
|
|
4012
|
+
} else if (item.type === "image-data") {
|
|
4013
|
+
const imageBlock = filePartToImageBlock({
|
|
4014
|
+
part: {
|
|
4015
|
+
type: "file",
|
|
4016
|
+
mediaType: item.mediaType,
|
|
4017
|
+
data: item.data
|
|
4018
|
+
},
|
|
4019
|
+
warnings
|
|
4020
|
+
});
|
|
4021
|
+
if (imageBlock != null) {
|
|
4022
|
+
blocks.push(imageBlock);
|
|
4023
|
+
}
|
|
4024
|
+
} else if (item.type === "image-url") {
|
|
4025
|
+
const imageBlock = filePartToImageBlock({
|
|
4026
|
+
part: {
|
|
4027
|
+
type: "file",
|
|
4028
|
+
mediaType: "image/*",
|
|
4029
|
+
data: new URL(item.url)
|
|
4030
|
+
},
|
|
4031
|
+
warnings
|
|
4032
|
+
});
|
|
4033
|
+
if (imageBlock != null) {
|
|
4034
|
+
blocks.push(imageBlock);
|
|
4035
|
+
}
|
|
4036
|
+
} else if (item.type === "file-data" || item.type === "file-url") {
|
|
4037
|
+
const mediaType = item.type === "file-data" ? item.mediaType : "application/*";
|
|
4038
|
+
const topLevel = getTopLevelMediaType(mediaType);
|
|
4039
|
+
if (topLevel !== "image") {
|
|
4040
|
+
warnings.push({
|
|
4041
|
+
type: "other",
|
|
4042
|
+
message: `google.interactions: tool-result file with mediaType "${mediaType}" is not supported (Interactions \`function_result.result\` accepts only text and image content); part dropped.`
|
|
4043
|
+
});
|
|
4044
|
+
continue;
|
|
4045
|
+
}
|
|
4046
|
+
const imageBlock = filePartToImageBlock({
|
|
4047
|
+
part: item.type === "file-data" ? {
|
|
4048
|
+
type: "file",
|
|
4049
|
+
mediaType: item.mediaType,
|
|
4050
|
+
data: item.data
|
|
4051
|
+
} : {
|
|
4052
|
+
type: "file",
|
|
4053
|
+
mediaType,
|
|
4054
|
+
data: new URL(item.url)
|
|
4055
|
+
},
|
|
4056
|
+
warnings
|
|
4057
|
+
});
|
|
4058
|
+
if (imageBlock != null) {
|
|
4059
|
+
blocks.push(imageBlock);
|
|
4060
|
+
}
|
|
4061
|
+
} else {
|
|
4062
|
+
warnings.push({
|
|
4063
|
+
type: "other",
|
|
4064
|
+
message: `google.interactions: tool-result content part type "${item.type}" is not supported; part dropped.`
|
|
4065
|
+
});
|
|
4066
|
+
}
|
|
4067
|
+
}
|
|
4068
|
+
return { ...base, result: blocks };
|
|
4069
|
+
}
|
|
4070
|
+
}
|
|
4071
|
+
}
|
|
4072
|
+
function filePartToImageBlock({
|
|
4073
|
+
part,
|
|
4074
|
+
warnings
|
|
4075
|
+
}) {
|
|
4076
|
+
if (part.data instanceof URL) {
|
|
4077
|
+
return {
|
|
4078
|
+
type: "image",
|
|
4079
|
+
uri: part.data.toString(),
|
|
4080
|
+
...isFullMediaType(part.mediaType) ? { mime_type: part.mediaType } : {}
|
|
4081
|
+
};
|
|
4082
|
+
}
|
|
4083
|
+
if (!isFullMediaType(part.mediaType)) {
|
|
4084
|
+
warnings.push({
|
|
4085
|
+
type: "other",
|
|
4086
|
+
message: `google.interactions: tool-result image part requires a full IANA media type (e.g. "image/png"), got "${part.mediaType}"; part dropped.`
|
|
4087
|
+
});
|
|
4088
|
+
return void 0;
|
|
4089
|
+
}
|
|
4090
|
+
return {
|
|
4091
|
+
type: "image",
|
|
4092
|
+
data: convertToBase643(part.data),
|
|
4093
|
+
mime_type: part.mediaType
|
|
4094
|
+
};
|
|
4095
|
+
}
|
|
4096
|
+
function mergeAdjacentTextContent(content) {
|
|
4097
|
+
if (content.length < 2) {
|
|
4098
|
+
return content;
|
|
4099
|
+
}
|
|
4100
|
+
const result = [];
|
|
4101
|
+
for (const block of content) {
|
|
4102
|
+
const last = result[result.length - 1];
|
|
4103
|
+
if (block.type === "text" && last != null && last.type === "text" && last.annotations == null && block.annotations == null) {
|
|
4104
|
+
const merged = {
|
|
4105
|
+
type: "text",
|
|
4106
|
+
text: `${last.text}
|
|
4107
|
+
|
|
4108
|
+
${block.text}`
|
|
4109
|
+
};
|
|
4110
|
+
result[result.length - 1] = merged;
|
|
4111
|
+
continue;
|
|
4112
|
+
}
|
|
4113
|
+
result.push(block);
|
|
4114
|
+
}
|
|
4115
|
+
return result;
|
|
4116
|
+
}
|
|
4117
|
+
|
|
4118
|
+
// src/interactions/google-interactions-api.ts
|
|
4119
|
+
import {
|
|
4120
|
+
lazySchema as lazySchema13,
|
|
4121
|
+
zodSchema as zodSchema13
|
|
4122
|
+
} from "@ai-sdk/provider-utils";
|
|
4123
|
+
import { z as z15 } from "zod/v4";
|
|
4124
|
+
var tokenByModalitySchema = () => z15.object({
|
|
4125
|
+
modality: z15.string().nullish(),
|
|
4126
|
+
tokens: z15.number().nullish()
|
|
4127
|
+
}).loose();
|
|
4128
|
+
var usageSchema2 = () => z15.object({
|
|
4129
|
+
total_input_tokens: z15.number().nullish(),
|
|
4130
|
+
total_output_tokens: z15.number().nullish(),
|
|
4131
|
+
total_thought_tokens: z15.number().nullish(),
|
|
4132
|
+
total_cached_tokens: z15.number().nullish(),
|
|
4133
|
+
total_tool_use_tokens: z15.number().nullish(),
|
|
4134
|
+
total_tokens: z15.number().nullish(),
|
|
4135
|
+
input_tokens_by_modality: z15.array(tokenByModalitySchema()).nullish(),
|
|
4136
|
+
output_tokens_by_modality: z15.array(tokenByModalitySchema()).nullish(),
|
|
4137
|
+
cached_tokens_by_modality: z15.array(tokenByModalitySchema()).nullish(),
|
|
4138
|
+
tool_use_tokens_by_modality: z15.array(tokenByModalitySchema()).nullish(),
|
|
4139
|
+
grounding_tool_count: z15.array(
|
|
4140
|
+
z15.object({
|
|
4141
|
+
type: z15.string().nullish(),
|
|
4142
|
+
count: z15.number().nullish()
|
|
4143
|
+
}).loose()
|
|
4144
|
+
).nullish()
|
|
4145
|
+
}).loose();
|
|
4146
|
+
var interactionStatusSchema = () => z15.enum([
|
|
4147
|
+
"in_progress",
|
|
4148
|
+
"requires_action",
|
|
4149
|
+
"completed",
|
|
4150
|
+
"failed",
|
|
4151
|
+
"cancelled",
|
|
4152
|
+
"incomplete"
|
|
4153
|
+
]);
|
|
4154
|
+
var annotationSchema = () => {
|
|
4155
|
+
const urlCitation = z15.object({
|
|
4156
|
+
type: z15.literal("url_citation"),
|
|
4157
|
+
url: z15.string().nullish(),
|
|
4158
|
+
title: z15.string().nullish(),
|
|
4159
|
+
start_index: z15.number().nullish(),
|
|
4160
|
+
end_index: z15.number().nullish()
|
|
4161
|
+
}).loose();
|
|
4162
|
+
const fileCitation = z15.object({
|
|
4163
|
+
type: z15.literal("file_citation"),
|
|
4164
|
+
file_name: z15.string().nullish(),
|
|
4165
|
+
document_uri: z15.string().nullish(),
|
|
4166
|
+
source: z15.string().nullish(),
|
|
4167
|
+
page_number: z15.number().nullish(),
|
|
4168
|
+
media_id: z15.string().nullish(),
|
|
4169
|
+
start_index: z15.number().nullish(),
|
|
4170
|
+
end_index: z15.number().nullish(),
|
|
4171
|
+
custom_metadata: z15.record(z15.string(), z15.unknown()).nullish()
|
|
4172
|
+
}).loose();
|
|
4173
|
+
const placeCitation = z15.object({
|
|
4174
|
+
type: z15.literal("place_citation"),
|
|
4175
|
+
name: z15.string().nullish(),
|
|
4176
|
+
url: z15.string().nullish(),
|
|
4177
|
+
place_id: z15.string().nullish(),
|
|
4178
|
+
start_index: z15.number().nullish(),
|
|
4179
|
+
end_index: z15.number().nullish()
|
|
4180
|
+
}).loose();
|
|
4181
|
+
return z15.union([
|
|
4182
|
+
urlCitation,
|
|
4183
|
+
fileCitation,
|
|
4184
|
+
placeCitation,
|
|
4185
|
+
z15.object({ type: z15.string() }).loose()
|
|
4186
|
+
]);
|
|
4187
|
+
};
|
|
4188
|
+
var thoughtSummaryItemSchema = () => z15.object({
|
|
4189
|
+
type: z15.string(),
|
|
4190
|
+
text: z15.string().nullish(),
|
|
4191
|
+
data: z15.string().nullish(),
|
|
4192
|
+
mime_type: z15.string().nullish()
|
|
4193
|
+
}).loose();
|
|
4194
|
+
var contentBlockSchema = () => {
|
|
4195
|
+
const textContent = z15.object({
|
|
4196
|
+
type: z15.literal("text"),
|
|
4197
|
+
text: z15.string(),
|
|
4198
|
+
annotations: z15.array(annotationSchema()).nullish()
|
|
4199
|
+
}).loose();
|
|
4200
|
+
const thoughtContent = z15.object({
|
|
4201
|
+
type: z15.literal("thought"),
|
|
4202
|
+
signature: z15.string().nullish(),
|
|
4203
|
+
summary: z15.array(thoughtSummaryItemSchema()).nullish()
|
|
4204
|
+
}).loose();
|
|
4205
|
+
const functionCallContent = z15.object({
|
|
4206
|
+
type: z15.literal("function_call"),
|
|
4207
|
+
id: z15.string(),
|
|
4208
|
+
name: z15.string(),
|
|
4209
|
+
arguments: z15.record(z15.string(), z15.unknown()).nullish(),
|
|
4210
|
+
signature: z15.string().nullish()
|
|
4211
|
+
}).loose();
|
|
4212
|
+
const imageContent = z15.object({
|
|
4213
|
+
type: z15.literal("image"),
|
|
4214
|
+
data: z15.string().nullish(),
|
|
4215
|
+
mime_type: z15.string().nullish(),
|
|
4216
|
+
resolution: z15.enum(["low", "medium", "high", "ultra_high"]).nullish(),
|
|
4217
|
+
uri: z15.string().nullish()
|
|
4218
|
+
}).loose();
|
|
4219
|
+
const builtinToolCall = z15.object({
|
|
4220
|
+
type: z15.enum([
|
|
4221
|
+
"google_search_call",
|
|
4222
|
+
"code_execution_call",
|
|
4223
|
+
"url_context_call",
|
|
4224
|
+
"file_search_call",
|
|
4225
|
+
"google_maps_call",
|
|
4226
|
+
"mcp_server_tool_call"
|
|
4227
|
+
]),
|
|
4228
|
+
id: z15.string(),
|
|
4229
|
+
arguments: z15.record(z15.string(), z15.unknown()).nullish(),
|
|
4230
|
+
name: z15.string().nullish(),
|
|
4231
|
+
server_name: z15.string().nullish(),
|
|
4232
|
+
search_type: z15.string().nullish(),
|
|
4233
|
+
signature: z15.string().nullish()
|
|
4234
|
+
}).loose();
|
|
4235
|
+
const builtinToolResult = z15.object({
|
|
4236
|
+
type: z15.enum([
|
|
4237
|
+
"google_search_result",
|
|
4238
|
+
"code_execution_result",
|
|
4239
|
+
"url_context_result",
|
|
4240
|
+
"file_search_result",
|
|
4241
|
+
"google_maps_result",
|
|
4242
|
+
"mcp_server_tool_result"
|
|
4243
|
+
]),
|
|
4244
|
+
call_id: z15.string(),
|
|
4245
|
+
result: z15.unknown().nullish(),
|
|
4246
|
+
is_error: z15.boolean().nullish(),
|
|
4247
|
+
name: z15.string().nullish(),
|
|
4248
|
+
server_name: z15.string().nullish(),
|
|
4249
|
+
signature: z15.string().nullish()
|
|
4250
|
+
}).loose();
|
|
4251
|
+
return z15.union([
|
|
4252
|
+
textContent,
|
|
4253
|
+
imageContent,
|
|
4254
|
+
thoughtContent,
|
|
4255
|
+
functionCallContent,
|
|
4256
|
+
builtinToolCall,
|
|
4257
|
+
builtinToolResult,
|
|
4258
|
+
z15.object({ type: z15.string() }).loose()
|
|
4259
|
+
]);
|
|
4260
|
+
};
|
|
4261
|
+
var googleInteractionsResponseSchema = lazySchema13(
|
|
4262
|
+
() => zodSchema13(
|
|
4263
|
+
z15.object({
|
|
4264
|
+
/*
|
|
4265
|
+
* `id` is omitted from the response body when `store: false` (fully
|
|
4266
|
+
* stateless mode) — there is no server-side interaction record for the
|
|
4267
|
+
* client to reference. `nullish` lets the schema accept that shape.
|
|
4268
|
+
*/
|
|
4269
|
+
id: z15.string().nullish(),
|
|
4270
|
+
created: z15.string().nullish(),
|
|
4271
|
+
updated: z15.string().nullish(),
|
|
4272
|
+
status: interactionStatusSchema(),
|
|
4273
|
+
model: z15.string().nullish(),
|
|
4274
|
+
agent: z15.string().nullish(),
|
|
4275
|
+
outputs: z15.array(contentBlockSchema()).nullish(),
|
|
4276
|
+
usage: usageSchema2().nullish(),
|
|
4277
|
+
service_tier: z15.string().nullish(),
|
|
4278
|
+
previous_interaction_id: z15.string().nullish(),
|
|
4279
|
+
response_modalities: z15.array(z15.string()).nullish()
|
|
4280
|
+
}).loose()
|
|
4281
|
+
)
|
|
4282
|
+
);
|
|
4283
|
+
var googleInteractionsEventSchema = lazySchema13(
|
|
4284
|
+
() => zodSchema13(
|
|
4285
|
+
(() => {
|
|
4286
|
+
const status = interactionStatusSchema();
|
|
4287
|
+
const annotation = annotationSchema();
|
|
4288
|
+
const thoughtSummaryItem = thoughtSummaryItemSchema();
|
|
4289
|
+
const interactionStartEvent = z15.object({
|
|
4290
|
+
event_type: z15.literal("interaction.start"),
|
|
4291
|
+
event_id: z15.string().nullish(),
|
|
4292
|
+
interaction: z15.object({
|
|
4293
|
+
/*
|
|
4294
|
+
* `id` is omitted when `store: false` (fully stateless mode);
|
|
4295
|
+
* see the matching note on `googleInteractionsResponseSchema.id`.
|
|
4296
|
+
*/
|
|
4297
|
+
id: z15.string().nullish(),
|
|
4298
|
+
created: z15.string().nullish(),
|
|
4299
|
+
model: z15.string().nullish(),
|
|
4300
|
+
agent: z15.string().nullish(),
|
|
4301
|
+
status: status.nullish()
|
|
4302
|
+
}).loose()
|
|
4303
|
+
}).loose();
|
|
4304
|
+
const contentStartEvent = z15.object({
|
|
4305
|
+
event_type: z15.literal("content.start"),
|
|
4306
|
+
event_id: z15.string().nullish(),
|
|
4307
|
+
index: z15.number(),
|
|
4308
|
+
content: contentBlockSchema()
|
|
4309
|
+
}).loose();
|
|
4310
|
+
const contentDeltaText = z15.object({
|
|
4311
|
+
type: z15.literal("text"),
|
|
4312
|
+
text: z15.string()
|
|
4313
|
+
}).loose();
|
|
4314
|
+
const contentDeltaThoughtSummary = z15.object({
|
|
4315
|
+
type: z15.literal("thought_summary"),
|
|
4316
|
+
content: thoughtSummaryItem.nullish()
|
|
4317
|
+
}).loose();
|
|
4318
|
+
const contentDeltaThoughtSignature = z15.object({
|
|
4319
|
+
type: z15.literal("thought_signature"),
|
|
4320
|
+
signature: z15.string().nullish()
|
|
4321
|
+
}).loose();
|
|
4322
|
+
const contentDeltaFunctionCall = z15.object({
|
|
4323
|
+
type: z15.literal("function_call"),
|
|
4324
|
+
id: z15.string(),
|
|
4325
|
+
name: z15.string(),
|
|
4326
|
+
arguments: z15.record(z15.string(), z15.unknown()).nullish(),
|
|
4327
|
+
signature: z15.string().nullish()
|
|
4328
|
+
}).loose();
|
|
4329
|
+
const contentDeltaTextAnnotation = z15.object({
|
|
4330
|
+
type: z15.literal("text_annotation"),
|
|
4331
|
+
annotations: z15.array(annotation).nullish()
|
|
4332
|
+
}).loose();
|
|
4333
|
+
const contentDeltaImage = z15.object({
|
|
4334
|
+
type: z15.literal("image"),
|
|
4335
|
+
data: z15.string().nullish(),
|
|
4336
|
+
mime_type: z15.string().nullish(),
|
|
4337
|
+
resolution: z15.enum(["low", "medium", "high", "ultra_high"]).nullish(),
|
|
4338
|
+
uri: z15.string().nullish()
|
|
4339
|
+
}).loose();
|
|
4340
|
+
const contentDeltaBuiltinToolCall = z15.object({
|
|
4341
|
+
type: z15.enum([
|
|
4342
|
+
"google_search_call",
|
|
4343
|
+
"code_execution_call",
|
|
4344
|
+
"url_context_call",
|
|
4345
|
+
"file_search_call",
|
|
4346
|
+
"google_maps_call",
|
|
4347
|
+
"mcp_server_tool_call"
|
|
4348
|
+
]),
|
|
4349
|
+
id: z15.string(),
|
|
4350
|
+
arguments: z15.record(z15.string(), z15.unknown()).nullish(),
|
|
4351
|
+
name: z15.string().nullish(),
|
|
4352
|
+
server_name: z15.string().nullish(),
|
|
4353
|
+
search_type: z15.string().nullish(),
|
|
4354
|
+
signature: z15.string().nullish()
|
|
4355
|
+
}).loose();
|
|
4356
|
+
const contentDeltaBuiltinToolResult = z15.object({
|
|
4357
|
+
type: z15.enum([
|
|
4358
|
+
"google_search_result",
|
|
4359
|
+
"code_execution_result",
|
|
4360
|
+
"url_context_result",
|
|
4361
|
+
"file_search_result",
|
|
4362
|
+
"google_maps_result",
|
|
4363
|
+
"mcp_server_tool_result"
|
|
4364
|
+
]),
|
|
4365
|
+
call_id: z15.string(),
|
|
4366
|
+
result: z15.unknown().nullish(),
|
|
4367
|
+
is_error: z15.boolean().nullish(),
|
|
4368
|
+
name: z15.string().nullish(),
|
|
4369
|
+
server_name: z15.string().nullish(),
|
|
4370
|
+
signature: z15.string().nullish()
|
|
4371
|
+
}).loose();
|
|
4372
|
+
const contentDeltaUnknown = z15.object({ type: z15.string() }).loose();
|
|
4373
|
+
const contentDeltaUnion = z15.union([
|
|
4374
|
+
contentDeltaText,
|
|
4375
|
+
contentDeltaImage,
|
|
4376
|
+
contentDeltaThoughtSummary,
|
|
4377
|
+
contentDeltaThoughtSignature,
|
|
4378
|
+
contentDeltaFunctionCall,
|
|
4379
|
+
contentDeltaTextAnnotation,
|
|
4380
|
+
contentDeltaBuiltinToolCall,
|
|
4381
|
+
contentDeltaBuiltinToolResult,
|
|
4382
|
+
contentDeltaUnknown
|
|
4383
|
+
]);
|
|
4384
|
+
const contentDeltaEvent = z15.object({
|
|
4385
|
+
event_type: z15.literal("content.delta"),
|
|
4386
|
+
event_id: z15.string().nullish(),
|
|
4387
|
+
index: z15.number(),
|
|
4388
|
+
delta: contentDeltaUnion
|
|
4389
|
+
}).loose();
|
|
4390
|
+
const contentStopEvent = z15.object({
|
|
4391
|
+
event_type: z15.literal("content.stop"),
|
|
4392
|
+
event_id: z15.string().nullish(),
|
|
4393
|
+
index: z15.number()
|
|
4394
|
+
}).loose();
|
|
4395
|
+
const interactionStatusUpdateEvent = z15.object({
|
|
4396
|
+
event_type: z15.literal("interaction.status_update"),
|
|
4397
|
+
event_id: z15.string().nullish(),
|
|
4398
|
+
interaction_id: z15.string().nullish(),
|
|
4399
|
+
status
|
|
4400
|
+
}).loose();
|
|
4401
|
+
const interactionCompleteEvent = z15.object({
|
|
4402
|
+
event_type: z15.literal("interaction.complete"),
|
|
4403
|
+
event_id: z15.string().nullish(),
|
|
4404
|
+
interaction: z15.object({
|
|
4405
|
+
id: z15.string().nullish(),
|
|
4406
|
+
status: status.nullish(),
|
|
4407
|
+
usage: usageSchema2().nullish(),
|
|
4408
|
+
service_tier: z15.string().nullish()
|
|
4409
|
+
}).loose()
|
|
4410
|
+
}).loose();
|
|
4411
|
+
const errorEvent = z15.object({
|
|
4412
|
+
event_type: z15.literal("error"),
|
|
4413
|
+
event_id: z15.string().nullish(),
|
|
4414
|
+
error: z15.object({
|
|
4415
|
+
code: z15.string().nullish(),
|
|
4416
|
+
message: z15.string().nullish()
|
|
4417
|
+
}).loose().nullish()
|
|
4418
|
+
}).loose();
|
|
4419
|
+
const unknownEvent = z15.object({ event_type: z15.string() }).loose();
|
|
4420
|
+
return z15.union([
|
|
4421
|
+
interactionStartEvent,
|
|
4422
|
+
contentStartEvent,
|
|
4423
|
+
contentDeltaEvent,
|
|
4424
|
+
contentStopEvent,
|
|
4425
|
+
interactionStatusUpdateEvent,
|
|
4426
|
+
interactionCompleteEvent,
|
|
4427
|
+
errorEvent,
|
|
4428
|
+
unknownEvent
|
|
4429
|
+
]);
|
|
4430
|
+
})()
|
|
4431
|
+
)
|
|
4432
|
+
);
|
|
4433
|
+
|
|
4434
|
+
// src/interactions/google-interactions-language-model-options.ts
|
|
4435
|
+
import {
|
|
4436
|
+
lazySchema as lazySchema14,
|
|
4437
|
+
zodSchema as zodSchema14
|
|
4438
|
+
} from "@ai-sdk/provider-utils";
|
|
4439
|
+
import { z as z16 } from "zod/v4";
|
|
4440
|
+
var googleInteractionsLanguageModelOptions = lazySchema14(
|
|
4441
|
+
() => zodSchema14(
|
|
4442
|
+
z16.object({
|
|
4443
|
+
previousInteractionId: z16.string().nullish(),
|
|
4444
|
+
store: z16.boolean().nullish(),
|
|
4445
|
+
agent: z16.string().nullish(),
|
|
4446
|
+
agentConfig: z16.union([
|
|
4447
|
+
z16.object({
|
|
4448
|
+
type: z16.literal("dynamic")
|
|
4449
|
+
}).loose(),
|
|
4450
|
+
z16.object({
|
|
4451
|
+
type: z16.literal("deep-research"),
|
|
4452
|
+
thinkingSummaries: z16.enum(["auto", "none"]).nullish(),
|
|
4453
|
+
visualization: z16.enum(["off", "auto"]).nullish(),
|
|
4454
|
+
collaborativePlanning: z16.boolean().nullish()
|
|
4455
|
+
})
|
|
4456
|
+
]).nullish(),
|
|
4457
|
+
thinkingLevel: z16.enum(["minimal", "low", "medium", "high"]).nullish(),
|
|
4458
|
+
thinkingSummaries: z16.enum(["auto", "none"]).nullish(),
|
|
4459
|
+
imageConfig: z16.object({
|
|
4460
|
+
aspectRatio: z16.enum([
|
|
4461
|
+
"1:1",
|
|
4462
|
+
"2:3",
|
|
4463
|
+
"3:2",
|
|
4464
|
+
"3:4",
|
|
4465
|
+
"4:3",
|
|
4466
|
+
"4:5",
|
|
4467
|
+
"5:4",
|
|
4468
|
+
"9:16",
|
|
4469
|
+
"16:9",
|
|
4470
|
+
"21:9",
|
|
4471
|
+
"1:8",
|
|
4472
|
+
"8:1",
|
|
4473
|
+
"1:4",
|
|
4474
|
+
"4:1"
|
|
4475
|
+
]).nullish(),
|
|
4476
|
+
imageSize: z16.enum(["1K", "2K", "4K", "512"]).nullish()
|
|
4477
|
+
}).nullish(),
|
|
4478
|
+
mediaResolution: z16.enum(["low", "medium", "high", "ultra_high"]).nullish(),
|
|
4479
|
+
responseModalities: z16.array(z16.enum(["text", "image", "audio", "video", "document"])).nullish(),
|
|
4480
|
+
serviceTier: z16.enum(["flex", "standard", "priority"]).nullish(),
|
|
4481
|
+
/**
|
|
4482
|
+
* Alternative to AI SDK `system` message. If both are set, the AI SDK
|
|
4483
|
+
* `system` message wins and a warning is emitted.
|
|
4484
|
+
*/
|
|
4485
|
+
systemInstruction: z16.string().nullish(),
|
|
4486
|
+
/**
|
|
4487
|
+
* Per-block signature for round-tripping `thought.signature` and
|
|
4488
|
+
* `function_call.signature` blocks. Set by the SDK on output reasoning /
|
|
4489
|
+
* tool-call parts; passed back unchanged on input parts so the API
|
|
4490
|
+
* accepts the prior turn.
|
|
4491
|
+
*/
|
|
4492
|
+
signature: z16.string().nullish(),
|
|
4493
|
+
/**
|
|
4494
|
+
* Set by the SDK on output assistant messages. The converter uses it to
|
|
4495
|
+
* decide which messages to drop when compacting under
|
|
4496
|
+
* `previousInteractionId`.
|
|
4497
|
+
*/
|
|
4498
|
+
interactionId: z16.string().nullish(),
|
|
4499
|
+
/**
|
|
4500
|
+
* Maximum time, in milliseconds, to poll a background interaction (agent
|
|
4501
|
+
* call) before giving up. Defaults to 30 minutes. Long-running agents
|
|
4502
|
+
* such as deep research can take tens of minutes — increase if needed.
|
|
4503
|
+
*/
|
|
4504
|
+
pollingTimeoutMs: z16.number().int().positive().nullish()
|
|
4505
|
+
})
|
|
4506
|
+
)
|
|
4507
|
+
);
|
|
4508
|
+
|
|
4509
|
+
// src/interactions/parse-google-interactions-outputs.ts
|
|
4510
|
+
function googleProviderMetadata({
|
|
4511
|
+
signature,
|
|
4512
|
+
interactionId
|
|
4513
|
+
}) {
|
|
4514
|
+
const google2 = {};
|
|
4515
|
+
if (signature != null) {
|
|
4516
|
+
google2.signature = signature;
|
|
4517
|
+
}
|
|
4518
|
+
if (interactionId != null) {
|
|
4519
|
+
google2.interactionId = interactionId;
|
|
4520
|
+
}
|
|
4521
|
+
return Object.keys(google2).length > 0 ? { providerMetadata: { google: google2 } } : {};
|
|
4522
|
+
}
|
|
4523
|
+
var BUILTIN_TOOL_CALL_TYPES2 = /* @__PURE__ */ new Set([
|
|
4524
|
+
"google_search_call",
|
|
4525
|
+
"code_execution_call",
|
|
4526
|
+
"url_context_call",
|
|
4527
|
+
"file_search_call",
|
|
4528
|
+
"google_maps_call",
|
|
4529
|
+
"mcp_server_tool_call"
|
|
4530
|
+
]);
|
|
4531
|
+
var BUILTIN_TOOL_RESULT_TYPES2 = /* @__PURE__ */ new Set([
|
|
4532
|
+
"google_search_result",
|
|
4533
|
+
"code_execution_result",
|
|
4534
|
+
"url_context_result",
|
|
4535
|
+
"file_search_result",
|
|
4536
|
+
"google_maps_result",
|
|
4537
|
+
"mcp_server_tool_result"
|
|
4538
|
+
]);
|
|
4539
|
+
function builtinToolNameFromCallType2(type) {
|
|
4540
|
+
return type.replace(/_call$/, "");
|
|
4541
|
+
}
|
|
4542
|
+
function builtinToolNameFromResultType2(type) {
|
|
4543
|
+
return type.replace(/_result$/, "");
|
|
4544
|
+
}
|
|
4545
|
+
function parseGoogleInteractionsOutputs({
|
|
4546
|
+
outputs,
|
|
4547
|
+
generateId: generateId3,
|
|
4548
|
+
interactionId
|
|
4549
|
+
}) {
|
|
4550
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
4551
|
+
const content = [];
|
|
4552
|
+
let hasFunctionCall = false;
|
|
4553
|
+
if (outputs == null) {
|
|
4554
|
+
return { content, hasFunctionCall };
|
|
4555
|
+
}
|
|
4556
|
+
for (const block of outputs) {
|
|
4557
|
+
if (block == null || typeof block !== "object") continue;
|
|
4558
|
+
const type = block.type;
|
|
4559
|
+
if (typeof type !== "string") continue;
|
|
4560
|
+
switch (type) {
|
|
4561
|
+
case "text": {
|
|
4562
|
+
const text = (_a = block.text) != null ? _a : "";
|
|
4563
|
+
const annotations = block.annotations;
|
|
4564
|
+
content.push({
|
|
4565
|
+
type: "text",
|
|
4566
|
+
text,
|
|
4567
|
+
...googleProviderMetadata({ interactionId })
|
|
4568
|
+
});
|
|
4569
|
+
const sources = annotationsToSources({ annotations, generateId: generateId3 });
|
|
4570
|
+
for (const source of sources) {
|
|
4571
|
+
content.push(source);
|
|
4572
|
+
}
|
|
4573
|
+
break;
|
|
4574
|
+
}
|
|
4575
|
+
case "thought": {
|
|
4576
|
+
const thought = block;
|
|
4577
|
+
const summary = Array.isArray(thought.summary) ? thought.summary : [];
|
|
4578
|
+
const text = summary.filter(
|
|
4579
|
+
(item) => (item == null ? void 0 : item.type) === "text" && typeof item.text === "string"
|
|
4580
|
+
).map((item) => item.text).join("\n");
|
|
4581
|
+
content.push({
|
|
4582
|
+
type: "reasoning",
|
|
4583
|
+
text,
|
|
4584
|
+
...googleProviderMetadata({
|
|
4585
|
+
signature: thought.signature,
|
|
4586
|
+
interactionId
|
|
4587
|
+
})
|
|
4588
|
+
});
|
|
4589
|
+
break;
|
|
4590
|
+
}
|
|
4591
|
+
case "image": {
|
|
4592
|
+
const image = block;
|
|
4593
|
+
if (image.data != null && image.data.length > 0) {
|
|
4594
|
+
content.push({
|
|
4595
|
+
type: "file",
|
|
4596
|
+
mediaType: (_b = image.mime_type) != null ? _b : "image/png",
|
|
4597
|
+
data: image.data,
|
|
4598
|
+
...googleProviderMetadata({ interactionId })
|
|
4599
|
+
});
|
|
4600
|
+
} else if (image.uri != null && image.uri.length > 0) {
|
|
4601
|
+
content.push({
|
|
4602
|
+
type: "file",
|
|
4603
|
+
mediaType: (_c = image.mime_type) != null ? _c : "image/png",
|
|
4604
|
+
data: "",
|
|
4605
|
+
providerMetadata: {
|
|
4606
|
+
google: {
|
|
4607
|
+
...interactionId != null ? { interactionId } : {},
|
|
4608
|
+
imageUri: image.uri
|
|
4609
|
+
}
|
|
4610
|
+
}
|
|
4611
|
+
});
|
|
4612
|
+
}
|
|
4613
|
+
break;
|
|
4614
|
+
}
|
|
4615
|
+
case "function_call": {
|
|
4616
|
+
hasFunctionCall = true;
|
|
4617
|
+
const call = block;
|
|
4618
|
+
content.push({
|
|
4619
|
+
type: "tool-call",
|
|
4620
|
+
toolCallId: call.id,
|
|
4621
|
+
toolName: call.name,
|
|
4622
|
+
input: JSON.stringify((_d = call.arguments) != null ? _d : {}),
|
|
4623
|
+
...googleProviderMetadata({
|
|
4624
|
+
signature: call.signature,
|
|
4625
|
+
interactionId
|
|
4626
|
+
})
|
|
4627
|
+
});
|
|
4628
|
+
break;
|
|
4629
|
+
}
|
|
4630
|
+
default: {
|
|
4631
|
+
if (BUILTIN_TOOL_CALL_TYPES2.has(type)) {
|
|
4632
|
+
const call = block;
|
|
4633
|
+
const toolName = type === "mcp_server_tool_call" ? (_e = call.name) != null ? _e : "mcp_server_tool" : builtinToolNameFromCallType2(type);
|
|
4634
|
+
const input = JSON.stringify((_f = call.arguments) != null ? _f : {});
|
|
4635
|
+
content.push({
|
|
4636
|
+
type: "tool-call",
|
|
4637
|
+
toolCallId: (_g = call.id) != null ? _g : generateId3(),
|
|
4638
|
+
toolName,
|
|
4639
|
+
input,
|
|
4640
|
+
providerExecuted: true
|
|
4641
|
+
});
|
|
4642
|
+
} else if (BUILTIN_TOOL_RESULT_TYPES2.has(type)) {
|
|
4643
|
+
const result = block;
|
|
4644
|
+
const toolName = type === "mcp_server_tool_result" ? (_h = result.name) != null ? _h : "mcp_server_tool" : builtinToolNameFromResultType2(type);
|
|
4645
|
+
content.push({
|
|
4646
|
+
type: "tool-result",
|
|
4647
|
+
toolCallId: (_i = result.call_id) != null ? _i : generateId3(),
|
|
4648
|
+
toolName,
|
|
4649
|
+
result: (_j = result.result) != null ? _j : null
|
|
4650
|
+
});
|
|
4651
|
+
const sources = builtinToolResultToSources({
|
|
4652
|
+
block,
|
|
4653
|
+
generateId: generateId3
|
|
4654
|
+
});
|
|
4655
|
+
for (const source of sources) {
|
|
4656
|
+
content.push(source);
|
|
4657
|
+
}
|
|
4658
|
+
}
|
|
4659
|
+
break;
|
|
4660
|
+
}
|
|
4661
|
+
}
|
|
4662
|
+
}
|
|
4663
|
+
return { content, hasFunctionCall };
|
|
4664
|
+
}
|
|
4665
|
+
|
|
4666
|
+
// src/interactions/poll-google-interactions.ts
|
|
4667
|
+
import {
|
|
4668
|
+
createJsonResponseHandler as createJsonResponseHandler5,
|
|
4669
|
+
delay as delay2,
|
|
4670
|
+
getFromApi as getFromApi2
|
|
4671
|
+
} from "@ai-sdk/provider-utils";
|
|
4672
|
+
var TERMINAL_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "incomplete"]);
|
|
4673
|
+
function isTerminalStatus(status) {
|
|
4674
|
+
return status != null && TERMINAL_STATUSES.has(status);
|
|
4675
|
+
}
|
|
4676
|
+
var DEFAULT_INITIAL_DELAY_MS = 1e3;
|
|
4677
|
+
var DEFAULT_MAX_DELAY_MS = 1e4;
|
|
4678
|
+
var DEFAULT_TIMEOUT_MS = 30 * 60 * 1e3;
|
|
4679
|
+
async function pollGoogleInteractionUntilTerminal({
|
|
4680
|
+
baseURL,
|
|
4681
|
+
interactionId,
|
|
4682
|
+
headers,
|
|
4683
|
+
fetch,
|
|
4684
|
+
abortSignal,
|
|
4685
|
+
initialDelayMs = DEFAULT_INITIAL_DELAY_MS,
|
|
4686
|
+
maxDelayMs = DEFAULT_MAX_DELAY_MS,
|
|
4687
|
+
timeoutMs = DEFAULT_TIMEOUT_MS
|
|
4688
|
+
}) {
|
|
4689
|
+
if (interactionId == null || interactionId.length === 0) {
|
|
4690
|
+
throw new Error(
|
|
4691
|
+
"google.interactions: cannot poll a background interaction without an id. The POST response did not include an interaction id."
|
|
4692
|
+
);
|
|
4693
|
+
}
|
|
4694
|
+
const startedAt = Date.now();
|
|
4695
|
+
let nextDelayMs = initialDelayMs;
|
|
4696
|
+
const url = `${baseURL}/interactions/${encodeURIComponent(interactionId)}`;
|
|
4697
|
+
while (true) {
|
|
4698
|
+
if (abortSignal == null ? void 0 : abortSignal.aborted) {
|
|
4699
|
+
throw new DOMException("Polling was aborted", "AbortError");
|
|
4700
|
+
}
|
|
4701
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
4702
|
+
throw new Error(
|
|
4703
|
+
`google.interactions: timed out polling interaction ${interactionId} after ${timeoutMs}ms.`
|
|
4704
|
+
);
|
|
4705
|
+
}
|
|
4706
|
+
await delay2(nextDelayMs, { abortSignal });
|
|
4707
|
+
const {
|
|
4708
|
+
value: response,
|
|
4709
|
+
rawValue: rawResponse,
|
|
4710
|
+
responseHeaders
|
|
4711
|
+
} = await getFromApi2({
|
|
4712
|
+
url,
|
|
4713
|
+
headers,
|
|
4714
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
4715
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
4716
|
+
googleInteractionsResponseSchema
|
|
4717
|
+
),
|
|
4718
|
+
abortSignal,
|
|
4719
|
+
fetch
|
|
4720
|
+
});
|
|
4721
|
+
if (isTerminalStatus(response.status)) {
|
|
4722
|
+
return { response, rawResponse, responseHeaders };
|
|
4723
|
+
}
|
|
4724
|
+
nextDelayMs = Math.min(nextDelayMs * 2, maxDelayMs);
|
|
4725
|
+
}
|
|
4726
|
+
}
|
|
4727
|
+
|
|
4728
|
+
// src/interactions/prepare-google-interactions-tools.ts
|
|
4729
|
+
function prepareGoogleInteractionsTools({
|
|
4730
|
+
tools,
|
|
4731
|
+
toolChoice
|
|
4732
|
+
}) {
|
|
4733
|
+
var _a, _b, _c, _d;
|
|
4734
|
+
const toolWarnings = [];
|
|
4735
|
+
const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
4736
|
+
if (normalized == null) {
|
|
4737
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
4738
|
+
}
|
|
4739
|
+
const interactionsTools = [];
|
|
4740
|
+
for (const tool of normalized) {
|
|
4741
|
+
if (tool.type === "function") {
|
|
4742
|
+
interactionsTools.push({
|
|
4743
|
+
type: "function",
|
|
4744
|
+
name: tool.name,
|
|
4745
|
+
description: (_a = tool.description) != null ? _a : "",
|
|
4746
|
+
parameters: tool.inputSchema
|
|
4747
|
+
});
|
|
4748
|
+
continue;
|
|
4749
|
+
}
|
|
4750
|
+
if (tool.type === "provider") {
|
|
4751
|
+
const args = (_b = tool.args) != null ? _b : {};
|
|
4752
|
+
switch (tool.id) {
|
|
4753
|
+
case "google.google_search": {
|
|
4754
|
+
const searchTypesArg = args.searchTypes;
|
|
4755
|
+
let search_types;
|
|
4756
|
+
if (searchTypesArg != null && typeof searchTypesArg === "object") {
|
|
4757
|
+
const list = [];
|
|
4758
|
+
if (searchTypesArg.webSearch != null) list.push("web_search");
|
|
4759
|
+
if (searchTypesArg.imageSearch != null) list.push("image_search");
|
|
4760
|
+
if (list.length > 0) {
|
|
4761
|
+
search_types = list;
|
|
4762
|
+
}
|
|
4763
|
+
}
|
|
4764
|
+
interactionsTools.push({
|
|
4765
|
+
type: "google_search",
|
|
4766
|
+
...search_types != null ? { search_types } : {}
|
|
4767
|
+
});
|
|
4768
|
+
break;
|
|
4769
|
+
}
|
|
4770
|
+
case "google.code_execution": {
|
|
4771
|
+
interactionsTools.push({ type: "code_execution" });
|
|
4772
|
+
break;
|
|
4773
|
+
}
|
|
4774
|
+
case "google.url_context": {
|
|
4775
|
+
interactionsTools.push({ type: "url_context" });
|
|
4776
|
+
break;
|
|
4777
|
+
}
|
|
4778
|
+
case "google.file_search": {
|
|
4779
|
+
interactionsTools.push({
|
|
4780
|
+
type: "file_search",
|
|
4781
|
+
...args.fileSearchStoreNames != null ? {
|
|
4782
|
+
file_search_store_names: args.fileSearchStoreNames
|
|
4783
|
+
} : {},
|
|
4784
|
+
...args.topK != null ? { top_k: args.topK } : {},
|
|
4785
|
+
...args.metadataFilter != null ? { metadata_filter: args.metadataFilter } : {}
|
|
4786
|
+
});
|
|
4787
|
+
break;
|
|
4788
|
+
}
|
|
4789
|
+
case "google.google_maps": {
|
|
4790
|
+
interactionsTools.push({
|
|
4791
|
+
type: "google_maps",
|
|
4792
|
+
...args.latitude != null ? { latitude: args.latitude } : {},
|
|
4793
|
+
...args.longitude != null ? { longitude: args.longitude } : {},
|
|
4794
|
+
...args.enableWidget != null ? { enable_widget: args.enableWidget } : {}
|
|
4795
|
+
});
|
|
4796
|
+
break;
|
|
4797
|
+
}
|
|
4798
|
+
case "google.computer_use": {
|
|
4799
|
+
interactionsTools.push({
|
|
4800
|
+
type: "computer_use",
|
|
4801
|
+
environment: (_c = args.environment) != null ? _c : "browser",
|
|
4802
|
+
...args.excludedPredefinedFunctions != null ? {
|
|
4803
|
+
excludedPredefinedFunctions: args.excludedPredefinedFunctions
|
|
4804
|
+
} : {}
|
|
4805
|
+
});
|
|
4806
|
+
break;
|
|
4807
|
+
}
|
|
4808
|
+
case "google.mcp_server": {
|
|
4809
|
+
interactionsTools.push({
|
|
4810
|
+
type: "mcp_server",
|
|
4811
|
+
...args.name != null ? { name: args.name } : {},
|
|
4812
|
+
...args.url != null ? { url: args.url } : {},
|
|
4813
|
+
...args.headers != null ? { headers: args.headers } : {},
|
|
4814
|
+
...args.allowedTools != null ? { allowed_tools: args.allowedTools } : {}
|
|
4815
|
+
});
|
|
4816
|
+
break;
|
|
4817
|
+
}
|
|
4818
|
+
case "google.retrieval": {
|
|
4819
|
+
const vertexAiSearchConfig = (_d = args.vertexAiSearchConfig) != null ? _d : void 0;
|
|
4820
|
+
interactionsTools.push({
|
|
4821
|
+
type: "retrieval",
|
|
4822
|
+
...args.retrievalTypes != null ? {
|
|
4823
|
+
retrieval_types: args.retrievalTypes
|
|
4824
|
+
} : { retrieval_types: ["vertex_ai_search"] },
|
|
4825
|
+
...vertexAiSearchConfig != null ? { vertex_ai_search_config: vertexAiSearchConfig } : {}
|
|
4826
|
+
});
|
|
4827
|
+
break;
|
|
4828
|
+
}
|
|
4829
|
+
default: {
|
|
4830
|
+
toolWarnings.push({
|
|
4831
|
+
type: "unsupported",
|
|
4832
|
+
feature: `provider-defined tool ${tool.id}`,
|
|
4833
|
+
details: `provider-defined tool ${tool.id} is not supported by google.interactions; tool dropped.`
|
|
4834
|
+
});
|
|
4835
|
+
break;
|
|
4836
|
+
}
|
|
4837
|
+
}
|
|
4838
|
+
continue;
|
|
4839
|
+
}
|
|
4840
|
+
toolWarnings.push({
|
|
4841
|
+
type: "unsupported",
|
|
4842
|
+
feature: `tool of type ${tool.type}`,
|
|
4843
|
+
details: "Only function tools and google.* provider-defined tools are supported by google.interactions; tool dropped."
|
|
4844
|
+
});
|
|
4845
|
+
}
|
|
4846
|
+
const hasFunctionTool = interactionsTools.some((t) => t.type === "function");
|
|
4847
|
+
let mappedToolChoice;
|
|
4848
|
+
if (toolChoice != null && hasFunctionTool) {
|
|
4849
|
+
switch (toolChoice.type) {
|
|
4850
|
+
case "auto":
|
|
4851
|
+
mappedToolChoice = "auto";
|
|
4852
|
+
break;
|
|
4853
|
+
case "required":
|
|
4854
|
+
mappedToolChoice = "any";
|
|
4855
|
+
break;
|
|
4856
|
+
case "none":
|
|
4857
|
+
mappedToolChoice = "none";
|
|
4858
|
+
break;
|
|
4859
|
+
case "tool":
|
|
4860
|
+
mappedToolChoice = {
|
|
4861
|
+
allowed_tools: {
|
|
4862
|
+
mode: "validated",
|
|
4863
|
+
tools: [toolChoice.toolName]
|
|
4864
|
+
}
|
|
4865
|
+
};
|
|
4866
|
+
break;
|
|
4867
|
+
}
|
|
4868
|
+
}
|
|
4869
|
+
return {
|
|
4870
|
+
tools: interactionsTools.length > 0 ? interactionsTools : void 0,
|
|
4871
|
+
toolChoice: mappedToolChoice,
|
|
4872
|
+
toolWarnings
|
|
4873
|
+
};
|
|
4874
|
+
}
|
|
4875
|
+
|
|
4876
|
+
// src/interactions/synthesize-google-interactions-agent-stream.ts
|
|
4877
|
+
function synthesizeGoogleInteractionsAgentStream({
|
|
4878
|
+
response,
|
|
4879
|
+
warnings,
|
|
4880
|
+
generateId: generateId3,
|
|
4881
|
+
includeRawChunks,
|
|
4882
|
+
headerServiceTier
|
|
4883
|
+
}) {
|
|
4884
|
+
return new ReadableStream({
|
|
4885
|
+
start(controller) {
|
|
4886
|
+
var _a, _b, _c;
|
|
4887
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
4888
|
+
const interactionId = typeof response.id === "string" && response.id.length > 0 ? response.id : void 0;
|
|
4889
|
+
let timestamp;
|
|
4890
|
+
const created = response.created;
|
|
4891
|
+
if (typeof created === "string") {
|
|
4892
|
+
const parsed = new Date(created);
|
|
4893
|
+
if (!Number.isNaN(parsed.getTime())) {
|
|
4894
|
+
timestamp = parsed;
|
|
4895
|
+
}
|
|
4896
|
+
}
|
|
4897
|
+
controller.enqueue({
|
|
4898
|
+
type: "response-metadata",
|
|
4899
|
+
...interactionId != null ? { id: interactionId } : {},
|
|
4900
|
+
modelId: (_a = response.model) != null ? _a : void 0,
|
|
4901
|
+
...timestamp ? { timestamp } : {}
|
|
4902
|
+
});
|
|
4903
|
+
if (includeRawChunks) {
|
|
4904
|
+
controller.enqueue({ type: "raw", rawValue: response });
|
|
4905
|
+
}
|
|
4906
|
+
const { content, hasFunctionCall } = parseGoogleInteractionsOutputs({
|
|
4907
|
+
outputs: (_b = response.outputs) != null ? _b : null,
|
|
4908
|
+
generateId: generateId3,
|
|
4909
|
+
interactionId
|
|
4910
|
+
});
|
|
4911
|
+
let blockCounter = 0;
|
|
4912
|
+
const nextBlockId = () => `${interactionId != null ? interactionId : "agent"}:${blockCounter++}`;
|
|
4913
|
+
for (const part of content) {
|
|
4914
|
+
switch (part.type) {
|
|
4915
|
+
case "text": {
|
|
4916
|
+
const id = nextBlockId();
|
|
4917
|
+
const providerMetadata2 = part.providerMetadata;
|
|
4918
|
+
controller.enqueue({ type: "text-start", id });
|
|
4919
|
+
if (part.text.length > 0) {
|
|
4920
|
+
controller.enqueue({ type: "text-delta", id, delta: part.text });
|
|
4921
|
+
}
|
|
4922
|
+
controller.enqueue({
|
|
4923
|
+
type: "text-end",
|
|
4924
|
+
id,
|
|
4925
|
+
...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
|
|
4926
|
+
});
|
|
4927
|
+
break;
|
|
4928
|
+
}
|
|
4929
|
+
case "reasoning": {
|
|
4930
|
+
const id = nextBlockId();
|
|
4931
|
+
const providerMetadata2 = part.providerMetadata;
|
|
4932
|
+
controller.enqueue({ type: "reasoning-start", id });
|
|
4933
|
+
if (part.text.length > 0) {
|
|
4934
|
+
controller.enqueue({
|
|
4935
|
+
type: "reasoning-delta",
|
|
4936
|
+
id,
|
|
4937
|
+
delta: part.text
|
|
4938
|
+
});
|
|
4939
|
+
}
|
|
4940
|
+
controller.enqueue({
|
|
4941
|
+
type: "reasoning-end",
|
|
4942
|
+
id,
|
|
4943
|
+
...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
|
|
4944
|
+
});
|
|
4945
|
+
break;
|
|
4946
|
+
}
|
|
4947
|
+
case "tool-call": {
|
|
4948
|
+
const providerMetadata2 = part.providerMetadata;
|
|
4949
|
+
controller.enqueue({
|
|
4950
|
+
type: "tool-input-start",
|
|
4951
|
+
id: part.toolCallId,
|
|
4952
|
+
toolName: part.toolName,
|
|
4953
|
+
...part.providerExecuted ? { providerExecuted: part.providerExecuted } : {}
|
|
4954
|
+
});
|
|
4955
|
+
controller.enqueue({
|
|
4956
|
+
type: "tool-input-delta",
|
|
4957
|
+
id: part.toolCallId,
|
|
4958
|
+
delta: part.input
|
|
4959
|
+
});
|
|
4960
|
+
controller.enqueue({
|
|
4961
|
+
type: "tool-input-end",
|
|
4962
|
+
id: part.toolCallId
|
|
4963
|
+
});
|
|
4964
|
+
controller.enqueue({
|
|
4965
|
+
type: "tool-call",
|
|
4966
|
+
toolCallId: part.toolCallId,
|
|
4967
|
+
toolName: part.toolName,
|
|
4968
|
+
input: part.input,
|
|
4969
|
+
...part.providerExecuted ? { providerExecuted: part.providerExecuted } : {},
|
|
4970
|
+
...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
|
|
4971
|
+
});
|
|
4972
|
+
break;
|
|
4973
|
+
}
|
|
4974
|
+
case "tool-result": {
|
|
4975
|
+
controller.enqueue({
|
|
4976
|
+
type: "tool-result",
|
|
4977
|
+
toolCallId: part.toolCallId,
|
|
4978
|
+
toolName: part.toolName,
|
|
4979
|
+
result: part.result
|
|
4980
|
+
});
|
|
4981
|
+
break;
|
|
4982
|
+
}
|
|
4983
|
+
case "source":
|
|
4984
|
+
case "file": {
|
|
4985
|
+
controller.enqueue(part);
|
|
4986
|
+
break;
|
|
4987
|
+
}
|
|
4988
|
+
default:
|
|
4989
|
+
break;
|
|
4990
|
+
}
|
|
4991
|
+
}
|
|
4992
|
+
const serviceTier = (_c = response.service_tier) != null ? _c : headerServiceTier;
|
|
4993
|
+
const finishReason = {
|
|
4994
|
+
unified: mapGoogleInteractionsFinishReason({
|
|
4995
|
+
status: response.status,
|
|
4996
|
+
hasFunctionCall
|
|
4997
|
+
}),
|
|
4998
|
+
raw: response.status
|
|
4999
|
+
};
|
|
5000
|
+
const providerMetadata = {
|
|
5001
|
+
google: {
|
|
5002
|
+
...interactionId != null ? { interactionId } : {},
|
|
5003
|
+
...serviceTier != null ? { serviceTier } : {}
|
|
5004
|
+
}
|
|
5005
|
+
};
|
|
5006
|
+
controller.enqueue({
|
|
5007
|
+
type: "finish",
|
|
5008
|
+
finishReason,
|
|
5009
|
+
usage: convertGoogleInteractionsUsage(response.usage),
|
|
5010
|
+
providerMetadata
|
|
5011
|
+
});
|
|
5012
|
+
controller.close();
|
|
5013
|
+
}
|
|
5014
|
+
});
|
|
5015
|
+
}
|
|
5016
|
+
|
|
5017
|
+
// src/interactions/google-interactions-language-model.ts
|
|
5018
|
+
var GoogleInteractionsLanguageModel = class {
|
|
5019
|
+
constructor(modelOrAgent, config) {
|
|
5020
|
+
this.specificationVersion = "v3";
|
|
5021
|
+
if (typeof modelOrAgent === "string") {
|
|
5022
|
+
this.modelId = modelOrAgent;
|
|
5023
|
+
this.agent = void 0;
|
|
5024
|
+
} else {
|
|
5025
|
+
this.modelId = modelOrAgent.agent;
|
|
5026
|
+
this.agent = modelOrAgent.agent;
|
|
5027
|
+
}
|
|
5028
|
+
this.config = config;
|
|
5029
|
+
}
|
|
5030
|
+
get provider() {
|
|
5031
|
+
return this.config.provider;
|
|
5032
|
+
}
|
|
5033
|
+
get supportedUrls() {
|
|
5034
|
+
if (this.config.supportedUrls) {
|
|
5035
|
+
return this.config.supportedUrls();
|
|
5036
|
+
}
|
|
5037
|
+
return {
|
|
5038
|
+
"image/*": [/^https?:\/\/.+/],
|
|
5039
|
+
"application/pdf": [/^https?:\/\/.+/],
|
|
5040
|
+
"audio/*": [/^https?:\/\/.+/],
|
|
5041
|
+
"video/*": [
|
|
5042
|
+
/^https?:\/\/(www\.)?youtube\.com\/watch\?v=.+/,
|
|
5043
|
+
/^https?:\/\/youtu\.be\/.+/,
|
|
5044
|
+
/^gs:\/\/.+/
|
|
5045
|
+
]
|
|
5046
|
+
};
|
|
5047
|
+
}
|
|
5048
|
+
async getArgs(options) {
|
|
5049
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
5050
|
+
const warnings = [];
|
|
5051
|
+
const opts = await parseProviderOptions5({
|
|
5052
|
+
provider: "google",
|
|
5053
|
+
providerOptions: options.providerOptions,
|
|
5054
|
+
schema: googleInteractionsLanguageModelOptions
|
|
5055
|
+
});
|
|
5056
|
+
const isAgent = this.agent != null;
|
|
5057
|
+
const hasTools = options.tools != null && options.tools.length > 0;
|
|
5058
|
+
let toolsForBody;
|
|
5059
|
+
let toolChoiceForBody;
|
|
5060
|
+
if (hasTools && isAgent) {
|
|
5061
|
+
warnings.push({
|
|
5062
|
+
type: "other",
|
|
5063
|
+
message: "google.interactions: tools are not supported when an agent is set; tools will be omitted from the request body."
|
|
5064
|
+
});
|
|
5065
|
+
} else if (hasTools) {
|
|
5066
|
+
const prepared = prepareGoogleInteractionsTools({
|
|
5067
|
+
tools: options.tools,
|
|
5068
|
+
toolChoice: options.toolChoice
|
|
5069
|
+
});
|
|
5070
|
+
toolsForBody = prepared.tools;
|
|
5071
|
+
toolChoiceForBody = prepared.toolChoice;
|
|
5072
|
+
warnings.push(...prepared.toolWarnings);
|
|
5073
|
+
}
|
|
5074
|
+
let responseMimeType;
|
|
5075
|
+
let responseFormat;
|
|
5076
|
+
if (((_a = options.responseFormat) == null ? void 0 : _a.type) === "json") {
|
|
5077
|
+
if (isAgent) {
|
|
5078
|
+
warnings.push({
|
|
5079
|
+
type: "other",
|
|
5080
|
+
message: "google.interactions: structured output (responseFormat) is not supported when an agent is set; responseFormat will be ignored."
|
|
5081
|
+
});
|
|
5082
|
+
} else {
|
|
5083
|
+
responseMimeType = "application/json";
|
|
5084
|
+
if (options.responseFormat.schema != null) {
|
|
5085
|
+
responseFormat = options.responseFormat.schema;
|
|
5086
|
+
}
|
|
5087
|
+
}
|
|
5088
|
+
}
|
|
5089
|
+
const {
|
|
5090
|
+
input,
|
|
5091
|
+
systemInstruction: convertedSystemInstruction,
|
|
5092
|
+
warnings: convWarnings
|
|
5093
|
+
} = convertToGoogleInteractionsInput({
|
|
5094
|
+
prompt: options.prompt,
|
|
5095
|
+
previousInteractionId: (_b = opts == null ? void 0 : opts.previousInteractionId) != null ? _b : void 0,
|
|
5096
|
+
store: (_c = opts == null ? void 0 : opts.store) != null ? _c : void 0,
|
|
5097
|
+
mediaResolution: (_d = opts == null ? void 0 : opts.mediaResolution) != null ? _d : void 0
|
|
5098
|
+
});
|
|
5099
|
+
warnings.push(...convWarnings);
|
|
5100
|
+
let systemInstruction = convertedSystemInstruction;
|
|
5101
|
+
const optionSystemInstruction = (_e = opts == null ? void 0 : opts.systemInstruction) != null ? _e : void 0;
|
|
5102
|
+
if (systemInstruction != null && optionSystemInstruction != null) {
|
|
5103
|
+
warnings.push({
|
|
5104
|
+
type: "other",
|
|
5105
|
+
message: "google.interactions: both AI SDK system message and providerOptions.google.systemInstruction were set; using the AI SDK system message."
|
|
5106
|
+
});
|
|
5107
|
+
} else if (systemInstruction == null && optionSystemInstruction != null) {
|
|
5108
|
+
systemInstruction = optionSystemInstruction;
|
|
5109
|
+
}
|
|
5110
|
+
let generationConfig;
|
|
5111
|
+
if (isAgent) {
|
|
5112
|
+
const droppedFields = [];
|
|
5113
|
+
if (options.temperature != null) droppedFields.push("temperature");
|
|
5114
|
+
if (options.topP != null) droppedFields.push("topP");
|
|
5115
|
+
if (options.seed != null) droppedFields.push("seed");
|
|
5116
|
+
if (options.stopSequences != null && options.stopSequences.length > 0) {
|
|
5117
|
+
droppedFields.push("stopSequences");
|
|
5118
|
+
}
|
|
5119
|
+
if (options.maxOutputTokens != null)
|
|
5120
|
+
droppedFields.push("maxOutputTokens");
|
|
5121
|
+
if ((opts == null ? void 0 : opts.thinkingLevel) != null) droppedFields.push("thinkingLevel");
|
|
5122
|
+
if ((opts == null ? void 0 : opts.thinkingSummaries) != null) {
|
|
5123
|
+
droppedFields.push("thinkingSummaries");
|
|
5124
|
+
}
|
|
5125
|
+
if ((opts == null ? void 0 : opts.imageConfig) != null) droppedFields.push("imageConfig");
|
|
5126
|
+
if (droppedFields.length > 0) {
|
|
5127
|
+
warnings.push({
|
|
5128
|
+
type: "other",
|
|
5129
|
+
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.`
|
|
5130
|
+
});
|
|
5131
|
+
}
|
|
5132
|
+
generationConfig = void 0;
|
|
5133
|
+
} else {
|
|
5134
|
+
generationConfig = pruneUndefined({
|
|
5135
|
+
temperature: (_f = options.temperature) != null ? _f : void 0,
|
|
5136
|
+
top_p: (_g = options.topP) != null ? _g : void 0,
|
|
5137
|
+
seed: (_h = options.seed) != null ? _h : void 0,
|
|
5138
|
+
stop_sequences: options.stopSequences != null && options.stopSequences.length > 0 ? options.stopSequences : void 0,
|
|
5139
|
+
max_output_tokens: (_i = options.maxOutputTokens) != null ? _i : void 0,
|
|
5140
|
+
thinking_level: (_j = opts == null ? void 0 : opts.thinkingLevel) != null ? _j : void 0,
|
|
5141
|
+
thinking_summaries: (_k = opts == null ? void 0 : opts.thinkingSummaries) != null ? _k : void 0,
|
|
5142
|
+
image_config: (opts == null ? void 0 : opts.imageConfig) != null ? pruneUndefined({
|
|
5143
|
+
aspect_ratio: (_l = opts.imageConfig.aspectRatio) != null ? _l : void 0,
|
|
5144
|
+
image_size: (_m = opts.imageConfig.imageSize) != null ? _m : void 0
|
|
5145
|
+
}) : void 0,
|
|
5146
|
+
tool_choice: toolChoiceForBody
|
|
5147
|
+
});
|
|
5148
|
+
}
|
|
5149
|
+
let agentConfig;
|
|
5150
|
+
if (isAgent && (opts == null ? void 0 : opts.agentConfig) != null) {
|
|
5151
|
+
const ac = opts.agentConfig;
|
|
5152
|
+
if (ac.type === "deep-research") {
|
|
5153
|
+
agentConfig = pruneUndefined({
|
|
5154
|
+
type: "deep-research",
|
|
5155
|
+
thinking_summaries: (_n = ac.thinkingSummaries) != null ? _n : void 0,
|
|
5156
|
+
visualization: (_o = ac.visualization) != null ? _o : void 0,
|
|
5157
|
+
collaborative_planning: (_p = ac.collaborativePlanning) != null ? _p : void 0
|
|
5158
|
+
});
|
|
5159
|
+
} else if (ac.type === "dynamic") {
|
|
5160
|
+
agentConfig = { type: "dynamic" };
|
|
5161
|
+
}
|
|
5162
|
+
}
|
|
5163
|
+
const args = pruneUndefined({
|
|
5164
|
+
...isAgent ? { agent: this.agent } : { model: this.modelId },
|
|
5165
|
+
input,
|
|
5166
|
+
system_instruction: systemInstruction,
|
|
5167
|
+
tools: toolsForBody,
|
|
5168
|
+
response_format: responseFormat,
|
|
5169
|
+
response_mime_type: responseMimeType,
|
|
5170
|
+
response_modalities: (opts == null ? void 0 : opts.responseModalities) != null ? opts.responseModalities : void 0,
|
|
5171
|
+
previous_interaction_id: (_q = opts == null ? void 0 : opts.previousInteractionId) != null ? _q : void 0,
|
|
5172
|
+
service_tier: (_r = opts == null ? void 0 : opts.serviceTier) != null ? _r : void 0,
|
|
5173
|
+
store: (_s = opts == null ? void 0 : opts.store) != null ? _s : void 0,
|
|
5174
|
+
generation_config: generationConfig != null && Object.keys(generationConfig).length > 0 ? generationConfig : void 0,
|
|
5175
|
+
agent_config: agentConfig,
|
|
5176
|
+
...isAgent ? { background: true } : {}
|
|
5177
|
+
});
|
|
5178
|
+
return {
|
|
5179
|
+
args,
|
|
5180
|
+
warnings,
|
|
5181
|
+
isAgent,
|
|
5182
|
+
pollingTimeoutMs: (_t = opts == null ? void 0 : opts.pollingTimeoutMs) != null ? _t : void 0
|
|
5183
|
+
};
|
|
5184
|
+
}
|
|
5185
|
+
async doGenerate(options) {
|
|
5186
|
+
var _a, _b, _c, _d, _e, _f;
|
|
5187
|
+
const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
|
|
5188
|
+
const url = `${this.config.baseURL}/interactions`;
|
|
5189
|
+
const mergedHeaders = combineHeaders5(
|
|
5190
|
+
this.config.headers ? await resolve5(this.config.headers) : void 0,
|
|
5191
|
+
options.headers
|
|
5192
|
+
);
|
|
5193
|
+
const postResult = await postJsonToApi5({
|
|
5194
|
+
url,
|
|
5195
|
+
headers: mergedHeaders,
|
|
5196
|
+
body: args,
|
|
5197
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
5198
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
5199
|
+
googleInteractionsResponseSchema
|
|
5200
|
+
),
|
|
5201
|
+
abortSignal: options.abortSignal,
|
|
5202
|
+
fetch: this.config.fetch
|
|
5203
|
+
});
|
|
5204
|
+
let {
|
|
5205
|
+
responseHeaders,
|
|
5206
|
+
value: response,
|
|
5207
|
+
rawValue: rawResponse
|
|
5208
|
+
} = postResult;
|
|
5209
|
+
if (isAgent && !isTerminalStatus(response.status)) {
|
|
5210
|
+
const polled = await pollGoogleInteractionUntilTerminal({
|
|
5211
|
+
baseURL: this.config.baseURL,
|
|
5212
|
+
interactionId: response.id,
|
|
5213
|
+
headers: mergedHeaders,
|
|
5214
|
+
fetch: this.config.fetch,
|
|
5215
|
+
abortSignal: options.abortSignal,
|
|
5216
|
+
timeoutMs: pollingTimeoutMs
|
|
5217
|
+
});
|
|
5218
|
+
response = polled.response;
|
|
5219
|
+
rawResponse = polled.rawResponse;
|
|
5220
|
+
responseHeaders = (_a = polled.responseHeaders) != null ? _a : responseHeaders;
|
|
5221
|
+
}
|
|
5222
|
+
const interactionId = typeof response.id === "string" && response.id.length > 0 ? response.id : void 0;
|
|
5223
|
+
const { content, hasFunctionCall } = parseGoogleInteractionsOutputs({
|
|
5224
|
+
outputs: (_b = response.outputs) != null ? _b : null,
|
|
5225
|
+
generateId: (_c = this.config.generateId) != null ? _c : defaultGenerateId2,
|
|
5226
|
+
interactionId
|
|
5227
|
+
});
|
|
5228
|
+
const finishReason = {
|
|
5229
|
+
unified: mapGoogleInteractionsFinishReason({
|
|
5230
|
+
status: response.status,
|
|
5231
|
+
hasFunctionCall
|
|
5232
|
+
}),
|
|
5233
|
+
raw: response.status
|
|
5234
|
+
};
|
|
5235
|
+
const serviceTier = (_e = (_d = response.service_tier) != null ? _d : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _e : void 0;
|
|
5236
|
+
const providerMetadata = {
|
|
5237
|
+
google: {
|
|
5238
|
+
...interactionId != null ? { interactionId } : {},
|
|
5239
|
+
...serviceTier != null ? { serviceTier } : {}
|
|
5240
|
+
}
|
|
5241
|
+
};
|
|
5242
|
+
let timestamp;
|
|
5243
|
+
if (typeof response.created === "string") {
|
|
5244
|
+
const parsed = new Date(response.created);
|
|
5245
|
+
if (!Number.isNaN(parsed.getTime())) {
|
|
5246
|
+
timestamp = parsed;
|
|
5247
|
+
}
|
|
5248
|
+
}
|
|
5249
|
+
return {
|
|
5250
|
+
content,
|
|
5251
|
+
finishReason,
|
|
5252
|
+
usage: convertGoogleInteractionsUsage(response.usage),
|
|
5253
|
+
warnings,
|
|
5254
|
+
providerMetadata,
|
|
5255
|
+
request: { body: args },
|
|
5256
|
+
response: {
|
|
5257
|
+
headers: responseHeaders,
|
|
5258
|
+
body: rawResponse,
|
|
5259
|
+
...interactionId != null ? { id: interactionId } : {},
|
|
5260
|
+
...timestamp ? { timestamp } : {},
|
|
5261
|
+
modelId: (_f = response.model) != null ? _f : void 0
|
|
5262
|
+
}
|
|
5263
|
+
};
|
|
5264
|
+
}
|
|
5265
|
+
async doStream(options) {
|
|
5266
|
+
var _a;
|
|
5267
|
+
const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
|
|
5268
|
+
const url = `${this.config.baseURL}/interactions`;
|
|
5269
|
+
const mergedHeaders = combineHeaders5(
|
|
5270
|
+
this.config.headers ? await resolve5(this.config.headers) : void 0,
|
|
5271
|
+
options.headers
|
|
5272
|
+
);
|
|
5273
|
+
if (isAgent) {
|
|
5274
|
+
return this.doStreamAgent({
|
|
5275
|
+
args,
|
|
5276
|
+
warnings,
|
|
5277
|
+
url,
|
|
5278
|
+
mergedHeaders,
|
|
5279
|
+
options,
|
|
5280
|
+
pollingTimeoutMs
|
|
5281
|
+
});
|
|
5282
|
+
}
|
|
5283
|
+
const body = { ...args, stream: true };
|
|
5284
|
+
const { responseHeaders, value: response } = await postJsonToApi5({
|
|
5285
|
+
url,
|
|
5286
|
+
headers: mergedHeaders,
|
|
5287
|
+
body,
|
|
5288
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
5289
|
+
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
5290
|
+
googleInteractionsEventSchema
|
|
5291
|
+
),
|
|
5292
|
+
abortSignal: options.abortSignal,
|
|
5293
|
+
fetch: this.config.fetch
|
|
5294
|
+
});
|
|
5295
|
+
const headerServiceTier = responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"];
|
|
5296
|
+
const transform = buildGoogleInteractionsStreamTransform({
|
|
5297
|
+
warnings,
|
|
5298
|
+
generateId: (_a = this.config.generateId) != null ? _a : defaultGenerateId2,
|
|
5299
|
+
includeRawChunks: options.includeRawChunks,
|
|
5300
|
+
serviceTier: headerServiceTier
|
|
5301
|
+
});
|
|
5302
|
+
return {
|
|
5303
|
+
stream: response.pipeThrough(transform),
|
|
5304
|
+
request: { body },
|
|
5305
|
+
response: { headers: responseHeaders }
|
|
5306
|
+
};
|
|
5307
|
+
}
|
|
5308
|
+
/*
|
|
5309
|
+
* Drive the streaming surface for agent calls. Agent calls require
|
|
5310
|
+
* `background: true`, which is incompatible with `stream: true` on POST.
|
|
5311
|
+
*
|
|
5312
|
+
* In principle the API also exposes `GET /interactions/{id}?stream=true`
|
|
5313
|
+
* to replay events as the agent runs. In practice the connection is
|
|
5314
|
+
* idle for long stretches while the agent thinks (deep-research can run
|
|
5315
|
+
* for a minute or more between SSE events), and undici's default body
|
|
5316
|
+
* timeout terminates the request mid-flight with `UND_ERR_BODY_TIMEOUT`.
|
|
5317
|
+
* Tuning the timeout per-call would require the caller to thread an
|
|
5318
|
+
* `undici.Agent` through `fetch`, which contradicts the AI SDK's
|
|
5319
|
+
* pluggable-fetch contract.
|
|
5320
|
+
*
|
|
5321
|
+
* We therefore drive `doStream` exactly like `doGenerate` for agents:
|
|
5322
|
+
* POST with `background: true`, poll `GET /interactions/{id}` until
|
|
5323
|
+
* terminal, then synthesize the stream from the final outputs. The
|
|
5324
|
+
* user-facing surface stays identical -- text-start / text-delta /
|
|
5325
|
+
* text-end / finish parts arrive in the same order as a true SSE
|
|
5326
|
+
* response, just buffered until the agent completes.
|
|
5327
|
+
*/
|
|
5328
|
+
async doStreamAgent({
|
|
5329
|
+
args,
|
|
5330
|
+
warnings,
|
|
5331
|
+
url,
|
|
5332
|
+
mergedHeaders,
|
|
5333
|
+
options,
|
|
5334
|
+
pollingTimeoutMs
|
|
5335
|
+
}) {
|
|
5336
|
+
var _a, _b;
|
|
5337
|
+
const postResult = await postJsonToApi5({
|
|
5338
|
+
url,
|
|
5339
|
+
headers: mergedHeaders,
|
|
5340
|
+
body: args,
|
|
5341
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
5342
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
5343
|
+
googleInteractionsResponseSchema
|
|
5344
|
+
),
|
|
5345
|
+
abortSignal: options.abortSignal,
|
|
5346
|
+
fetch: this.config.fetch
|
|
5347
|
+
});
|
|
5348
|
+
let { responseHeaders: postHeaders, value: postResponse } = postResult;
|
|
5349
|
+
const interactionId = postResponse.id;
|
|
5350
|
+
if (interactionId == null || interactionId.length === 0) {
|
|
5351
|
+
throw new Error(
|
|
5352
|
+
"google.interactions: agent POST response did not include an interaction id; cannot poll for the agent result."
|
|
5353
|
+
);
|
|
5354
|
+
}
|
|
5355
|
+
if (!isTerminalStatus(postResponse.status)) {
|
|
5356
|
+
const polled = await pollGoogleInteractionUntilTerminal({
|
|
5357
|
+
baseURL: this.config.baseURL,
|
|
5358
|
+
interactionId,
|
|
5359
|
+
headers: mergedHeaders,
|
|
5360
|
+
fetch: this.config.fetch,
|
|
5361
|
+
abortSignal: options.abortSignal,
|
|
5362
|
+
timeoutMs: pollingTimeoutMs
|
|
5363
|
+
});
|
|
5364
|
+
postResponse = polled.response;
|
|
5365
|
+
postHeaders = (_a = polled.responseHeaders) != null ? _a : postHeaders;
|
|
5366
|
+
}
|
|
5367
|
+
const stream = synthesizeGoogleInteractionsAgentStream({
|
|
5368
|
+
response: postResponse,
|
|
5369
|
+
warnings,
|
|
5370
|
+
generateId: (_b = this.config.generateId) != null ? _b : defaultGenerateId2,
|
|
5371
|
+
includeRawChunks: options.includeRawChunks,
|
|
5372
|
+
headerServiceTier: postHeaders == null ? void 0 : postHeaders["x-gemini-service-tier"]
|
|
5373
|
+
});
|
|
5374
|
+
return {
|
|
5375
|
+
stream,
|
|
5376
|
+
request: { body: args },
|
|
5377
|
+
response: { headers: postHeaders }
|
|
5378
|
+
};
|
|
5379
|
+
}
|
|
5380
|
+
};
|
|
5381
|
+
function pruneUndefined(obj) {
|
|
5382
|
+
const result = {};
|
|
5383
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
5384
|
+
if (value === void 0) continue;
|
|
5385
|
+
result[key] = value;
|
|
5386
|
+
}
|
|
5387
|
+
return result;
|
|
5388
|
+
}
|
|
5389
|
+
|
|
3052
5390
|
// src/google-provider.ts
|
|
3053
5391
|
function createGoogleGenerativeAI(options = {}) {
|
|
3054
5392
|
var _a, _b;
|
|
@@ -3109,6 +5447,19 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
3109
5447
|
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2
|
|
3110
5448
|
});
|
|
3111
5449
|
};
|
|
5450
|
+
const createInteractionsModel = (modelIdOrAgent) => {
|
|
5451
|
+
var _a2;
|
|
5452
|
+
return new GoogleInteractionsLanguageModel(
|
|
5453
|
+
modelIdOrAgent,
|
|
5454
|
+
{
|
|
5455
|
+
provider: `${providerName}.interactions`,
|
|
5456
|
+
baseURL,
|
|
5457
|
+
headers: getHeaders,
|
|
5458
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
|
|
5459
|
+
fetch: options.fetch
|
|
5460
|
+
}
|
|
5461
|
+
);
|
|
5462
|
+
};
|
|
3112
5463
|
const provider = function(modelId) {
|
|
3113
5464
|
if (new.target) {
|
|
3114
5465
|
throw new Error(
|
|
@@ -3129,6 +5480,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
3129
5480
|
provider.imageModel = createImageModel;
|
|
3130
5481
|
provider.video = createVideoModel;
|
|
3131
5482
|
provider.videoModel = createVideoModel;
|
|
5483
|
+
provider.interactions = createInteractionsModel;
|
|
3132
5484
|
provider.tools = googleTools;
|
|
3133
5485
|
return provider;
|
|
3134
5486
|
}
|