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