@mastra/client-js 0.0.0-storage-20250225005900 → 0.0.0-vnextWorkflows-20250416071310
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +726 -3
- package/{LICENSE → LICENSE.md} +3 -1
- package/README.md +6 -3
- package/dist/index.cjs +729 -0
- package/dist/{index.d.mts → index.d.cts} +202 -22
- package/dist/index.d.ts +585 -0
- package/dist/{index.mjs → index.js} +256 -16
- package/package.json +25 -19
- package/src/client.ts +19 -1
- package/src/example.ts +65 -43
- package/src/index.test.ts +170 -57
- package/src/resources/agent.ts +93 -4
- package/src/resources/base.ts +4 -2
- package/src/resources/index.ts +1 -0
- package/src/resources/network.ts +92 -0
- package/src/resources/workflow.ts +156 -9
- package/src/types.ts +76 -15
- package/.turbo/turbo-build.log +0 -16
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { GetWorkflowResponse, ClientOptions } from '../types';
|
|
1
|
+
import type { GetWorkflowResponse, ClientOptions, WorkflowRunResult } from '../types';
|
|
2
2
|
|
|
3
3
|
import { BaseResource } from './base';
|
|
4
4
|
|
|
5
|
+
const RECORD_SEPARATOR = '\x1E';
|
|
6
|
+
|
|
5
7
|
export class Workflow extends BaseResource {
|
|
6
8
|
constructor(
|
|
7
9
|
options: ClientOptions,
|
|
@@ -19,11 +21,12 @@ export class Workflow extends BaseResource {
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
/**
|
|
24
|
+
* @deprecated Use `startAsync` instead
|
|
22
25
|
* Executes the workflow with the provided parameters
|
|
23
26
|
* @param params - Parameters required for workflow execution
|
|
24
27
|
* @returns Promise containing the workflow execution results
|
|
25
28
|
*/
|
|
26
|
-
execute(params: Record<string, any>): Promise<
|
|
29
|
+
execute(params: Record<string, any>): Promise<WorkflowRunResult> {
|
|
27
30
|
return this.request(`/api/workflows/${this.workflowId}/execute`, {
|
|
28
31
|
method: 'POST',
|
|
29
32
|
body: params,
|
|
@@ -31,7 +34,35 @@ export class Workflow extends BaseResource {
|
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
/**
|
|
34
|
-
*
|
|
37
|
+
* Creates a new workflow run
|
|
38
|
+
* @returns Promise containing the generated run ID
|
|
39
|
+
*/
|
|
40
|
+
createRun(params?: { runId?: string }): Promise<{ runId: string }> {
|
|
41
|
+
const searchParams = new URLSearchParams();
|
|
42
|
+
|
|
43
|
+
if (!!params?.runId) {
|
|
44
|
+
searchParams.set('runId', params.runId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return this.request(`/api/workflows/${this.workflowId}/createRun?${searchParams.toString()}`, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
54
|
+
* @param params - Object containing the runId and triggerData
|
|
55
|
+
* @returns Promise containing success message
|
|
56
|
+
*/
|
|
57
|
+
start(params: { runId: string; triggerData: Record<string, any> }): Promise<{ message: string }> {
|
|
58
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
body: params?.triggerData,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
35
66
|
* @param stepId - ID of the step to resume
|
|
36
67
|
* @param runId - ID of the workflow run
|
|
37
68
|
* @param context - Context to resume the workflow with
|
|
@@ -45,24 +76,140 @@ export class Workflow extends BaseResource {
|
|
|
45
76
|
stepId: string;
|
|
46
77
|
runId: string;
|
|
47
78
|
context: Record<string, any>;
|
|
48
|
-
}): Promise<
|
|
49
|
-
return this.request(`/api/workflows/${this.workflowId}/resume`, {
|
|
79
|
+
}): Promise<{ message: string }> {
|
|
80
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
50
81
|
method: 'POST',
|
|
51
82
|
body: {
|
|
52
83
|
stepId,
|
|
53
|
-
runId,
|
|
54
84
|
context,
|
|
55
85
|
},
|
|
56
86
|
});
|
|
57
87
|
}
|
|
58
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
91
|
+
* @param params - Object containing the optional runId and triggerData
|
|
92
|
+
* @returns Promise containing the workflow execution results
|
|
93
|
+
*/
|
|
94
|
+
startAsync(params: { runId?: string; triggerData: Record<string, any> }): Promise<WorkflowRunResult> {
|
|
95
|
+
const searchParams = new URLSearchParams();
|
|
96
|
+
|
|
97
|
+
if (!!params?.runId) {
|
|
98
|
+
searchParams.set('runId', params.runId);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
body: params?.triggerData,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
109
|
+
* @param params - Object containing the runId, stepId, and context
|
|
110
|
+
* @returns Promise containing the workflow resume results
|
|
111
|
+
*/
|
|
112
|
+
resumeAsync(params: { runId: string; stepId: string; context: Record<string, any> }): Promise<WorkflowRunResult> {
|
|
113
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
114
|
+
method: 'POST',
|
|
115
|
+
body: {
|
|
116
|
+
stepId: params.stepId,
|
|
117
|
+
context: params.context,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Creates an async generator that processes a readable stream and yields records
|
|
124
|
+
* separated by the Record Separator character (\x1E)
|
|
125
|
+
*
|
|
126
|
+
* @param stream - The readable stream to process
|
|
127
|
+
* @returns An async generator that yields parsed records
|
|
128
|
+
*/
|
|
129
|
+
private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WorkflowRunResult, void, unknown> {
|
|
130
|
+
const reader = stream.getReader();
|
|
131
|
+
|
|
132
|
+
// Track if we've finished reading from the stream
|
|
133
|
+
let doneReading = false;
|
|
134
|
+
// Buffer to accumulate partial chunks
|
|
135
|
+
let buffer = '';
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
while (!doneReading) {
|
|
139
|
+
// Read the next chunk from the stream
|
|
140
|
+
const { done, value } = await reader.read();
|
|
141
|
+
doneReading = done;
|
|
142
|
+
|
|
143
|
+
// Skip processing if we're done and there's no value
|
|
144
|
+
if (done && !value) continue;
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
// Decode binary data to text
|
|
148
|
+
const decoded = value ? new TextDecoder().decode(value) : '';
|
|
149
|
+
|
|
150
|
+
// Split the combined buffer and new data by record separator
|
|
151
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
152
|
+
|
|
153
|
+
// The last chunk might be incomplete, so save it for the next iteration
|
|
154
|
+
buffer = chunks.pop() || '';
|
|
155
|
+
|
|
156
|
+
// Process complete chunks
|
|
157
|
+
for (const chunk of chunks) {
|
|
158
|
+
if (chunk) {
|
|
159
|
+
// Only process non-empty chunks
|
|
160
|
+
if (typeof chunk === 'string') {
|
|
161
|
+
try {
|
|
162
|
+
const parsedChunk = JSON.parse(chunk);
|
|
163
|
+
yield parsedChunk;
|
|
164
|
+
} catch {
|
|
165
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
166
|
+
// This allows the stream to continue even if one record is malformed
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
173
|
+
// This allows the stream to continue even if one record is malformed
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Process any remaining data in the buffer after stream is done
|
|
178
|
+
if (buffer) {
|
|
179
|
+
try {
|
|
180
|
+
yield JSON.parse(buffer);
|
|
181
|
+
} catch {
|
|
182
|
+
// Ignore parsing error for final chunk
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} finally {
|
|
186
|
+
// Always ensure we clean up the reader
|
|
187
|
+
reader.cancel().catch(() => {
|
|
188
|
+
// Ignore cancel errors
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
59
193
|
/**
|
|
60
194
|
* Watches workflow transitions in real-time
|
|
61
|
-
* @
|
|
195
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
196
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
62
197
|
*/
|
|
63
|
-
watch(
|
|
64
|
-
|
|
198
|
+
async watch({ runId }: { runId?: string }, onRecord: (record: WorkflowRunResult) => void) {
|
|
199
|
+
const response: Response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
65
200
|
stream: true,
|
|
66
201
|
});
|
|
202
|
+
|
|
203
|
+
if (!response.ok) {
|
|
204
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!response.body) {
|
|
208
|
+
throw new Error('Response body is null');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
212
|
+
onRecord(record);
|
|
213
|
+
}
|
|
67
214
|
}
|
|
68
215
|
}
|
package/src/types.ts
CHANGED
|
@@ -7,10 +7,12 @@ import type {
|
|
|
7
7
|
StepGraph,
|
|
8
8
|
StorageThreadType,
|
|
9
9
|
BaseLogMessage,
|
|
10
|
-
|
|
10
|
+
WorkflowRunResult as CoreWorkflowRunResult,
|
|
11
11
|
} from '@mastra/core';
|
|
12
|
+
|
|
13
|
+
import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
12
14
|
import type { JSONSchema7 } from 'json-schema';
|
|
13
|
-
import { ZodSchema } from 'zod';
|
|
15
|
+
import type { ZodSchema } from 'zod';
|
|
14
16
|
|
|
15
17
|
export interface ClientOptions {
|
|
16
18
|
/** Base URL for API requests */
|
|
@@ -23,6 +25,7 @@ export interface ClientOptions {
|
|
|
23
25
|
maxBackoffMs?: number;
|
|
24
26
|
/** Custom headers to include with requests */
|
|
25
27
|
headers?: Record<string, string>;
|
|
28
|
+
/** Abort signal for request */
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
export interface RequestOptions {
|
|
@@ -30,6 +33,7 @@ export interface RequestOptions {
|
|
|
30
33
|
headers?: Record<string, string>;
|
|
31
34
|
body?: any;
|
|
32
35
|
stream?: boolean;
|
|
36
|
+
signal?: AbortSignal;
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
export interface GetAgentResponse {
|
|
@@ -37,21 +41,16 @@ export interface GetAgentResponse {
|
|
|
37
41
|
instructions: string;
|
|
38
42
|
tools: Record<string, GetToolResponse>;
|
|
39
43
|
provider: string;
|
|
44
|
+
modelId: string;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
export
|
|
43
|
-
messages: string | string[] | CoreMessage[];
|
|
44
|
-
|
|
45
|
-
resourceid?: string;
|
|
46
|
-
output?: OutputType | T;
|
|
47
|
-
}
|
|
47
|
+
export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
48
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
49
|
+
} & Partial<AgentGenerateOptions<T>>;
|
|
48
50
|
|
|
49
|
-
export
|
|
50
|
-
messages: string | string[] | CoreMessage[];
|
|
51
|
-
|
|
52
|
-
resourceid?: string;
|
|
53
|
-
output?: OutputType | T;
|
|
54
|
-
}
|
|
51
|
+
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
52
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
53
|
+
} & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
|
|
55
54
|
|
|
56
55
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
57
56
|
evals: any[];
|
|
@@ -70,8 +69,15 @@ export interface GetWorkflowResponse {
|
|
|
70
69
|
steps: Record<string, StepAction<any, any, any, any>>;
|
|
71
70
|
stepGraph: StepGraph;
|
|
72
71
|
stepSubscriberGraph: Record<string, StepGraph>;
|
|
72
|
+
workflowId?: string;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
export type WorkflowRunResult = {
|
|
76
|
+
activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
|
|
77
|
+
results: CoreWorkflowRunResult<any, any, any>['results'];
|
|
78
|
+
timestamp: number;
|
|
79
|
+
runId: string;
|
|
80
|
+
};
|
|
75
81
|
export interface UpsertVectorParams {
|
|
76
82
|
indexName: string;
|
|
77
83
|
vectors: number[][];
|
|
@@ -150,8 +156,48 @@ export type GetLogsResponse = BaseLogMessage[];
|
|
|
150
156
|
|
|
151
157
|
export type RequestFunction = (path: string, options?: RequestOptions) => Promise<any>;
|
|
152
158
|
|
|
159
|
+
type SpanStatus = {
|
|
160
|
+
code: number;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
type SpanOther = {
|
|
164
|
+
droppedAttributesCount: number;
|
|
165
|
+
droppedEventsCount: number;
|
|
166
|
+
droppedLinksCount: number;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
type SpanEventAttributes = {
|
|
170
|
+
key: string;
|
|
171
|
+
value: { [key: string]: string | number | boolean | null };
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
type SpanEvent = {
|
|
175
|
+
attributes: SpanEventAttributes[];
|
|
176
|
+
name: string;
|
|
177
|
+
timeUnixNano: string;
|
|
178
|
+
droppedAttributesCount: number;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
type Span = {
|
|
182
|
+
id: string;
|
|
183
|
+
parentSpanId: string | null;
|
|
184
|
+
traceId: string;
|
|
185
|
+
name: string;
|
|
186
|
+
scope: string;
|
|
187
|
+
kind: number;
|
|
188
|
+
status: SpanStatus;
|
|
189
|
+
events: SpanEvent[];
|
|
190
|
+
links: any[];
|
|
191
|
+
attributes: Record<string, string | number | boolean | null>;
|
|
192
|
+
startTime: number;
|
|
193
|
+
endTime: number;
|
|
194
|
+
duration: number;
|
|
195
|
+
other: SpanOther;
|
|
196
|
+
createdAt: string;
|
|
197
|
+
};
|
|
198
|
+
|
|
153
199
|
export interface GetTelemetryResponse {
|
|
154
|
-
traces:
|
|
200
|
+
traces: Span[];
|
|
155
201
|
}
|
|
156
202
|
|
|
157
203
|
export interface GetTelemetryParams {
|
|
@@ -161,3 +207,18 @@ export interface GetTelemetryParams {
|
|
|
161
207
|
perPage?: number;
|
|
162
208
|
attribute?: Record<string, string>;
|
|
163
209
|
}
|
|
210
|
+
|
|
211
|
+
export interface GetNetworkResponse {
|
|
212
|
+
name: string;
|
|
213
|
+
instructions: string;
|
|
214
|
+
agents: Array<{
|
|
215
|
+
name: string;
|
|
216
|
+
provider: string;
|
|
217
|
+
modelId: string;
|
|
218
|
+
}>;
|
|
219
|
+
routingModel: {
|
|
220
|
+
provider: string;
|
|
221
|
+
modelId: string;
|
|
222
|
+
};
|
|
223
|
+
state?: Record<string, any>;
|
|
224
|
+
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
> @mastra/client-js@0.1.4-alpha.0 build /Users/ward/projects/mastra/mastra/client-sdks/client-js
|
|
4
|
-
> tsup-node src/index.ts --format esm --dts --clean --treeshake
|
|
5
|
-
|
|
6
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
7
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
-
[34mCLI[39m tsup v8.3.6
|
|
9
|
-
[34mCLI[39m Target: es2022
|
|
10
|
-
[34mCLI[39m Cleaning output folder
|
|
11
|
-
[34mESM[39m Build start
|
|
12
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m13.56 KB[39m
|
|
13
|
-
[32mESM[39m ⚡️ Build success in 119ms
|
|
14
|
-
DTS Build start
|
|
15
|
-
DTS ⚡️ Build success in 2785ms
|
|
16
|
-
DTS dist/index.d.mts 13.64 KB
|