@mastra/client-js 0.0.0-ai-v5-20250801192614 → 0.0.0-ai-telementry-ui-20250908094819

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 (81) hide show
  1. package/CHANGELOG.md +541 -4
  2. package/README.md +5 -3
  3. package/dist/client.d.ts +23 -8
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/index.cjs +1171 -379
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +1170 -378
  10. package/dist/index.js.map +1 -1
  11. package/dist/resources/a2a.d.ts +15 -18
  12. package/dist/resources/a2a.d.ts.map +1 -1
  13. package/dist/resources/agent-builder.d.ts +161 -0
  14. package/dist/resources/agent-builder.d.ts.map +1 -0
  15. package/dist/resources/agent.d.ts +52 -9
  16. package/dist/resources/agent.d.ts.map +1 -1
  17. package/dist/resources/base.d.ts +1 -1
  18. package/dist/resources/index.d.ts +12 -10
  19. package/dist/resources/index.d.ts.map +1 -1
  20. package/dist/resources/legacy-workflow.d.ts +2 -2
  21. package/dist/resources/mcp-tool.d.ts +2 -2
  22. package/dist/resources/memory-thread.d.ts +3 -3
  23. package/dist/resources/memory-thread.d.ts.map +1 -1
  24. package/dist/resources/network-memory-thread.d.ts +3 -3
  25. package/dist/resources/network-memory-thread.d.ts.map +1 -1
  26. package/dist/resources/network.d.ts +4 -4
  27. package/dist/resources/network.d.ts.map +1 -1
  28. package/dist/resources/observability.d.ts +19 -0
  29. package/dist/resources/observability.d.ts.map +1 -0
  30. package/dist/resources/tool.d.ts +2 -2
  31. package/dist/resources/tool.d.ts.map +1 -1
  32. package/dist/resources/vNextNetwork.d.ts +3 -3
  33. package/dist/resources/vNextNetwork.d.ts.map +1 -1
  34. package/dist/resources/vector.d.ts +2 -2
  35. package/dist/resources/workflow.d.ts +2 -2
  36. package/dist/types.d.ts +44 -10
  37. package/dist/types.d.ts.map +1 -1
  38. package/dist/utils/process-client-tools.d.ts.map +1 -1
  39. package/dist/utils/process-mastra-stream.d.ts +7 -0
  40. package/dist/utils/process-mastra-stream.d.ts.map +1 -0
  41. package/dist/utils/zod-to-json-schema.d.ts +2 -104
  42. package/dist/utils/zod-to-json-schema.d.ts.map +1 -1
  43. package/package.json +27 -17
  44. package/.turbo/turbo-build.log +0 -4
  45. package/dist/adapters/agui.d.ts +0 -22
  46. package/dist/adapters/agui.d.ts.map +0 -1
  47. package/eslint.config.js +0 -6
  48. package/integration-tests/agui-adapter.test.ts +0 -122
  49. package/integration-tests/package.json +0 -17
  50. package/integration-tests/src/mastra/index.ts +0 -38
  51. package/integration-tests/vitest.config.ts +0 -9
  52. package/src/adapters/agui.test.ts +0 -322
  53. package/src/adapters/agui.ts +0 -251
  54. package/src/client.ts +0 -623
  55. package/src/example.ts +0 -95
  56. package/src/index.test.ts +0 -1226
  57. package/src/index.ts +0 -2
  58. package/src/resources/a2a.ts +0 -88
  59. package/src/resources/agent.ts +0 -781
  60. package/src/resources/base.ts +0 -77
  61. package/src/resources/index.ts +0 -10
  62. package/src/resources/legacy-workflow.ts +0 -242
  63. package/src/resources/mcp-tool.ts +0 -48
  64. package/src/resources/memory-thread.test.ts +0 -285
  65. package/src/resources/memory-thread.ts +0 -100
  66. package/src/resources/network-memory-thread.test.ts +0 -269
  67. package/src/resources/network-memory-thread.ts +0 -81
  68. package/src/resources/network.ts +0 -86
  69. package/src/resources/tool.ts +0 -45
  70. package/src/resources/vNextNetwork.ts +0 -194
  71. package/src/resources/vector.ts +0 -83
  72. package/src/resources/workflow.ts +0 -410
  73. package/src/types.ts +0 -506
  74. package/src/utils/index.ts +0 -11
  75. package/src/utils/process-client-tools.ts +0 -32
  76. package/src/utils/zod-to-json-schema.ts +0 -10
  77. package/src/v2-messages.test.ts +0 -180
  78. package/tsconfig.build.json +0 -9
  79. package/tsconfig.json +0 -5
  80. package/tsup.config.ts +0 -22
  81. package/vitest.config.js +0 -8
@@ -1,322 +0,0 @@
1
- import type { Message, BaseEvent } from '@ag-ui/client';
2
- import { describe, it, expect, vi } from 'vitest';
3
- import { generateUUID, convertMessagesToMastraMessages, AGUIAdapter } from './agui';
4
- import { Agent } from '@mastra/core/agent';
5
- import { MockLanguageModelV1 } from 'ai/test';
6
- import { simulateReadableStream } from 'ai';
7
-
8
- describe('generateUUID', () => {
9
- it('should generate a valid UUID v4 string', () => {
10
- const uuid = generateUUID();
11
- // Check UUID format (8-4-4-4-12 hex digits)
12
- 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);
13
- });
14
-
15
- it('should generate unique UUIDs', () => {
16
- const uuids = new Set();
17
- for (let i = 0; i < 100; i++) {
18
- uuids.add(generateUUID());
19
- }
20
- // All UUIDs should be unique
21
- expect(uuids.size).toBe(100);
22
- });
23
- });
24
-
25
- describe('convertMessagesToMastraMessages', () => {
26
- it('should convert user messages correctly', () => {
27
- const messages: Message[] = [
28
- {
29
- id: '1',
30
- role: 'user',
31
- content: 'Hello, world!',
32
- },
33
- ];
34
-
35
- const result = convertMessagesToMastraMessages(messages);
36
-
37
- expect(result).toEqual([
38
- {
39
- role: 'user',
40
- content: 'Hello, world!',
41
- },
42
- ]);
43
- });
44
-
45
- it('should convert assistant messages correctly', () => {
46
- const messages: Message[] = [
47
- {
48
- id: '1',
49
- role: 'assistant',
50
- content: 'Hello, I am an assistant',
51
- },
52
- ];
53
-
54
- const result = convertMessagesToMastraMessages(messages);
55
-
56
- expect(result).toEqual([
57
- {
58
- role: 'assistant',
59
- content: [{ type: 'text', text: 'Hello, I am an assistant' }],
60
- },
61
- ]);
62
- });
63
-
64
- it('should convert assistant messages with tool calls correctly', () => {
65
- const messages: Message[] = [
66
- {
67
- id: '1',
68
- role: 'assistant',
69
- content: undefined,
70
- toolCalls: [
71
- {
72
- id: 'tool-call-1',
73
- type: 'function',
74
- function: {
75
- name: 'getWeather',
76
- arguments: '{"location":"San Francisco"}',
77
- },
78
- },
79
- ],
80
- },
81
- ];
82
-
83
- const result = convertMessagesToMastraMessages(messages);
84
-
85
- expect(result).toEqual([
86
- {
87
- role: 'assistant',
88
- content: [
89
- {
90
- type: 'tool-call',
91
- toolCallId: 'tool-call-1',
92
- toolName: 'getWeather',
93
- args: { location: 'San Francisco' },
94
- },
95
- ],
96
- },
97
- {
98
- role: 'tool',
99
- content: [
100
- {
101
- type: 'tool-result',
102
- toolCallId: 'tool-call-1',
103
- toolName: 'getWeather',
104
- result: { location: 'San Francisco' },
105
- },
106
- ],
107
- },
108
- ]);
109
- });
110
-
111
- it('should convert tool messages correctly', () => {
112
- const messages: Message[] = [
113
- {
114
- id: '1',
115
- role: 'tool',
116
- toolCallId: 'tool-call-1',
117
- content: '{"temperature":72,"unit":"F"}',
118
- },
119
- ];
120
-
121
- const result = convertMessagesToMastraMessages(messages);
122
-
123
- expect(result).toEqual([
124
- {
125
- role: 'tool',
126
- content: [
127
- {
128
- type: 'tool-result',
129
- toolCallId: 'tool-call-1',
130
- toolName: 'unknown',
131
- result: '{"temperature":72,"unit":"F"}',
132
- },
133
- ],
134
- },
135
- ]);
136
- });
137
-
138
- it('should convert a complex conversation correctly', () => {
139
- const messages: Message[] = [
140
- {
141
- id: '1',
142
- role: 'user',
143
- content: "What's the weather in San Francisco?",
144
- },
145
- {
146
- id: '2',
147
- role: 'assistant',
148
- content: undefined,
149
- toolCalls: [
150
- {
151
- id: 'tool-call-1',
152
- type: 'function',
153
- function: {
154
- name: 'getWeather',
155
- arguments: '{"location":"San Francisco"}',
156
- },
157
- },
158
- ],
159
- },
160
- {
161
- id: '4',
162
- role: 'assistant',
163
- content: 'The weather in San Francisco is 72°F.',
164
- },
165
- ];
166
-
167
- const result = convertMessagesToMastraMessages(messages);
168
-
169
- expect(result).toHaveLength(4);
170
- expect(result[0].role).toBe('user');
171
- expect(result[1].role).toBe('assistant');
172
- expect(result[2].role).toBe('tool');
173
- expect(result[2].content).toEqual([
174
- {
175
- type: 'tool-result',
176
- toolCallId: 'tool-call-1',
177
- toolName: 'getWeather',
178
- result: { location: 'San Francisco' },
179
- },
180
- ]);
181
- expect(result[3].role).toBe('assistant');
182
- });
183
- });
184
-
185
- describe('AGUIAdapter', () => {
186
- it('should correctly pass parameters to agent stream method', async () => {
187
- // Create a real agent with MockLanguageModelV1
188
- const mockModel = new MockLanguageModelV1({
189
- doStream: async () => ({
190
- stream: simulateReadableStream({
191
- chunks: [
192
- { type: 'text-delta', textDelta: 'Hello' },
193
- { type: 'text-delta', textDelta: ' from' },
194
- { type: 'text-delta', textDelta: ' agent' },
195
- {
196
- type: 'finish',
197
- finishReason: 'stop',
198
- logprobs: undefined,
199
- usage: { completionTokens: 3, promptTokens: 10 },
200
- },
201
- ],
202
- }),
203
- rawCall: { rawPrompt: null, rawSettings: {} },
204
- }),
205
- });
206
-
207
- const agent = new Agent({
208
- name: 'Test Agent',
209
- instructions: 'You are a test agent',
210
- model: mockModel,
211
- });
212
-
213
- // Create a mock client agent that simulates the expected behavior
214
- const clientAgent = {
215
- stream: vi.fn().mockImplementation(async (params: any) => {
216
- // Verify the parameters are passed correctly
217
- expect(params).toHaveProperty('messages');
218
- expect(params).toHaveProperty('threadId');
219
- expect(params).toHaveProperty('resourceId');
220
- expect(params).toHaveProperty('runId');
221
- expect(params).toHaveProperty('clientTools');
222
-
223
- // Verify that messages array is passed, not the entire request object
224
- expect(Array.isArray(params.messages)).toBe(true);
225
- expect(params.messages[0]).toHaveProperty('role');
226
- expect(params.messages[0]).toHaveProperty('content');
227
-
228
- // Return a mock processDataStream that mimics the expected behavior
229
- return {
230
- processDataStream: vi.fn().mockImplementation(async ({ onTextPart, onFinishMessagePart }: any) => {
231
- // Simulate streaming text
232
- if (onTextPart) {
233
- onTextPart('Hello from agent');
234
- }
235
- if (onFinishMessagePart) {
236
- onFinishMessagePart();
237
- }
238
- return Promise.resolve();
239
- }),
240
- };
241
- }),
242
- };
243
-
244
- const adapter = new AGUIAdapter({
245
- agent: clientAgent as any,
246
- agentId: 'test',
247
- resourceId: 'testAgent',
248
- });
249
-
250
- const input = {
251
- threadId: 'test-thread-id',
252
- runId: 'test-run-id',
253
- messages: [
254
- {
255
- id: '1',
256
- role: 'user' as const,
257
- content: 'Hello',
258
- },
259
- ],
260
- tools: [],
261
- context: [],
262
- };
263
-
264
- const observable = adapter['run'](input);
265
- const events: BaseEvent[] = [];
266
-
267
- await new Promise<void>((resolve, reject) => {
268
- observable.subscribe({
269
- next: (event: BaseEvent) => events.push(event),
270
- complete: () => resolve(),
271
- error: (error: any) => reject(error),
272
- });
273
- });
274
-
275
- // Verify we received the expected events
276
- expect(events).toHaveLength(5); // RUN_STARTED, TEXT_MESSAGE_START, TEXT_MESSAGE_CONTENT, TEXT_MESSAGE_END, RUN_FINISHED
277
- expect(events[0].type).toBe('RUN_STARTED');
278
- expect(events[1].type).toBe('TEXT_MESSAGE_START');
279
- expect(events[2].type).toBe('TEXT_MESSAGE_CONTENT');
280
- expect(events[3].type).toBe('TEXT_MESSAGE_END');
281
- expect(events[4].type).toBe('RUN_FINISHED');
282
-
283
- // Verify the stream method was called with the correct parameters
284
- expect(clientAgent.stream).toHaveBeenCalledWith({
285
- threadId: 'test-thread-id',
286
- resourceId: 'testAgent',
287
- runId: 'test-run-id',
288
- messages: [{ role: 'user', content: 'Hello' }],
289
- clientTools: {},
290
- });
291
- });
292
-
293
- it('should handle messages without role property in request objects', async () => {
294
- // This test demonstrates that request objects without role property
295
- // would cause validation errors if passed directly to MessageList
296
- const requestObject = {
297
- threadId: 'test-thread-id',
298
- resourceId: 'testAgent',
299
- runId: 'test-run-id',
300
- messages: [
301
- {
302
- role: 'user',
303
- content: 'Hello',
304
- },
305
- ],
306
- clientTools: {},
307
- };
308
-
309
- // Request objects don't have role property
310
- expect('role' in requestObject).toBe(false);
311
- expect('messages' in requestObject).toBe(true);
312
- expect('content' in requestObject).toBe(false);
313
- expect('parts' in requestObject).toBe(false);
314
-
315
- // This structure would cause validation errors if treated as a message
316
- // because it lacks required message properties (role, content/parts)
317
- const hasValidMessageStructure =
318
- 'role' in requestObject && ('content' in requestObject || 'parts' in requestObject);
319
-
320
- expect(hasValidMessageStructure).toBe(false);
321
- });
322
- });
@@ -1,251 +0,0 @@
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 { Observable } from 'rxjs';
18
- import type { Agent } from '../resources/agent';
19
-
20
- interface MastraAgentConfig extends AgentConfig {
21
- agent: Agent;
22
- agentId: string;
23
- resourceId?: string;
24
- }
25
-
26
- export class AGUIAdapter extends AbstractAgent {
27
- agent: Agent;
28
- resourceId?: string;
29
- constructor({ agent, agentId, resourceId, ...rest }: MastraAgentConfig) {
30
- super({
31
- agentId,
32
- ...rest,
33
- });
34
- this.agent = agent;
35
- this.resourceId = resourceId;
36
- }
37
-
38
- protected run(input: RunAgentInput): Observable<BaseEvent> {
39
- return new Observable<BaseEvent>(subscriber => {
40
- const convertedMessages = convertMessagesToMastraMessages(input.messages);
41
- subscriber.next({
42
- type: EventType.RUN_STARTED,
43
- threadId: input.threadId,
44
- runId: input.runId,
45
- } as RunStartedEvent);
46
-
47
- this.agent
48
- .stream({
49
- threadId: input.threadId,
50
- resourceId: this.resourceId ?? '',
51
- runId: input.runId,
52
- messages: convertedMessages,
53
- clientTools: input.tools.reduce(
54
- (acc, tool) => {
55
- acc[tool.name as string] = {
56
- id: tool.name,
57
- description: tool.description,
58
- inputSchema: tool.parameters,
59
- };
60
- return acc;
61
- },
62
- {} as Record<string, any>,
63
- ),
64
- })
65
- .then(response => {
66
- let currentMessageId: string | undefined = undefined;
67
- let isInTextMessage = false;
68
- return response.processDataStream({
69
- onTextPart: text => {
70
- if (currentMessageId === undefined) {
71
- currentMessageId = generateUUID();
72
- const message: TextMessageStartEvent = {
73
- type: EventType.TEXT_MESSAGE_START,
74
- messageId: currentMessageId,
75
- role: 'assistant',
76
- };
77
- subscriber.next(message);
78
- isInTextMessage = true;
79
- }
80
-
81
- const message: TextMessageContentEvent = {
82
- type: EventType.TEXT_MESSAGE_CONTENT,
83
- messageId: currentMessageId,
84
- delta: text,
85
- };
86
- subscriber.next(message);
87
- },
88
- onFinishMessagePart: () => {
89
- if (currentMessageId !== undefined) {
90
- const message: TextMessageEndEvent = {
91
- type: EventType.TEXT_MESSAGE_END,
92
- messageId: currentMessageId,
93
- };
94
- subscriber.next(message);
95
- isInTextMessage = false;
96
- }
97
- // Emit run finished event
98
- subscriber.next({
99
- type: EventType.RUN_FINISHED,
100
- threadId: input.threadId,
101
- runId: input.runId,
102
- } as RunFinishedEvent);
103
-
104
- // Complete the observable
105
- subscriber.complete();
106
- },
107
- onToolCallPart(streamPart) {
108
- const parentMessageId = currentMessageId || generateUUID();
109
- if (isInTextMessage) {
110
- const message: TextMessageEndEvent = {
111
- type: EventType.TEXT_MESSAGE_END,
112
- messageId: parentMessageId,
113
- };
114
- subscriber.next(message);
115
- isInTextMessage = false;
116
- }
117
-
118
- subscriber.next({
119
- type: EventType.TOOL_CALL_START,
120
- toolCallId: streamPart.toolCallId,
121
- toolCallName: streamPart.toolName,
122
- parentMessageId,
123
- } as ToolCallStartEvent);
124
-
125
- subscriber.next({
126
- type: EventType.TOOL_CALL_ARGS,
127
- toolCallId: streamPart.toolCallId,
128
- delta: JSON.stringify(streamPart.args),
129
- parentMessageId,
130
- } as ToolCallArgsEvent);
131
-
132
- subscriber.next({
133
- type: EventType.TOOL_CALL_END,
134
- toolCallId: streamPart.toolCallId,
135
- parentMessageId,
136
- } as ToolCallEndEvent);
137
- },
138
- });
139
- })
140
- .catch(error => {
141
- console.error('error', error);
142
- // Handle error
143
- subscriber.error(error);
144
- });
145
-
146
- return () => {};
147
- });
148
- }
149
- }
150
-
151
- /**
152
- * Generates a UUID v4 that works in both browser and Node.js environments
153
- */
154
- export function generateUUID(): string {
155
- // Use crypto.randomUUID() if available (Node.js environment or modern browsers)
156
- if (typeof crypto !== 'undefined') {
157
- // Browser crypto API or Node.js crypto global
158
- if (typeof crypto.randomUUID === 'function') {
159
- return crypto.randomUUID();
160
- }
161
- // Fallback for older browsers
162
- if (typeof crypto.getRandomValues === 'function') {
163
- const buffer = new Uint8Array(16);
164
- crypto.getRandomValues(buffer);
165
- // Set version (4) and variant (8, 9, A, or B)
166
- buffer[6] = (buffer[6]! & 0x0f) | 0x40; // version 4
167
- buffer[8] = (buffer[8]! & 0x3f) | 0x80; // variant
168
-
169
- // Convert to hex string in UUID format
170
- let hex = '';
171
- for (let i = 0; i < 16; i++) {
172
- hex += buffer[i]!.toString(16).padStart(2, '0');
173
- // Add hyphens at standard positions
174
- if (i === 3 || i === 5 || i === 7 || i === 9) hex += '-';
175
- }
176
- return hex;
177
- }
178
- }
179
-
180
- // Last resort fallback (less secure but works everywhere)
181
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
182
- const r = (Math.random() * 16) | 0;
183
- const v = c === 'x' ? r : (r & 0x3) | 0x8;
184
- return v.toString(16);
185
- });
186
- }
187
-
188
- export function convertMessagesToMastraMessages(messages: Message[]): any[] {
189
- const result: any[] = [];
190
-
191
- for (const message of messages) {
192
- if (message.role === 'assistant') {
193
- const content: any[] = [];
194
-
195
- if (message.content) {
196
- content.push({ type: 'text', text: message.content });
197
- }
198
-
199
- for (const toolCall of message.toolCalls ?? []) {
200
- content.push({
201
- type: 'tool-call',
202
- toolCallId: toolCall.id,
203
- toolName: toolCall.function.name,
204
- args: JSON.parse(toolCall.function.arguments),
205
- });
206
- }
207
-
208
- // Always add the assistant message
209
- result.push({
210
- role: 'assistant',
211
- content: content.length > 0 ? content : message.content || '',
212
- });
213
-
214
- // If there are tool calls, add tool result messages for each
215
- if (message.toolCalls?.length) {
216
- for (const toolCall of message.toolCalls) {
217
- result.push({
218
- role: 'tool',
219
- content: [
220
- {
221
- type: 'tool-result',
222
- toolCallId: toolCall.id,
223
- toolName: toolCall.function.name,
224
- result: JSON.parse(toolCall.function.arguments),
225
- },
226
- ],
227
- });
228
- }
229
- }
230
- } else if (message.role === 'user') {
231
- result.push({
232
- role: 'user',
233
- content: message.content || '',
234
- });
235
- } else if (message.role === 'tool') {
236
- result.push({
237
- role: 'tool',
238
- content: [
239
- {
240
- type: 'tool-result',
241
- toolCallId: message.toolCallId,
242
- toolName: 'unknown',
243
- result: message.content || '',
244
- },
245
- ],
246
- });
247
- }
248
- }
249
-
250
- return result;
251
- }