@mastra/client-js 0.0.0-storage-20250225005900 → 0.0.0-taofeeqInngest-20250603090617
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 +19 -16
- package/CHANGELOG.md +1305 -3
- package/{LICENSE → LICENSE.md} +3 -1
- package/README.md +6 -3
- package/dist/index.cjs +1451 -0
- package/dist/index.d.cts +907 -0
- package/dist/index.d.ts +907 -0
- package/dist/index.js +1445 -0
- package/package.json +31 -19
- package/src/adapters/agui.test.ts +180 -0
- package/src/adapters/agui.ts +239 -0
- package/src/client.ts +140 -11
- package/src/example.ts +47 -26
- package/src/index.test.ts +293 -60
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +104 -15
- package/src/resources/base.ts +6 -4
- package/src/resources/index.ts +5 -1
- package/src/resources/legacy-workflow.ts +242 -0
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +8 -5
- package/src/resources/network.ts +86 -0
- package/src/resources/tool.ts +16 -3
- package/src/resources/workflow.ts +309 -24
- package/src/types.ts +178 -27
- package/src/utils/index.ts +11 -0
- package/src/utils/zod-to-json-schema.ts +10 -0
- package/dist/index.d.mts +0 -405
- package/dist/index.mjs +0 -487
package/src/resources/agent.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
2
|
+
import type { GenerateReturn } from '@mastra/core';
|
|
2
3
|
import type { JSONSchema7 } from 'json-schema';
|
|
3
4
|
import { ZodSchema } from 'zod';
|
|
4
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
5
|
+
import { zodToJsonSchema } from '../utils/zod-to-json-schema';
|
|
5
6
|
|
|
6
7
|
import type {
|
|
7
8
|
GenerateParams,
|
|
@@ -13,35 +14,81 @@ import type {
|
|
|
13
14
|
} from '../types';
|
|
14
15
|
|
|
15
16
|
import { BaseResource } from './base';
|
|
17
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
18
|
+
import { parseClientRuntimeContext } from '../utils';
|
|
16
19
|
|
|
17
|
-
export class
|
|
20
|
+
export class AgentVoice extends BaseResource {
|
|
18
21
|
constructor(
|
|
19
22
|
options: ClientOptions,
|
|
20
23
|
private agentId: string,
|
|
21
|
-
private toolId: string,
|
|
22
24
|
) {
|
|
23
25
|
super(options);
|
|
26
|
+
this.agentId = agentId;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @param
|
|
29
|
-
* @
|
|
30
|
+
* Convert text to speech using the agent's voice provider
|
|
31
|
+
* @param text - Text to convert to speech
|
|
32
|
+
* @param options - Optional provider-specific options for speech generation
|
|
33
|
+
* @returns Promise containing the audio data
|
|
34
|
+
*/
|
|
35
|
+
async speak(text: string, options?: { speaker?: string; [key: string]: any }): Promise<Response> {
|
|
36
|
+
return this.request<Response>(`/api/agents/${this.agentId}/voice/speak`, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
},
|
|
41
|
+
body: { input: text, options },
|
|
42
|
+
stream: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Convert speech to text using the agent's voice provider
|
|
48
|
+
* @param audio - Audio data to transcribe
|
|
49
|
+
* @param options - Optional provider-specific options
|
|
50
|
+
* @returns Promise containing the transcribed text
|
|
30
51
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
listen(audio: Blob, options?: Record<string, any>): Promise<{ text: string }> {
|
|
53
|
+
const formData = new FormData();
|
|
54
|
+
formData.append('audio', audio);
|
|
55
|
+
|
|
56
|
+
if (options) {
|
|
57
|
+
formData.append('options', JSON.stringify(options));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return this.request(`/api/agents/${this.agentId}/voice/listen`, {
|
|
33
61
|
method: 'POST',
|
|
34
|
-
body:
|
|
62
|
+
body: formData,
|
|
35
63
|
});
|
|
36
64
|
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get available speakers for the agent's voice provider
|
|
68
|
+
* @returns Promise containing list of available speakers
|
|
69
|
+
*/
|
|
70
|
+
getSpeakers(): Promise<Array<{ voiceId: string; [key: string]: any }>> {
|
|
71
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get the listener configuration for the agent's voice provider
|
|
76
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
77
|
+
*/
|
|
78
|
+
getListener(): Promise<{ enabled: boolean }> {
|
|
79
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener`);
|
|
80
|
+
}
|
|
37
81
|
}
|
|
38
82
|
|
|
39
83
|
export class Agent extends BaseResource {
|
|
84
|
+
public readonly voice: AgentVoice;
|
|
85
|
+
|
|
40
86
|
constructor(
|
|
41
87
|
options: ClientOptions,
|
|
42
88
|
private agentId: string,
|
|
43
89
|
) {
|
|
44
90
|
super(options);
|
|
91
|
+
this.voice = new AgentVoice(options, this.agentId);
|
|
45
92
|
}
|
|
46
93
|
|
|
47
94
|
/**
|
|
@@ -62,7 +109,9 @@ export class Agent extends BaseResource {
|
|
|
62
109
|
): Promise<GenerateReturn<T>> {
|
|
63
110
|
const processedParams = {
|
|
64
111
|
...params,
|
|
65
|
-
output: params.output
|
|
112
|
+
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
113
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
114
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
66
115
|
};
|
|
67
116
|
|
|
68
117
|
return this.request(`/api/agents/${this.agentId}/generate`, {
|
|
@@ -74,19 +123,42 @@ export class Agent extends BaseResource {
|
|
|
74
123
|
/**
|
|
75
124
|
* Streams a response from the agent
|
|
76
125
|
* @param params - Stream parameters including prompt
|
|
77
|
-
* @returns Promise containing the
|
|
126
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
78
127
|
*/
|
|
79
|
-
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
128
|
+
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
129
|
+
params: StreamParams<T>,
|
|
130
|
+
): Promise<
|
|
131
|
+
Response & {
|
|
132
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
> {
|
|
80
135
|
const processedParams = {
|
|
81
136
|
...params,
|
|
82
|
-
output: params.output
|
|
137
|
+
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
138
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
139
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
83
140
|
};
|
|
84
141
|
|
|
85
|
-
|
|
142
|
+
const response: Response & {
|
|
143
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
144
|
+
} = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
86
145
|
method: 'POST',
|
|
87
146
|
body: processedParams,
|
|
88
147
|
stream: true,
|
|
89
148
|
});
|
|
149
|
+
|
|
150
|
+
if (!response.body) {
|
|
151
|
+
throw new Error('No response body');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
response.processDataStream = async (options = {}) => {
|
|
155
|
+
await processDataStream({
|
|
156
|
+
stream: response.body as ReadableStream<Uint8Array>,
|
|
157
|
+
...options,
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return response;
|
|
90
162
|
}
|
|
91
163
|
|
|
92
164
|
/**
|
|
@@ -98,6 +170,23 @@ export class Agent extends BaseResource {
|
|
|
98
170
|
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
99
171
|
}
|
|
100
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Executes a tool for the agent
|
|
175
|
+
* @param toolId - ID of the tool to execute
|
|
176
|
+
* @param params - Parameters required for tool execution
|
|
177
|
+
* @returns Promise containing the tool execution results
|
|
178
|
+
*/
|
|
179
|
+
executeTool(toolId: string, params: { data: any; runtimeContext?: RuntimeContext }): Promise<any> {
|
|
180
|
+
const body = {
|
|
181
|
+
data: params.data,
|
|
182
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined,
|
|
183
|
+
};
|
|
184
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
185
|
+
method: 'POST',
|
|
186
|
+
body,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
101
190
|
/**
|
|
102
191
|
* Retrieves evaluation results for the agent
|
|
103
192
|
* @returns Promise containing agent evaluations
|
package/src/resources/base.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RequestOptions, ClientOptions } from '../types';
|
|
2
2
|
|
|
3
3
|
export class BaseResource {
|
|
4
4
|
readonly options: ClientOptions;
|
|
@@ -21,14 +21,16 @@ export class BaseResource {
|
|
|
21
21
|
|
|
22
22
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
23
23
|
try {
|
|
24
|
-
const response = await fetch(`${baseUrl}${path}`, {
|
|
24
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, '')}${path}`, {
|
|
25
25
|
...options,
|
|
26
26
|
headers: {
|
|
27
|
-
'Content-Type': 'application/json',
|
|
28
27
|
...headers,
|
|
29
28
|
...options.headers,
|
|
29
|
+
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
30
|
+
// 'x-mastra-client-type': 'js',
|
|
30
31
|
},
|
|
31
|
-
body:
|
|
32
|
+
body:
|
|
33
|
+
options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : undefined,
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
if (!response.ok) {
|
package/src/resources/index.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export * from './agent';
|
|
2
|
+
export * from './network';
|
|
2
3
|
export * from './memory-thread';
|
|
3
4
|
export * from './vector';
|
|
4
|
-
export * from './workflow';
|
|
5
|
+
export * from './legacy-workflow';
|
|
5
6
|
export * from './tool';
|
|
6
7
|
export * from './base';
|
|
8
|
+
export * from './workflow';
|
|
9
|
+
export * from './a2a';
|
|
10
|
+
export * from './mcp-tool';
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientOptions,
|
|
3
|
+
LegacyWorkflowRunResult,
|
|
4
|
+
GetLegacyWorkflowRunsResponse,
|
|
5
|
+
GetWorkflowRunsParams,
|
|
6
|
+
GetLegacyWorkflowResponse,
|
|
7
|
+
} from '../types';
|
|
8
|
+
|
|
9
|
+
import { BaseResource } from './base';
|
|
10
|
+
|
|
11
|
+
const RECORD_SEPARATOR = '\x1E';
|
|
12
|
+
|
|
13
|
+
export class LegacyWorkflow extends BaseResource {
|
|
14
|
+
constructor(
|
|
15
|
+
options: ClientOptions,
|
|
16
|
+
private workflowId: string,
|
|
17
|
+
) {
|
|
18
|
+
super(options);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves details about the legacy workflow
|
|
23
|
+
* @returns Promise containing legacy workflow details including steps and graphs
|
|
24
|
+
*/
|
|
25
|
+
details(): Promise<GetLegacyWorkflowResponse> {
|
|
26
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves all runs for a legacy workflow
|
|
31
|
+
* @param params - Parameters for filtering runs
|
|
32
|
+
* @returns Promise containing legacy workflow runs array
|
|
33
|
+
*/
|
|
34
|
+
runs(params?: GetWorkflowRunsParams): Promise<GetLegacyWorkflowRunsResponse> {
|
|
35
|
+
const searchParams = new URLSearchParams();
|
|
36
|
+
if (params?.fromDate) {
|
|
37
|
+
searchParams.set('fromDate', params.fromDate.toISOString());
|
|
38
|
+
}
|
|
39
|
+
if (params?.toDate) {
|
|
40
|
+
searchParams.set('toDate', params.toDate.toISOString());
|
|
41
|
+
}
|
|
42
|
+
if (params?.limit) {
|
|
43
|
+
searchParams.set('limit', String(params.limit));
|
|
44
|
+
}
|
|
45
|
+
if (params?.offset) {
|
|
46
|
+
searchParams.set('offset', String(params.offset));
|
|
47
|
+
}
|
|
48
|
+
if (params?.resourceId) {
|
|
49
|
+
searchParams.set('resourceId', params.resourceId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (searchParams.size) {
|
|
53
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs?${searchParams}`);
|
|
54
|
+
} else {
|
|
55
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new legacy workflow run
|
|
61
|
+
* @returns Promise containing the generated run ID
|
|
62
|
+
*/
|
|
63
|
+
createRun(params?: { runId?: string }): Promise<{ runId: string }> {
|
|
64
|
+
const searchParams = new URLSearchParams();
|
|
65
|
+
|
|
66
|
+
if (!!params?.runId) {
|
|
67
|
+
searchParams.set('runId', params.runId);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
77
|
+
* @param params - Object containing the runId and triggerData
|
|
78
|
+
* @returns Promise containing success message
|
|
79
|
+
*/
|
|
80
|
+
start(params: { runId: string; triggerData: Record<string, any> }): Promise<{ message: string }> {
|
|
81
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
body: params?.triggerData,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
89
|
+
* @param stepId - ID of the step to resume
|
|
90
|
+
* @param runId - ID of the legacy workflow run
|
|
91
|
+
* @param context - Context to resume the legacy workflow with
|
|
92
|
+
* @returns Promise containing the legacy workflow resume results
|
|
93
|
+
*/
|
|
94
|
+
resume({
|
|
95
|
+
stepId,
|
|
96
|
+
runId,
|
|
97
|
+
context,
|
|
98
|
+
}: {
|
|
99
|
+
stepId: string;
|
|
100
|
+
runId: string;
|
|
101
|
+
context: Record<string, any>;
|
|
102
|
+
}): Promise<{ message: string }> {
|
|
103
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
104
|
+
method: 'POST',
|
|
105
|
+
body: {
|
|
106
|
+
stepId,
|
|
107
|
+
context,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
114
|
+
* @param params - Object containing the optional runId and triggerData
|
|
115
|
+
* @returns Promise containing the workflow execution results
|
|
116
|
+
*/
|
|
117
|
+
startAsync(params: { runId?: string; triggerData: Record<string, any> }): Promise<LegacyWorkflowRunResult> {
|
|
118
|
+
const searchParams = new URLSearchParams();
|
|
119
|
+
|
|
120
|
+
if (!!params?.runId) {
|
|
121
|
+
searchParams.set('runId', params.runId);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
125
|
+
method: 'POST',
|
|
126
|
+
body: params?.triggerData,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
132
|
+
* @param params - Object containing the runId, stepId, and context
|
|
133
|
+
* @returns Promise containing the workflow resume results
|
|
134
|
+
*/
|
|
135
|
+
resumeAsync(params: {
|
|
136
|
+
runId: string;
|
|
137
|
+
stepId: string;
|
|
138
|
+
context: Record<string, any>;
|
|
139
|
+
}): Promise<LegacyWorkflowRunResult> {
|
|
140
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
body: {
|
|
143
|
+
stepId: params.stepId,
|
|
144
|
+
context: params.context,
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates an async generator that processes a readable stream and yields records
|
|
151
|
+
* separated by the Record Separator character (\x1E)
|
|
152
|
+
*
|
|
153
|
+
* @param stream - The readable stream to process
|
|
154
|
+
* @returns An async generator that yields parsed records
|
|
155
|
+
*/
|
|
156
|
+
private async *streamProcessor(stream: ReadableStream): AsyncGenerator<LegacyWorkflowRunResult, void, unknown> {
|
|
157
|
+
const reader = stream.getReader();
|
|
158
|
+
|
|
159
|
+
// Track if we've finished reading from the stream
|
|
160
|
+
let doneReading = false;
|
|
161
|
+
// Buffer to accumulate partial chunks
|
|
162
|
+
let buffer = '';
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
while (!doneReading) {
|
|
166
|
+
// Read the next chunk from the stream
|
|
167
|
+
const { done, value } = await reader.read();
|
|
168
|
+
doneReading = done;
|
|
169
|
+
|
|
170
|
+
// Skip processing if we're done and there's no value
|
|
171
|
+
if (done && !value) continue;
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
// Decode binary data to text
|
|
175
|
+
const decoded = value ? new TextDecoder().decode(value) : '';
|
|
176
|
+
|
|
177
|
+
// Split the combined buffer and new data by record separator
|
|
178
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
179
|
+
|
|
180
|
+
// The last chunk might be incomplete, so save it for the next iteration
|
|
181
|
+
buffer = chunks.pop() || '';
|
|
182
|
+
|
|
183
|
+
// Process complete chunks
|
|
184
|
+
for (const chunk of chunks) {
|
|
185
|
+
if (chunk) {
|
|
186
|
+
// Only process non-empty chunks
|
|
187
|
+
if (typeof chunk === 'string') {
|
|
188
|
+
try {
|
|
189
|
+
const parsedChunk = JSON.parse(chunk);
|
|
190
|
+
yield parsedChunk;
|
|
191
|
+
} catch {
|
|
192
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
193
|
+
// This allows the stream to continue even if one record is malformed
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
} catch {
|
|
199
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
200
|
+
// This allows the stream to continue even if one record is malformed
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Process any remaining data in the buffer after stream is done
|
|
205
|
+
if (buffer) {
|
|
206
|
+
try {
|
|
207
|
+
yield JSON.parse(buffer);
|
|
208
|
+
} catch {
|
|
209
|
+
// Ignore parsing error for final chunk
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} finally {
|
|
213
|
+
// Always ensure we clean up the reader
|
|
214
|
+
reader.cancel().catch(() => {
|
|
215
|
+
// Ignore cancel errors
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Watches legacy workflow transitions in real-time
|
|
222
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
223
|
+
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
224
|
+
*/
|
|
225
|
+
async watch({ runId }: { runId?: string }, onRecord: (record: LegacyWorkflowRunResult) => void) {
|
|
226
|
+
const response: Response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
227
|
+
stream: true,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
if (!response.ok) {
|
|
231
|
+
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (!response.body) {
|
|
235
|
+
throw new Error('Response body is null');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
239
|
+
onRecord(record);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
2
|
+
import type { ClientOptions, McpToolInfo } from '../types';
|
|
3
|
+
import { BaseResource } from './base';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Represents a specific tool available on a specific MCP server.
|
|
7
|
+
* Provides methods to get details and execute the tool.
|
|
8
|
+
*/
|
|
9
|
+
export class MCPTool extends BaseResource {
|
|
10
|
+
private serverId: string;
|
|
11
|
+
private toolId: string;
|
|
12
|
+
|
|
13
|
+
constructor(options: ClientOptions, serverId: string, toolId: string) {
|
|
14
|
+
super(options);
|
|
15
|
+
this.serverId = serverId;
|
|
16
|
+
this.toolId = toolId;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
21
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
22
|
+
*/
|
|
23
|
+
details(): Promise<McpToolInfo> {
|
|
24
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Executes this specific tool on the MCP server.
|
|
29
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
30
|
+
* @returns Promise containing the result of the tool execution.
|
|
31
|
+
*/
|
|
32
|
+
execute(params: { data?: any; runtimeContext?: RuntimeContext }): Promise<any> {
|
|
33
|
+
const body: any = {};
|
|
34
|
+
if (params.data !== undefined) body.data = params.data;
|
|
35
|
+
// If none of data, args the body might be empty or just contain runtimeContext.
|
|
36
|
+
// The handler will look for these, so an empty args object might be appropriate if that's the intent.
|
|
37
|
+
// else body.data = {}; // Or let it be empty if no specific input fields are used
|
|
38
|
+
|
|
39
|
+
if (params.runtimeContext !== undefined) {
|
|
40
|
+
body.runtimeContext = params.runtimeContext;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
body: Object.keys(body).length > 0 ? body : undefined,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import type { StorageThreadType } from '@mastra/core';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
CreateMemoryThreadParams,
|
|
5
4
|
GetMemoryThreadMessagesResponse,
|
|
6
|
-
GetMemoryThreadResponse,
|
|
7
5
|
ClientOptions,
|
|
8
|
-
SaveMessageToMemoryParams,
|
|
9
6
|
UpdateMemoryThreadParams,
|
|
7
|
+
GetMemoryThreadMessagesParams,
|
|
10
8
|
} from '../types';
|
|
11
9
|
|
|
12
10
|
import { BaseResource } from './base';
|
|
@@ -52,9 +50,14 @@ export class MemoryThread extends BaseResource {
|
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
52
|
* Retrieves messages associated with the thread
|
|
53
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
55
54
|
* @returns Promise containing thread messages and UI messages
|
|
56
55
|
*/
|
|
57
|
-
getMessages(): Promise<GetMemoryThreadMessagesResponse> {
|
|
58
|
-
|
|
56
|
+
getMessages(params?: GetMemoryThreadMessagesParams): Promise<GetMemoryThreadMessagesResponse> {
|
|
57
|
+
const query = new URLSearchParams({
|
|
58
|
+
agentId: this.agentId,
|
|
59
|
+
...(params?.limit ? { limit: params.limit.toString() } : {}),
|
|
60
|
+
});
|
|
61
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
59
62
|
}
|
|
60
63
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
2
|
+
import type { GenerateReturn } from '@mastra/core';
|
|
3
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
4
|
+
import { ZodSchema } from 'zod';
|
|
5
|
+
import { zodToJsonSchema } from '../utils/zod-to-json-schema';
|
|
6
|
+
|
|
7
|
+
import type { GenerateParams, ClientOptions, StreamParams, GetNetworkResponse } from '../types';
|
|
8
|
+
|
|
9
|
+
import { BaseResource } from './base';
|
|
10
|
+
|
|
11
|
+
export class Network extends BaseResource {
|
|
12
|
+
constructor(
|
|
13
|
+
options: ClientOptions,
|
|
14
|
+
private networkId: string,
|
|
15
|
+
) {
|
|
16
|
+
super(options);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves details about the network
|
|
21
|
+
* @returns Promise containing network details
|
|
22
|
+
*/
|
|
23
|
+
details(): Promise<GetNetworkResponse> {
|
|
24
|
+
return this.request(`/api/networks/${this.networkId}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Generates a response from the agent
|
|
29
|
+
* @param params - Generation parameters including prompt
|
|
30
|
+
* @returns Promise containing the generated response
|
|
31
|
+
*/
|
|
32
|
+
generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
33
|
+
params: GenerateParams<T>,
|
|
34
|
+
): Promise<GenerateReturn<T>> {
|
|
35
|
+
const processedParams = {
|
|
36
|
+
...params,
|
|
37
|
+
output: zodToJsonSchema(params.output),
|
|
38
|
+
experimental_output: zodToJsonSchema(params.experimental_output),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
body: processedParams,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Streams a response from the agent
|
|
49
|
+
* @param params - Stream parameters including prompt
|
|
50
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
51
|
+
*/
|
|
52
|
+
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
53
|
+
params: StreamParams<T>,
|
|
54
|
+
): Promise<
|
|
55
|
+
Response & {
|
|
56
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
> {
|
|
59
|
+
const processedParams = {
|
|
60
|
+
...params,
|
|
61
|
+
output: zodToJsonSchema(params.output),
|
|
62
|
+
experimental_output: zodToJsonSchema(params.experimental_output),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const response: Response & {
|
|
66
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
67
|
+
} = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
body: processedParams,
|
|
70
|
+
stream: true,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (!response.body) {
|
|
74
|
+
throw new Error('No response body');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
response.processDataStream = async (options = {}) => {
|
|
78
|
+
await processDataStream({
|
|
79
|
+
stream: response.body as ReadableStream<Uint8Array>,
|
|
80
|
+
...options,
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return response;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/resources/tool.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
1
2
|
import type { GetToolResponse, ClientOptions } from '../types';
|
|
2
3
|
|
|
3
4
|
import { BaseResource } from './base';
|
|
5
|
+
import { parseClientRuntimeContext } from '../utils';
|
|
4
6
|
|
|
5
7
|
export class Tool extends BaseResource {
|
|
6
8
|
constructor(
|
|
@@ -23,10 +25,21 @@ export class Tool extends BaseResource {
|
|
|
23
25
|
* @param params - Parameters required for tool execution
|
|
24
26
|
* @returns Promise containing the tool execution results
|
|
25
27
|
*/
|
|
26
|
-
execute(params: { data: any }): Promise<any> {
|
|
27
|
-
|
|
28
|
+
execute(params: { data: any; runId?: string; runtimeContext?: RuntimeContext | Record<string, any> }): Promise<any> {
|
|
29
|
+
const url = new URLSearchParams();
|
|
30
|
+
|
|
31
|
+
if (params.runId) {
|
|
32
|
+
url.set('runId', params.runId);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const body = {
|
|
36
|
+
data: params.data,
|
|
37
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
28
41
|
method: 'POST',
|
|
29
|
-
body
|
|
42
|
+
body,
|
|
30
43
|
});
|
|
31
44
|
}
|
|
32
45
|
}
|