@mastra/client-js 0.11.3-alpha.2 → 0.11.3-alpha.4
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 +23 -0
- package/dist/client.d.ts +12 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +398 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +398 -4
- package/dist/index.js.map +1 -1
- package/dist/resources/agent-builder.d.ts +148 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +25 -0
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/index.d.ts +1 -0
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/types.d.ts +33 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -6
- package/.turbo/turbo-build.log +0 -18
- package/eslint.config.js +0 -11
- package/integration-tests/agui-adapter.test.ts +0 -122
- package/integration-tests/package.json +0 -18
- package/integration-tests/src/mastra/index.ts +0 -35
- package/integration-tests/vitest.config.ts +0 -9
- package/src/adapters/agui.test.ts +0 -293
- package/src/adapters/agui.ts +0 -257
- package/src/client.ts +0 -644
- package/src/example.ts +0 -95
- package/src/index.test.ts +0 -1253
- package/src/index.ts +0 -3
- package/src/resources/a2a.ts +0 -98
- package/src/resources/agent.ts +0 -1460
- package/src/resources/base.ts +0 -77
- package/src/resources/index.ts +0 -11
- package/src/resources/legacy-workflow.ts +0 -242
- package/src/resources/mcp-tool.ts +0 -48
- package/src/resources/memory-thread.test.ts +0 -285
- package/src/resources/memory-thread.ts +0 -99
- package/src/resources/network-memory-thread.test.ts +0 -269
- package/src/resources/network-memory-thread.ts +0 -81
- package/src/resources/network.ts +0 -86
- package/src/resources/observability.ts +0 -53
- package/src/resources/tool.ts +0 -45
- package/src/resources/vNextNetwork.ts +0 -194
- package/src/resources/vector.ts +0 -83
- package/src/resources/workflow.ts +0 -410
- package/src/types.ts +0 -534
- package/src/utils/index.ts +0 -11
- package/src/utils/process-client-tools.ts +0 -32
- package/src/utils/process-mastra-stream.test.ts +0 -353
- package/src/utils/process-mastra-stream.ts +0 -49
- package/src/utils/zod-to-json-schema.ts +0 -30
- package/src/v2-messages.test.ts +0 -180
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -17
- package/vitest.config.js +0 -8
package/src/index.test.ts
DELETED
|
@@ -1,1253 +0,0 @@
|
|
|
1
|
-
import type { ServerDetailInfo } from '@mastra/core/mcp';
|
|
2
|
-
import type { ScoringEntityType, ScoringSource } from '@mastra/core/scores';
|
|
3
|
-
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
4
|
-
import { MastraClient } from './client';
|
|
5
|
-
import type { McpServerListResponse } from './types';
|
|
6
|
-
|
|
7
|
-
// Mock fetch globally
|
|
8
|
-
global.fetch = vi.fn();
|
|
9
|
-
|
|
10
|
-
describe('MastraClient Resources', () => {
|
|
11
|
-
let client: MastraClient;
|
|
12
|
-
const clientOptions = {
|
|
13
|
-
baseUrl: 'http://localhost:4111',
|
|
14
|
-
headers: {
|
|
15
|
-
Authorization: 'Bearer test-key',
|
|
16
|
-
'x-mastra-client-type': 'js',
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// Helper to mock successful API responses
|
|
21
|
-
const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
|
|
22
|
-
if (options.isStream) {
|
|
23
|
-
let contentType = 'text/event-stream';
|
|
24
|
-
let responseBody: ReadableStream;
|
|
25
|
-
|
|
26
|
-
if (data instanceof ReadableStream) {
|
|
27
|
-
responseBody = data;
|
|
28
|
-
contentType = 'audio/mp3';
|
|
29
|
-
} else {
|
|
30
|
-
responseBody = new ReadableStream({
|
|
31
|
-
start(controller) {
|
|
32
|
-
if (typeof data === 'string') {
|
|
33
|
-
controller.enqueue(new TextEncoder().encode(data));
|
|
34
|
-
} else if (typeof data === 'object' && data !== null) {
|
|
35
|
-
controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
|
|
36
|
-
} else {
|
|
37
|
-
controller.enqueue(new TextEncoder().encode(String(data)));
|
|
38
|
-
}
|
|
39
|
-
controller.close();
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const headers = new Headers();
|
|
45
|
-
if (contentType === 'audio/mp3') {
|
|
46
|
-
headers.set('Transfer-Encoding', 'chunked');
|
|
47
|
-
}
|
|
48
|
-
headers.set('Content-Type', contentType);
|
|
49
|
-
|
|
50
|
-
(global.fetch as any).mockResolvedValueOnce(
|
|
51
|
-
new Response(responseBody, {
|
|
52
|
-
status: 200,
|
|
53
|
-
statusText: 'OK',
|
|
54
|
-
headers,
|
|
55
|
-
}),
|
|
56
|
-
);
|
|
57
|
-
} else {
|
|
58
|
-
const response = new Response(undefined, {
|
|
59
|
-
status: 200,
|
|
60
|
-
statusText: 'OK',
|
|
61
|
-
headers: new Headers({
|
|
62
|
-
'Content-Type': 'application/json',
|
|
63
|
-
}),
|
|
64
|
-
});
|
|
65
|
-
response.json = () => Promise.resolve(data);
|
|
66
|
-
(global.fetch as any).mockResolvedValueOnce(response);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
beforeEach(() => {
|
|
71
|
-
vi.clearAllMocks();
|
|
72
|
-
client = new MastraClient(clientOptions);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe('Vector Resource', () => {
|
|
76
|
-
const vectorName = 'test-vector';
|
|
77
|
-
let vector: ReturnType<typeof client.getVector>;
|
|
78
|
-
|
|
79
|
-
beforeEach(() => {
|
|
80
|
-
vector = client.getVector(vectorName);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should get vector index details', async () => {
|
|
84
|
-
const mockResponse = {
|
|
85
|
-
dimension: 128,
|
|
86
|
-
metric: 'cosine',
|
|
87
|
-
count: 1000,
|
|
88
|
-
};
|
|
89
|
-
mockFetchResponse(mockResponse);
|
|
90
|
-
|
|
91
|
-
const result = await vector.details('test-index');
|
|
92
|
-
expect(result).toEqual(mockResponse);
|
|
93
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
94
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
|
|
95
|
-
expect.objectContaining({
|
|
96
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
97
|
-
}),
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should delete vector index', async () => {
|
|
102
|
-
mockFetchResponse({ success: true });
|
|
103
|
-
const result = await vector.delete('test-index');
|
|
104
|
-
expect(result).toEqual({ success: true });
|
|
105
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
106
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
|
|
107
|
-
expect.objectContaining({
|
|
108
|
-
method: 'DELETE',
|
|
109
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
110
|
-
}),
|
|
111
|
-
);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should get all indexes', async () => {
|
|
115
|
-
const mockResponse = { indexes: ['index1', 'index2'] };
|
|
116
|
-
mockFetchResponse(mockResponse);
|
|
117
|
-
const result = await vector.getIndexes();
|
|
118
|
-
expect(result).toEqual(mockResponse);
|
|
119
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
120
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/indexes`,
|
|
121
|
-
expect.objectContaining({
|
|
122
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
123
|
-
}),
|
|
124
|
-
);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('should create vector index with all parameters', async () => {
|
|
128
|
-
mockFetchResponse({ success: true });
|
|
129
|
-
const result = await vector.createIndex({
|
|
130
|
-
indexName: 'test-index',
|
|
131
|
-
dimension: 128,
|
|
132
|
-
metric: 'cosine',
|
|
133
|
-
});
|
|
134
|
-
expect(result).toEqual({ success: true });
|
|
135
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
136
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/create-index`,
|
|
137
|
-
expect.objectContaining({
|
|
138
|
-
method: 'POST',
|
|
139
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
140
|
-
body: JSON.stringify({
|
|
141
|
-
indexName: 'test-index',
|
|
142
|
-
dimension: 128,
|
|
143
|
-
metric: 'cosine',
|
|
144
|
-
}),
|
|
145
|
-
}),
|
|
146
|
-
);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('should upsert vectors with metadata and ids', async () => {
|
|
150
|
-
const mockResponse = ['id1', 'id2'];
|
|
151
|
-
mockFetchResponse(mockResponse);
|
|
152
|
-
const result = await vector.upsert({
|
|
153
|
-
indexName: 'test-index',
|
|
154
|
-
vectors: [
|
|
155
|
-
[1, 2],
|
|
156
|
-
[3, 4],
|
|
157
|
-
],
|
|
158
|
-
metadata: [{ label: 'a' }, { label: 'b' }],
|
|
159
|
-
ids: ['id1', 'id2'],
|
|
160
|
-
});
|
|
161
|
-
expect(result).toEqual(mockResponse);
|
|
162
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
163
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/upsert`,
|
|
164
|
-
expect.objectContaining({
|
|
165
|
-
method: 'POST',
|
|
166
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
167
|
-
body: JSON.stringify({
|
|
168
|
-
indexName: 'test-index',
|
|
169
|
-
vectors: [
|
|
170
|
-
[1, 2],
|
|
171
|
-
[3, 4],
|
|
172
|
-
],
|
|
173
|
-
metadata: [{ label: 'a' }, { label: 'b' }],
|
|
174
|
-
ids: ['id1', 'id2'],
|
|
175
|
-
}),
|
|
176
|
-
}),
|
|
177
|
-
);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('should query vectors with all parameters', async () => {
|
|
181
|
-
const mockResponse = {
|
|
182
|
-
results: [
|
|
183
|
-
{
|
|
184
|
-
id: 'id1',
|
|
185
|
-
score: 0.9,
|
|
186
|
-
metadata: { label: 'a' },
|
|
187
|
-
vector: [1, 2],
|
|
188
|
-
},
|
|
189
|
-
],
|
|
190
|
-
};
|
|
191
|
-
mockFetchResponse(mockResponse);
|
|
192
|
-
const result = await vector.query({
|
|
193
|
-
indexName: 'test-index',
|
|
194
|
-
queryVector: [1, 2],
|
|
195
|
-
topK: 10,
|
|
196
|
-
filter: { label: 'a' },
|
|
197
|
-
includeVector: true,
|
|
198
|
-
});
|
|
199
|
-
expect(result).toEqual(mockResponse);
|
|
200
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
201
|
-
`${clientOptions.baseUrl}/api/vector/test-vector/query`,
|
|
202
|
-
expect.objectContaining({
|
|
203
|
-
method: 'POST',
|
|
204
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
205
|
-
body: JSON.stringify({
|
|
206
|
-
indexName: 'test-index',
|
|
207
|
-
queryVector: [1, 2],
|
|
208
|
-
topK: 10,
|
|
209
|
-
filter: { label: 'a' },
|
|
210
|
-
includeVector: true,
|
|
211
|
-
}),
|
|
212
|
-
}),
|
|
213
|
-
);
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
describe('Agent Resource', () => {
|
|
218
|
-
const agentId = 'test-agent';
|
|
219
|
-
let agent: ReturnType<typeof client.getAgent>;
|
|
220
|
-
|
|
221
|
-
beforeEach(() => {
|
|
222
|
-
agent = client.getAgent(agentId);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('should get all agents', async () => {
|
|
226
|
-
const mockResponse = {
|
|
227
|
-
agent1: { name: 'Agent 1', model: 'gpt-4' },
|
|
228
|
-
agent2: { name: 'Agent 2', model: 'gpt-3.5' },
|
|
229
|
-
};
|
|
230
|
-
mockFetchResponse(mockResponse);
|
|
231
|
-
const result = await client.getAgents();
|
|
232
|
-
expect(result).toEqual(mockResponse);
|
|
233
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
234
|
-
`${clientOptions.baseUrl}/api/agents`,
|
|
235
|
-
expect.objectContaining({
|
|
236
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
it('should get agent details', async () => {
|
|
242
|
-
const mockResponse = {
|
|
243
|
-
name: 'Test Agent',
|
|
244
|
-
model: 'gpt-4',
|
|
245
|
-
instructions: 'Test instructions',
|
|
246
|
-
tools: {},
|
|
247
|
-
workflows: {},
|
|
248
|
-
};
|
|
249
|
-
mockFetchResponse(mockResponse);
|
|
250
|
-
|
|
251
|
-
const result = await agent.details();
|
|
252
|
-
expect(result).toEqual(mockResponse);
|
|
253
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
254
|
-
`${clientOptions.baseUrl}/api/agents/test-agent`,
|
|
255
|
-
expect.objectContaining({
|
|
256
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
257
|
-
}),
|
|
258
|
-
);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('should generate response', async () => {
|
|
262
|
-
const mockResponse = {
|
|
263
|
-
response: 'Generated response',
|
|
264
|
-
};
|
|
265
|
-
mockFetchResponse(mockResponse);
|
|
266
|
-
|
|
267
|
-
const result = await agent.generate({
|
|
268
|
-
messages: [],
|
|
269
|
-
threadId: 'test-thread',
|
|
270
|
-
resourceId: 'test-resource',
|
|
271
|
-
output: {},
|
|
272
|
-
});
|
|
273
|
-
expect(result).toEqual(mockResponse);
|
|
274
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
275
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/generate`,
|
|
276
|
-
expect.objectContaining({
|
|
277
|
-
method: 'POST',
|
|
278
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
279
|
-
body: JSON.stringify({
|
|
280
|
-
messages: [],
|
|
281
|
-
threadId: 'test-thread',
|
|
282
|
-
resourceId: 'test-resource',
|
|
283
|
-
output: {},
|
|
284
|
-
}),
|
|
285
|
-
}),
|
|
286
|
-
);
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it('should stream responses', async () => {
|
|
290
|
-
const mockChunk = `0:"test response"\n`;
|
|
291
|
-
mockFetchResponse(mockChunk, { isStream: true });
|
|
292
|
-
|
|
293
|
-
const response = await agent.stream({
|
|
294
|
-
messages: [
|
|
295
|
-
{
|
|
296
|
-
role: 'user',
|
|
297
|
-
content: 'test',
|
|
298
|
-
},
|
|
299
|
-
],
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
expect(response.body).toBeInstanceOf(ReadableStream);
|
|
303
|
-
const reader = response?.body?.getReader();
|
|
304
|
-
expect(reader).toBeDefined();
|
|
305
|
-
|
|
306
|
-
if (reader) {
|
|
307
|
-
const { value, done } = await reader.read();
|
|
308
|
-
expect(done).toBe(false);
|
|
309
|
-
expect(new TextDecoder().decode(value)).toBe(mockChunk);
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
it('should stream responses with tool calls', async () => {
|
|
314
|
-
const firstMockChunk = `0:"test "
|
|
315
|
-
0:"response"
|
|
316
|
-
9:{"toolCallId":"tool1","toolName":"testTool","args":{"arg1":"value1"}}
|
|
317
|
-
e:{"finishReason":"tool-calls","usage":{"promptTokens":1,"completionTokens":1},"isContinued":false}
|
|
318
|
-
d:{"finishReason":"tool-calls","usage":{"promptTokens":2,"completionTokens":2}}
|
|
319
|
-
`;
|
|
320
|
-
|
|
321
|
-
const secondMockChunk = `0:"final response"
|
|
322
|
-
e:{"finishReason":"stop","usage":{"promptTokens":2,"completionTokens":2},"isContinued":false}
|
|
323
|
-
d:{"finishReason":"stop","usage":{"promptTokens":2,"completionTokens":2}}
|
|
324
|
-
`;
|
|
325
|
-
|
|
326
|
-
const firstResponseBody = new ReadableStream({
|
|
327
|
-
start(controller) {
|
|
328
|
-
controller.enqueue(new TextEncoder().encode(firstMockChunk));
|
|
329
|
-
controller.close();
|
|
330
|
-
},
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const secondResponseBody = new ReadableStream({
|
|
334
|
-
start(controller) {
|
|
335
|
-
controller.enqueue(new TextEncoder().encode(secondMockChunk));
|
|
336
|
-
controller.close();
|
|
337
|
-
},
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
(global.fetch as any)
|
|
341
|
-
.mockResolvedValueOnce(
|
|
342
|
-
new Response(firstResponseBody, {
|
|
343
|
-
status: 200,
|
|
344
|
-
headers: new Headers({ 'Content-Type': 'text/event-stream' }),
|
|
345
|
-
}),
|
|
346
|
-
)
|
|
347
|
-
.mockResolvedValueOnce(
|
|
348
|
-
new Response(secondResponseBody, {
|
|
349
|
-
status: 200,
|
|
350
|
-
headers: new Headers({ 'Content-Type': 'text/event-stream' }),
|
|
351
|
-
}),
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
const response = await agent.stream({
|
|
355
|
-
messages: [
|
|
356
|
-
{
|
|
357
|
-
role: 'user',
|
|
358
|
-
content: 'test',
|
|
359
|
-
},
|
|
360
|
-
],
|
|
361
|
-
clientTools: {
|
|
362
|
-
testTool: {
|
|
363
|
-
id: 'testTool',
|
|
364
|
-
description: 'Test Tool',
|
|
365
|
-
inputSchema: {
|
|
366
|
-
type: 'object',
|
|
367
|
-
properties: {
|
|
368
|
-
arg1: { type: 'string' },
|
|
369
|
-
},
|
|
370
|
-
},
|
|
371
|
-
execute: async () => {
|
|
372
|
-
return 'test result';
|
|
373
|
-
},
|
|
374
|
-
},
|
|
375
|
-
},
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
expect(response.body).toBeInstanceOf(ReadableStream);
|
|
379
|
-
const reader = response?.body?.getReader();
|
|
380
|
-
expect(reader).toBeDefined();
|
|
381
|
-
|
|
382
|
-
let output = '';
|
|
383
|
-
if (reader) {
|
|
384
|
-
while (true) {
|
|
385
|
-
const { value, done } = await reader.read();
|
|
386
|
-
if (done) break;
|
|
387
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
388
|
-
output += new TextDecoder().decode(value);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
expect(global.fetch).toHaveBeenCalledTimes(2);
|
|
393
|
-
|
|
394
|
-
const [secondUrl, secondConfig] = (global.fetch as any).mock.calls[1];
|
|
395
|
-
expect(secondUrl).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/stream`);
|
|
396
|
-
|
|
397
|
-
const secondRequestBody = JSON.parse(secondConfig.body);
|
|
398
|
-
expect(secondRequestBody.messages).toHaveLength(2);
|
|
399
|
-
expect(secondRequestBody.messages[0].content).toBe('test');
|
|
400
|
-
expect(secondRequestBody.messages[1].content).toBe('test response');
|
|
401
|
-
expect(secondRequestBody.messages[1].parts).toEqual([
|
|
402
|
-
{
|
|
403
|
-
type: 'text',
|
|
404
|
-
text: 'test response',
|
|
405
|
-
},
|
|
406
|
-
{
|
|
407
|
-
type: 'tool-invocation',
|
|
408
|
-
toolInvocation: {
|
|
409
|
-
state: 'result',
|
|
410
|
-
step: 0,
|
|
411
|
-
toolCallId: 'tool1',
|
|
412
|
-
toolName: 'testTool',
|
|
413
|
-
args: {
|
|
414
|
-
arg1: 'value1',
|
|
415
|
-
},
|
|
416
|
-
result: 'test result',
|
|
417
|
-
},
|
|
418
|
-
},
|
|
419
|
-
]);
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
it('should get agent tool', async () => {
|
|
423
|
-
const mockResponse = {
|
|
424
|
-
id: 'tool1',
|
|
425
|
-
description: 'Test Tool',
|
|
426
|
-
};
|
|
427
|
-
mockFetchResponse(mockResponse);
|
|
428
|
-
const result = await agent.getTool('tool1');
|
|
429
|
-
expect(result).toEqual(mockResponse);
|
|
430
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
431
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/tools/tool1`,
|
|
432
|
-
expect.objectContaining({
|
|
433
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
434
|
-
}),
|
|
435
|
-
);
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
it('should get agent evals', async () => {
|
|
439
|
-
const mockResponse = { data: 'test' };
|
|
440
|
-
mockFetchResponse(mockResponse);
|
|
441
|
-
const result = await agent.evals();
|
|
442
|
-
expect(result).toEqual(mockResponse);
|
|
443
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
444
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/evals/ci`,
|
|
445
|
-
expect.objectContaining({
|
|
446
|
-
headers: expect.objectContaining({
|
|
447
|
-
Authorization: 'Bearer test-key',
|
|
448
|
-
}),
|
|
449
|
-
}),
|
|
450
|
-
);
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
it('should get live evals', async () => {
|
|
454
|
-
const mockResponse = {
|
|
455
|
-
name: 'Test Agent',
|
|
456
|
-
evals: [{ id: 'eval1', live: true }],
|
|
457
|
-
};
|
|
458
|
-
mockFetchResponse(mockResponse);
|
|
459
|
-
const result = await agent.liveEvals();
|
|
460
|
-
expect(result).toEqual(mockResponse);
|
|
461
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
462
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/evals/live`,
|
|
463
|
-
expect.objectContaining({
|
|
464
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
465
|
-
}),
|
|
466
|
-
);
|
|
467
|
-
});
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
describe('Agent Voice Resource', () => {
|
|
471
|
-
const agentId = 'test-agent';
|
|
472
|
-
let agent: ReturnType<typeof client.getAgent>;
|
|
473
|
-
beforeEach(() => {
|
|
474
|
-
agent = client.getAgent(agentId);
|
|
475
|
-
});
|
|
476
|
-
it('should get available speakers', async () => {
|
|
477
|
-
const mockResponse = [{ voiceId: 'speaker1' }];
|
|
478
|
-
mockFetchResponse(mockResponse);
|
|
479
|
-
|
|
480
|
-
const result = await agent.voice.getSpeakers();
|
|
481
|
-
|
|
482
|
-
expect(result).toEqual(mockResponse);
|
|
483
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
484
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speakers`,
|
|
485
|
-
expect.objectContaining({
|
|
486
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
487
|
-
}),
|
|
488
|
-
);
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
it(`should call speak without options`, async () => {
|
|
492
|
-
const mockAudioStream = new ReadableStream();
|
|
493
|
-
mockFetchResponse(mockAudioStream, { isStream: true });
|
|
494
|
-
|
|
495
|
-
const result = await agent.voice.speak('test');
|
|
496
|
-
|
|
497
|
-
expect(result).toBeInstanceOf(Response);
|
|
498
|
-
expect(result.body).toBeInstanceOf(ReadableStream);
|
|
499
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
500
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speak`,
|
|
501
|
-
expect.objectContaining({
|
|
502
|
-
method: 'POST',
|
|
503
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
504
|
-
}),
|
|
505
|
-
);
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
it(`should call speak with options`, async () => {
|
|
509
|
-
const mockAudioStream = new ReadableStream();
|
|
510
|
-
mockFetchResponse(mockAudioStream, { isStream: true });
|
|
511
|
-
|
|
512
|
-
const result = await agent.voice.speak('test', { speaker: 'speaker1' });
|
|
513
|
-
expect(result).toBeInstanceOf(Response);
|
|
514
|
-
expect(result.body).toBeInstanceOf(ReadableStream);
|
|
515
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
516
|
-
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speak`,
|
|
517
|
-
expect.objectContaining({
|
|
518
|
-
method: 'POST',
|
|
519
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
520
|
-
}),
|
|
521
|
-
);
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
it(`should call listen with audio file`, async () => {
|
|
525
|
-
const transcriptionResponse = { text: 'Hello world' };
|
|
526
|
-
mockFetchResponse(transcriptionResponse);
|
|
527
|
-
|
|
528
|
-
const audioBlob = new Blob(['test audio data'], { type: 'audio/wav' });
|
|
529
|
-
|
|
530
|
-
const result = await agent.voice.listen(audioBlob, { filetype: 'wav' });
|
|
531
|
-
expect(result).toEqual(transcriptionResponse);
|
|
532
|
-
|
|
533
|
-
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
534
|
-
const [url, config] = (global.fetch as any).mock.calls[0];
|
|
535
|
-
expect(url).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/voice/listen`);
|
|
536
|
-
expect(config.method).toBe('POST');
|
|
537
|
-
expect(config.headers).toMatchObject(clientOptions.headers);
|
|
538
|
-
|
|
539
|
-
const formData = config.body;
|
|
540
|
-
expect(formData).toBeInstanceOf(FormData);
|
|
541
|
-
const audioContent = formData.get('audio');
|
|
542
|
-
expect(audioContent).toBeInstanceOf(Blob);
|
|
543
|
-
expect(audioContent.type).toBe('audio/wav');
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it(`should call listen with audio blob and options`, async () => {
|
|
547
|
-
const transcriptionResponse = { text: 'Hello world' };
|
|
548
|
-
mockFetchResponse(transcriptionResponse);
|
|
549
|
-
|
|
550
|
-
const audioBlob = new Blob(['test audio data'], { type: 'audio/mp3' });
|
|
551
|
-
|
|
552
|
-
const result = await agent.voice.listen(audioBlob, { filetype: 'mp3' });
|
|
553
|
-
|
|
554
|
-
expect(result).toEqual(transcriptionResponse);
|
|
555
|
-
|
|
556
|
-
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
557
|
-
const [url, config] = (global.fetch as any).mock.calls[0];
|
|
558
|
-
expect(url).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/voice/listen`);
|
|
559
|
-
expect(config.method).toBe('POST');
|
|
560
|
-
expect(config.headers).toMatchObject(clientOptions.headers);
|
|
561
|
-
|
|
562
|
-
const formData = config.body as FormData;
|
|
563
|
-
expect(formData).toBeInstanceOf(FormData);
|
|
564
|
-
const audioContent = formData.get('audio');
|
|
565
|
-
expect(audioContent).toBeInstanceOf(Blob);
|
|
566
|
-
expect(formData.get('options')).toBe(JSON.stringify({ filetype: 'mp3' }));
|
|
567
|
-
});
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
const agentId = 'test-agent';
|
|
571
|
-
|
|
572
|
-
describe('Memory Thread Resource', () => {
|
|
573
|
-
const threadId = 'test-thread';
|
|
574
|
-
let memoryThread: ReturnType<typeof client.getMemoryThread>;
|
|
575
|
-
|
|
576
|
-
beforeEach(() => {
|
|
577
|
-
memoryThread = client.getMemoryThread(threadId, agentId);
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
it('should get thread details', async () => {
|
|
581
|
-
const mockResponse = {
|
|
582
|
-
id: threadId,
|
|
583
|
-
title: 'Test Thread',
|
|
584
|
-
metadata: {},
|
|
585
|
-
};
|
|
586
|
-
mockFetchResponse(mockResponse);
|
|
587
|
-
|
|
588
|
-
const result = await memoryThread.get();
|
|
589
|
-
expect(result).toEqual(mockResponse);
|
|
590
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
591
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
592
|
-
expect.objectContaining({
|
|
593
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
594
|
-
}),
|
|
595
|
-
);
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
it('should update thread', async () => {
|
|
599
|
-
const mockResponse = {
|
|
600
|
-
id: threadId,
|
|
601
|
-
title: 'Updated Thread',
|
|
602
|
-
metadata: { updated: true },
|
|
603
|
-
};
|
|
604
|
-
mockFetchResponse(mockResponse);
|
|
605
|
-
|
|
606
|
-
const result = await memoryThread.update({
|
|
607
|
-
title: 'Updated Thread',
|
|
608
|
-
metadata: { updated: true },
|
|
609
|
-
resourceId: 'test-resource',
|
|
610
|
-
});
|
|
611
|
-
expect(result).toEqual(mockResponse);
|
|
612
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
613
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
614
|
-
expect.objectContaining({
|
|
615
|
-
method: 'PATCH',
|
|
616
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
617
|
-
}),
|
|
618
|
-
);
|
|
619
|
-
});
|
|
620
|
-
|
|
621
|
-
it('should delete thread', async () => {
|
|
622
|
-
const mockResponse = { result: 'deleted' };
|
|
623
|
-
mockFetchResponse(mockResponse);
|
|
624
|
-
const result = await memoryThread.delete();
|
|
625
|
-
expect(result).toEqual(mockResponse);
|
|
626
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
627
|
-
`${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
|
|
628
|
-
expect.objectContaining({
|
|
629
|
-
method: 'DELETE',
|
|
630
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
631
|
-
}),
|
|
632
|
-
);
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
it('should get memory status', async () => {
|
|
636
|
-
const mockResponse = { result: true };
|
|
637
|
-
mockFetchResponse(mockResponse);
|
|
638
|
-
const result = await client.getMemoryStatus(agentId);
|
|
639
|
-
expect(result).toEqual(mockResponse);
|
|
640
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
641
|
-
`${clientOptions.baseUrl}/api/memory/status?agentId=${agentId}`,
|
|
642
|
-
expect.objectContaining({
|
|
643
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
644
|
-
}),
|
|
645
|
-
);
|
|
646
|
-
});
|
|
647
|
-
|
|
648
|
-
it('should save messages to memory', async () => {
|
|
649
|
-
const messages = [
|
|
650
|
-
{
|
|
651
|
-
id: '1',
|
|
652
|
-
type: 'text' as const,
|
|
653
|
-
content: 'test',
|
|
654
|
-
role: 'user' as const,
|
|
655
|
-
threadId: 'test-thread',
|
|
656
|
-
resourceId: 'test-resource',
|
|
657
|
-
createdAt: new Date('2025-03-26T10:40:55.116Z'),
|
|
658
|
-
},
|
|
659
|
-
];
|
|
660
|
-
mockFetchResponse(messages);
|
|
661
|
-
const result = await client.saveMessageToMemory({ agentId, messages });
|
|
662
|
-
expect(result).toEqual(messages);
|
|
663
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
664
|
-
`${clientOptions.baseUrl}/api/memory/save-messages?agentId=${agentId}`,
|
|
665
|
-
expect.objectContaining({
|
|
666
|
-
method: 'POST',
|
|
667
|
-
headers: expect.objectContaining({
|
|
668
|
-
Authorization: 'Bearer test-key',
|
|
669
|
-
}),
|
|
670
|
-
}),
|
|
671
|
-
);
|
|
672
|
-
});
|
|
673
|
-
|
|
674
|
-
it('should get thread messages with limit', async () => {
|
|
675
|
-
const mockResponse = {
|
|
676
|
-
messages: [
|
|
677
|
-
{
|
|
678
|
-
id: '1',
|
|
679
|
-
content: 'test',
|
|
680
|
-
threadId,
|
|
681
|
-
role: 'user',
|
|
682
|
-
type: 'text',
|
|
683
|
-
resourceId: 'test-resource',
|
|
684
|
-
createdAt: new Date(),
|
|
685
|
-
},
|
|
686
|
-
],
|
|
687
|
-
uiMessages: [],
|
|
688
|
-
};
|
|
689
|
-
mockFetchResponse(mockResponse);
|
|
690
|
-
|
|
691
|
-
const limit = 5;
|
|
692
|
-
const result = await memoryThread.getMessages({ limit });
|
|
693
|
-
|
|
694
|
-
expect(result).toEqual(mockResponse);
|
|
695
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
696
|
-
`${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages?agentId=${agentId}&limit=${limit}`,
|
|
697
|
-
expect.objectContaining({
|
|
698
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
699
|
-
}),
|
|
700
|
-
);
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
it('should get paginated thread messages', async () => {
|
|
704
|
-
const mockResponse = {
|
|
705
|
-
messages: [
|
|
706
|
-
{
|
|
707
|
-
id: '1',
|
|
708
|
-
content: 'test message',
|
|
709
|
-
threadId,
|
|
710
|
-
role: 'user',
|
|
711
|
-
type: 'text',
|
|
712
|
-
resourceId: 'test-resource',
|
|
713
|
-
createdAt: new Date(),
|
|
714
|
-
},
|
|
715
|
-
],
|
|
716
|
-
total: 5,
|
|
717
|
-
page: 1,
|
|
718
|
-
perPage: 2,
|
|
719
|
-
hasMore: true,
|
|
720
|
-
};
|
|
721
|
-
mockFetchResponse(mockResponse);
|
|
722
|
-
|
|
723
|
-
const selectBy = {
|
|
724
|
-
pagination: {
|
|
725
|
-
page: 1,
|
|
726
|
-
perPage: 2,
|
|
727
|
-
},
|
|
728
|
-
};
|
|
729
|
-
|
|
730
|
-
const result = await memoryThread.getMessagesPaginated({
|
|
731
|
-
resourceId: 'test-resource',
|
|
732
|
-
format: 'v2',
|
|
733
|
-
selectBy,
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
expect(result).toEqual(mockResponse);
|
|
737
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
738
|
-
`${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages/paginated?resourceId=test-resource&format=v2&selectBy=${encodeURIComponent(JSON.stringify(selectBy))}`,
|
|
739
|
-
expect.objectContaining({
|
|
740
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
741
|
-
}),
|
|
742
|
-
);
|
|
743
|
-
});
|
|
744
|
-
});
|
|
745
|
-
|
|
746
|
-
describe('Tool Resource', () => {
|
|
747
|
-
const toolId = 'test-tool';
|
|
748
|
-
let tool: ReturnType<typeof client.getTool>;
|
|
749
|
-
|
|
750
|
-
beforeEach(() => {
|
|
751
|
-
tool = client.getTool(toolId);
|
|
752
|
-
});
|
|
753
|
-
|
|
754
|
-
it('should get tool details', async () => {
|
|
755
|
-
const mockResponse = {
|
|
756
|
-
id: toolId,
|
|
757
|
-
description: 'Test Tool',
|
|
758
|
-
inputSchema: '{}',
|
|
759
|
-
outputSchema: '{}',
|
|
760
|
-
};
|
|
761
|
-
mockFetchResponse(mockResponse);
|
|
762
|
-
|
|
763
|
-
const result = await tool.details();
|
|
764
|
-
expect(result).toEqual(mockResponse);
|
|
765
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
766
|
-
`${clientOptions.baseUrl}/api/tools/test-tool`,
|
|
767
|
-
expect.objectContaining({
|
|
768
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
769
|
-
}),
|
|
770
|
-
);
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
it('should execute tool', async () => {
|
|
774
|
-
const mockResponse = { data: 'test' };
|
|
775
|
-
mockFetchResponse(mockResponse);
|
|
776
|
-
const result = await tool.execute({ data: '', runId: 'test-run-id' });
|
|
777
|
-
expect(result).toEqual(mockResponse);
|
|
778
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
779
|
-
`${clientOptions.baseUrl}/api/tools/test-tool/execute?runId=test-run-id`,
|
|
780
|
-
expect.objectContaining({
|
|
781
|
-
method: 'POST',
|
|
782
|
-
headers: expect.objectContaining({
|
|
783
|
-
Authorization: 'Bearer test-key',
|
|
784
|
-
}),
|
|
785
|
-
}),
|
|
786
|
-
);
|
|
787
|
-
});
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
describe('Workflow Resource', () => {
|
|
791
|
-
const workflowId = 'test-workflow';
|
|
792
|
-
let workflow: ReturnType<typeof client.getWorkflow>;
|
|
793
|
-
|
|
794
|
-
beforeEach(() => {
|
|
795
|
-
workflow = client.getWorkflow(workflowId);
|
|
796
|
-
});
|
|
797
|
-
|
|
798
|
-
it('should get workflow details', async () => {
|
|
799
|
-
const mockResponse = {
|
|
800
|
-
name: 'Test Workflow',
|
|
801
|
-
triggerSchema: '{}',
|
|
802
|
-
steps: {},
|
|
803
|
-
stepGraph: {},
|
|
804
|
-
stepSubscriberGraph: {},
|
|
805
|
-
};
|
|
806
|
-
mockFetchResponse(mockResponse);
|
|
807
|
-
|
|
808
|
-
const result = await workflow.details();
|
|
809
|
-
expect(result).toEqual(mockResponse);
|
|
810
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
811
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow`,
|
|
812
|
-
expect.objectContaining({
|
|
813
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
814
|
-
}),
|
|
815
|
-
);
|
|
816
|
-
});
|
|
817
|
-
|
|
818
|
-
it('should execute workflow', async () => {
|
|
819
|
-
const mockResponse = {
|
|
820
|
-
result: 'Workflow execution result',
|
|
821
|
-
};
|
|
822
|
-
mockFetchResponse(mockResponse);
|
|
823
|
-
|
|
824
|
-
const result = await workflow.startAsync({ inputData: { test: 'test' } });
|
|
825
|
-
expect(result).toEqual(mockResponse);
|
|
826
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
827
|
-
`${clientOptions.baseUrl}/api/workflows/test-workflow/start-async?`,
|
|
828
|
-
expect.objectContaining({
|
|
829
|
-
method: 'POST',
|
|
830
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
831
|
-
body: JSON.stringify({ inputData: { test: 'test' } }),
|
|
832
|
-
}),
|
|
833
|
-
);
|
|
834
|
-
});
|
|
835
|
-
});
|
|
836
|
-
|
|
837
|
-
describe('Client Error Handling', () => {
|
|
838
|
-
it('should retry failed requests', async () => {
|
|
839
|
-
// Mock first two calls to fail, third to succeed
|
|
840
|
-
(global.fetch as any)
|
|
841
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
842
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
843
|
-
.mockResolvedValueOnce({
|
|
844
|
-
ok: true,
|
|
845
|
-
headers: {
|
|
846
|
-
get: () => 'application/json',
|
|
847
|
-
},
|
|
848
|
-
json: async () => ({ success: true }),
|
|
849
|
-
});
|
|
850
|
-
|
|
851
|
-
const result = await client.request('/test-endpoint');
|
|
852
|
-
expect(result).toEqual({ success: true });
|
|
853
|
-
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
854
|
-
});
|
|
855
|
-
|
|
856
|
-
it('should throw error after max retries', async () => {
|
|
857
|
-
(global.fetch as any).mockRejectedValue(new Error('Network error'));
|
|
858
|
-
|
|
859
|
-
await expect(client.request('/test-endpoint')).rejects.toThrow('Network error');
|
|
860
|
-
|
|
861
|
-
expect(global.fetch).toHaveBeenCalledTimes(4);
|
|
862
|
-
});
|
|
863
|
-
});
|
|
864
|
-
|
|
865
|
-
describe('Client Configuration', () => {
|
|
866
|
-
it('should handle custom retry configuration', async () => {
|
|
867
|
-
const customClient = new MastraClient({
|
|
868
|
-
baseUrl: 'http://localhost:4111',
|
|
869
|
-
retries: 2,
|
|
870
|
-
backoffMs: 100,
|
|
871
|
-
maxBackoffMs: 1000,
|
|
872
|
-
headers: { 'Custom-Header': 'value' },
|
|
873
|
-
credentials: 'same-origin',
|
|
874
|
-
});
|
|
875
|
-
|
|
876
|
-
(global.fetch as any)
|
|
877
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
878
|
-
.mockRejectedValueOnce(new Error('Network error'))
|
|
879
|
-
.mockResolvedValueOnce({
|
|
880
|
-
ok: true,
|
|
881
|
-
headers: {
|
|
882
|
-
get: () => 'application/json',
|
|
883
|
-
},
|
|
884
|
-
json: async () => ({ success: true }),
|
|
885
|
-
})
|
|
886
|
-
.mockResolvedValueOnce({
|
|
887
|
-
ok: true,
|
|
888
|
-
headers: {
|
|
889
|
-
get: () => 'application/json',
|
|
890
|
-
},
|
|
891
|
-
json: async () => ({ success: true }),
|
|
892
|
-
});
|
|
893
|
-
|
|
894
|
-
const result = await customClient.request('/test');
|
|
895
|
-
expect(result).toEqual({ success: true });
|
|
896
|
-
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
897
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
898
|
-
'http://localhost:4111/test',
|
|
899
|
-
expect.objectContaining({
|
|
900
|
-
headers: expect.objectContaining({
|
|
901
|
-
'Custom-Header': 'value',
|
|
902
|
-
}),
|
|
903
|
-
credentials: 'same-origin',
|
|
904
|
-
}),
|
|
905
|
-
);
|
|
906
|
-
|
|
907
|
-
// ensure custom headers and credentials are overridable per request
|
|
908
|
-
const result2 = await customClient.request('/test', {
|
|
909
|
-
headers: { 'Custom-Header': 'new-value' },
|
|
910
|
-
credentials: 'include',
|
|
911
|
-
});
|
|
912
|
-
expect(result2).toEqual({ success: true });
|
|
913
|
-
expect(global.fetch).toHaveBeenCalledTimes(4);
|
|
914
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
915
|
-
'http://localhost:4111/test',
|
|
916
|
-
expect.objectContaining({
|
|
917
|
-
headers: expect.objectContaining({
|
|
918
|
-
'Custom-Header': 'new-value',
|
|
919
|
-
}),
|
|
920
|
-
credentials: 'include',
|
|
921
|
-
}),
|
|
922
|
-
);
|
|
923
|
-
});
|
|
924
|
-
});
|
|
925
|
-
|
|
926
|
-
describe('MCP Server Registry Client Methods', () => {
|
|
927
|
-
const mockServerInfo1 = {
|
|
928
|
-
id: 'mcp-server-1',
|
|
929
|
-
name: 'Test MCP Server 1',
|
|
930
|
-
version_detail: { version: '1.0.0', release_date: '2023-01-01T00:00:00Z', is_latest: true },
|
|
931
|
-
};
|
|
932
|
-
const mockServerInfo2 = {
|
|
933
|
-
id: 'mcp-server-2',
|
|
934
|
-
name: 'Test MCP Server 2',
|
|
935
|
-
version_detail: { version: '1.1.0', release_date: '2023-02-01T00:00:00Z', is_latest: true },
|
|
936
|
-
};
|
|
937
|
-
|
|
938
|
-
const mockServerDetail1: ServerDetailInfo = {
|
|
939
|
-
...mockServerInfo1,
|
|
940
|
-
description: 'Detailed description for server 1',
|
|
941
|
-
package_canonical: 'npm',
|
|
942
|
-
packages: [{ registry_name: 'npm', name: '@example/server1', version: '1.0.0' }],
|
|
943
|
-
remotes: [{ transport_type: 'sse', url: 'http://localhost/sse1' }],
|
|
944
|
-
};
|
|
945
|
-
|
|
946
|
-
describe('getMcpServers()', () => {
|
|
947
|
-
it('should fetch a list of MCP servers', async () => {
|
|
948
|
-
const mockResponse: McpServerListResponse = {
|
|
949
|
-
servers: [mockServerInfo1, mockServerInfo2],
|
|
950
|
-
total_count: 2,
|
|
951
|
-
next: null,
|
|
952
|
-
};
|
|
953
|
-
mockFetchResponse(mockResponse);
|
|
954
|
-
|
|
955
|
-
const result = await client.getMcpServers();
|
|
956
|
-
expect(result).toEqual(mockResponse);
|
|
957
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
958
|
-
`${clientOptions.baseUrl}/api/mcp/v0/servers`,
|
|
959
|
-
expect.objectContaining({
|
|
960
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
961
|
-
}),
|
|
962
|
-
);
|
|
963
|
-
});
|
|
964
|
-
|
|
965
|
-
it('should fetch MCP servers with limit and offset parameters', async () => {
|
|
966
|
-
const mockResponse: McpServerListResponse = {
|
|
967
|
-
servers: [mockServerInfo1],
|
|
968
|
-
total_count: 2,
|
|
969
|
-
next: '/api/mcp/v0/servers?limit=1&offset=1',
|
|
970
|
-
};
|
|
971
|
-
mockFetchResponse(mockResponse);
|
|
972
|
-
|
|
973
|
-
const result = await client.getMcpServers({ limit: 1, offset: 0 });
|
|
974
|
-
expect(result).toEqual(mockResponse);
|
|
975
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
976
|
-
`${clientOptions.baseUrl}/api/mcp/v0/servers?limit=1&offset=0`,
|
|
977
|
-
expect.objectContaining({
|
|
978
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
979
|
-
}),
|
|
980
|
-
);
|
|
981
|
-
});
|
|
982
|
-
});
|
|
983
|
-
|
|
984
|
-
describe('getMcpServerDetails()', () => {
|
|
985
|
-
const serverId = 'mcp-server-1';
|
|
986
|
-
|
|
987
|
-
it('should fetch details for a specific MCP server', async () => {
|
|
988
|
-
mockFetchResponse(mockServerDetail1);
|
|
989
|
-
|
|
990
|
-
const result = await client.getMcpServerDetails(serverId);
|
|
991
|
-
expect(result).toEqual(mockServerDetail1);
|
|
992
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
993
|
-
`${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}`,
|
|
994
|
-
expect.objectContaining({
|
|
995
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
996
|
-
}),
|
|
997
|
-
);
|
|
998
|
-
});
|
|
999
|
-
|
|
1000
|
-
it('should fetch MCP server details with a version parameter', async () => {
|
|
1001
|
-
mockFetchResponse(mockServerDetail1);
|
|
1002
|
-
const version = '1.0.0';
|
|
1003
|
-
|
|
1004
|
-
const result = await client.getMcpServerDetails(serverId, { version });
|
|
1005
|
-
expect(result).toEqual(mockServerDetail1);
|
|
1006
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1007
|
-
`${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}?version=${version}`,
|
|
1008
|
-
expect.objectContaining({
|
|
1009
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1010
|
-
}),
|
|
1011
|
-
);
|
|
1012
|
-
});
|
|
1013
|
-
});
|
|
1014
|
-
});
|
|
1015
|
-
|
|
1016
|
-
describe('Scores Methods', () => {
|
|
1017
|
-
describe('getScorers()', () => {
|
|
1018
|
-
it('should fetch all available scorers', async () => {
|
|
1019
|
-
const mockResponse = {
|
|
1020
|
-
scorers: [
|
|
1021
|
-
{ id: 'scorer-1', name: 'Test Scorer 1', description: 'A test scorer' },
|
|
1022
|
-
{ id: 'scorer-2', name: 'Test Scorer 2', description: 'Another test scorer' },
|
|
1023
|
-
],
|
|
1024
|
-
};
|
|
1025
|
-
mockFetchResponse(mockResponse);
|
|
1026
|
-
|
|
1027
|
-
const result = await client.getScorers();
|
|
1028
|
-
expect(result).toEqual(mockResponse);
|
|
1029
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1030
|
-
`${clientOptions.baseUrl}/api/scores/scorers`,
|
|
1031
|
-
expect.objectContaining({
|
|
1032
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1033
|
-
}),
|
|
1034
|
-
);
|
|
1035
|
-
});
|
|
1036
|
-
});
|
|
1037
|
-
|
|
1038
|
-
describe('getScoresByRunId()', () => {
|
|
1039
|
-
it('should fetch scores by run ID without pagination', async () => {
|
|
1040
|
-
const mockResponse = {
|
|
1041
|
-
pagination: {
|
|
1042
|
-
total: 10,
|
|
1043
|
-
page: 0,
|
|
1044
|
-
perPage: 10,
|
|
1045
|
-
hasMore: false,
|
|
1046
|
-
},
|
|
1047
|
-
scores: [
|
|
1048
|
-
{
|
|
1049
|
-
id: 'score-1',
|
|
1050
|
-
runId: 'run-123',
|
|
1051
|
-
scorer: { name: 'test-scorer' },
|
|
1052
|
-
result: { score: 0.8 },
|
|
1053
|
-
input: { messages: [] },
|
|
1054
|
-
output: { response: 'test' },
|
|
1055
|
-
source: 'LIVE',
|
|
1056
|
-
createdAt: new Date(),
|
|
1057
|
-
updatedAt: new Date(),
|
|
1058
|
-
},
|
|
1059
|
-
],
|
|
1060
|
-
};
|
|
1061
|
-
|
|
1062
|
-
mockFetchResponse({
|
|
1063
|
-
...mockResponse,
|
|
1064
|
-
scores: mockResponse.scores.map(score => ({
|
|
1065
|
-
...score,
|
|
1066
|
-
createdAt: score.createdAt.toISOString(),
|
|
1067
|
-
updatedAt: score.updatedAt.toISOString(),
|
|
1068
|
-
})),
|
|
1069
|
-
});
|
|
1070
|
-
|
|
1071
|
-
const result = await client.getScoresByRunId({ runId: 'run-123' });
|
|
1072
|
-
|
|
1073
|
-
expect(result).toEqual({
|
|
1074
|
-
...mockResponse,
|
|
1075
|
-
scores: mockResponse.scores.map(score => ({
|
|
1076
|
-
...score,
|
|
1077
|
-
createdAt: score.createdAt.toISOString(),
|
|
1078
|
-
updatedAt: score.updatedAt.toISOString(),
|
|
1079
|
-
})),
|
|
1080
|
-
});
|
|
1081
|
-
|
|
1082
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1083
|
-
`${clientOptions.baseUrl}/api/scores/run/run-123`,
|
|
1084
|
-
expect.objectContaining({
|
|
1085
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1086
|
-
}),
|
|
1087
|
-
);
|
|
1088
|
-
});
|
|
1089
|
-
|
|
1090
|
-
it('should fetch scores by run ID with pagination', async () => {
|
|
1091
|
-
const mockResponse = {
|
|
1092
|
-
pagination: {
|
|
1093
|
-
total: 20,
|
|
1094
|
-
page: 1,
|
|
1095
|
-
perPage: 5,
|
|
1096
|
-
hasMore: true,
|
|
1097
|
-
},
|
|
1098
|
-
scores: [],
|
|
1099
|
-
};
|
|
1100
|
-
mockFetchResponse(mockResponse);
|
|
1101
|
-
|
|
1102
|
-
const result = await client.getScoresByRunId({
|
|
1103
|
-
runId: 'run-123',
|
|
1104
|
-
page: 1,
|
|
1105
|
-
perPage: 5,
|
|
1106
|
-
});
|
|
1107
|
-
expect(result).toEqual(mockResponse);
|
|
1108
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1109
|
-
`${clientOptions.baseUrl}/api/scores/run/run-123?page=1&perPage=5`,
|
|
1110
|
-
expect.objectContaining({
|
|
1111
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1112
|
-
}),
|
|
1113
|
-
);
|
|
1114
|
-
});
|
|
1115
|
-
});
|
|
1116
|
-
|
|
1117
|
-
describe('getScoresByEntityId()', () => {
|
|
1118
|
-
it('should fetch scores by entity ID and type without pagination', async () => {
|
|
1119
|
-
const mockResponse = {
|
|
1120
|
-
pagination: {
|
|
1121
|
-
total: 5,
|
|
1122
|
-
page: 0,
|
|
1123
|
-
perPage: 10,
|
|
1124
|
-
hasMore: false,
|
|
1125
|
-
},
|
|
1126
|
-
scores: [
|
|
1127
|
-
{
|
|
1128
|
-
id: 'score-1',
|
|
1129
|
-
runId: 'run-123',
|
|
1130
|
-
entityId: 'agent-456',
|
|
1131
|
-
entityType: 'AGENT',
|
|
1132
|
-
scorer: { name: 'test-scorer' },
|
|
1133
|
-
result: { score: 0.9 },
|
|
1134
|
-
input: { messages: [] },
|
|
1135
|
-
output: { response: 'test' },
|
|
1136
|
-
source: 'LIVE',
|
|
1137
|
-
createdAt: new Date(),
|
|
1138
|
-
updatedAt: new Date(),
|
|
1139
|
-
},
|
|
1140
|
-
],
|
|
1141
|
-
};
|
|
1142
|
-
|
|
1143
|
-
const mockResponseWithDates = mockResponse.scores.map(score => ({
|
|
1144
|
-
...score,
|
|
1145
|
-
createdAt: score.createdAt.toISOString(),
|
|
1146
|
-
updatedAt: score.updatedAt.toISOString(),
|
|
1147
|
-
}));
|
|
1148
|
-
|
|
1149
|
-
mockFetchResponse({
|
|
1150
|
-
...mockResponse,
|
|
1151
|
-
scores: mockResponseWithDates,
|
|
1152
|
-
});
|
|
1153
|
-
|
|
1154
|
-
const result = await client.getScoresByEntityId({
|
|
1155
|
-
entityId: 'agent-456',
|
|
1156
|
-
entityType: 'AGENT',
|
|
1157
|
-
});
|
|
1158
|
-
|
|
1159
|
-
expect(result).toEqual({
|
|
1160
|
-
...mockResponse,
|
|
1161
|
-
scores: mockResponseWithDates,
|
|
1162
|
-
});
|
|
1163
|
-
|
|
1164
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1165
|
-
`${clientOptions.baseUrl}/api/scores/entity/AGENT/agent-456`,
|
|
1166
|
-
expect.objectContaining({
|
|
1167
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1168
|
-
}),
|
|
1169
|
-
);
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
it('should fetch scores by entity ID and type with pagination', async () => {
|
|
1173
|
-
const mockResponse = {
|
|
1174
|
-
pagination: {
|
|
1175
|
-
total: 15,
|
|
1176
|
-
page: 2,
|
|
1177
|
-
perPage: 5,
|
|
1178
|
-
hasMore: true,
|
|
1179
|
-
},
|
|
1180
|
-
scores: [],
|
|
1181
|
-
};
|
|
1182
|
-
mockFetchResponse(mockResponse);
|
|
1183
|
-
|
|
1184
|
-
const result = await client.getScoresByEntityId({
|
|
1185
|
-
entityId: 'workflow-789',
|
|
1186
|
-
entityType: 'WORKFLOW',
|
|
1187
|
-
page: 2,
|
|
1188
|
-
perPage: 5,
|
|
1189
|
-
});
|
|
1190
|
-
expect(result).toEqual(mockResponse);
|
|
1191
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1192
|
-
`${clientOptions.baseUrl}/api/scores/entity/WORKFLOW/workflow-789?page=2&perPage=5`,
|
|
1193
|
-
expect.objectContaining({
|
|
1194
|
-
body: undefined,
|
|
1195
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1196
|
-
signal: undefined,
|
|
1197
|
-
}),
|
|
1198
|
-
);
|
|
1199
|
-
});
|
|
1200
|
-
});
|
|
1201
|
-
|
|
1202
|
-
describe('saveScore()', () => {
|
|
1203
|
-
it('should save a score', async () => {
|
|
1204
|
-
const scoreData = {
|
|
1205
|
-
id: 'score-1',
|
|
1206
|
-
scorerId: 'test-scorer',
|
|
1207
|
-
runId: 'run-123',
|
|
1208
|
-
scorer: { name: 'test-scorer' },
|
|
1209
|
-
score: 0.85,
|
|
1210
|
-
input: [],
|
|
1211
|
-
output: { response: 'test response' },
|
|
1212
|
-
source: 'LIVE' as ScoringSource,
|
|
1213
|
-
entityId: 'agent-456',
|
|
1214
|
-
entityType: 'AGENT' as ScoringEntityType,
|
|
1215
|
-
entity: { id: 'agent-456', name: 'test-agent' },
|
|
1216
|
-
createdAt: new Date(),
|
|
1217
|
-
updatedAt: new Date(),
|
|
1218
|
-
runtimeContext: {
|
|
1219
|
-
model: {
|
|
1220
|
-
name: 'test-model',
|
|
1221
|
-
version: '1.0.0',
|
|
1222
|
-
},
|
|
1223
|
-
},
|
|
1224
|
-
};
|
|
1225
|
-
const mockResponse = {
|
|
1226
|
-
score: {
|
|
1227
|
-
...scoreData,
|
|
1228
|
-
createdAt: scoreData.createdAt.toISOString(),
|
|
1229
|
-
updatedAt: scoreData.updatedAt.toISOString(),
|
|
1230
|
-
},
|
|
1231
|
-
};
|
|
1232
|
-
mockFetchResponse(mockResponse);
|
|
1233
|
-
|
|
1234
|
-
const result = await client.saveScore({ score: scoreData });
|
|
1235
|
-
expect(result).toEqual({
|
|
1236
|
-
score: {
|
|
1237
|
-
...scoreData,
|
|
1238
|
-
createdAt: scoreData.createdAt.toISOString(),
|
|
1239
|
-
updatedAt: scoreData.updatedAt.toISOString(),
|
|
1240
|
-
},
|
|
1241
|
-
});
|
|
1242
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
1243
|
-
`${clientOptions.baseUrl}/api/scores`,
|
|
1244
|
-
expect.objectContaining({
|
|
1245
|
-
method: 'POST',
|
|
1246
|
-
headers: expect.objectContaining(clientOptions.headers),
|
|
1247
|
-
body: JSON.stringify({ score: scoreData }),
|
|
1248
|
-
}),
|
|
1249
|
-
);
|
|
1250
|
-
});
|
|
1251
|
-
});
|
|
1252
|
-
});
|
|
1253
|
-
});
|