ai 6.0.303 → 6.0.305

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/dist/index.mjs CHANGED
@@ -1225,7 +1225,7 @@ import {
1225
1225
  } from "@ai-sdk/provider-utils";
1226
1226
 
1227
1227
  // src/version.ts
1228
- var VERSION = true ? "6.0.303" : "0.0.0-test";
1228
+ var VERSION = true ? "6.0.305" : "0.0.0-test";
1229
1229
 
1230
1230
  // src/util/download/download.ts
1231
1231
  var download = async ({
@@ -1428,32 +1428,6 @@ async function convertToLanguageModelPrompt({
1428
1428
  download2 != null ? download2 : createDefaultDownloadFunction(void 0, abortSignal),
1429
1429
  supportedUrls
1430
1430
  );
1431
- const approvalIdToToolCallId = /* @__PURE__ */ new Map();
1432
- for (const message of prompt.messages) {
1433
- if (message.role === "assistant" && Array.isArray(message.content)) {
1434
- for (const part of message.content) {
1435
- if (part.type === "tool-approval-request" && "approvalId" in part && "toolCallId" in part) {
1436
- approvalIdToToolCallId.set(
1437
- part.approvalId,
1438
- part.toolCallId
1439
- );
1440
- }
1441
- }
1442
- }
1443
- }
1444
- const approvedToolCallIds = /* @__PURE__ */ new Set();
1445
- for (const message of prompt.messages) {
1446
- if (message.role === "tool") {
1447
- for (const part of message.content) {
1448
- if (part.type === "tool-approval-response") {
1449
- const toolCallId = approvalIdToToolCallId.get(part.approvalId);
1450
- if (toolCallId) {
1451
- approvedToolCallIds.add(toolCallId);
1452
- }
1453
- }
1454
- }
1455
- }
1456
- }
1457
1431
  const messages = [
1458
1432
  ...prompt.system != null ? typeof prompt.system === "string" ? [{ role: "system", content: prompt.system }] : asArray(prompt.system).map((message) => ({
1459
1433
  role: "system",
@@ -1506,9 +1480,6 @@ async function convertToLanguageModelPrompt({
1506
1480
  }
1507
1481
  case "user":
1508
1482
  case "system":
1509
- for (const id of approvedToolCallIds) {
1510
- toolCallIds.delete(id);
1511
- }
1512
1483
  if (toolCallIds.size > 0) {
1513
1484
  throw new MissingToolResultsError({
1514
1485
  toolCallIds: Array.from(toolCallIds)
@@ -1517,9 +1488,6 @@ async function convertToLanguageModelPrompt({
1517
1488
  break;
1518
1489
  }
1519
1490
  }
1520
- for (const id of approvedToolCallIds) {
1521
- toolCallIds.delete(id);
1522
- }
1523
1491
  if (toolCallIds.size > 0) {
1524
1492
  throw new MissingToolResultsError({ toolCallIds: Array.from(toolCallIds) });
1525
1493
  }
@@ -6052,13 +6020,15 @@ function processUIMessageStream({
6052
6020
  onError,
6053
6021
  onToolCall,
6054
6022
  onData,
6055
- resetStateOnMessageIdChange = false
6023
+ resetStateOnMessageIdChange = false,
6024
+ resetStateOnFirstMessageStart = false
6056
6025
  }) {
6026
+ let isFirstMessageStart = true;
6057
6027
  return stream.pipeThrough(
6058
6028
  new TransformStream({
6059
6029
  async transform(chunk, controller) {
6060
6030
  await runUpdateMessageJob(async ({ state, write }) => {
6061
- var _a23, _b, _c, _d, _e, _f, _g, _h;
6031
+ var _a23, _b, _c, _d, _e, _f, _g, _h, _i;
6062
6032
  function getCurrentStepParts() {
6063
6033
  const parts = state.message.parts;
6064
6034
  let currentStepStartIndex = parts.length - 1;
@@ -6565,12 +6535,14 @@ function processUIMessageStream({
6565
6535
  break;
6566
6536
  }
6567
6537
  case "start": {
6568
- if (resetStateOnMessageIdChange && chunk.messageId != null && chunk.messageId !== state.message.id) {
6538
+ const shouldReset = resetStateOnFirstMessageStart && isFirstMessageStart || resetStateOnMessageIdChange && chunk.messageId != null && chunk.messageId !== state.message.id;
6539
+ isFirstMessageStart = false;
6540
+ if (shouldReset) {
6569
6541
  Object.assign(
6570
6542
  state,
6571
6543
  createStreamingUIMessageState({
6572
6544
  lastMessage: void 0,
6573
- messageId: chunk.messageId
6545
+ messageId: (_i = chunk.messageId) != null ? _i : state.message.id
6574
6546
  }),
6575
6547
  { finishReason: void 0 }
6576
6548
  );
@@ -6579,7 +6551,7 @@ function processUIMessageStream({
6579
6551
  state.message.id = chunk.messageId;
6580
6552
  }
6581
6553
  await updateMessageMetadata(chunk.messageMetadata);
6582
- if (chunk.messageId != null || chunk.messageMetadata != null) {
6554
+ if (shouldReset || chunk.messageId != null || chunk.messageMetadata != null) {
6583
6555
  write({ updateStatus: false });
6584
6556
  }
6585
6557
  break;
@@ -14635,8 +14607,12 @@ var HttpChatTransport = class {
14635
14607
 
14636
14608
  // src/ui/default-chat-transport.ts
14637
14609
  var DefaultChatTransport = class extends HttpChatTransport {
14638
- constructor(options = {}) {
14610
+ constructor({
14611
+ resumeStreamIsReplay = true,
14612
+ ...options
14613
+ } = {}) {
14639
14614
  super(options);
14615
+ this.resumeStreamIsReplay = resumeStreamIsReplay;
14640
14616
  }
14641
14617
  processResponseStream(stream) {
14642
14618
  return parseJsonEventStream2({
@@ -14781,6 +14757,27 @@ var AbstractChat = class {
14781
14757
  this.resumeStream = async (options = {}) => {
14782
14758
  await this.makeRequest({ trigger: "resume-stream", ...options });
14783
14759
  };
14760
+ /**
14761
+ * Resume the stream if the latest response ended with a resumable network
14762
+ * disconnect. If a response is still active, wait for it to settle first so
14763
+ * disconnects reported shortly after the caller's trigger are handled.
14764
+ *
14765
+ * @internal
14766
+ */
14767
+ this["~resumeStreamIfDisconnected"] = async ({
14768
+ shouldResume = () => true
14769
+ } = {}) => {
14770
+ const activeResponse = this.activeResponse;
14771
+ if (this.resumableStreamState == null && activeResponse != null) {
14772
+ await activeResponse.completionPromise;
14773
+ if (this.resumableStreamState !== activeResponse.state) {
14774
+ return;
14775
+ }
14776
+ }
14777
+ if (this.resumableStreamState != null && shouldResume()) {
14778
+ await this.resumeStream();
14779
+ }
14780
+ };
14784
14781
  /**
14785
14782
  * Clear the error state and set the status to ready if the chat is in an error state.
14786
14783
  */
@@ -14841,27 +14838,31 @@ var AbstractChat = class {
14841
14838
  options
14842
14839
  }) => this.jobExecutor.run(async () => {
14843
14840
  const messages = this.state.messages;
14844
- const lastMessage = messages[messages.length - 1];
14845
14841
  const updatePart = (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state, output, errorText } : part;
14846
- this.state.replaceMessage(messages.length - 1, {
14847
- ...lastMessage,
14848
- parts: lastMessage.parts.map(updatePart)
14849
- });
14842
+ const messageIndex = messages.findIndex(
14843
+ (message) => message.parts.some(
14844
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId
14845
+ )
14846
+ );
14847
+ if (messageIndex !== -1) {
14848
+ const message = messages[messageIndex];
14849
+ this.state.replaceMessage(messageIndex, {
14850
+ ...message,
14851
+ parts: message.parts.map(updatePart)
14852
+ });
14853
+ }
14850
14854
  if (this.activeResponse) {
14851
14855
  this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
14852
14856
  }
14853
- if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
14857
+ if (messageIndex !== -1 && this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
14854
14858
  const shouldSend = await this.shouldSendAutomatically();
14855
14859
  if (shouldSend) {
14856
14860
  void this.runAutomaticRequest(
14857
- () => {
14858
- var _a23;
14859
- return this.makeRequest({
14860
- trigger: "submit-message",
14861
- messageId: (_a23 = this.lastMessage) == null ? void 0 : _a23.id,
14862
- ...options
14863
- });
14864
- },
14861
+ () => this.makeRequest({
14862
+ trigger: "submit-message",
14863
+ messageId: messages[messageIndex].id,
14864
+ ...options
14865
+ }),
14865
14866
  shouldSend
14866
14867
  );
14867
14868
  }
@@ -15065,6 +15066,7 @@ var AbstractChat = class {
15065
15066
  const lastMessage = this.lastMessage;
15066
15067
  const responseMessageIndex = trigger === "submit-message" && messageId != null ? this.state.messages.findIndex((message) => message.id === messageId) : this.state.messages.length - 1;
15067
15068
  const responseMessage = responseMessageIndex === -1 ? lastMessage : this.state.messages[responseMessageIndex];
15069
+ const originalResponseMessageId = responseMessage == null ? void 0 : responseMessage.id;
15068
15070
  const resumableResponseMessage = trigger === "resume-stream" && (responseMessage == null ? void 0 : responseMessage.role) === "assistant" && responseMessage.parts.some(
15069
15071
  (part) => isToolUIPart(part) && part.state === "input-streaming"
15070
15072
  ) ? this.state.snapshot(responseMessage) : void 0;
@@ -15111,23 +15113,32 @@ var AbstractChat = class {
15111
15113
  return job({
15112
15114
  state: response.state,
15113
15115
  write: ({ updateStatus = true } = {}) => {
15114
- var _a24;
15115
15116
  if (response.abortController.signal.aborted) {
15116
15117
  return;
15117
15118
  }
15118
15119
  if (updateStatus) {
15119
15120
  this.setStatus({ status: "streaming" });
15120
15121
  }
15121
- if (usesEarlierAssistantMessage) {
15122
+ const existingMessageIndex = this.state.messages.findLastIndex(
15123
+ (message) => message.id === response.state.message.id
15124
+ );
15125
+ if (existingMessageIndex !== -1) {
15122
15126
  this.state.replaceMessage(
15123
- responseMessageIndex,
15127
+ existingMessageIndex,
15124
15128
  response.state.message
15125
15129
  );
15126
- } else if (response.state.message.id === ((_a24 = this.lastMessage) == null ? void 0 : _a24.id)) {
15127
- this.state.replaceMessage(
15128
- this.state.messages.length - 1,
15129
- response.state.message
15130
+ } else if (usesEarlierAssistantMessage) {
15131
+ const originalMessageIndex = this.state.messages.findLastIndex(
15132
+ (message) => message.id === originalResponseMessageId
15130
15133
  );
15134
+ if (originalMessageIndex !== -1) {
15135
+ this.state.replaceMessage(
15136
+ originalMessageIndex,
15137
+ response.state.message
15138
+ );
15139
+ } else {
15140
+ this.state.pushMessage(response.state.message);
15141
+ }
15131
15142
  } else {
15132
15143
  this.state.pushMessage(response.state.message);
15133
15144
  }
@@ -15138,6 +15149,8 @@ var AbstractChat = class {
15138
15149
  await consumeStream({
15139
15150
  stream: processUIMessageStream({
15140
15151
  stream,
15152
+ resetStateOnMessageIdChange: trigger === "resume-stream",
15153
+ resetStateOnFirstMessageStart: trigger === "resume-stream" && this.transport.resumeStreamIsReplay,
15141
15154
  onToolCall: this.onToolCall,
15142
15155
  onData: this.onData,
15143
15156
  messageMetadataSchema: this.messageMetadataSchema,