ai 4.1.11 → 4.1.13
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 +16 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +116 -77
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +96 -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,21 @@
|
|
1
1
|
# ai
|
2
2
|
|
3
|
+
## 4.1.13
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 999085e: feat (ai/core): add write function to DataStreamWriter
|
8
|
+
|
9
|
+
## 4.1.12
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 0d2d9bf: fix (ui): single assistant message with multiple tool steps
|
14
|
+
- Updated dependencies [0d2d9bf]
|
15
|
+
- Updated dependencies [0d2d9bf]
|
16
|
+
- @ai-sdk/react@1.1.7
|
17
|
+
- @ai-sdk/ui-utils@1.1.7
|
18
|
+
|
3
19
|
## 4.1.11
|
4
20
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -10,6 +10,10 @@ import { z } from 'zod';
|
|
10
10
|
import { ServerResponse as ServerResponse$1 } from 'http';
|
11
11
|
|
12
12
|
interface DataStreamWriter {
|
13
|
+
/**
|
14
|
+
* Appends a data part to the stream.
|
15
|
+
*/
|
16
|
+
write(data: DataStreamString): void;
|
13
17
|
/**
|
14
18
|
* Appends a data part to the stream.
|
15
19
|
*/
|
package/dist/index.d.ts
CHANGED
@@ -10,6 +10,10 @@ import { z } from 'zod';
|
|
10
10
|
import { ServerResponse as ServerResponse$1 } from 'http';
|
11
11
|
|
12
12
|
interface DataStreamWriter {
|
13
|
+
/**
|
14
|
+
* Appends a data part to the stream.
|
15
|
+
*/
|
16
|
+
write(data: DataStreamString): void;
|
13
17
|
/**
|
14
18
|
* Appends a data part to the stream.
|
15
19
|
*/
|
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");
|
@@ -115,6 +115,9 @@ function createDataStream({
|
|
115
115
|
}
|
116
116
|
try {
|
117
117
|
const result = execute({
|
118
|
+
write(data) {
|
119
|
+
safeEnqueue(data);
|
120
|
+
},
|
118
121
|
writeData(data) {
|
119
122
|
safeEnqueue((0, import_ui_utils.formatDataStreamPart)("data", [data]));
|
120
123
|
},
|
@@ -1618,45 +1621,62 @@ function convertToCoreMessages(messages, options) {
|
|
1618
1621
|
coreMessages.push({ role: "assistant", content });
|
1619
1622
|
break;
|
1620
1623
|
}
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1624
|
+
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
1625
|
+
var _a16;
|
1626
|
+
return Math.max(max, (_a16 = toolInvocation.step) != null ? _a16 : 0);
|
1627
|
+
}, 0);
|
1628
|
+
for (let i = 0; i <= maxStep; i++) {
|
1629
|
+
const stepInvocations = toolInvocations.filter(
|
1630
|
+
(toolInvocation) => {
|
1631
|
+
var _a16;
|
1632
|
+
return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i;
|
1633
|
+
}
|
1634
|
+
);
|
1635
|
+
if (stepInvocations.length === 0) {
|
1636
|
+
continue;
|
1637
|
+
}
|
1638
|
+
coreMessages.push({
|
1639
|
+
role: "assistant",
|
1640
|
+
content: [
|
1641
|
+
...stepInvocations.map(
|
1642
|
+
({ toolCallId, toolName, args }) => ({
|
1643
|
+
type: "tool-call",
|
1644
|
+
toolCallId,
|
1645
|
+
toolName,
|
1646
|
+
args
|
1647
|
+
})
|
1648
|
+
)
|
1649
|
+
]
|
1650
|
+
});
|
1651
|
+
coreMessages.push({
|
1652
|
+
role: "tool",
|
1653
|
+
content: stepInvocations.map((toolInvocation) => {
|
1654
|
+
if (!("result" in toolInvocation)) {
|
1655
|
+
throw new MessageConversionError({
|
1656
|
+
originalMessage: message,
|
1657
|
+
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
1658
|
+
});
|
1659
|
+
}
|
1660
|
+
const { toolCallId, toolName, result } = toolInvocation;
|
1661
|
+
const tool2 = tools[toolName];
|
1662
|
+
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
1663
|
+
type: "tool-result",
|
1628
1664
|
toolCallId,
|
1629
1665
|
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
|
-
});
|
1666
|
+
result: tool2.experimental_toToolResultContent(result),
|
1667
|
+
experimental_content: tool2.experimental_toToolResultContent(result)
|
1668
|
+
} : {
|
1669
|
+
type: "tool-result",
|
1670
|
+
toolCallId,
|
1671
|
+
toolName,
|
1672
|
+
result
|
1673
|
+
};
|
1674
|
+
})
|
1675
|
+
});
|
1676
|
+
}
|
1677
|
+
if (content) {
|
1678
|
+
coreMessages.push({ role: "assistant", content });
|
1679
|
+
}
|
1660
1680
|
break;
|
1661
1681
|
}
|
1662
1682
|
case "data": {
|
@@ -5771,41 +5791,60 @@ function appendClientMessage({
|
|
5771
5791
|
}
|
5772
5792
|
|
5773
5793
|
// core/prompt/append-response-messages.ts
|
5794
|
+
var import_ui_utils9 = require("@ai-sdk/ui-utils");
|
5774
5795
|
function appendResponseMessages({
|
5775
5796
|
messages,
|
5776
5797
|
responseMessages
|
5777
5798
|
}) {
|
5778
|
-
var _a15;
|
5799
|
+
var _a15, _b;
|
5779
5800
|
const clonedMessages = structuredClone(messages);
|
5780
5801
|
for (const message of responseMessages) {
|
5781
5802
|
const role = message.role;
|
5803
|
+
const lastMessage = clonedMessages[clonedMessages.length - 1];
|
5804
|
+
const isLastMessageAssistant = lastMessage.role === "assistant";
|
5782
5805
|
switch (role) {
|
5783
5806
|
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) => ({
|
5807
|
+
let getToolInvocations2 = function(step) {
|
5808
|
+
return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
|
5793
5809
|
state: "call",
|
5794
|
-
|
5795
|
-
|
5796
|
-
|
5810
|
+
step,
|
5811
|
+
args: call.args,
|
5812
|
+
toolCallId: call.toolCallId,
|
5813
|
+
toolName: call.toolName
|
5814
|
+
}));
|
5815
|
+
};
|
5816
|
+
var getToolInvocations = getToolInvocations2;
|
5817
|
+
const textContent = typeof message.content === "string" ? message.content : message.content.filter((part) => part.type === "text").map((part) => part.text).join("");
|
5818
|
+
if (isLastMessageAssistant) {
|
5819
|
+
const maxStep = (0, import_ui_utils9.extractMaxToolInvocationStep)(
|
5820
|
+
lastMessage.toolInvocations
|
5821
|
+
);
|
5822
|
+
lastMessage.content = textContent;
|
5823
|
+
lastMessage.toolInvocations = [
|
5824
|
+
...(_a15 = lastMessage.toolInvocations) != null ? _a15 : [],
|
5825
|
+
...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
|
5826
|
+
];
|
5827
|
+
} else {
|
5828
|
+
clonedMessages.push({
|
5829
|
+
role: "assistant",
|
5830
|
+
id: message.id,
|
5831
|
+
createdAt: /* @__PURE__ */ new Date(),
|
5832
|
+
// generate a createdAt date for the message, will be overridden by the client
|
5833
|
+
content: textContent,
|
5834
|
+
toolInvocations: getToolInvocations2(0)
|
5835
|
+
});
|
5836
|
+
}
|
5797
5837
|
break;
|
5798
5838
|
}
|
5799
5839
|
case "tool": {
|
5800
|
-
|
5801
|
-
(
|
5802
|
-
if (previousMessage.role !== "assistant") {
|
5840
|
+
(_b = lastMessage.toolInvocations) != null ? _b : lastMessage.toolInvocations = [];
|
5841
|
+
if (lastMessage.role !== "assistant") {
|
5803
5842
|
throw new Error(
|
5804
|
-
`Tool result must follow an assistant message: ${
|
5843
|
+
`Tool result must follow an assistant message: ${lastMessage.role}`
|
5805
5844
|
);
|
5806
5845
|
}
|
5807
5846
|
for (const part of message.content) {
|
5808
|
-
const toolCall =
|
5847
|
+
const toolCall = lastMessage.toolInvocations.find(
|
5809
5848
|
(call) => call.toolCallId === part.toolCallId
|
5810
5849
|
);
|
5811
5850
|
if (!toolCall) {
|
@@ -6012,7 +6051,7 @@ function simulateReadableStream({
|
|
6012
6051
|
}
|
6013
6052
|
|
6014
6053
|
// streams/assistant-response.ts
|
6015
|
-
var
|
6054
|
+
var import_ui_utils11 = require("@ai-sdk/ui-utils");
|
6016
6055
|
function AssistantResponse({ threadId, messageId }, process2) {
|
6017
6056
|
const stream = new ReadableStream({
|
6018
6057
|
async start(controller) {
|
@@ -6021,20 +6060,20 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6021
6060
|
const sendMessage = (message) => {
|
6022
6061
|
controller.enqueue(
|
6023
6062
|
textEncoder.encode(
|
6024
|
-
(0,
|
6063
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_message", message)
|
6025
6064
|
)
|
6026
6065
|
);
|
6027
6066
|
};
|
6028
6067
|
const sendDataMessage = (message) => {
|
6029
6068
|
controller.enqueue(
|
6030
6069
|
textEncoder.encode(
|
6031
|
-
(0,
|
6070
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("data_message", message)
|
6032
6071
|
)
|
6033
6072
|
);
|
6034
6073
|
};
|
6035
6074
|
const sendError = (errorMessage) => {
|
6036
6075
|
controller.enqueue(
|
6037
|
-
textEncoder.encode((0,
|
6076
|
+
textEncoder.encode((0, import_ui_utils11.formatAssistantStreamPart)("error", errorMessage))
|
6038
6077
|
);
|
6039
6078
|
};
|
6040
6079
|
const forwardStream = async (stream2) => {
|
@@ -6045,7 +6084,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6045
6084
|
case "thread.message.created": {
|
6046
6085
|
controller.enqueue(
|
6047
6086
|
textEncoder.encode(
|
6048
|
-
(0,
|
6087
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_message", {
|
6049
6088
|
id: value.data.id,
|
6050
6089
|
role: "assistant",
|
6051
6090
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -6059,7 +6098,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6059
6098
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
6060
6099
|
controller.enqueue(
|
6061
6100
|
textEncoder.encode(
|
6062
|
-
(0,
|
6101
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("text", content.text.value)
|
6063
6102
|
)
|
6064
6103
|
);
|
6065
6104
|
}
|
@@ -6076,7 +6115,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6076
6115
|
};
|
6077
6116
|
controller.enqueue(
|
6078
6117
|
textEncoder.encode(
|
6079
|
-
(0,
|
6118
|
+
(0, import_ui_utils11.formatAssistantStreamPart)("assistant_control_data", {
|
6080
6119
|
threadId,
|
6081
6120
|
messageId
|
6082
6121
|
})
|
@@ -6114,7 +6153,7 @@ __export(langchain_adapter_exports, {
|
|
6114
6153
|
toDataStream: () => toDataStream,
|
6115
6154
|
toDataStreamResponse: () => toDataStreamResponse
|
6116
6155
|
});
|
6117
|
-
var
|
6156
|
+
var import_ui_utils12 = require("@ai-sdk/ui-utils");
|
6118
6157
|
|
6119
6158
|
// streams/stream-callbacks.ts
|
6120
6159
|
function createCallbacksTransformer(callbacks = {}) {
|
@@ -6170,7 +6209,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6170
6209
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6171
6210
|
new TransformStream({
|
6172
6211
|
transform: async (chunk, controller) => {
|
6173
|
-
controller.enqueue((0,
|
6212
|
+
controller.enqueue((0, import_ui_utils12.formatDataStreamPart)("text", chunk));
|
6174
6213
|
}
|
6175
6214
|
})
|
6176
6215
|
);
|
@@ -6222,7 +6261,7 @@ __export(llamaindex_adapter_exports, {
|
|
6222
6261
|
toDataStreamResponse: () => toDataStreamResponse2
|
6223
6262
|
});
|
6224
6263
|
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
6225
|
-
var
|
6264
|
+
var import_ui_utils13 = require("@ai-sdk/ui-utils");
|
6226
6265
|
function toDataStreamInternal2(stream, callbacks) {
|
6227
6266
|
const trimStart = trimStartOfStream();
|
6228
6267
|
return (0, import_provider_utils15.convertAsyncIteratorToReadableStream)(stream[Symbol.asyncIterator]()).pipeThrough(
|
@@ -6234,7 +6273,7 @@ function toDataStreamInternal2(stream, callbacks) {
|
|
6234
6273
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6235
6274
|
new TransformStream({
|
6236
6275
|
transform: async (chunk, controller) => {
|
6237
|
-
controller.enqueue((0,
|
6276
|
+
controller.enqueue((0, import_ui_utils13.formatDataStreamPart)("text", chunk));
|
6238
6277
|
}
|
6239
6278
|
})
|
6240
6279
|
);
|
@@ -6276,7 +6315,7 @@ function trimStartOfStream() {
|
|
6276
6315
|
}
|
6277
6316
|
|
6278
6317
|
// streams/stream-data.ts
|
6279
|
-
var
|
6318
|
+
var import_ui_utils14 = require("@ai-sdk/ui-utils");
|
6280
6319
|
|
6281
6320
|
// util/constants.ts
|
6282
6321
|
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
@@ -6328,7 +6367,7 @@ var StreamData = class {
|
|
6328
6367
|
throw new Error("Stream controller is not initialized.");
|
6329
6368
|
}
|
6330
6369
|
this.controller.enqueue(
|
6331
|
-
this.encoder.encode((0,
|
6370
|
+
this.encoder.encode((0, import_ui_utils14.formatDataStreamPart)("data", [value]))
|
6332
6371
|
);
|
6333
6372
|
}
|
6334
6373
|
appendMessageAnnotation(value) {
|
@@ -6339,7 +6378,7 @@ var StreamData = class {
|
|
6339
6378
|
throw new Error("Stream controller is not initialized.");
|
6340
6379
|
}
|
6341
6380
|
this.controller.enqueue(
|
6342
|
-
this.encoder.encode((0,
|
6381
|
+
this.encoder.encode((0, import_ui_utils14.formatDataStreamPart)("message_annotations", [value]))
|
6343
6382
|
);
|
6344
6383
|
}
|
6345
6384
|
};
|