ai 5.0.0-alpha.12 → 5.0.0-alpha.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/dist/index.mjs CHANGED
@@ -455,42 +455,45 @@ import { parseJsonEventStream } from "@ai-sdk/provider-utils";
455
455
  // src/ui-message-stream/ui-message-stream-parts.ts
456
456
  import { z } from "zod";
457
457
  var uiMessageStreamPartSchema = z.union([
458
- z.object({
458
+ z.strictObject({
459
459
  type: z.literal("text"),
460
460
  text: z.string()
461
461
  }),
462
- z.object({
462
+ z.strictObject({
463
463
  type: z.literal("error"),
464
464
  errorText: z.string()
465
465
  }),
466
- z.object({
466
+ z.strictObject({
467
467
  type: z.literal("tool-call-streaming-start"),
468
468
  toolCallId: z.string(),
469
469
  toolName: z.string()
470
470
  }),
471
- z.object({
471
+ z.strictObject({
472
472
  type: z.literal("tool-call-delta"),
473
473
  toolCallId: z.string(),
474
474
  argsTextDelta: z.string()
475
475
  }),
476
- z.object({
476
+ z.strictObject({
477
477
  type: z.literal("tool-call"),
478
478
  toolCallId: z.string(),
479
479
  toolName: z.string(),
480
480
  args: z.unknown()
481
481
  }),
482
- z.object({
482
+ z.strictObject({
483
483
  type: z.literal("tool-result"),
484
484
  toolCallId: z.string(),
485
485
  result: z.unknown(),
486
486
  providerMetadata: z.any().optional()
487
487
  }),
488
- z.object({
488
+ z.strictObject({
489
489
  type: z.literal("reasoning"),
490
490
  text: z.string(),
491
491
  providerMetadata: z.record(z.any()).optional()
492
492
  }),
493
- z.object({
493
+ z.strictObject({
494
+ type: z.literal("reasoning-part-finish")
495
+ }),
496
+ z.strictObject({
494
497
  type: z.literal("source-url"),
495
498
  sourceId: z.string(),
496
499
  url: z.string(),
@@ -498,7 +501,7 @@ var uiMessageStreamPartSchema = z.union([
498
501
  providerMetadata: z.any().optional()
499
502
  // Use z.any() for generic metadata
500
503
  }),
501
- z.object({
504
+ z.strictObject({
502
505
  type: z.literal("source-document"),
503
506
  sourceId: z.string(),
504
507
  mediaType: z.string(),
@@ -507,39 +510,34 @@ var uiMessageStreamPartSchema = z.union([
507
510
  providerMetadata: z.any().optional()
508
511
  // Use z.any() for generic metadata
509
512
  }),
510
- z.object({
513
+ z.strictObject({
511
514
  type: z.literal("file"),
512
515
  url: z.string(),
513
516
  mediaType: z.string()
514
517
  }),
515
- z.object({
518
+ z.strictObject({
516
519
  type: z.string().startsWith("data-"),
517
520
  id: z.string().optional(),
518
521
  data: z.unknown()
519
522
  }),
520
- z.object({
521
- type: z.literal("metadata"),
522
- value: z.object({ metadata: z.unknown() })
523
- }),
524
- z.object({
525
- type: z.literal("start-step"),
526
- metadata: z.unknown().optional()
523
+ z.strictObject({
524
+ type: z.literal("start-step")
527
525
  }),
528
- z.object({
529
- type: z.literal("finish-step"),
530
- metadata: z.unknown().optional()
526
+ z.strictObject({
527
+ type: z.literal("finish-step")
531
528
  }),
532
- z.object({
529
+ z.strictObject({
533
530
  type: z.literal("start"),
534
531
  messageId: z.string().optional(),
535
- metadata: z.unknown().optional()
532
+ messageMetadata: z.unknown().optional()
536
533
  }),
537
- z.object({
534
+ z.strictObject({
538
535
  type: z.literal("finish"),
539
- metadata: z.unknown().optional()
536
+ messageMetadata: z.unknown().optional()
540
537
  }),
541
- z.object({
542
- type: z.literal("reasoning-part-finish")
538
+ z.strictObject({
539
+ type: z.literal("message-metadata"),
540
+ messageMetadata: z.unknown()
543
541
  })
544
542
  ]);
545
543
  function isDataUIMessageStreamPart(part) {
@@ -1235,27 +1233,26 @@ async function parsePartialJson(jsonText) {
1235
1233
  return { value: void 0, state: "failed-parse" };
1236
1234
  }
1237
1235
 
1238
- // src/ui/get-tool-invocations.ts
1239
- function getToolInvocations(message) {
1240
- return message.parts.filter(
1241
- (part) => part.type === "tool-invocation"
1242
- ).map((part) => part.toolInvocation);
1236
+ // src/ui/ui-messages.ts
1237
+ function isToolUIPart(part) {
1238
+ return part.type.startsWith("tool-");
1239
+ }
1240
+ function getToolName(part) {
1241
+ return part.type.split("-")[1];
1243
1242
  }
1244
1243
 
1245
1244
  // src/ui/process-ui-message-stream.ts
1246
1245
  function createStreamingUIMessageState({
1247
1246
  lastMessage,
1248
- newMessageId = ""
1249
- } = {}) {
1250
- const isContinuation = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
1251
- const message = isContinuation ? lastMessage : {
1252
- id: newMessageId,
1253
- metadata: {},
1254
- role: "assistant",
1255
- parts: []
1256
- };
1247
+ messageId
1248
+ }) {
1257
1249
  return {
1258
- message,
1250
+ message: (lastMessage == null ? void 0 : lastMessage.role) === "assistant" ? lastMessage : {
1251
+ id: messageId,
1252
+ metadata: void 0,
1253
+ role: "assistant",
1254
+ parts: []
1255
+ },
1259
1256
  activeTextPart: void 0,
1260
1257
  activeReasoningPart: void 0,
1261
1258
  partialToolCalls: {}
@@ -1272,16 +1269,21 @@ function processUIMessageStream({
1272
1269
  new TransformStream({
1273
1270
  async transform(part, controller) {
1274
1271
  await runUpdateMessageJob(async ({ state, write }) => {
1275
- function updateToolInvocationPart(toolCallId, invocation) {
1272
+ function updateToolInvocationPart(options) {
1276
1273
  const part2 = state.message.parts.find(
1277
- (part3) => isToolInvocationUIPart(part3) && part3.toolInvocation.toolCallId === toolCallId
1274
+ (part3) => isToolUIPart(part3) && part3.toolCallId === options.toolCallId
1278
1275
  );
1279
1276
  if (part2 != null) {
1280
- part2.toolInvocation = invocation;
1277
+ part2.state = options.state;
1278
+ part2.args = options.args;
1279
+ part2.result = options.result;
1281
1280
  } else {
1282
1281
  state.message.parts.push({
1283
- type: "tool-invocation",
1284
- toolInvocation: invocation
1282
+ type: `tool-${options.toolName}`,
1283
+ toolCallId: options.toolCallId,
1284
+ state: options.state,
1285
+ args: options.args,
1286
+ result: options.result
1285
1287
  });
1286
1288
  }
1287
1289
  }
@@ -1365,16 +1367,16 @@ function processUIMessageStream({
1365
1367
  break;
1366
1368
  }
1367
1369
  case "tool-call-streaming-start": {
1368
- const toolInvocations = getToolInvocations(state.message);
1370
+ const toolInvocations = state.message.parts.filter(isToolUIPart);
1369
1371
  state.partialToolCalls[part.toolCallId] = {
1370
1372
  text: "",
1371
1373
  toolName: part.toolName,
1372
1374
  index: toolInvocations.length
1373
1375
  };
1374
- updateToolInvocationPart(part.toolCallId, {
1375
- state: "partial-call",
1376
+ updateToolInvocationPart({
1376
1377
  toolCallId: part.toolCallId,
1377
1378
  toolName: part.toolName,
1379
+ state: "partial-call",
1378
1380
  args: void 0
1379
1381
  });
1380
1382
  write();
@@ -1386,20 +1388,20 @@ function processUIMessageStream({
1386
1388
  const { value: partialArgs } = await parsePartialJson(
1387
1389
  partialToolCall.text
1388
1390
  );
1389
- updateToolInvocationPart(part.toolCallId, {
1390
- state: "partial-call",
1391
+ updateToolInvocationPart({
1391
1392
  toolCallId: part.toolCallId,
1392
1393
  toolName: partialToolCall.toolName,
1394
+ state: "partial-call",
1393
1395
  args: partialArgs
1394
1396
  });
1395
1397
  write();
1396
1398
  break;
1397
1399
  }
1398
1400
  case "tool-call": {
1399
- updateToolInvocationPart(part.toolCallId, {
1400
- state: "call",
1401
+ updateToolInvocationPart({
1401
1402
  toolCallId: part.toolCallId,
1402
1403
  toolName: part.toolName,
1404
+ state: "call",
1403
1405
  args: part.args
1404
1406
  });
1405
1407
  write();
@@ -1408,10 +1410,10 @@ function processUIMessageStream({
1408
1410
  toolCall: part
1409
1411
  });
1410
1412
  if (result != null) {
1411
- updateToolInvocationPart(part.toolCallId, {
1412
- state: "result",
1413
+ updateToolInvocationPart({
1413
1414
  toolCallId: part.toolCallId,
1414
1415
  toolName: part.toolName,
1416
+ state: "result",
1415
1417
  args: part.args,
1416
1418
  result
1417
1419
  });
@@ -1421,7 +1423,7 @@ function processUIMessageStream({
1421
1423
  break;
1422
1424
  }
1423
1425
  case "tool-result": {
1424
- const toolInvocations = getToolInvocations(state.message);
1426
+ const toolInvocations = state.message.parts.filter(isToolUIPart);
1425
1427
  if (toolInvocations == null) {
1426
1428
  throw new Error("tool_result must be preceded by a tool_call");
1427
1429
  }
@@ -1433,9 +1435,14 @@ function processUIMessageStream({
1433
1435
  "tool_result must be preceded by a tool_call with the same toolCallId"
1434
1436
  );
1435
1437
  }
1436
- updateToolInvocationPart(part.toolCallId, {
1437
- ...toolInvocations[toolInvocationIndex],
1438
+ const toolName = getToolName(
1439
+ toolInvocations[toolInvocationIndex]
1440
+ );
1441
+ updateToolInvocationPart({
1442
+ toolCallId: part.toolCallId,
1443
+ toolName,
1438
1444
  state: "result",
1445
+ args: toolInvocations[toolInvocationIndex].args,
1439
1446
  result: part.result
1440
1447
  });
1441
1448
  write();
@@ -1443,39 +1450,33 @@ function processUIMessageStream({
1443
1450
  }
1444
1451
  case "start-step": {
1445
1452
  state.message.parts.push({ type: "step-start" });
1446
- await updateMessageMetadata(part.metadata);
1447
- write();
1448
1453
  break;
1449
1454
  }
1450
1455
  case "finish-step": {
1451
1456
  state.activeTextPart = void 0;
1452
1457
  state.activeReasoningPart = void 0;
1453
- await updateMessageMetadata(part.metadata);
1454
- if (part.metadata != null) {
1455
- write();
1456
- }
1457
1458
  break;
1458
1459
  }
1459
1460
  case "start": {
1460
1461
  if (part.messageId != null) {
1461
1462
  state.message.id = part.messageId;
1462
1463
  }
1463
- await updateMessageMetadata(part.metadata);
1464
- if (part.messageId != null || part.metadata != null) {
1464
+ await updateMessageMetadata(part.messageMetadata);
1465
+ if (part.messageId != null || part.messageMetadata != null) {
1465
1466
  write();
1466
1467
  }
1467
1468
  break;
1468
1469
  }
1469
1470
  case "finish": {
1470
- await updateMessageMetadata(part.metadata);
1471
- if (part.metadata != null) {
1471
+ await updateMessageMetadata(part.messageMetadata);
1472
+ if (part.messageMetadata != null) {
1472
1473
  write();
1473
1474
  }
1474
1475
  break;
1475
1476
  }
1476
- case "metadata": {
1477
- await updateMessageMetadata(part.metadata);
1478
- if (part.metadata != null) {
1477
+ case "message-metadata": {
1478
+ await updateMessageMetadata(part.messageMetadata);
1479
+ if (part.messageMetadata != null) {
1479
1480
  write();
1480
1481
  }
1481
1482
  break;
@@ -1503,9 +1504,6 @@ function processUIMessageStream({
1503
1504
  })
1504
1505
  );
1505
1506
  }
1506
- function isToolInvocationUIPart(part) {
1507
- return part.type === "tool-invocation";
1508
- }
1509
1507
  function isObject(value) {
1510
1508
  return typeof value === "object" && value !== null;
1511
1509
  }
@@ -1540,8 +1538,8 @@ function isAssistantMessageWithCompletedToolCalls(message) {
1540
1538
  const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
1541
1539
  return part.type === "step-start" ? index : lastIndex;
1542
1540
  }, -1);
1543
- const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter((part) => part.type === "tool-invocation");
1544
- return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
1541
+ const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
1542
+ return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "result");
1545
1543
  }
1546
1544
 
1547
1545
  // src/ui/chat.ts
@@ -1618,7 +1616,7 @@ var AbstractChat = class {
1618
1616
  result
1619
1617
  }) => {
1620
1618
  this.jobExecutor.run(async () => {
1621
- updateToolCallResult({
1619
+ updateToolResult({
1622
1620
  messages: this.state.messages,
1623
1621
  toolCallId,
1624
1622
  toolResult: result
@@ -1644,7 +1642,6 @@ var AbstractChat = class {
1644
1642
  return;
1645
1643
  if ((_a17 = this.activeResponse) == null ? void 0 : _a17.abortController) {
1646
1644
  this.activeResponse.abortController.abort();
1647
- this.activeResponse.abortController = void 0;
1648
1645
  }
1649
1646
  };
1650
1647
  this.id = id;
@@ -1705,7 +1702,7 @@ var AbstractChat = class {
1705
1702
  const activeResponse = {
1706
1703
  state: createStreamingUIMessageState({
1707
1704
  lastMessage: this.state.snapshot(lastMessage),
1708
- newMessageId: this.generateId()
1705
+ messageId: this.generateId()
1709
1706
  }),
1710
1707
  abortController: new AbortController()
1711
1708
  };
@@ -1782,23 +1779,20 @@ var AbstractChat = class {
1782
1779
  }
1783
1780
  }
1784
1781
  };
1785
- function updateToolCallResult({
1782
+ function updateToolResult({
1786
1783
  messages,
1787
1784
  toolCallId,
1788
1785
  toolResult: result
1789
1786
  }) {
1790
1787
  const lastMessage = messages[messages.length - 1];
1791
- const invocationPart = lastMessage.parts.find(
1792
- (part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
1788
+ const toolPart = lastMessage.parts.find(
1789
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId
1793
1790
  );
1794
- if (invocationPart == null) {
1791
+ if (toolPart == null) {
1795
1792
  return;
1796
1793
  }
1797
- invocationPart.toolInvocation = {
1798
- ...invocationPart.toolInvocation,
1799
- state: "result",
1800
- result
1801
- };
1794
+ toolPart.state = "result";
1795
+ toolPart.result = result;
1802
1796
  }
1803
1797
 
1804
1798
  // src/ui/convert-to-model-messages.ts
@@ -1839,75 +1833,71 @@ function convertToModelMessages(messages, options) {
1839
1833
  }
1840
1834
  const content = [];
1841
1835
  for (const part of block) {
1842
- switch (part.type) {
1843
- case "text": {
1844
- content.push(part);
1845
- break;
1846
- }
1847
- case "file": {
1848
- content.push({
1849
- type: "file",
1850
- mediaType: part.mediaType,
1851
- data: part.url
1852
- });
1853
- break;
1854
- }
1855
- case "reasoning": {
1856
- content.push({
1857
- type: "reasoning",
1858
- text: part.text,
1859
- providerOptions: part.providerMetadata
1836
+ if (part.type === "text") {
1837
+ content.push(part);
1838
+ } else if (part.type === "file") {
1839
+ content.push({
1840
+ type: "file",
1841
+ mediaType: part.mediaType,
1842
+ data: part.url
1843
+ });
1844
+ } else if (part.type === "reasoning") {
1845
+ content.push({
1846
+ type: "reasoning",
1847
+ text: part.text,
1848
+ providerOptions: part.providerMetadata
1849
+ });
1850
+ } else if (isToolUIPart(part)) {
1851
+ const toolName = getToolName(part);
1852
+ if (part.state === "partial-call") {
1853
+ throw new MessageConversionError({
1854
+ originalMessage: message,
1855
+ message: `Partial tool call is not supported: ${part.toolCallId}`
1860
1856
  });
1861
- break;
1862
- }
1863
- case "tool-invocation":
1857
+ } else {
1864
1858
  content.push({
1865
1859
  type: "tool-call",
1866
- toolCallId: part.toolInvocation.toolCallId,
1867
- toolName: part.toolInvocation.toolName,
1868
- args: part.toolInvocation.args
1860
+ toolCallId: part.toolCallId,
1861
+ toolName,
1862
+ args: part.args
1869
1863
  });
1870
- break;
1871
- default: {
1872
- const _exhaustiveCheck = part;
1873
- throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
1874
1864
  }
1865
+ } else {
1866
+ const _exhaustiveCheck = part;
1867
+ throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
1875
1868
  }
1876
1869
  }
1877
1870
  modelMessages.push({
1878
1871
  role: "assistant",
1879
1872
  content
1880
1873
  });
1881
- const stepInvocations = block.filter(
1882
- (part) => part.type === "tool-invocation"
1883
- ).map((part) => part.toolInvocation);
1884
- if (stepInvocations.length > 0) {
1874
+ const toolParts = block.filter(isToolUIPart);
1875
+ if (toolParts.length > 0) {
1885
1876
  modelMessages.push({
1886
1877
  role: "tool",
1887
- content: stepInvocations.map(
1888
- (toolInvocation) => {
1889
- if (!("result" in toolInvocation)) {
1890
- throw new MessageConversionError({
1891
- originalMessage: message,
1892
- message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
1893
- });
1894
- }
1895
- const { toolCallId, toolName, result } = toolInvocation;
1896
- const tool2 = tools[toolName];
1897
- return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
1898
- type: "tool-result",
1899
- toolCallId,
1900
- toolName,
1901
- result: tool2.experimental_toToolResultContent(result),
1902
- experimental_content: tool2.experimental_toToolResultContent(result)
1903
- } : {
1904
- type: "tool-result",
1905
- toolCallId,
1906
- toolName,
1907
- result
1908
- };
1878
+ content: toolParts.map((toolPart) => {
1879
+ if (toolPart.state !== "result") {
1880
+ throw new MessageConversionError({
1881
+ originalMessage: message,
1882
+ message: "ToolInvocation must have a result: " + JSON.stringify(toolPart)
1883
+ });
1909
1884
  }
1910
- )
1885
+ const toolName = getToolName(toolPart);
1886
+ const { toolCallId, result } = toolPart;
1887
+ const tool2 = tools[toolName];
1888
+ return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
1889
+ type: "tool-result",
1890
+ toolCallId,
1891
+ toolName,
1892
+ result: tool2.experimental_toToolResultContent(result),
1893
+ experimental_content: tool2.experimental_toToolResultContent(result)
1894
+ } : {
1895
+ type: "tool-result",
1896
+ toolCallId,
1897
+ toolName,
1898
+ result
1899
+ };
1900
+ })
1911
1901
  });
1912
1902
  }
1913
1903
  block = [];
@@ -1915,18 +1905,10 @@ function convertToModelMessages(messages, options) {
1915
1905
  var processBlock = processBlock2;
1916
1906
  let block = [];
1917
1907
  for (const part of message.parts) {
1918
- switch (part.type) {
1919
- case "text":
1920
- case "reasoning":
1921
- case "file":
1922
- case "tool-invocation": {
1923
- block.push(part);
1924
- break;
1925
- }
1926
- case "step-start": {
1927
- processBlock2();
1928
- break;
1929
- }
1908
+ if (part.type === "text" || part.type === "reasoning" || part.type === "file" || isToolUIPart(part)) {
1909
+ block.push(part);
1910
+ } else if (part.type === "step-start") {
1911
+ processBlock2();
1930
1912
  }
1931
1913
  }
1932
1914
  processBlock2();
@@ -2056,9 +2038,14 @@ var TextStreamChatTransport = class {
2056
2038
  }
2057
2039
  };
2058
2040
 
2041
+ // src/ui-message-stream/create-ui-message-stream.ts
2042
+ import {
2043
+ generateId as generateIdFunc2
2044
+ } from "@ai-sdk/provider-utils";
2045
+
2059
2046
  // src/ui-message-stream/handle-ui-message-stream-finish.ts
2060
2047
  function handleUIMessageStreamFinish({
2061
- newMessageId,
2048
+ messageId,
2062
2049
  originalMessages = [],
2063
2050
  onFinish,
2064
2051
  stream
@@ -2066,19 +2053,30 @@ function handleUIMessageStreamFinish({
2066
2053
  if (onFinish == null) {
2067
2054
  return stream;
2068
2055
  }
2069
- const lastMessage = originalMessages[originalMessages.length - 1];
2070
- const isContinuation = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
2071
- const messageId = isContinuation ? lastMessage.id : newMessageId;
2056
+ const lastMessage = originalMessages == null ? void 0 : originalMessages[originalMessages.length - 1];
2072
2057
  const state = createStreamingUIMessageState({
2073
- lastMessage: structuredClone(lastMessage),
2074
- newMessageId: messageId
2058
+ lastMessage: lastMessage ? structuredClone(lastMessage) : void 0,
2059
+ messageId
2060
+ // will be overridden by the stream
2075
2061
  });
2076
2062
  const runUpdateMessageJob = async (job) => {
2077
2063
  await job({ state, write: () => {
2078
2064
  } });
2079
2065
  };
2080
2066
  return processUIMessageStream({
2081
- stream,
2067
+ stream: stream.pipeThrough(
2068
+ new TransformStream({
2069
+ transform(chunk, controller) {
2070
+ if (chunk.type === "start") {
2071
+ const startChunk = chunk;
2072
+ if (startChunk.messageId == null) {
2073
+ startChunk.messageId = messageId;
2074
+ }
2075
+ }
2076
+ controller.enqueue(chunk);
2077
+ }
2078
+ })
2079
+ ),
2082
2080
  runUpdateMessageJob
2083
2081
  }).pipeThrough(
2084
2082
  new TransformStream({
@@ -2086,12 +2084,12 @@ function handleUIMessageStreamFinish({
2086
2084
  controller.enqueue(chunk);
2087
2085
  },
2088
2086
  flush() {
2089
- const isContinuation2 = state.message.id === (lastMessage == null ? void 0 : lastMessage.id);
2087
+ const isContinuation = state.message.id === (lastMessage == null ? void 0 : lastMessage.id);
2090
2088
  onFinish({
2091
- isContinuation: isContinuation2,
2089
+ isContinuation,
2092
2090
  responseMessage: state.message,
2093
2091
  messages: [
2094
- ...isContinuation2 ? originalMessages.slice(0, -1) : originalMessages,
2092
+ ...isContinuation ? originalMessages.slice(0, -1) : originalMessages,
2095
2093
  state.message
2096
2094
  ]
2097
2095
  });
@@ -2106,7 +2104,8 @@ function createUIMessageStream({
2106
2104
  onError = () => "An error occurred.",
2107
2105
  // mask error messages for safety by default
2108
2106
  originalMessages,
2109
- onFinish
2107
+ onFinish,
2108
+ generateId: generateId3 = generateIdFunc2
2110
2109
  }) {
2111
2110
  let controller;
2112
2111
  const ongoingStreamPromises = [];
@@ -2178,7 +2177,7 @@ function createUIMessageStream({
2178
2177
  });
2179
2178
  return handleUIMessageStreamFinish({
2180
2179
  stream,
2181
- newMessageId: "",
2180
+ messageId: generateId3(),
2182
2181
  originalMessages,
2183
2182
  onFinish
2184
2183
  });
@@ -6434,6 +6433,18 @@ function runToolsTransformation({
6434
6433
  });
6435
6434
  }
6436
6435
 
6436
+ // src/ui-message-stream/get-response-ui-message-id.ts
6437
+ function getResponseUIMessageId({
6438
+ originalMessages,
6439
+ responseMessageId
6440
+ }) {
6441
+ if (originalMessages == null) {
6442
+ return void 0;
6443
+ }
6444
+ const lastMessage = originalMessages[originalMessages.length - 1];
6445
+ return (lastMessage == null ? void 0 : lastMessage.role) === "assistant" ? lastMessage.id : typeof responseMessageId === "function" ? responseMessageId() : responseMessageId;
6446
+ }
6447
+
6437
6448
  // core/generate-text/stream-text.ts
6438
6449
  var originalGenerateId4 = createIdGenerator4({
6439
6450
  prefix: "aitxt",
@@ -6598,7 +6609,7 @@ var DefaultStreamTextResult = class {
6598
6609
  async transform(chunk, controller) {
6599
6610
  controller.enqueue(chunk);
6600
6611
  const { part } = chunk;
6601
- if (part.type === "text" || part.type === "reasoning" || part.type === "source" || part.type === "tool-call" || part.type === "tool-result" || part.type === "tool-call-streaming-start" || part.type === "tool-call-delta") {
6612
+ if (part.type === "text" || part.type === "reasoning" || part.type === "source" || part.type === "tool-call" || part.type === "tool-result" || part.type === "tool-call-streaming-start" || part.type === "tool-call-delta" || part.type === "raw") {
6602
6613
  await (onChunk == null ? void 0 : onChunk({ chunk: part }));
6603
6614
  }
6604
6615
  if (part.type === "error") {
@@ -7047,7 +7058,9 @@ var DefaultStreamTextResult = class {
7047
7058
  break;
7048
7059
  }
7049
7060
  case "raw": {
7050
- controller.enqueue(chunk);
7061
+ if (includeRawChunks2) {
7062
+ controller.enqueue(chunk);
7063
+ }
7051
7064
  break;
7052
7065
  }
7053
7066
  default: {
@@ -7270,23 +7283,24 @@ var DefaultStreamTextResult = class {
7270
7283
  );
7271
7284
  }
7272
7285
  toUIMessageStream({
7273
- newMessageId,
7274
- originalMessages = [],
7286
+ originalMessages,
7275
7287
  onFinish,
7276
7288
  messageMetadata,
7277
- sendReasoning = false,
7289
+ sendReasoning = true,
7278
7290
  sendSources = false,
7279
7291
  sendStart = true,
7280
7292
  sendFinish = true,
7281
7293
  onError = () => "An error occurred."
7282
7294
  // mask error messages for safety by default
7283
7295
  } = {}) {
7284
- const lastMessage = originalMessages[originalMessages.length - 1];
7285
- const isContinuation = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
7286
- const messageId = isContinuation ? lastMessage.id : newMessageId;
7296
+ const responseMessageId = getResponseUIMessageId({
7297
+ originalMessages,
7298
+ responseMessageId: this.generateId
7299
+ });
7287
7300
  const baseStream = this.fullStream.pipeThrough(
7288
7301
  new TransformStream({
7289
7302
  transform: async (part, controller) => {
7303
+ const messageMetadataValue = messageMetadata == null ? void 0 : messageMetadata({ part });
7290
7304
  const partType = part.type;
7291
7305
  switch (partType) {
7292
7306
  case "text": {
@@ -7383,38 +7397,28 @@ var DefaultStreamTextResult = class {
7383
7397
  break;
7384
7398
  }
7385
7399
  case "start-step": {
7386
- const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
7387
- controller.enqueue({
7388
- type: "start-step",
7389
- metadata
7390
- });
7400
+ controller.enqueue({ type: "start-step" });
7391
7401
  break;
7392
7402
  }
7393
7403
  case "finish-step": {
7394
- const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
7395
- controller.enqueue({
7396
- type: "finish-step",
7397
- metadata
7398
- });
7404
+ controller.enqueue({ type: "finish-step" });
7399
7405
  break;
7400
7406
  }
7401
7407
  case "start": {
7402
7408
  if (sendStart) {
7403
- const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
7404
7409
  controller.enqueue({
7405
7410
  type: "start",
7406
- messageId,
7407
- metadata
7411
+ messageId: responseMessageId,
7412
+ messageMetadata: messageMetadataValue
7408
7413
  });
7409
7414
  }
7410
7415
  break;
7411
7416
  }
7412
7417
  case "finish": {
7413
7418
  if (sendFinish) {
7414
- const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
7415
7419
  controller.enqueue({
7416
7420
  type: "finish",
7417
- metadata
7421
+ messageMetadata: messageMetadataValue
7418
7422
  });
7419
7423
  }
7420
7424
  break;
@@ -7427,18 +7431,23 @@ var DefaultStreamTextResult = class {
7427
7431
  throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
7428
7432
  }
7429
7433
  }
7434
+ if (messageMetadataValue != null && partType !== "start" && partType !== "finish") {
7435
+ controller.enqueue({
7436
+ type: "message-metadata",
7437
+ messageMetadata: messageMetadataValue
7438
+ });
7439
+ }
7430
7440
  }
7431
7441
  })
7432
7442
  );
7433
7443
  return handleUIMessageStreamFinish({
7434
7444
  stream: baseStream,
7435
- newMessageId: messageId != null ? messageId : this.generateId(),
7445
+ messageId: responseMessageId != null ? responseMessageId : this.generateId(),
7436
7446
  originalMessages,
7437
7447
  onFinish
7438
7448
  });
7439
7449
  }
7440
7450
  pipeUIMessageStreamToResponse(response, {
7441
- newMessageId,
7442
7451
  originalMessages,
7443
7452
  onFinish,
7444
7453
  messageMetadata,
@@ -7452,7 +7461,6 @@ var DefaultStreamTextResult = class {
7452
7461
  pipeUIMessageStreamToResponse({
7453
7462
  response,
7454
7463
  stream: this.toUIMessageStream({
7455
- newMessageId,
7456
7464
  originalMessages,
7457
7465
  onFinish,
7458
7466
  messageMetadata,
@@ -7473,7 +7481,6 @@ var DefaultStreamTextResult = class {
7473
7481
  });
7474
7482
  }
7475
7483
  toUIMessageStreamResponse({
7476
- newMessageId,
7477
7484
  originalMessages,
7478
7485
  onFinish,
7479
7486
  messageMetadata,
@@ -7486,7 +7493,6 @@ var DefaultStreamTextResult = class {
7486
7493
  } = {}) {
7487
7494
  return createUIMessageStreamResponse({
7488
7495
  stream: this.toUIMessageStream({
7489
- newMessageId,
7490
7496
  originalMessages,
7491
7497
  onFinish,
7492
7498
  messageMetadata,
@@ -8583,9 +8589,10 @@ export {
8583
8589
  generateObject,
8584
8590
  generateText,
8585
8591
  getTextFromDataUrl,
8586
- getToolInvocations,
8592
+ getToolName,
8587
8593
  hasToolCall,
8588
8594
  isDeepEqualData,
8595
+ isToolUIPart,
8589
8596
  jsonSchema2 as jsonSchema,
8590
8597
  modelMessageSchema,
8591
8598
  parsePartialJson,