@mastra/client-js 0.0.0-taofeeqInngest-20250603090617 → 0.0.0-tool-call-parts-20250630193309
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 +9 -9
- package/CHANGELOG.md +322 -2
- package/README.md +1 -1
- package/dist/index.cjs +754 -23
- package/dist/index.d.cts +239 -13
- package/dist/index.d.ts +239 -13
- package/dist/index.js +755 -24
- package/package.json +20 -16
- package/src/client.ts +149 -3
- package/src/example.ts +4 -1
- package/src/resources/agent.ts +550 -10
- package/src/resources/base.ts +1 -0
- package/src/resources/network-memory-thread.ts +63 -0
- package/src/resources/network.ts +2 -3
- package/src/resources/vNextNetwork.ts +177 -0
- package/src/resources/workflow.ts +24 -4
- package/src/types.ts +102 -5
- package/src/utils/process-client-tools.ts +32 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { WatchEvent } from '@mastra/core/workflows';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ClientOptions,
|
|
5
|
+
GetVNextNetworkResponse,
|
|
6
|
+
GenerateVNextNetworkResponse,
|
|
7
|
+
LoopVNextNetworkResponse,
|
|
8
|
+
GenerateOrStreamVNextNetworkParams,
|
|
9
|
+
LoopStreamVNextNetworkParams,
|
|
10
|
+
} from '../types';
|
|
11
|
+
|
|
12
|
+
import { BaseResource } from './base';
|
|
13
|
+
|
|
14
|
+
const RECORD_SEPARATOR = '\x1E';
|
|
15
|
+
|
|
16
|
+
export class VNextNetwork extends BaseResource {
|
|
17
|
+
constructor(
|
|
18
|
+
options: ClientOptions,
|
|
19
|
+
private networkId: string,
|
|
20
|
+
) {
|
|
21
|
+
super(options);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves details about the network
|
|
26
|
+
* @returns Promise containing vNext network details
|
|
27
|
+
*/
|
|
28
|
+
details(): Promise<GetVNextNetworkResponse> {
|
|
29
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Generates a response from the v-next network
|
|
34
|
+
* @param params - Generation parameters including message
|
|
35
|
+
* @returns Promise containing the generated response
|
|
36
|
+
*/
|
|
37
|
+
generate(params: GenerateOrStreamVNextNetworkParams): Promise<GenerateVNextNetworkResponse> {
|
|
38
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
body: params,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Generates a response from the v-next network using multiple primitives
|
|
46
|
+
* @param params - Generation parameters including message
|
|
47
|
+
* @returns Promise containing the generated response
|
|
48
|
+
*/
|
|
49
|
+
loop(params: { message: string }): Promise<LoopVNextNetworkResponse> {
|
|
50
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
body: params,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WatchEvent, void, unknown> {
|
|
57
|
+
const reader = stream.getReader();
|
|
58
|
+
|
|
59
|
+
// Track if we've finished reading from the stream
|
|
60
|
+
let doneReading = false;
|
|
61
|
+
// Buffer to accumulate partial chunks
|
|
62
|
+
let buffer = '';
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
while (!doneReading) {
|
|
66
|
+
// Read the next chunk from the stream
|
|
67
|
+
const { done, value } = await reader.read();
|
|
68
|
+
doneReading = done;
|
|
69
|
+
|
|
70
|
+
// Skip processing if we're done and there's no value
|
|
71
|
+
if (done && !value) continue;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
// Decode binary data to text
|
|
75
|
+
const decoded = value ? new TextDecoder().decode(value) : '';
|
|
76
|
+
|
|
77
|
+
// Split the combined buffer and new data by record separator
|
|
78
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
79
|
+
|
|
80
|
+
// The last chunk might be incomplete, so save it for the next iteration
|
|
81
|
+
buffer = chunks.pop() || '';
|
|
82
|
+
|
|
83
|
+
// Process complete chunks
|
|
84
|
+
for (const chunk of chunks) {
|
|
85
|
+
if (chunk) {
|
|
86
|
+
// Only process non-empty chunks
|
|
87
|
+
if (typeof chunk === 'string') {
|
|
88
|
+
try {
|
|
89
|
+
const parsedChunk = JSON.parse(chunk);
|
|
90
|
+
yield parsedChunk;
|
|
91
|
+
} catch {
|
|
92
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
93
|
+
// This allows the stream to continue even if one record is malformed
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} catch {
|
|
99
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
100
|
+
// This allows the stream to continue even if one record is malformed
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Process any remaining data in the buffer after stream is done
|
|
105
|
+
if (buffer) {
|
|
106
|
+
try {
|
|
107
|
+
yield JSON.parse(buffer);
|
|
108
|
+
} catch {
|
|
109
|
+
// Ignore parsing error for final chunk
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} finally {
|
|
113
|
+
// Always ensure we clean up the reader
|
|
114
|
+
reader.cancel().catch(() => {
|
|
115
|
+
// Ignore cancel errors
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Streams a response from the v-next network
|
|
122
|
+
* @param params - Stream parameters including message
|
|
123
|
+
* @returns Promise containing the results
|
|
124
|
+
*/
|
|
125
|
+
async stream(params: GenerateOrStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
|
|
126
|
+
const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
body: params,
|
|
129
|
+
stream: true,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!response.body) {
|
|
137
|
+
throw new Error('Response body is null');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
141
|
+
if (typeof record === 'string') {
|
|
142
|
+
onRecord(JSON.parse(record));
|
|
143
|
+
} else {
|
|
144
|
+
onRecord(record);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Streams a response from the v-next network loop
|
|
151
|
+
* @param params - Stream parameters including message
|
|
152
|
+
* @returns Promise containing the results
|
|
153
|
+
*/
|
|
154
|
+
async loopStream(params: LoopStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
|
|
155
|
+
const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
156
|
+
method: 'POST',
|
|
157
|
+
body: params,
|
|
158
|
+
stream: true,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!response.body) {
|
|
166
|
+
throw new Error('Response body is null');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
170
|
+
if (typeof record === 'string') {
|
|
171
|
+
onRecord(JSON.parse(record));
|
|
172
|
+
} else {
|
|
173
|
+
onRecord(record);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -6,6 +6,8 @@ import type {
|
|
|
6
6
|
GetWorkflowRunsParams,
|
|
7
7
|
WorkflowRunResult,
|
|
8
8
|
WorkflowWatchResult,
|
|
9
|
+
GetWorkflowRunByIdResponse,
|
|
10
|
+
GetWorkflowRunExecutionResultResponse,
|
|
9
11
|
} from '../types';
|
|
10
12
|
|
|
11
13
|
import { parseClientRuntimeContext } from '../utils';
|
|
@@ -130,6 +132,24 @@ export class Workflow extends BaseResource {
|
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Retrieves a specific workflow run by its ID
|
|
137
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
138
|
+
* @returns Promise containing the workflow run details
|
|
139
|
+
*/
|
|
140
|
+
runById(runId: string): Promise<GetWorkflowRunByIdResponse> {
|
|
141
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
146
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
147
|
+
* @returns Promise containing the workflow run execution result
|
|
148
|
+
*/
|
|
149
|
+
runExecutionResult(runId: string): Promise<GetWorkflowRunExecutionResultResponse> {
|
|
150
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
151
|
+
}
|
|
152
|
+
|
|
133
153
|
/**
|
|
134
154
|
* Creates a new workflow run
|
|
135
155
|
* @param params - Optional object containing the optional runId
|
|
@@ -217,9 +237,9 @@ export class Workflow extends BaseResource {
|
|
|
217
237
|
}
|
|
218
238
|
|
|
219
239
|
/**
|
|
220
|
-
* Starts a
|
|
240
|
+
* Starts a workflow run and returns a stream
|
|
221
241
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
222
|
-
* @returns Promise containing the
|
|
242
|
+
* @returns Promise containing the workflow execution results
|
|
223
243
|
*/
|
|
224
244
|
async stream(params: { runId?: string; inputData: Record<string, any>; runtimeContext?: RuntimeContext }) {
|
|
225
245
|
const searchParams = new URLSearchParams();
|
|
@@ -228,7 +248,7 @@ export class Workflow extends BaseResource {
|
|
|
228
248
|
searchParams.set('runId', params.runId);
|
|
229
249
|
}
|
|
230
250
|
|
|
231
|
-
const runtimeContext =
|
|
251
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
232
252
|
const response: Response = await this.request(
|
|
233
253
|
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
234
254
|
{
|
|
@@ -247,7 +267,7 @@ export class Workflow extends BaseResource {
|
|
|
247
267
|
}
|
|
248
268
|
|
|
249
269
|
// Create a transform stream that processes the response body
|
|
250
|
-
const transformStream = new TransformStream<ArrayBuffer,
|
|
270
|
+
const transformStream = new TransformStream<ArrayBuffer, { type: string; payload: any }>({
|
|
251
271
|
start() {},
|
|
252
272
|
async transform(chunk, controller) {
|
|
253
273
|
try {
|
package/src/types.ts
CHANGED
|
@@ -5,10 +5,11 @@ import type {
|
|
|
5
5
|
QueryResult,
|
|
6
6
|
StorageThreadType,
|
|
7
7
|
WorkflowRuns,
|
|
8
|
+
WorkflowRun,
|
|
8
9
|
LegacyWorkflowRuns,
|
|
9
10
|
} from '@mastra/core';
|
|
10
|
-
import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
11
|
-
import type { BaseLogMessage } from '@mastra/core/logger';
|
|
11
|
+
import type { AgentGenerateOptions, AgentStreamOptions, ToolsInput } from '@mastra/core/agent';
|
|
12
|
+
import type { BaseLogMessage, LogLevel } from '@mastra/core/logger';
|
|
12
13
|
|
|
13
14
|
import type { MCPToolType, ServerInfo } from '@mastra/core/mcp';
|
|
14
15
|
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
@@ -69,14 +70,16 @@ export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undef
|
|
|
69
70
|
output?: T;
|
|
70
71
|
experimental_output?: T;
|
|
71
72
|
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
72
|
-
|
|
73
|
+
clientTools?: ToolsInput;
|
|
74
|
+
} & WithoutMethods<Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools'>>;
|
|
73
75
|
|
|
74
76
|
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
75
77
|
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
76
78
|
output?: T;
|
|
77
79
|
experimental_output?: T;
|
|
78
80
|
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
79
|
-
|
|
81
|
+
clientTools?: ToolsInput;
|
|
82
|
+
} & WithoutMethods<Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools'>>;
|
|
80
83
|
|
|
81
84
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
82
85
|
evals: any[];
|
|
@@ -113,6 +116,10 @@ export type GetLegacyWorkflowRunsResponse = LegacyWorkflowRuns;
|
|
|
113
116
|
|
|
114
117
|
export type GetWorkflowRunsResponse = WorkflowRuns;
|
|
115
118
|
|
|
119
|
+
export type GetWorkflowRunByIdResponse = WorkflowRun;
|
|
120
|
+
|
|
121
|
+
export type GetWorkflowRunExecutionResultResponse = WatchEvent['payload']['workflowState'];
|
|
122
|
+
|
|
116
123
|
export type LegacyWorkflowRunResult = {
|
|
117
124
|
activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
|
|
118
125
|
results: CoreLegacyWorkflowRunResult<any, any, any>['results'];
|
|
@@ -176,6 +183,11 @@ export interface SaveMessageToMemoryParams {
|
|
|
176
183
|
agentId: string;
|
|
177
184
|
}
|
|
178
185
|
|
|
186
|
+
export interface SaveNetworkMessageToMemoryParams {
|
|
187
|
+
messages: MastraMessageV1[];
|
|
188
|
+
networkId: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
179
191
|
export type SaveMessageToMemoryResponse = MastraMessageV1[];
|
|
180
192
|
|
|
181
193
|
export interface CreateMemoryThreadParams {
|
|
@@ -186,6 +198,14 @@ export interface CreateMemoryThreadParams {
|
|
|
186
198
|
agentId: string;
|
|
187
199
|
}
|
|
188
200
|
|
|
201
|
+
export interface CreateNetworkMemoryThreadParams {
|
|
202
|
+
title?: string;
|
|
203
|
+
metadata?: Record<string, any>;
|
|
204
|
+
resourceId: string;
|
|
205
|
+
threadId?: string;
|
|
206
|
+
networkId: string;
|
|
207
|
+
}
|
|
208
|
+
|
|
189
209
|
export type CreateMemoryThreadResponse = StorageThreadType;
|
|
190
210
|
|
|
191
211
|
export interface GetMemoryThreadParams {
|
|
@@ -193,6 +213,11 @@ export interface GetMemoryThreadParams {
|
|
|
193
213
|
agentId: string;
|
|
194
214
|
}
|
|
195
215
|
|
|
216
|
+
export interface GetNetworkMemoryThreadParams {
|
|
217
|
+
resourceId: string;
|
|
218
|
+
networkId: string;
|
|
219
|
+
}
|
|
220
|
+
|
|
196
221
|
export type GetMemoryThreadResponse = StorageThreadType[];
|
|
197
222
|
|
|
198
223
|
export interface UpdateMemoryThreadParams {
|
|
@@ -215,14 +240,32 @@ export interface GetMemoryThreadMessagesResponse {
|
|
|
215
240
|
|
|
216
241
|
export interface GetLogsParams {
|
|
217
242
|
transportId: string;
|
|
243
|
+
fromDate?: Date;
|
|
244
|
+
toDate?: Date;
|
|
245
|
+
logLevel?: LogLevel;
|
|
246
|
+
filters?: Record<string, string>;
|
|
247
|
+
page?: number;
|
|
248
|
+
perPage?: number;
|
|
218
249
|
}
|
|
219
250
|
|
|
220
251
|
export interface GetLogParams {
|
|
221
252
|
runId: string;
|
|
222
253
|
transportId: string;
|
|
254
|
+
fromDate?: Date;
|
|
255
|
+
toDate?: Date;
|
|
256
|
+
logLevel?: LogLevel;
|
|
257
|
+
filters?: Record<string, string>;
|
|
258
|
+
page?: number;
|
|
259
|
+
perPage?: number;
|
|
223
260
|
}
|
|
224
261
|
|
|
225
|
-
export type GetLogsResponse =
|
|
262
|
+
export type GetLogsResponse = {
|
|
263
|
+
logs: BaseLogMessage[];
|
|
264
|
+
total: number;
|
|
265
|
+
page: number;
|
|
266
|
+
perPage: number;
|
|
267
|
+
hasMore: boolean;
|
|
268
|
+
};
|
|
226
269
|
|
|
227
270
|
export type RequestFunction = (path: string, options?: RequestOptions) => Promise<any>;
|
|
228
271
|
|
|
@@ -281,6 +324,7 @@ export interface GetTelemetryParams {
|
|
|
281
324
|
}
|
|
282
325
|
|
|
283
326
|
export interface GetNetworkResponse {
|
|
327
|
+
id: string;
|
|
284
328
|
name: string;
|
|
285
329
|
instructions: string;
|
|
286
330
|
agents: Array<{
|
|
@@ -295,6 +339,59 @@ export interface GetNetworkResponse {
|
|
|
295
339
|
state?: Record<string, any>;
|
|
296
340
|
}
|
|
297
341
|
|
|
342
|
+
export interface GetVNextNetworkResponse {
|
|
343
|
+
id: string;
|
|
344
|
+
name: string;
|
|
345
|
+
instructions: string;
|
|
346
|
+
agents: Array<{
|
|
347
|
+
name: string;
|
|
348
|
+
provider: string;
|
|
349
|
+
modelId: string;
|
|
350
|
+
}>;
|
|
351
|
+
routingModel: {
|
|
352
|
+
provider: string;
|
|
353
|
+
modelId: string;
|
|
354
|
+
};
|
|
355
|
+
workflows: Array<{
|
|
356
|
+
name: string;
|
|
357
|
+
description: string;
|
|
358
|
+
inputSchema: string | undefined;
|
|
359
|
+
outputSchema: string | undefined;
|
|
360
|
+
}>;
|
|
361
|
+
tools: Array<{
|
|
362
|
+
id: string;
|
|
363
|
+
description: string;
|
|
364
|
+
}>;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export interface GenerateVNextNetworkResponse {
|
|
368
|
+
task: string;
|
|
369
|
+
result: string;
|
|
370
|
+
resourceId: string;
|
|
371
|
+
resourceType: 'none' | 'tool' | 'agent' | 'workflow';
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export interface GenerateOrStreamVNextNetworkParams {
|
|
375
|
+
message: string;
|
|
376
|
+
threadId?: string;
|
|
377
|
+
resourceId?: string;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export interface LoopStreamVNextNetworkParams {
|
|
381
|
+
message: string;
|
|
382
|
+
threadId?: string;
|
|
383
|
+
resourceId?: string;
|
|
384
|
+
maxIterations?: number;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface LoopVNextNetworkResponse {
|
|
388
|
+
status: 'success';
|
|
389
|
+
result: {
|
|
390
|
+
text: string;
|
|
391
|
+
};
|
|
392
|
+
steps: WorkflowResult<any, any>['steps'];
|
|
393
|
+
}
|
|
394
|
+
|
|
298
395
|
export interface McpServerListResponse {
|
|
299
396
|
servers: ServerInfo[];
|
|
300
397
|
next: string | null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isVercelTool } from '@mastra/core/tools';
|
|
2
|
+
import { zodToJsonSchema } from './zod-to-json-schema';
|
|
3
|
+
import type { ToolsInput } from '@mastra/core/agent';
|
|
4
|
+
|
|
5
|
+
export function processClientTools(clientTools: ToolsInput | undefined): ToolsInput | undefined {
|
|
6
|
+
if (!clientTools) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return Object.fromEntries(
|
|
11
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
12
|
+
if (isVercelTool(value)) {
|
|
13
|
+
return [
|
|
14
|
+
key,
|
|
15
|
+
{
|
|
16
|
+
...value,
|
|
17
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : undefined,
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
} else {
|
|
21
|
+
return [
|
|
22
|
+
key,
|
|
23
|
+
{
|
|
24
|
+
...value,
|
|
25
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : undefined,
|
|
26
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : undefined,
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
}
|