@ai-sdk/google 3.0.71 → 3.0.72
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 +6 -0
- package/dist/index.js +294 -82
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +290 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/interactions/cancel-google-interaction.ts +60 -0
- package/src/interactions/google-interactions-language-model.ts +68 -42
- package/src/interactions/poll-google-interactions.ts +47 -28
- package/src/interactions/stream-google-interactions.ts +239 -0
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -27,10 +27,10 @@ __export(index_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
29
|
// src/google-provider.ts
|
|
30
|
-
var
|
|
30
|
+
var import_provider_utils23 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/version.ts
|
|
33
|
-
var VERSION = true ? "3.0.
|
|
33
|
+
var VERSION = true ? "3.0.72" : "0.0.0-test";
|
|
34
34
|
|
|
35
35
|
// src/google-generative-ai-embedding-model.ts
|
|
36
36
|
var import_provider = require("@ai-sdk/provider");
|
|
@@ -3023,7 +3023,7 @@ var googleVideoModelOptionsSchema = (0, import_provider_utils15.lazySchema)(
|
|
|
3023
3023
|
);
|
|
3024
3024
|
|
|
3025
3025
|
// src/interactions/google-interactions-language-model.ts
|
|
3026
|
-
var
|
|
3026
|
+
var import_provider_utils22 = require("@ai-sdk/provider-utils");
|
|
3027
3027
|
|
|
3028
3028
|
// src/interactions/convert-google-interactions-usage.ts
|
|
3029
3029
|
function convertGoogleInteractionsUsage(usage) {
|
|
@@ -4632,7 +4632,39 @@ function parseGoogleInteractionsOutputs({
|
|
|
4632
4632
|
}
|
|
4633
4633
|
|
|
4634
4634
|
// src/interactions/poll-google-interactions.ts
|
|
4635
|
+
var import_provider_utils20 = require("@ai-sdk/provider-utils");
|
|
4636
|
+
|
|
4637
|
+
// src/interactions/cancel-google-interaction.ts
|
|
4635
4638
|
var import_provider_utils19 = require("@ai-sdk/provider-utils");
|
|
4639
|
+
var getOriginalFetch = () => globalThis.fetch;
|
|
4640
|
+
async function cancelGoogleInteraction({
|
|
4641
|
+
baseURL,
|
|
4642
|
+
interactionId,
|
|
4643
|
+
headers,
|
|
4644
|
+
fetch = getOriginalFetch()
|
|
4645
|
+
}) {
|
|
4646
|
+
if (interactionId == null || interactionId.length === 0) {
|
|
4647
|
+
return;
|
|
4648
|
+
}
|
|
4649
|
+
const url = `${baseURL}/interactions/${encodeURIComponent(interactionId)}/cancel`;
|
|
4650
|
+
try {
|
|
4651
|
+
const response = await fetch(url, {
|
|
4652
|
+
method: "POST",
|
|
4653
|
+
headers: (0, import_provider_utils19.withUserAgentSuffix)(
|
|
4654
|
+
(0, import_provider_utils19.combineHeaders)({ "Content-Type": "application/json" }, headers),
|
|
4655
|
+
(0, import_provider_utils19.getRuntimeEnvironmentUserAgent)()
|
|
4656
|
+
),
|
|
4657
|
+
body: "{}"
|
|
4658
|
+
});
|
|
4659
|
+
try {
|
|
4660
|
+
await response.text();
|
|
4661
|
+
} catch (e) {
|
|
4662
|
+
}
|
|
4663
|
+
} catch (e) {
|
|
4664
|
+
}
|
|
4665
|
+
}
|
|
4666
|
+
|
|
4667
|
+
// src/interactions/poll-google-interactions.ts
|
|
4636
4668
|
var TERMINAL_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "incomplete"]);
|
|
4637
4669
|
function isTerminalStatus(status) {
|
|
4638
4670
|
return status != null && TERMINAL_STATUSES.has(status);
|
|
@@ -4658,34 +4690,43 @@ async function pollGoogleInteractionUntilTerminal({
|
|
|
4658
4690
|
const startedAt = Date.now();
|
|
4659
4691
|
let nextDelayMs = initialDelayMs;
|
|
4660
4692
|
const url = `${baseURL}/interactions/${encodeURIComponent(interactionId)}`;
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
)
|
|
4693
|
+
const cancelOnServer = () => cancelGoogleInteraction({ baseURL, interactionId, headers, fetch });
|
|
4694
|
+
try {
|
|
4695
|
+
while (true) {
|
|
4696
|
+
if (abortSignal == null ? void 0 : abortSignal.aborted) {
|
|
4697
|
+
await cancelOnServer();
|
|
4698
|
+
throw new DOMException("Polling was aborted", "AbortError");
|
|
4699
|
+
}
|
|
4700
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
4701
|
+
throw new Error(
|
|
4702
|
+
`google.interactions: timed out polling interaction ${interactionId} after ${timeoutMs}ms.`
|
|
4703
|
+
);
|
|
4704
|
+
}
|
|
4705
|
+
await (0, import_provider_utils20.delay)(nextDelayMs, { abortSignal });
|
|
4706
|
+
const {
|
|
4707
|
+
value: response,
|
|
4708
|
+
rawValue: rawResponse,
|
|
4709
|
+
responseHeaders
|
|
4710
|
+
} = await (0, import_provider_utils20.getFromApi)({
|
|
4711
|
+
url,
|
|
4712
|
+
headers,
|
|
4713
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
4714
|
+
successfulResponseHandler: (0, import_provider_utils20.createJsonResponseHandler)(
|
|
4715
|
+
googleInteractionsResponseSchema
|
|
4716
|
+
),
|
|
4717
|
+
abortSignal,
|
|
4718
|
+
fetch
|
|
4719
|
+
});
|
|
4720
|
+
if (isTerminalStatus(response.status)) {
|
|
4721
|
+
return { response, rawResponse, responseHeaders };
|
|
4722
|
+
}
|
|
4723
|
+
nextDelayMs = Math.min(nextDelayMs * 2, maxDelayMs);
|
|
4669
4724
|
}
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
rawValue: rawResponse,
|
|
4674
|
-
responseHeaders
|
|
4675
|
-
} = await (0, import_provider_utils19.getFromApi)({
|
|
4676
|
-
url,
|
|
4677
|
-
headers,
|
|
4678
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
4679
|
-
successfulResponseHandler: (0, import_provider_utils19.createJsonResponseHandler)(
|
|
4680
|
-
googleInteractionsResponseSchema
|
|
4681
|
-
),
|
|
4682
|
-
abortSignal,
|
|
4683
|
-
fetch
|
|
4684
|
-
});
|
|
4685
|
-
if (isTerminalStatus(response.status)) {
|
|
4686
|
-
return { response, rawResponse, responseHeaders };
|
|
4725
|
+
} catch (error) {
|
|
4726
|
+
if ((0, import_provider_utils20.isAbortError)(error)) {
|
|
4727
|
+
await cancelOnServer();
|
|
4687
4728
|
}
|
|
4688
|
-
|
|
4729
|
+
throw error;
|
|
4689
4730
|
}
|
|
4690
4731
|
}
|
|
4691
4732
|
|
|
@@ -4837,6 +4878,169 @@ function prepareGoogleInteractionsTools({
|
|
|
4837
4878
|
};
|
|
4838
4879
|
}
|
|
4839
4880
|
|
|
4881
|
+
// src/interactions/stream-google-interactions.ts
|
|
4882
|
+
var import_provider_utils21 = require("@ai-sdk/provider-utils");
|
|
4883
|
+
var DEFAULT_MAX_RETRIES = 3;
|
|
4884
|
+
var DEFAULT_RETRY_DELAY_MS = 500;
|
|
4885
|
+
function streamGoogleInteractionEvents({
|
|
4886
|
+
baseURL,
|
|
4887
|
+
interactionId,
|
|
4888
|
+
headers,
|
|
4889
|
+
fetch,
|
|
4890
|
+
abortSignal,
|
|
4891
|
+
maxRetries = DEFAULT_MAX_RETRIES,
|
|
4892
|
+
retryDelayMs = DEFAULT_RETRY_DELAY_MS
|
|
4893
|
+
}) {
|
|
4894
|
+
if (interactionId.length === 0) {
|
|
4895
|
+
throw new Error(
|
|
4896
|
+
"google.interactions: cannot stream a background interaction without an id."
|
|
4897
|
+
);
|
|
4898
|
+
}
|
|
4899
|
+
const eventSourceHeaders = {
|
|
4900
|
+
...headers,
|
|
4901
|
+
accept: "text/event-stream"
|
|
4902
|
+
};
|
|
4903
|
+
let lastEventId;
|
|
4904
|
+
let complete = false;
|
|
4905
|
+
let attempt = 0;
|
|
4906
|
+
let receivedAnyEventThisAttempt = false;
|
|
4907
|
+
let currentReader;
|
|
4908
|
+
const internalAbort = new AbortController();
|
|
4909
|
+
const upstreamAbortHandler = () => internalAbort.abort();
|
|
4910
|
+
if (abortSignal != null) {
|
|
4911
|
+
if (abortSignal.aborted) {
|
|
4912
|
+
internalAbort.abort();
|
|
4913
|
+
} else {
|
|
4914
|
+
abortSignal.addEventListener("abort", upstreamAbortHandler, {
|
|
4915
|
+
once: true
|
|
4916
|
+
});
|
|
4917
|
+
}
|
|
4918
|
+
}
|
|
4919
|
+
const effectiveSignal = internalAbort.signal;
|
|
4920
|
+
function buildUrl() {
|
|
4921
|
+
const base = `${baseURL}/interactions/${encodeURIComponent(interactionId)}`;
|
|
4922
|
+
const params = new URLSearchParams({ stream: "true" });
|
|
4923
|
+
if (lastEventId != null) {
|
|
4924
|
+
params.set("last_event_id", lastEventId);
|
|
4925
|
+
}
|
|
4926
|
+
return `${base}?${params.toString()}`;
|
|
4927
|
+
}
|
|
4928
|
+
async function openReader() {
|
|
4929
|
+
const { value: stream } = await (0, import_provider_utils21.getFromApi)({
|
|
4930
|
+
url: buildUrl(),
|
|
4931
|
+
headers: eventSourceHeaders,
|
|
4932
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
4933
|
+
successfulResponseHandler: (0, import_provider_utils21.createEventSourceResponseHandler)(
|
|
4934
|
+
googleInteractionsEventSchema
|
|
4935
|
+
),
|
|
4936
|
+
abortSignal: effectiveSignal,
|
|
4937
|
+
fetch
|
|
4938
|
+
});
|
|
4939
|
+
return stream.getReader();
|
|
4940
|
+
}
|
|
4941
|
+
return new ReadableStream({
|
|
4942
|
+
async start(controller) {
|
|
4943
|
+
try {
|
|
4944
|
+
while (!complete && !effectiveSignal.aborted) {
|
|
4945
|
+
if (currentReader == null) {
|
|
4946
|
+
try {
|
|
4947
|
+
currentReader = await openReader();
|
|
4948
|
+
receivedAnyEventThisAttempt = false;
|
|
4949
|
+
} catch (error) {
|
|
4950
|
+
if ((0, import_provider_utils21.isAbortError)(error) || effectiveSignal.aborted) {
|
|
4951
|
+
controller.error(error);
|
|
4952
|
+
return;
|
|
4953
|
+
}
|
|
4954
|
+
attempt++;
|
|
4955
|
+
if (attempt >= maxRetries) {
|
|
4956
|
+
controller.error(error);
|
|
4957
|
+
return;
|
|
4958
|
+
}
|
|
4959
|
+
await (0, import_provider_utils21.delay)(retryDelayMs * attempt, {
|
|
4960
|
+
abortSignal: effectiveSignal
|
|
4961
|
+
});
|
|
4962
|
+
continue;
|
|
4963
|
+
}
|
|
4964
|
+
}
|
|
4965
|
+
try {
|
|
4966
|
+
const { done, value } = await currentReader.read();
|
|
4967
|
+
if (done) {
|
|
4968
|
+
currentReader = void 0;
|
|
4969
|
+
if (complete) break;
|
|
4970
|
+
if (!receivedAnyEventThisAttempt) {
|
|
4971
|
+
attempt++;
|
|
4972
|
+
if (attempt >= maxRetries) {
|
|
4973
|
+
controller.error(
|
|
4974
|
+
new Error(
|
|
4975
|
+
"google.interactions: SSE stream closed without producing any events."
|
|
4976
|
+
)
|
|
4977
|
+
);
|
|
4978
|
+
return;
|
|
4979
|
+
}
|
|
4980
|
+
await (0, import_provider_utils21.delay)(retryDelayMs * attempt, {
|
|
4981
|
+
abortSignal: effectiveSignal
|
|
4982
|
+
});
|
|
4983
|
+
} else {
|
|
4984
|
+
attempt = 0;
|
|
4985
|
+
}
|
|
4986
|
+
continue;
|
|
4987
|
+
}
|
|
4988
|
+
receivedAnyEventThisAttempt = true;
|
|
4989
|
+
if (value.success) {
|
|
4990
|
+
const ev = value.value;
|
|
4991
|
+
if (typeof ev.event_id === "string" && ev.event_id.length > 0) {
|
|
4992
|
+
lastEventId = ev.event_id;
|
|
4993
|
+
}
|
|
4994
|
+
if (ev.event_type === "interaction.complete" || ev.event_type === "error") {
|
|
4995
|
+
complete = true;
|
|
4996
|
+
}
|
|
4997
|
+
}
|
|
4998
|
+
controller.enqueue(value);
|
|
4999
|
+
} catch (error) {
|
|
5000
|
+
if ((0, import_provider_utils21.isAbortError)(error) || effectiveSignal.aborted) {
|
|
5001
|
+
controller.error(error);
|
|
5002
|
+
return;
|
|
5003
|
+
}
|
|
5004
|
+
currentReader = void 0;
|
|
5005
|
+
attempt++;
|
|
5006
|
+
if (attempt >= maxRetries) {
|
|
5007
|
+
controller.error(error);
|
|
5008
|
+
return;
|
|
5009
|
+
}
|
|
5010
|
+
await (0, import_provider_utils21.delay)(retryDelayMs * attempt, {
|
|
5011
|
+
abortSignal: effectiveSignal
|
|
5012
|
+
});
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
controller.close();
|
|
5016
|
+
} catch (error) {
|
|
5017
|
+
controller.error(error);
|
|
5018
|
+
} finally {
|
|
5019
|
+
if (abortSignal != null) {
|
|
5020
|
+
abortSignal.removeEventListener("abort", upstreamAbortHandler);
|
|
5021
|
+
}
|
|
5022
|
+
currentReader == null ? void 0 : currentReader.cancel().catch(() => {
|
|
5023
|
+
});
|
|
5024
|
+
currentReader = void 0;
|
|
5025
|
+
if (effectiveSignal.aborted && !complete) {
|
|
5026
|
+
await cancelGoogleInteraction({
|
|
5027
|
+
baseURL,
|
|
5028
|
+
interactionId,
|
|
5029
|
+
headers,
|
|
5030
|
+
fetch
|
|
5031
|
+
});
|
|
5032
|
+
}
|
|
5033
|
+
}
|
|
5034
|
+
},
|
|
5035
|
+
cancel() {
|
|
5036
|
+
internalAbort.abort();
|
|
5037
|
+
currentReader == null ? void 0 : currentReader.cancel().catch(() => {
|
|
5038
|
+
});
|
|
5039
|
+
currentReader = void 0;
|
|
5040
|
+
}
|
|
5041
|
+
});
|
|
5042
|
+
}
|
|
5043
|
+
|
|
4840
5044
|
// src/interactions/synthesize-google-interactions-agent-stream.ts
|
|
4841
5045
|
function synthesizeGoogleInteractionsAgentStream({
|
|
4842
5046
|
response,
|
|
@@ -5012,7 +5216,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5012
5216
|
async getArgs(options) {
|
|
5013
5217
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
5014
5218
|
const warnings = [];
|
|
5015
|
-
const opts = await (0,
|
|
5219
|
+
const opts = await (0, import_provider_utils22.parseProviderOptions)({
|
|
5016
5220
|
provider: "google",
|
|
5017
5221
|
providerOptions: options.providerOptions,
|
|
5018
5222
|
schema: googleInteractionsLanguageModelOptions
|
|
@@ -5150,16 +5354,16 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5150
5354
|
var _a, _b, _c, _d, _e, _f;
|
|
5151
5355
|
const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
|
|
5152
5356
|
const url = `${this.config.baseURL}/interactions`;
|
|
5153
|
-
const mergedHeaders = (0,
|
|
5154
|
-
this.config.headers ? await (0,
|
|
5357
|
+
const mergedHeaders = (0, import_provider_utils22.combineHeaders)(
|
|
5358
|
+
this.config.headers ? await (0, import_provider_utils22.resolve)(this.config.headers) : void 0,
|
|
5155
5359
|
options.headers
|
|
5156
5360
|
);
|
|
5157
|
-
const postResult = await (0,
|
|
5361
|
+
const postResult = await (0, import_provider_utils22.postJsonToApi)({
|
|
5158
5362
|
url,
|
|
5159
5363
|
headers: mergedHeaders,
|
|
5160
5364
|
body: args,
|
|
5161
5365
|
failedResponseHandler: googleFailedResponseHandler,
|
|
5162
|
-
successfulResponseHandler: (0,
|
|
5366
|
+
successfulResponseHandler: (0, import_provider_utils22.createJsonResponseHandler)(
|
|
5163
5367
|
googleInteractionsResponseSchema
|
|
5164
5368
|
),
|
|
5165
5369
|
abortSignal: options.abortSignal,
|
|
@@ -5186,7 +5390,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5186
5390
|
const interactionId = typeof response.id === "string" && response.id.length > 0 ? response.id : void 0;
|
|
5187
5391
|
const { content, hasFunctionCall } = parseGoogleInteractionsOutputs({
|
|
5188
5392
|
outputs: (_b = response.outputs) != null ? _b : null,
|
|
5189
|
-
generateId: (_c = this.config.generateId) != null ? _c :
|
|
5393
|
+
generateId: (_c = this.config.generateId) != null ? _c : import_provider_utils22.generateId,
|
|
5190
5394
|
interactionId
|
|
5191
5395
|
});
|
|
5192
5396
|
const finishReason = {
|
|
@@ -5230,12 +5434,12 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5230
5434
|
var _a;
|
|
5231
5435
|
const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
|
|
5232
5436
|
const url = `${this.config.baseURL}/interactions`;
|
|
5233
|
-
const mergedHeaders = (0,
|
|
5234
|
-
this.config.headers ? await (0,
|
|
5437
|
+
const mergedHeaders = (0, import_provider_utils22.combineHeaders)(
|
|
5438
|
+
this.config.headers ? await (0, import_provider_utils22.resolve)(this.config.headers) : void 0,
|
|
5235
5439
|
options.headers
|
|
5236
5440
|
);
|
|
5237
5441
|
if (isAgent) {
|
|
5238
|
-
return this.
|
|
5442
|
+
return this.doStreamBackground({
|
|
5239
5443
|
args,
|
|
5240
5444
|
warnings,
|
|
5241
5445
|
url,
|
|
@@ -5245,12 +5449,12 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5245
5449
|
});
|
|
5246
5450
|
}
|
|
5247
5451
|
const body = { ...args, stream: true };
|
|
5248
|
-
const { responseHeaders, value: response } = await (0,
|
|
5452
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils22.postJsonToApi)({
|
|
5249
5453
|
url,
|
|
5250
5454
|
headers: mergedHeaders,
|
|
5251
5455
|
body,
|
|
5252
5456
|
failedResponseHandler: googleFailedResponseHandler,
|
|
5253
|
-
successfulResponseHandler: (0,
|
|
5457
|
+
successfulResponseHandler: (0, import_provider_utils22.createEventSourceResponseHandler)(
|
|
5254
5458
|
googleInteractionsEventSchema
|
|
5255
5459
|
),
|
|
5256
5460
|
abortSignal: options.abortSignal,
|
|
@@ -5259,7 +5463,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5259
5463
|
const headerServiceTier = responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"];
|
|
5260
5464
|
const transform = buildGoogleInteractionsStreamTransform({
|
|
5261
5465
|
warnings,
|
|
5262
|
-
generateId: (_a = this.config.generateId) != null ? _a :
|
|
5466
|
+
generateId: (_a = this.config.generateId) != null ? _a : import_provider_utils22.generateId,
|
|
5263
5467
|
includeRawChunks: options.includeRawChunks,
|
|
5264
5468
|
serviceTier: headerServiceTier
|
|
5265
5469
|
});
|
|
@@ -5270,26 +5474,24 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5270
5474
|
};
|
|
5271
5475
|
}
|
|
5272
5476
|
/*
|
|
5273
|
-
* Drive the streaming surface for agent calls.
|
|
5477
|
+
* Drive the streaming surface for agent calls. Agents require
|
|
5274
5478
|
* `background: true`, which is incompatible with `stream: true` on POST.
|
|
5275
5479
|
*
|
|
5276
|
-
*
|
|
5277
|
-
*
|
|
5278
|
-
*
|
|
5279
|
-
*
|
|
5280
|
-
*
|
|
5281
|
-
*
|
|
5282
|
-
*
|
|
5283
|
-
*
|
|
5480
|
+
* Approach:
|
|
5481
|
+
* 1. POST `/interactions` with `background: true`. The response includes
|
|
5482
|
+
* the interaction id and an initial (usually non-terminal) status.
|
|
5483
|
+
* 2. If the POST status is already terminal (rare), synthesize a stream
|
|
5484
|
+
* from the polled outputs and we're done.
|
|
5485
|
+
* 3. Otherwise open `GET /interactions/{id}?stream=true` and pipe the
|
|
5486
|
+
* SSE events through `buildGoogleInteractionsStreamTransform` so the
|
|
5487
|
+
* consumer receives text deltas / thinking summaries / tool events as
|
|
5488
|
+
* they happen instead of all at once at the end.
|
|
5284
5489
|
*
|
|
5285
|
-
*
|
|
5286
|
-
*
|
|
5287
|
-
*
|
|
5288
|
-
* user-facing surface stays identical -- text-start / text-delta /
|
|
5289
|
-
* text-end / finish parts arrive in the same order as a true SSE
|
|
5290
|
-
* response, just buffered until the agent completes.
|
|
5490
|
+
* The SSE connection can drop while the agent idles between events
|
|
5491
|
+
* (`UND_ERR_BODY_TIMEOUT`); `streamGoogleInteractionEvents` handles the
|
|
5492
|
+
* reconnect-with-`last_event_id` loop transparently.
|
|
5291
5493
|
*/
|
|
5292
|
-
async
|
|
5494
|
+
async doStreamBackground({
|
|
5293
5495
|
args,
|
|
5294
5496
|
warnings,
|
|
5295
5497
|
url,
|
|
@@ -5298,45 +5500,55 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5298
5500
|
pollingTimeoutMs
|
|
5299
5501
|
}) {
|
|
5300
5502
|
var _a, _b;
|
|
5301
|
-
const postResult = await (0,
|
|
5503
|
+
const postResult = await (0, import_provider_utils22.postJsonToApi)({
|
|
5302
5504
|
url,
|
|
5303
5505
|
headers: mergedHeaders,
|
|
5304
5506
|
body: args,
|
|
5305
5507
|
failedResponseHandler: googleFailedResponseHandler,
|
|
5306
|
-
successfulResponseHandler: (0,
|
|
5508
|
+
successfulResponseHandler: (0, import_provider_utils22.createJsonResponseHandler)(
|
|
5307
5509
|
googleInteractionsResponseSchema
|
|
5308
5510
|
),
|
|
5309
5511
|
abortSignal: options.abortSignal,
|
|
5310
5512
|
fetch: this.config.fetch
|
|
5311
5513
|
});
|
|
5312
|
-
|
|
5514
|
+
const { responseHeaders: postHeaders, value: postResponse } = postResult;
|
|
5313
5515
|
const interactionId = postResponse.id;
|
|
5314
5516
|
if (interactionId == null || interactionId.length === 0) {
|
|
5315
5517
|
throw new Error(
|
|
5316
|
-
"google.interactions:
|
|
5518
|
+
"google.interactions: background POST response did not include an interaction id; cannot stream the result."
|
|
5317
5519
|
);
|
|
5318
5520
|
}
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
|
|
5521
|
+
const headerServiceTier = postHeaders == null ? void 0 : postHeaders["x-gemini-service-tier"];
|
|
5522
|
+
if (isTerminalStatus(postResponse.status)) {
|
|
5523
|
+
const synthesized = synthesizeGoogleInteractionsAgentStream({
|
|
5524
|
+
response: postResponse,
|
|
5525
|
+
warnings,
|
|
5526
|
+
generateId: (_a = this.config.generateId) != null ? _a : import_provider_utils22.generateId,
|
|
5527
|
+
includeRawChunks: options.includeRawChunks,
|
|
5528
|
+
headerServiceTier
|
|
5327
5529
|
});
|
|
5328
|
-
|
|
5329
|
-
|
|
5530
|
+
return {
|
|
5531
|
+
stream: synthesized,
|
|
5532
|
+
request: { body: args },
|
|
5533
|
+
response: { headers: postHeaders }
|
|
5534
|
+
};
|
|
5330
5535
|
}
|
|
5331
|
-
|
|
5332
|
-
|
|
5536
|
+
void pollingTimeoutMs;
|
|
5537
|
+
const events = streamGoogleInteractionEvents({
|
|
5538
|
+
baseURL: this.config.baseURL,
|
|
5539
|
+
interactionId,
|
|
5540
|
+
headers: mergedHeaders,
|
|
5541
|
+
fetch: this.config.fetch,
|
|
5542
|
+
abortSignal: options.abortSignal
|
|
5543
|
+
});
|
|
5544
|
+
const transform = buildGoogleInteractionsStreamTransform({
|
|
5333
5545
|
warnings,
|
|
5334
|
-
generateId: (_b = this.config.generateId) != null ? _b :
|
|
5546
|
+
generateId: (_b = this.config.generateId) != null ? _b : import_provider_utils22.generateId,
|
|
5335
5547
|
includeRawChunks: options.includeRawChunks,
|
|
5336
|
-
|
|
5548
|
+
serviceTier: headerServiceTier
|
|
5337
5549
|
});
|
|
5338
5550
|
return {
|
|
5339
|
-
stream,
|
|
5551
|
+
stream: events.pipeThrough(transform),
|
|
5340
5552
|
request: { body: args },
|
|
5341
5553
|
response: { headers: postHeaders }
|
|
5342
5554
|
};
|
|
@@ -5354,11 +5566,11 @@ function pruneUndefined(obj) {
|
|
|
5354
5566
|
// src/google-provider.ts
|
|
5355
5567
|
function createGoogleGenerativeAI(options = {}) {
|
|
5356
5568
|
var _a, _b;
|
|
5357
|
-
const baseURL = (_a = (0,
|
|
5569
|
+
const baseURL = (_a = (0, import_provider_utils23.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
|
5358
5570
|
const providerName = (_b = options.name) != null ? _b : "google.generative-ai";
|
|
5359
|
-
const getHeaders = () => (0,
|
|
5571
|
+
const getHeaders = () => (0, import_provider_utils23.withUserAgentSuffix)(
|
|
5360
5572
|
{
|
|
5361
|
-
"x-goog-api-key": (0,
|
|
5573
|
+
"x-goog-api-key": (0, import_provider_utils23.loadApiKey)({
|
|
5362
5574
|
apiKey: options.apiKey,
|
|
5363
5575
|
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
|
5364
5576
|
description: "Google Generative AI"
|
|
@@ -5373,7 +5585,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
5373
5585
|
provider: providerName,
|
|
5374
5586
|
baseURL,
|
|
5375
5587
|
headers: getHeaders,
|
|
5376
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
5588
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils23.generateId,
|
|
5377
5589
|
supportedUrls: () => ({
|
|
5378
5590
|
"*": [
|
|
5379
5591
|
// Google Generative Language "files" endpoint
|
|
@@ -5408,7 +5620,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
5408
5620
|
baseURL,
|
|
5409
5621
|
headers: getHeaders,
|
|
5410
5622
|
fetch: options.fetch,
|
|
5411
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
5623
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils23.generateId
|
|
5412
5624
|
});
|
|
5413
5625
|
};
|
|
5414
5626
|
const createInteractionsModel = (modelIdOrAgent) => {
|
|
@@ -5419,7 +5631,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
5419
5631
|
provider: `${providerName}.interactions`,
|
|
5420
5632
|
baseURL,
|
|
5421
5633
|
headers: getHeaders,
|
|
5422
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
5634
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils23.generateId,
|
|
5423
5635
|
fetch: options.fetch
|
|
5424
5636
|
}
|
|
5425
5637
|
);
|