@mastra/client-js 0.0.0-mastra-3171-llamaindex-pin-20250423175211 → 0.0.0-mastra-3338-mastra-memory-pinecone-20250507174328

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
@@ -1,8 +1,173 @@
1
+ import { AbstractAgent, EventType } from '@ag-ui/client';
2
+ import { Observable } from 'rxjs';
3
+ import { processDataStream } from '@ai-sdk/ui-utils';
1
4
  import { ZodSchema } from 'zod';
2
5
  import { zodToJsonSchema } from 'zod-to-json-schema';
3
- import { processDataStream } from '@ai-sdk/ui-utils';
4
6
 
5
- // src/resources/agent.ts
7
+ // src/adapters/agui.ts
8
+ var AGUIAdapter = class extends AbstractAgent {
9
+ agent;
10
+ resourceId;
11
+ constructor({ agent, agentId, resourceId, ...rest }) {
12
+ super({
13
+ agentId,
14
+ ...rest
15
+ });
16
+ this.agent = agent;
17
+ this.resourceId = resourceId;
18
+ }
19
+ run(input) {
20
+ return new Observable((subscriber) => {
21
+ const convertedMessages = convertMessagesToMastraMessages(input.messages);
22
+ subscriber.next({
23
+ type: EventType.RUN_STARTED,
24
+ threadId: input.threadId,
25
+ runId: input.runId
26
+ });
27
+ this.agent.stream({
28
+ threadId: input.threadId,
29
+ resourceId: this.resourceId ?? "",
30
+ runId: input.runId,
31
+ messages: convertedMessages,
32
+ clientTools: input.tools.reduce(
33
+ (acc, tool) => {
34
+ acc[tool.name] = {
35
+ id: tool.name,
36
+ description: tool.description,
37
+ inputSchema: tool.parameters
38
+ };
39
+ return acc;
40
+ },
41
+ {}
42
+ )
43
+ }).then((response) => {
44
+ let currentMessageId = void 0;
45
+ return response.processDataStream({
46
+ onTextPart: (text) => {
47
+ if (currentMessageId === void 0) {
48
+ currentMessageId = generateUUID();
49
+ const message2 = {
50
+ type: EventType.TEXT_MESSAGE_START,
51
+ messageId: currentMessageId,
52
+ role: "assistant"
53
+ };
54
+ subscriber.next(message2);
55
+ }
56
+ const message = {
57
+ type: EventType.TEXT_MESSAGE_CONTENT,
58
+ messageId: currentMessageId,
59
+ delta: text
60
+ };
61
+ subscriber.next(message);
62
+ },
63
+ onFinishMessagePart: (message) => {
64
+ console.log("onFinishMessagePart", message);
65
+ if (currentMessageId !== void 0) {
66
+ const message2 = {
67
+ type: EventType.TEXT_MESSAGE_END,
68
+ messageId: currentMessageId
69
+ };
70
+ subscriber.next(message2);
71
+ }
72
+ subscriber.next({
73
+ type: EventType.RUN_FINISHED,
74
+ threadId: input.threadId,
75
+ runId: input.runId
76
+ });
77
+ subscriber.complete();
78
+ },
79
+ onToolCallPart(streamPart) {
80
+ const parentMessageId = currentMessageId || generateUUID();
81
+ subscriber.next({
82
+ type: EventType.TOOL_CALL_START,
83
+ toolCallId: streamPart.toolCallId,
84
+ toolCallName: streamPart.toolName,
85
+ parentMessageId
86
+ });
87
+ subscriber.next({
88
+ type: EventType.TOOL_CALL_ARGS,
89
+ toolCallId: streamPart.toolCallId,
90
+ delta: JSON.stringify(streamPart.args),
91
+ parentMessageId
92
+ });
93
+ subscriber.next({
94
+ type: EventType.TOOL_CALL_END,
95
+ toolCallId: streamPart.toolCallId,
96
+ parentMessageId
97
+ });
98
+ }
99
+ });
100
+ }).catch((error) => {
101
+ console.log("error", error);
102
+ subscriber.error(error);
103
+ });
104
+ return () => {
105
+ };
106
+ });
107
+ }
108
+ };
109
+ function generateUUID() {
110
+ if (typeof crypto !== "undefined") {
111
+ if (typeof crypto.randomUUID === "function") {
112
+ return crypto.randomUUID();
113
+ }
114
+ if (typeof crypto.getRandomValues === "function") {
115
+ const buffer = new Uint8Array(16);
116
+ crypto.getRandomValues(buffer);
117
+ buffer[6] = buffer[6] & 15 | 64;
118
+ buffer[8] = buffer[8] & 63 | 128;
119
+ let hex = "";
120
+ for (let i = 0; i < 16; i++) {
121
+ hex += buffer[i].toString(16).padStart(2, "0");
122
+ if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
123
+ }
124
+ return hex;
125
+ }
126
+ }
127
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
128
+ const r = Math.random() * 16 | 0;
129
+ const v = c === "x" ? r : r & 3 | 8;
130
+ return v.toString(16);
131
+ });
132
+ }
133
+ function convertMessagesToMastraMessages(messages) {
134
+ const result = [];
135
+ for (const message of messages) {
136
+ if (message.role === "assistant") {
137
+ const parts = message.content ? [{ type: "text", text: message.content }] : [];
138
+ for (const toolCall of message.toolCalls ?? []) {
139
+ parts.push({
140
+ type: "tool-call",
141
+ toolCallId: toolCall.id,
142
+ toolName: toolCall.function.name,
143
+ args: JSON.parse(toolCall.function.arguments)
144
+ });
145
+ }
146
+ result.push({
147
+ role: "assistant",
148
+ content: parts
149
+ });
150
+ } else if (message.role === "user") {
151
+ result.push({
152
+ role: "user",
153
+ content: message.content || ""
154
+ });
155
+ } else if (message.role === "tool") {
156
+ result.push({
157
+ role: "tool",
158
+ content: [
159
+ {
160
+ type: "tool-result",
161
+ toolCallId: message.toolCallId,
162
+ toolName: "unknown",
163
+ result: message.content
164
+ }
165
+ ]
166
+ });
167
+ }
168
+ }
169
+ return result;
170
+ }
6
171
 
7
172
  // src/resources/base.ts
8
173
  var BaseResource = class {
@@ -134,7 +299,8 @@ var Agent = class extends BaseResource {
134
299
  const processedParams = {
135
300
  ...params,
136
301
  output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
137
- experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output
302
+ experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output,
303
+ runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
138
304
  };
139
305
  return this.request(`/api/agents/${this.agentId}/generate`, {
140
306
  method: "POST",
@@ -150,7 +316,8 @@ var Agent = class extends BaseResource {
150
316
  const processedParams = {
151
317
  ...params,
152
318
  output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
153
- experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output
319
+ experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output,
320
+ runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
154
321
  };
155
322
  const response = await this.request(`/api/agents/${this.agentId}/stream`, {
156
323
  method: "POST",
@@ -371,6 +538,34 @@ var Workflow = class extends BaseResource {
371
538
  details() {
372
539
  return this.request(`/api/workflows/${this.workflowId}`);
373
540
  }
541
+ /**
542
+ * Retrieves all runs for a workflow
543
+ * @param params - Parameters for filtering runs
544
+ * @returns Promise containing workflow runs array
545
+ */
546
+ runs(params) {
547
+ const searchParams = new URLSearchParams();
548
+ if (params?.fromDate) {
549
+ searchParams.set("fromDate", params.fromDate.toISOString());
550
+ }
551
+ if (params?.toDate) {
552
+ searchParams.set("toDate", params.toDate.toISOString());
553
+ }
554
+ if (params?.limit) {
555
+ searchParams.set("limit", String(params.limit));
556
+ }
557
+ if (params?.offset) {
558
+ searchParams.set("offset", String(params.offset));
559
+ }
560
+ if (params?.resourceId) {
561
+ searchParams.set("resourceId", params.resourceId);
562
+ }
563
+ if (searchParams.size) {
564
+ return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
565
+ } else {
566
+ return this.request(`/api/workflows/${this.workflowId}/runs`);
567
+ }
568
+ }
374
569
  /**
375
570
  * @deprecated Use `startAsync` instead
376
571
  * Executes the workflow with the provided parameters
@@ -487,7 +682,7 @@ var Workflow = class extends BaseResource {
487
682
  }
488
683
  }
489
684
  }
490
- } catch (error) {
685
+ } catch {
491
686
  }
492
687
  }
493
688
  if (buffer) {
@@ -541,13 +736,201 @@ var Tool = class extends BaseResource {
541
736
  * @returns Promise containing the tool execution results
542
737
  */
543
738
  execute(params) {
544
- return this.request(`/api/tools/${this.toolId}/execute`, {
739
+ const url = new URLSearchParams();
740
+ if (params.runId) {
741
+ url.set("runId", params.runId);
742
+ }
743
+ return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
545
744
  method: "POST",
546
- body: params
745
+ body: params.data
547
746
  });
548
747
  }
549
748
  };
550
749
 
750
+ // src/resources/vnext-workflow.ts
751
+ var RECORD_SEPARATOR2 = "";
752
+ var VNextWorkflow = class extends BaseResource {
753
+ constructor(options, workflowId) {
754
+ super(options);
755
+ this.workflowId = workflowId;
756
+ }
757
+ /**
758
+ * Creates an async generator that processes a readable stream and yields vNext workflow records
759
+ * separated by the Record Separator character (\x1E)
760
+ *
761
+ * @param stream - The readable stream to process
762
+ * @returns An async generator that yields parsed records
763
+ */
764
+ async *streamProcessor(stream) {
765
+ const reader = stream.getReader();
766
+ let doneReading = false;
767
+ let buffer = "";
768
+ try {
769
+ while (!doneReading) {
770
+ const { done, value } = await reader.read();
771
+ doneReading = done;
772
+ if (done && !value) continue;
773
+ try {
774
+ const decoded = value ? new TextDecoder().decode(value) : "";
775
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
776
+ buffer = chunks.pop() || "";
777
+ for (const chunk of chunks) {
778
+ if (chunk) {
779
+ if (typeof chunk === "string") {
780
+ try {
781
+ const parsedChunk = JSON.parse(chunk);
782
+ yield parsedChunk;
783
+ } catch {
784
+ }
785
+ }
786
+ }
787
+ }
788
+ } catch {
789
+ }
790
+ }
791
+ if (buffer) {
792
+ try {
793
+ yield JSON.parse(buffer);
794
+ } catch {
795
+ }
796
+ }
797
+ } finally {
798
+ reader.cancel().catch(() => {
799
+ });
800
+ }
801
+ }
802
+ /**
803
+ * Retrieves details about the vNext workflow
804
+ * @returns Promise containing vNext workflow details including steps and graphs
805
+ */
806
+ details() {
807
+ return this.request(`/api/workflows/v-next/${this.workflowId}`);
808
+ }
809
+ /**
810
+ * Retrieves all runs for a vNext workflow
811
+ * @param params - Parameters for filtering runs
812
+ * @returns Promise containing vNext workflow runs array
813
+ */
814
+ runs(params) {
815
+ const searchParams = new URLSearchParams();
816
+ if (params?.fromDate) {
817
+ searchParams.set("fromDate", params.fromDate.toISOString());
818
+ }
819
+ if (params?.toDate) {
820
+ searchParams.set("toDate", params.toDate.toISOString());
821
+ }
822
+ if (params?.limit) {
823
+ searchParams.set("limit", String(params.limit));
824
+ }
825
+ if (params?.offset) {
826
+ searchParams.set("offset", String(params.offset));
827
+ }
828
+ if (params?.resourceId) {
829
+ searchParams.set("resourceId", params.resourceId);
830
+ }
831
+ if (searchParams.size) {
832
+ return this.request(`/api/workflows/v-next/${this.workflowId}/runs?${searchParams}`);
833
+ } else {
834
+ return this.request(`/api/workflows/v-next/${this.workflowId}/runs`);
835
+ }
836
+ }
837
+ /**
838
+ * Creates a new vNext workflow run
839
+ * @param params - Optional object containing the optional runId
840
+ * @returns Promise containing the runId of the created run
841
+ */
842
+ createRun(params) {
843
+ const searchParams = new URLSearchParams();
844
+ if (!!params?.runId) {
845
+ searchParams.set("runId", params.runId);
846
+ }
847
+ return this.request(`/api/workflows/v-next/${this.workflowId}/create-run?${searchParams.toString()}`, {
848
+ method: "POST"
849
+ });
850
+ }
851
+ /**
852
+ * Starts a vNext workflow run synchronously without waiting for the workflow to complete
853
+ * @param params - Object containing the runId, inputData and runtimeContext
854
+ * @returns Promise containing success message
855
+ */
856
+ start(params) {
857
+ return this.request(`/api/workflows/v-next/${this.workflowId}/start?runId=${params.runId}`, {
858
+ method: "POST",
859
+ body: { inputData: params?.inputData, runtimeContext: params.runtimeContext }
860
+ });
861
+ }
862
+ /**
863
+ * Resumes a suspended vNext workflow step synchronously without waiting for the vNext workflow to complete
864
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
865
+ * @returns Promise containing success message
866
+ */
867
+ resume({
868
+ step,
869
+ runId,
870
+ resumeData,
871
+ runtimeContext
872
+ }) {
873
+ return this.request(`/api/workflows/v-next/${this.workflowId}/resume?runId=${runId}`, {
874
+ method: "POST",
875
+ stream: true,
876
+ body: {
877
+ step,
878
+ resumeData,
879
+ runtimeContext
880
+ }
881
+ });
882
+ }
883
+ /**
884
+ * Starts a vNext workflow run asynchronously and returns a promise that resolves when the vNext workflow is complete
885
+ * @param params - Object containing the optional runId, inputData and runtimeContext
886
+ * @returns Promise containing the vNext workflow execution results
887
+ */
888
+ startAsync(params) {
889
+ const searchParams = new URLSearchParams();
890
+ if (!!params?.runId) {
891
+ searchParams.set("runId", params.runId);
892
+ }
893
+ return this.request(`/api/workflows/v-next/${this.workflowId}/start-async?${searchParams.toString()}`, {
894
+ method: "POST",
895
+ body: { inputData: params.inputData, runtimeContext: params.runtimeContext }
896
+ });
897
+ }
898
+ /**
899
+ * Resumes a suspended vNext workflow step asynchronously and returns a promise that resolves when the vNext workflow is complete
900
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
901
+ * @returns Promise containing the vNext workflow resume results
902
+ */
903
+ resumeAsync(params) {
904
+ return this.request(`/api/workflows/v-next/${this.workflowId}/resume-async?runId=${params.runId}`, {
905
+ method: "POST",
906
+ body: {
907
+ step: params.step,
908
+ resumeData: params.resumeData,
909
+ runtimeContext: params.runtimeContext
910
+ }
911
+ });
912
+ }
913
+ /**
914
+ * Watches vNext workflow transitions in real-time
915
+ * @param runId - Optional run ID to filter the watch stream
916
+ * @returns AsyncGenerator that yields parsed records from the vNext workflow watch stream
917
+ */
918
+ async watch({ runId }, onRecord) {
919
+ const response = await this.request(`/api/workflows/v-next/${this.workflowId}/watch?runId=${runId}`, {
920
+ stream: true
921
+ });
922
+ if (!response.ok) {
923
+ throw new Error(`Failed to watch vNext workflow: ${response.statusText}`);
924
+ }
925
+ if (!response.body) {
926
+ throw new Error("Response body is null");
927
+ }
928
+ for await (const record of this.streamProcessor(response.body)) {
929
+ onRecord(record);
930
+ }
931
+ }
932
+ };
933
+
551
934
  // src/client.ts
552
935
  var MastraClient = class extends BaseResource {
553
936
  constructor(options) {
@@ -560,6 +943,21 @@ var MastraClient = class extends BaseResource {
560
943
  getAgents() {
561
944
  return this.request("/api/agents");
562
945
  }
946
+ async getAGUI({ resourceId }) {
947
+ const agents = await this.getAgents();
948
+ return Object.entries(agents).reduce(
949
+ (acc, [agentId]) => {
950
+ const agent = this.getAgent(agentId);
951
+ acc[agentId] = new AGUIAdapter({
952
+ agentId,
953
+ agent,
954
+ resourceId
955
+ });
956
+ return acc;
957
+ },
958
+ {}
959
+ );
960
+ }
563
961
  /**
564
962
  * Gets an agent instance by ID
565
963
  * @param agentId - ID of the agent to retrieve
@@ -640,6 +1038,21 @@ var MastraClient = class extends BaseResource {
640
1038
  getWorkflow(workflowId) {
641
1039
  return new Workflow(this.options, workflowId);
642
1040
  }
1041
+ /**
1042
+ * Retrieves all available vNext workflows
1043
+ * @returns Promise containing map of vNext workflow IDs to vNext workflow details
1044
+ */
1045
+ getVNextWorkflows() {
1046
+ return this.request("/api/workflows/v-next");
1047
+ }
1048
+ /**
1049
+ * Gets a vNext workflow instance by ID
1050
+ * @param workflowId - ID of the vNext workflow to retrieve
1051
+ * @returns vNext Workflow instance
1052
+ */
1053
+ getVNextWorkflow(workflowId) {
1054
+ return new VNextWorkflow(this.options, workflowId);
1055
+ }
643
1056
  /**
644
1057
  * Gets a vector instance by name
645
1058
  * @param vectorName - Name of the vector to retrieve
@@ -677,7 +1090,7 @@ var MastraClient = class extends BaseResource {
677
1090
  * @returns Promise containing telemetry data
678
1091
  */
679
1092
  getTelemetry(params) {
680
- const { name, scope, page, perPage, attribute } = params || {};
1093
+ const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
681
1094
  const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
682
1095
  const searchParams = new URLSearchParams();
683
1096
  if (name) {
@@ -701,6 +1114,12 @@ var MastraClient = class extends BaseResource {
701
1114
  searchParams.set("attribute", _attribute);
702
1115
  }
703
1116
  }
1117
+ if (fromDate) {
1118
+ searchParams.set("fromDate", fromDate.toISOString());
1119
+ }
1120
+ if (toDate) {
1121
+ searchParams.set("toDate", toDate.toISOString());
1122
+ }
704
1123
  if (searchParams.size) {
705
1124
  return this.request(`/api/telemetry?${searchParams}`);
706
1125
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/client-js",
3
- "version": "0.0.0-mastra-3171-llamaindex-pin-20250423175211",
3
+ "version": "0.0.0-mastra-3338-mastra-memory-pinecone-20250507174328",
4
4
  "description": "The official TypeScript library for the Mastra Client API",
5
5
  "author": "",
6
6
  "type": "module",
@@ -22,11 +22,13 @@
22
22
  "repository": "github:mastra-ai/client-js",
23
23
  "license": "Elastic-2.0",
24
24
  "dependencies": {
25
+ "@ag-ui/client": "^0.0.27",
25
26
  "@ai-sdk/ui-utils": "^1.1.19",
26
27
  "json-schema": "^0.4.0",
27
- "zod": "^3.24.2",
28
- "zod-to-json-schema": "^3.24.3",
29
- "@mastra/core": "0.0.0-mastra-3171-llamaindex-pin-20250423175211"
28
+ "rxjs": "7.8.1",
29
+ "zod": "^3.24.3",
30
+ "zod-to-json-schema": "^3.24.5",
31
+ "@mastra/core": "0.0.0-mastra-3338-mastra-memory-pinecone-20250507174328"
30
32
  },
31
33
  "peerDependencies": {
32
34
  "zod": "^3.24.2"
@@ -39,8 +41,8 @@
39
41
  "@types/node": "^20.17.27",
40
42
  "tsup": "^8.4.0",
41
43
  "typescript": "^5.8.2",
42
- "vitest": "^3.0.9",
43
- "@internal/lint": "0.0.2"
44
+ "vitest": "^3.1.2",
45
+ "@internal/lint": "0.0.0-mastra-3338-mastra-memory-pinecone-20250507174328"
44
46
  },
45
47
  "scripts": {
46
48
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting",
@@ -0,0 +1,167 @@
1
+ import type { Message } from '@ag-ui/client';
2
+ import { describe, it, expect } from 'vitest';
3
+ import { generateUUID, convertMessagesToMastraMessages } from './agui';
4
+
5
+ describe('generateUUID', () => {
6
+ it('should generate a valid UUID v4 string', () => {
7
+ const uuid = generateUUID();
8
+ // Check UUID format (8-4-4-4-12 hex digits)
9
+ expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
10
+ });
11
+
12
+ it('should generate unique UUIDs', () => {
13
+ const uuids = new Set();
14
+ for (let i = 0; i < 100; i++) {
15
+ uuids.add(generateUUID());
16
+ }
17
+ // All UUIDs should be unique
18
+ expect(uuids.size).toBe(100);
19
+ });
20
+ });
21
+
22
+ describe('convertMessagesToMastraMessages', () => {
23
+ it('should convert user messages correctly', () => {
24
+ const messages: Message[] = [
25
+ {
26
+ id: '1',
27
+ role: 'user',
28
+ content: 'Hello, world!',
29
+ },
30
+ ];
31
+
32
+ const result = convertMessagesToMastraMessages(messages);
33
+
34
+ expect(result).toEqual([
35
+ {
36
+ role: 'user',
37
+ content: 'Hello, world!',
38
+ },
39
+ ]);
40
+ });
41
+
42
+ it('should convert assistant messages correctly', () => {
43
+ const messages: Message[] = [
44
+ {
45
+ id: '1',
46
+ role: 'assistant',
47
+ content: 'Hello, I am an assistant',
48
+ },
49
+ ];
50
+
51
+ const result = convertMessagesToMastraMessages(messages);
52
+
53
+ expect(result).toEqual([
54
+ {
55
+ role: 'assistant',
56
+ content: [{ type: 'text', text: 'Hello, I am an assistant' }],
57
+ },
58
+ ]);
59
+ });
60
+
61
+ it('should convert assistant messages with tool calls correctly', () => {
62
+ const messages: Message[] = [
63
+ {
64
+ id: '1',
65
+ role: 'assistant',
66
+ content: undefined,
67
+ toolCalls: [
68
+ {
69
+ id: 'tool-call-1',
70
+ type: 'function',
71
+ function: {
72
+ name: 'getWeather',
73
+ arguments: '{"location":"San Francisco"}',
74
+ },
75
+ },
76
+ ],
77
+ },
78
+ ];
79
+
80
+ const result = convertMessagesToMastraMessages(messages);
81
+
82
+ expect(result).toEqual([
83
+ {
84
+ role: 'assistant',
85
+ content: [
86
+ {
87
+ type: 'tool-call',
88
+ toolCallId: 'tool-call-1',
89
+ toolName: 'getWeather',
90
+ args: { location: 'San Francisco' },
91
+ },
92
+ ],
93
+ },
94
+ ]);
95
+ });
96
+
97
+ it('should convert tool messages correctly', () => {
98
+ const messages: Message[] = [
99
+ {
100
+ id: '1',
101
+ role: 'tool',
102
+ toolCallId: 'tool-call-1',
103
+ content: '{"temperature":72,"unit":"F"}',
104
+ },
105
+ ];
106
+
107
+ const result = convertMessagesToMastraMessages(messages);
108
+
109
+ expect(result).toEqual([
110
+ {
111
+ role: 'tool',
112
+ content: [
113
+ {
114
+ type: 'tool-result',
115
+ toolCallId: 'tool-call-1',
116
+ toolName: 'unknown',
117
+ result: '{"temperature":72,"unit":"F"}',
118
+ },
119
+ ],
120
+ },
121
+ ]);
122
+ });
123
+
124
+ it('should convert a complex conversation correctly', () => {
125
+ const messages: Message[] = [
126
+ {
127
+ id: '1',
128
+ role: 'user',
129
+ content: "What's the weather in San Francisco?",
130
+ },
131
+ {
132
+ id: '2',
133
+ role: 'assistant',
134
+ content: undefined,
135
+ toolCalls: [
136
+ {
137
+ id: 'tool-call-1',
138
+ type: 'function',
139
+ function: {
140
+ name: 'getWeather',
141
+ arguments: '{"location":"San Francisco"}',
142
+ },
143
+ },
144
+ ],
145
+ },
146
+ {
147
+ id: '3',
148
+ role: 'tool',
149
+ toolCallId: 'tool-call-1',
150
+ content: '{"temperature":72,"unit":"F"}',
151
+ },
152
+ {
153
+ id: '4',
154
+ role: 'assistant',
155
+ content: 'The weather in San Francisco is 72°F.',
156
+ },
157
+ ];
158
+
159
+ const result = convertMessagesToMastraMessages(messages);
160
+
161
+ expect(result).toHaveLength(4);
162
+ expect(result[0].role).toBe('user');
163
+ expect(result[1].role).toBe('assistant');
164
+ expect(result[2].role).toBe('tool');
165
+ expect(result[3].role).toBe('assistant');
166
+ });
167
+ });