@mastra/client-js 0.1.13-alpha.2 → 0.1.13-alpha.3
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 +8 -8
- package/CHANGELOG.md +9 -0
- package/dist/index.cjs +27 -6
- package/dist/index.d.cts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +27 -6
- package/package.json +3 -2
- package/src/example.ts +20 -29
- package/src/index.test.ts +28 -33
- package/src/resources/agent.ts +27 -4
- package/src/resources/network.ts +25 -3
- package/src/types.ts +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/client-js@0.1.13-alpha.
|
|
2
|
+
> @mastra/client-js@0.1.13-alpha.3 build /home/runner/work/mastra/mastra/client-sdks/client-js
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
[34mCLI[39m Cleaning output folder
|
|
10
10
|
[34mESM[39m Build start
|
|
11
11
|
[34mCJS[39m Build start
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
12
|
+
[32mESM[39m [1mdist/index.js [22m[32m21.58 KB[39m
|
|
13
|
+
[32mESM[39m ⚡️ Build success in 1464ms
|
|
14
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m21.76 KB[39m
|
|
15
|
+
[32mCJS[39m ⚡️ Build success in 1475ms
|
|
16
16
|
[34mDTS[39m Build start
|
|
17
|
-
[32mDTS[39m ⚡️ Build success in
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
19
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[
|
|
17
|
+
[32mDTS[39m ⚡️ Build success in 15172ms
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m19.18 KB[39m
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m19.18 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mastra/client-js
|
|
2
2
|
|
|
3
|
+
## 0.1.13-alpha.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 160f88e: Leverage aiSDK processDataStream util in MastraClient
|
|
8
|
+
- Updated dependencies [b3b34f5]
|
|
9
|
+
- Updated dependencies [a4686e8]
|
|
10
|
+
- @mastra/core@0.7.0-alpha.3
|
|
11
|
+
|
|
3
12
|
## 0.1.13-alpha.2
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var zod = require('zod');
|
|
4
4
|
var zodToJsonSchema = require('zod-to-json-schema');
|
|
5
|
+
var uiUtils = require('@ai-sdk/ui-utils');
|
|
5
6
|
|
|
6
7
|
// src/resources/agent.ts
|
|
7
8
|
|
|
@@ -144,19 +145,29 @@ var Agent = class extends BaseResource {
|
|
|
144
145
|
/**
|
|
145
146
|
* Streams a response from the agent
|
|
146
147
|
* @param params - Stream parameters including prompt
|
|
147
|
-
* @returns Promise containing the
|
|
148
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
148
149
|
*/
|
|
149
|
-
stream(params) {
|
|
150
|
+
async stream(params) {
|
|
150
151
|
const processedParams = {
|
|
151
152
|
...params,
|
|
152
153
|
output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output,
|
|
153
154
|
experimental_output: params.experimental_output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
154
155
|
};
|
|
155
|
-
|
|
156
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
156
157
|
method: "POST",
|
|
157
158
|
body: processedParams,
|
|
158
159
|
stream: true
|
|
159
160
|
});
|
|
161
|
+
if (!response.body) {
|
|
162
|
+
throw new Error("No response body");
|
|
163
|
+
}
|
|
164
|
+
response.processDataStream = async (options = {}) => {
|
|
165
|
+
await uiUtils.processDataStream({
|
|
166
|
+
stream: response.body,
|
|
167
|
+
...options
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
return response;
|
|
160
171
|
}
|
|
161
172
|
/**
|
|
162
173
|
* Gets details about a specific tool available to the agent
|
|
@@ -212,19 +223,29 @@ var Network = class extends BaseResource {
|
|
|
212
223
|
/**
|
|
213
224
|
* Streams a response from the agent
|
|
214
225
|
* @param params - Stream parameters including prompt
|
|
215
|
-
* @returns Promise containing the
|
|
226
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
216
227
|
*/
|
|
217
|
-
stream(params) {
|
|
228
|
+
async stream(params) {
|
|
218
229
|
const processedParams = {
|
|
219
230
|
...params,
|
|
220
231
|
output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output,
|
|
221
232
|
experimental_output: params.experimental_output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
222
233
|
};
|
|
223
|
-
|
|
234
|
+
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
224
235
|
method: "POST",
|
|
225
236
|
body: processedParams,
|
|
226
237
|
stream: true
|
|
227
238
|
});
|
|
239
|
+
if (!response.body) {
|
|
240
|
+
throw new Error("No response body");
|
|
241
|
+
}
|
|
242
|
+
response.processDataStream = async (options = {}) => {
|
|
243
|
+
await uiUtils.processDataStream({
|
|
244
|
+
stream: response.body,
|
|
245
|
+
...options
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
return response;
|
|
228
249
|
}
|
|
229
250
|
};
|
|
230
251
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
|
|
2
2
|
import { JSONSchema7 } from 'json-schema';
|
|
3
3
|
import { ZodSchema } from 'zod';
|
|
4
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
4
5
|
import { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
5
6
|
|
|
6
7
|
interface ClientOptions {
|
|
@@ -34,7 +35,7 @@ type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> =
|
|
|
34
35
|
} & Partial<AgentGenerateOptions<T>>;
|
|
35
36
|
type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
36
37
|
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
37
|
-
} &
|
|
38
|
+
} & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
|
|
38
39
|
interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
39
40
|
evals: any[];
|
|
40
41
|
}
|
|
@@ -221,9 +222,11 @@ declare class Agent extends BaseResource {
|
|
|
221
222
|
/**
|
|
222
223
|
* Streams a response from the agent
|
|
223
224
|
* @param params - Stream parameters including prompt
|
|
224
|
-
* @returns Promise containing the
|
|
225
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
225
226
|
*/
|
|
226
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response
|
|
227
|
+
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
228
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
229
|
+
}>;
|
|
227
230
|
/**
|
|
228
231
|
* Gets details about a specific tool available to the agent
|
|
229
232
|
* @param toolId - ID of the tool to retrieve
|
|
@@ -259,9 +262,11 @@ declare class Network extends BaseResource {
|
|
|
259
262
|
/**
|
|
260
263
|
* Streams a response from the agent
|
|
261
264
|
* @param params - Stream parameters including prompt
|
|
262
|
-
* @returns Promise containing the
|
|
265
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
263
266
|
*/
|
|
264
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response
|
|
267
|
+
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
268
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
269
|
+
}>;
|
|
265
270
|
}
|
|
266
271
|
|
|
267
272
|
declare class MemoryThread extends BaseResource {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
|
|
2
2
|
import { JSONSchema7 } from 'json-schema';
|
|
3
3
|
import { ZodSchema } from 'zod';
|
|
4
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
4
5
|
import { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
5
6
|
|
|
6
7
|
interface ClientOptions {
|
|
@@ -34,7 +35,7 @@ type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> =
|
|
|
34
35
|
} & Partial<AgentGenerateOptions<T>>;
|
|
35
36
|
type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
36
37
|
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
37
|
-
} &
|
|
38
|
+
} & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
|
|
38
39
|
interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
39
40
|
evals: any[];
|
|
40
41
|
}
|
|
@@ -221,9 +222,11 @@ declare class Agent extends BaseResource {
|
|
|
221
222
|
/**
|
|
222
223
|
* Streams a response from the agent
|
|
223
224
|
* @param params - Stream parameters including prompt
|
|
224
|
-
* @returns Promise containing the
|
|
225
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
225
226
|
*/
|
|
226
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response
|
|
227
|
+
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
228
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
229
|
+
}>;
|
|
227
230
|
/**
|
|
228
231
|
* Gets details about a specific tool available to the agent
|
|
229
232
|
* @param toolId - ID of the tool to retrieve
|
|
@@ -259,9 +262,11 @@ declare class Network extends BaseResource {
|
|
|
259
262
|
/**
|
|
260
263
|
* Streams a response from the agent
|
|
261
264
|
* @param params - Stream parameters including prompt
|
|
262
|
-
* @returns Promise containing the
|
|
265
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
263
266
|
*/
|
|
264
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response
|
|
267
|
+
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
268
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
269
|
+
}>;
|
|
265
270
|
}
|
|
266
271
|
|
|
267
272
|
declare class MemoryThread extends BaseResource {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ZodSchema } from 'zod';
|
|
2
2
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
3
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
3
4
|
|
|
4
5
|
// src/resources/agent.ts
|
|
5
6
|
|
|
@@ -142,19 +143,29 @@ var Agent = class extends BaseResource {
|
|
|
142
143
|
/**
|
|
143
144
|
* Streams a response from the agent
|
|
144
145
|
* @param params - Stream parameters including prompt
|
|
145
|
-
* @returns Promise containing the
|
|
146
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
146
147
|
*/
|
|
147
|
-
stream(params) {
|
|
148
|
+
async stream(params) {
|
|
148
149
|
const processedParams = {
|
|
149
150
|
...params,
|
|
150
151
|
output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
|
|
151
152
|
experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
152
153
|
};
|
|
153
|
-
|
|
154
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
154
155
|
method: "POST",
|
|
155
156
|
body: processedParams,
|
|
156
157
|
stream: true
|
|
157
158
|
});
|
|
159
|
+
if (!response.body) {
|
|
160
|
+
throw new Error("No response body");
|
|
161
|
+
}
|
|
162
|
+
response.processDataStream = async (options = {}) => {
|
|
163
|
+
await processDataStream({
|
|
164
|
+
stream: response.body,
|
|
165
|
+
...options
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
return response;
|
|
158
169
|
}
|
|
159
170
|
/**
|
|
160
171
|
* Gets details about a specific tool available to the agent
|
|
@@ -210,19 +221,29 @@ var Network = class extends BaseResource {
|
|
|
210
221
|
/**
|
|
211
222
|
* Streams a response from the agent
|
|
212
223
|
* @param params - Stream parameters including prompt
|
|
213
|
-
* @returns Promise containing the
|
|
224
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
214
225
|
*/
|
|
215
|
-
stream(params) {
|
|
226
|
+
async stream(params) {
|
|
216
227
|
const processedParams = {
|
|
217
228
|
...params,
|
|
218
229
|
output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
|
|
219
230
|
experimental_output: params.experimental_output instanceof ZodSchema ? zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
220
231
|
};
|
|
221
|
-
|
|
232
|
+
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
222
233
|
method: "POST",
|
|
223
234
|
body: processedParams,
|
|
224
235
|
stream: true
|
|
225
236
|
});
|
|
237
|
+
if (!response.body) {
|
|
238
|
+
throw new Error("No response body");
|
|
239
|
+
}
|
|
240
|
+
response.processDataStream = async (options = {}) => {
|
|
241
|
+
await processDataStream({
|
|
242
|
+
stream: response.body,
|
|
243
|
+
...options
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
return response;
|
|
226
247
|
}
|
|
227
248
|
};
|
|
228
249
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/client-js",
|
|
3
|
-
"version": "0.1.13-alpha.
|
|
3
|
+
"version": "0.1.13-alpha.3",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
"repository": "github:mastra-ai/client-js",
|
|
23
23
|
"license": "ISC",
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"@ai-sdk/ui-utils": "^1.1.19",
|
|
25
26
|
"json-schema": "^0.4.0",
|
|
26
27
|
"zod": "^3.24.2",
|
|
27
28
|
"zod-to-json-schema": "^3.24.3",
|
|
28
|
-
"@mastra/core": "^0.7.0-alpha.
|
|
29
|
+
"@mastra/core": "^0.7.0-alpha.3"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@babel/preset-env": "^7.26.9",
|
package/src/example.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MastraClient } from './client';
|
|
2
|
-
import type { WorkflowRunResult } from './types';
|
|
1
|
+
// import { MastraClient } from './client';
|
|
2
|
+
// import type { WorkflowRunResult } from './types';
|
|
3
3
|
|
|
4
4
|
// Agent
|
|
5
5
|
|
|
@@ -8,38 +8,29 @@ import type { WorkflowRunResult } from './types';
|
|
|
8
8
|
// baseUrl: 'http://localhost:4111',
|
|
9
9
|
// });
|
|
10
10
|
|
|
11
|
+
// console.log('Starting agent...');
|
|
12
|
+
|
|
11
13
|
// try {
|
|
12
14
|
// const agent = client.getAgent('weatherAgent');
|
|
13
15
|
// const response = await agent.stream({
|
|
14
|
-
// messages:
|
|
15
|
-
//
|
|
16
|
-
// role: 'user',
|
|
17
|
-
// content: 'Hello, world!',
|
|
18
|
-
// },
|
|
19
|
-
// ],
|
|
20
|
-
// });
|
|
21
|
-
|
|
22
|
-
// const reader = response?.body?.getReader();
|
|
23
|
-
// const decoder = new TextDecoder();
|
|
24
|
-
// let buffer = '';
|
|
25
|
-
|
|
26
|
-
// while (true) {
|
|
27
|
-
// if (!reader) break;
|
|
28
|
-
// const { value, done } = await reader.read();
|
|
29
|
-
// if (done) break;
|
|
30
|
-
|
|
31
|
-
// const chunk = decoder.decode(value);
|
|
32
|
-
// buffer += chunk;
|
|
33
|
-
|
|
34
|
-
// console.log(buffer);
|
|
16
|
+
// messages: 'what is the weather in new york?',
|
|
17
|
+
// })
|
|
35
18
|
|
|
36
|
-
//
|
|
19
|
+
// response.processDataStream({
|
|
20
|
+
// onTextPart: (text) => {
|
|
21
|
+
// process.stdout.write(text);
|
|
22
|
+
// },
|
|
23
|
+
// onFilePart: (file) => {
|
|
24
|
+
// console.log(file);
|
|
25
|
+
// },
|
|
26
|
+
// onDataPart: (data) => {
|
|
27
|
+
// console.log(data);
|
|
28
|
+
// },
|
|
29
|
+
// onErrorPart: (error) => {
|
|
30
|
+
// console.error(error);
|
|
31
|
+
// },
|
|
32
|
+
// });
|
|
37
33
|
|
|
38
|
-
// for (const match of matches) {
|
|
39
|
-
// const content = match[1];
|
|
40
|
-
// process.stdout.write(`${content}\n`);
|
|
41
|
-
// }
|
|
42
|
-
// }
|
|
43
34
|
// } catch (error) {
|
|
44
35
|
// console.error(error);
|
|
45
36
|
// }
|
package/src/index.test.ts
CHANGED
|
@@ -318,19 +318,18 @@ describe('MastraClient Resources', () => {
|
|
|
318
318
|
});
|
|
319
319
|
|
|
320
320
|
it('should get agent evals', async () => {
|
|
321
|
-
const mockResponse = {
|
|
322
|
-
name: 'Test Agent',
|
|
323
|
-
evals: [{ id: 'eval1' }],
|
|
324
|
-
};
|
|
321
|
+
const mockResponse = { data: 'test' };
|
|
325
322
|
mockFetchResponse(mockResponse);
|
|
326
323
|
const result = await agent.evals();
|
|
327
324
|
expect(result).toEqual(mockResponse);
|
|
328
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
325
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
326
|
+
`${clientOptions.baseUrl}/api/agents/test-agent/evals/ci`,
|
|
327
|
+
expect.objectContaining({
|
|
328
|
+
headers: expect.objectContaining({
|
|
329
|
+
Authorization: 'Bearer test-key',
|
|
330
|
+
}),
|
|
331
|
+
}),
|
|
332
|
+
);
|
|
334
333
|
});
|
|
335
334
|
|
|
336
335
|
it('should get live evals', async () => {
|
|
@@ -529,29 +528,27 @@ describe('MastraClient Resources', () => {
|
|
|
529
528
|
});
|
|
530
529
|
|
|
531
530
|
it('should save messages to memory', async () => {
|
|
532
|
-
const messages
|
|
531
|
+
const messages = [
|
|
533
532
|
{
|
|
534
533
|
id: '1',
|
|
535
|
-
type: 'text',
|
|
534
|
+
type: 'text' as const,
|
|
536
535
|
content: 'test',
|
|
537
|
-
role: 'user',
|
|
536
|
+
role: 'user' as const,
|
|
538
537
|
threadId: 'test-thread',
|
|
539
|
-
createdAt: new Date(),
|
|
538
|
+
createdAt: new Date('2025-03-26T10:40:55.116Z'),
|
|
540
539
|
},
|
|
541
540
|
];
|
|
542
541
|
mockFetchResponse(messages);
|
|
543
|
-
const result = await client.saveMessageToMemory({
|
|
542
|
+
const result = await client.saveMessageToMemory({ agentId, messages });
|
|
544
543
|
expect(result).toEqual(messages);
|
|
545
544
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
546
545
|
`${clientOptions.baseUrl}/api/memory/save-messages?agentId=${agentId}`,
|
|
547
|
-
{
|
|
546
|
+
expect.objectContaining({
|
|
548
547
|
method: 'POST',
|
|
549
|
-
headers: {
|
|
548
|
+
headers: expect.objectContaining({
|
|
550
549
|
Authorization: 'Bearer test-key',
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
body: JSON.stringify({ messages, agentId }),
|
|
554
|
-
},
|
|
550
|
+
}),
|
|
551
|
+
}),
|
|
555
552
|
);
|
|
556
553
|
});
|
|
557
554
|
});
|
|
@@ -584,21 +581,19 @@ describe('MastraClient Resources', () => {
|
|
|
584
581
|
});
|
|
585
582
|
|
|
586
583
|
it('should execute tool', async () => {
|
|
587
|
-
const mockResponse = {
|
|
588
|
-
result: 'Tool execution result',
|
|
589
|
-
};
|
|
584
|
+
const mockResponse = { data: 'test' };
|
|
590
585
|
mockFetchResponse(mockResponse);
|
|
591
|
-
|
|
592
586
|
const result = await tool.execute({ data: '' });
|
|
593
587
|
expect(result).toEqual(mockResponse);
|
|
594
|
-
expect(global.fetch).toHaveBeenCalledWith(
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
588
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
589
|
+
`${clientOptions.baseUrl}/api/tools/test-tool/execute`,
|
|
590
|
+
expect.objectContaining({
|
|
591
|
+
method: 'POST',
|
|
592
|
+
headers: expect.objectContaining({
|
|
593
|
+
Authorization: 'Bearer test-key',
|
|
594
|
+
}),
|
|
595
|
+
}),
|
|
596
|
+
);
|
|
602
597
|
});
|
|
603
598
|
});
|
|
604
599
|
|
package/src/resources/agent.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { GenerateReturn
|
|
1
|
+
import type { GenerateReturn } from '@mastra/core';
|
|
2
2
|
import type { JSONSchema7 } from 'json-schema';
|
|
3
3
|
import { ZodSchema } from 'zod';
|
|
4
4
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
5
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
5
6
|
|
|
6
7
|
import type {
|
|
7
8
|
GenerateParams,
|
|
@@ -28,6 +29,7 @@ export class AgentTool extends BaseResource {
|
|
|
28
29
|
* @param params - Parameters required for tool execution
|
|
29
30
|
* @returns Promise containing tool execution results
|
|
30
31
|
*/
|
|
32
|
+
/** @deprecated use CreateRun/startRun */
|
|
31
33
|
execute(params: { data: any }): Promise<any> {
|
|
32
34
|
return this.request(`/api/agents/${this.agentId}/tools/${this.toolId}/execute`, {
|
|
33
35
|
method: 'POST',
|
|
@@ -136,9 +138,15 @@ export class Agent extends BaseResource {
|
|
|
136
138
|
/**
|
|
137
139
|
* Streams a response from the agent
|
|
138
140
|
* @param params - Stream parameters including prompt
|
|
139
|
-
* @returns Promise containing the
|
|
141
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
140
142
|
*/
|
|
141
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
143
|
+
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
144
|
+
params: StreamParams<T>,
|
|
145
|
+
): Promise<
|
|
146
|
+
Response & {
|
|
147
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
148
|
+
}
|
|
149
|
+
> {
|
|
142
150
|
const processedParams = {
|
|
143
151
|
...params,
|
|
144
152
|
output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
|
|
@@ -148,11 +156,26 @@ export class Agent extends BaseResource {
|
|
|
148
156
|
: params.experimental_output,
|
|
149
157
|
};
|
|
150
158
|
|
|
151
|
-
|
|
159
|
+
const response: Response & {
|
|
160
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
161
|
+
} = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
152
162
|
method: 'POST',
|
|
153
163
|
body: processedParams,
|
|
154
164
|
stream: true,
|
|
155
165
|
});
|
|
166
|
+
|
|
167
|
+
if (!response.body) {
|
|
168
|
+
throw new Error('No response body');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
response.processDataStream = async (options = {}) => {
|
|
172
|
+
await processDataStream({
|
|
173
|
+
stream: response.body as ReadableStream<Uint8Array>,
|
|
174
|
+
...options,
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return response;
|
|
156
179
|
}
|
|
157
180
|
|
|
158
181
|
/**
|
package/src/resources/network.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
|
6
6
|
import type { GenerateParams, ClientOptions, StreamParams, GetNetworkResponse } from '../types';
|
|
7
7
|
|
|
8
8
|
import { BaseResource } from './base';
|
|
9
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
9
10
|
|
|
10
11
|
export class Network extends BaseResource {
|
|
11
12
|
constructor(
|
|
@@ -49,9 +50,15 @@ export class Network extends BaseResource {
|
|
|
49
50
|
/**
|
|
50
51
|
* Streams a response from the agent
|
|
51
52
|
* @param params - Stream parameters including prompt
|
|
52
|
-
* @returns Promise containing the
|
|
53
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
53
54
|
*/
|
|
54
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
55
|
+
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
56
|
+
params: StreamParams<T>,
|
|
57
|
+
): Promise<
|
|
58
|
+
Response & {
|
|
59
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
> {
|
|
55
62
|
const processedParams = {
|
|
56
63
|
...params,
|
|
57
64
|
output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
|
|
@@ -61,10 +68,25 @@ export class Network extends BaseResource {
|
|
|
61
68
|
: params.experimental_output,
|
|
62
69
|
};
|
|
63
70
|
|
|
64
|
-
|
|
71
|
+
const response: Response & {
|
|
72
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
73
|
+
} = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
65
74
|
method: 'POST',
|
|
66
75
|
body: processedParams,
|
|
67
76
|
stream: true,
|
|
68
77
|
});
|
|
78
|
+
|
|
79
|
+
if (!response.body) {
|
|
80
|
+
throw new Error('No response body');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
response.processDataStream = async (options = {}) => {
|
|
84
|
+
await processDataStream({
|
|
85
|
+
stream: response.body as ReadableStream<Uint8Array>,
|
|
86
|
+
...options,
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return response;
|
|
69
91
|
}
|
|
70
92
|
}
|
package/src/types.ts
CHANGED
|
@@ -49,7 +49,7 @@ export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undef
|
|
|
49
49
|
|
|
50
50
|
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
51
51
|
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
52
|
-
} &
|
|
52
|
+
} & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
|
|
53
53
|
|
|
54
54
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
55
55
|
evals: any[];
|