@mastra/client-js 0.10.15-alpha.1 → 0.10.15-alpha.2

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,7 +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
+ var isVercelTool = require('@mastra/core/tools/is-vercel-tool');
9
9
  var uuid = require('@lukeed/uuid');
10
10
  var runtimeContext = require('@mastra/core/runtime-context');
11
11
 
@@ -210,7 +210,7 @@ function processClientTools(clientTools) {
210
210
  }
211
211
  return Object.fromEntries(
212
212
  Object.entries(clientTools).map(([key, value]) => {
213
- if (tools.isVercelTool(value)) {
213
+ if (isVercelTool.isVercelTool(value)) {
214
214
  return [
215
215
  key,
216
216
  {
@@ -253,7 +253,7 @@ var BaseResource = class {
253
253
  const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
254
254
  ...options,
255
255
  headers: {
256
- ...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
256
+ ...options.body && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
257
257
  ...headers,
258
258
  ...options.headers
259
259
  // TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
@@ -378,10 +378,13 @@ var Agent = class extends BaseResource {
378
378
  clientTools: processClientTools(params.clientTools)
379
379
  };
380
380
  const { runId, resourceId, threadId, runtimeContext } = processedParams;
381
- const response = await this.request(`/api/agents/${this.agentId}/generate`, {
382
- method: "POST",
383
- body: processedParams
384
- });
381
+ const response = await this.request(
382
+ `/api/agents/${this.agentId}/generate`,
383
+ {
384
+ method: "POST",
385
+ body: processedParams
386
+ }
387
+ );
385
388
  if (response.finishReason === "tool-calls") {
386
389
  const toolCalls = response.toolCalls;
387
390
  if (!toolCalls || !Array.isArray(toolCalls)) {
@@ -430,8 +433,7 @@ var Agent = class extends BaseResource {
430
433
  onToolCall,
431
434
  onFinish,
432
435
  getCurrentDate = () => /* @__PURE__ */ new Date(),
433
- lastMessage,
434
- streamProtocol
436
+ lastMessage
435
437
  }) {
436
438
  const replaceLastMessage = lastMessage?.role === "assistant";
437
439
  let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
@@ -492,213 +494,228 @@ var Agent = class extends BaseResource {
492
494
  replaceLastMessage
493
495
  });
494
496
  }
495
- if (streamProtocol === "text") {
496
- await uiUtils.processTextStream({
497
- stream,
498
- onTextPart(value) {
499
- message.content += value;
500
- execUpdate();
497
+ await uiUtils.processDataStream({
498
+ stream,
499
+ onTextPart(value) {
500
+ if (currentTextPart == null) {
501
+ currentTextPart = {
502
+ type: "text",
503
+ text: value
504
+ };
505
+ message.parts.push(currentTextPart);
506
+ } else {
507
+ currentTextPart.text += value;
501
508
  }
502
- });
503
- onFinish?.({ message, finishReason, usage });
504
- } else {
505
- await uiUtils.processDataStream({
506
- stream,
507
- onTextPart(value) {
508
- if (currentTextPart == null) {
509
- currentTextPart = {
510
- type: "text",
511
- text: value
512
- };
513
- message.parts.push(currentTextPart);
514
- } else {
515
- currentTextPart.text += value;
516
- }
517
- message.content += value;
518
- execUpdate();
519
- },
520
- onReasoningPart(value) {
521
- if (currentReasoningTextDetail == null) {
522
- currentReasoningTextDetail = { type: "text", text: value };
523
- if (currentReasoningPart != null) {
524
- currentReasoningPart.details.push(currentReasoningTextDetail);
525
- }
526
- } else {
527
- currentReasoningTextDetail.text += value;
528
- }
529
- if (currentReasoningPart == null) {
530
- currentReasoningPart = {
531
- type: "reasoning",
532
- reasoning: value,
533
- details: [currentReasoningTextDetail]
534
- };
535
- message.parts.push(currentReasoningPart);
536
- } else {
537
- currentReasoningPart.reasoning += value;
538
- }
539
- message.reasoning = (message.reasoning ?? "") + value;
540
- execUpdate();
541
- },
542
- onReasoningSignaturePart(value) {
543
- if (currentReasoningTextDetail != null) {
544
- currentReasoningTextDetail.signature = value.signature;
545
- }
546
- },
547
- onRedactedReasoningPart(value) {
548
- if (currentReasoningPart == null) {
549
- currentReasoningPart = {
550
- type: "reasoning",
551
- reasoning: "",
552
- details: []
553
- };
554
- message.parts.push(currentReasoningPart);
509
+ message.content += value;
510
+ execUpdate();
511
+ },
512
+ onReasoningPart(value) {
513
+ if (currentReasoningTextDetail == null) {
514
+ currentReasoningTextDetail = { type: "text", text: value };
515
+ if (currentReasoningPart != null) {
516
+ currentReasoningPart.details.push(currentReasoningTextDetail);
555
517
  }
556
- currentReasoningPart.details.push({
557
- type: "redacted",
558
- data: value.data
559
- });
560
- currentReasoningTextDetail = void 0;
561
- execUpdate();
562
- },
563
- onFilePart(value) {
564
- message.parts.push({
565
- type: "file",
566
- mimeType: value.mimeType,
567
- data: value.data
568
- });
569
- execUpdate();
570
- },
571
- onSourcePart(value) {
572
- message.parts.push({
573
- type: "source",
574
- source: value
575
- });
576
- execUpdate();
577
- },
578
- onToolCallStreamingStartPart(value) {
518
+ } else {
519
+ currentReasoningTextDetail.text += value;
520
+ }
521
+ if (currentReasoningPart == null) {
522
+ currentReasoningPart = {
523
+ type: "reasoning",
524
+ reasoning: value,
525
+ details: [currentReasoningTextDetail]
526
+ };
527
+ message.parts.push(currentReasoningPart);
528
+ } else {
529
+ currentReasoningPart.reasoning += value;
530
+ }
531
+ message.reasoning = (message.reasoning ?? "") + value;
532
+ execUpdate();
533
+ },
534
+ onReasoningSignaturePart(value) {
535
+ if (currentReasoningTextDetail != null) {
536
+ currentReasoningTextDetail.signature = value.signature;
537
+ }
538
+ },
539
+ onRedactedReasoningPart(value) {
540
+ if (currentReasoningPart == null) {
541
+ currentReasoningPart = {
542
+ type: "reasoning",
543
+ reasoning: "",
544
+ details: []
545
+ };
546
+ message.parts.push(currentReasoningPart);
547
+ }
548
+ currentReasoningPart.details.push({
549
+ type: "redacted",
550
+ data: value.data
551
+ });
552
+ currentReasoningTextDetail = void 0;
553
+ execUpdate();
554
+ },
555
+ onFilePart(value) {
556
+ message.parts.push({
557
+ type: "file",
558
+ mimeType: value.mimeType,
559
+ data: value.data
560
+ });
561
+ execUpdate();
562
+ },
563
+ onSourcePart(value) {
564
+ message.parts.push({
565
+ type: "source",
566
+ source: value
567
+ });
568
+ execUpdate();
569
+ },
570
+ onToolCallStreamingStartPart(value) {
571
+ if (message.toolInvocations == null) {
572
+ message.toolInvocations = [];
573
+ }
574
+ partialToolCalls[value.toolCallId] = {
575
+ text: "",
576
+ step,
577
+ toolName: value.toolName,
578
+ index: message.toolInvocations.length
579
+ };
580
+ const invocation = {
581
+ state: "partial-call",
582
+ step,
583
+ toolCallId: value.toolCallId,
584
+ toolName: value.toolName,
585
+ args: void 0
586
+ };
587
+ message.toolInvocations.push(invocation);
588
+ updateToolInvocationPart(value.toolCallId, invocation);
589
+ execUpdate();
590
+ },
591
+ onToolCallDeltaPart(value) {
592
+ const partialToolCall = partialToolCalls[value.toolCallId];
593
+ partialToolCall.text += value.argsTextDelta;
594
+ const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
595
+ const invocation = {
596
+ state: "partial-call",
597
+ step: partialToolCall.step,
598
+ toolCallId: value.toolCallId,
599
+ toolName: partialToolCall.toolName,
600
+ args: partialArgs
601
+ };
602
+ message.toolInvocations[partialToolCall.index] = invocation;
603
+ updateToolInvocationPart(value.toolCallId, invocation);
604
+ execUpdate();
605
+ },
606
+ async onToolCallPart(value) {
607
+ const invocation = {
608
+ state: "call",
609
+ step,
610
+ ...value
611
+ };
612
+ if (partialToolCalls[value.toolCallId] != null) {
613
+ message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
614
+ } else {
579
615
  if (message.toolInvocations == null) {
580
616
  message.toolInvocations = [];
581
617
  }
582
- partialToolCalls[value.toolCallId] = {
583
- text: "",
584
- step,
585
- toolName: value.toolName,
586
- index: message.toolInvocations.length
587
- };
588
- const invocation = {
589
- state: "partial-call",
590
- step,
591
- toolCallId: value.toolCallId,
592
- toolName: value.toolName,
593
- args: void 0
594
- };
595
618
  message.toolInvocations.push(invocation);
596
- updateToolInvocationPart(value.toolCallId, invocation);
597
- execUpdate();
598
- },
599
- onToolCallDeltaPart(value) {
600
- const partialToolCall = partialToolCalls[value.toolCallId];
601
- partialToolCall.text += value.argsTextDelta;
602
- const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
603
- const invocation = {
604
- state: "partial-call",
605
- step: partialToolCall.step,
606
- toolCallId: value.toolCallId,
607
- toolName: partialToolCall.toolName,
608
- args: partialArgs
609
- };
610
- message.toolInvocations[partialToolCall.index] = invocation;
611
- updateToolInvocationPart(value.toolCallId, invocation);
612
- execUpdate();
613
- },
614
- async onToolCallPart(value) {
615
- const invocation = {
616
- state: "call",
617
- step,
618
- ...value
619
- };
620
- if (partialToolCalls[value.toolCallId] != null) {
621
- message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
622
- } else {
623
- if (message.toolInvocations == null) {
624
- message.toolInvocations = [];
625
- }
626
- message.toolInvocations.push(invocation);
627
- }
628
- updateToolInvocationPart(value.toolCallId, invocation);
629
- execUpdate();
630
- if (onToolCall) {
631
- const result = await onToolCall({ toolCall: value });
632
- if (result != null) {
633
- const invocation2 = {
634
- state: "result",
635
- step,
636
- ...value,
637
- result
638
- };
639
- message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
640
- updateToolInvocationPart(value.toolCallId, invocation2);
641
- execUpdate();
642
- }
643
- }
644
- },
645
- onToolResultPart(value) {
646
- const toolInvocations = message.toolInvocations;
647
- if (toolInvocations == null) {
648
- throw new Error("tool_result must be preceded by a tool_call");
649
- }
650
- const toolInvocationIndex = toolInvocations.findIndex(
651
- (invocation2) => invocation2.toolCallId === value.toolCallId
652
- );
653
- if (toolInvocationIndex === -1) {
654
- throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
655
- }
656
- const invocation = {
657
- ...toolInvocations[toolInvocationIndex],
658
- state: "result",
659
- ...value
660
- };
661
- toolInvocations[toolInvocationIndex] = invocation;
662
- updateToolInvocationPart(value.toolCallId, invocation);
663
- execUpdate();
664
- },
665
- onDataPart(value) {
666
- data.push(...value);
667
- execUpdate();
668
- },
669
- onMessageAnnotationsPart(value) {
670
- if (messageAnnotations == null) {
671
- messageAnnotations = [...value];
672
- } else {
673
- messageAnnotations.push(...value);
674
- }
675
- execUpdate();
676
- },
677
- onFinishStepPart(value) {
678
- step += 1;
679
- currentTextPart = value.isContinued ? currentTextPart : void 0;
680
- currentReasoningPart = void 0;
681
- currentReasoningTextDetail = void 0;
682
- },
683
- onStartStepPart(value) {
684
- if (!replaceLastMessage) {
685
- message.id = value.messageId;
686
- }
687
- message.parts.push({ type: "step-start" });
688
- execUpdate();
689
- },
690
- onFinishMessagePart(value) {
691
- finishReason = value.finishReason;
692
- if (value.usage != null) {
693
- usage = value.usage;
619
+ }
620
+ updateToolInvocationPart(value.toolCallId, invocation);
621
+ execUpdate();
622
+ if (onToolCall) {
623
+ const result = await onToolCall({ toolCall: value });
624
+ if (result != null) {
625
+ const invocation2 = {
626
+ state: "result",
627
+ step,
628
+ ...value,
629
+ result
630
+ };
631
+ message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
632
+ updateToolInvocationPart(value.toolCallId, invocation2);
633
+ execUpdate();
694
634
  }
695
- },
696
- onErrorPart(error) {
697
- throw new Error(error);
698
635
  }
636
+ },
637
+ onToolResultPart(value) {
638
+ const toolInvocations = message.toolInvocations;
639
+ if (toolInvocations == null) {
640
+ throw new Error("tool_result must be preceded by a tool_call");
641
+ }
642
+ const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
643
+ if (toolInvocationIndex === -1) {
644
+ throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
645
+ }
646
+ const invocation = {
647
+ ...toolInvocations[toolInvocationIndex],
648
+ state: "result",
649
+ ...value
650
+ };
651
+ toolInvocations[toolInvocationIndex] = invocation;
652
+ updateToolInvocationPart(value.toolCallId, invocation);
653
+ execUpdate();
654
+ },
655
+ onDataPart(value) {
656
+ data.push(...value);
657
+ execUpdate();
658
+ },
659
+ onMessageAnnotationsPart(value) {
660
+ if (messageAnnotations == null) {
661
+ messageAnnotations = [...value];
662
+ } else {
663
+ messageAnnotations.push(...value);
664
+ }
665
+ execUpdate();
666
+ },
667
+ onFinishStepPart(value) {
668
+ step += 1;
669
+ currentTextPart = value.isContinued ? currentTextPart : void 0;
670
+ currentReasoningPart = void 0;
671
+ currentReasoningTextDetail = void 0;
672
+ },
673
+ onStartStepPart(value) {
674
+ if (!replaceLastMessage) {
675
+ message.id = value.messageId;
676
+ }
677
+ message.parts.push({ type: "step-start" });
678
+ execUpdate();
679
+ },
680
+ onFinishMessagePart(value) {
681
+ finishReason = value.finishReason;
682
+ if (value.usage != null) {
683
+ usage = value.usage;
684
+ }
685
+ },
686
+ onErrorPart(error) {
687
+ throw new Error(error);
688
+ }
689
+ });
690
+ onFinish?.({ message, finishReason, usage });
691
+ }
692
+ /**
693
+ * Streams a response from the agent
694
+ * @param params - Stream parameters including prompt
695
+ * @returns Promise containing the enhanced Response object with processDataStream method
696
+ */
697
+ async stream(params) {
698
+ const processedParams = {
699
+ ...params,
700
+ output: params.output ? zodToJsonSchema(params.output) : void 0,
701
+ experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
702
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
703
+ clientTools: processClientTools(params.clientTools)
704
+ };
705
+ const { readable, writable } = new TransformStream();
706
+ const response = await this.processStreamResponse(processedParams, writable);
707
+ const streamResponse = new Response(readable, {
708
+ status: response.status,
709
+ statusText: response.statusText,
710
+ headers: response.headers
711
+ });
712
+ streamResponse.processDataStream = async (options = {}) => {
713
+ await uiUtils.processDataStream({
714
+ stream: streamResponse.body,
715
+ ...options
699
716
  });
700
- onFinish?.({ message, finishReason, usage });
701
- }
717
+ };
718
+ return streamResponse;
702
719
  }
703
720
  /**
704
721
  * Processes the stream response and handles tool calls
@@ -713,7 +730,6 @@ var Agent = class extends BaseResource {
713
730
  throw new Error("No response body");
714
731
  }
715
732
  try {
716
- const streamProtocol = processedParams.output ? "text" : "data";
717
733
  let toolCalls = [];
718
734
  let messages = [];
719
735
  const [streamForWritable, streamForProcessing] = response.body.tee();
@@ -793,55 +809,17 @@ var Agent = class extends BaseResource {
793
809
  }
794
810
  } else {
795
811
  setTimeout(() => {
796
- if (!writable.locked) {
797
- writable.close();
798
- }
812
+ writable.close();
799
813
  }, 0);
800
814
  }
801
815
  },
802
- lastMessage: void 0,
803
- streamProtocol
816
+ lastMessage: void 0
804
817
  });
805
818
  } catch (error) {
806
819
  console.error("Error processing stream response:", error);
807
820
  }
808
821
  return response;
809
822
  }
810
- /**
811
- * Streams a response from the agent
812
- * @param params - Stream parameters including prompt
813
- * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
814
- */
815
- async stream(params) {
816
- const processedParams = {
817
- ...params,
818
- output: params.output ? zodToJsonSchema(params.output) : void 0,
819
- experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
820
- runtimeContext: parseClientRuntimeContext(params.runtimeContext),
821
- clientTools: processClientTools(params.clientTools)
822
- };
823
- const { readable, writable } = new TransformStream();
824
- const response = await this.processStreamResponse(processedParams, writable);
825
- const streamResponse = new Response(readable, {
826
- status: response.status,
827
- statusText: response.statusText,
828
- headers: response.headers
829
- });
830
- streamResponse.processDataStream = async (options = {}) => {
831
- await uiUtils.processDataStream({
832
- stream: streamResponse.body,
833
- ...options
834
- });
835
- };
836
- streamResponse.processTextStream = async (options) => {
837
- await uiUtils.processTextStream({
838
- stream: streamResponse.body,
839
- onTextPart: options?.onTextPart ?? (() => {
840
- })
841
- });
842
- };
843
- return streamResponse;
844
- }
845
823
  /**
846
824
  * Gets details about a specific tool available to the agent
847
825
  * @param toolId - ID of the tool to retrieve
@@ -984,6 +962,21 @@ var MemoryThread = class extends BaseResource {
984
962
  });
985
963
  return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
986
964
  }
965
+ /**
966
+ * Retrieves paginated messages associated with the thread with advanced filtering and selection options
967
+ * @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
968
+ * @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
969
+ */
970
+ getMessagesPaginated({
971
+ selectBy,
972
+ ...rest
973
+ }) {
974
+ const query = new URLSearchParams({
975
+ ...rest,
976
+ ...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
977
+ });
978
+ return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
979
+ }
987
980
  };
988
981
 
989
982
  // src/resources/vector.ts
@@ -1405,6 +1398,14 @@ var Workflow = class extends BaseResource {
1405
1398
  method: "POST"
1406
1399
  });
1407
1400
  }
1401
+ /**
1402
+ * Creates a new workflow run (alias for createRun)
1403
+ * @param params - Optional object containing the optional runId
1404
+ * @returns Promise containing the runId of the created run
1405
+ */
1406
+ createRunAsync(params) {
1407
+ return this.createRun(params);
1408
+ }
1408
1409
  /**
1409
1410
  * Starts a workflow run synchronously without waiting for the workflow to complete
1410
1411
  * @param params - Object containing the runId, inputData and runtimeContext
@@ -2293,6 +2294,84 @@ var MastraClient = class extends BaseResource {
2293
2294
  }
2294
2295
  });
2295
2296
  }
2297
+ /**
2298
+ * Retrieves all available scorers
2299
+ * @returns Promise containing list of available scorers
2300
+ */
2301
+ getScorers() {
2302
+ return this.request("/api/scores/scorers");
2303
+ }
2304
+ /**
2305
+ * Retrieves a scorer by ID
2306
+ * @param scorerId - ID of the scorer to retrieve
2307
+ * @returns Promise containing the scorer
2308
+ */
2309
+ getScorer(scorerId) {
2310
+ return this.request(`/api/scores/scorers/${scorerId}`);
2311
+ }
2312
+ getScoresByScorerId(params) {
2313
+ const { page, perPage, scorerId, entityId, entityType } = params;
2314
+ const searchParams = new URLSearchParams();
2315
+ if (entityId) {
2316
+ searchParams.set("entityId", entityId);
2317
+ }
2318
+ if (entityType) {
2319
+ searchParams.set("entityType", entityType);
2320
+ }
2321
+ if (page !== void 0) {
2322
+ searchParams.set("page", String(page));
2323
+ }
2324
+ if (perPage !== void 0) {
2325
+ searchParams.set("perPage", String(perPage));
2326
+ }
2327
+ const queryString = searchParams.toString();
2328
+ return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
2329
+ }
2330
+ /**
2331
+ * Retrieves scores by run ID
2332
+ * @param params - Parameters containing run ID and pagination options
2333
+ * @returns Promise containing scores and pagination info
2334
+ */
2335
+ getScoresByRunId(params) {
2336
+ const { runId, page, perPage } = params;
2337
+ const searchParams = new URLSearchParams();
2338
+ if (page !== void 0) {
2339
+ searchParams.set("page", String(page));
2340
+ }
2341
+ if (perPage !== void 0) {
2342
+ searchParams.set("perPage", String(perPage));
2343
+ }
2344
+ const queryString = searchParams.toString();
2345
+ return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
2346
+ }
2347
+ /**
2348
+ * Retrieves scores by entity ID and type
2349
+ * @param params - Parameters containing entity ID, type, and pagination options
2350
+ * @returns Promise containing scores and pagination info
2351
+ */
2352
+ getScoresByEntityId(params) {
2353
+ const { entityId, entityType, page, perPage } = params;
2354
+ const searchParams = new URLSearchParams();
2355
+ if (page !== void 0) {
2356
+ searchParams.set("page", String(page));
2357
+ }
2358
+ if (perPage !== void 0) {
2359
+ searchParams.set("perPage", String(perPage));
2360
+ }
2361
+ const queryString = searchParams.toString();
2362
+ return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
2363
+ }
2364
+ /**
2365
+ * Saves a score
2366
+ * @param params - Parameters containing the score data to save
2367
+ * @returns Promise containing the saved score
2368
+ */
2369
+ saveScore(params) {
2370
+ return this.request("/api/scores", {
2371
+ method: "POST",
2372
+ body: params
2373
+ });
2374
+ }
2296
2375
  };
2297
2376
 
2298
2377
  exports.MastraClient = MastraClient;