@mastra/client-js 0.0.0-taofeeqInngest-20250603090617 → 0.0.0-tool-call-parts-20250630193309

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.cjs CHANGED
@@ -5,6 +5,7 @@ var rxjs = require('rxjs');
5
5
  var uiUtils = require('@ai-sdk/ui-utils');
6
6
  var zod = require('zod');
7
7
  var originalZodToJsonSchema = require('zod-to-json-schema');
8
+ var tools = require('@mastra/core/tools');
8
9
  var runtimeContext = require('@mastra/core/runtime-context');
9
10
 
10
11
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -202,6 +203,33 @@ function zodToJsonSchema(zodSchema) {
202
203
  }
203
204
  return originalZodToJsonSchema__default.default(zodSchema, { $refStrategy: "none" });
204
205
  }
206
+ function processClientTools(clientTools) {
207
+ if (!clientTools) {
208
+ return void 0;
209
+ }
210
+ return Object.fromEntries(
211
+ Object.entries(clientTools).map(([key, value]) => {
212
+ if (tools.isVercelTool(value)) {
213
+ return [
214
+ key,
215
+ {
216
+ ...value,
217
+ parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
218
+ }
219
+ ];
220
+ } else {
221
+ return [
222
+ key,
223
+ {
224
+ ...value,
225
+ inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
226
+ outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
227
+ }
228
+ ];
229
+ }
230
+ })
231
+ );
232
+ }
205
233
 
206
234
  // src/resources/base.ts
207
235
  var BaseResource = class {
@@ -224,6 +252,7 @@ var BaseResource = class {
224
252
  const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
225
253
  ...options,
226
254
  headers: {
255
+ ...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
227
256
  ...headers,
228
257
  ...options.headers
229
258
  // TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
@@ -270,8 +299,6 @@ function parseClientRuntimeContext(runtimeContext$1) {
270
299
  }
271
300
  return void 0;
272
301
  }
273
-
274
- // src/resources/agent.ts
275
302
  var AgentVoice = class extends BaseResource {
276
303
  constructor(options, agentId) {
277
304
  super(options);
@@ -340,22 +367,318 @@ var Agent = class extends BaseResource {
340
367
  details() {
341
368
  return this.request(`/api/agents/${this.agentId}`);
342
369
  }
343
- /**
344
- * Generates a response from the agent
345
- * @param params - Generation parameters including prompt
346
- * @returns Promise containing the generated response
347
- */
348
- generate(params) {
370
+ async generate(params) {
349
371
  const processedParams = {
350
372
  ...params,
351
373
  output: params.output ? zodToJsonSchema(params.output) : void 0,
352
374
  experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
353
- runtimeContext: parseClientRuntimeContext(params.runtimeContext)
375
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
376
+ clientTools: processClientTools(params.clientTools)
354
377
  };
355
- return this.request(`/api/agents/${this.agentId}/generate`, {
378
+ const { runId, resourceId, threadId, runtimeContext } = processedParams;
379
+ const response = await this.request(`/api/agents/${this.agentId}/generate`, {
356
380
  method: "POST",
357
381
  body: processedParams
358
382
  });
383
+ if (response.finishReason === "tool-calls") {
384
+ for (const toolCall of response.toolCalls) {
385
+ const clientTool = params.clientTools?.[toolCall.toolName];
386
+ if (clientTool && clientTool.execute) {
387
+ const result = await clientTool.execute(
388
+ { context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
389
+ {
390
+ messages: response.messages,
391
+ toolCallId: toolCall?.toolCallId
392
+ }
393
+ );
394
+ const updatedMessages = [
395
+ {
396
+ role: "user",
397
+ content: params.messages
398
+ },
399
+ ...response.response.messages,
400
+ {
401
+ role: "tool",
402
+ content: [
403
+ {
404
+ type: "tool-result",
405
+ toolCallId: toolCall.toolCallId,
406
+ toolName: toolCall.toolName,
407
+ result
408
+ }
409
+ ]
410
+ }
411
+ ];
412
+ return this.generate({
413
+ ...params,
414
+ messages: updatedMessages
415
+ });
416
+ }
417
+ }
418
+ }
419
+ return response;
420
+ }
421
+ async processChatResponse({
422
+ stream,
423
+ update,
424
+ onToolCall,
425
+ onFinish,
426
+ getCurrentDate = () => /* @__PURE__ */ new Date(),
427
+ lastMessage
428
+ }) {
429
+ const replaceLastMessage = lastMessage?.role === "assistant";
430
+ let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
431
+ (lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
432
+ return Math.max(max, toolInvocation.step ?? 0);
433
+ }, 0) ?? 0) : 0;
434
+ const message = replaceLastMessage ? structuredClone(lastMessage) : {
435
+ id: crypto.randomUUID(),
436
+ createdAt: getCurrentDate(),
437
+ role: "assistant",
438
+ content: "",
439
+ parts: []
440
+ };
441
+ let currentTextPart = void 0;
442
+ let currentReasoningPart = void 0;
443
+ let currentReasoningTextDetail = void 0;
444
+ function updateToolInvocationPart(toolCallId, invocation) {
445
+ const part = message.parts.find(
446
+ (part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
447
+ );
448
+ if (part != null) {
449
+ part.toolInvocation = invocation;
450
+ } else {
451
+ message.parts.push({
452
+ type: "tool-invocation",
453
+ toolInvocation: invocation
454
+ });
455
+ }
456
+ }
457
+ const data = [];
458
+ let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
459
+ const partialToolCalls = {};
460
+ let usage = {
461
+ completionTokens: NaN,
462
+ promptTokens: NaN,
463
+ totalTokens: NaN
464
+ };
465
+ let finishReason = "unknown";
466
+ function execUpdate() {
467
+ const copiedData = [...data];
468
+ if (messageAnnotations?.length) {
469
+ message.annotations = messageAnnotations;
470
+ }
471
+ const copiedMessage = {
472
+ // deep copy the message to ensure that deep changes (msg attachments) are updated
473
+ // with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
474
+ ...structuredClone(message),
475
+ // add a revision id to ensure that the message is updated with SWR. SWR uses a
476
+ // hashing approach by default to detect changes, but it only works for shallow
477
+ // changes. This is why we need to add a revision id to ensure that the message
478
+ // is updated with SWR (without it, the changes get stuck in SWR and are not
479
+ // forwarded to rendering):
480
+ revisionId: crypto.randomUUID()
481
+ };
482
+ update({
483
+ message: copiedMessage,
484
+ data: copiedData,
485
+ replaceLastMessage
486
+ });
487
+ }
488
+ await uiUtils.processDataStream({
489
+ stream,
490
+ onTextPart(value) {
491
+ if (currentTextPart == null) {
492
+ currentTextPart = {
493
+ type: "text",
494
+ text: value
495
+ };
496
+ message.parts.push(currentTextPart);
497
+ } else {
498
+ currentTextPart.text += value;
499
+ }
500
+ message.content += value;
501
+ execUpdate();
502
+ },
503
+ onReasoningPart(value) {
504
+ if (currentReasoningTextDetail == null) {
505
+ currentReasoningTextDetail = { type: "text", text: value };
506
+ if (currentReasoningPart != null) {
507
+ currentReasoningPart.details.push(currentReasoningTextDetail);
508
+ }
509
+ } else {
510
+ currentReasoningTextDetail.text += value;
511
+ }
512
+ if (currentReasoningPart == null) {
513
+ currentReasoningPart = {
514
+ type: "reasoning",
515
+ reasoning: value,
516
+ details: [currentReasoningTextDetail]
517
+ };
518
+ message.parts.push(currentReasoningPart);
519
+ } else {
520
+ currentReasoningPart.reasoning += value;
521
+ }
522
+ message.reasoning = (message.reasoning ?? "") + value;
523
+ execUpdate();
524
+ },
525
+ onReasoningSignaturePart(value) {
526
+ if (currentReasoningTextDetail != null) {
527
+ currentReasoningTextDetail.signature = value.signature;
528
+ }
529
+ },
530
+ onRedactedReasoningPart(value) {
531
+ if (currentReasoningPart == null) {
532
+ currentReasoningPart = {
533
+ type: "reasoning",
534
+ reasoning: "",
535
+ details: []
536
+ };
537
+ message.parts.push(currentReasoningPart);
538
+ }
539
+ currentReasoningPart.details.push({
540
+ type: "redacted",
541
+ data: value.data
542
+ });
543
+ currentReasoningTextDetail = void 0;
544
+ execUpdate();
545
+ },
546
+ onFilePart(value) {
547
+ message.parts.push({
548
+ type: "file",
549
+ mimeType: value.mimeType,
550
+ data: value.data
551
+ });
552
+ execUpdate();
553
+ },
554
+ onSourcePart(value) {
555
+ message.parts.push({
556
+ type: "source",
557
+ source: value
558
+ });
559
+ execUpdate();
560
+ },
561
+ onToolCallStreamingStartPart(value) {
562
+ if (message.toolInvocations == null) {
563
+ message.toolInvocations = [];
564
+ }
565
+ partialToolCalls[value.toolCallId] = {
566
+ text: "",
567
+ step,
568
+ toolName: value.toolName,
569
+ index: message.toolInvocations.length
570
+ };
571
+ const invocation = {
572
+ state: "partial-call",
573
+ step,
574
+ toolCallId: value.toolCallId,
575
+ toolName: value.toolName,
576
+ args: void 0
577
+ };
578
+ message.toolInvocations.push(invocation);
579
+ updateToolInvocationPart(value.toolCallId, invocation);
580
+ execUpdate();
581
+ },
582
+ onToolCallDeltaPart(value) {
583
+ const partialToolCall = partialToolCalls[value.toolCallId];
584
+ partialToolCall.text += value.argsTextDelta;
585
+ const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
586
+ const invocation = {
587
+ state: "partial-call",
588
+ step: partialToolCall.step,
589
+ toolCallId: value.toolCallId,
590
+ toolName: partialToolCall.toolName,
591
+ args: partialArgs
592
+ };
593
+ message.toolInvocations[partialToolCall.index] = invocation;
594
+ updateToolInvocationPart(value.toolCallId, invocation);
595
+ execUpdate();
596
+ },
597
+ async onToolCallPart(value) {
598
+ const invocation = {
599
+ state: "call",
600
+ step,
601
+ ...value
602
+ };
603
+ if (partialToolCalls[value.toolCallId] != null) {
604
+ message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
605
+ } else {
606
+ if (message.toolInvocations == null) {
607
+ message.toolInvocations = [];
608
+ }
609
+ message.toolInvocations.push(invocation);
610
+ }
611
+ updateToolInvocationPart(value.toolCallId, invocation);
612
+ execUpdate();
613
+ if (onToolCall) {
614
+ const result = await onToolCall({ toolCall: value });
615
+ if (result != null) {
616
+ const invocation2 = {
617
+ state: "result",
618
+ step,
619
+ ...value,
620
+ result
621
+ };
622
+ message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
623
+ updateToolInvocationPart(value.toolCallId, invocation2);
624
+ execUpdate();
625
+ }
626
+ }
627
+ },
628
+ onToolResultPart(value) {
629
+ const toolInvocations = message.toolInvocations;
630
+ if (toolInvocations == null) {
631
+ throw new Error("tool_result must be preceded by a tool_call");
632
+ }
633
+ const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
634
+ if (toolInvocationIndex === -1) {
635
+ throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
636
+ }
637
+ const invocation = {
638
+ ...toolInvocations[toolInvocationIndex],
639
+ state: "result",
640
+ ...value
641
+ };
642
+ toolInvocations[toolInvocationIndex] = invocation;
643
+ updateToolInvocationPart(value.toolCallId, invocation);
644
+ execUpdate();
645
+ },
646
+ onDataPart(value) {
647
+ data.push(...value);
648
+ execUpdate();
649
+ },
650
+ onMessageAnnotationsPart(value) {
651
+ if (messageAnnotations == null) {
652
+ messageAnnotations = [...value];
653
+ } else {
654
+ messageAnnotations.push(...value);
655
+ }
656
+ execUpdate();
657
+ },
658
+ onFinishStepPart(value) {
659
+ step += 1;
660
+ currentTextPart = value.isContinued ? currentTextPart : void 0;
661
+ currentReasoningPart = void 0;
662
+ currentReasoningTextDetail = void 0;
663
+ },
664
+ onStartStepPart(value) {
665
+ if (!replaceLastMessage) {
666
+ message.id = value.messageId;
667
+ }
668
+ message.parts.push({ type: "step-start" });
669
+ execUpdate();
670
+ },
671
+ onFinishMessagePart(value) {
672
+ finishReason = value.finishReason;
673
+ if (value.usage != null) {
674
+ usage = value.usage;
675
+ }
676
+ },
677
+ onErrorPart(error) {
678
+ throw new Error(error);
679
+ }
680
+ });
681
+ onFinish?.({ message, finishReason, usage });
359
682
  }
360
683
  /**
361
684
  * Streams a response from the agent
@@ -367,8 +690,28 @@ var Agent = class extends BaseResource {
367
690
  ...params,
368
691
  output: params.output ? zodToJsonSchema(params.output) : void 0,
369
692
  experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
370
- runtimeContext: parseClientRuntimeContext(params.runtimeContext)
693
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
694
+ clientTools: processClientTools(params.clientTools)
371
695
  };
696
+ const { readable, writable } = new TransformStream();
697
+ const response = await this.processStreamResponse(processedParams, writable);
698
+ const streamResponse = new Response(readable, {
699
+ status: response.status,
700
+ statusText: response.statusText,
701
+ headers: response.headers
702
+ });
703
+ streamResponse.processDataStream = async (options = {}) => {
704
+ await uiUtils.processDataStream({
705
+ stream: streamResponse.body,
706
+ ...options
707
+ });
708
+ };
709
+ return streamResponse;
710
+ }
711
+ /**
712
+ * Processes the stream response and handles tool calls
713
+ */
714
+ async processStreamResponse(processedParams, writable) {
372
715
  const response = await this.request(`/api/agents/${this.agentId}/stream`, {
373
716
  method: "POST",
374
717
  body: processedParams,
@@ -377,12 +720,82 @@ var Agent = class extends BaseResource {
377
720
  if (!response.body) {
378
721
  throw new Error("No response body");
379
722
  }
380
- response.processDataStream = async (options = {}) => {
381
- await uiUtils.processDataStream({
382
- stream: response.body,
383
- ...options
723
+ try {
724
+ let toolCalls = [];
725
+ let messages = [];
726
+ const [streamForWritable, streamForProcessing] = response.body.tee();
727
+ streamForWritable.pipeTo(writable, {
728
+ preventClose: true
729
+ }).catch((error) => {
730
+ console.error("Error piping to writable stream:", error);
384
731
  });
385
- };
732
+ this.processChatResponse({
733
+ stream: streamForProcessing,
734
+ update: ({ message }) => {
735
+ messages.push(message);
736
+ },
737
+ onFinish: async ({ finishReason, message }) => {
738
+ if (finishReason === "tool-calls") {
739
+ const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
740
+ if (toolCall) {
741
+ toolCalls.push(toolCall);
742
+ }
743
+ for (const toolCall2 of toolCalls) {
744
+ const clientTool = processedParams.clientTools?.[toolCall2.toolName];
745
+ if (clientTool && clientTool.execute) {
746
+ const result = await clientTool.execute(
747
+ {
748
+ context: toolCall2?.args,
749
+ runId: processedParams.runId,
750
+ resourceId: processedParams.resourceId,
751
+ threadId: processedParams.threadId,
752
+ runtimeContext: processedParams.runtimeContext
753
+ },
754
+ {
755
+ messages: response.messages,
756
+ toolCallId: toolCall2?.toolCallId
757
+ }
758
+ );
759
+ const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
760
+ const toolInvocationPart = lastMessage?.parts?.find(
761
+ (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
762
+ );
763
+ if (toolInvocationPart) {
764
+ toolInvocationPart.toolInvocation = {
765
+ ...toolInvocationPart.toolInvocation,
766
+ state: "result",
767
+ result
768
+ };
769
+ }
770
+ const toolInvocation = lastMessage?.toolInvocations?.find(
771
+ (toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
772
+ );
773
+ if (toolInvocation) {
774
+ toolInvocation.state = "result";
775
+ toolInvocation.result = result;
776
+ }
777
+ const originalMessages = processedParams.messages;
778
+ const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
779
+ this.processStreamResponse(
780
+ {
781
+ ...processedParams,
782
+ messages: [...messageArray, ...messages, lastMessage]
783
+ },
784
+ writable
785
+ );
786
+ }
787
+ }
788
+ } else {
789
+ setTimeout(() => {
790
+ writable.close();
791
+ }, 0);
792
+ }
793
+ },
794
+ lastMessage: void 0
795
+ });
796
+ } catch (error) {
797
+ console.error("Error processing stream response:", error);
798
+ }
386
799
  return response;
387
800
  }
388
801
  /**
@@ -777,7 +1190,7 @@ var LegacyWorkflow = class extends BaseResource {
777
1190
  };
778
1191
 
779
1192
  // src/resources/tool.ts
780
- var Tool = class extends BaseResource {
1193
+ var Tool2 = class extends BaseResource {
781
1194
  constructor(options, toolId) {
782
1195
  super(options);
783
1196
  this.toolId = toolId;
@@ -897,6 +1310,22 @@ var Workflow = class extends BaseResource {
897
1310
  return this.request(`/api/workflows/${this.workflowId}/runs`);
898
1311
  }
899
1312
  }
1313
+ /**
1314
+ * Retrieves a specific workflow run by its ID
1315
+ * @param runId - The ID of the workflow run to retrieve
1316
+ * @returns Promise containing the workflow run details
1317
+ */
1318
+ runById(runId) {
1319
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
1320
+ }
1321
+ /**
1322
+ * Retrieves the execution result for a specific workflow run by its ID
1323
+ * @param runId - The ID of the workflow run to retrieve the execution result for
1324
+ * @returns Promise containing the workflow run execution result
1325
+ */
1326
+ runExecutionResult(runId) {
1327
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
1328
+ }
900
1329
  /**
901
1330
  * Creates a new workflow run
902
1331
  * @param params - Optional object containing the optional runId
@@ -962,16 +1391,16 @@ var Workflow = class extends BaseResource {
962
1391
  });
963
1392
  }
964
1393
  /**
965
- * Starts a vNext workflow run and returns a stream
1394
+ * Starts a workflow run and returns a stream
966
1395
  * @param params - Object containing the optional runId, inputData and runtimeContext
967
- * @returns Promise containing the vNext workflow execution results
1396
+ * @returns Promise containing the workflow execution results
968
1397
  */
969
1398
  async stream(params) {
970
1399
  const searchParams = new URLSearchParams();
971
1400
  if (!!params?.runId) {
972
1401
  searchParams.set("runId", params.runId);
973
1402
  }
974
- const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0;
1403
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
975
1404
  const response = await this.request(
976
1405
  `/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
977
1406
  {
@@ -1180,6 +1609,180 @@ var MCPTool = class extends BaseResource {
1180
1609
  }
1181
1610
  };
1182
1611
 
1612
+ // src/resources/vNextNetwork.ts
1613
+ var RECORD_SEPARATOR3 = "";
1614
+ var VNextNetwork = class extends BaseResource {
1615
+ constructor(options, networkId) {
1616
+ super(options);
1617
+ this.networkId = networkId;
1618
+ }
1619
+ /**
1620
+ * Retrieves details about the network
1621
+ * @returns Promise containing vNext network details
1622
+ */
1623
+ details() {
1624
+ return this.request(`/api/networks/v-next/${this.networkId}`);
1625
+ }
1626
+ /**
1627
+ * Generates a response from the v-next network
1628
+ * @param params - Generation parameters including message
1629
+ * @returns Promise containing the generated response
1630
+ */
1631
+ generate(params) {
1632
+ return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
1633
+ method: "POST",
1634
+ body: params
1635
+ });
1636
+ }
1637
+ /**
1638
+ * Generates a response from the v-next network using multiple primitives
1639
+ * @param params - Generation parameters including message
1640
+ * @returns Promise containing the generated response
1641
+ */
1642
+ loop(params) {
1643
+ return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
1644
+ method: "POST",
1645
+ body: params
1646
+ });
1647
+ }
1648
+ async *streamProcessor(stream) {
1649
+ const reader = stream.getReader();
1650
+ let doneReading = false;
1651
+ let buffer = "";
1652
+ try {
1653
+ while (!doneReading) {
1654
+ const { done, value } = await reader.read();
1655
+ doneReading = done;
1656
+ if (done && !value) continue;
1657
+ try {
1658
+ const decoded = value ? new TextDecoder().decode(value) : "";
1659
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
1660
+ buffer = chunks.pop() || "";
1661
+ for (const chunk of chunks) {
1662
+ if (chunk) {
1663
+ if (typeof chunk === "string") {
1664
+ try {
1665
+ const parsedChunk = JSON.parse(chunk);
1666
+ yield parsedChunk;
1667
+ } catch {
1668
+ }
1669
+ }
1670
+ }
1671
+ }
1672
+ } catch {
1673
+ }
1674
+ }
1675
+ if (buffer) {
1676
+ try {
1677
+ yield JSON.parse(buffer);
1678
+ } catch {
1679
+ }
1680
+ }
1681
+ } finally {
1682
+ reader.cancel().catch(() => {
1683
+ });
1684
+ }
1685
+ }
1686
+ /**
1687
+ * Streams a response from the v-next network
1688
+ * @param params - Stream parameters including message
1689
+ * @returns Promise containing the results
1690
+ */
1691
+ async stream(params, onRecord) {
1692
+ const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
1693
+ method: "POST",
1694
+ body: params,
1695
+ stream: true
1696
+ });
1697
+ if (!response.ok) {
1698
+ throw new Error(`Failed to stream vNext network: ${response.statusText}`);
1699
+ }
1700
+ if (!response.body) {
1701
+ throw new Error("Response body is null");
1702
+ }
1703
+ for await (const record of this.streamProcessor(response.body)) {
1704
+ if (typeof record === "string") {
1705
+ onRecord(JSON.parse(record));
1706
+ } else {
1707
+ onRecord(record);
1708
+ }
1709
+ }
1710
+ }
1711
+ /**
1712
+ * Streams a response from the v-next network loop
1713
+ * @param params - Stream parameters including message
1714
+ * @returns Promise containing the results
1715
+ */
1716
+ async loopStream(params, onRecord) {
1717
+ const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
1718
+ method: "POST",
1719
+ body: params,
1720
+ stream: true
1721
+ });
1722
+ if (!response.ok) {
1723
+ throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
1724
+ }
1725
+ if (!response.body) {
1726
+ throw new Error("Response body is null");
1727
+ }
1728
+ for await (const record of this.streamProcessor(response.body)) {
1729
+ if (typeof record === "string") {
1730
+ onRecord(JSON.parse(record));
1731
+ } else {
1732
+ onRecord(record);
1733
+ }
1734
+ }
1735
+ }
1736
+ };
1737
+
1738
+ // src/resources/network-memory-thread.ts
1739
+ var NetworkMemoryThread = class extends BaseResource {
1740
+ constructor(options, threadId, networkId) {
1741
+ super(options);
1742
+ this.threadId = threadId;
1743
+ this.networkId = networkId;
1744
+ }
1745
+ /**
1746
+ * Retrieves the memory thread details
1747
+ * @returns Promise containing thread details including title and metadata
1748
+ */
1749
+ get() {
1750
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1751
+ }
1752
+ /**
1753
+ * Updates the memory thread properties
1754
+ * @param params - Update parameters including title and metadata
1755
+ * @returns Promise containing updated thread details
1756
+ */
1757
+ update(params) {
1758
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1759
+ method: "PATCH",
1760
+ body: params
1761
+ });
1762
+ }
1763
+ /**
1764
+ * Deletes the memory thread
1765
+ * @returns Promise containing deletion result
1766
+ */
1767
+ delete() {
1768
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1769
+ method: "DELETE"
1770
+ });
1771
+ }
1772
+ /**
1773
+ * Retrieves messages associated with the thread
1774
+ * @param params - Optional parameters including limit for number of messages to retrieve
1775
+ * @returns Promise containing thread messages and UI messages
1776
+ */
1777
+ getMessages(params) {
1778
+ const query = new URLSearchParams({
1779
+ networkId: this.networkId,
1780
+ ...params?.limit ? { limit: params.limit.toString() } : {}
1781
+ });
1782
+ return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1783
+ }
1784
+ };
1785
+
1183
1786
  // src/client.ts
1184
1787
  var MastraClient = class extends BaseResource {
1185
1788
  constructor(options) {
@@ -1257,6 +1860,48 @@ var MastraClient = class extends BaseResource {
1257
1860
  getMemoryStatus(agentId) {
1258
1861
  return this.request(`/api/memory/status?agentId=${agentId}`);
1259
1862
  }
1863
+ /**
1864
+ * Retrieves memory threads for a resource
1865
+ * @param params - Parameters containing the resource ID
1866
+ * @returns Promise containing array of memory threads
1867
+ */
1868
+ getNetworkMemoryThreads(params) {
1869
+ return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
1870
+ }
1871
+ /**
1872
+ * Creates a new memory thread
1873
+ * @param params - Parameters for creating the memory thread
1874
+ * @returns Promise containing the created memory thread
1875
+ */
1876
+ createNetworkMemoryThread(params) {
1877
+ return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
1878
+ }
1879
+ /**
1880
+ * Gets a memory thread instance by ID
1881
+ * @param threadId - ID of the memory thread to retrieve
1882
+ * @returns MemoryThread instance
1883
+ */
1884
+ getNetworkMemoryThread(threadId, networkId) {
1885
+ return new NetworkMemoryThread(this.options, threadId, networkId);
1886
+ }
1887
+ /**
1888
+ * Saves messages to memory
1889
+ * @param params - Parameters containing messages to save
1890
+ * @returns Promise containing the saved messages
1891
+ */
1892
+ saveNetworkMessageToMemory(params) {
1893
+ return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
1894
+ method: "POST",
1895
+ body: params
1896
+ });
1897
+ }
1898
+ /**
1899
+ * Gets the status of the memory system
1900
+ * @returns Promise containing memory system status
1901
+ */
1902
+ getNetworkMemoryStatus(networkId) {
1903
+ return this.request(`/api/memory/network/status?networkId=${networkId}`);
1904
+ }
1260
1905
  /**
1261
1906
  * Retrieves all available tools
1262
1907
  * @returns Promise containing map of tool IDs to tool details
@@ -1270,7 +1915,7 @@ var MastraClient = class extends BaseResource {
1270
1915
  * @returns Tool instance
1271
1916
  */
1272
1917
  getTool(toolId) {
1273
- return new Tool(this.options, toolId);
1918
+ return new Tool2(this.options, toolId);
1274
1919
  }
1275
1920
  /**
1276
1921
  * Retrieves all available legacy workflows
@@ -1316,7 +1961,41 @@ var MastraClient = class extends BaseResource {
1316
1961
  * @returns Promise containing array of log messages
1317
1962
  */
1318
1963
  getLogs(params) {
1319
- return this.request(`/api/logs?transportId=${params.transportId}`);
1964
+ const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
1965
+ const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
1966
+ const searchParams = new URLSearchParams();
1967
+ if (transportId) {
1968
+ searchParams.set("transportId", transportId);
1969
+ }
1970
+ if (fromDate) {
1971
+ searchParams.set("fromDate", fromDate.toISOString());
1972
+ }
1973
+ if (toDate) {
1974
+ searchParams.set("toDate", toDate.toISOString());
1975
+ }
1976
+ if (logLevel) {
1977
+ searchParams.set("logLevel", logLevel);
1978
+ }
1979
+ if (page) {
1980
+ searchParams.set("page", String(page));
1981
+ }
1982
+ if (perPage) {
1983
+ searchParams.set("perPage", String(perPage));
1984
+ }
1985
+ if (_filters) {
1986
+ if (Array.isArray(_filters)) {
1987
+ for (const filter of _filters) {
1988
+ searchParams.append("filters", filter);
1989
+ }
1990
+ } else {
1991
+ searchParams.set("filters", _filters);
1992
+ }
1993
+ }
1994
+ if (searchParams.size) {
1995
+ return this.request(`/api/logs?${searchParams}`);
1996
+ } else {
1997
+ return this.request(`/api/logs`);
1998
+ }
1320
1999
  }
1321
2000
  /**
1322
2001
  * Gets logs for a specific run
@@ -1324,7 +2003,44 @@ var MastraClient = class extends BaseResource {
1324
2003
  * @returns Promise containing array of log messages
1325
2004
  */
1326
2005
  getLogForRun(params) {
1327
- return this.request(`/api/logs/${params.runId}?transportId=${params.transportId}`);
2006
+ const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
2007
+ const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
2008
+ const searchParams = new URLSearchParams();
2009
+ if (runId) {
2010
+ searchParams.set("runId", runId);
2011
+ }
2012
+ if (transportId) {
2013
+ searchParams.set("transportId", transportId);
2014
+ }
2015
+ if (fromDate) {
2016
+ searchParams.set("fromDate", fromDate.toISOString());
2017
+ }
2018
+ if (toDate) {
2019
+ searchParams.set("toDate", toDate.toISOString());
2020
+ }
2021
+ if (logLevel) {
2022
+ searchParams.set("logLevel", logLevel);
2023
+ }
2024
+ if (page) {
2025
+ searchParams.set("page", String(page));
2026
+ }
2027
+ if (perPage) {
2028
+ searchParams.set("perPage", String(perPage));
2029
+ }
2030
+ if (_filters) {
2031
+ if (Array.isArray(_filters)) {
2032
+ for (const filter of _filters) {
2033
+ searchParams.append("filters", filter);
2034
+ }
2035
+ } else {
2036
+ searchParams.set("filters", _filters);
2037
+ }
2038
+ }
2039
+ if (searchParams.size) {
2040
+ return this.request(`/api/logs/${runId}?${searchParams}`);
2041
+ } else {
2042
+ return this.request(`/api/logs/${runId}`);
2043
+ }
1328
2044
  }
1329
2045
  /**
1330
2046
  * List of all log transports
@@ -1382,6 +2098,13 @@ var MastraClient = class extends BaseResource {
1382
2098
  getNetworks() {
1383
2099
  return this.request("/api/networks");
1384
2100
  }
2101
+ /**
2102
+ * Retrieves all available vNext networks
2103
+ * @returns Promise containing map of vNext network IDs to vNext network details
2104
+ */
2105
+ getVNextNetworks() {
2106
+ return this.request("/api/networks/v-next");
2107
+ }
1385
2108
  /**
1386
2109
  * Gets a network instance by ID
1387
2110
  * @param networkId - ID of the network to retrieve
@@ -1390,6 +2113,14 @@ var MastraClient = class extends BaseResource {
1390
2113
  getNetwork(networkId) {
1391
2114
  return new Network(this.options, networkId);
1392
2115
  }
2116
+ /**
2117
+ * Gets a vNext network instance by ID
2118
+ * @param networkId - ID of the vNext network to retrieve
2119
+ * @returns vNext Network instance
2120
+ */
2121
+ getVNextNetwork(networkId) {
2122
+ return new VNextNetwork(this.options, networkId);
2123
+ }
1393
2124
  /**
1394
2125
  * Retrieves a list of available MCP servers.
1395
2126
  * @param params - Optional parameters for pagination (limit, offset).