@mastra/client-js 0.0.0-storage-20250225005900 → 0.0.0-stream-vnext-usage-20250908171242
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/CHANGELOG.md +2639 -3
- package/LICENSE.md +15 -0
- package/README.md +12 -6
- package/dist/client.d.ts +280 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +3279 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3273 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent-builder.d.ts +161 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +155 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +13 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/legacy-workflow.d.ts +87 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +27 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/network.d.ts +30 -0
- package/dist/resources/network.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +19 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +23 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +42 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +48 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +169 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/types.d.ts +461 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +7 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/package.json +50 -21
- package/.turbo/turbo-build.log +0 -16
- package/LICENSE +0 -44
- package/dist/index.d.mts +0 -405
- package/dist/index.mjs +0 -487
- package/eslint.config.js +0 -6
- package/src/client.ts +0 -205
- package/src/example.ts +0 -43
- package/src/index.test.ts +0 -597
- package/src/index.ts +0 -2
- package/src/resources/agent.ts +0 -116
- package/src/resources/base.ts +0 -68
- package/src/resources/index.ts +0 -6
- package/src/resources/memory-thread.ts +0 -60
- package/src/resources/tool.ts +0 -32
- package/src/resources/vector.ts +0 -83
- package/src/resources/workflow.ts +0 -68
- package/src/types.ts +0 -163
- package/tsconfig.json +0 -5
- package/vitest.config.js +0 -8
package/src/example.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { MastraClient } from './client';
|
|
2
|
-
|
|
3
|
-
(async () => {
|
|
4
|
-
const client = new MastraClient({
|
|
5
|
-
baseUrl: 'http://localhost:4111',
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
const agent = client.getAgent('weatherAgent');
|
|
10
|
-
const response = await agent.stream({
|
|
11
|
-
messages: [
|
|
12
|
-
{
|
|
13
|
-
role: 'user',
|
|
14
|
-
content: 'Hello, world!',
|
|
15
|
-
},
|
|
16
|
-
],
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const reader = response?.body?.getReader();
|
|
20
|
-
const decoder = new TextDecoder();
|
|
21
|
-
let buffer = '';
|
|
22
|
-
|
|
23
|
-
while (true) {
|
|
24
|
-
if (!reader) break;
|
|
25
|
-
const { value, done } = await reader.read();
|
|
26
|
-
if (done) break;
|
|
27
|
-
|
|
28
|
-
const chunk = decoder.decode(value);
|
|
29
|
-
buffer += chunk;
|
|
30
|
-
|
|
31
|
-
console.log(buffer);
|
|
32
|
-
|
|
33
|
-
const matches = buffer.matchAll(/0:"([^"]*)"/g);
|
|
34
|
-
|
|
35
|
-
for (const match of matches) {
|
|
36
|
-
const content = match[1];
|
|
37
|
-
process.stdout.write(`${content}\n`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
} catch (error) {
|
|
41
|
-
console.error(error);
|
|
42
|
-
}
|
|
43
|
-
})();
|
package/src/index.test.ts
DELETED
|
@@ -1,597 +0,0 @@
|
|
|
1
|
-
import type { MessageType } from '@mastra/core';
|
|
2
|
-
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { MastraClient } from './client';
|
|
5
|
-
|
|
6
|
-
// Mock fetch globally
|
|
7
|
-
global.fetch = vi.fn();
|
|
8
|
-
|
|
9
|
-
describe('MastraClient Resources', () => {
|
|
10
|
-
let client: MastraClient;
|
|
11
|
-
const clientOptions = {
|
|
12
|
-
baseUrl: 'http://localhost:4111',
|
|
13
|
-
headers: {
|
|
14
|
-
Authorization: 'Bearer test-key',
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// Helper to mock successful API responses
|
|
19
|
-
const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
|
|
20
|
-
if (options.isStream) {
|
|
21
|
-
const stream = new ReadableStream({
|
|
22
|
-
start(controller) {
|
|
23
|
-
controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
|
|
24
|
-
controller.close();
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
(global.fetch as any).mockResolvedValueOnce({
|
|
28
|
-
ok: true,
|
|
29
|
-
headers: {
|
|
30
|
-
get: (name: string) => (name === 'Content-Type' ? 'text/event-stream' : null),
|
|
31
|
-
},
|
|
32
|
-
body: stream,
|
|
33
|
-
});
|
|
34
|
-
} else {
|
|
35
|
-
(global.fetch as any).mockResolvedValueOnce({
|
|
36
|
-
ok: true,
|
|
37
|
-
headers: {
|
|
38
|
-
get: (name: string) => (name === 'Content-Type' ? 'application/json' : null),
|
|
39
|
-
},
|
|
40
|
-
json: async () => data,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
beforeEach(() => {
|
|
46
|
-
vi.clearAllMocks();
|
|
47
|
-
client = new MastraClient(clientOptions);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('Vector Resource', () => {
|
|
51
|
-
const vectorName = 'test-vector';
|
|
52
|
-
let vector: ReturnType<typeof client.getVector>;
|
|
53
|
-
|
|
54
|
-
beforeEach(() => {
|
|
55
|
-
vector = client.getVector(vectorName);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should get vector index details', async () => {
|
|
59
|
-
const mockResponse = {
|
|
60
|
-
dimension: 128,
|
|
61
|
-
metric: 'cosine',
|
|
62
|
-
count: 1000,
|
|
63
|
-
};
|
|
64
|
-
mockFetchResponse(mockResponse);
|
|
65
|
-
|
|
66
|
-
const result = await vector.details('test-index');
|
|
67
|
-
expect(result).toEqual(mockResponse);
|
|
68
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
69
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
|
|
70
|
-
expect.objectContaining({
|
|
71
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
72
|
-
}),
|
|
73
|
-
);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should delete vector index', async () => {
|
|
77
|
-
mockFetchResponse({ success: true });
|
|
78
|
-
const result = await vector.delete('test-index');
|
|
79
|
-
expect(result).toEqual({ success: true });
|
|
80
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
81
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
|
|
82
|
-
expect.objectContaining({
|
|
83
|
-
method: 'DELETE',
|
|
84
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
85
|
-
}),
|
|
86
|
-
);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('should get all indexes', async () => {
|
|
90
|
-
const mockResponse = { indexes: ['index1', 'index2'] };
|
|
91
|
-
mockFetchResponse(mockResponse);
|
|
92
|
-
const result = await vector.getIndexes();
|
|
93
|
-
expect(result).toEqual(mockResponse);
|
|
94
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
95
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes`,
|
|
96
|
-
expect.objectContaining({
|
|
97
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
98
|
-
}),
|
|
99
|
-
);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should create vector index with all parameters', async () => {
|
|
103
|
-
mockFetchResponse({ success: true });
|
|
104
|
-
const result = await vector.createIndex({
|
|
105
|
-
indexName: 'test-index',
|
|
106
|
-
dimension: 128,
|
|
107
|
-
metric: 'cosine',
|
|
108
|
-
});
|
|
109
|
-
expect(result).toEqual({ success: true });
|
|
110
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
111
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/create-index`,
|
|
112
|
-
expect.objectContaining({
|
|
113
|
-
method: 'POST',
|
|
114
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
115
|
-
body: JSON.stringify({
|
|
116
|
-
indexName: 'test-index',
|
|
117
|
-
dimension: 128,
|
|
118
|
-
metric: 'cosine',
|
|
119
|
-
}),
|
|
120
|
-
}),
|
|
121
|
-
);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('should upsert vectors with metadata and ids', async () => {
|
|
125
|
-
const mockResponse = ['id1', 'id2'];
|
|
126
|
-
mockFetchResponse(mockResponse);
|
|
127
|
-
const result = await vector.upsert({
|
|
128
|
-
indexName: 'test-index',
|
|
129
|
-
vectors: [
|
|
130
|
-
[1, 2],
|
|
131
|
-
[3, 4],
|
|
132
|
-
],
|
|
133
|
-
metadata: [{ label: 'a' }, { label: 'b' }],
|
|
134
|
-
ids: ['id1', 'id2'],
|
|
135
|
-
});
|
|
136
|
-
expect(result).toEqual(mockResponse);
|
|
137
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
138
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/upsert`,
|
|
139
|
-
expect.objectContaining({
|
|
140
|
-
method: 'POST',
|
|
141
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
142
|
-
body: JSON.stringify({
|
|
143
|
-
indexName: 'test-index',
|
|
144
|
-
vectors: [
|
|
145
|
-
[1, 2],
|
|
146
|
-
[3, 4],
|
|
147
|
-
],
|
|
148
|
-
metadata: [{ label: 'a' }, { label: 'b' }],
|
|
149
|
-
ids: ['id1', 'id2'],
|
|
150
|
-
}),
|
|
151
|
-
}),
|
|
152
|
-
);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('should query vectors with all parameters', async () => {
|
|
156
|
-
const mockResponse = {
|
|
157
|
-
results: [
|
|
158
|
-
{
|
|
159
|
-
id: 'id1',
|
|
160
|
-
score: 0.9,
|
|
161
|
-
metadata: { label: 'a' },
|
|
162
|
-
vector: [1, 2],
|
|
163
|
-
},
|
|
164
|
-
],
|
|
165
|
-
};
|
|
166
|
-
mockFetchResponse(mockResponse);
|
|
167
|
-
const result = await vector.query({
|
|
168
|
-
indexName: 'test-index',
|
|
169
|
-
queryVector: [1, 2],
|
|
170
|
-
topK: 10,
|
|
171
|
-
filter: { label: 'a' },
|
|
172
|
-
includeVector: true,
|
|
173
|
-
});
|
|
174
|
-
expect(result).toEqual(mockResponse);
|
|
175
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
176
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/query`,
|
|
177
|
-
expect.objectContaining({
|
|
178
|
-
method: 'POST',
|
|
179
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
180
|
-
body: JSON.stringify({
|
|
181
|
-
indexName: 'test-index',
|
|
182
|
-
queryVector: [1, 2],
|
|
183
|
-
topK: 10,
|
|
184
|
-
filter: { label: 'a' },
|
|
185
|
-
includeVector: true,
|
|
186
|
-
}),
|
|
187
|
-
}),
|
|
188
|
-
);
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
describe('Agent Resource', () => {
|
|
193
|
-
const agentId = 'test-agent';
|
|
194
|
-
let agent: ReturnType<typeof client.getAgent>;
|
|
195
|
-
|
|
196
|
-
beforeEach(() => {
|
|
197
|
-
agent = client.getAgent(agentId);
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it('should get all agents', async () => {
|
|
201
|
-
const mockResponse = {
|
|
202
|
-
agent1: { name: 'Agent 1', model: 'gpt-4' },
|
|
203
|
-
agent2: { name: 'Agent 2', model: 'gpt-3.5' },
|
|
204
|
-
};
|
|
205
|
-
mockFetchResponse(mockResponse);
|
|
206
|
-
const result = await client.getAgents();
|
|
207
|
-
expect(result).toEqual(mockResponse);
|
|
208
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
209
|
-
`${clientOptions.baseUrl}/api/agents`,
|
|
210
|
-
expect.objectContaining({
|
|
211
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
212
|
-
}),
|
|
213
|
-
);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should get agent details', async () => {
|
|
217
|
-
const mockResponse = {
|
|
218
|
-
name: 'Test Agent',
|
|
219
|
-
model: 'gpt-4',
|
|
220
|
-
instructions: 'Test instructions',
|
|
221
|
-
tools: {},
|
|
222
|
-
};
|
|
223
|
-
mockFetchResponse(mockResponse);
|
|
224
|
-
|
|
225
|
-
const result = await agent.details();
|
|
226
|
-
expect(result).toEqual(mockResponse);
|
|
227
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
228
|
-
`${clientOptions.baseUrl}/api/agents/test-agent`,
|
|
229
|
-
expect.objectContaining({
|
|
230
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
231
|
-
}),
|
|
232
|
-
);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('should generate response', async () => {
|
|
236
|
-
const mockResponse = {
|
|
237
|
-
response: 'Generated response',
|
|
238
|
-
};
|
|
239
|
-
mockFetchResponse(mockResponse);
|
|
240
|
-
|
|
241
|
-
const result = await agent.generate({
|
|
242
|
-
messages: [],
|
|
243
|
-
threadId: 'test-thread',
|
|
244
|
-
resourceid: 'test-resource',
|
|
245
|
-
output: {},
|
|
246
|
-
});
|
|
247
|
-
expect(result).toEqual(mockResponse);
|
|
248
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
249
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/generate`,
|
|
250
|
-
expect.objectContaining({
|
|
251
|
-
method: 'POST',
|
|
252
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
253
|
-
body: JSON.stringify({
|
|
254
|
-
messages: [],
|
|
255
|
-
threadId: 'test-thread',
|
|
256
|
-
resourceid: 'test-resource',
|
|
257
|
-
output: {},
|
|
258
|
-
}),
|
|
259
|
-
}),
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it('should stream responses', async () => {
|
|
264
|
-
const mockChunk = { content: 'test response' };
|
|
265
|
-
mockFetchResponse(mockChunk, { isStream: true });
|
|
266
|
-
|
|
267
|
-
const response = await agent.stream({
|
|
268
|
-
messages: [
|
|
269
|
-
{
|
|
270
|
-
role: 'user',
|
|
271
|
-
content: 'test',
|
|
272
|
-
},
|
|
273
|
-
],
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
expect(response.body).toBeInstanceOf(ReadableStream);
|
|
277
|
-
const reader = response?.body?.getReader();
|
|
278
|
-
expect(reader).toBeDefined();
|
|
279
|
-
|
|
280
|
-
if (reader) {
|
|
281
|
-
const { value, done } = await reader.read();
|
|
282
|
-
expect(done).toBe(false);
|
|
283
|
-
expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it('should get agent tool', async () => {
|
|
288
|
-
const mockResponse = {
|
|
289
|
-
id: 'tool1',
|
|
290
|
-
description: 'Test Tool',
|
|
291
|
-
};
|
|
292
|
-
mockFetchResponse(mockResponse);
|
|
293
|
-
const result = await agent.getTool('tool1');
|
|
294
|
-
expect(result).toEqual(mockResponse);
|
|
295
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
296
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/tools/tool1`,
|
|
297
|
-
expect.objectContaining({
|
|
298
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
299
|
-
}),
|
|
300
|
-
);
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
it('should get agent evals', async () => {
|
|
304
|
-
const mockResponse = {
|
|
305
|
-
name: 'Test Agent',
|
|
306
|
-
evals: [{ id: 'eval1' }],
|
|
307
|
-
};
|
|
308
|
-
mockFetchResponse(mockResponse);
|
|
309
|
-
const result = await agent.evals();
|
|
310
|
-
expect(result).toEqual(mockResponse);
|
|
311
|
-
expect(global.fetch).toHaveBeenCalledWith(`${clientOptions.baseUrl}/api/agents/test-agent/evals/ci`, {
|
|
312
|
-
headers: {
|
|
313
|
-
Authorization: 'Bearer test-key',
|
|
314
|
-
'Content-Type': 'application/json',
|
|
315
|
-
},
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it('should get live evals', async () => {
|
|
320
|
-
const mockResponse = {
|
|
321
|
-
name: 'Test Agent',
|
|
322
|
-
evals: [{ id: 'eval1', live: true }],
|
|
323
|
-
};
|
|
324
|
-
mockFetchResponse(mockResponse);
|
|
325
|
-
const result = await agent.liveEvals();
|
|
326
|
-
expect(result).toEqual(mockResponse);
|
|
327
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
328
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/evals/live`,
|
|
329
|
-
expect.objectContaining({
|
|
330
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
331
|
-
}),
|
|
332
|
-
);
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
const agentId = 'test-agent';
|
|
337
|
-
|
|
338
|
-
describe('Memory Thread Resource', () => {
|
|
339
|
-
const threadId = 'test-thread';
|
|
340
|
-
let memoryThread: ReturnType<typeof client.getMemoryThread>;
|
|
341
|
-
|
|
342
|
-
beforeEach(() => {
|
|
343
|
-
memoryThread = client.getMemoryThread(threadId, agentId);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it('should get thread details', async () => {
|
|
347
|
-
const mockResponse = {
|
|
348
|
-
id: threadId,
|
|
349
|
-
title: 'Test Thread',
|
|
350
|
-
metadata: {},
|
|
351
|
-
};
|
|
352
|
-
mockFetchResponse(mockResponse);
|
|
353
|
-
|
|
354
|
-
const result = await memoryThread.get();
|
|
355
|
-
expect(result).toEqual(mockResponse);
|
|
356
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
357
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
358
|
-
expect.objectContaining({
|
|
359
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
360
|
-
}),
|
|
361
|
-
);
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
it('should update thread', async () => {
|
|
365
|
-
const mockResponse = {
|
|
366
|
-
id: threadId,
|
|
367
|
-
title: 'Updated Thread',
|
|
368
|
-
metadata: { updated: true },
|
|
369
|
-
};
|
|
370
|
-
mockFetchResponse(mockResponse);
|
|
371
|
-
|
|
372
|
-
const result = await memoryThread.update({
|
|
373
|
-
title: 'Updated Thread',
|
|
374
|
-
metadata: { updated: true },
|
|
375
|
-
resourceid: 'test-resource',
|
|
376
|
-
});
|
|
377
|
-
expect(result).toEqual(mockResponse);
|
|
378
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
379
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
380
|
-
expect.objectContaining({
|
|
381
|
-
method: 'PATCH',
|
|
382
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
383
|
-
}),
|
|
384
|
-
);
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
it('should delete thread', async () => {
|
|
388
|
-
const mockResponse = { result: 'deleted' };
|
|
389
|
-
mockFetchResponse(mockResponse);
|
|
390
|
-
const result = await memoryThread.delete();
|
|
391
|
-
expect(result).toEqual(mockResponse);
|
|
392
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
393
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
394
|
-
expect.objectContaining({
|
|
395
|
-
method: 'DELETE',
|
|
396
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
397
|
-
}),
|
|
398
|
-
);
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
it('should get memory status', async () => {
|
|
402
|
-
const mockResponse = { result: true };
|
|
403
|
-
mockFetchResponse(mockResponse);
|
|
404
|
-
const result = await client.getMemoryStatus(agentId);
|
|
405
|
-
expect(result).toEqual(mockResponse);
|
|
406
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
407
|
-
`${clientOptions.baseUrl}/api/memory/status?agentId=${agentId}`,
|
|
408
|
-
expect.objectContaining({
|
|
409
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
410
|
-
}),
|
|
411
|
-
);
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
it('should save messages to memory', async () => {
|
|
415
|
-
const messages: MessageType[] = [
|
|
416
|
-
{
|
|
417
|
-
id: '1',
|
|
418
|
-
type: 'text',
|
|
419
|
-
content: 'test',
|
|
420
|
-
role: 'user',
|
|
421
|
-
threadId: 'test-thread',
|
|
422
|
-
createdAt: new Date(),
|
|
423
|
-
},
|
|
424
|
-
];
|
|
425
|
-
mockFetchResponse(messages);
|
|
426
|
-
const result = await client.saveMessageToMemory({ messages, agentId });
|
|
427
|
-
expect(result).toEqual(messages);
|
|
428
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
429
|
-
`${clientOptions.baseUrl}/api/memory/save-messages?agentId=${agentId}`,
|
|
430
|
-
{
|
|
431
|
-
method: 'POST',
|
|
432
|
-
headers: {
|
|
433
|
-
Authorization: 'Bearer test-key',
|
|
434
|
-
'Content-Type': 'application/json',
|
|
435
|
-
},
|
|
436
|
-
body: JSON.stringify({ messages, agentId }),
|
|
437
|
-
},
|
|
438
|
-
);
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
describe('Tool Resource', () => {
|
|
443
|
-
const toolId = 'test-tool';
|
|
444
|
-
let tool: ReturnType<typeof client.getTool>;
|
|
445
|
-
|
|
446
|
-
beforeEach(() => {
|
|
447
|
-
tool = client.getTool(toolId);
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
it('should get tool details', async () => {
|
|
451
|
-
const mockResponse = {
|
|
452
|
-
id: toolId,
|
|
453
|
-
description: 'Test Tool',
|
|
454
|
-
inputSchema: '{}',
|
|
455
|
-
outputSchema: '{}',
|
|
456
|
-
};
|
|
457
|
-
mockFetchResponse(mockResponse);
|
|
458
|
-
|
|
459
|
-
const result = await tool.details();
|
|
460
|
-
expect(result).toEqual(mockResponse);
|
|
461
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
462
|
-
`${clientOptions.baseUrl}/api/tools/test-tool`,
|
|
463
|
-
expect.objectContaining({
|
|
464
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
465
|
-
}),
|
|
466
|
-
);
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
it('should execute tool', async () => {
|
|
470
|
-
const mockResponse = {
|
|
471
|
-
result: 'Tool execution result',
|
|
472
|
-
};
|
|
473
|
-
mockFetchResponse(mockResponse);
|
|
474
|
-
|
|
475
|
-
const result = await tool.execute({ data: '' });
|
|
476
|
-
expect(result).toEqual(mockResponse);
|
|
477
|
-
expect(global.fetch).toHaveBeenCalledWith(`${clientOptions.baseUrl}/api/tools/test-tool/execute`, {
|
|
478
|
-
method: 'POST',
|
|
479
|
-
headers: {
|
|
480
|
-
Authorization: 'Bearer test-key',
|
|
481
|
-
'Content-Type': 'application/json',
|
|
482
|
-
},
|
|
483
|
-
body: JSON.stringify({ data: '' }),
|
|
484
|
-
});
|
|
485
|
-
});
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
describe('Workflow Resource', () => {
|
|
489
|
-
const workflowId = 'test-workflow';
|
|
490
|
-
let workflow: ReturnType<typeof client.getWorkflow>;
|
|
491
|
-
|
|
492
|
-
beforeEach(() => {
|
|
493
|
-
workflow = client.getWorkflow(workflowId);
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
it('should get workflow details', async () => {
|
|
497
|
-
const mockResponse = {
|
|
498
|
-
name: 'Test Workflow',
|
|
499
|
-
triggerSchema: '{}',
|
|
500
|
-
steps: {},
|
|
501
|
-
stepGraph: {},
|
|
502
|
-
stepSubscriberGraph: {},
|
|
503
|
-
};
|
|
504
|
-
mockFetchResponse(mockResponse);
|
|
505
|
-
|
|
506
|
-
const result = await workflow.details();
|
|
507
|
-
expect(result).toEqual(mockResponse);
|
|
508
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
509
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow`,
|
|
510
|
-
expect.objectContaining({
|
|
511
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
512
|
-
}),
|
|
513
|
-
);
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
it('should execute workflow', async () => {
|
|
517
|
-
const mockResponse = {
|
|
518
|
-
result: 'Workflow execution result',
|
|
519
|
-
};
|
|
520
|
-
mockFetchResponse(mockResponse);
|
|
521
|
-
|
|
522
|
-
const result = await workflow.execute({ trigger: 'test' });
|
|
523
|
-
expect(result).toEqual(mockResponse);
|
|
524
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
525
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
|
|
526
|
-
expect.objectContaining({
|
|
527
|
-
method: 'POST',
|
|
528
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
529
|
-
body: JSON.stringify({ trigger: 'test' }),
|
|
530
|
-
}),
|
|
531
|
-
);
|
|
532
|
-
});
|
|
533
|
-
});
|
|
534
|
-
|
|
535
|
-
describe('Client Error Handling', () => {
|
|
536
|
-
it('should retry failed requests', async () => {
|
|
537
|
-
// Mock first two calls to fail, third to succeed
|
|
538
|
-
(global.fetch as any)
|
|
539
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
540
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
541
|
-
.mockResolvedValueOnce({
|
|
542
|
-
ok: true,
|
|
543
|
-
headers: {
|
|
544
|
-
get: () => 'application/json',
|
|
545
|
-
},
|
|
546
|
-
json: async () => ({ success: true }),
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
const result = await client.request('/test-endpoint');
|
|
550
|
-
expect(result).toEqual({ success: true });
|
|
551
|
-
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
it('should throw error after max retries', async () => {
|
|
555
|
-
(global.fetch as any).mockRejectedValue(new Error('Network error'));
|
|
556
|
-
|
|
557
|
-
await expect(client.request('/test-endpoint')).rejects.toThrow('Network error');
|
|
558
|
-
|
|
559
|
-
expect(global.fetch).toHaveBeenCalledTimes(4);
|
|
560
|
-
});
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
describe('Client Configuration', () => {
|
|
564
|
-
it('should handle custom retry configuration', async () => {
|
|
565
|
-
const customClient = new MastraClient({
|
|
566
|
-
baseUrl: 'http://localhost:4111',
|
|
567
|
-
retries: 2,
|
|
568
|
-
backoffMs: 100,
|
|
569
|
-
maxBackoffMs: 1000,
|
|
570
|
-
headers: { 'Custom-Header': 'value' },
|
|
571
|
-
});
|
|
572
|
-
|
|
573
|
-
(global.fetch as any)
|
|
574
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
575
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
576
|
-
.mockResolvedValueOnce({
|
|
577
|
-
ok: true,
|
|
578
|
-
headers: {
|
|
579
|
-
get: () => 'application/json',
|
|
580
|
-
},
|
|
581
|
-
json: async () => ({ success: true }),
|
|
582
|
-
});
|
|
583
|
-
|
|
584
|
-
const result = await customClient.request('/test');
|
|
585
|
-
expect(result).toEqual({ success: true });
|
|
586
|
-
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
587
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
588
|
-
'http://localhost:4111/test',
|
|
589
|
-
expect.objectContaining({
|
|
590
|
-
headers: expect.objectContaining({
|
|
591
|
-
'Custom-Header': 'value',
|
|
592
|
-
}),
|
|
593
|
-
}),
|
|
594
|
-
);
|
|
595
|
-
});
|
|
596
|
-
});
|
|
597
|
-
});
|
package/src/index.ts
DELETED