ai 5.0.0-alpha.8 → 5.0.0-alpha.9
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 +20 -0
- package/dist/index.d.mts +99 -257
- package/dist/index.d.ts +99 -257
- package/dist/index.js +363 -448
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +306 -392
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
@@ -22,7 +22,7 @@ var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
23
23
|
AISDKError: () => import_provider16.AISDKError,
|
24
24
|
APICallError: () => import_provider16.APICallError,
|
25
|
-
|
25
|
+
AbstractChat: () => AbstractChat,
|
26
26
|
DefaultChatTransport: () => DefaultChatTransport,
|
27
27
|
DownloadError: () => DownloadError,
|
28
28
|
EmptyResponseBodyError: () => import_provider16.EmptyResponseBodyError,
|
@@ -54,7 +54,7 @@ __export(src_exports, {
|
|
54
54
|
ToolExecutionError: () => ToolExecutionError,
|
55
55
|
TypeValidationError: () => import_provider16.TypeValidationError,
|
56
56
|
UnsupportedFunctionalityError: () => import_provider16.UnsupportedFunctionalityError,
|
57
|
-
asSchema: () =>
|
57
|
+
asSchema: () => import_provider_utils25.asSchema,
|
58
58
|
assistantModelMessageSchema: () => assistantModelMessageSchema,
|
59
59
|
callCompletionApi: () => callCompletionApi,
|
60
60
|
convertFileListToFileUIParts: () => convertFileListToFileUIParts,
|
@@ -66,13 +66,12 @@ __export(src_exports, {
|
|
66
66
|
coreToolMessageSchema: () => coreToolMessageSchema,
|
67
67
|
coreUserMessageSchema: () => coreUserMessageSchema,
|
68
68
|
cosineSimilarity: () => cosineSimilarity,
|
69
|
-
createIdGenerator: () =>
|
69
|
+
createIdGenerator: () => import_provider_utils25.createIdGenerator,
|
70
70
|
createProviderRegistry: () => createProviderRegistry,
|
71
71
|
createTextStreamResponse: () => createTextStreamResponse,
|
72
72
|
createUIMessageStream: () => createUIMessageStream,
|
73
73
|
createUIMessageStreamResponse: () => createUIMessageStreamResponse,
|
74
74
|
customProvider: () => customProvider,
|
75
|
-
defaultChatStoreOptions: () => defaultChatStoreOptions,
|
76
75
|
defaultSettingsMiddleware: () => defaultSettingsMiddleware,
|
77
76
|
embed: () => embed,
|
78
77
|
embedMany: () => embedMany,
|
@@ -83,14 +82,14 @@ __export(src_exports, {
|
|
83
82
|
experimental_generateSpeech: () => generateSpeech,
|
84
83
|
experimental_transcribe: () => transcribe,
|
85
84
|
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
86
|
-
generateId: () =>
|
85
|
+
generateId: () => import_provider_utils25.generateId,
|
87
86
|
generateObject: () => generateObject,
|
88
87
|
generateText: () => generateText,
|
89
88
|
getTextFromDataUrl: () => getTextFromDataUrl,
|
90
89
|
getToolInvocations: () => getToolInvocations,
|
91
90
|
hasToolCall: () => hasToolCall,
|
92
91
|
isDeepEqualData: () => isDeepEqualData,
|
93
|
-
jsonSchema: () =>
|
92
|
+
jsonSchema: () => import_provider_utils25.jsonSchema,
|
94
93
|
modelMessageSchema: () => modelMessageSchema,
|
95
94
|
parsePartialJson: () => parsePartialJson,
|
96
95
|
pipeTextStreamToResponse: () => pipeTextStreamToResponse,
|
@@ -108,7 +107,7 @@ __export(src_exports, {
|
|
108
107
|
wrapLanguageModel: () => wrapLanguageModel
|
109
108
|
});
|
110
109
|
module.exports = __toCommonJS(src_exports);
|
111
|
-
var
|
110
|
+
var import_provider_utils25 = require("@ai-sdk/provider-utils");
|
112
111
|
|
113
112
|
// src/error/index.ts
|
114
113
|
var import_provider16 = require("@ai-sdk/provider");
|
@@ -769,8 +768,40 @@ async function callCompletionApi({
|
|
769
768
|
}
|
770
769
|
}
|
771
770
|
|
772
|
-
// src/ui/chat
|
773
|
-
var
|
771
|
+
// src/ui/chat.ts
|
772
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
773
|
+
|
774
|
+
// src/util/serial-job-executor.ts
|
775
|
+
var SerialJobExecutor = class {
|
776
|
+
constructor() {
|
777
|
+
this.queue = [];
|
778
|
+
this.isProcessing = false;
|
779
|
+
}
|
780
|
+
async processQueue() {
|
781
|
+
if (this.isProcessing) {
|
782
|
+
return;
|
783
|
+
}
|
784
|
+
this.isProcessing = true;
|
785
|
+
while (this.queue.length > 0) {
|
786
|
+
await this.queue[0]();
|
787
|
+
this.queue.shift();
|
788
|
+
}
|
789
|
+
this.isProcessing = false;
|
790
|
+
}
|
791
|
+
async run(job) {
|
792
|
+
return new Promise((resolve, reject) => {
|
793
|
+
this.queue.push(async () => {
|
794
|
+
try {
|
795
|
+
await job();
|
796
|
+
resolve();
|
797
|
+
} catch (error) {
|
798
|
+
reject(error);
|
799
|
+
}
|
800
|
+
});
|
801
|
+
void this.processQueue();
|
802
|
+
});
|
803
|
+
}
|
804
|
+
};
|
774
805
|
|
775
806
|
// src/ui/process-ui-message-stream.ts
|
776
807
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
@@ -1429,6 +1460,9 @@ function shouldResubmitMessages({
|
|
1429
1460
|
);
|
1430
1461
|
}
|
1431
1462
|
function isAssistantMessageWithCompletedToolCalls(message) {
|
1463
|
+
if (!message) {
|
1464
|
+
return false;
|
1465
|
+
}
|
1432
1466
|
if (message.role !== "assistant") {
|
1433
1467
|
return false;
|
1434
1468
|
}
|
@@ -1439,242 +1473,296 @@ function isAssistantMessageWithCompletedToolCalls(message) {
|
|
1439
1473
|
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1440
1474
|
}
|
1441
1475
|
|
1442
|
-
// src/ui/chat-
|
1443
|
-
var
|
1476
|
+
// src/ui/default-chat-transport.ts
|
1477
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
1478
|
+
var getOriginalFetch2 = () => fetch;
|
1479
|
+
async function fetchUIMessageStream({
|
1480
|
+
api,
|
1481
|
+
body,
|
1482
|
+
credentials,
|
1483
|
+
headers,
|
1484
|
+
abortController,
|
1485
|
+
fetch: fetch2 = getOriginalFetch2(),
|
1486
|
+
requestType = "generate"
|
1487
|
+
}) {
|
1488
|
+
var _a17, _b, _c;
|
1489
|
+
const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.chatId}`, {
|
1490
|
+
method: "GET",
|
1491
|
+
headers: {
|
1492
|
+
"Content-Type": "application/json",
|
1493
|
+
...headers
|
1494
|
+
},
|
1495
|
+
signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
|
1496
|
+
credentials
|
1497
|
+
}) : await fetch2(api, {
|
1498
|
+
method: "POST",
|
1499
|
+
body: JSON.stringify(body),
|
1500
|
+
headers: {
|
1501
|
+
"Content-Type": "application/json",
|
1502
|
+
...headers
|
1503
|
+
},
|
1504
|
+
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1505
|
+
credentials
|
1506
|
+
});
|
1507
|
+
if (!response.ok) {
|
1508
|
+
throw new Error(
|
1509
|
+
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
1510
|
+
);
|
1511
|
+
}
|
1512
|
+
if (!response.body) {
|
1513
|
+
throw new Error("The response body is empty.");
|
1514
|
+
}
|
1515
|
+
return (0, import_provider_utils4.parseJsonEventStream)({
|
1516
|
+
stream: response.body,
|
1517
|
+
schema: uiMessageStreamPartSchema
|
1518
|
+
}).pipeThrough(
|
1519
|
+
new TransformStream({
|
1520
|
+
async transform(part, controller) {
|
1521
|
+
if (!part.success) {
|
1522
|
+
throw part.error;
|
1523
|
+
}
|
1524
|
+
controller.enqueue(part.value);
|
1525
|
+
}
|
1526
|
+
})
|
1527
|
+
);
|
1528
|
+
}
|
1529
|
+
var DefaultChatTransport = class {
|
1444
1530
|
constructor({
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1531
|
+
api = "/api/chat",
|
1532
|
+
credentials,
|
1533
|
+
headers,
|
1534
|
+
body,
|
1535
|
+
fetch: fetch2,
|
1536
|
+
prepareRequestBody
|
1537
|
+
} = {}) {
|
1538
|
+
this.api = api;
|
1539
|
+
this.credentials = credentials;
|
1540
|
+
this.headers = headers;
|
1541
|
+
this.body = body;
|
1542
|
+
this.fetch = fetch2;
|
1543
|
+
this.prepareRequestBody = prepareRequestBody;
|
1544
|
+
}
|
1545
|
+
submitMessages({
|
1546
|
+
chatId,
|
1547
|
+
messages,
|
1548
|
+
abortController,
|
1549
|
+
body,
|
1550
|
+
headers,
|
1551
|
+
requestType
|
1552
|
+
}) {
|
1553
|
+
var _a17, _b;
|
1554
|
+
return fetchUIMessageStream({
|
1555
|
+
api: this.api,
|
1556
|
+
headers: {
|
1557
|
+
...this.headers,
|
1558
|
+
...headers
|
1559
|
+
},
|
1560
|
+
body: (_b = (_a17 = this.prepareRequestBody) == null ? void 0 : _a17.call(this, {
|
1561
|
+
chatId,
|
1562
|
+
messages,
|
1563
|
+
...this.body,
|
1564
|
+
...body
|
1565
|
+
})) != null ? _b : {
|
1566
|
+
chatId,
|
1567
|
+
messages,
|
1568
|
+
...this.body,
|
1569
|
+
...body
|
1570
|
+
},
|
1571
|
+
credentials: this.credentials,
|
1572
|
+
abortController: () => abortController,
|
1573
|
+
fetch: this.fetch,
|
1574
|
+
requestType
|
1575
|
+
});
|
1576
|
+
}
|
1577
|
+
};
|
1578
|
+
|
1579
|
+
// src/ui/chat.ts
|
1580
|
+
var AbstractChat = class {
|
1581
|
+
constructor({
|
1582
|
+
generateId: generateId3 = import_provider_utils5.generateId,
|
1583
|
+
id = generateId3(),
|
1584
|
+
transport = new DefaultChatTransport(),
|
1448
1585
|
maxSteps = 1,
|
1449
1586
|
messageMetadataSchema,
|
1450
1587
|
dataPartSchemas,
|
1451
|
-
|
1588
|
+
state,
|
1589
|
+
onError,
|
1590
|
+
onToolCall,
|
1591
|
+
onFinish
|
1452
1592
|
}) {
|
1453
|
-
this.
|
1454
|
-
this.
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1593
|
+
this.subscribers = /* @__PURE__ */ new Set();
|
1594
|
+
this.activeResponse = void 0;
|
1595
|
+
this.jobExecutor = new SerialJobExecutor();
|
1596
|
+
this.removeAssistantResponse = () => {
|
1597
|
+
const lastMessage = this.state.messages[this.state.messages.length - 1];
|
1598
|
+
if (lastMessage == null) {
|
1599
|
+
throw new Error("Cannot remove assistant response from empty chat");
|
1600
|
+
}
|
1601
|
+
if (lastMessage.role !== "assistant") {
|
1602
|
+
throw new Error("Last message is not an assistant message");
|
1603
|
+
}
|
1604
|
+
this.state.popMessage();
|
1605
|
+
this.emit({ type: "messages-changed" });
|
1606
|
+
};
|
1607
|
+
/**
|
1608
|
+
* Append a user message to the chat list. This triggers the API call to fetch
|
1609
|
+
* the assistant's response.
|
1610
|
+
*/
|
1611
|
+
this.append = async (message, { headers, body } = {}) => {
|
1612
|
+
var _a17;
|
1613
|
+
this.state.pushMessage({ ...message, id: (_a17 = message.id) != null ? _a17 : this.generateId() });
|
1614
|
+
this.emit({ type: "messages-changed" });
|
1615
|
+
await this.triggerRequest({
|
1616
|
+
headers,
|
1617
|
+
body,
|
1618
|
+
requestType: "generate"
|
1619
|
+
});
|
1620
|
+
};
|
1621
|
+
/**
|
1622
|
+
* Reload the last AI chat response for the given chat history. If the last
|
1623
|
+
* message isn't from the assistant, it will request the API to generate a
|
1624
|
+
* new response.
|
1625
|
+
*/
|
1626
|
+
this.reload = async ({
|
1627
|
+
headers,
|
1628
|
+
body
|
1629
|
+
} = {}) => {
|
1630
|
+
if (this.lastMessage === void 0) {
|
1631
|
+
return;
|
1632
|
+
}
|
1633
|
+
if (this.lastMessage.role === "assistant") {
|
1634
|
+
this.state.popMessage();
|
1635
|
+
this.emit({ type: "messages-changed" });
|
1636
|
+
}
|
1637
|
+
await this.triggerRequest({
|
1638
|
+
requestType: "generate",
|
1639
|
+
headers,
|
1640
|
+
body
|
1641
|
+
});
|
1642
|
+
};
|
1643
|
+
/**
|
1644
|
+
* Resume an ongoing chat generation stream. This does not resume an aborted generation.
|
1645
|
+
*/
|
1646
|
+
this.experimental_resume = async ({
|
1647
|
+
headers,
|
1648
|
+
body
|
1649
|
+
} = {}) => {
|
1650
|
+
await this.triggerRequest({
|
1651
|
+
requestType: "resume",
|
1652
|
+
headers,
|
1653
|
+
body
|
1654
|
+
});
|
1655
|
+
};
|
1656
|
+
this.addToolResult = async ({
|
1657
|
+
toolCallId,
|
1658
|
+
result
|
1659
|
+
}) => {
|
1660
|
+
this.jobExecutor.run(async () => {
|
1661
|
+
updateToolCallResult({
|
1662
|
+
messages: this.state.messages,
|
1663
|
+
toolCallId,
|
1664
|
+
toolResult: result
|
1665
|
+
});
|
1666
|
+
this.messages = this.state.messages;
|
1667
|
+
if (this.status === "submitted" || this.status === "streaming") {
|
1668
|
+
return;
|
1669
|
+
}
|
1670
|
+
const lastMessage = this.lastMessage;
|
1671
|
+
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
1672
|
+
this.triggerRequest({
|
1673
|
+
requestType: "generate"
|
1674
|
+
});
|
1675
|
+
}
|
1676
|
+
});
|
1677
|
+
};
|
1678
|
+
/**
|
1679
|
+
* Abort the current request immediately, keep the generated tokens if any.
|
1680
|
+
*/
|
1681
|
+
this.stop = async () => {
|
1682
|
+
var _a17;
|
1683
|
+
if (this.status !== "streaming" && this.status !== "submitted")
|
1684
|
+
return;
|
1685
|
+
if ((_a17 = this.activeResponse) == null ? void 0 : _a17.abortController) {
|
1686
|
+
this.activeResponse.abortController.abort();
|
1687
|
+
this.activeResponse.abortController = void 0;
|
1688
|
+
}
|
1689
|
+
};
|
1690
|
+
this.id = id;
|
1460
1691
|
this.maxSteps = maxSteps;
|
1461
1692
|
this.transport = transport;
|
1462
|
-
this.
|
1463
|
-
this.generateId = generateId3 != null ? generateId3 : import_provider_utils4.generateId;
|
1693
|
+
this.generateId = generateId3;
|
1464
1694
|
this.messageMetadataSchema = messageMetadataSchema;
|
1465
1695
|
this.dataPartSchemas = dataPartSchemas;
|
1696
|
+
this.state = state;
|
1697
|
+
this.onError = onError;
|
1698
|
+
this.onToolCall = onToolCall;
|
1699
|
+
this.onFinish = onFinish;
|
1466
1700
|
}
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
return this.chats.size;
|
1478
|
-
}
|
1479
|
-
getStatus(id) {
|
1480
|
-
return this.getChat(id).status;
|
1701
|
+
/**
|
1702
|
+
* Hook status:
|
1703
|
+
*
|
1704
|
+
* - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
|
1705
|
+
* - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
|
1706
|
+
* - `ready`: The full response has been received and processed; a new user message can be submitted.
|
1707
|
+
* - `error`: An error occurred during the API request, preventing successful completion.
|
1708
|
+
*/
|
1709
|
+
get status() {
|
1710
|
+
return this.state.status;
|
1481
1711
|
}
|
1482
1712
|
setStatus({
|
1483
|
-
id,
|
1484
1713
|
status,
|
1485
1714
|
error
|
1486
1715
|
}) {
|
1487
|
-
|
1488
|
-
if (state.status === status)
|
1716
|
+
if (this.status === status)
|
1489
1717
|
return;
|
1490
|
-
state.
|
1491
|
-
state.
|
1492
|
-
this.emit({ type: "
|
1718
|
+
this.state.status = status;
|
1719
|
+
this.state.error = error;
|
1720
|
+
this.emit({ type: "status-changed" });
|
1493
1721
|
}
|
1494
|
-
|
1495
|
-
return this.
|
1722
|
+
get error() {
|
1723
|
+
return this.state.error;
|
1496
1724
|
}
|
1497
|
-
|
1498
|
-
return this.
|
1725
|
+
get messages() {
|
1726
|
+
return this.state.messages;
|
1499
1727
|
}
|
1500
|
-
|
1501
|
-
|
1502
|
-
return chat.messages[chat.messages.length - 1];
|
1728
|
+
get lastMessage() {
|
1729
|
+
return this.state.messages[this.state.messages.length - 1];
|
1503
1730
|
}
|
1504
1731
|
subscribe(subscriber) {
|
1505
1732
|
this.subscribers.add(subscriber);
|
1506
1733
|
return () => this.subscribers.delete(subscriber);
|
1507
1734
|
}
|
1508
|
-
|
1509
|
-
|
1510
|
-
messages
|
1511
|
-
}) {
|
1512
|
-
this.getChat(id).setMessages(messages);
|
1513
|
-
this.emit({ type: "chat-messages-changed", chatId: id });
|
1514
|
-
}
|
1515
|
-
removeAssistantResponse(id) {
|
1516
|
-
const chat = this.getChat(id);
|
1517
|
-
const lastMessage = chat.messages[chat.messages.length - 1];
|
1518
|
-
if (lastMessage == null) {
|
1519
|
-
throw new Error("Cannot remove assistant response from empty chat");
|
1520
|
-
}
|
1521
|
-
if (lastMessage.role !== "assistant") {
|
1522
|
-
throw new Error("Last message is not an assistant message");
|
1523
|
-
}
|
1524
|
-
chat.popMessage();
|
1525
|
-
this.emit({ type: "chat-messages-changed", chatId: id });
|
1526
|
-
}
|
1527
|
-
async submitMessage({
|
1528
|
-
chatId,
|
1529
|
-
message,
|
1530
|
-
headers,
|
1531
|
-
body,
|
1532
|
-
onError,
|
1533
|
-
onToolCall,
|
1534
|
-
onFinish
|
1535
|
-
}) {
|
1536
|
-
var _a17;
|
1537
|
-
const chat = this.getChat(chatId);
|
1538
|
-
chat.pushMessage({ ...message, id: (_a17 = message.id) != null ? _a17 : this.generateId() });
|
1539
|
-
this.emit({
|
1540
|
-
type: "chat-messages-changed",
|
1541
|
-
chatId
|
1542
|
-
});
|
1543
|
-
await this.triggerRequest({
|
1544
|
-
chatId,
|
1545
|
-
headers,
|
1546
|
-
body,
|
1547
|
-
requestType: "generate",
|
1548
|
-
onError,
|
1549
|
-
onToolCall,
|
1550
|
-
onFinish
|
1551
|
-
});
|
1552
|
-
}
|
1553
|
-
async resubmitLastUserMessage({
|
1554
|
-
chatId,
|
1555
|
-
headers,
|
1556
|
-
body,
|
1557
|
-
onError,
|
1558
|
-
onToolCall,
|
1559
|
-
onFinish
|
1560
|
-
}) {
|
1561
|
-
const chat = this.getChat(chatId);
|
1562
|
-
if (chat.messages[chat.messages.length - 1].role === "assistant") {
|
1563
|
-
chat.popMessage();
|
1564
|
-
this.emit({
|
1565
|
-
type: "chat-messages-changed",
|
1566
|
-
chatId
|
1567
|
-
});
|
1568
|
-
}
|
1569
|
-
if (chat.messages.length === 0) {
|
1570
|
-
return;
|
1571
|
-
}
|
1572
|
-
return this.triggerRequest({
|
1573
|
-
chatId,
|
1574
|
-
requestType: "generate",
|
1575
|
-
headers,
|
1576
|
-
body,
|
1577
|
-
onError,
|
1578
|
-
onToolCall,
|
1579
|
-
onFinish
|
1580
|
-
});
|
1581
|
-
}
|
1582
|
-
async resumeStream({
|
1583
|
-
chatId,
|
1584
|
-
headers,
|
1585
|
-
body,
|
1586
|
-
onError,
|
1587
|
-
onToolCall,
|
1588
|
-
onFinish
|
1589
|
-
}) {
|
1590
|
-
return this.triggerRequest({
|
1591
|
-
chatId,
|
1592
|
-
requestType: "resume",
|
1593
|
-
headers,
|
1594
|
-
body,
|
1595
|
-
onError,
|
1596
|
-
onToolCall,
|
1597
|
-
onFinish
|
1598
|
-
});
|
1599
|
-
}
|
1600
|
-
async addToolResult({
|
1601
|
-
chatId,
|
1602
|
-
toolCallId,
|
1603
|
-
result
|
1604
|
-
}) {
|
1605
|
-
const chat = this.getChat(chatId);
|
1606
|
-
chat.jobExecutor.run(async () => {
|
1607
|
-
updateToolCallResult({
|
1608
|
-
messages: chat.messages,
|
1609
|
-
toolCallId,
|
1610
|
-
toolResult: result
|
1611
|
-
});
|
1612
|
-
this.setMessages({
|
1613
|
-
id: chatId,
|
1614
|
-
messages: chat.messages
|
1615
|
-
});
|
1616
|
-
if (chat.status === "submitted" || chat.status === "streaming") {
|
1617
|
-
return;
|
1618
|
-
}
|
1619
|
-
const lastMessage = chat.messages[chat.messages.length - 1];
|
1620
|
-
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
1621
|
-
this.triggerRequest({
|
1622
|
-
requestType: "generate",
|
1623
|
-
chatId
|
1624
|
-
});
|
1625
|
-
}
|
1626
|
-
});
|
1627
|
-
}
|
1628
|
-
async stopStream({ chatId }) {
|
1629
|
-
var _a17;
|
1630
|
-
const chat = this.getChat(chatId);
|
1631
|
-
if (chat.status !== "streaming" && chat.status !== "submitted")
|
1632
|
-
return;
|
1633
|
-
if ((_a17 = chat.activeResponse) == null ? void 0 : _a17.abortController) {
|
1634
|
-
chat.activeResponse.abortController.abort();
|
1635
|
-
chat.activeResponse.abortController = void 0;
|
1636
|
-
}
|
1735
|
+
set messages(messages) {
|
1736
|
+
this.state.messages = messages;
|
1737
|
+
this.emit({ type: "messages-changed" });
|
1637
1738
|
}
|
1638
1739
|
emit(event) {
|
1639
1740
|
for (const subscriber of this.subscribers) {
|
1640
|
-
subscriber.
|
1741
|
+
subscriber.onChange(event);
|
1641
1742
|
}
|
1642
1743
|
}
|
1643
|
-
getChat(id) {
|
1644
|
-
if (!this.hasChat(id)) {
|
1645
|
-
this.addChat(id, []);
|
1646
|
-
}
|
1647
|
-
return this.chats.get(id);
|
1648
|
-
}
|
1649
1744
|
async triggerRequest({
|
1650
|
-
chatId,
|
1651
1745
|
requestType,
|
1652
1746
|
headers,
|
1653
|
-
body
|
1654
|
-
onError,
|
1655
|
-
onToolCall,
|
1656
|
-
onFinish
|
1747
|
+
body
|
1657
1748
|
}) {
|
1658
|
-
|
1659
|
-
this.setStatus({
|
1660
|
-
const messageCount =
|
1661
|
-
const lastMessage =
|
1662
|
-
const maxStep = lastMessage.parts.filter(
|
1663
|
-
(part) => part.type === "step-start"
|
1664
|
-
).length;
|
1749
|
+
var _a17, _b;
|
1750
|
+
this.setStatus({ status: "submitted", error: void 0 });
|
1751
|
+
const messageCount = this.state.messages.length;
|
1752
|
+
const lastMessage = this.lastMessage;
|
1753
|
+
const maxStep = (_a17 = lastMessage == null ? void 0 : lastMessage.parts.filter((part) => part.type === "step-start").length) != null ? _a17 : 0;
|
1665
1754
|
try {
|
1666
|
-
const lastMessage2 = chat.messages[chat.messages.length - 1];
|
1667
1755
|
const activeResponse = {
|
1668
1756
|
state: createStreamingUIMessageState({
|
1669
|
-
lastMessage:
|
1757
|
+
lastMessage: this.state.snapshot(lastMessage),
|
1670
1758
|
newMessageId: this.generateId()
|
1671
1759
|
}),
|
1672
1760
|
abortController: new AbortController()
|
1673
1761
|
};
|
1674
|
-
|
1762
|
+
this.activeResponse = activeResponse;
|
1675
1763
|
const stream = await this.transport.submitMessages({
|
1676
|
-
chatId,
|
1677
|
-
messages:
|
1764
|
+
chatId: this.id,
|
1765
|
+
messages: this.state.messages,
|
1678
1766
|
body,
|
1679
1767
|
headers,
|
1680
1768
|
abortController: activeResponse.abortController,
|
@@ -1682,23 +1770,23 @@ var ChatStore = class {
|
|
1682
1770
|
});
|
1683
1771
|
const runUpdateMessageJob = (job) => (
|
1684
1772
|
// serialize the job execution to avoid race conditions:
|
1685
|
-
|
1773
|
+
this.jobExecutor.run(
|
1686
1774
|
() => job({
|
1687
1775
|
state: activeResponse.state,
|
1688
1776
|
write: () => {
|
1689
|
-
|
1690
|
-
|
1777
|
+
var _a18;
|
1778
|
+
this.setStatus({ status: "streaming" });
|
1779
|
+
const replaceLastMessage = activeResponse.state.message.id === ((_a18 = this.lastMessage) == null ? void 0 : _a18.id);
|
1691
1780
|
if (replaceLastMessage) {
|
1692
|
-
|
1693
|
-
|
1781
|
+
this.state.replaceMessage(
|
1782
|
+
this.state.messages.length - 1,
|
1694
1783
|
activeResponse.state.message
|
1695
1784
|
);
|
1696
1785
|
} else {
|
1697
|
-
|
1786
|
+
this.state.pushMessage(activeResponse.state.message);
|
1698
1787
|
}
|
1699
1788
|
this.emit({
|
1700
|
-
type: "
|
1701
|
-
chatId
|
1789
|
+
type: "messages-changed"
|
1702
1790
|
});
|
1703
1791
|
}
|
1704
1792
|
})
|
@@ -1707,7 +1795,7 @@ var ChatStore = class {
|
|
1707
1795
|
await consumeStream({
|
1708
1796
|
stream: processUIMessageStream({
|
1709
1797
|
stream,
|
1710
|
-
onToolCall,
|
1798
|
+
onToolCall: this.onToolCall,
|
1711
1799
|
messageMetadataSchema: this.messageMetadataSchema,
|
1712
1800
|
dataPartSchemas: this.dataPartSchemas,
|
1713
1801
|
runUpdateMessageJob
|
@@ -1716,32 +1804,29 @@ var ChatStore = class {
|
|
1716
1804
|
throw error;
|
1717
1805
|
}
|
1718
1806
|
});
|
1719
|
-
onFinish == null ? void 0 :
|
1720
|
-
this.setStatus({
|
1807
|
+
(_b = this.onFinish) == null ? void 0 : _b.call(this, { message: activeResponse.state.message });
|
1808
|
+
this.setStatus({ status: "ready" });
|
1721
1809
|
} catch (err) {
|
1810
|
+
console.error(err);
|
1722
1811
|
if (err.name === "AbortError") {
|
1723
|
-
this.setStatus({
|
1812
|
+
this.setStatus({ status: "ready" });
|
1724
1813
|
return null;
|
1725
1814
|
}
|
1726
|
-
if (onError && err instanceof Error) {
|
1727
|
-
onError(err);
|
1815
|
+
if (this.onError && err instanceof Error) {
|
1816
|
+
this.onError(err);
|
1728
1817
|
}
|
1729
|
-
this.setStatus({
|
1818
|
+
this.setStatus({ status: "error", error: err });
|
1730
1819
|
} finally {
|
1731
|
-
|
1820
|
+
this.activeResponse = void 0;
|
1732
1821
|
}
|
1733
1822
|
if (shouldResubmitMessages({
|
1734
1823
|
originalMaxToolInvocationStep: maxStep,
|
1735
1824
|
originalMessageCount: messageCount,
|
1736
1825
|
maxSteps: this.maxSteps,
|
1737
|
-
messages:
|
1826
|
+
messages: this.state.messages
|
1738
1827
|
})) {
|
1739
1828
|
await this.triggerRequest({
|
1740
|
-
chatId,
|
1741
1829
|
requestType,
|
1742
|
-
onError,
|
1743
|
-
onToolCall,
|
1744
|
-
onFinish,
|
1745
1830
|
headers,
|
1746
1831
|
body
|
1747
1832
|
});
|
@@ -1943,143 +2028,6 @@ function convertToModelMessages(messages, options) {
|
|
1943
2028
|
}
|
1944
2029
|
var convertToCoreMessages = convertToModelMessages;
|
1945
2030
|
|
1946
|
-
// src/ui/default-chat-store-options.ts
|
1947
|
-
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
1948
|
-
|
1949
|
-
// src/ui/default-chat-transport.ts
|
1950
|
-
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1951
|
-
var getOriginalFetch2 = () => fetch;
|
1952
|
-
async function fetchUIMessageStream({
|
1953
|
-
api,
|
1954
|
-
body,
|
1955
|
-
credentials,
|
1956
|
-
headers,
|
1957
|
-
abortController,
|
1958
|
-
fetch: fetch2 = getOriginalFetch2(),
|
1959
|
-
requestType = "generate"
|
1960
|
-
}) {
|
1961
|
-
var _a17, _b, _c;
|
1962
|
-
const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.chatId}`, {
|
1963
|
-
method: "GET",
|
1964
|
-
headers: {
|
1965
|
-
"Content-Type": "application/json",
|
1966
|
-
...headers
|
1967
|
-
},
|
1968
|
-
signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
|
1969
|
-
credentials
|
1970
|
-
}) : await fetch2(api, {
|
1971
|
-
method: "POST",
|
1972
|
-
body: JSON.stringify(body),
|
1973
|
-
headers: {
|
1974
|
-
"Content-Type": "application/json",
|
1975
|
-
...headers
|
1976
|
-
},
|
1977
|
-
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1978
|
-
credentials
|
1979
|
-
});
|
1980
|
-
if (!response.ok) {
|
1981
|
-
throw new Error(
|
1982
|
-
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
1983
|
-
);
|
1984
|
-
}
|
1985
|
-
if (!response.body) {
|
1986
|
-
throw new Error("The response body is empty.");
|
1987
|
-
}
|
1988
|
-
return (0, import_provider_utils5.parseJsonEventStream)({
|
1989
|
-
stream: response.body,
|
1990
|
-
schema: uiMessageStreamPartSchema
|
1991
|
-
}).pipeThrough(
|
1992
|
-
new TransformStream({
|
1993
|
-
async transform(part, controller) {
|
1994
|
-
if (!part.success) {
|
1995
|
-
throw part.error;
|
1996
|
-
}
|
1997
|
-
controller.enqueue(part.value);
|
1998
|
-
}
|
1999
|
-
})
|
2000
|
-
);
|
2001
|
-
}
|
2002
|
-
var DefaultChatTransport = class {
|
2003
|
-
constructor({
|
2004
|
-
api,
|
2005
|
-
credentials,
|
2006
|
-
headers,
|
2007
|
-
body,
|
2008
|
-
fetch: fetch2,
|
2009
|
-
prepareRequestBody
|
2010
|
-
}) {
|
2011
|
-
this.api = api;
|
2012
|
-
this.credentials = credentials;
|
2013
|
-
this.headers = headers;
|
2014
|
-
this.body = body;
|
2015
|
-
this.fetch = fetch2;
|
2016
|
-
this.prepareRequestBody = prepareRequestBody;
|
2017
|
-
}
|
2018
|
-
submitMessages({
|
2019
|
-
chatId,
|
2020
|
-
messages,
|
2021
|
-
abortController,
|
2022
|
-
body,
|
2023
|
-
headers,
|
2024
|
-
requestType
|
2025
|
-
}) {
|
2026
|
-
var _a17, _b;
|
2027
|
-
return fetchUIMessageStream({
|
2028
|
-
api: this.api,
|
2029
|
-
headers: {
|
2030
|
-
...this.headers,
|
2031
|
-
...headers
|
2032
|
-
},
|
2033
|
-
body: (_b = (_a17 = this.prepareRequestBody) == null ? void 0 : _a17.call(this, {
|
2034
|
-
chatId,
|
2035
|
-
messages,
|
2036
|
-
...this.body,
|
2037
|
-
...body
|
2038
|
-
})) != null ? _b : {
|
2039
|
-
chatId,
|
2040
|
-
messages,
|
2041
|
-
...this.body,
|
2042
|
-
...body
|
2043
|
-
},
|
2044
|
-
credentials: this.credentials,
|
2045
|
-
abortController: () => abortController,
|
2046
|
-
fetch: this.fetch,
|
2047
|
-
requestType
|
2048
|
-
});
|
2049
|
-
}
|
2050
|
-
};
|
2051
|
-
|
2052
|
-
// src/ui/default-chat-store-options.ts
|
2053
|
-
function defaultChatStoreOptions({
|
2054
|
-
api = "/api/chat",
|
2055
|
-
fetch: fetch2,
|
2056
|
-
credentials,
|
2057
|
-
headers,
|
2058
|
-
body,
|
2059
|
-
prepareRequestBody,
|
2060
|
-
generateId: generateId3 = import_provider_utils6.generateId,
|
2061
|
-
messageMetadataSchema,
|
2062
|
-
maxSteps = 1,
|
2063
|
-
dataPartSchemas,
|
2064
|
-
chats
|
2065
|
-
}) {
|
2066
|
-
return () => ({
|
2067
|
-
transport: new DefaultChatTransport({
|
2068
|
-
api,
|
2069
|
-
fetch: fetch2,
|
2070
|
-
credentials,
|
2071
|
-
headers,
|
2072
|
-
body,
|
2073
|
-
prepareRequestBody
|
2074
|
-
}),
|
2075
|
-
generateId: generateId3,
|
2076
|
-
messageMetadataSchema,
|
2077
|
-
dataPartSchemas,
|
2078
|
-
maxSteps,
|
2079
|
-
chats
|
2080
|
-
});
|
2081
|
-
}
|
2082
|
-
|
2083
2031
|
// src/ui/transform-text-to-ui-message-stream.ts
|
2084
2032
|
function transformTextToUiMessageStream({
|
2085
2033
|
stream
|
@@ -2449,40 +2397,8 @@ function isDeepEqualData(obj1, obj2) {
|
|
2449
2397
|
return true;
|
2450
2398
|
}
|
2451
2399
|
|
2452
|
-
// src/util/serial-job-executor.ts
|
2453
|
-
var SerialJobExecutor = class {
|
2454
|
-
constructor() {
|
2455
|
-
this.queue = [];
|
2456
|
-
this.isProcessing = false;
|
2457
|
-
}
|
2458
|
-
async processQueue() {
|
2459
|
-
if (this.isProcessing) {
|
2460
|
-
return;
|
2461
|
-
}
|
2462
|
-
this.isProcessing = true;
|
2463
|
-
while (this.queue.length > 0) {
|
2464
|
-
await this.queue[0]();
|
2465
|
-
this.queue.shift();
|
2466
|
-
}
|
2467
|
-
this.isProcessing = false;
|
2468
|
-
}
|
2469
|
-
async run(job) {
|
2470
|
-
return new Promise((resolve, reject) => {
|
2471
|
-
this.queue.push(async () => {
|
2472
|
-
try {
|
2473
|
-
await job();
|
2474
|
-
resolve();
|
2475
|
-
} catch (error) {
|
2476
|
-
reject(error);
|
2477
|
-
}
|
2478
|
-
});
|
2479
|
-
void this.processQueue();
|
2480
|
-
});
|
2481
|
-
}
|
2482
|
-
};
|
2483
|
-
|
2484
2400
|
// src/util/simulate-readable-stream.ts
|
2485
|
-
var
|
2401
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
2486
2402
|
function simulateReadableStream({
|
2487
2403
|
chunks,
|
2488
2404
|
initialDelayInMs = 0,
|
@@ -2490,7 +2406,7 @@ function simulateReadableStream({
|
|
2490
2406
|
_internal
|
2491
2407
|
}) {
|
2492
2408
|
var _a17;
|
2493
|
-
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 :
|
2409
|
+
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 : import_provider_utils6.delay;
|
2494
2410
|
let index = 0;
|
2495
2411
|
return new ReadableStream({
|
2496
2412
|
async pull(controller) {
|
@@ -2506,7 +2422,7 @@ function simulateReadableStream({
|
|
2506
2422
|
|
2507
2423
|
// src/util/retry-with-exponential-backoff.ts
|
2508
2424
|
var import_provider17 = require("@ai-sdk/provider");
|
2509
|
-
var
|
2425
|
+
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
2510
2426
|
var retryWithExponentialBackoff = ({
|
2511
2427
|
maxRetries = 2,
|
2512
2428
|
initialDelayInMs = 2e3,
|
@@ -2524,13 +2440,13 @@ async function _retryWithExponentialBackoff(f, {
|
|
2524
2440
|
try {
|
2525
2441
|
return await f();
|
2526
2442
|
} catch (error) {
|
2527
|
-
if ((0,
|
2443
|
+
if ((0, import_provider_utils7.isAbortError)(error)) {
|
2528
2444
|
throw error;
|
2529
2445
|
}
|
2530
2446
|
if (maxRetries === 0) {
|
2531
2447
|
throw error;
|
2532
2448
|
}
|
2533
|
-
const errorMessage = (0,
|
2449
|
+
const errorMessage = (0, import_provider_utils7.getErrorMessage)(error);
|
2534
2450
|
const newErrors = [...errors, error];
|
2535
2451
|
const tryNumber = newErrors.length;
|
2536
2452
|
if (tryNumber > maxRetries) {
|
@@ -2541,7 +2457,7 @@ async function _retryWithExponentialBackoff(f, {
|
|
2541
2457
|
});
|
2542
2458
|
}
|
2543
2459
|
if (error instanceof Error && import_provider17.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
2544
|
-
await (0,
|
2460
|
+
await (0, import_provider_utils7.delay)(delayInMs);
|
2545
2461
|
return _retryWithExponentialBackoff(
|
2546
2462
|
f,
|
2547
2463
|
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
@@ -3100,7 +3016,7 @@ var DefaultEmbedManyResult = class {
|
|
3100
3016
|
};
|
3101
3017
|
|
3102
3018
|
// src/util/detect-media-type.ts
|
3103
|
-
var
|
3019
|
+
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
3104
3020
|
var imageMediaTypeSignatures = [
|
3105
3021
|
{
|
3106
3022
|
mediaType: "image/gif",
|
@@ -3207,7 +3123,7 @@ var audioMediaTypeSignatures = [
|
|
3207
3123
|
}
|
3208
3124
|
];
|
3209
3125
|
var stripID3 = (data) => {
|
3210
|
-
const bytes = typeof data === "string" ? (0,
|
3126
|
+
const bytes = typeof data === "string" ? (0, import_provider_utils8.convertBase64ToUint8Array)(data) : data;
|
3211
3127
|
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
|
3212
3128
|
return bytes.slice(id3Size + 10);
|
3213
3129
|
};
|
@@ -3233,7 +3149,7 @@ function detectMediaType({
|
|
3233
3149
|
}
|
3234
3150
|
|
3235
3151
|
// core/generate-text/generated-file.ts
|
3236
|
-
var
|
3152
|
+
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
3237
3153
|
var DefaultGeneratedFile = class {
|
3238
3154
|
constructor({
|
3239
3155
|
data,
|
@@ -3247,14 +3163,14 @@ var DefaultGeneratedFile = class {
|
|
3247
3163
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
3248
3164
|
get base64() {
|
3249
3165
|
if (this.base64Data == null) {
|
3250
|
-
this.base64Data = (0,
|
3166
|
+
this.base64Data = (0, import_provider_utils9.convertUint8ArrayToBase64)(this.uint8ArrayData);
|
3251
3167
|
}
|
3252
3168
|
return this.base64Data;
|
3253
3169
|
}
|
3254
3170
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
3255
3171
|
get uint8Array() {
|
3256
3172
|
if (this.uint8ArrayData == null) {
|
3257
|
-
this.uint8ArrayData = (0,
|
3173
|
+
this.uint8ArrayData = (0, import_provider_utils9.convertBase64ToUint8Array)(this.base64Data);
|
3258
3174
|
}
|
3259
3175
|
return this.uint8ArrayData;
|
3260
3176
|
}
|
@@ -3370,7 +3286,7 @@ async function invokeModelMaxImagesPerCall(model) {
|
|
3370
3286
|
|
3371
3287
|
// core/generate-object/generate-object.ts
|
3372
3288
|
var import_provider22 = require("@ai-sdk/provider");
|
3373
|
-
var
|
3289
|
+
var import_provider_utils14 = require("@ai-sdk/provider-utils");
|
3374
3290
|
|
3375
3291
|
// core/generate-text/extract-content-text.ts
|
3376
3292
|
function extractContentText(content) {
|
@@ -3384,7 +3300,7 @@ function extractContentText(content) {
|
|
3384
3300
|
}
|
3385
3301
|
|
3386
3302
|
// core/prompt/convert-to-language-model-prompt.ts
|
3387
|
-
var
|
3303
|
+
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
3388
3304
|
|
3389
3305
|
// src/util/download.ts
|
3390
3306
|
async function download({ url }) {
|
@@ -3413,7 +3329,7 @@ async function download({ url }) {
|
|
3413
3329
|
|
3414
3330
|
// core/prompt/data-content.ts
|
3415
3331
|
var import_provider18 = require("@ai-sdk/provider");
|
3416
|
-
var
|
3332
|
+
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
3417
3333
|
var import_zod2 = require("zod");
|
3418
3334
|
|
3419
3335
|
// core/prompt/split-data-url.ts
|
@@ -3478,9 +3394,9 @@ function convertDataContentToBase64String(content) {
|
|
3478
3394
|
return content;
|
3479
3395
|
}
|
3480
3396
|
if (content instanceof ArrayBuffer) {
|
3481
|
-
return (0,
|
3397
|
+
return (0, import_provider_utils10.convertUint8ArrayToBase64)(new Uint8Array(content));
|
3482
3398
|
}
|
3483
|
-
return (0,
|
3399
|
+
return (0, import_provider_utils10.convertUint8ArrayToBase64)(content);
|
3484
3400
|
}
|
3485
3401
|
function convertDataContentToUint8Array(content) {
|
3486
3402
|
if (content instanceof Uint8Array) {
|
@@ -3488,7 +3404,7 @@ function convertDataContentToUint8Array(content) {
|
|
3488
3404
|
}
|
3489
3405
|
if (typeof content === "string") {
|
3490
3406
|
try {
|
3491
|
-
return (0,
|
3407
|
+
return (0, import_provider_utils10.convertBase64ToUint8Array)(content);
|
3492
3408
|
} catch (error) {
|
3493
3409
|
throw new InvalidDataContentError({
|
3494
3410
|
message: "Invalid data content. Content string is not a base64-encoded media.",
|
@@ -3639,7 +3555,7 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
3639
3555
|
}
|
3640
3556
|
return { mediaType, data };
|
3641
3557
|
}).filter(
|
3642
|
-
(part) => part.data instanceof URL && part.mediaType != null && !(0,
|
3558
|
+
(part) => part.data instanceof URL && part.mediaType != null && !(0, import_provider_utils11.isUrlSupported)({
|
3643
3559
|
url: part.data.toString(),
|
3644
3560
|
mediaType: part.mediaType,
|
3645
3561
|
supportedUrls
|
@@ -3823,7 +3739,7 @@ function resolveLanguageModel(model) {
|
|
3823
3739
|
|
3824
3740
|
// core/prompt/standardize-prompt.ts
|
3825
3741
|
var import_provider19 = require("@ai-sdk/provider");
|
3826
|
-
var
|
3742
|
+
var import_provider_utils12 = require("@ai-sdk/provider-utils");
|
3827
3743
|
var import_zod8 = require("zod");
|
3828
3744
|
|
3829
3745
|
// core/prompt/message.ts
|
@@ -3995,7 +3911,7 @@ async function standardizePrompt(prompt) {
|
|
3995
3911
|
message: "messages must not be empty"
|
3996
3912
|
});
|
3997
3913
|
}
|
3998
|
-
const validationResult = await (0,
|
3914
|
+
const validationResult = await (0, import_provider_utils12.safeValidateTypes)({
|
3999
3915
|
value: messages,
|
4000
3916
|
schema: import_zod8.z.array(modelMessageSchema)
|
4001
3917
|
});
|
@@ -4043,7 +3959,7 @@ function stringifyForTelemetry(prompt) {
|
|
4043
3959
|
|
4044
3960
|
// core/generate-object/output-strategy.ts
|
4045
3961
|
var import_provider21 = require("@ai-sdk/provider");
|
4046
|
-
var
|
3962
|
+
var import_provider_utils13 = require("@ai-sdk/provider-utils");
|
4047
3963
|
|
4048
3964
|
// src/util/async-iterable-stream.ts
|
4049
3965
|
function createAsyncIterableStream(source) {
|
@@ -4099,7 +4015,7 @@ var objectOutputStrategy = (schema) => ({
|
|
4099
4015
|
};
|
4100
4016
|
},
|
4101
4017
|
async validateFinalResult(value) {
|
4102
|
-
return (0,
|
4018
|
+
return (0, import_provider_utils13.safeValidateTypes)({ value, schema });
|
4103
4019
|
},
|
4104
4020
|
createElementStream() {
|
4105
4021
|
throw new import_provider21.UnsupportedFunctionalityError({
|
@@ -4143,7 +4059,7 @@ var arrayOutputStrategy = (schema) => {
|
|
4143
4059
|
const resultArray = [];
|
4144
4060
|
for (let i = 0; i < inputArray.length; i++) {
|
4145
4061
|
const element = inputArray[i];
|
4146
|
-
const result = await (0,
|
4062
|
+
const result = await (0, import_provider_utils13.safeValidateTypes)({ value: element, schema });
|
4147
4063
|
if (i === inputArray.length - 1 && !isFinalDelta) {
|
4148
4064
|
continue;
|
4149
4065
|
}
|
@@ -4184,7 +4100,7 @@ var arrayOutputStrategy = (schema) => {
|
|
4184
4100
|
}
|
4185
4101
|
const inputArray = value.elements;
|
4186
4102
|
for (const element of inputArray) {
|
4187
|
-
const result = await (0,
|
4103
|
+
const result = await (0, import_provider_utils13.safeValidateTypes)({ value: element, schema });
|
4188
4104
|
if (!result.success) {
|
4189
4105
|
return result;
|
4190
4106
|
}
|
@@ -4302,9 +4218,9 @@ function getOutputStrategy({
|
|
4302
4218
|
}) {
|
4303
4219
|
switch (output) {
|
4304
4220
|
case "object":
|
4305
|
-
return objectOutputStrategy((0,
|
4221
|
+
return objectOutputStrategy((0, import_provider_utils13.asSchema)(schema));
|
4306
4222
|
case "array":
|
4307
|
-
return arrayOutputStrategy((0,
|
4223
|
+
return arrayOutputStrategy((0, import_provider_utils13.asSchema)(schema));
|
4308
4224
|
case "enum":
|
4309
4225
|
return enumOutputStrategy(enumValues);
|
4310
4226
|
case "no-schema":
|
@@ -4435,7 +4351,7 @@ function validateObjectGenerationInput({
|
|
4435
4351
|
}
|
4436
4352
|
|
4437
4353
|
// core/generate-object/generate-object.ts
|
4438
|
-
var originalGenerateId = (0,
|
4354
|
+
var originalGenerateId = (0, import_provider_utils14.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
4439
4355
|
async function generateObject(options) {
|
4440
4356
|
const {
|
4441
4357
|
model: modelArg,
|
@@ -4613,7 +4529,7 @@ async function generateObject(options) {
|
|
4613
4529
|
request = (_a17 = generateResult.request) != null ? _a17 : {};
|
4614
4530
|
response = generateResult.responseData;
|
4615
4531
|
async function processResult(result2) {
|
4616
|
-
const parseResult = await (0,
|
4532
|
+
const parseResult = await (0, import_provider_utils14.safeParseJSON)({ text: result2 });
|
4617
4533
|
if (!parseResult.success) {
|
4618
4534
|
throw new NoObjectGeneratedError({
|
4619
4535
|
message: "No object generated: could not parse the response.",
|
@@ -4712,7 +4628,7 @@ var DefaultGenerateObjectResult = class {
|
|
4712
4628
|
};
|
4713
4629
|
|
4714
4630
|
// core/generate-object/stream-object.ts
|
4715
|
-
var
|
4631
|
+
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
4716
4632
|
|
4717
4633
|
// src/util/create-resolvable-promise.ts
|
4718
4634
|
function createResolvablePromise() {
|
@@ -4856,7 +4772,7 @@ function now() {
|
|
4856
4772
|
}
|
4857
4773
|
|
4858
4774
|
// core/generate-object/stream-object.ts
|
4859
|
-
var originalGenerateId2 = (0,
|
4775
|
+
var originalGenerateId2 = (0, import_provider_utils15.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
4860
4776
|
function streamObject(options) {
|
4861
4777
|
const {
|
4862
4778
|
model,
|
@@ -5454,7 +5370,7 @@ var DefaultSpeechResult = class {
|
|
5454
5370
|
};
|
5455
5371
|
|
5456
5372
|
// core/generate-text/generate-text.ts
|
5457
|
-
var
|
5373
|
+
var import_provider_utils18 = require("@ai-sdk/provider-utils");
|
5458
5374
|
|
5459
5375
|
// src/util/as-array.ts
|
5460
5376
|
function asArray(value) {
|
@@ -5462,7 +5378,7 @@ function asArray(value) {
|
|
5462
5378
|
}
|
5463
5379
|
|
5464
5380
|
// core/prompt/prepare-tools-and-tool-choice.ts
|
5465
|
-
var
|
5381
|
+
var import_provider_utils16 = require("@ai-sdk/provider-utils");
|
5466
5382
|
|
5467
5383
|
// src/util/is-non-empty-object.ts
|
5468
5384
|
function isNonEmptyObject(object2) {
|
@@ -5494,7 +5410,7 @@ function prepareToolsAndToolChoice({
|
|
5494
5410
|
type: "function",
|
5495
5411
|
name: name17,
|
5496
5412
|
description: tool2.description,
|
5497
|
-
parameters: (0,
|
5413
|
+
parameters: (0, import_provider_utils16.asSchema)(tool2.parameters).jsonSchema
|
5498
5414
|
};
|
5499
5415
|
case "provider-defined":
|
5500
5416
|
return {
|
@@ -5564,7 +5480,7 @@ function asContent({
|
|
5564
5480
|
}
|
5565
5481
|
|
5566
5482
|
// core/generate-text/parse-tool-call.ts
|
5567
|
-
var
|
5483
|
+
var import_provider_utils17 = require("@ai-sdk/provider-utils");
|
5568
5484
|
async function parseToolCall({
|
5569
5485
|
toolCall,
|
5570
5486
|
tools,
|
@@ -5588,7 +5504,7 @@ async function parseToolCall({
|
|
5588
5504
|
tools,
|
5589
5505
|
parameterSchema: ({ toolName }) => {
|
5590
5506
|
const { parameters } = tools[toolName];
|
5591
|
-
return (0,
|
5507
|
+
return (0, import_provider_utils17.asSchema)(parameters).jsonSchema;
|
5592
5508
|
},
|
5593
5509
|
system,
|
5594
5510
|
messages,
|
@@ -5618,8 +5534,8 @@ async function doParseToolCall({
|
|
5618
5534
|
availableTools: Object.keys(tools)
|
5619
5535
|
});
|
5620
5536
|
}
|
5621
|
-
const schema = (0,
|
5622
|
-
const parseResult = toolCall.args.trim() === "" ? await (0,
|
5537
|
+
const schema = (0, import_provider_utils17.asSchema)(tool2.parameters);
|
5538
|
+
const parseResult = toolCall.args.trim() === "" ? await (0, import_provider_utils17.safeValidateTypes)({ value: {}, schema }) : await (0, import_provider_utils17.safeParseJSON)({ text: toolCall.args, schema });
|
5623
5539
|
if (parseResult.success === false) {
|
5624
5540
|
throw new InvalidToolArgumentsError({
|
5625
5541
|
toolName,
|
@@ -5755,7 +5671,7 @@ function toResponseMessages({
|
|
5755
5671
|
}
|
5756
5672
|
|
5757
5673
|
// core/generate-text/generate-text.ts
|
5758
|
-
var originalGenerateId3 = (0,
|
5674
|
+
var originalGenerateId3 = (0, import_provider_utils18.createIdGenerator)({
|
5759
5675
|
prefix: "aitxt",
|
5760
5676
|
size: 24
|
5761
5677
|
});
|
@@ -6206,7 +6122,7 @@ __export(output_exports, {
|
|
6206
6122
|
object: () => object,
|
6207
6123
|
text: () => text
|
6208
6124
|
});
|
6209
|
-
var
|
6125
|
+
var import_provider_utils19 = require("@ai-sdk/provider-utils");
|
6210
6126
|
var text = () => ({
|
6211
6127
|
type: "text",
|
6212
6128
|
responseFormat: { type: "text" },
|
@@ -6220,7 +6136,7 @@ var text = () => ({
|
|
6220
6136
|
var object = ({
|
6221
6137
|
schema: inputSchema
|
6222
6138
|
}) => {
|
6223
|
-
const schema = (0,
|
6139
|
+
const schema = (0, import_provider_utils19.asSchema)(inputSchema);
|
6224
6140
|
return {
|
6225
6141
|
type: "object",
|
6226
6142
|
responseFormat: {
|
@@ -6246,7 +6162,7 @@ var object = ({
|
|
6246
6162
|
}
|
6247
6163
|
},
|
6248
6164
|
async parseOutput({ text: text2 }, context) {
|
6249
|
-
const parseResult = await (0,
|
6165
|
+
const parseResult = await (0, import_provider_utils19.safeParseJSON)({ text: text2 });
|
6250
6166
|
if (!parseResult.success) {
|
6251
6167
|
throw new NoObjectGeneratedError({
|
6252
6168
|
message: "No object generated: could not parse the response.",
|
@@ -6257,7 +6173,7 @@ var object = ({
|
|
6257
6173
|
finishReason: context.finishReason
|
6258
6174
|
});
|
6259
6175
|
}
|
6260
|
-
const validationResult = await (0,
|
6176
|
+
const validationResult = await (0, import_provider_utils19.safeValidateTypes)({
|
6261
6177
|
value: parseResult.value,
|
6262
6178
|
schema
|
6263
6179
|
});
|
@@ -6277,7 +6193,7 @@ var object = ({
|
|
6277
6193
|
};
|
6278
6194
|
|
6279
6195
|
// core/generate-text/smooth-stream.ts
|
6280
|
-
var
|
6196
|
+
var import_provider_utils20 = require("@ai-sdk/provider-utils");
|
6281
6197
|
var import_provider24 = require("@ai-sdk/provider");
|
6282
6198
|
var CHUNKING_REGEXPS = {
|
6283
6199
|
word: /\S+\s+/m,
|
@@ -6286,7 +6202,7 @@ var CHUNKING_REGEXPS = {
|
|
6286
6202
|
function smoothStream({
|
6287
6203
|
delayInMs = 10,
|
6288
6204
|
chunking = "word",
|
6289
|
-
_internal: { delay: delay2 =
|
6205
|
+
_internal: { delay: delay2 = import_provider_utils20.delay } = {}
|
6290
6206
|
} = {}) {
|
6291
6207
|
let detectChunk;
|
6292
6208
|
if (typeof chunking === "function") {
|
@@ -6346,10 +6262,10 @@ function smoothStream({
|
|
6346
6262
|
}
|
6347
6263
|
|
6348
6264
|
// core/generate-text/stream-text.ts
|
6349
|
-
var
|
6265
|
+
var import_provider_utils22 = require("@ai-sdk/provider-utils");
|
6350
6266
|
|
6351
6267
|
// core/generate-text/run-tools-transformation.ts
|
6352
|
-
var
|
6268
|
+
var import_provider_utils21 = require("@ai-sdk/provider-utils");
|
6353
6269
|
function runToolsTransformation({
|
6354
6270
|
tools,
|
6355
6271
|
generatorStream,
|
@@ -6435,7 +6351,7 @@ function runToolsTransformation({
|
|
6435
6351
|
controller.enqueue(toolCall);
|
6436
6352
|
const tool2 = tools[toolCall.toolName];
|
6437
6353
|
if (tool2.execute != null) {
|
6438
|
-
const toolExecutionId = (0,
|
6354
|
+
const toolExecutionId = (0, import_provider_utils21.generateId)();
|
6439
6355
|
outstandingToolResults.add(toolExecutionId);
|
6440
6356
|
recordSpan({
|
6441
6357
|
name: "ai.toolCall",
|
@@ -6544,7 +6460,7 @@ function runToolsTransformation({
|
|
6544
6460
|
}
|
6545
6461
|
|
6546
6462
|
// core/generate-text/stream-text.ts
|
6547
|
-
var originalGenerateId4 = (0,
|
6463
|
+
var originalGenerateId4 = (0, import_provider_utils22.createIdGenerator)({
|
6548
6464
|
prefix: "aitxt",
|
6549
6465
|
size: 24
|
6550
6466
|
});
|
@@ -7954,7 +7870,7 @@ var DefaultProviderRegistry = class {
|
|
7954
7870
|
};
|
7955
7871
|
|
7956
7872
|
// core/tool/mcp/mcp-client.ts
|
7957
|
-
var
|
7873
|
+
var import_provider_utils24 = require("@ai-sdk/provider-utils");
|
7958
7874
|
|
7959
7875
|
// core/tool/tool.ts
|
7960
7876
|
function tool(tool2) {
|
@@ -7962,7 +7878,7 @@ function tool(tool2) {
|
|
7962
7878
|
}
|
7963
7879
|
|
7964
7880
|
// core/tool/mcp/mcp-sse-transport.ts
|
7965
|
-
var
|
7881
|
+
var import_provider_utils23 = require("@ai-sdk/provider-utils");
|
7966
7882
|
|
7967
7883
|
// core/tool/mcp/json-rpc-message.ts
|
7968
7884
|
var import_zod10 = require("zod");
|
@@ -8133,7 +8049,7 @@ var SseMCPTransport = class {
|
|
8133
8049
|
(_b = this.onerror) == null ? void 0 : _b.call(this, error);
|
8134
8050
|
return reject(error);
|
8135
8051
|
}
|
8136
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough((0,
|
8052
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough((0, import_provider_utils23.createEventSourceParserStream)());
|
8137
8053
|
const reader = stream.getReader();
|
8138
8054
|
const processEvents = async () => {
|
8139
8055
|
var _a18, _b2, _c2;
|
@@ -8457,7 +8373,7 @@ var MCPClient = class {
|
|
8457
8373
|
if (schemas !== "automatic" && !(name17 in schemas)) {
|
8458
8374
|
continue;
|
8459
8375
|
}
|
8460
|
-
const parameters = schemas === "automatic" ? (0,
|
8376
|
+
const parameters = schemas === "automatic" ? (0, import_provider_utils24.jsonSchema)({
|
8461
8377
|
...inputSchema,
|
8462
8378
|
properties: (_a17 = inputSchema.properties) != null ? _a17 : {},
|
8463
8379
|
additionalProperties: false
|
@@ -8587,7 +8503,7 @@ var DefaultTranscriptionResult = class {
|
|
8587
8503
|
0 && (module.exports = {
|
8588
8504
|
AISDKError,
|
8589
8505
|
APICallError,
|
8590
|
-
|
8506
|
+
AbstractChat,
|
8591
8507
|
DefaultChatTransport,
|
8592
8508
|
DownloadError,
|
8593
8509
|
EmptyResponseBodyError,
|
@@ -8637,7 +8553,6 @@ var DefaultTranscriptionResult = class {
|
|
8637
8553
|
createUIMessageStream,
|
8638
8554
|
createUIMessageStreamResponse,
|
8639
8555
|
customProvider,
|
8640
|
-
defaultChatStoreOptions,
|
8641
8556
|
defaultSettingsMiddleware,
|
8642
8557
|
embed,
|
8643
8558
|
embedMany,
|