@mastra/client-js 0.1.0-alpha.9 → 0.1.1
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/.turbo/turbo-build.log +15 -0
- package/CHANGELOG.md +20 -0
- package/README.md +24 -10
- package/dist/index.d.mts +48 -9
- package/dist/index.mjs +63 -13
- package/eslint.config.js +6 -0
- package/package.json +5 -4
- package/src/client.ts +170 -132
- package/src/example.ts +38 -42
- package/src/index.test.ts +542 -530
- package/src/index.ts +1 -1
- package/src/resources/agent.ts +99 -98
- package/src/resources/base.ts +54 -54
- package/src/resources/index.ts +1 -1
- package/src/resources/memory-thread.ts +50 -47
- package/src/resources/tool.ts +25 -24
- package/src/resources/vector.ts +72 -71
- package/src/resources/workflow.ts +62 -25
- package/src/types.ts +98 -77
- package/tsconfig.json +4 -28
- package/.eslintrc.js +0 -10
- package/.prettierignore +0 -3
- package/.prettierrc.json +0 -26
package/src/index.test.ts
CHANGED
|
@@ -1,585 +1,597 @@
|
|
|
1
|
-
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
2
|
-
import { MastraClient } from "./client";
|
|
3
1
|
import type { MessageType } from '@mastra/core';
|
|
2
|
+
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { MastraClient } from './client';
|
|
4
5
|
|
|
5
6
|
// Mock fetch globally
|
|
6
7
|
global.fetch = vi.fn();
|
|
7
8
|
|
|
8
9
|
describe('MastraClient Resources', () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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,
|
|
12
37
|
headers: {
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
}
|
|
42
|
-
};
|
|
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>;
|
|
43
53
|
|
|
44
54
|
beforeEach(() => {
|
|
45
|
-
|
|
46
|
-
client = new MastraClient(clientOptions);
|
|
55
|
+
vector = client.getVector(vectorName);
|
|
47
56
|
});
|
|
48
57
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
expect(result).toEqual(mockResponse);
|
|
67
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
68
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
|
|
69
|
-
expect.objectContaining({
|
|
70
|
-
headers: expect.objectContaining(clientOptions.headers)
|
|
71
|
-
})
|
|
72
|
-
);
|
|
73
|
-
});
|
|
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
|
+
});
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
+
});
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
+
});
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
+
});
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
+
});
|
|
147
154
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
})
|
|
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
|
+
);
|
|
181
189
|
});
|
|
190
|
+
});
|
|
182
191
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
192
|
+
describe('Agent Resource', () => {
|
|
193
|
+
const agentId = 'test-agent';
|
|
194
|
+
let agent: ReturnType<typeof client.getAgent>;
|
|
186
195
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
196
|
+
beforeEach(() => {
|
|
197
|
+
agent = client.getAgent(agentId);
|
|
198
|
+
});
|
|
190
199
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
+
});
|
|
206
215
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
+
});
|
|
225
234
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
+
});
|
|
253
262
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
});
|
|
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
|
+
});
|
|
279
286
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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
|
+
});
|
|
295
302
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
+
});
|
|
311
318
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
});
|
|
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
|
+
);
|
|
327
333
|
});
|
|
334
|
+
});
|
|
328
335
|
|
|
329
|
-
|
|
330
|
-
const threadId = 'test-thread';
|
|
331
|
-
let memoryThread: ReturnType<typeof client.getMemoryThread>;
|
|
336
|
+
const agentId = 'test-agent';
|
|
332
337
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
338
|
+
describe('Memory Thread Resource', () => {
|
|
339
|
+
const threadId = 'test-thread';
|
|
340
|
+
let memoryThread: ReturnType<typeof client.getMemoryThread>;
|
|
336
341
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
title: 'Test Thread',
|
|
341
|
-
metadata: {}
|
|
342
|
-
};
|
|
343
|
-
mockFetchResponse(mockResponse);
|
|
344
|
-
|
|
345
|
-
const result = await memoryThread.get();
|
|
346
|
-
expect(result).toEqual(mockResponse);
|
|
347
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
348
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread`,
|
|
349
|
-
expect.objectContaining({
|
|
350
|
-
headers: expect.objectContaining(clientOptions.headers)
|
|
351
|
-
})
|
|
352
|
-
);
|
|
353
|
-
});
|
|
342
|
+
beforeEach(() => {
|
|
343
|
+
memoryThread = client.getMemoryThread(threadId, agentId);
|
|
344
|
+
});
|
|
354
345
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
method: 'PATCH',
|
|
373
|
-
headers: expect.objectContaining(clientOptions.headers)
|
|
374
|
-
})
|
|
375
|
-
);
|
|
376
|
-
});
|
|
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
|
+
});
|
|
377
363
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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
|
+
});
|
|
391
386
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
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
|
+
});
|
|
404
400
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
expect(result).toEqual(messages);
|
|
417
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
418
|
-
`${clientOptions.baseUrl}/api/memory/save-messages`,
|
|
419
|
-
expect.objectContaining({
|
|
420
|
-
method: 'POST',
|
|
421
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
422
|
-
body: JSON.stringify({ messages })
|
|
423
|
-
})
|
|
424
|
-
);
|
|
425
|
-
});
|
|
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
|
+
);
|
|
426
412
|
});
|
|
427
413
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
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
|
+
});
|
|
431
441
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
442
|
+
describe('Tool Resource', () => {
|
|
443
|
+
const toolId = 'test-tool';
|
|
444
|
+
let tool: ReturnType<typeof client.getTool>;
|
|
435
445
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
description: 'Test Tool',
|
|
440
|
-
inputSchema: '{}',
|
|
441
|
-
outputSchema: '{}'
|
|
442
|
-
};
|
|
443
|
-
mockFetchResponse(mockResponse);
|
|
444
|
-
|
|
445
|
-
const result = await tool.details();
|
|
446
|
-
expect(result).toEqual(mockResponse);
|
|
447
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
448
|
-
`${clientOptions.baseUrl}/api/tools/test-tool`,
|
|
449
|
-
expect.objectContaining({
|
|
450
|
-
headers: expect.objectContaining(clientOptions.headers)
|
|
451
|
-
})
|
|
452
|
-
);
|
|
453
|
-
});
|
|
446
|
+
beforeEach(() => {
|
|
447
|
+
tool = client.getTool(toolId);
|
|
448
|
+
});
|
|
454
449
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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
|
+
);
|
|
472
467
|
});
|
|
473
468
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
469
|
+
it('should execute tool', async () => {
|
|
470
|
+
const mockResponse = {
|
|
471
|
+
result: 'Tool execution result',
|
|
472
|
+
};
|
|
473
|
+
mockFetchResponse(mockResponse);
|
|
477
474
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
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
|
+
});
|
|
481
487
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
triggerSchema: '{}',
|
|
486
|
-
steps: {},
|
|
487
|
-
stepGraph: {},
|
|
488
|
-
stepSubscriberGraph: {}
|
|
489
|
-
};
|
|
490
|
-
mockFetchResponse(mockResponse);
|
|
491
|
-
|
|
492
|
-
const result = await workflow.details();
|
|
493
|
-
expect(result).toEqual(mockResponse);
|
|
494
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
495
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow`,
|
|
496
|
-
expect.objectContaining({
|
|
497
|
-
headers: expect.objectContaining(clientOptions.headers)
|
|
498
|
-
})
|
|
499
|
-
);
|
|
500
|
-
});
|
|
488
|
+
describe('Workflow Resource', () => {
|
|
489
|
+
const workflowId = 'test-workflow';
|
|
490
|
+
let workflow: ReturnType<typeof client.getWorkflow>;
|
|
501
491
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
result: 'Workflow execution result'
|
|
505
|
-
};
|
|
506
|
-
mockFetchResponse(mockResponse);
|
|
507
|
-
|
|
508
|
-
const result = await workflow.execute({ trigger: 'test' });
|
|
509
|
-
expect(result).toEqual(mockResponse);
|
|
510
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
511
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
|
|
512
|
-
expect.objectContaining({
|
|
513
|
-
method: 'POST',
|
|
514
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
515
|
-
body: JSON.stringify({ trigger: 'test' })
|
|
516
|
-
})
|
|
517
|
-
);
|
|
518
|
-
});
|
|
492
|
+
beforeEach(() => {
|
|
493
|
+
workflow = client.getWorkflow(workflowId);
|
|
519
494
|
});
|
|
520
495
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
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 }),
|
|
538
547
|
});
|
|
539
548
|
|
|
540
|
-
|
|
541
|
-
|
|
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'));
|
|
542
556
|
|
|
543
|
-
|
|
544
|
-
.rejects
|
|
545
|
-
.toThrow('Network error');
|
|
557
|
+
await expect(client.request('/test-endpoint')).rejects.toThrow('Network error');
|
|
546
558
|
|
|
547
|
-
|
|
548
|
-
});
|
|
559
|
+
expect(global.fetch).toHaveBeenCalledTimes(4);
|
|
549
560
|
});
|
|
561
|
+
});
|
|
550
562
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
const result = await customClient.request('/test');
|
|
573
|
-
expect(result).toEqual({ success: true });
|
|
574
|
-
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
575
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
576
|
-
'http://localhost:4111/test',
|
|
577
|
-
expect.objectContaining({
|
|
578
|
-
headers: expect.objectContaining({
|
|
579
|
-
'Custom-Header': 'value'
|
|
580
|
-
})
|
|
581
|
-
})
|
|
582
|
-
);
|
|
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 }),
|
|
583
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
|
+
);
|
|
584
595
|
});
|
|
596
|
+
});
|
|
585
597
|
});
|