@mastra/client-js 0.1.0-alpha.5 → 0.1.0-alpha.7

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/src/client.ts CHANGED
@@ -1,63 +1,12 @@
1
1
  import type { ClientOptions, CreateMemoryThreadParams, CreateMemoryThreadResponse, GetAgentResponse, GetLogParams, GetLogsParams, GetLogsResponse, GetMemoryThreadParams, GetMemoryThreadResponse, GetToolResponse, GetWorkflowResponse, RequestOptions, SaveMessageToMemoryParams, SaveMessageToMemoryResponse } from './types';
2
- import { Agent, MemoryThread, Tool, Workflow, Vector } from './resources';
2
+ import { Agent, MemoryThread, Tool, Workflow, Vector, BaseResource } from './resources';
3
3
 
4
- export class MastraClient {
5
- readonly baseUrl: string;
6
- private readonly retries: number;
7
- private readonly backoffMs: number;
8
- private readonly maxBackoffMs: number;
9
- private readonly headers: Record<string, string>;
4
+ export class MastraClient extends BaseResource {
10
5
 
11
6
  constructor(options: ClientOptions) {
12
- this.baseUrl = options.baseUrl.replace(/\/$/, '');
13
- this.retries = options.retries ?? 3;
14
- this.backoffMs = options.backoffMs ?? 300;
15
- this.maxBackoffMs = options.maxBackoffMs ?? 5000;
16
- this.headers = {
17
- 'Content-Type': 'application/json',
18
- ...options.headers,
19
- };
7
+ super(options);
20
8
  }
21
9
 
22
- /**
23
- * Makes an HTTP request to the Mastra API
24
- * @param path - API endpoint path
25
- * @param options - Request options including method, headers, and body
26
- * @returns Promise containing the API response
27
- * @throws Error if the request fails after all retries
28
- */
29
- async request(path: string, options: RequestOptions = {}): Promise<any> {
30
- const url = `${this.baseUrl}${path}`;
31
- let lastError: Error | null = null;
32
- let currentBackoff = this.backoffMs;
33
-
34
- for (let attempt = 0; attempt <= this.retries; attempt++) {
35
- try {
36
- const response = await fetch(url, {
37
- method: options.method ?? 'GET',
38
- headers: {
39
- ...this.headers,
40
- ...options.headers,
41
- },
42
- body: options.body ? JSON.stringify(options.body) : undefined,
43
- });
44
-
45
- if (!response.ok) {
46
- throw new Error(`HTTP error! status: ${response.status}`);
47
- }
48
-
49
- return await response.json();
50
- } catch (error) {
51
- lastError = error as Error;
52
- if (attempt === this.retries) break;
53
-
54
- await new Promise(resolve => setTimeout(resolve, currentBackoff));
55
- currentBackoff = Math.min(currentBackoff * 2, this.maxBackoffMs);
56
- }
57
- }
58
-
59
- throw lastError;
60
- }
61
10
 
62
11
  /**
63
12
  * Retrieves all available agents
@@ -73,10 +22,7 @@ export class MastraClient {
73
22
  * @returns Agent instance
74
23
  */
75
24
  public getAgent(agentId: string) {
76
- return new Agent(
77
- (path: string, options?: RequestOptions) => this.request(path, options),
78
- agentId
79
- );
25
+ return new Agent(this.options, agentId);
80
26
  }
81
27
 
82
28
  /**
@@ -103,10 +49,7 @@ export class MastraClient {
103
49
  * @returns MemoryThread instance
104
50
  */
105
51
  public getMemoryThread(threadId: string) {
106
- return new MemoryThread(
107
- (path: string, options?: RequestOptions) => this.request(path, options),
108
- threadId
109
- );
52
+ return new MemoryThread(this.options, threadId);
110
53
  }
111
54
 
112
55
  /**
@@ -143,10 +86,7 @@ export class MastraClient {
143
86
  * @returns Tool instance
144
87
  */
145
88
  public getTool(toolId: string) {
146
- return new Tool(
147
- (path: string, options?: RequestOptions) => this.request(path, options),
148
- toolId
149
- );
89
+ return new Tool(this.options, toolId);
150
90
  }
151
91
 
152
92
  /**
@@ -163,10 +103,7 @@ export class MastraClient {
163
103
  * @returns Workflow instance
164
104
  */
165
105
  public getWorkflow(workflowId: string) {
166
- return new Workflow(
167
- (path: string, options?: RequestOptions) => this.request(path, options),
168
- workflowId
169
- );
106
+ return new Workflow(this.options, workflowId);
170
107
  }
171
108
 
172
109
  /**
@@ -175,10 +112,7 @@ export class MastraClient {
175
112
  * @returns Vector instance
176
113
  */
177
114
  public getVector(vectorName: string) {
178
- return new Vector(
179
- (path: string, options?: RequestOptions) => this.request(path, options),
180
- vectorName
181
- );
115
+ return new Vector(this.options, vectorName);
182
116
  }
183
117
 
184
118
  /**
package/src/example.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { MastraClient } from "./client";
2
+
3
+
4
+ (async () => {
5
+ const client = new MastraClient({
6
+ baseUrl: 'http://localhost:4111',
7
+ });
8
+
9
+ try {
10
+ const agent = client.getAgent('weatherAgent');
11
+ const response = await agent.stream({
12
+ messages: [{
13
+ role: 'user',
14
+ content: 'Hello, world!',
15
+ id: '1',
16
+ createdAt: new Date(),
17
+ threadId: '1',
18
+ type: 'text',
19
+ }],
20
+ })
21
+
22
+
23
+ const reader = response?.getReader()
24
+ while (true) {
25
+ const { done, value } = await reader.read();
26
+ if (done) break;
27
+
28
+ const text = new TextDecoder().decode(value);
29
+ console.log(text);
30
+ }
31
+ } catch (error) {
32
+ console.error(error);
33
+ }
34
+ })()
package/src/index.test.ts CHANGED
@@ -7,26 +7,43 @@ global.fetch = vi.fn();
7
7
 
8
8
  describe('MastraClient Resources', () => {
9
9
  let client: MastraClient;
10
+ const clientOptions = {
11
+ baseUrl: 'http://localhost:4111',
12
+ headers: {
13
+ 'Authorization': 'Bearer test-key'
14
+ }
15
+ };
10
16
 
11
17
  // Helper to mock successful API responses
12
- const mockFetchResponse = (data: any) => {
13
- (global.fetch as any).mockResolvedValueOnce({
14
- ok: true,
15
- json: async () => data
16
- });
18
+ const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
19
+ if (options.isStream) {
20
+ const stream = new ReadableStream({
21
+ start(controller) {
22
+ controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
23
+ controller.close();
24
+ }
25
+ });
26
+ (global.fetch as any).mockResolvedValueOnce({
27
+ ok: true,
28
+ headers: {
29
+ get: (name: string) => name === 'Content-Type' ? 'text/event-stream' : null
30
+ },
31
+ body: stream
32
+ });
33
+ } else {
34
+ (global.fetch as any).mockResolvedValueOnce({
35
+ ok: true,
36
+ headers: {
37
+ get: (name: string) => name === 'Content-Type' ? 'application/json' : null
38
+ },
39
+ json: async () => data
40
+ });
41
+ }
17
42
  };
18
43
 
19
44
  beforeEach(() => {
20
- // Reset mocks
21
45
  vi.clearAllMocks();
22
-
23
- // Create fresh client for each test
24
- client = new MastraClient({
25
- baseUrl: 'http://localhost:4111',
26
- headers: {
27
- 'Authorization': 'Bearer test-key'
28
- }
29
- });
46
+ client = new MastraClient(clientOptions);
30
47
  });
31
48
 
32
49
  describe('Vector Resource', () => {
@@ -48,8 +65,10 @@ describe('MastraClient Resources', () => {
48
65
  const result = await vector.details('test-index');
49
66
  expect(result).toEqual(mockResponse);
50
67
  expect(global.fetch).toHaveBeenCalledWith(
51
- `${client.baseUrl}/api/vector/test-vector/indexes/test-index`,
52
- expect.any(Object)
68
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
69
+ expect.objectContaining({
70
+ headers: expect.objectContaining(clientOptions.headers)
71
+ })
53
72
  );
54
73
  });
55
74
 
@@ -58,8 +77,11 @@ describe('MastraClient Resources', () => {
58
77
  const result = await vector.delete('test-index');
59
78
  expect(result).toEqual({ success: true });
60
79
  expect(global.fetch).toHaveBeenCalledWith(
61
- `${client.baseUrl}/api/vector/test-vector/indexes/test-index`,
62
- expect.objectContaining({ method: 'DELETE' })
80
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
81
+ expect.objectContaining({
82
+ method: 'DELETE',
83
+ headers: expect.objectContaining(clientOptions.headers)
84
+ })
63
85
  );
64
86
  });
65
87
 
@@ -69,8 +91,10 @@ describe('MastraClient Resources', () => {
69
91
  const result = await vector.getIndexes();
70
92
  expect(result).toEqual(mockResponse);
71
93
  expect(global.fetch).toHaveBeenCalledWith(
72
- `${client.baseUrl}/api/vector/test-vector/indexes`,
73
- expect.any(Object)
94
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes`,
95
+ expect.objectContaining({
96
+ headers: expect.objectContaining(clientOptions.headers)
97
+ })
74
98
  );
75
99
  });
76
100
 
@@ -83,9 +107,10 @@ describe('MastraClient Resources', () => {
83
107
  });
84
108
  expect(result).toEqual({ success: true });
85
109
  expect(global.fetch).toHaveBeenCalledWith(
86
- `${client.baseUrl}/api/vector/test-vector/create-index`,
110
+ `${clientOptions.baseUrl}/api/vector/test-vector/create-index`,
87
111
  expect.objectContaining({
88
112
  method: 'POST',
113
+ headers: expect.objectContaining(clientOptions.headers),
89
114
  body: JSON.stringify({
90
115
  indexName: 'test-index',
91
116
  dimension: 128,
@@ -106,9 +131,10 @@ describe('MastraClient Resources', () => {
106
131
  });
107
132
  expect(result).toEqual(mockResponse);
108
133
  expect(global.fetch).toHaveBeenCalledWith(
109
- `${client.baseUrl}/api/vector/test-vector/upsert`,
134
+ `${clientOptions.baseUrl}/api/vector/test-vector/upsert`,
110
135
  expect.objectContaining({
111
136
  method: 'POST',
137
+ headers: expect.objectContaining(clientOptions.headers),
112
138
  body: JSON.stringify({
113
139
  indexName: 'test-index',
114
140
  vectors: [[1, 2], [3, 4]],
@@ -138,9 +164,10 @@ describe('MastraClient Resources', () => {
138
164
  });
139
165
  expect(result).toEqual(mockResponse);
140
166
  expect(global.fetch).toHaveBeenCalledWith(
141
- `${client.baseUrl}/api/vector/test-vector/query`,
167
+ `${clientOptions.baseUrl}/api/vector/test-vector/query`,
142
168
  expect.objectContaining({
143
169
  method: 'POST',
170
+ headers: expect.objectContaining(clientOptions.headers),
144
171
  body: JSON.stringify({
145
172
  indexName: 'test-index',
146
173
  queryVector: [1, 2],
@@ -170,8 +197,10 @@ describe('MastraClient Resources', () => {
170
197
  const result = await client.getAgents();
171
198
  expect(result).toEqual(mockResponse);
172
199
  expect(global.fetch).toHaveBeenCalledWith(
173
- `${client.baseUrl}/api/agents`,
174
- expect.any(Object)
200
+ `${clientOptions.baseUrl}/api/agents`,
201
+ expect.objectContaining({
202
+ headers: expect.objectContaining(clientOptions.headers)
203
+ })
175
204
  );
176
205
  });
177
206
 
@@ -187,8 +216,10 @@ describe('MastraClient Resources', () => {
187
216
  const result = await agent.details();
188
217
  expect(result).toEqual(mockResponse);
189
218
  expect(global.fetch).toHaveBeenCalledWith(
190
- `${client.baseUrl}/api/agents/test-agent`,
191
- expect.any(Object)
219
+ `${clientOptions.baseUrl}/api/agents/test-agent`,
220
+ expect.objectContaining({
221
+ headers: expect.objectContaining(clientOptions.headers)
222
+ })
192
223
  );
193
224
  });
194
225
 
@@ -206,9 +237,10 @@ describe('MastraClient Resources', () => {
206
237
  });
207
238
  expect(result).toEqual(mockResponse);
208
239
  expect(global.fetch).toHaveBeenCalledWith(
209
- `${client.baseUrl}/api/agents/test-agent/generate`,
240
+ `${clientOptions.baseUrl}/api/agents/test-agent/generate`,
210
241
  expect.objectContaining({
211
242
  method: 'POST',
243
+ headers: expect.objectContaining(clientOptions.headers),
212
244
  body: JSON.stringify({
213
245
  messages: [],
214
246
  threadId: 'test-thread',
@@ -220,31 +252,29 @@ describe('MastraClient Resources', () => {
220
252
  });
221
253
 
222
254
  it('should stream responses', async () => {
223
- const mockResponse = {
224
- stream: true,
225
- chunks: ['chunk1', 'chunk2']
226
- };
227
- mockFetchResponse(mockResponse);
228
- const result = await agent.stream({
229
- messages: [],
230
- threadId: 'test-thread',
231
- resourceid: 'test-resource',
232
- output: undefined
255
+ const mockChunk = { content: 'test response' };
256
+ mockFetchResponse(mockChunk, { isStream: true });
257
+
258
+ const response = await agent.stream({
259
+ messages: [{
260
+ role: 'user',
261
+ content: 'test',
262
+ id: '1',
263
+ createdAt: new Date(),
264
+ threadId: '1',
265
+ type: 'text'
266
+ }]
233
267
  });
234
- expect(result).toEqual(mockResponse);
235
- expect(global.fetch).toHaveBeenCalledWith(
236
- `${client.baseUrl}/api/agents/test-agent/generate`,
237
- expect.objectContaining({
238
- method: 'POST',
239
- body: JSON.stringify({
240
- messages: [],
241
- threadId: 'test-thread',
242
- resourceid: 'test-resource',
243
- output: undefined,
244
- stream: true
245
- })
246
- })
247
- );
268
+
269
+ expect(response).toBeInstanceOf(ReadableStream);
270
+ const reader = response?.getReader();
271
+ expect(reader).toBeDefined();
272
+
273
+ if (reader) {
274
+ const { value, done } = await reader.read();
275
+ expect(done).toBe(false);
276
+ expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
277
+ }
248
278
  });
249
279
 
250
280
  it('should get agent tool', async () => {
@@ -256,8 +286,10 @@ describe('MastraClient Resources', () => {
256
286
  const result = await agent.getTool('tool1');
257
287
  expect(result).toEqual(mockResponse);
258
288
  expect(global.fetch).toHaveBeenCalledWith(
259
- `${client.baseUrl}/api/agents/test-agent/tools/tool1`,
260
- expect.any(Object)
289
+ `${clientOptions.baseUrl}/api/agents/test-agent/tools/tool1`,
290
+ expect.objectContaining({
291
+ headers: expect.objectContaining(clientOptions.headers)
292
+ })
261
293
  );
262
294
  });
263
295
 
@@ -270,8 +302,10 @@ describe('MastraClient Resources', () => {
270
302
  const result = await agent.evals();
271
303
  expect(result).toEqual(mockResponse);
272
304
  expect(global.fetch).toHaveBeenCalledWith(
273
- `${client.baseUrl}/api/agents/test-agent/evals`,
274
- expect.any(Object)
305
+ `${clientOptions.baseUrl}/api/agents/test-agent/evals`,
306
+ expect.objectContaining({
307
+ headers: expect.objectContaining(clientOptions.headers)
308
+ })
275
309
  );
276
310
  });
277
311
 
@@ -284,8 +318,10 @@ describe('MastraClient Resources', () => {
284
318
  const result = await agent.liveEvals();
285
319
  expect(result).toEqual(mockResponse);
286
320
  expect(global.fetch).toHaveBeenCalledWith(
287
- `${client.baseUrl}/api/agents/test-agent/evals/live`,
288
- expect.any(Object)
321
+ `${clientOptions.baseUrl}/api/agents/test-agent/evals/live`,
322
+ expect.objectContaining({
323
+ headers: expect.objectContaining(clientOptions.headers)
324
+ })
289
325
  );
290
326
  });
291
327
  });
@@ -309,8 +345,10 @@ describe('MastraClient Resources', () => {
309
345
  const result = await memoryThread.get();
310
346
  expect(result).toEqual(mockResponse);
311
347
  expect(global.fetch).toHaveBeenCalledWith(
312
- `${client.baseUrl}/api/memory/threads/test-thread`,
313
- expect.any(Object)
348
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
349
+ expect.objectContaining({
350
+ headers: expect.objectContaining(clientOptions.headers)
351
+ })
314
352
  );
315
353
  });
316
354
 
@@ -329,9 +367,10 @@ describe('MastraClient Resources', () => {
329
367
  });
330
368
  expect(result).toEqual(mockResponse);
331
369
  expect(global.fetch).toHaveBeenCalledWith(
332
- `${client.baseUrl}/api/memory/threads/test-thread`,
370
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
333
371
  expect.objectContaining({
334
- method: 'PATCH'
372
+ method: 'PATCH',
373
+ headers: expect.objectContaining(clientOptions.headers)
335
374
  })
336
375
  );
337
376
  });
@@ -342,8 +381,11 @@ describe('MastraClient Resources', () => {
342
381
  const result = await memoryThread.delete();
343
382
  expect(result).toEqual(mockResponse);
344
383
  expect(global.fetch).toHaveBeenCalledWith(
345
- `${client.baseUrl}/api/memory/threads/test-thread`,
346
- expect.objectContaining({ method: 'DELETE' })
384
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
385
+ expect.objectContaining({
386
+ method: 'DELETE',
387
+ headers: expect.objectContaining(clientOptions.headers)
388
+ })
347
389
  );
348
390
  });
349
391
 
@@ -353,8 +395,10 @@ describe('MastraClient Resources', () => {
353
395
  const result = await client.getMemoryStatus();
354
396
  expect(result).toEqual(mockResponse);
355
397
  expect(global.fetch).toHaveBeenCalledWith(
356
- `${client.baseUrl}/api/memory/status`,
357
- expect.any(Object)
398
+ `${clientOptions.baseUrl}/api/memory/status`,
399
+ expect.objectContaining({
400
+ headers: expect.objectContaining(clientOptions.headers)
401
+ })
358
402
  );
359
403
  });
360
404
 
@@ -371,9 +415,10 @@ describe('MastraClient Resources', () => {
371
415
  const result = await client.saveMessageToMemory({ messages });
372
416
  expect(result).toEqual(messages);
373
417
  expect(global.fetch).toHaveBeenCalledWith(
374
- `${client.baseUrl}/api/memory/save-messages`,
418
+ `${clientOptions.baseUrl}/api/memory/save-messages`,
375
419
  expect.objectContaining({
376
420
  method: 'POST',
421
+ headers: expect.objectContaining(clientOptions.headers),
377
422
  body: JSON.stringify({ messages })
378
423
  })
379
424
  );
@@ -400,8 +445,10 @@ describe('MastraClient Resources', () => {
400
445
  const result = await tool.details();
401
446
  expect(result).toEqual(mockResponse);
402
447
  expect(global.fetch).toHaveBeenCalledWith(
403
- `${client.baseUrl}/api/tools/test-tool`,
404
- expect.any(Object)
448
+ `${clientOptions.baseUrl}/api/tools/test-tool`,
449
+ expect.objectContaining({
450
+ headers: expect.objectContaining(clientOptions.headers)
451
+ })
405
452
  );
406
453
  });
407
454
 
@@ -414,9 +461,10 @@ describe('MastraClient Resources', () => {
414
461
  const result = await tool.execute({ input: 'test' });
415
462
  expect(result).toEqual(mockResponse);
416
463
  expect(global.fetch).toHaveBeenCalledWith(
417
- `${client.baseUrl}/api/tools/test-tool/execute`,
464
+ `${clientOptions.baseUrl}/api/tools/test-tool/execute`,
418
465
  expect.objectContaining({
419
466
  method: 'POST',
467
+ headers: expect.objectContaining(clientOptions.headers),
420
468
  body: JSON.stringify({ input: 'test' })
421
469
  })
422
470
  );
@@ -444,8 +492,10 @@ describe('MastraClient Resources', () => {
444
492
  const result = await workflow.details();
445
493
  expect(result).toEqual(mockResponse);
446
494
  expect(global.fetch).toHaveBeenCalledWith(
447
- `${client.baseUrl}/api/workflows/test-workflow`,
448
- expect.any(Object)
495
+ `${clientOptions.baseUrl}/api/workflows/test-workflow`,
496
+ expect.objectContaining({
497
+ headers: expect.objectContaining(clientOptions.headers)
498
+ })
449
499
  );
450
500
  });
451
501
 
@@ -458,9 +508,10 @@ describe('MastraClient Resources', () => {
458
508
  const result = await workflow.execute({ trigger: 'test' });
459
509
  expect(result).toEqual(mockResponse);
460
510
  expect(global.fetch).toHaveBeenCalledWith(
461
- `${client.baseUrl}/api/workflows/test-workflow/execute`,
511
+ `${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
462
512
  expect.objectContaining({
463
513
  method: 'POST',
514
+ headers: expect.objectContaining(clientOptions.headers),
464
515
  body: JSON.stringify({ trigger: 'test' })
465
516
  })
466
517
  );
@@ -475,6 +526,9 @@ describe('MastraClient Resources', () => {
475
526
  .mockRejectedValueOnce(new Error('Network error'))
476
527
  .mockResolvedValueOnce({
477
528
  ok: true,
529
+ headers: {
530
+ get: () => 'application/json'
531
+ },
478
532
  json: async () => ({ success: true })
479
533
  });
480
534
 
@@ -509,6 +563,9 @@ describe('MastraClient Resources', () => {
509
563
  .mockRejectedValueOnce(new Error('Network error'))
510
564
  .mockResolvedValueOnce({
511
565
  ok: true,
566
+ headers: {
567
+ get: () => 'application/json'
568
+ },
512
569
  json: async () => ({ success: true })
513
570
  });
514
571
 
@@ -3,7 +3,7 @@ import type {
3
3
  GetAgentResponse,
4
4
  GetEvalsByAgentIdResponse,
5
5
  GetToolResponse,
6
- RequestFunction,
6
+ ClientOptions,
7
7
  StreamParams,
8
8
  } from '../types';
9
9
  import type {
@@ -11,13 +11,18 @@ import type {
11
11
  StreamReturn,
12
12
  } from '@mastra/core';
13
13
  import type { JSONSchema7 } from 'json-schema';
14
+ import { ZodSchema } from "zod";
15
+ import { zodToJsonSchema } from 'zod-to-json-schema';
16
+ import { BaseResource } from './base';
14
17
 
15
- export class AgentTool {
18
+ export class AgentTool extends BaseResource {
16
19
  constructor(
17
- private request: RequestFunction,
20
+ options: ClientOptions,
18
21
  private agentId: string,
19
22
  private toolId: string
20
- ) { }
23
+ ) {
24
+ super(options);
25
+ }
21
26
 
22
27
  /**
23
28
  * Executes a specific tool for an agent
@@ -32,11 +37,13 @@ export class AgentTool {
32
37
  }
33
38
  }
34
39
 
35
- export class Agent {
40
+ export class Agent extends BaseResource {
36
41
  constructor(
37
- private request: RequestFunction,
42
+ options: ClientOptions,
38
43
  private agentId: string
39
- ) { }
44
+ ) {
45
+ super(options);
46
+ }
40
47
 
41
48
  /**
42
49
  * Retrieves details about the agent
@@ -51,10 +58,15 @@ export class Agent {
51
58
  * @param params - Generation parameters including prompt
52
59
  * @returns Promise containing the generated response
53
60
  */
54
- generate<T extends JSONSchema7 | undefined = undefined>(params: GenerateParams<T>): Promise<GenerateReturn<T>> {
61
+ generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: GenerateParams<T>): Promise<GenerateReturn<T>> {
62
+ const processedParams = {
63
+ ...params,
64
+ output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output
65
+ };
66
+
55
67
  return this.request(`/api/agents/${this.agentId}/generate`, {
56
68
  method: 'POST',
57
- body: params,
69
+ body: processedParams,
58
70
  });
59
71
  }
60
72
 
@@ -63,11 +75,17 @@ export class Agent {
63
75
  * @param params - Stream parameters including prompt
64
76
  * @returns Promise containing the streamed response
65
77
  */
66
- stream<T extends JSONSchema7 | undefined = undefined>(params: StreamParams<T>): Promise<StreamReturn<T>> {
67
- return this.request(`/api/agents/${this.agentId}/generate`, {
78
+ stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>) {
79
+ const processedParams = {
80
+ ...params,
81
+ output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
82
+ stream: true
83
+ };
84
+
85
+ return this.request(`/api/agents/${this.agentId}/stream`, {
68
86
  method: 'POST',
69
- body: { ...params, stream: true },
70
- });
87
+ body: processedParams,
88
+ }) as T extends undefined ? Promise<ReadableStream<Uint8Array>> : Promise<Response>;
71
89
  }
72
90
 
73
91
  /**