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.js
CHANGED
|
@@ -895,7 +895,8 @@ function convertV2UsageToV3(usage) {
|
|
|
895
895
|
total: usage.outputTokens,
|
|
896
896
|
text: void 0,
|
|
897
897
|
reasoning: usage.reasoningTokens
|
|
898
|
-
}
|
|
898
|
+
},
|
|
899
|
+
raw: usage.totalTokens == null ? void 0 : { totalTokens: usage.totalTokens }
|
|
899
900
|
};
|
|
900
901
|
}
|
|
901
902
|
|
|
@@ -1334,7 +1335,7 @@ function detectMediaType({
|
|
|
1334
1335
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
1335
1336
|
|
|
1336
1337
|
// src/version.ts
|
|
1337
|
-
var VERSION = true ? "6.0.
|
|
1338
|
+
var VERSION = true ? "6.0.303" : "0.0.0-test";
|
|
1338
1339
|
|
|
1339
1340
|
// src/util/download/download.ts
|
|
1340
1341
|
var download = async ({
|
|
@@ -2806,6 +2807,7 @@ function createNullLanguageModelUsage() {
|
|
|
2806
2807
|
}
|
|
2807
2808
|
function addLanguageModelUsage(usage1, usage2) {
|
|
2808
2809
|
var _a23, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2810
|
+
const raw = isNullLanguageModelUsage(usage1) ? usage2.raw : isNullLanguageModelUsage(usage2) ? usage1.raw : void 0;
|
|
2809
2811
|
return {
|
|
2810
2812
|
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2811
2813
|
inputTokenDetails: {
|
|
@@ -2841,9 +2843,14 @@ function addLanguageModelUsage(usage1, usage2) {
|
|
|
2841
2843
|
cachedInputTokens: addTokenCounts(
|
|
2842
2844
|
usage1.cachedInputTokens,
|
|
2843
2845
|
usage2.cachedInputTokens
|
|
2844
|
-
)
|
|
2846
|
+
),
|
|
2847
|
+
...raw == null ? {} : { raw }
|
|
2845
2848
|
};
|
|
2846
2849
|
}
|
|
2850
|
+
function isNullLanguageModelUsage(usage) {
|
|
2851
|
+
var _a23, _b, _c, _d, _e;
|
|
2852
|
+
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;
|
|
2853
|
+
}
|
|
2847
2854
|
function addTokenCounts(tokenCount1, tokenCount2) {
|
|
2848
2855
|
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 != null ? tokenCount1 : 0) + (tokenCount2 != null ? tokenCount2 : 0);
|
|
2849
2856
|
}
|
|
@@ -6117,7 +6124,8 @@ function processUIMessageStream({
|
|
|
6117
6124
|
runUpdateMessageJob,
|
|
6118
6125
|
onError,
|
|
6119
6126
|
onToolCall,
|
|
6120
|
-
onData
|
|
6127
|
+
onData,
|
|
6128
|
+
resetStateOnMessageIdChange = false
|
|
6121
6129
|
}) {
|
|
6122
6130
|
return stream.pipeThrough(
|
|
6123
6131
|
new TransformStream({
|
|
@@ -6630,6 +6638,16 @@ function processUIMessageStream({
|
|
|
6630
6638
|
break;
|
|
6631
6639
|
}
|
|
6632
6640
|
case "start": {
|
|
6641
|
+
if (resetStateOnMessageIdChange && chunk.messageId != null && chunk.messageId !== state.message.id) {
|
|
6642
|
+
Object.assign(
|
|
6643
|
+
state,
|
|
6644
|
+
createStreamingUIMessageState({
|
|
6645
|
+
lastMessage: void 0,
|
|
6646
|
+
messageId: chunk.messageId
|
|
6647
|
+
}),
|
|
6648
|
+
{ finishReason: void 0 }
|
|
6649
|
+
);
|
|
6650
|
+
}
|
|
6633
6651
|
if (chunk.messageId != null) {
|
|
6634
6652
|
state.message.id = chunk.messageId;
|
|
6635
6653
|
}
|
|
@@ -9684,6 +9702,7 @@ function readUIMessageStream({
|
|
|
9684
9702
|
consumeStream({
|
|
9685
9703
|
stream: processUIMessageStream({
|
|
9686
9704
|
stream,
|
|
9705
|
+
resetStateOnMessageIdChange: true,
|
|
9687
9706
|
runUpdateMessageJob(job) {
|
|
9688
9707
|
return job({
|
|
9689
9708
|
state,
|
|
@@ -9708,6 +9727,22 @@ function readUIMessageStream({
|
|
|
9708
9727
|
|
|
9709
9728
|
// src/ui/convert-to-model-messages.ts
|
|
9710
9729
|
var import_provider_utils23 = require("@ai-sdk/provider-utils");
|
|
9730
|
+
|
|
9731
|
+
// src/util/get-own.ts
|
|
9732
|
+
function getOwn(obj, key) {
|
|
9733
|
+
return obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0;
|
|
9734
|
+
}
|
|
9735
|
+
|
|
9736
|
+
// src/ui/unavailable-tool.ts
|
|
9737
|
+
function markToolPartAsUnavailable(part) {
|
|
9738
|
+
part.dynamic = false;
|
|
9739
|
+
return part;
|
|
9740
|
+
}
|
|
9741
|
+
function isToolPartFromUnavailableTool(part) {
|
|
9742
|
+
return part.type === "dynamic-tool" && part.dynamic === false;
|
|
9743
|
+
}
|
|
9744
|
+
|
|
9745
|
+
// src/ui/convert-to-model-messages.ts
|
|
9711
9746
|
async function convertToModelMessages(messages, options) {
|
|
9712
9747
|
const modelMessages = [];
|
|
9713
9748
|
if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
|
|
@@ -9718,6 +9753,27 @@ async function convertToModelMessages(messages, options) {
|
|
|
9718
9753
|
)
|
|
9719
9754
|
}));
|
|
9720
9755
|
}
|
|
9756
|
+
async function createModelOutput({
|
|
9757
|
+
toolPart,
|
|
9758
|
+
toolName,
|
|
9759
|
+
output,
|
|
9760
|
+
errorMode
|
|
9761
|
+
}) {
|
|
9762
|
+
const tool2 = getOwn(options == null ? void 0 : options.tools, toolName);
|
|
9763
|
+
if (errorMode === "none" && tool2 == null && isToolPartFromUnavailableTool(toolPart)) {
|
|
9764
|
+
return {
|
|
9765
|
+
type: "text",
|
|
9766
|
+
value: "Tool output omitted because the tool is no longer available."
|
|
9767
|
+
};
|
|
9768
|
+
}
|
|
9769
|
+
return createToolModelOutput({
|
|
9770
|
+
toolCallId: toolPart.toolCallId,
|
|
9771
|
+
input: toolPart.input,
|
|
9772
|
+
output,
|
|
9773
|
+
tool: tool2,
|
|
9774
|
+
errorMode
|
|
9775
|
+
});
|
|
9776
|
+
}
|
|
9721
9777
|
for (const message of messages) {
|
|
9722
9778
|
switch (message.role) {
|
|
9723
9779
|
case "system": {
|
|
@@ -9772,7 +9828,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9772
9828
|
if (message.parts != null) {
|
|
9773
9829
|
let block = [];
|
|
9774
9830
|
async function processBlock() {
|
|
9775
|
-
var _a23, _b, _c, _d, _e, _f, _g
|
|
9831
|
+
var _a23, _b, _c, _d, _e, _f, _g;
|
|
9776
9832
|
if (block.length === 0) {
|
|
9777
9833
|
return;
|
|
9778
9834
|
}
|
|
@@ -9830,11 +9886,10 @@ async function convertToModelMessages(messages, options) {
|
|
|
9830
9886
|
type: "tool-result",
|
|
9831
9887
|
toolCallId: part.toolCallId,
|
|
9832
9888
|
toolName,
|
|
9833
|
-
output: await
|
|
9834
|
-
|
|
9835
|
-
|
|
9889
|
+
output: await createModelOutput({
|
|
9890
|
+
toolPart: part,
|
|
9891
|
+
toolName,
|
|
9836
9892
|
output: part.state === "output-error" ? part.errorText : part.output,
|
|
9837
|
-
tool: (_d = options == null ? void 0 : options.tools) == null ? void 0 : _d[toolName],
|
|
9838
9893
|
errorMode: part.state === "output-error" ? "json" : "none"
|
|
9839
9894
|
}),
|
|
9840
9895
|
...resultProviderMetadata != null ? { providerOptions: resultProviderMetadata } : {}
|
|
@@ -9842,7 +9897,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9842
9897
|
}
|
|
9843
9898
|
}
|
|
9844
9899
|
} else if (isDataUIPart(part)) {
|
|
9845
|
-
const dataPart = (
|
|
9900
|
+
const dataPart = (_d = options == null ? void 0 : options.convertDataPart) == null ? void 0 : _d.call(
|
|
9846
9901
|
options,
|
|
9847
9902
|
part
|
|
9848
9903
|
);
|
|
@@ -9870,7 +9925,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9870
9925
|
{
|
|
9871
9926
|
const content2 = [];
|
|
9872
9927
|
for (const toolPart of toolParts) {
|
|
9873
|
-
if (((
|
|
9928
|
+
if (((_e = toolPart.approval) == null ? void 0 : _e.approved) != null) {
|
|
9874
9929
|
content2.push({
|
|
9875
9930
|
type: "tool-approval-response",
|
|
9876
9931
|
approvalId: toolPart.approval.id,
|
|
@@ -9890,7 +9945,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9890
9945
|
toolName: getToolName(toolPart),
|
|
9891
9946
|
output: {
|
|
9892
9947
|
type: "error-text",
|
|
9893
|
-
value: (
|
|
9948
|
+
value: (_g = (_f = toolPart.approval) == null ? void 0 : _f.reason) != null ? _g : "Tool call execution denied."
|
|
9894
9949
|
},
|
|
9895
9950
|
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
9896
9951
|
});
|
|
@@ -9903,11 +9958,10 @@ async function convertToModelMessages(messages, options) {
|
|
|
9903
9958
|
type: "tool-result",
|
|
9904
9959
|
toolCallId: toolPart.toolCallId,
|
|
9905
9960
|
toolName,
|
|
9906
|
-
output: await
|
|
9907
|
-
|
|
9908
|
-
|
|
9961
|
+
output: await createModelOutput({
|
|
9962
|
+
toolPart,
|
|
9963
|
+
toolName,
|
|
9909
9964
|
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
9910
|
-
tool: (_i = options == null ? void 0 : options.tools) == null ? void 0 : _i[toolName],
|
|
9911
9965
|
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
9912
9966
|
}),
|
|
9913
9967
|
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
@@ -9963,11 +10017,11 @@ function isEmptyObject(value) {
|
|
|
9963
10017
|
}
|
|
9964
10018
|
function asDynamicToolPart(toolPart) {
|
|
9965
10019
|
const { type, ...part } = toolPart;
|
|
9966
|
-
return {
|
|
10020
|
+
return markToolPartAsUnavailable({
|
|
9967
10021
|
...part,
|
|
9968
10022
|
type: "dynamic-tool",
|
|
9969
10023
|
toolName: type.slice(5)
|
|
9970
|
-
};
|
|
10024
|
+
});
|
|
9971
10025
|
}
|
|
9972
10026
|
function getToolPartInputSchemaInput(toolPart) {
|
|
9973
10027
|
return toolPart.approval != null && Object.prototype.hasOwnProperty.call(toolPart.approval, "inputSchemaInput") ? { value: toolPart.approval.inputSchemaInput } : void 0;
|
|
@@ -10044,6 +10098,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10044
10098
|
}),
|
|
10045
10099
|
import_v48.z.object({
|
|
10046
10100
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10101
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10047
10102
|
toolName: import_v48.z.string(),
|
|
10048
10103
|
toolCallId: import_v48.z.string(),
|
|
10049
10104
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10058,6 +10113,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10058
10113
|
}),
|
|
10059
10114
|
import_v48.z.object({
|
|
10060
10115
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10116
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10061
10117
|
toolName: import_v48.z.string(),
|
|
10062
10118
|
toolCallId: import_v48.z.string(),
|
|
10063
10119
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10071,6 +10127,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10071
10127
|
}),
|
|
10072
10128
|
import_v48.z.object({
|
|
10073
10129
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10130
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10074
10131
|
toolName: import_v48.z.string(),
|
|
10075
10132
|
toolCallId: import_v48.z.string(),
|
|
10076
10133
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10084,6 +10141,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10084
10141
|
}),
|
|
10085
10142
|
import_v48.z.object({
|
|
10086
10143
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10144
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10087
10145
|
toolName: import_v48.z.string(),
|
|
10088
10146
|
toolCallId: import_v48.z.string(),
|
|
10089
10147
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10097,6 +10155,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10097
10155
|
}),
|
|
10098
10156
|
import_v48.z.object({
|
|
10099
10157
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10158
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10100
10159
|
toolName: import_v48.z.string(),
|
|
10101
10160
|
toolCallId: import_v48.z.string(),
|
|
10102
10161
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10112,6 +10171,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10112
10171
|
}),
|
|
10113
10172
|
import_v48.z.object({
|
|
10114
10173
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10174
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10115
10175
|
toolName: import_v48.z.string(),
|
|
10116
10176
|
toolCallId: import_v48.z.string(),
|
|
10117
10177
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10127,6 +10187,7 @@ var uiMessagesSchema = (0, import_provider_utils24.lazySchema)(() => {
|
|
|
10127
10187
|
}),
|
|
10128
10188
|
import_v48.z.object({
|
|
10129
10189
|
type: import_v48.z.literal("dynamic-tool"),
|
|
10190
|
+
dynamic: import_v48.z.literal(false).optional(),
|
|
10130
10191
|
toolName: import_v48.z.string(),
|
|
10131
10192
|
toolCallId: import_v48.z.string(),
|
|
10132
10193
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
@@ -10315,7 +10376,7 @@ async function safeValidateUIMessagesInternal({
|
|
|
10315
10376
|
if (shouldValidateToolParts && part.type.startsWith("tool-")) {
|
|
10316
10377
|
const toolPart = part;
|
|
10317
10378
|
const toolName = toolPart.type.slice(5);
|
|
10318
|
-
const tool2 = tools
|
|
10379
|
+
const tool2 = getOwn(tools, toolName);
|
|
10319
10380
|
const isTerminal = toolPart.state === "output-available" || toolPart.state === "output-error" || toolPart.state === "output-denied";
|
|
10320
10381
|
if (!tool2 && isTerminal) {
|
|
10321
10382
|
if (tools != null || convertMissingTerminalToolsToDynamic) {
|
|
@@ -10632,11 +10693,6 @@ var DefaultEmbedResult = class {
|
|
|
10632
10693
|
}
|
|
10633
10694
|
};
|
|
10634
10695
|
|
|
10635
|
-
// src/util/get-own.ts
|
|
10636
|
-
function getOwn(obj, key) {
|
|
10637
|
-
return obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0;
|
|
10638
|
-
}
|
|
10639
|
-
|
|
10640
10696
|
// src/util/set-own.ts
|
|
10641
10697
|
function setOwn(object2, key, value) {
|
|
10642
10698
|
Object.defineProperty(object2, key, {
|
|
@@ -11953,17 +12009,24 @@ var SerialJobExecutor = class {
|
|
|
11953
12009
|
constructor() {
|
|
11954
12010
|
this.queue = [];
|
|
11955
12011
|
this.isProcessing = false;
|
|
12012
|
+
this.idleWaiters = [];
|
|
11956
12013
|
}
|
|
11957
12014
|
async processQueue() {
|
|
11958
12015
|
if (this.isProcessing) {
|
|
11959
12016
|
return;
|
|
11960
12017
|
}
|
|
11961
12018
|
this.isProcessing = true;
|
|
11962
|
-
|
|
11963
|
-
|
|
11964
|
-
|
|
12019
|
+
try {
|
|
12020
|
+
while (this.queue.length > 0) {
|
|
12021
|
+
await this.queue[0]();
|
|
12022
|
+
this.queue.shift();
|
|
12023
|
+
}
|
|
12024
|
+
} finally {
|
|
12025
|
+
this.isProcessing = false;
|
|
12026
|
+
for (const resolve3 of this.idleWaiters.splice(0)) {
|
|
12027
|
+
resolve3();
|
|
12028
|
+
}
|
|
11965
12029
|
}
|
|
11966
|
-
this.isProcessing = false;
|
|
11967
12030
|
}
|
|
11968
12031
|
async run(job) {
|
|
11969
12032
|
return new Promise((resolve3, reject) => {
|
|
@@ -11978,6 +12041,14 @@ var SerialJobExecutor = class {
|
|
|
11978
12041
|
void this.processQueue();
|
|
11979
12042
|
});
|
|
11980
12043
|
}
|
|
12044
|
+
async waitForIdle() {
|
|
12045
|
+
if (!this.isProcessing && this.queue.length === 0) {
|
|
12046
|
+
return;
|
|
12047
|
+
}
|
|
12048
|
+
await new Promise((resolve3) => {
|
|
12049
|
+
this.idleWaiters.push(resolve3);
|
|
12050
|
+
});
|
|
12051
|
+
}
|
|
11981
12052
|
};
|
|
11982
12053
|
|
|
11983
12054
|
// src/util/simulate-readable-stream.ts
|
|
@@ -14440,7 +14511,9 @@ var import_provider_utils42 = require("@ai-sdk/provider-utils");
|
|
|
14440
14511
|
var import_provider_utils41 = require("@ai-sdk/provider-utils");
|
|
14441
14512
|
function appendPathToUrl(url, path) {
|
|
14442
14513
|
const queryOrFragmentStart = url.search(/[?#]/);
|
|
14443
|
-
|
|
14514
|
+
const urlPath = queryOrFragmentStart === -1 ? url : url.slice(0, queryOrFragmentStart);
|
|
14515
|
+
const suffix = queryOrFragmentStart === -1 ? "" : url.slice(queryOrFragmentStart);
|
|
14516
|
+
return `${urlPath.endsWith("/") && path.startsWith("/") ? urlPath.slice(0, -1) : urlPath}${path}${suffix}`;
|
|
14444
14517
|
}
|
|
14445
14518
|
var HttpChatTransport = class {
|
|
14446
14519
|
constructor({
|
|
@@ -14611,6 +14684,8 @@ var AbstractChat = class {
|
|
|
14611
14684
|
this.activeResumeRequest = void 0;
|
|
14612
14685
|
this.resumableStreamState = void 0;
|
|
14613
14686
|
this.jobExecutor = new SerialJobExecutor();
|
|
14687
|
+
this.activeStopCount = 0;
|
|
14688
|
+
this.stopGeneration = 0;
|
|
14614
14689
|
/**
|
|
14615
14690
|
* Appends or replaces a user message to the chat list. This triggers the API call to fetch
|
|
14616
14691
|
* the assistant's response.
|
|
@@ -14754,17 +14829,18 @@ var AbstractChat = class {
|
|
|
14754
14829
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
14755
14830
|
}
|
|
14756
14831
|
if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
|
|
14757
|
-
this.shouldSendAutomatically()
|
|
14758
|
-
|
|
14759
|
-
|
|
14832
|
+
const shouldSend = await this.shouldSendAutomatically();
|
|
14833
|
+
if (shouldSend) {
|
|
14834
|
+
void this.runAutomaticRequest(() => {
|
|
14835
|
+
var _a23;
|
|
14760
14836
|
const messageId = messageIndex === -1 ? (_a23 = this.lastMessage) == null ? void 0 : _a23.id : messages[messageIndex].id;
|
|
14761
|
-
this.makeRequestForToolApproval({
|
|
14837
|
+
return this.makeRequestForToolApproval({
|
|
14762
14838
|
messageId,
|
|
14763
14839
|
messageIndex,
|
|
14764
14840
|
...options
|
|
14765
14841
|
});
|
|
14766
|
-
}
|
|
14767
|
-
}
|
|
14842
|
+
}, shouldSend);
|
|
14843
|
+
}
|
|
14768
14844
|
}
|
|
14769
14845
|
});
|
|
14770
14846
|
this.addToolOutput = async ({
|
|
@@ -14785,27 +14861,44 @@ var AbstractChat = class {
|
|
|
14785
14861
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
14786
14862
|
}
|
|
14787
14863
|
if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
|
|
14788
|
-
this.shouldSendAutomatically()
|
|
14789
|
-
|
|
14790
|
-
|
|
14791
|
-
|
|
14792
|
-
|
|
14793
|
-
|
|
14794
|
-
|
|
14795
|
-
|
|
14796
|
-
|
|
14797
|
-
|
|
14864
|
+
const shouldSend = await this.shouldSendAutomatically();
|
|
14865
|
+
if (shouldSend) {
|
|
14866
|
+
void this.runAutomaticRequest(
|
|
14867
|
+
() => {
|
|
14868
|
+
var _a23;
|
|
14869
|
+
return this.makeRequest({
|
|
14870
|
+
trigger: "submit-message",
|
|
14871
|
+
messageId: (_a23 = this.lastMessage) == null ? void 0 : _a23.id,
|
|
14872
|
+
...options
|
|
14873
|
+
});
|
|
14874
|
+
},
|
|
14875
|
+
shouldSend
|
|
14876
|
+
);
|
|
14877
|
+
}
|
|
14798
14878
|
}
|
|
14799
14879
|
});
|
|
14800
14880
|
/** @deprecated Use addToolOutput */
|
|
14801
14881
|
this.addToolResult = this.addToolOutput;
|
|
14802
14882
|
/**
|
|
14803
|
-
* Abort the current request
|
|
14883
|
+
* Abort the current request, keep the generated tokens if any, and wait for
|
|
14884
|
+
* the request pipeline to finish.
|
|
14804
14885
|
*/
|
|
14805
14886
|
this.stop = async () => {
|
|
14806
|
-
|
|
14807
|
-
|
|
14808
|
-
|
|
14887
|
+
this.activeStopCount++;
|
|
14888
|
+
this.stopGeneration++;
|
|
14889
|
+
try {
|
|
14890
|
+
const activeResumeRequest = this.activeResumeRequest;
|
|
14891
|
+
const activeResponse = this.activeResponse;
|
|
14892
|
+
activeResumeRequest == null ? void 0 : activeResumeRequest.abortController.abort();
|
|
14893
|
+
activeResponse == null ? void 0 : activeResponse.abortController.abort();
|
|
14894
|
+
await Promise.all([
|
|
14895
|
+
activeResumeRequest == null ? void 0 : activeResumeRequest.completionPromise,
|
|
14896
|
+
activeResponse == null ? void 0 : activeResponse.completionPromise
|
|
14897
|
+
]);
|
|
14898
|
+
await this.jobExecutor.waitForIdle();
|
|
14899
|
+
} finally {
|
|
14900
|
+
this.activeStopCount--;
|
|
14901
|
+
}
|
|
14809
14902
|
};
|
|
14810
14903
|
this.id = id;
|
|
14811
14904
|
this.transport = transport;
|
|
@@ -14834,8 +14927,12 @@ var AbstractChat = class {
|
|
|
14834
14927
|
status,
|
|
14835
14928
|
error
|
|
14836
14929
|
}) {
|
|
14837
|
-
if (this.status === status)
|
|
14930
|
+
if (this.status === status) {
|
|
14931
|
+
if (this.error !== error) {
|
|
14932
|
+
this.state.error = error;
|
|
14933
|
+
}
|
|
14838
14934
|
return;
|
|
14935
|
+
}
|
|
14839
14936
|
this.state.status = status;
|
|
14840
14937
|
this.state.error = error;
|
|
14841
14938
|
}
|
|
@@ -14862,6 +14959,17 @@ var AbstractChat = class {
|
|
|
14862
14959
|
}
|
|
14863
14960
|
return result;
|
|
14864
14961
|
}
|
|
14962
|
+
async runAutomaticRequest(request, shouldSend) {
|
|
14963
|
+
const stopGeneration = this.stopGeneration;
|
|
14964
|
+
const startedWhileStopping = this.activeStopCount > 0;
|
|
14965
|
+
if (!(shouldSend != null ? shouldSend : await this.shouldSendAutomatically())) {
|
|
14966
|
+
return;
|
|
14967
|
+
}
|
|
14968
|
+
if (startedWhileStopping || stopGeneration !== this.stopGeneration || this.activeStopCount > 0) {
|
|
14969
|
+
return;
|
|
14970
|
+
}
|
|
14971
|
+
await request();
|
|
14972
|
+
}
|
|
14865
14973
|
async makeRequestForToolApproval({
|
|
14866
14974
|
messageId,
|
|
14867
14975
|
messageIndex,
|
|
@@ -14881,19 +14989,31 @@ var AbstractChat = class {
|
|
|
14881
14989
|
this.pendingApprovalMessageId = (_b = (_a23 = this.state.messages[messageIndex]) == null ? void 0 : _a23.id) != null ? _b : messageId;
|
|
14882
14990
|
}
|
|
14883
14991
|
}
|
|
14884
|
-
async makeRequest({
|
|
14992
|
+
async makeRequest(options) {
|
|
14993
|
+
const completion = createResolvablePromise();
|
|
14994
|
+
try {
|
|
14995
|
+
await this.makeRequestImpl({
|
|
14996
|
+
...options,
|
|
14997
|
+
completionPromise: completion.promise
|
|
14998
|
+
});
|
|
14999
|
+
} finally {
|
|
15000
|
+
completion.resolve();
|
|
15001
|
+
}
|
|
15002
|
+
}
|
|
15003
|
+
async makeRequestImpl({
|
|
14885
15004
|
trigger,
|
|
14886
15005
|
metadata,
|
|
14887
15006
|
headers,
|
|
14888
15007
|
body,
|
|
14889
|
-
messageId
|
|
15008
|
+
messageId,
|
|
15009
|
+
completionPromise
|
|
14890
15010
|
}) {
|
|
14891
|
-
var _a23, _b
|
|
15011
|
+
var _a23, _b;
|
|
14892
15012
|
if (trigger !== "resume-stream") {
|
|
14893
15013
|
this.resumableStreamState = void 0;
|
|
14894
15014
|
}
|
|
14895
15015
|
const abortController = new AbortController();
|
|
14896
|
-
const activeResumeRequest = trigger === "resume-stream" ? { abortController } : void 0;
|
|
15016
|
+
const activeResumeRequest = trigger === "resume-stream" ? { abortController, completionPromise } : void 0;
|
|
14897
15017
|
if (activeResumeRequest) {
|
|
14898
15018
|
(_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
|
|
14899
15019
|
this.activeResumeRequest = activeResumeRequest;
|
|
@@ -14969,7 +15089,8 @@ var AbstractChat = class {
|
|
|
14969
15089
|
lastMessage: trigger === "resume-stream" ? resumableResponseMessage : trigger === "regenerate-message" ? void 0 : this.state.snapshot(responseMessage),
|
|
14970
15090
|
messageId: this.generateId()
|
|
14971
15091
|
}),
|
|
14972
|
-
abortController
|
|
15092
|
+
abortController,
|
|
15093
|
+
completionPromise
|
|
14973
15094
|
};
|
|
14974
15095
|
activeResponse = response;
|
|
14975
15096
|
response.abortController.signal.addEventListener("abort", () => {
|
|
@@ -15102,14 +15223,19 @@ var AbstractChat = class {
|
|
|
15102
15223
|
clearActiveResumeRequest();
|
|
15103
15224
|
}
|
|
15104
15225
|
}
|
|
15105
|
-
if (!
|
|
15106
|
-
await this.
|
|
15107
|
-
|
|
15108
|
-
|
|
15109
|
-
|
|
15110
|
-
|
|
15111
|
-
|
|
15112
|
-
|
|
15226
|
+
if (!isAbort && !isError) {
|
|
15227
|
+
await this.runAutomaticRequest(
|
|
15228
|
+
() => {
|
|
15229
|
+
var _a24;
|
|
15230
|
+
return this.makeRequest({
|
|
15231
|
+
trigger: "submit-message",
|
|
15232
|
+
messageId: (_a24 = this.lastMessage) == null ? void 0 : _a24.id,
|
|
15233
|
+
metadata,
|
|
15234
|
+
headers,
|
|
15235
|
+
body
|
|
15236
|
+
});
|
|
15237
|
+
}
|
|
15238
|
+
);
|
|
15113
15239
|
}
|
|
15114
15240
|
}
|
|
15115
15241
|
};
|