@mastra/client-js 1.21.0-alpha.6 → 1.21.0-alpha.7

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.js CHANGED
@@ -1289,6 +1289,7 @@ var Agent = class extends BaseResource {
1289
1289
  update,
1290
1290
  onToolCall,
1291
1291
  onFinish,
1292
+ onStreamChunk,
1292
1293
  getCurrentDate = () => /* @__PURE__ */ new Date(),
1293
1294
  lastMessage
1294
1295
  }) {
@@ -1356,6 +1357,7 @@ var Agent = class extends BaseResource {
1356
1357
  // TODO: casting as any here because the stream types were all typed as any before in core.
1357
1358
  // but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
1358
1359
  onChunk: async (chunk) => {
1360
+ onStreamChunk?.(chunk);
1359
1361
  switch (chunk.type) {
1360
1362
  case "tripwire": {
1361
1363
  message.parts.push({
@@ -1457,6 +1459,7 @@ var Agent = class extends BaseResource {
1457
1459
  execUpdate();
1458
1460
  }
1459
1461
  }
1462
+ break;
1460
1463
  }
1461
1464
  case "tool-call-input-streaming-start": {
1462
1465
  if (message.toolInvocations == null) {
@@ -1573,6 +1576,7 @@ var Agent = class extends BaseResource {
1573
1576
  try {
1574
1577
  let toolCalls = [];
1575
1578
  let messages = [];
1579
+ let streamRunId = processedParams.runId;
1576
1580
  const [streamForController, streamForProcessing] = response.body.tee();
1577
1581
  const pipePromise = streamForController.pipeTo(
1578
1582
  new WritableStream({
@@ -1626,25 +1630,76 @@ var Agent = class extends BaseResource {
1626
1630
  const clientTool = processedParams.clientTools?.[toolCall2.toolName];
1627
1631
  if (clientTool && clientTool.execute) {
1628
1632
  shouldExecuteClientTool = true;
1629
- const { result, observability } = await executeClientToolWithObservability({
1630
- clientTool,
1631
- args: toolCall2?.args,
1632
- toolName: toolCall2.toolName,
1633
- parentContext: getClientToolObservabilityContext(toolCall2),
1634
- executeContext: {
1635
- requestContext: processedParams.requestContext,
1636
- tracingContext: { currentSpan: void 0 },
1637
- agent: {
1638
- agentId: this.agentId,
1639
- messages: response.messages,
1640
- toolCallId: toolCall2?.toolCallId,
1641
- suspend: async () => {
1642
- },
1643
- threadId,
1644
- resourceId
1633
+ const runId = streamRunId ?? toolCall2.toolCallId;
1634
+ let result;
1635
+ let observability;
1636
+ let synthetic;
1637
+ try {
1638
+ ({ result, observability } = await executeClientToolWithObservability({
1639
+ clientTool,
1640
+ args: toolCall2?.args,
1641
+ toolName: toolCall2.toolName,
1642
+ parentContext: getClientToolObservabilityContext(toolCall2),
1643
+ executeContext: {
1644
+ requestContext: processedParams.requestContext,
1645
+ tracingContext: { currentSpan: void 0 },
1646
+ agent: {
1647
+ agentId: this.agentId,
1648
+ messages: response.messages,
1649
+ toolCallId: toolCall2?.toolCallId,
1650
+ suspend: async () => {
1651
+ },
1652
+ threadId,
1653
+ resourceId
1654
+ }
1645
1655
  }
1646
- }
1647
- });
1656
+ }));
1657
+ synthetic = {
1658
+ type: "tool-result",
1659
+ runId,
1660
+ from: "AGENT",
1661
+ payload: {
1662
+ toolCallId: toolCall2.toolCallId,
1663
+ toolName: toolCall2.toolName,
1664
+ result,
1665
+ isError: false,
1666
+ providerExecuted: false
1667
+ }
1668
+ };
1669
+ } catch (error) {
1670
+ synthetic = {
1671
+ type: "tool-error",
1672
+ runId,
1673
+ from: "AGENT",
1674
+ payload: {
1675
+ toolCallId: toolCall2.toolCallId,
1676
+ toolName: toolCall2.toolName,
1677
+ error,
1678
+ args: toolCall2?.args,
1679
+ providerExecuted: false
1680
+ }
1681
+ };
1682
+ result = { error: error instanceof Error ? error.message : String(error) };
1683
+ }
1684
+ try {
1685
+ await pipePromise;
1686
+ } catch {
1687
+ }
1688
+ try {
1689
+ const errorForSerialization = synthetic.type === "tool-error" ? synthetic.payload.error : void 0;
1690
+ const serializedError = errorForSerialization instanceof Error ? {
1691
+ name: errorForSerialization.name,
1692
+ message: errorForSerialization.message,
1693
+ stack: errorForSerialization.stack
1694
+ } : errorForSerialization;
1695
+ const payloadForWire = synthetic.type === "tool-error" ? { ...synthetic, payload: { ...synthetic.payload, error: serializedError } } : synthetic;
1696
+ const sseLine = `data: ${JSON.stringify(payloadForWire)}
1697
+
1698
+ `;
1699
+ controller.enqueue(new TextEncoder().encode(sseLine));
1700
+ } catch (enqueueErr) {
1701
+ console.error("Failed to enqueue synthetic tool-result chunk:", enqueueErr);
1702
+ }
1648
1703
  const lastMessageRaw = messages[messages.length - 1];
1649
1704
  const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
1650
1705
  const toolInvocationPart = lastMessage?.parts?.find(
@@ -1694,6 +1749,11 @@ var Agent = class extends BaseResource {
1694
1749
  controller.close();
1695
1750
  }
1696
1751
  },
1752
+ onStreamChunk: (chunk) => {
1753
+ if (!streamRunId && typeof chunk.runId === "string") {
1754
+ streamRunId = chunk.runId;
1755
+ }
1756
+ },
1697
1757
  lastMessage: void 0
1698
1758
  }).catch(async (error) => {
1699
1759
  console.error("Error processing stream response:", error);