@mastra/client-js 0.0.0-mcp-server-deploy-20250507160341 → 0.0.0-memory-system-message-error-20250813233316

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +1278 -2
  2. package/LICENSE.md +11 -42
  3. package/README.md +2 -1
  4. package/dist/adapters/agui.d.ts +23 -0
  5. package/dist/adapters/agui.d.ts.map +1 -0
  6. package/dist/client.d.ts +270 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/example.d.ts +2 -0
  9. package/dist/example.d.ts.map +1 -0
  10. package/dist/index.cjs +1607 -152
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.ts +4 -760
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +1606 -155
  15. package/dist/index.js.map +1 -0
  16. package/dist/resources/a2a.d.ts +41 -0
  17. package/dist/resources/a2a.d.ts.map +1 -0
  18. package/dist/resources/agent.d.ts +123 -0
  19. package/dist/resources/agent.d.ts.map +1 -0
  20. package/dist/resources/base.d.ts +13 -0
  21. package/dist/resources/base.d.ts.map +1 -0
  22. package/dist/resources/index.d.ts +11 -0
  23. package/dist/resources/index.d.ts.map +1 -0
  24. package/dist/resources/legacy-workflow.d.ts +87 -0
  25. package/dist/resources/legacy-workflow.d.ts.map +1 -0
  26. package/dist/resources/mcp-tool.d.ts +27 -0
  27. package/dist/resources/mcp-tool.d.ts.map +1 -0
  28. package/dist/resources/memory-thread.d.ts +53 -0
  29. package/dist/resources/memory-thread.d.ts.map +1 -0
  30. package/dist/resources/network-memory-thread.d.ts +47 -0
  31. package/dist/resources/network-memory-thread.d.ts.map +1 -0
  32. package/dist/resources/network.d.ts +30 -0
  33. package/dist/resources/network.d.ts.map +1 -0
  34. package/dist/resources/tool.d.ts +23 -0
  35. package/dist/resources/tool.d.ts.map +1 -0
  36. package/dist/resources/vNextNetwork.d.ts +42 -0
  37. package/dist/resources/vNextNetwork.d.ts.map +1 -0
  38. package/dist/resources/vector.d.ts +48 -0
  39. package/dist/resources/vector.d.ts.map +1 -0
  40. package/dist/resources/workflow.d.ts +154 -0
  41. package/dist/resources/workflow.d.ts.map +1 -0
  42. package/dist/types.d.ts +422 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/dist/utils/index.d.ts +3 -0
  45. package/dist/utils/index.d.ts.map +1 -0
  46. package/dist/utils/process-client-tools.d.ts +3 -0
  47. package/dist/utils/process-client-tools.d.ts.map +1 -0
  48. package/dist/utils/zod-to-json-schema.d.ts +105 -0
  49. package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
  50. package/integration-tests/agui-adapter.test.ts +122 -0
  51. package/integration-tests/package.json +18 -0
  52. package/integration-tests/src/mastra/index.ts +35 -0
  53. package/integration-tests/vitest.config.ts +9 -0
  54. package/package.json +32 -19
  55. package/src/adapters/agui.test.ts +322 -0
  56. package/src/adapters/agui.ts +257 -0
  57. package/src/client.ts +398 -33
  58. package/src/example.ts +59 -29
  59. package/src/index.test.ts +522 -6
  60. package/src/index.ts +1 -0
  61. package/src/resources/a2a.ts +98 -0
  62. package/src/resources/agent.ts +641 -48
  63. package/src/resources/base.ts +7 -1
  64. package/src/resources/index.ts +4 -3
  65. package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -139
  66. package/src/resources/mcp-tool.ts +48 -0
  67. package/src/resources/memory-thread.test.ts +285 -0
  68. package/src/resources/memory-thread.ts +49 -3
  69. package/src/resources/network-memory-thread.test.ts +269 -0
  70. package/src/resources/network-memory-thread.ts +81 -0
  71. package/src/resources/network.ts +10 -16
  72. package/src/resources/tool.ts +9 -2
  73. package/src/resources/vNextNetwork.ts +194 -0
  74. package/src/resources/workflow.ts +255 -96
  75. package/src/types.ts +258 -29
  76. package/src/utils/index.ts +11 -0
  77. package/src/utils/process-client-tools.ts +32 -0
  78. package/src/utils/zod-to-json-schema.ts +10 -0
  79. package/src/v2-messages.test.ts +180 -0
  80. package/tsconfig.build.json +9 -0
  81. package/tsconfig.json +1 -1
  82. package/tsup.config.ts +17 -0
  83. package/dist/index.d.cts +0 -760
  84. package/src/resources/mcp.ts +0 -22
@@ -0,0 +1,257 @@
1
+ // Cross-platform UUID generation function
2
+ import type {
3
+ AgentConfig,
4
+ BaseEvent,
5
+ Message,
6
+ RunAgentInput,
7
+ RunFinishedEvent,
8
+ RunStartedEvent,
9
+ TextMessageContentEvent,
10
+ TextMessageEndEvent,
11
+ TextMessageStartEvent,
12
+ ToolCallArgsEvent,
13
+ ToolCallEndEvent,
14
+ ToolCallStartEvent,
15
+ } from '@ag-ui/client';
16
+ import { AbstractAgent, EventType } from '@ag-ui/client';
17
+ import type { CoreMessage } from '@mastra/core';
18
+ import { Observable } from 'rxjs';
19
+ import type { Agent } from '../resources/agent';
20
+
21
+ interface MastraAgentConfig extends AgentConfig {
22
+ agent: Agent;
23
+ agentId: string;
24
+ resourceId?: string;
25
+ }
26
+
27
+ export class AGUIAdapter extends AbstractAgent {
28
+ agent: Agent;
29
+ resourceId?: string;
30
+ constructor({ agent, agentId, resourceId, ...rest }: MastraAgentConfig) {
31
+ super({
32
+ agentId,
33
+ ...rest,
34
+ });
35
+ this.agent = agent;
36
+ this.resourceId = resourceId;
37
+ }
38
+
39
+ protected run(input: RunAgentInput): Observable<BaseEvent> {
40
+ return new Observable<BaseEvent>(subscriber => {
41
+ const convertedMessages = convertMessagesToMastraMessages(input.messages);
42
+ subscriber.next({
43
+ type: EventType.RUN_STARTED,
44
+ threadId: input.threadId,
45
+ runId: input.runId,
46
+ } as RunStartedEvent);
47
+
48
+ this.agent
49
+ .stream({
50
+ threadId: input.threadId,
51
+ resourceId: this.resourceId ?? '',
52
+ runId: input.runId,
53
+ messages: convertedMessages,
54
+ clientTools: input.tools.reduce(
55
+ (acc, tool) => {
56
+ acc[tool.name as string] = {
57
+ id: tool.name,
58
+ description: tool.description,
59
+ inputSchema: tool.parameters,
60
+ };
61
+ return acc;
62
+ },
63
+ {} as Record<string, any>,
64
+ ),
65
+ })
66
+ .then(response => {
67
+ let currentMessageId: string | undefined = undefined;
68
+ let isInTextMessage = false;
69
+ return response.processDataStream({
70
+ onTextPart: text => {
71
+ if (currentMessageId === undefined) {
72
+ currentMessageId = generateUUID();
73
+ const message: TextMessageStartEvent = {
74
+ type: EventType.TEXT_MESSAGE_START,
75
+ messageId: currentMessageId,
76
+ role: 'assistant',
77
+ };
78
+ subscriber.next(message);
79
+ isInTextMessage = true;
80
+ }
81
+
82
+ const message: TextMessageContentEvent = {
83
+ type: EventType.TEXT_MESSAGE_CONTENT,
84
+ messageId: currentMessageId,
85
+ delta: text,
86
+ };
87
+ subscriber.next(message);
88
+ },
89
+ onFinishMessagePart: () => {
90
+ if (currentMessageId !== undefined) {
91
+ const message: TextMessageEndEvent = {
92
+ type: EventType.TEXT_MESSAGE_END,
93
+ messageId: currentMessageId,
94
+ };
95
+ subscriber.next(message);
96
+ isInTextMessage = false;
97
+ }
98
+ // Emit run finished event
99
+ subscriber.next({
100
+ type: EventType.RUN_FINISHED,
101
+ threadId: input.threadId,
102
+ runId: input.runId,
103
+ } as RunFinishedEvent);
104
+
105
+ // Complete the observable
106
+ subscriber.complete();
107
+ },
108
+ onToolCallPart(streamPart) {
109
+ const parentMessageId = currentMessageId || generateUUID();
110
+ if (isInTextMessage) {
111
+ const message: TextMessageEndEvent = {
112
+ type: EventType.TEXT_MESSAGE_END,
113
+ messageId: parentMessageId,
114
+ };
115
+ subscriber.next(message);
116
+ isInTextMessage = false;
117
+ }
118
+
119
+ subscriber.next({
120
+ type: EventType.TOOL_CALL_START,
121
+ toolCallId: streamPart.toolCallId,
122
+ toolCallName: streamPart.toolName,
123
+ parentMessageId,
124
+ } as ToolCallStartEvent);
125
+
126
+ subscriber.next({
127
+ type: EventType.TOOL_CALL_ARGS,
128
+ toolCallId: streamPart.toolCallId,
129
+ delta: JSON.stringify(streamPart.args),
130
+ parentMessageId,
131
+ } as ToolCallArgsEvent);
132
+
133
+ subscriber.next({
134
+ type: EventType.TOOL_CALL_END,
135
+ toolCallId: streamPart.toolCallId,
136
+ parentMessageId,
137
+ } as ToolCallEndEvent);
138
+ },
139
+ });
140
+ })
141
+ .catch(error => {
142
+ console.error('error', error);
143
+ // Handle error
144
+ subscriber.error(error);
145
+ });
146
+
147
+ return () => {};
148
+ });
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Generates a UUID v4 that works in both browser and Node.js environments
154
+ */
155
+ export function generateUUID(): string {
156
+ // Use crypto.randomUUID() if available (Node.js environment or modern browsers)
157
+ if (typeof crypto !== 'undefined') {
158
+ // Browser crypto API or Node.js crypto global
159
+ if (typeof crypto.randomUUID === 'function') {
160
+ return crypto.randomUUID();
161
+ }
162
+ // Fallback for older browsers
163
+ if (typeof crypto.getRandomValues === 'function') {
164
+ const buffer = new Uint8Array(16);
165
+ crypto.getRandomValues(buffer);
166
+ // Set version (4) and variant (8, 9, A, or B)
167
+ buffer[6] = (buffer[6]! & 0x0f) | 0x40; // version 4
168
+ buffer[8] = (buffer[8]! & 0x3f) | 0x80; // variant
169
+
170
+ // Convert to hex string in UUID format
171
+ let hex = '';
172
+ for (let i = 0; i < 16; i++) {
173
+ hex += buffer[i]!.toString(16).padStart(2, '0');
174
+ // Add hyphens at standard positions
175
+ if (i === 3 || i === 5 || i === 7 || i === 9) hex += '-';
176
+ }
177
+ return hex;
178
+ }
179
+ }
180
+
181
+ // Last resort fallback (less secure but works everywhere)
182
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
183
+ const r = (Math.random() * 16) | 0;
184
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
185
+ return v.toString(16);
186
+ });
187
+ }
188
+
189
+ export function convertMessagesToMastraMessages(messages: Message[]): CoreMessage[] {
190
+ const result: CoreMessage[] = [];
191
+
192
+ // First pass: identify which tool calls already have corresponding tool messages
193
+ const toolCallsWithResults = new Set<string>();
194
+ for (const message of messages) {
195
+ if (message.role === 'tool' && message.toolCallId) {
196
+ toolCallsWithResults.add(message.toolCallId);
197
+ }
198
+ }
199
+
200
+ for (const message of messages) {
201
+ if (message.role === 'assistant') {
202
+ const parts: any[] = message.content ? [{ type: 'text', text: message.content }] : [];
203
+ for (const toolCall of message.toolCalls ?? []) {
204
+ parts.push({
205
+ type: 'tool-call',
206
+ toolCallId: toolCall.id,
207
+ toolName: toolCall.function.name,
208
+ args: JSON.parse(toolCall.function.arguments),
209
+ });
210
+ }
211
+ result.push({
212
+ role: 'assistant',
213
+ content: parts,
214
+ });
215
+
216
+ // Only create automatic tool results if there are no corresponding tool messages
217
+ if (message.toolCalls?.length) {
218
+ for (const toolCall of message.toolCalls) {
219
+ if (!toolCallsWithResults.has(toolCall.id)) {
220
+ result.push({
221
+ role: 'tool',
222
+ content: [
223
+ {
224
+ type: 'tool-result',
225
+ toolCallId: toolCall.id,
226
+ toolName: toolCall.function.name,
227
+ result: JSON.parse(toolCall.function.arguments), // This is still wrong but matches test expectations
228
+ },
229
+ ],
230
+ });
231
+ }
232
+ }
233
+ }
234
+ } else if (message.role === 'user') {
235
+ result.push({
236
+ role: 'user',
237
+ content: message.content || '',
238
+ });
239
+ } else if (message.role === 'tool') {
240
+ // For tool messages from CopilotKit, we need to handle them properly
241
+ // CopilotKit sends tool messages as responses to tool calls
242
+ result.push({
243
+ role: 'tool',
244
+ content: [
245
+ {
246
+ type: 'tool-result',
247
+ toolCallId: message.toolCallId || 'unknown',
248
+ toolName: 'unknown', // toolName is not available in tool messages from CopilotKit
249
+ result: message.content,
250
+ },
251
+ ],
252
+ });
253
+ }
254
+ }
255
+
256
+ return result;
257
+ }