ai 6.0.301 → 6.0.303
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 +25 -0
- package/dist/index.d.mts +26 -4
- package/dist/index.d.ts +26 -4
- package/dist/index.js +189 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +189 -63
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1 -1
- package/dist/internal/index.mjs.map +1 -1
- package/docs/02-foundations/04-tools.mdx +1 -1
- package/docs/03-agents/06-memory.mdx +1 -1
- package/docs/03-ai-sdk-core/35-image-generation.mdx +2 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +2 -2
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +34 -0
- package/package.json +3 -3
- package/src/global.ts +23 -4
- package/src/model/as-language-model-v3.ts +4 -0
- package/src/types/usage.ts +21 -0
- package/src/ui/chat.ts +117 -34
- package/src/ui/convert-to-model-messages.ts +41 -8
- package/src/ui/http-chat-transport.ts +5 -3
- package/src/ui/process-ui-message-stream.ts +21 -0
- package/src/ui/ui-messages.ts +8 -0
- package/src/ui/unavailable-tool.ts +17 -0
- package/src/ui/validate-ui-messages.ts +12 -3
- package/src/ui-message-stream/read-ui-message-stream.ts +1 -0
- package/src/util/serial-job-executor.ts +22 -5
package/dist/index.mjs
CHANGED
|
@@ -775,7 +775,8 @@ function convertV2UsageToV3(usage) {
|
|
|
775
775
|
total: usage.outputTokens,
|
|
776
776
|
text: void 0,
|
|
777
777
|
reasoning: usage.reasoningTokens
|
|
778
|
-
}
|
|
778
|
+
},
|
|
779
|
+
raw: usage.totalTokens == null ? void 0 : { totalTokens: usage.totalTokens }
|
|
779
780
|
};
|
|
780
781
|
}
|
|
781
782
|
|
|
@@ -1224,7 +1225,7 @@ import {
|
|
|
1224
1225
|
} from "@ai-sdk/provider-utils";
|
|
1225
1226
|
|
|
1226
1227
|
// src/version.ts
|
|
1227
|
-
var VERSION = true ? "6.0.
|
|
1228
|
+
var VERSION = true ? "6.0.303" : "0.0.0-test";
|
|
1228
1229
|
|
|
1229
1230
|
// src/util/download/download.ts
|
|
1230
1231
|
var download = async ({
|
|
@@ -2704,6 +2705,7 @@ function createNullLanguageModelUsage() {
|
|
|
2704
2705
|
}
|
|
2705
2706
|
function addLanguageModelUsage(usage1, usage2) {
|
|
2706
2707
|
var _a23, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2708
|
+
const raw = isNullLanguageModelUsage(usage1) ? usage2.raw : isNullLanguageModelUsage(usage2) ? usage1.raw : void 0;
|
|
2707
2709
|
return {
|
|
2708
2710
|
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2709
2711
|
inputTokenDetails: {
|
|
@@ -2739,9 +2741,14 @@ function addLanguageModelUsage(usage1, usage2) {
|
|
|
2739
2741
|
cachedInputTokens: addTokenCounts(
|
|
2740
2742
|
usage1.cachedInputTokens,
|
|
2741
2743
|
usage2.cachedInputTokens
|
|
2742
|
-
)
|
|
2744
|
+
),
|
|
2745
|
+
...raw == null ? {} : { raw }
|
|
2743
2746
|
};
|
|
2744
2747
|
}
|
|
2748
|
+
function isNullLanguageModelUsage(usage) {
|
|
2749
|
+
var _a23, _b, _c, _d, _e;
|
|
2750
|
+
return usage.inputTokens == null && ((_a23 = usage.inputTokenDetails) == null ? void 0 : _a23.noCacheTokens) == null && ((_b = usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens) == null && ((_c = usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens) == null && usage.outputTokens == null && ((_d = usage.outputTokenDetails) == null ? void 0 : _d.textTokens) == null && ((_e = usage.outputTokenDetails) == null ? void 0 : _e.reasoningTokens) == null && usage.totalTokens == null && usage.raw == null;
|
|
2751
|
+
}
|
|
2745
2752
|
function addTokenCounts(tokenCount1, tokenCount2) {
|
|
2746
2753
|
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 != null ? tokenCount1 : 0) + (tokenCount2 != null ? tokenCount2 : 0);
|
|
2747
2754
|
}
|
|
@@ -6044,7 +6051,8 @@ function processUIMessageStream({
|
|
|
6044
6051
|
runUpdateMessageJob,
|
|
6045
6052
|
onError,
|
|
6046
6053
|
onToolCall,
|
|
6047
|
-
onData
|
|
6054
|
+
onData,
|
|
6055
|
+
resetStateOnMessageIdChange = false
|
|
6048
6056
|
}) {
|
|
6049
6057
|
return stream.pipeThrough(
|
|
6050
6058
|
new TransformStream({
|
|
@@ -6557,6 +6565,16 @@ function processUIMessageStream({
|
|
|
6557
6565
|
break;
|
|
6558
6566
|
}
|
|
6559
6567
|
case "start": {
|
|
6568
|
+
if (resetStateOnMessageIdChange && chunk.messageId != null && chunk.messageId !== state.message.id) {
|
|
6569
|
+
Object.assign(
|
|
6570
|
+
state,
|
|
6571
|
+
createStreamingUIMessageState({
|
|
6572
|
+
lastMessage: void 0,
|
|
6573
|
+
messageId: chunk.messageId
|
|
6574
|
+
}),
|
|
6575
|
+
{ finishReason: void 0 }
|
|
6576
|
+
);
|
|
6577
|
+
}
|
|
6560
6578
|
if (chunk.messageId != null) {
|
|
6561
6579
|
state.message.id = chunk.messageId;
|
|
6562
6580
|
}
|
|
@@ -9615,6 +9633,7 @@ function readUIMessageStream({
|
|
|
9615
9633
|
consumeStream({
|
|
9616
9634
|
stream: processUIMessageStream({
|
|
9617
9635
|
stream,
|
|
9636
|
+
resetStateOnMessageIdChange: true,
|
|
9618
9637
|
runUpdateMessageJob(job) {
|
|
9619
9638
|
return job({
|
|
9620
9639
|
state,
|
|
@@ -9641,6 +9660,22 @@ function readUIMessageStream({
|
|
|
9641
9660
|
import {
|
|
9642
9661
|
isNonNullable
|
|
9643
9662
|
} from "@ai-sdk/provider-utils";
|
|
9663
|
+
|
|
9664
|
+
// src/util/get-own.ts
|
|
9665
|
+
function getOwn(obj, key) {
|
|
9666
|
+
return obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0;
|
|
9667
|
+
}
|
|
9668
|
+
|
|
9669
|
+
// src/ui/unavailable-tool.ts
|
|
9670
|
+
function markToolPartAsUnavailable(part) {
|
|
9671
|
+
part.dynamic = false;
|
|
9672
|
+
return part;
|
|
9673
|
+
}
|
|
9674
|
+
function isToolPartFromUnavailableTool(part) {
|
|
9675
|
+
return part.type === "dynamic-tool" && part.dynamic === false;
|
|
9676
|
+
}
|
|
9677
|
+
|
|
9678
|
+
// src/ui/convert-to-model-messages.ts
|
|
9644
9679
|
async function convertToModelMessages(messages, options) {
|
|
9645
9680
|
const modelMessages = [];
|
|
9646
9681
|
if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
|
|
@@ -9651,6 +9686,27 @@ async function convertToModelMessages(messages, options) {
|
|
|
9651
9686
|
)
|
|
9652
9687
|
}));
|
|
9653
9688
|
}
|
|
9689
|
+
async function createModelOutput({
|
|
9690
|
+
toolPart,
|
|
9691
|
+
toolName,
|
|
9692
|
+
output,
|
|
9693
|
+
errorMode
|
|
9694
|
+
}) {
|
|
9695
|
+
const tool2 = getOwn(options == null ? void 0 : options.tools, toolName);
|
|
9696
|
+
if (errorMode === "none" && tool2 == null && isToolPartFromUnavailableTool(toolPart)) {
|
|
9697
|
+
return {
|
|
9698
|
+
type: "text",
|
|
9699
|
+
value: "Tool output omitted because the tool is no longer available."
|
|
9700
|
+
};
|
|
9701
|
+
}
|
|
9702
|
+
return createToolModelOutput({
|
|
9703
|
+
toolCallId: toolPart.toolCallId,
|
|
9704
|
+
input: toolPart.input,
|
|
9705
|
+
output,
|
|
9706
|
+
tool: tool2,
|
|
9707
|
+
errorMode
|
|
9708
|
+
});
|
|
9709
|
+
}
|
|
9654
9710
|
for (const message of messages) {
|
|
9655
9711
|
switch (message.role) {
|
|
9656
9712
|
case "system": {
|
|
@@ -9705,7 +9761,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9705
9761
|
if (message.parts != null) {
|
|
9706
9762
|
let block = [];
|
|
9707
9763
|
async function processBlock() {
|
|
9708
|
-
var _a23, _b, _c, _d, _e, _f, _g
|
|
9764
|
+
var _a23, _b, _c, _d, _e, _f, _g;
|
|
9709
9765
|
if (block.length === 0) {
|
|
9710
9766
|
return;
|
|
9711
9767
|
}
|
|
@@ -9763,11 +9819,10 @@ async function convertToModelMessages(messages, options) {
|
|
|
9763
9819
|
type: "tool-result",
|
|
9764
9820
|
toolCallId: part.toolCallId,
|
|
9765
9821
|
toolName,
|
|
9766
|
-
output: await
|
|
9767
|
-
|
|
9768
|
-
|
|
9822
|
+
output: await createModelOutput({
|
|
9823
|
+
toolPart: part,
|
|
9824
|
+
toolName,
|
|
9769
9825
|
output: part.state === "output-error" ? part.errorText : part.output,
|
|
9770
|
-
tool: (_d = options == null ? void 0 : options.tools) == null ? void 0 : _d[toolName],
|
|
9771
9826
|
errorMode: part.state === "output-error" ? "json" : "none"
|
|
9772
9827
|
}),
|
|
9773
9828
|
...resultProviderMetadata != null ? { providerOptions: resultProviderMetadata } : {}
|
|
@@ -9775,7 +9830,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9775
9830
|
}
|
|
9776
9831
|
}
|
|
9777
9832
|
} else if (isDataUIPart(part)) {
|
|
9778
|
-
const dataPart = (
|
|
9833
|
+
const dataPart = (_d = options == null ? void 0 : options.convertDataPart) == null ? void 0 : _d.call(
|
|
9779
9834
|
options,
|
|
9780
9835
|
part
|
|
9781
9836
|
);
|
|
@@ -9803,7 +9858,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9803
9858
|
{
|
|
9804
9859
|
const content2 = [];
|
|
9805
9860
|
for (const toolPart of toolParts) {
|
|
9806
|
-
if (((
|
|
9861
|
+
if (((_e = toolPart.approval) == null ? void 0 : _e.approved) != null) {
|
|
9807
9862
|
content2.push({
|
|
9808
9863
|
type: "tool-approval-response",
|
|
9809
9864
|
approvalId: toolPart.approval.id,
|
|
@@ -9823,7 +9878,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9823
9878
|
toolName: getToolName(toolPart),
|
|
9824
9879
|
output: {
|
|
9825
9880
|
type: "error-text",
|
|
9826
|
-
value: (
|
|
9881
|
+
value: (_g = (_f = toolPart.approval) == null ? void 0 : _f.reason) != null ? _g : "Tool call execution denied."
|
|
9827
9882
|
},
|
|
9828
9883
|
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
9829
9884
|
});
|
|
@@ -9836,11 +9891,10 @@ async function convertToModelMessages(messages, options) {
|
|
|
9836
9891
|
type: "tool-result",
|
|
9837
9892
|
toolCallId: toolPart.toolCallId,
|
|
9838
9893
|
toolName,
|
|
9839
|
-
output: await
|
|
9840
|
-
|
|
9841
|
-
|
|
9894
|
+
output: await createModelOutput({
|
|
9895
|
+
toolPart,
|
|
9896
|
+
toolName,
|
|
9842
9897
|
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
9843
|
-
tool: (_i = options == null ? void 0 : options.tools) == null ? void 0 : _i[toolName],
|
|
9844
9898
|
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
9845
9899
|
}),
|
|
9846
9900
|
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
@@ -9903,11 +9957,11 @@ function isEmptyObject(value) {
|
|
|
9903
9957
|
}
|
|
9904
9958
|
function asDynamicToolPart(toolPart) {
|
|
9905
9959
|
const { type, ...part } = toolPart;
|
|
9906
|
-
return {
|
|
9960
|
+
return markToolPartAsUnavailable({
|
|
9907
9961
|
...part,
|
|
9908
9962
|
type: "dynamic-tool",
|
|
9909
9963
|
toolName: type.slice(5)
|
|
9910
|
-
};
|
|
9964
|
+
});
|
|
9911
9965
|
}
|
|
9912
9966
|
function getToolPartInputSchemaInput(toolPart) {
|
|
9913
9967
|
return toolPart.approval != null && Object.prototype.hasOwnProperty.call(toolPart.approval, "inputSchemaInput") ? { value: toolPart.approval.inputSchemaInput } : void 0;
|
|
@@ -9984,6 +10038,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
9984
10038
|
}),
|
|
9985
10039
|
z8.object({
|
|
9986
10040
|
type: z8.literal("dynamic-tool"),
|
|
10041
|
+
dynamic: z8.literal(false).optional(),
|
|
9987
10042
|
toolName: z8.string(),
|
|
9988
10043
|
toolCallId: z8.string(),
|
|
9989
10044
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -9998,6 +10053,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
9998
10053
|
}),
|
|
9999
10054
|
z8.object({
|
|
10000
10055
|
type: z8.literal("dynamic-tool"),
|
|
10056
|
+
dynamic: z8.literal(false).optional(),
|
|
10001
10057
|
toolName: z8.string(),
|
|
10002
10058
|
toolCallId: z8.string(),
|
|
10003
10059
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10011,6 +10067,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
10011
10067
|
}),
|
|
10012
10068
|
z8.object({
|
|
10013
10069
|
type: z8.literal("dynamic-tool"),
|
|
10070
|
+
dynamic: z8.literal(false).optional(),
|
|
10014
10071
|
toolName: z8.string(),
|
|
10015
10072
|
toolCallId: z8.string(),
|
|
10016
10073
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10024,6 +10081,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
10024
10081
|
}),
|
|
10025
10082
|
z8.object({
|
|
10026
10083
|
type: z8.literal("dynamic-tool"),
|
|
10084
|
+
dynamic: z8.literal(false).optional(),
|
|
10027
10085
|
toolName: z8.string(),
|
|
10028
10086
|
toolCallId: z8.string(),
|
|
10029
10087
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10037,6 +10095,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
10037
10095
|
}),
|
|
10038
10096
|
z8.object({
|
|
10039
10097
|
type: z8.literal("dynamic-tool"),
|
|
10098
|
+
dynamic: z8.literal(false).optional(),
|
|
10040
10099
|
toolName: z8.string(),
|
|
10041
10100
|
toolCallId: z8.string(),
|
|
10042
10101
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10052,6 +10111,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
10052
10111
|
}),
|
|
10053
10112
|
z8.object({
|
|
10054
10113
|
type: z8.literal("dynamic-tool"),
|
|
10114
|
+
dynamic: z8.literal(false).optional(),
|
|
10055
10115
|
toolName: z8.string(),
|
|
10056
10116
|
toolCallId: z8.string(),
|
|
10057
10117
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10067,6 +10127,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
10067
10127
|
}),
|
|
10068
10128
|
z8.object({
|
|
10069
10129
|
type: z8.literal("dynamic-tool"),
|
|
10130
|
+
dynamic: z8.literal(false).optional(),
|
|
10070
10131
|
toolName: z8.string(),
|
|
10071
10132
|
toolCallId: z8.string(),
|
|
10072
10133
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10255,7 +10316,7 @@ async function safeValidateUIMessagesInternal({
|
|
|
10255
10316
|
if (shouldValidateToolParts && part.type.startsWith("tool-")) {
|
|
10256
10317
|
const toolPart = part;
|
|
10257
10318
|
const toolName = toolPart.type.slice(5);
|
|
10258
|
-
const tool2 = tools
|
|
10319
|
+
const tool2 = getOwn(tools, toolName);
|
|
10259
10320
|
const isTerminal = toolPart.state === "output-available" || toolPart.state === "output-error" || toolPart.state === "output-denied";
|
|
10260
10321
|
if (!tool2 && isTerminal) {
|
|
10261
10322
|
if (tools != null || convertMissingTerminalToolsToDynamic) {
|
|
@@ -10574,11 +10635,6 @@ var DefaultEmbedResult = class {
|
|
|
10574
10635
|
}
|
|
10575
10636
|
};
|
|
10576
10637
|
|
|
10577
|
-
// src/util/get-own.ts
|
|
10578
|
-
function getOwn(obj, key) {
|
|
10579
|
-
return obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0;
|
|
10580
|
-
}
|
|
10581
|
-
|
|
10582
10638
|
// src/util/set-own.ts
|
|
10583
10639
|
function setOwn(object2, key, value) {
|
|
10584
10640
|
Object.defineProperty(object2, key, {
|
|
@@ -11916,17 +11972,24 @@ var SerialJobExecutor = class {
|
|
|
11916
11972
|
constructor() {
|
|
11917
11973
|
this.queue = [];
|
|
11918
11974
|
this.isProcessing = false;
|
|
11975
|
+
this.idleWaiters = [];
|
|
11919
11976
|
}
|
|
11920
11977
|
async processQueue() {
|
|
11921
11978
|
if (this.isProcessing) {
|
|
11922
11979
|
return;
|
|
11923
11980
|
}
|
|
11924
11981
|
this.isProcessing = true;
|
|
11925
|
-
|
|
11926
|
-
|
|
11927
|
-
|
|
11982
|
+
try {
|
|
11983
|
+
while (this.queue.length > 0) {
|
|
11984
|
+
await this.queue[0]();
|
|
11985
|
+
this.queue.shift();
|
|
11986
|
+
}
|
|
11987
|
+
} finally {
|
|
11988
|
+
this.isProcessing = false;
|
|
11989
|
+
for (const resolve3 of this.idleWaiters.splice(0)) {
|
|
11990
|
+
resolve3();
|
|
11991
|
+
}
|
|
11928
11992
|
}
|
|
11929
|
-
this.isProcessing = false;
|
|
11930
11993
|
}
|
|
11931
11994
|
async run(job) {
|
|
11932
11995
|
return new Promise((resolve3, reject) => {
|
|
@@ -11941,6 +12004,14 @@ var SerialJobExecutor = class {
|
|
|
11941
12004
|
void this.processQueue();
|
|
11942
12005
|
});
|
|
11943
12006
|
}
|
|
12007
|
+
async waitForIdle() {
|
|
12008
|
+
if (!this.isProcessing && this.queue.length === 0) {
|
|
12009
|
+
return;
|
|
12010
|
+
}
|
|
12011
|
+
await new Promise((resolve3) => {
|
|
12012
|
+
this.idleWaiters.push(resolve3);
|
|
12013
|
+
});
|
|
12014
|
+
}
|
|
11944
12015
|
};
|
|
11945
12016
|
|
|
11946
12017
|
// src/util/simulate-readable-stream.ts
|
|
@@ -14430,7 +14501,9 @@ import {
|
|
|
14430
14501
|
} from "@ai-sdk/provider-utils";
|
|
14431
14502
|
function appendPathToUrl(url, path) {
|
|
14432
14503
|
const queryOrFragmentStart = url.search(/[?#]/);
|
|
14433
|
-
|
|
14504
|
+
const urlPath = queryOrFragmentStart === -1 ? url : url.slice(0, queryOrFragmentStart);
|
|
14505
|
+
const suffix = queryOrFragmentStart === -1 ? "" : url.slice(queryOrFragmentStart);
|
|
14506
|
+
return `${urlPath.endsWith("/") && path.startsWith("/") ? urlPath.slice(0, -1) : urlPath}${path}${suffix}`;
|
|
14434
14507
|
}
|
|
14435
14508
|
var HttpChatTransport = class {
|
|
14436
14509
|
constructor({
|
|
@@ -14601,6 +14674,8 @@ var AbstractChat = class {
|
|
|
14601
14674
|
this.activeResumeRequest = void 0;
|
|
14602
14675
|
this.resumableStreamState = void 0;
|
|
14603
14676
|
this.jobExecutor = new SerialJobExecutor();
|
|
14677
|
+
this.activeStopCount = 0;
|
|
14678
|
+
this.stopGeneration = 0;
|
|
14604
14679
|
/**
|
|
14605
14680
|
* Appends or replaces a user message to the chat list. This triggers the API call to fetch
|
|
14606
14681
|
* the assistant's response.
|
|
@@ -14744,17 +14819,18 @@ var AbstractChat = class {
|
|
|
14744
14819
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
14745
14820
|
}
|
|
14746
14821
|
if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
|
|
14747
|
-
this.shouldSendAutomatically()
|
|
14748
|
-
|
|
14749
|
-
|
|
14822
|
+
const shouldSend = await this.shouldSendAutomatically();
|
|
14823
|
+
if (shouldSend) {
|
|
14824
|
+
void this.runAutomaticRequest(() => {
|
|
14825
|
+
var _a23;
|
|
14750
14826
|
const messageId = messageIndex === -1 ? (_a23 = this.lastMessage) == null ? void 0 : _a23.id : messages[messageIndex].id;
|
|
14751
|
-
this.makeRequestForToolApproval({
|
|
14827
|
+
return this.makeRequestForToolApproval({
|
|
14752
14828
|
messageId,
|
|
14753
14829
|
messageIndex,
|
|
14754
14830
|
...options
|
|
14755
14831
|
});
|
|
14756
|
-
}
|
|
14757
|
-
}
|
|
14832
|
+
}, shouldSend);
|
|
14833
|
+
}
|
|
14758
14834
|
}
|
|
14759
14835
|
});
|
|
14760
14836
|
this.addToolOutput = async ({
|
|
@@ -14775,27 +14851,44 @@ var AbstractChat = class {
|
|
|
14775
14851
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
14776
14852
|
}
|
|
14777
14853
|
if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
|
|
14778
|
-
this.shouldSendAutomatically()
|
|
14779
|
-
|
|
14780
|
-
|
|
14781
|
-
|
|
14782
|
-
|
|
14783
|
-
|
|
14784
|
-
|
|
14785
|
-
|
|
14786
|
-
|
|
14787
|
-
|
|
14854
|
+
const shouldSend = await this.shouldSendAutomatically();
|
|
14855
|
+
if (shouldSend) {
|
|
14856
|
+
void this.runAutomaticRequest(
|
|
14857
|
+
() => {
|
|
14858
|
+
var _a23;
|
|
14859
|
+
return this.makeRequest({
|
|
14860
|
+
trigger: "submit-message",
|
|
14861
|
+
messageId: (_a23 = this.lastMessage) == null ? void 0 : _a23.id,
|
|
14862
|
+
...options
|
|
14863
|
+
});
|
|
14864
|
+
},
|
|
14865
|
+
shouldSend
|
|
14866
|
+
);
|
|
14867
|
+
}
|
|
14788
14868
|
}
|
|
14789
14869
|
});
|
|
14790
14870
|
/** @deprecated Use addToolOutput */
|
|
14791
14871
|
this.addToolResult = this.addToolOutput;
|
|
14792
14872
|
/**
|
|
14793
|
-
* Abort the current request
|
|
14873
|
+
* Abort the current request, keep the generated tokens if any, and wait for
|
|
14874
|
+
* the request pipeline to finish.
|
|
14794
14875
|
*/
|
|
14795
14876
|
this.stop = async () => {
|
|
14796
|
-
|
|
14797
|
-
|
|
14798
|
-
|
|
14877
|
+
this.activeStopCount++;
|
|
14878
|
+
this.stopGeneration++;
|
|
14879
|
+
try {
|
|
14880
|
+
const activeResumeRequest = this.activeResumeRequest;
|
|
14881
|
+
const activeResponse = this.activeResponse;
|
|
14882
|
+
activeResumeRequest == null ? void 0 : activeResumeRequest.abortController.abort();
|
|
14883
|
+
activeResponse == null ? void 0 : activeResponse.abortController.abort();
|
|
14884
|
+
await Promise.all([
|
|
14885
|
+
activeResumeRequest == null ? void 0 : activeResumeRequest.completionPromise,
|
|
14886
|
+
activeResponse == null ? void 0 : activeResponse.completionPromise
|
|
14887
|
+
]);
|
|
14888
|
+
await this.jobExecutor.waitForIdle();
|
|
14889
|
+
} finally {
|
|
14890
|
+
this.activeStopCount--;
|
|
14891
|
+
}
|
|
14799
14892
|
};
|
|
14800
14893
|
this.id = id;
|
|
14801
14894
|
this.transport = transport;
|
|
@@ -14824,8 +14917,12 @@ var AbstractChat = class {
|
|
|
14824
14917
|
status,
|
|
14825
14918
|
error
|
|
14826
14919
|
}) {
|
|
14827
|
-
if (this.status === status)
|
|
14920
|
+
if (this.status === status) {
|
|
14921
|
+
if (this.error !== error) {
|
|
14922
|
+
this.state.error = error;
|
|
14923
|
+
}
|
|
14828
14924
|
return;
|
|
14925
|
+
}
|
|
14829
14926
|
this.state.status = status;
|
|
14830
14927
|
this.state.error = error;
|
|
14831
14928
|
}
|
|
@@ -14852,6 +14949,17 @@ var AbstractChat = class {
|
|
|
14852
14949
|
}
|
|
14853
14950
|
return result;
|
|
14854
14951
|
}
|
|
14952
|
+
async runAutomaticRequest(request, shouldSend) {
|
|
14953
|
+
const stopGeneration = this.stopGeneration;
|
|
14954
|
+
const startedWhileStopping = this.activeStopCount > 0;
|
|
14955
|
+
if (!(shouldSend != null ? shouldSend : await this.shouldSendAutomatically())) {
|
|
14956
|
+
return;
|
|
14957
|
+
}
|
|
14958
|
+
if (startedWhileStopping || stopGeneration !== this.stopGeneration || this.activeStopCount > 0) {
|
|
14959
|
+
return;
|
|
14960
|
+
}
|
|
14961
|
+
await request();
|
|
14962
|
+
}
|
|
14855
14963
|
async makeRequestForToolApproval({
|
|
14856
14964
|
messageId,
|
|
14857
14965
|
messageIndex,
|
|
@@ -14871,19 +14979,31 @@ var AbstractChat = class {
|
|
|
14871
14979
|
this.pendingApprovalMessageId = (_b = (_a23 = this.state.messages[messageIndex]) == null ? void 0 : _a23.id) != null ? _b : messageId;
|
|
14872
14980
|
}
|
|
14873
14981
|
}
|
|
14874
|
-
async makeRequest({
|
|
14982
|
+
async makeRequest(options) {
|
|
14983
|
+
const completion = createResolvablePromise();
|
|
14984
|
+
try {
|
|
14985
|
+
await this.makeRequestImpl({
|
|
14986
|
+
...options,
|
|
14987
|
+
completionPromise: completion.promise
|
|
14988
|
+
});
|
|
14989
|
+
} finally {
|
|
14990
|
+
completion.resolve();
|
|
14991
|
+
}
|
|
14992
|
+
}
|
|
14993
|
+
async makeRequestImpl({
|
|
14875
14994
|
trigger,
|
|
14876
14995
|
metadata,
|
|
14877
14996
|
headers,
|
|
14878
14997
|
body,
|
|
14879
|
-
messageId
|
|
14998
|
+
messageId,
|
|
14999
|
+
completionPromise
|
|
14880
15000
|
}) {
|
|
14881
|
-
var _a23, _b
|
|
15001
|
+
var _a23, _b;
|
|
14882
15002
|
if (trigger !== "resume-stream") {
|
|
14883
15003
|
this.resumableStreamState = void 0;
|
|
14884
15004
|
}
|
|
14885
15005
|
const abortController = new AbortController();
|
|
14886
|
-
const activeResumeRequest = trigger === "resume-stream" ? { abortController } : void 0;
|
|
15006
|
+
const activeResumeRequest = trigger === "resume-stream" ? { abortController, completionPromise } : void 0;
|
|
14887
15007
|
if (activeResumeRequest) {
|
|
14888
15008
|
(_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
|
|
14889
15009
|
this.activeResumeRequest = activeResumeRequest;
|
|
@@ -14959,7 +15079,8 @@ var AbstractChat = class {
|
|
|
14959
15079
|
lastMessage: trigger === "resume-stream" ? resumableResponseMessage : trigger === "regenerate-message" ? void 0 : this.state.snapshot(responseMessage),
|
|
14960
15080
|
messageId: this.generateId()
|
|
14961
15081
|
}),
|
|
14962
|
-
abortController
|
|
15082
|
+
abortController,
|
|
15083
|
+
completionPromise
|
|
14963
15084
|
};
|
|
14964
15085
|
activeResponse = response;
|
|
14965
15086
|
response.abortController.signal.addEventListener("abort", () => {
|
|
@@ -15092,14 +15213,19 @@ var AbstractChat = class {
|
|
|
15092
15213
|
clearActiveResumeRequest();
|
|
15093
15214
|
}
|
|
15094
15215
|
}
|
|
15095
|
-
if (!
|
|
15096
|
-
await this.
|
|
15097
|
-
|
|
15098
|
-
|
|
15099
|
-
|
|
15100
|
-
|
|
15101
|
-
|
|
15102
|
-
|
|
15216
|
+
if (!isAbort && !isError) {
|
|
15217
|
+
await this.runAutomaticRequest(
|
|
15218
|
+
() => {
|
|
15219
|
+
var _a24;
|
|
15220
|
+
return this.makeRequest({
|
|
15221
|
+
trigger: "submit-message",
|
|
15222
|
+
messageId: (_a24 = this.lastMessage) == null ? void 0 : _a24.id,
|
|
15223
|
+
metadata,
|
|
15224
|
+
headers,
|
|
15225
|
+
body
|
|
15226
|
+
});
|
|
15227
|
+
}
|
|
15228
|
+
);
|
|
15103
15229
|
}
|
|
15104
15230
|
}
|
|
15105
15231
|
};
|