ai 6.0.295 → 6.0.297
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 +22 -0
- package/dist/index.d.mts +42 -11
- package/dist/index.d.ts +42 -11
- package/dist/index.js +210 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +210 -43
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +10 -1
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +8 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +10 -3
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +10 -1
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +3 -3
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +2 -2
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +3 -3
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +7 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +7 -0
- package/package.json +2 -2
- package/src/agent/create-agent-ui-stream-response.ts +5 -2
- package/src/agent/create-agent-ui-stream.ts +5 -3
- package/src/agent/pipe-agent-ui-stream-to-response.ts +5 -2
- package/src/embed/embed-many.ts +5 -3
- package/src/generate-image/generate-image.ts +7 -2
- package/src/generate-text/smooth-stream.ts +24 -9
- package/src/generate-text/stream-text-result.ts +5 -0
- package/src/generate-text/stream-text.ts +4 -1
- package/src/generate-video/generate-video.ts +6 -4
- package/src/registry/provider-registry.ts +1 -1
- package/src/ui/chat.ts +13 -4
- package/src/ui/http-chat-transport.ts +27 -1
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +15 -3
- package/src/ui/process-ui-message-stream.ts +59 -18
- package/src/ui/ui-messages.ts +14 -0
- package/src/ui/validate-ui-messages.ts +2 -0
- package/src/ui-message-stream/create-sse-stream-with-keep-alive.ts +89 -0
- package/src/ui-message-stream/create-ui-message-stream-response.ts +7 -1
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +7 -1
- package/src/ui-message-stream/ui-message-stream-response-init.ts +7 -0
- package/src/util/get-own.ts +18 -0
- package/src/util/set-own.ts +16 -0
package/dist/index.mjs
CHANGED
|
@@ -1224,7 +1224,7 @@ import {
|
|
|
1224
1224
|
} from "@ai-sdk/provider-utils";
|
|
1225
1225
|
|
|
1226
1226
|
// src/version.ts
|
|
1227
|
-
var VERSION = true ? "6.0.
|
|
1227
|
+
var VERSION = true ? "6.0.297" : "0.0.0-test";
|
|
1228
1228
|
|
|
1229
1229
|
// src/util/download/download.ts
|
|
1230
1230
|
var download = async ({
|
|
@@ -5639,6 +5639,75 @@ function pipeTextStreamToResponse({
|
|
|
5639
5639
|
});
|
|
5640
5640
|
}
|
|
5641
5641
|
|
|
5642
|
+
// src/ui-message-stream/create-sse-stream-with-keep-alive.ts
|
|
5643
|
+
var STREAM_OPEN_COMMENT = ": stream-open\n\n";
|
|
5644
|
+
var KEEP_ALIVE_COMMENT = ": keep-alive\n\n";
|
|
5645
|
+
function createSseStreamWithKeepAlive({
|
|
5646
|
+
stream,
|
|
5647
|
+
keepAliveMs
|
|
5648
|
+
}) {
|
|
5649
|
+
if (keepAliveMs == null) {
|
|
5650
|
+
return stream;
|
|
5651
|
+
}
|
|
5652
|
+
if (!Number.isFinite(keepAliveMs) || keepAliveMs <= 0 || keepAliveMs > 2147483647) {
|
|
5653
|
+
throw new Error(
|
|
5654
|
+
"keepAliveMs must be a positive finite timer duration no greater than 2147483647"
|
|
5655
|
+
);
|
|
5656
|
+
}
|
|
5657
|
+
const reader = stream.getReader();
|
|
5658
|
+
let keepAliveTimeout;
|
|
5659
|
+
let isCancelled = false;
|
|
5660
|
+
const clearKeepAliveTimeout = () => {
|
|
5661
|
+
clearTimeout(keepAliveTimeout);
|
|
5662
|
+
keepAliveTimeout = void 0;
|
|
5663
|
+
};
|
|
5664
|
+
const scheduleKeepAlive = (controller) => {
|
|
5665
|
+
clearKeepAliveTimeout();
|
|
5666
|
+
keepAliveTimeout = setTimeout(() => {
|
|
5667
|
+
if (isCancelled) {
|
|
5668
|
+
return;
|
|
5669
|
+
}
|
|
5670
|
+
if (controller.desiredSize != null && controller.desiredSize > 0) {
|
|
5671
|
+
controller.enqueue(KEEP_ALIVE_COMMENT);
|
|
5672
|
+
}
|
|
5673
|
+
scheduleKeepAlive(controller);
|
|
5674
|
+
}, keepAliveMs);
|
|
5675
|
+
};
|
|
5676
|
+
return new ReadableStream({
|
|
5677
|
+
start(controller) {
|
|
5678
|
+
controller.enqueue(STREAM_OPEN_COMMENT);
|
|
5679
|
+
scheduleKeepAlive(controller);
|
|
5680
|
+
},
|
|
5681
|
+
pull(controller) {
|
|
5682
|
+
return reader.read().then(
|
|
5683
|
+
(result) => {
|
|
5684
|
+
clearKeepAliveTimeout();
|
|
5685
|
+
if (isCancelled) {
|
|
5686
|
+
return;
|
|
5687
|
+
}
|
|
5688
|
+
if (result.done) {
|
|
5689
|
+
controller.close();
|
|
5690
|
+
} else {
|
|
5691
|
+
controller.enqueue(result.value);
|
|
5692
|
+
scheduleKeepAlive(controller);
|
|
5693
|
+
}
|
|
5694
|
+
},
|
|
5695
|
+
(error) => {
|
|
5696
|
+
clearKeepAliveTimeout();
|
|
5697
|
+
if (!isCancelled) {
|
|
5698
|
+
throw error;
|
|
5699
|
+
}
|
|
5700
|
+
}
|
|
5701
|
+
);
|
|
5702
|
+
},
|
|
5703
|
+
async cancel(reason) {
|
|
5704
|
+
isCancelled = true;
|
|
5705
|
+
clearKeepAliveTimeout();
|
|
5706
|
+
await reader.cancel(reason);
|
|
5707
|
+
}
|
|
5708
|
+
});
|
|
5709
|
+
}
|
|
5710
|
+
|
|
5642
5711
|
// src/ui-message-stream/json-to-sse-transform-stream.ts
|
|
5643
5712
|
var JsonToSseTransformStream = class extends TransformStream {
|
|
5644
5713
|
constructor() {
|
|
@@ -5671,9 +5740,13 @@ function createUIMessageStreamResponse({
|
|
|
5671
5740
|
statusText,
|
|
5672
5741
|
headers,
|
|
5673
5742
|
stream,
|
|
5743
|
+
keepAliveMs,
|
|
5674
5744
|
consumeSseStream
|
|
5675
5745
|
}) {
|
|
5676
|
-
let sseStream =
|
|
5746
|
+
let sseStream = createSseStreamWithKeepAlive({
|
|
5747
|
+
stream: stream.pipeThrough(new JsonToSseTransformStream()),
|
|
5748
|
+
keepAliveMs
|
|
5749
|
+
});
|
|
5677
5750
|
if (consumeSseStream) {
|
|
5678
5751
|
const [stream1, stream2] = sseStream.tee();
|
|
5679
5752
|
sseStream = stream1;
|
|
@@ -5925,16 +5998,43 @@ function createStreamingUIMessageState({
|
|
|
5925
5998
|
lastMessage,
|
|
5926
5999
|
messageId
|
|
5927
6000
|
}) {
|
|
6001
|
+
var _a23;
|
|
6002
|
+
const message = (lastMessage == null ? void 0 : lastMessage.role) === "assistant" ? lastMessage : {
|
|
6003
|
+
id: messageId,
|
|
6004
|
+
metadata: void 0,
|
|
6005
|
+
role: "assistant",
|
|
6006
|
+
parts: []
|
|
6007
|
+
};
|
|
6008
|
+
const partialToolCalls = createIdMap();
|
|
6009
|
+
const lastStepStartIndex = message.parts.findLastIndex(
|
|
6010
|
+
(part) => part.type === "step-start"
|
|
6011
|
+
);
|
|
6012
|
+
let staticToolIndex = 0;
|
|
6013
|
+
for (const part of message.parts.slice(lastStepStartIndex + 1)) {
|
|
6014
|
+
if (!isToolUIPart(part)) {
|
|
6015
|
+
continue;
|
|
6016
|
+
}
|
|
6017
|
+
const index = staticToolIndex;
|
|
6018
|
+
if (isStaticToolUIPart(part)) {
|
|
6019
|
+
staticToolIndex++;
|
|
6020
|
+
}
|
|
6021
|
+
if (part.state !== "input-streaming") {
|
|
6022
|
+
continue;
|
|
6023
|
+
}
|
|
6024
|
+
partialToolCalls[part.toolCallId] = {
|
|
6025
|
+
text: (_a23 = part.rawInput) != null ? _a23 : "",
|
|
6026
|
+
index,
|
|
6027
|
+
toolName: part.type === "dynamic-tool" ? part.toolName : getStaticToolName(part),
|
|
6028
|
+
dynamic: part.type === "dynamic-tool",
|
|
6029
|
+
title: part.title,
|
|
6030
|
+
toolMetadata: part.toolMetadata
|
|
6031
|
+
};
|
|
6032
|
+
}
|
|
5928
6033
|
return {
|
|
5929
|
-
message
|
|
5930
|
-
id: messageId,
|
|
5931
|
-
metadata: void 0,
|
|
5932
|
-
role: "assistant",
|
|
5933
|
-
parts: []
|
|
5934
|
-
},
|
|
6034
|
+
message,
|
|
5935
6035
|
activeTextParts: createIdMap(),
|
|
5936
6036
|
activeReasoningParts: createIdMap(),
|
|
5937
|
-
partialToolCalls
|
|
6037
|
+
partialToolCalls
|
|
5938
6038
|
};
|
|
5939
6039
|
}
|
|
5940
6040
|
function processUIMessageStream({
|
|
@@ -5950,7 +6050,7 @@ function processUIMessageStream({
|
|
|
5950
6050
|
new TransformStream({
|
|
5951
6051
|
async transform(chunk, controller) {
|
|
5952
6052
|
await runUpdateMessageJob(async ({ state, write }) => {
|
|
5953
|
-
var _a23, _b, _c, _d;
|
|
6053
|
+
var _a23, _b, _c, _d, _e, _f, _g, _h;
|
|
5954
6054
|
function getCurrentStepParts() {
|
|
5955
6055
|
const parts = state.message.parts;
|
|
5956
6056
|
let currentStepStartIndex = parts.length - 1;
|
|
@@ -6035,7 +6135,7 @@ function processUIMessageStream({
|
|
|
6035
6135
|
}
|
|
6036
6136
|
}
|
|
6037
6137
|
function updateDynamicToolPart(options, existingPart) {
|
|
6038
|
-
var _a24
|
|
6138
|
+
var _a24;
|
|
6039
6139
|
const part = existingPart != null ? existingPart : getCurrentStepParts().find(
|
|
6040
6140
|
(part2) => part2.type === "dynamic-tool" && part2.toolCallId === options.toolCallId
|
|
6041
6141
|
);
|
|
@@ -6047,7 +6147,7 @@ function processUIMessageStream({
|
|
|
6047
6147
|
anyPart.input = anyOptions.input;
|
|
6048
6148
|
anyPart.output = anyOptions.output;
|
|
6049
6149
|
anyPart.errorText = anyOptions.errorText;
|
|
6050
|
-
anyPart.rawInput =
|
|
6150
|
+
anyPart.rawInput = anyOptions.rawInput;
|
|
6051
6151
|
anyPart.preliminary = anyOptions.preliminary;
|
|
6052
6152
|
if (options.title !== void 0) {
|
|
6053
6153
|
anyPart.title = options.title;
|
|
@@ -6055,7 +6155,7 @@ function processUIMessageStream({
|
|
|
6055
6155
|
if (options.toolMetadata !== void 0) {
|
|
6056
6156
|
anyPart.toolMetadata = options.toolMetadata;
|
|
6057
6157
|
}
|
|
6058
|
-
anyPart.providerExecuted = (
|
|
6158
|
+
anyPart.providerExecuted = (_a24 = anyOptions.providerExecuted) != null ? _a24 : part.providerExecuted;
|
|
6059
6159
|
const providerMetadata = anyOptions.providerMetadata;
|
|
6060
6160
|
if (providerMetadata != null) {
|
|
6061
6161
|
if (options.state === "output-available" || options.state === "output-error") {
|
|
@@ -6271,6 +6371,7 @@ function processUIMessageStream({
|
|
|
6271
6371
|
toolName: partialToolCall.toolName,
|
|
6272
6372
|
state: "input-streaming",
|
|
6273
6373
|
input: partialArgs,
|
|
6374
|
+
rawInput: partialToolCall.text,
|
|
6274
6375
|
title: partialToolCall.title,
|
|
6275
6376
|
toolMetadata: partialToolCall.toolMetadata
|
|
6276
6377
|
});
|
|
@@ -6280,6 +6381,7 @@ function processUIMessageStream({
|
|
|
6280
6381
|
toolName: partialToolCall.toolName,
|
|
6281
6382
|
state: "input-streaming",
|
|
6282
6383
|
input: partialArgs,
|
|
6384
|
+
rawInput: partialToolCall.text,
|
|
6283
6385
|
title: partialToolCall.title,
|
|
6284
6386
|
toolMetadata: partialToolCall.toolMetadata
|
|
6285
6387
|
});
|
|
@@ -6384,7 +6486,7 @@ function processUIMessageStream({
|
|
|
6384
6486
|
providerExecuted: chunk.providerExecuted,
|
|
6385
6487
|
providerMetadata: chunk.providerMetadata,
|
|
6386
6488
|
title: toolInvocation.title,
|
|
6387
|
-
toolMetadata: toolInvocation.toolMetadata
|
|
6489
|
+
toolMetadata: (_e = chunk.toolMetadata) != null ? _e : toolInvocation.toolMetadata
|
|
6388
6490
|
},
|
|
6389
6491
|
toolInvocation
|
|
6390
6492
|
);
|
|
@@ -6400,7 +6502,7 @@ function processUIMessageStream({
|
|
|
6400
6502
|
preliminary: chunk.preliminary,
|
|
6401
6503
|
providerMetadata: chunk.providerMetadata,
|
|
6402
6504
|
title: toolInvocation.title,
|
|
6403
|
-
toolMetadata: toolInvocation.toolMetadata
|
|
6505
|
+
toolMetadata: (_f = chunk.toolMetadata) != null ? _f : toolInvocation.toolMetadata
|
|
6404
6506
|
},
|
|
6405
6507
|
toolInvocation
|
|
6406
6508
|
);
|
|
@@ -6421,7 +6523,7 @@ function processUIMessageStream({
|
|
|
6421
6523
|
providerExecuted: chunk.providerExecuted,
|
|
6422
6524
|
providerMetadata: chunk.providerMetadata,
|
|
6423
6525
|
title: toolInvocation.title,
|
|
6424
|
-
toolMetadata: toolInvocation.toolMetadata
|
|
6526
|
+
toolMetadata: (_g = chunk.toolMetadata) != null ? _g : toolInvocation.toolMetadata
|
|
6425
6527
|
},
|
|
6426
6528
|
toolInvocation
|
|
6427
6529
|
);
|
|
@@ -6437,7 +6539,7 @@ function processUIMessageStream({
|
|
|
6437
6539
|
providerExecuted: chunk.providerExecuted,
|
|
6438
6540
|
providerMetadata: chunk.providerMetadata,
|
|
6439
6541
|
title: toolInvocation.title,
|
|
6440
|
-
toolMetadata: toolInvocation.toolMetadata
|
|
6542
|
+
toolMetadata: (_h = chunk.toolMetadata) != null ? _h : toolInvocation.toolMetadata
|
|
6441
6543
|
},
|
|
6442
6544
|
toolInvocation
|
|
6443
6545
|
);
|
|
@@ -6668,9 +6770,13 @@ function pipeUIMessageStreamToResponse({
|
|
|
6668
6770
|
statusText,
|
|
6669
6771
|
headers,
|
|
6670
6772
|
stream,
|
|
6773
|
+
keepAliveMs,
|
|
6671
6774
|
consumeSseStream
|
|
6672
6775
|
}) {
|
|
6673
|
-
let sseStream =
|
|
6776
|
+
let sseStream = createSseStreamWithKeepAlive({
|
|
6777
|
+
stream: stream.pipeThrough(new JsonToSseTransformStream()),
|
|
6778
|
+
keepAliveMs
|
|
6779
|
+
});
|
|
6674
6780
|
if (consumeSseStream) {
|
|
6675
6781
|
const [stream1, stream2] = sseStream.tee();
|
|
6676
6782
|
sseStream = stream1;
|
|
@@ -8834,7 +8940,7 @@ var DefaultStreamTextResult = class {
|
|
|
8834
8940
|
sendStart = true,
|
|
8835
8941
|
sendFinish = true,
|
|
8836
8942
|
onError = () => "An error occurred."
|
|
8837
|
-
//
|
|
8943
|
+
// masks errors except provider-executed tool execution errors
|
|
8838
8944
|
} = {}) {
|
|
8839
8945
|
let outcome = { status: "unknown" };
|
|
8840
8946
|
let hasFatalFailure = false;
|
|
@@ -9883,6 +9989,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
9883
9989
|
toolMetadata: toolMetadataSchema2.optional(),
|
|
9884
9990
|
state: z8.literal("input-streaming"),
|
|
9885
9991
|
input: z8.unknown().optional(),
|
|
9992
|
+
rawInput: z8.string().optional(),
|
|
9886
9993
|
providerExecuted: z8.boolean().optional(),
|
|
9887
9994
|
callProviderMetadata: providerMetadataSchema.optional(),
|
|
9888
9995
|
output: z8.never().optional(),
|
|
@@ -9979,6 +10086,7 @@ var uiMessagesSchema = lazySchema2(() => {
|
|
|
9979
10086
|
providerExecuted: z8.boolean().optional(),
|
|
9980
10087
|
callProviderMetadata: providerMetadataSchema.optional(),
|
|
9981
10088
|
input: z8.unknown().optional(),
|
|
10089
|
+
rawInput: z8.string().optional(),
|
|
9982
10090
|
output: z8.never().optional(),
|
|
9983
10091
|
errorText: z8.never().optional(),
|
|
9984
10092
|
approval: z8.never().optional()
|
|
@@ -10272,8 +10380,9 @@ async function createAgentUIStream({
|
|
|
10272
10380
|
...uiMessageStreamOptions
|
|
10273
10381
|
}) {
|
|
10274
10382
|
var _a23;
|
|
10383
|
+
const inputMessages = uiMessages != null ? uiMessages : uiMessageStreamOptions.originalMessages;
|
|
10275
10384
|
const validatedMessages = await validateUIMessagesForAgent({
|
|
10276
|
-
messages:
|
|
10385
|
+
messages: inputMessages,
|
|
10277
10386
|
tools: agent.tools
|
|
10278
10387
|
});
|
|
10279
10388
|
const modelMessages = await convertToModelMessages(validatedMessages, {
|
|
@@ -10299,6 +10408,7 @@ async function createAgentUIStreamResponse({
|
|
|
10299
10408
|
headers,
|
|
10300
10409
|
status,
|
|
10301
10410
|
statusText,
|
|
10411
|
+
keepAliveMs,
|
|
10302
10412
|
consumeSseStream,
|
|
10303
10413
|
...options
|
|
10304
10414
|
}) {
|
|
@@ -10306,6 +10416,7 @@ async function createAgentUIStreamResponse({
|
|
|
10306
10416
|
headers,
|
|
10307
10417
|
status,
|
|
10308
10418
|
statusText,
|
|
10419
|
+
keepAliveMs,
|
|
10309
10420
|
consumeSseStream,
|
|
10310
10421
|
stream: await createAgentUIStream(options)
|
|
10311
10422
|
});
|
|
@@ -10317,6 +10428,7 @@ async function pipeAgentUIStreamToResponse({
|
|
|
10317
10428
|
headers,
|
|
10318
10429
|
status,
|
|
10319
10430
|
statusText,
|
|
10431
|
+
keepAliveMs,
|
|
10320
10432
|
consumeSseStream,
|
|
10321
10433
|
...options
|
|
10322
10434
|
}) {
|
|
@@ -10325,6 +10437,7 @@ async function pipeAgentUIStreamToResponse({
|
|
|
10325
10437
|
headers,
|
|
10326
10438
|
status,
|
|
10327
10439
|
statusText,
|
|
10440
|
+
keepAliveMs,
|
|
10328
10441
|
consumeSseStream,
|
|
10329
10442
|
stream: await createAgentUIStream(options)
|
|
10330
10443
|
});
|
|
@@ -10461,6 +10574,21 @@ var DefaultEmbedResult = class {
|
|
|
10461
10574
|
}
|
|
10462
10575
|
};
|
|
10463
10576
|
|
|
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
|
+
// src/util/set-own.ts
|
|
10583
|
+
function setOwn(object2, key, value) {
|
|
10584
|
+
Object.defineProperty(object2, key, {
|
|
10585
|
+
value,
|
|
10586
|
+
enumerable: true,
|
|
10587
|
+
configurable: true,
|
|
10588
|
+
writable: true
|
|
10589
|
+
});
|
|
10590
|
+
}
|
|
10591
|
+
|
|
10464
10592
|
// src/embed/embed-many.ts
|
|
10465
10593
|
import {
|
|
10466
10594
|
withUserAgentSuffix as withUserAgentSuffix4
|
|
@@ -10714,10 +10842,10 @@ async function embedMany({
|
|
|
10714
10842
|
for (const [providerName, metadata] of Object.entries(
|
|
10715
10843
|
result.providerMetadata
|
|
10716
10844
|
)) {
|
|
10717
|
-
providerMetadata
|
|
10718
|
-
...providerMetadata
|
|
10845
|
+
setOwn(providerMetadata, providerName, {
|
|
10846
|
+
...getOwn(providerMetadata, providerName),
|
|
10719
10847
|
...metadata
|
|
10720
|
-
};
|
|
10848
|
+
});
|
|
10721
10849
|
}
|
|
10722
10850
|
}
|
|
10723
10851
|
}
|
|
@@ -10916,10 +11044,13 @@ async function generateImage({
|
|
|
10916
11044
|
delete providerMetadata[providerName].images;
|
|
10917
11045
|
}
|
|
10918
11046
|
} else {
|
|
10919
|
-
(_b = providerMetadata
|
|
10920
|
-
|
|
11047
|
+
const currentEntry = (_b = getOwn(providerMetadata, providerName)) != null ? _b : {
|
|
11048
|
+
images: []
|
|
11049
|
+
};
|
|
11050
|
+
currentEntry.images.push(
|
|
10921
11051
|
...result.providerMetadata[providerName].images
|
|
10922
11052
|
);
|
|
11053
|
+
setOwn(providerMetadata, providerName, currentEntry);
|
|
10923
11054
|
}
|
|
10924
11055
|
}
|
|
10925
11056
|
}
|
|
@@ -12704,21 +12835,32 @@ function smoothStream({
|
|
|
12704
12835
|
controller.enqueue(chunk);
|
|
12705
12836
|
return;
|
|
12706
12837
|
}
|
|
12707
|
-
if (
|
|
12838
|
+
if (chunk.text.length === 0 && chunk.providerMetadata != null) {
|
|
12839
|
+
flushBuffer(controller);
|
|
12840
|
+
controller.enqueue(chunk);
|
|
12841
|
+
return;
|
|
12842
|
+
}
|
|
12843
|
+
if (buffer.length > 0 && (chunk.type !== type || chunk.id !== id || providerMetadata != null || chunk.providerMetadata != null)) {
|
|
12708
12844
|
flushBuffer(controller);
|
|
12709
12845
|
}
|
|
12710
12846
|
buffer += chunk.text;
|
|
12711
12847
|
id = chunk.id;
|
|
12712
12848
|
type = chunk.type;
|
|
12713
|
-
|
|
12714
|
-
providerMetadata = chunk.providerMetadata;
|
|
12715
|
-
}
|
|
12849
|
+
providerMetadata = chunk.providerMetadata;
|
|
12716
12850
|
let match;
|
|
12717
12851
|
while ((match = detectChunk(buffer)) != null) {
|
|
12718
|
-
controller.enqueue({
|
|
12852
|
+
controller.enqueue({
|
|
12853
|
+
type,
|
|
12854
|
+
text: match,
|
|
12855
|
+
id,
|
|
12856
|
+
...providerMetadata != null ? { providerMetadata } : {}
|
|
12857
|
+
});
|
|
12719
12858
|
buffer = buffer.slice(match.length);
|
|
12720
12859
|
await delay(isDocumentHidden() ? null : delayInMs);
|
|
12721
12860
|
}
|
|
12861
|
+
if (buffer.length === 0) {
|
|
12862
|
+
providerMetadata = void 0;
|
|
12863
|
+
}
|
|
12722
12864
|
}
|
|
12723
12865
|
});
|
|
12724
12866
|
};
|
|
@@ -12873,12 +13015,12 @@ async function experimental_generateVideo({
|
|
|
12873
13015
|
for (const [providerName, metadata] of Object.entries(
|
|
12874
13016
|
result.providerMetadata
|
|
12875
13017
|
)) {
|
|
12876
|
-
const existingMetadata = providerMetadata
|
|
13018
|
+
const existingMetadata = getOwn(providerMetadata, providerName);
|
|
12877
13019
|
if (existingMetadata != null && typeof existingMetadata === "object") {
|
|
12878
|
-
providerMetadata
|
|
13020
|
+
setOwn(providerMetadata, providerName, {
|
|
12879
13021
|
...existingMetadata,
|
|
12880
13022
|
...metadata
|
|
12881
|
-
};
|
|
13023
|
+
});
|
|
12882
13024
|
if ("videos" in existingMetadata && Array.isArray(existingMetadata.videos) && "videos" in metadata && Array.isArray(metadata.videos)) {
|
|
12883
13025
|
providerMetadata[providerName].videos = [
|
|
12884
13026
|
...existingMetadata.videos,
|
|
@@ -12886,7 +13028,7 @@ async function experimental_generateVideo({
|
|
|
12886
13028
|
];
|
|
12887
13029
|
}
|
|
12888
13030
|
} else {
|
|
12889
|
-
providerMetadata
|
|
13031
|
+
setOwn(providerMetadata, providerName, metadata);
|
|
12890
13032
|
}
|
|
12891
13033
|
}
|
|
12892
13034
|
}
|
|
@@ -13764,7 +13906,7 @@ var DefaultProviderRegistry = class {
|
|
|
13764
13906
|
languageModelMiddleware,
|
|
13765
13907
|
imageModelMiddleware
|
|
13766
13908
|
}) {
|
|
13767
|
-
this.providers =
|
|
13909
|
+
this.providers = /* @__PURE__ */ Object.create(null);
|
|
13768
13910
|
this.separator = separator;
|
|
13769
13911
|
this.languageModelMiddleware = languageModelMiddleware;
|
|
13770
13912
|
this.imageModelMiddleware = imageModelMiddleware;
|
|
@@ -14286,6 +14428,10 @@ import {
|
|
|
14286
14428
|
normalizeHeaders,
|
|
14287
14429
|
resolve as resolve2
|
|
14288
14430
|
} from "@ai-sdk/provider-utils";
|
|
14431
|
+
function appendPathToUrl(url, path) {
|
|
14432
|
+
const queryOrFragmentStart = url.search(/[?#]/);
|
|
14433
|
+
return queryOrFragmentStart === -1 ? `${url}${path}` : `${url.slice(0, queryOrFragmentStart)}${path}${url.slice(queryOrFragmentStart)}`;
|
|
14434
|
+
}
|
|
14289
14435
|
var HttpChatTransport = class {
|
|
14290
14436
|
constructor({
|
|
14291
14437
|
api = "/api/chat",
|
|
@@ -14360,7 +14506,7 @@ var HttpChatTransport = class {
|
|
|
14360
14506
|
return this.processResponseStream(response.body);
|
|
14361
14507
|
}
|
|
14362
14508
|
async reconnectToStream(options) {
|
|
14363
|
-
var _a23, _b, _c, _d
|
|
14509
|
+
var _a23, _b, _c, _d;
|
|
14364
14510
|
const resolvedBody = await resolve2(this.body);
|
|
14365
14511
|
const resolvedHeaders = await resolve2(this.headers);
|
|
14366
14512
|
const resolvedCredentials = await resolve2(this.credentials);
|
|
@@ -14376,10 +14522,23 @@ var HttpChatTransport = class {
|
|
|
14376
14522
|
credentials: resolvedCredentials,
|
|
14377
14523
|
requestMetadata: options.metadata
|
|
14378
14524
|
}));
|
|
14379
|
-
|
|
14525
|
+
let api = preparedRequest == null ? void 0 : preparedRequest.api;
|
|
14526
|
+
if (api == null) {
|
|
14527
|
+
if (options.chatId === "." || options.chatId === "..") {
|
|
14528
|
+
throw new InvalidArgumentError({
|
|
14529
|
+
parameter: "chatId",
|
|
14530
|
+
value: options.chatId,
|
|
14531
|
+
message: 'Chat IDs must not be "." or ".." when using the default reconnect URL.'
|
|
14532
|
+
});
|
|
14533
|
+
}
|
|
14534
|
+
api = appendPathToUrl(
|
|
14535
|
+
this.api,
|
|
14536
|
+
`/${encodeURIComponent(options.chatId)}/stream`
|
|
14537
|
+
);
|
|
14538
|
+
}
|
|
14380
14539
|
const headers = (preparedRequest == null ? void 0 : preparedRequest.headers) !== void 0 ? normalizeHeaders(preparedRequest.headers) : baseHeaders;
|
|
14381
|
-
const credentials = (
|
|
14382
|
-
const fetch2 = (
|
|
14540
|
+
const credentials = (_b = preparedRequest == null ? void 0 : preparedRequest.credentials) != null ? _b : resolvedCredentials;
|
|
14541
|
+
const fetch2 = (_c = this.fetch) != null ? _c : globalThis.fetch;
|
|
14383
14542
|
const response = await fetch2(api, {
|
|
14384
14543
|
method: "GET",
|
|
14385
14544
|
headers,
|
|
@@ -14391,7 +14550,7 @@ var HttpChatTransport = class {
|
|
|
14391
14550
|
}
|
|
14392
14551
|
if (!response.ok) {
|
|
14393
14552
|
throw new Error(
|
|
14394
|
-
(
|
|
14553
|
+
(_d = await response.text()) != null ? _d : "Failed to fetch the chat response."
|
|
14395
14554
|
);
|
|
14396
14555
|
}
|
|
14397
14556
|
if (!response.body) {
|
|
@@ -14786,6 +14945,9 @@ var AbstractChat = class {
|
|
|
14786
14945
|
const lastMessage = this.lastMessage;
|
|
14787
14946
|
const responseMessageIndex = trigger === "submit-message" && messageId != null ? this.state.messages.findIndex((message) => message.id === messageId) : this.state.messages.length - 1;
|
|
14788
14947
|
const responseMessage = responseMessageIndex === -1 ? lastMessage : this.state.messages[responseMessageIndex];
|
|
14948
|
+
const resumableResponseMessage = trigger === "resume-stream" && (responseMessage == null ? void 0 : responseMessage.role) === "assistant" && responseMessage.parts.some(
|
|
14949
|
+
(part) => isToolUIPart(part) && part.state === "input-streaming"
|
|
14950
|
+
) ? this.state.snapshot(responseMessage) : void 0;
|
|
14789
14951
|
const usesEarlierAssistantMessage = responseMessageIndex !== -1 && responseMessageIndex < this.state.messages.length - 1 && (responseMessage == null ? void 0 : responseMessage.role) === "assistant";
|
|
14790
14952
|
let isAbort = false;
|
|
14791
14953
|
let isDisconnect = false;
|
|
@@ -14794,7 +14956,7 @@ var AbstractChat = class {
|
|
|
14794
14956
|
try {
|
|
14795
14957
|
const response = {
|
|
14796
14958
|
state: trigger === "resume-stream" && this.resumableStreamState != null ? this.resumableStreamState : createStreamingUIMessageState({
|
|
14797
|
-
lastMessage: trigger === "resume-stream"
|
|
14959
|
+
lastMessage: trigger === "resume-stream" ? resumableResponseMessage : trigger === "regenerate-message" ? void 0 : this.state.snapshot(responseMessage),
|
|
14798
14960
|
messageId: this.generateId()
|
|
14799
14961
|
}),
|
|
14800
14962
|
abortController
|
|
@@ -15020,8 +15182,13 @@ function lastAssistantMessageIsCompleteWithToolCalls({
|
|
|
15020
15182
|
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
|
15021
15183
|
return part.type === "step-start" ? index : lastIndex;
|
|
15022
15184
|
}, -1);
|
|
15023
|
-
const
|
|
15024
|
-
|
|
15185
|
+
const lastStepParts = message.parts.slice(lastStepStartIndex + 1);
|
|
15186
|
+
const lastStepToolInvocations = lastStepParts.filter(isToolUIPart).filter((part) => !part.providerExecuted);
|
|
15187
|
+
const lastToolInvocationIndex = lastStepParts.reduce(
|
|
15188
|
+
(lastIndex, part, index) => isToolUIPart(part) && !part.providerExecuted ? index : lastIndex,
|
|
15189
|
+
-1
|
|
15190
|
+
);
|
|
15191
|
+
return lastStepToolInvocations.length > 0 && !lastStepParts.slice(lastToolInvocationIndex + 1).some((part) => part.type === "text" && part.state !== "done") && lastStepToolInvocations.every(
|
|
15025
15192
|
(part) => part.state === "output-available" && part.preliminary !== true || part.state === "output-error"
|
|
15026
15193
|
);
|
|
15027
15194
|
}
|