ai 4.1.11 → 4.1.12
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 +10 -0
- package/dist/index.js +113 -77
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +93 -55
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/rsc/dist/rsc-server.mjs +54 -37
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# ai
|
2
2
|
|
3
|
+
## 4.1.12
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 0d2d9bf: fix (ui): single assistant message with multiple tool steps
|
8
|
+
- Updated dependencies [0d2d9bf]
|
9
|
+
- Updated dependencies [0d2d9bf]
|
10
|
+
- @ai-sdk/react@1.1.7
|
11
|
+
- @ai-sdk/ui-utils@1.1.7
|
12
|
+
|
3
13
|
## 4.1.11
|
4
14
|
|
5
15
|
### Patch Changes
|
package/dist/index.js
CHANGED
@@ -69,17 +69,17 @@ __export(streams_exports, {
|
|
69
69
|
experimental_generateImage: () => generateImage,
|
70
70
|
experimental_wrapLanguageModel: () => experimental_wrapLanguageModel,
|
71
71
|
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
72
|
-
formatAssistantStreamPart: () =>
|
73
|
-
formatDataStreamPart: () =>
|
72
|
+
formatAssistantStreamPart: () => import_ui_utils10.formatAssistantStreamPart,
|
73
|
+
formatDataStreamPart: () => import_ui_utils10.formatDataStreamPart,
|
74
74
|
generateId: () => import_provider_utils14.generateId,
|
75
75
|
generateObject: () => generateObject,
|
76
76
|
generateText: () => generateText,
|
77
|
-
jsonSchema: () =>
|
78
|
-
parseAssistantStreamPart: () =>
|
79
|
-
parseDataStreamPart: () =>
|
77
|
+
jsonSchema: () => import_ui_utils10.jsonSchema,
|
78
|
+
parseAssistantStreamPart: () => import_ui_utils10.parseAssistantStreamPart,
|
79
|
+
parseDataStreamPart: () => import_ui_utils10.parseDataStreamPart,
|
80
80
|
pipeDataStreamToResponse: () => pipeDataStreamToResponse,
|
81
|
-
processDataStream: () =>
|
82
|
-
processTextStream: () =>
|
81
|
+
processDataStream: () => import_ui_utils10.processDataStream,
|
82
|
+
processTextStream: () => import_ui_utils10.processTextStream,
|
83
83
|
simulateReadableStream: () => simulateReadableStream,
|
84
84
|
smoothStream: () => smoothStream,
|
85
85
|
streamObject: () => streamObject,
|
@@ -91,7 +91,7 @@ module.exports = __toCommonJS(streams_exports);
|
|
91
91
|
|
92
92
|
// core/index.ts
|
93
93
|
var import_provider_utils14 = require("@ai-sdk/provider-utils");
|
94
|
-
var
|
94
|
+
var import_ui_utils10 = require("@ai-sdk/ui-utils");
|
95
95
|
|
96
96
|
// core/data-stream/create-data-stream.ts
|
97
97
|
var import_ui_utils = require("@ai-sdk/ui-utils");
|
@@ -1618,45 +1618,62 @@ function convertToCoreMessages(messages, options) {
|
|
1618
1618
|
coreMessages.push({ role: "assistant", content });
|
1619
1619
|
break;
|
1620
1620
|
}
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1621
|
+
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
1622
|
+
var _a16;
|
1623
|
+
return Math.max(max, (_a16 = toolInvocation.step) != null ? _a16 : 0);
|
1624
|
+
}, 0);
|
1625
|
+
for (let i = 0; i <= maxStep; i++) {
|
1626
|
+
const stepInvocations = toolInvocations.filter(
|
1627
|
+
(toolInvocation) => {
|
1628
|
+
var _a16;
|
1629
|
+
return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i;
|
1630
|
+
}
|
1631
|
+
);
|
1632
|
+
if (stepInvocations.length === 0) {
|
1633
|
+
continue;
|
1634
|
+
}
|
1635
|
+
coreMessages.push({
|
1636
|
+
role: "assistant",
|
1637
|
+
content: [
|
1638
|
+
...stepInvocations.map(
|
1639
|
+
({ toolCallId, toolName, args }) => ({
|
1640
|
+
type: "tool-call",
|
1641
|
+
toolCallId,
|
1642
|
+
toolName,
|
1643
|
+
args
|
1644
|
+
})
|
1645
|
+
)
|
1646
|
+
]
|
1647
|
+
});
|
1648
|
+
coreMessages.push({
|
1649
|
+
role: "tool",
|
1650
|
+
content: stepInvocations.map((toolInvocation) => {
|
1651
|
+
if (!("result" in toolInvocation)) {
|
1652
|
+
throw new MessageConversionError({
|
1653
|
+
originalMessage: message,
|
1654
|
+
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
1655
|
+
});
|
1656
|
+
}
|
1657
|
+
const { toolCallId, toolName, result } = toolInvocation;
|
1658
|
+
const tool2 = tools[toolName];
|
1659
|
+
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
1660
|
+
type: "tool-result",
|
1628
1661
|
toolCallId,
|
1629
1662
|
toolName,
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
1645
|
-
const tool2 = tools[toolName];
|
1646
|
-
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
1647
|
-
type: "tool-result",
|
1648
|
-
toolCallId,
|
1649
|
-
toolName,
|
1650
|
-
result: tool2.experimental_toToolResultContent(result),
|
1651
|
-
experimental_content: tool2.experimental_toToolResultContent(result)
|
1652
|
-
} : {
|
1653
|
-
type: "tool-result",
|
1654
|
-
toolCallId,
|
1655
|
-
toolName,
|
1656
|
-
result
|
1657
|
-
};
|
1658
|
-
})
|
1659
|
-
});
|
1663
|
+
result: tool2.experimental_toToolResultContent(result),
|
1664
|
+
experimental_content: tool2.experimental_toToolResultContent(result)
|
1665
|
+
} : {
|
1666
|
+
type: "tool-result",
|
1667
|
+
toolCallId,
|
1668
|
+
toolName,
|
1669
|
+
result
|
1670
|
+
};
|
1671
|
+
})
|
1672
|
+
});
|
1673
|
+
}
|
1674
|
+
if (content) {
|
1675
|
+
coreMessages.push({ role: "assistant", content });
|
1676
|
+
}
|
1660
1677
|
break;
|
1661
1678
|
}
|
1662
1679
|
case "data": {
|
@@ -5771,41 +5788,60 @@ function appendClientMessage({
|
|
5771
5788
|
}
|
5772
5789
|
|
5773
5790
|
// core/prompt/append-response-messages.ts
|
5791
|
+
var import_ui_utils9 = require("@ai-sdk/ui-utils");
|
5774
5792
|
function appendResponseMessages({
|
5775
5793
|
messages,
|
5776
5794
|
responseMessages
|
5777
5795
|
}) {
|
5778
|
-
var _a15;
|
5796
|
+
var _a15, _b;
|
5779
5797
|
const clonedMessages = structuredClone(messages);
|
5780
5798
|
for (const message of responseMessages) {
|
5781
5799
|
const role = message.role;
|
5800
|
+
const lastMessage = clonedMessages[clonedMessages.length - 1];
|
5801
|
+
const isLastMessageAssistant = lastMessage.role === "assistant";
|
5782
5802
|
switch (role) {
|
5783
5803
|
case "assistant": {
|
5784
|
-
|
5785
|
-
|
5786
|
-
id: message.id,
|
5787
|
-
createdAt: /* @__PURE__ */ new Date(),
|
5788
|
-
// generate a createdAt date for the message, will be overridden by the client
|
5789
|
-
// only include text in the content:
|
5790
|
-
content: typeof message.content === "string" ? message.content : message.content.filter((part) => part.type === "text").map((part) => part.text).join(""),
|
5791
|
-
// separate tool calls from the content:
|
5792
|
-
toolInvocations: (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
|
5804
|
+
let getToolInvocations2 = function(step) {
|
5805
|
+
return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
|
5793
5806
|
state: "call",
|
5794
|
-
|
5795
|
-
|
5796
|
-
|
5807
|
+
step,
|
5808
|
+
args: call.args,
|
5809
|
+
toolCallId: call.toolCallId,
|
5810
|
+
toolName: call.toolName
|
5811
|
+
}));
|
5812
|
+
};
|
5813
|
+
var getToolInvocations = getToolInvocations2;
|
5814
|
+
const textContent = typeof message.content === "string" ? message.content : message.content.filter((part) => part.type === "text").map((part) => part.text).join("");
|
5815
|
+
if (isLastMessageAssistant) {
|
5816
|
+
const maxStep = (0, import_ui_utils9.extractMaxToolInvocationStep)(
|
5817
|
+
lastMessage.toolInvocations
|
5818
|
+
);
|
5819
|
+
lastMessage.content = textContent;
|
5820
|
+
lastMessage.toolInvocations = [
|
5821
|
+
...(_a15 = lastMessage.toolInvocations) != null ? _a15 : [],
|
5822
|
+
...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
|
5823
|
+
];
|
5824
|
+
} else {
|
5825
|
+
clonedMessages.push({
|
5826
|
+
role: "assistant",
|
5827
|
+
id: message.id,
|
5828
|
+
createdAt: /* @__PURE__ */ new Date(),
|
5829
|
+
// generate a createdAt date for the message, will be overridden by the client
|
5830
|
+
content: textContent,
|
5831
|
+
toolInvocations: getToolInvocations2(0)
|
5832
|
+
});
|
5833
|
+
}
|
5797
5834
|
break;
|
5798
5835
|
}
|
5799
5836
|
case "tool": {
|
5800
|
-
|
5801
|
-
(
|
5802
|
-
if (previousMessage.role !== "assistant") {
|
5837
|
+
(_b = lastMessage.toolInvocations) != null ? _b : lastMessage.toolInvocations = [];
|
5838
|
+
if (lastMessage.role !== "assistant") {
|
5803
5839
|
throw new Error(
|
5804
|
-
`Tool result must follow an assistant message: ${
|
5840
|
+
`Tool result must follow an assistant message: ${lastMessage.role}`
|
5805
5841
|
);
|
5806
5842
|
}
|
5807
5843
|
for (const part of message.content) {
|
5808
|
-
const toolCall =
|
5844
|
+
const toolCall = lastMessage.toolInvocations.find(
|
5809
5845
|
(call) => call.toolCallId === part.toolCallId
|
5810
5846
|
);
|
5811
5847
|
if (!toolCall) {
|
@@ -6012,7 +6048,7 @@ function simulateReadableStream({
|
|
6012
6048
|
}
|
6013
6049
|
|
6014
6050
|
// streams/assistant-response.ts
|
6015
|
-
var
|
6051
|
+
var import_ui_utils11 = require("@ai-sdk/ui-utils");
|
6016
6052
|
function AssistantResponse({ threadId, messageId }, process2) {
|
6017
6053
|
const stream = new ReadableStream({
|
6018
6054
|
async start(controller) {
|
@@ -6021,20 +6057,20 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6021
6057
|
const sendMessage = (message) => {
|
6022
6058
|
controller.enqueue(
|
6023
6059
|
textEncoder.encode(
|
6024
|
-
(0,
|
6060
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_message", message)
|
6025
6061
|
)
|
6026
6062
|
);
|
6027
6063
|
};
|
6028
6064
|
const sendDataMessage = (message) => {
|
6029
6065
|
controller.enqueue(
|
6030
6066
|
textEncoder.encode(
|
6031
|
-
(0,
|
6067
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("data_message", message)
|
6032
6068
|
)
|
6033
6069
|
);
|
6034
6070
|
};
|
6035
6071
|
const sendError = (errorMessage) => {
|
6036
6072
|
controller.enqueue(
|
6037
|
-
textEncoder.encode((0,
|
6073
|
+
textEncoder.encode((0, import_ui_utils11.formatAssistantStreamPart)("error", errorMessage))
|
6038
6074
|
);
|
6039
6075
|
};
|
6040
6076
|
const forwardStream = async (stream2) => {
|
@@ -6045,7 +6081,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6045
6081
|
case "thread.message.created": {
|
6046
6082
|
controller.enqueue(
|
6047
6083
|
textEncoder.encode(
|
6048
|
-
(0,
|
6084
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_message", {
|
6049
6085
|
id: value.data.id,
|
6050
6086
|
role: "assistant",
|
6051
6087
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -6059,7 +6095,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6059
6095
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
6060
6096
|
controller.enqueue(
|
6061
6097
|
textEncoder.encode(
|
6062
|
-
(0,
|
6098
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("text", content.text.value)
|
6063
6099
|
)
|
6064
6100
|
);
|
6065
6101
|
}
|
@@ -6076,7 +6112,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6076
6112
|
};
|
6077
6113
|
controller.enqueue(
|
6078
6114
|
textEncoder.encode(
|
6079
|
-
(0,
|
6115
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_control_data", {
|
6080
6116
|
threadId,
|
6081
6117
|
messageId
|
6082
6118
|
})
|
@@ -6114,7 +6150,7 @@ __export(langchain_adapter_exports, {
|
|
6114
6150
|
toDataStream: () => toDataStream,
|
6115
6151
|
toDataStreamResponse: () => toDataStreamResponse
|
6116
6152
|
});
|
6117
|
-
var
|
6153
|
+
var import_ui_utils12 = require("@ai-sdk/ui-utils");
|
6118
6154
|
|
6119
6155
|
// streams/stream-callbacks.ts
|
6120
6156
|
function createCallbacksTransformer(callbacks = {}) {
|
@@ -6170,7 +6206,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6170
6206
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6171
6207
|
new TransformStream({
|
6172
6208
|
transform: async (chunk, controller) => {
|
6173
|
-
controller.enqueue((0,
|
6209
|
+
controller.enqueue((0, import_ui_utils12.formatDataStreamPart)("text", chunk));
|
6174
6210
|
}
|
6175
6211
|
})
|
6176
6212
|
);
|
@@ -6222,7 +6258,7 @@ __export(llamaindex_adapter_exports, {
|
|
6222
6258
|
toDataStreamResponse: () => toDataStreamResponse2
|
6223
6259
|
});
|
6224
6260
|
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
6225
|
-
var
|
6261
|
+
var import_ui_utils13 = require("@ai-sdk/ui-utils");
|
6226
6262
|
function toDataStreamInternal2(stream, callbacks) {
|
6227
6263
|
const trimStart = trimStartOfStream();
|
6228
6264
|
return (0, import_provider_utils15.convertAsyncIteratorToReadableStream)(stream[Symbol.asyncIterator]()).pipeThrough(
|
@@ -6234,7 +6270,7 @@ function toDataStreamInternal2(stream, callbacks) {
|
|
6234
6270
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6235
6271
|
new TransformStream({
|
6236
6272
|
transform: async (chunk, controller) => {
|
6237
|
-
controller.enqueue((0,
|
6273
|
+
controller.enqueue((0, import_ui_utils13.formatDataStreamPart)("text", chunk));
|
6238
6274
|
}
|
6239
6275
|
})
|
6240
6276
|
);
|
@@ -6276,7 +6312,7 @@ function trimStartOfStream() {
|
|
6276
6312
|
}
|
6277
6313
|
|
6278
6314
|
// streams/stream-data.ts
|
6279
|
-
var
|
6315
|
+
var import_ui_utils14 = require("@ai-sdk/ui-utils");
|
6280
6316
|
|
6281
6317
|
// util/constants.ts
|
6282
6318
|
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
@@ -6328,7 +6364,7 @@ var StreamData = class {
|
|
6328
6364
|
throw new Error("Stream controller is not initialized.");
|
6329
6365
|
}
|
6330
6366
|
this.controller.enqueue(
|
6331
|
-
this.encoder.encode((0,
|
6367
|
+
this.encoder.encode((0, import_ui_utils14.formatDataStreamPart)("data", [value]))
|
6332
6368
|
);
|
6333
6369
|
}
|
6334
6370
|
appendMessageAnnotation(value) {
|
@@ -6339,7 +6375,7 @@ var StreamData = class {
|
|
6339
6375
|
throw new Error("Stream controller is not initialized.");
|
6340
6376
|
}
|
6341
6377
|
this.controller.enqueue(
|
6342
|
-
this.encoder.encode((0,
|
6378
|
+
this.encoder.encode((0, import_ui_utils14.formatDataStreamPart)("message_annotations", [value]))
|
6343
6379
|
);
|
6344
6380
|
}
|
6345
6381
|
};
|