@mastra/client-js 0.1.0-alpha.6 → 0.1.0-alpha.8
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.d.mts +64 -64
- package/dist/index.mjs +142 -131
- package/package.json +1 -1
- package/src/client.ts +8 -74
- package/src/example.ts +47 -0
- package/src/index.test.ts +130 -73
- package/src/resources/agent.ts +16 -11
- package/src/resources/base.ts +68 -0
- package/src/resources/index.ts +3 -2
- package/src/resources/memory-thread.ts +11 -5
- package/src/resources/tool.ts +7 -4
- package/src/resources/vector.ts +7 -4
- package/src/resources/workflow.ts +7 -4
- package/src/types.ts +6 -0
package/src/example.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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?.body?.getReader()
|
|
24
|
+
const decoder = new TextDecoder()
|
|
25
|
+
let buffer = ''
|
|
26
|
+
|
|
27
|
+
while (true) {
|
|
28
|
+
if (!reader) break;
|
|
29
|
+
const { value, done } = await reader.read()
|
|
30
|
+
if (done) break;
|
|
31
|
+
|
|
32
|
+
const chunk = decoder.decode(value);
|
|
33
|
+
buffer += chunk;
|
|
34
|
+
|
|
35
|
+
console.log(buffer);
|
|
36
|
+
|
|
37
|
+
const matches = buffer.matchAll(/0:"([^"]*)"/g);
|
|
38
|
+
|
|
39
|
+
for (const match of matches) {
|
|
40
|
+
const content = match[1];
|
|
41
|
+
process.stdout.write(`${content}\n`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error);
|
|
46
|
+
}
|
|
47
|
+
})()
|
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
|
-
(
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
`${
|
|
52
|
-
expect.
|
|
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
|
-
`${
|
|
62
|
-
expect.objectContaining({
|
|
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
|
-
`${
|
|
73
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
174
|
-
expect.
|
|
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
|
-
`${
|
|
191
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
235
|
-
expect(
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
stream: true
|
|
245
|
-
})
|
|
246
|
-
})
|
|
247
|
-
);
|
|
268
|
+
|
|
269
|
+
expect(response.body).toBeInstanceOf(ReadableStream);
|
|
270
|
+
const reader = response?.body?.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
|
-
`${
|
|
260
|
-
expect.
|
|
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
|
-
`${
|
|
274
|
-
expect.
|
|
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
|
-
`${
|
|
288
|
-
expect.
|
|
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
|
-
`${
|
|
313
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
346
|
-
expect.objectContaining({
|
|
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
|
-
`${
|
|
357
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
404
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
-
`${
|
|
448
|
-
expect.
|
|
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
|
-
`${
|
|
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
|
|
package/src/resources/agent.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type {
|
|
|
3
3
|
GetAgentResponse,
|
|
4
4
|
GetEvalsByAgentIdResponse,
|
|
5
5
|
GetToolResponse,
|
|
6
|
-
|
|
6
|
+
ClientOptions,
|
|
7
7
|
StreamParams,
|
|
8
8
|
} from '../types';
|
|
9
9
|
import type {
|
|
@@ -13,13 +13,16 @@ import type {
|
|
|
13
13
|
import type { JSONSchema7 } from 'json-schema';
|
|
14
14
|
import { ZodSchema } from "zod";
|
|
15
15
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
16
|
+
import { BaseResource } from './base';
|
|
16
17
|
|
|
17
|
-
export class AgentTool {
|
|
18
|
+
export class AgentTool extends BaseResource {
|
|
18
19
|
constructor(
|
|
19
|
-
|
|
20
|
+
options: ClientOptions,
|
|
20
21
|
private agentId: string,
|
|
21
22
|
private toolId: string
|
|
22
|
-
) {
|
|
23
|
+
) {
|
|
24
|
+
super(options);
|
|
25
|
+
}
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* Executes a specific tool for an agent
|
|
@@ -34,11 +37,13 @@ export class AgentTool {
|
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
export class Agent {
|
|
40
|
+
export class Agent extends BaseResource {
|
|
38
41
|
constructor(
|
|
39
|
-
|
|
42
|
+
options: ClientOptions,
|
|
40
43
|
private agentId: string
|
|
41
|
-
) {
|
|
44
|
+
) {
|
|
45
|
+
super(options);
|
|
46
|
+
}
|
|
42
47
|
|
|
43
48
|
/**
|
|
44
49
|
* Retrieves details about the agent
|
|
@@ -70,17 +75,17 @@ export class Agent {
|
|
|
70
75
|
* @param params - Stream parameters including prompt
|
|
71
76
|
* @returns Promise containing the streamed response
|
|
72
77
|
*/
|
|
73
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<
|
|
78
|
+
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response> {
|
|
74
79
|
const processedParams = {
|
|
75
80
|
...params,
|
|
76
81
|
output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
|
|
77
|
-
stream: true
|
|
78
82
|
};
|
|
79
83
|
|
|
80
|
-
return this.request(`/api/agents/${this.agentId}/
|
|
84
|
+
return this.request(`/api/agents/${this.agentId}/stream`, {
|
|
81
85
|
method: 'POST',
|
|
82
86
|
body: processedParams,
|
|
83
|
-
|
|
87
|
+
stream: true
|
|
88
|
+
})
|
|
84
89
|
}
|
|
85
90
|
|
|
86
91
|
/**
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { RequestFunction, RequestOptions, ClientOptions } from '../types';
|
|
2
|
+
|
|
3
|
+
export class BaseResource {
|
|
4
|
+
readonly options: ClientOptions
|
|
5
|
+
|
|
6
|
+
constructor(options: ClientOptions) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Makes an HTTP request to the API with retries and exponential backoff
|
|
12
|
+
* @param path - The API endpoint path
|
|
13
|
+
* @param options - Optional request configuration
|
|
14
|
+
* @returns Promise containing the response data
|
|
15
|
+
*/
|
|
16
|
+
public async request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
|
17
|
+
let lastError: Error | null = null;
|
|
18
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1000, headers = {} } = this.options;
|
|
19
|
+
|
|
20
|
+
let delay = backoffMs;
|
|
21
|
+
|
|
22
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
23
|
+
try {
|
|
24
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
25
|
+
...options,
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json',
|
|
28
|
+
...headers,
|
|
29
|
+
...options.headers,
|
|
30
|
+
},
|
|
31
|
+
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
const errorBody = await response.text();
|
|
36
|
+
let errorMessage = `HTTP error! status: ${response.status}`;
|
|
37
|
+
try {
|
|
38
|
+
const errorJson = JSON.parse(errorBody);
|
|
39
|
+
errorMessage += ` - ${JSON.stringify(errorJson)}`;
|
|
40
|
+
} catch {
|
|
41
|
+
if (errorBody) {
|
|
42
|
+
errorMessage += ` - ${errorBody}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(errorMessage);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (options.stream) {
|
|
49
|
+
return response as unknown as T;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const data = await response.json();
|
|
53
|
+
return data as T;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
lastError = error as Error;
|
|
56
|
+
|
|
57
|
+
if (attempt === retries) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
62
|
+
delay = Math.min(delay * 2, maxBackoffMs);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw lastError || new Error('Request failed');
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/resources/index.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import type { StorageThreadType } from '@mastra/core
|
|
1
|
+
import type { StorageThreadType } from '@mastra/core';
|
|
2
2
|
import type {
|
|
3
|
+
CreateMemoryThreadParams,
|
|
3
4
|
GetMemoryThreadMessagesResponse,
|
|
5
|
+
GetMemoryThreadResponse,
|
|
6
|
+
ClientOptions,
|
|
7
|
+
SaveMessageToMemoryParams,
|
|
4
8
|
UpdateMemoryThreadParams,
|
|
5
|
-
RequestFunction
|
|
6
9
|
} from '../types';
|
|
10
|
+
import { BaseResource } from './base';
|
|
7
11
|
|
|
8
|
-
export class MemoryThread {
|
|
12
|
+
export class MemoryThread extends BaseResource {
|
|
9
13
|
constructor(
|
|
10
|
-
|
|
14
|
+
options: ClientOptions,
|
|
11
15
|
private threadId: string
|
|
12
|
-
) {
|
|
16
|
+
) {
|
|
17
|
+
super(options);
|
|
18
|
+
}
|
|
13
19
|
|
|
14
20
|
/**
|
|
15
21
|
* Retrieves the memory thread details
|
package/src/resources/tool.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type { GetToolResponse,
|
|
1
|
+
import type { GetToolResponse, ClientOptions } from '../types';
|
|
2
|
+
import { BaseResource } from './base';
|
|
2
3
|
|
|
3
|
-
export class Tool {
|
|
4
|
+
export class Tool extends BaseResource {
|
|
4
5
|
constructor(
|
|
5
|
-
|
|
6
|
+
options: ClientOptions,
|
|
6
7
|
private toolId: string
|
|
7
|
-
) {
|
|
8
|
+
) {
|
|
9
|
+
super(options);
|
|
10
|
+
}
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Retrieves details about the tool
|
package/src/resources/vector.ts
CHANGED
|
@@ -3,15 +3,18 @@ import type {
|
|
|
3
3
|
GetVectorIndexResponse,
|
|
4
4
|
QueryVectorParams,
|
|
5
5
|
QueryVectorResponse,
|
|
6
|
+
ClientOptions,
|
|
6
7
|
UpsertVectorParams,
|
|
7
|
-
RequestFunction
|
|
8
8
|
} from '../types';
|
|
9
|
+
import { BaseResource } from './base';
|
|
9
10
|
|
|
10
|
-
export class Vector {
|
|
11
|
+
export class Vector extends BaseResource {
|
|
11
12
|
constructor(
|
|
12
|
-
|
|
13
|
+
options: ClientOptions,
|
|
13
14
|
private vectorName: string
|
|
14
|
-
) {
|
|
15
|
+
) {
|
|
16
|
+
super(options);
|
|
17
|
+
}
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
20
|
* Retrieves details about a specific vector index
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type { GetWorkflowResponse,
|
|
1
|
+
import type { GetWorkflowResponse, ClientOptions } from '../types';
|
|
2
|
+
import { BaseResource } from './base';
|
|
2
3
|
|
|
3
|
-
export class Workflow {
|
|
4
|
+
export class Workflow extends BaseResource {
|
|
4
5
|
constructor(
|
|
5
|
-
|
|
6
|
+
options: ClientOptions,
|
|
6
7
|
private workflowId: string
|
|
7
|
-
) {
|
|
8
|
+
) {
|
|
9
|
+
super(options);
|
|
10
|
+
}
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Retrieves details about the workflow
|