@mastra/client-js 0.0.0-fix-message-embedding-20250506021742 → 0.0.0-inject-middleware-20250528213451
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 +289 -2
- package/dist/index.cjs +399 -101
- package/dist/index.d.cts +247 -77
- package/dist/index.d.ts +247 -77
- package/dist/index.js +395 -101
- package/package.json +7 -6
- package/src/adapters/agui.test.ts +19 -6
- package/src/adapters/agui.ts +31 -11
- package/src/client.ts +94 -19
- package/src/example.ts +29 -30
- package/src/index.test.ts +121 -1
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +27 -34
- package/src/resources/base.ts +1 -1
- package/src/resources/index.ts +4 -2
- package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -139
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +13 -3
- package/src/resources/network.ts +5 -11
- package/src/resources/tool.ts +9 -2
- package/src/resources/workflow.ts +202 -100
- package/src/types.ts +70 -18
- package/src/utils/index.ts +11 -0
- package/src/utils/zod-to-json-schema.ts +10 -0
package/dist/index.js
CHANGED
|
@@ -2,7 +2,8 @@ import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
4
4
|
import { ZodSchema } from 'zod';
|
|
5
|
-
import
|
|
5
|
+
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
|
+
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
6
7
|
|
|
7
8
|
// src/adapters/agui.ts
|
|
8
9
|
var AGUIAdapter = class extends AbstractAgent {
|
|
@@ -42,6 +43,7 @@ var AGUIAdapter = class extends AbstractAgent {
|
|
|
42
43
|
)
|
|
43
44
|
}).then((response) => {
|
|
44
45
|
let currentMessageId = void 0;
|
|
46
|
+
let isInTextMessage = false;
|
|
45
47
|
return response.processDataStream({
|
|
46
48
|
onTextPart: (text) => {
|
|
47
49
|
if (currentMessageId === void 0) {
|
|
@@ -52,6 +54,7 @@ var AGUIAdapter = class extends AbstractAgent {
|
|
|
52
54
|
role: "assistant"
|
|
53
55
|
};
|
|
54
56
|
subscriber.next(message2);
|
|
57
|
+
isInTextMessage = true;
|
|
55
58
|
}
|
|
56
59
|
const message = {
|
|
57
60
|
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
@@ -60,14 +63,14 @@ var AGUIAdapter = class extends AbstractAgent {
|
|
|
60
63
|
};
|
|
61
64
|
subscriber.next(message);
|
|
62
65
|
},
|
|
63
|
-
onFinishMessagePart: (
|
|
64
|
-
console.log("onFinishMessagePart", message);
|
|
66
|
+
onFinishMessagePart: () => {
|
|
65
67
|
if (currentMessageId !== void 0) {
|
|
66
|
-
const
|
|
68
|
+
const message = {
|
|
67
69
|
type: EventType.TEXT_MESSAGE_END,
|
|
68
70
|
messageId: currentMessageId
|
|
69
71
|
};
|
|
70
|
-
subscriber.next(
|
|
72
|
+
subscriber.next(message);
|
|
73
|
+
isInTextMessage = false;
|
|
71
74
|
}
|
|
72
75
|
subscriber.next({
|
|
73
76
|
type: EventType.RUN_FINISHED,
|
|
@@ -78,6 +81,14 @@ var AGUIAdapter = class extends AbstractAgent {
|
|
|
78
81
|
},
|
|
79
82
|
onToolCallPart(streamPart) {
|
|
80
83
|
const parentMessageId = currentMessageId || generateUUID();
|
|
84
|
+
if (isInTextMessage) {
|
|
85
|
+
const message = {
|
|
86
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
87
|
+
messageId: parentMessageId
|
|
88
|
+
};
|
|
89
|
+
subscriber.next(message);
|
|
90
|
+
isInTextMessage = false;
|
|
91
|
+
}
|
|
81
92
|
subscriber.next({
|
|
82
93
|
type: EventType.TOOL_CALL_START,
|
|
83
94
|
toolCallId: streamPart.toolCallId,
|
|
@@ -98,7 +109,7 @@ var AGUIAdapter = class extends AbstractAgent {
|
|
|
98
109
|
}
|
|
99
110
|
});
|
|
100
111
|
}).catch((error) => {
|
|
101
|
-
console.
|
|
112
|
+
console.error("error", error);
|
|
102
113
|
subscriber.error(error);
|
|
103
114
|
});
|
|
104
115
|
return () => {
|
|
@@ -147,6 +158,17 @@ function convertMessagesToMastraMessages(messages) {
|
|
|
147
158
|
role: "assistant",
|
|
148
159
|
content: parts
|
|
149
160
|
});
|
|
161
|
+
if (message.toolCalls?.length) {
|
|
162
|
+
result.push({
|
|
163
|
+
role: "tool",
|
|
164
|
+
content: message.toolCalls.map((toolCall) => ({
|
|
165
|
+
type: "tool-result",
|
|
166
|
+
toolCallId: toolCall.id,
|
|
167
|
+
toolName: toolCall.function.name,
|
|
168
|
+
result: JSON.parse(toolCall.function.arguments)
|
|
169
|
+
}))
|
|
170
|
+
});
|
|
171
|
+
}
|
|
150
172
|
} else if (message.role === "user") {
|
|
151
173
|
result.push({
|
|
152
174
|
role: "user",
|
|
@@ -168,6 +190,12 @@ function convertMessagesToMastraMessages(messages) {
|
|
|
168
190
|
}
|
|
169
191
|
return result;
|
|
170
192
|
}
|
|
193
|
+
function zodToJsonSchema(zodSchema) {
|
|
194
|
+
if (!(zodSchema instanceof ZodSchema)) {
|
|
195
|
+
return zodSchema;
|
|
196
|
+
}
|
|
197
|
+
return originalZodToJsonSchema(zodSchema, { $refStrategy: "none" });
|
|
198
|
+
}
|
|
171
199
|
|
|
172
200
|
// src/resources/base.ts
|
|
173
201
|
var BaseResource = class {
|
|
@@ -187,7 +215,7 @@ var BaseResource = class {
|
|
|
187
215
|
let delay = backoffMs;
|
|
188
216
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
189
217
|
try {
|
|
190
|
-
const response = await fetch(`${baseUrl}${path}`, {
|
|
218
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
191
219
|
...options,
|
|
192
220
|
headers: {
|
|
193
221
|
...headers,
|
|
@@ -227,6 +255,15 @@ var BaseResource = class {
|
|
|
227
255
|
throw lastError || new Error("Request failed");
|
|
228
256
|
}
|
|
229
257
|
};
|
|
258
|
+
function parseClientRuntimeContext(runtimeContext) {
|
|
259
|
+
if (runtimeContext) {
|
|
260
|
+
if (runtimeContext instanceof RuntimeContext) {
|
|
261
|
+
return Object.fromEntries(runtimeContext.entries());
|
|
262
|
+
}
|
|
263
|
+
return runtimeContext;
|
|
264
|
+
}
|
|
265
|
+
return void 0;
|
|
266
|
+
}
|
|
230
267
|
|
|
231
268
|
// src/resources/agent.ts
|
|
232
269
|
var AgentVoice = class extends BaseResource {
|
|
@@ -298,8 +335,9 @@ var Agent = class extends BaseResource {
|
|
|
298
335
|
generate(params) {
|
|
299
336
|
const processedParams = {
|
|
300
337
|
...params,
|
|
301
|
-
output: params.output
|
|
302
|
-
experimental_output: params.experimental_output
|
|
338
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
339
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
340
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
303
341
|
};
|
|
304
342
|
return this.request(`/api/agents/${this.agentId}/generate`, {
|
|
305
343
|
method: "POST",
|
|
@@ -314,8 +352,9 @@ var Agent = class extends BaseResource {
|
|
|
314
352
|
async stream(params) {
|
|
315
353
|
const processedParams = {
|
|
316
354
|
...params,
|
|
317
|
-
output: params.output
|
|
318
|
-
experimental_output: params.experimental_output
|
|
355
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
356
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
357
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
319
358
|
};
|
|
320
359
|
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
321
360
|
method: "POST",
|
|
@@ -341,6 +380,22 @@ var Agent = class extends BaseResource {
|
|
|
341
380
|
getTool(toolId) {
|
|
342
381
|
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
343
382
|
}
|
|
383
|
+
/**
|
|
384
|
+
* Executes a tool for the agent
|
|
385
|
+
* @param toolId - ID of the tool to execute
|
|
386
|
+
* @param params - Parameters required for tool execution
|
|
387
|
+
* @returns Promise containing the tool execution results
|
|
388
|
+
*/
|
|
389
|
+
executeTool(toolId, params) {
|
|
390
|
+
const body = {
|
|
391
|
+
data: params.data,
|
|
392
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
393
|
+
};
|
|
394
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
395
|
+
method: "POST",
|
|
396
|
+
body
|
|
397
|
+
});
|
|
398
|
+
}
|
|
344
399
|
/**
|
|
345
400
|
* Retrieves evaluation results for the agent
|
|
346
401
|
* @returns Promise containing agent evaluations
|
|
@@ -376,8 +431,8 @@ var Network = class extends BaseResource {
|
|
|
376
431
|
generate(params) {
|
|
377
432
|
const processedParams = {
|
|
378
433
|
...params,
|
|
379
|
-
output:
|
|
380
|
-
experimental_output:
|
|
434
|
+
output: zodToJsonSchema(params.output),
|
|
435
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
381
436
|
};
|
|
382
437
|
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
383
438
|
method: "POST",
|
|
@@ -392,8 +447,8 @@ var Network = class extends BaseResource {
|
|
|
392
447
|
async stream(params) {
|
|
393
448
|
const processedParams = {
|
|
394
449
|
...params,
|
|
395
|
-
output:
|
|
396
|
-
experimental_output:
|
|
450
|
+
output: zodToJsonSchema(params.output),
|
|
451
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
397
452
|
};
|
|
398
453
|
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
399
454
|
method: "POST",
|
|
@@ -449,10 +504,15 @@ var MemoryThread = class extends BaseResource {
|
|
|
449
504
|
}
|
|
450
505
|
/**
|
|
451
506
|
* Retrieves messages associated with the thread
|
|
507
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
452
508
|
* @returns Promise containing thread messages and UI messages
|
|
453
509
|
*/
|
|
454
|
-
getMessages() {
|
|
455
|
-
|
|
510
|
+
getMessages(params) {
|
|
511
|
+
const query = new URLSearchParams({
|
|
512
|
+
agentId: this.agentId,
|
|
513
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
514
|
+
});
|
|
515
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
456
516
|
}
|
|
457
517
|
};
|
|
458
518
|
|
|
@@ -522,24 +582,24 @@ var Vector = class extends BaseResource {
|
|
|
522
582
|
}
|
|
523
583
|
};
|
|
524
584
|
|
|
525
|
-
// src/resources/workflow.ts
|
|
585
|
+
// src/resources/legacy-workflow.ts
|
|
526
586
|
var RECORD_SEPARATOR = "";
|
|
527
|
-
var
|
|
587
|
+
var LegacyWorkflow = class extends BaseResource {
|
|
528
588
|
constructor(options, workflowId) {
|
|
529
589
|
super(options);
|
|
530
590
|
this.workflowId = workflowId;
|
|
531
591
|
}
|
|
532
592
|
/**
|
|
533
|
-
* Retrieves details about the workflow
|
|
534
|
-
* @returns Promise containing workflow details including steps and graphs
|
|
593
|
+
* Retrieves details about the legacy workflow
|
|
594
|
+
* @returns Promise containing legacy workflow details including steps and graphs
|
|
535
595
|
*/
|
|
536
596
|
details() {
|
|
537
|
-
return this.request(`/api/workflows/${this.workflowId}`);
|
|
597
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}`);
|
|
538
598
|
}
|
|
539
599
|
/**
|
|
540
|
-
* Retrieves all runs for a workflow
|
|
600
|
+
* Retrieves all runs for a legacy workflow
|
|
541
601
|
* @param params - Parameters for filtering runs
|
|
542
|
-
* @returns Promise containing workflow runs array
|
|
602
|
+
* @returns Promise containing legacy workflow runs array
|
|
543
603
|
*/
|
|
544
604
|
runs(params) {
|
|
545
605
|
const searchParams = new URLSearchParams();
|
|
@@ -559,25 +619,13 @@ var Workflow = class extends BaseResource {
|
|
|
559
619
|
searchParams.set("resourceId", params.resourceId);
|
|
560
620
|
}
|
|
561
621
|
if (searchParams.size) {
|
|
562
|
-
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
622
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs?${searchParams}`);
|
|
563
623
|
} else {
|
|
564
|
-
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
624
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs`);
|
|
565
625
|
}
|
|
566
626
|
}
|
|
567
627
|
/**
|
|
568
|
-
*
|
|
569
|
-
* Executes the workflow with the provided parameters
|
|
570
|
-
* @param params - Parameters required for workflow execution
|
|
571
|
-
* @returns Promise containing the workflow execution results
|
|
572
|
-
*/
|
|
573
|
-
execute(params) {
|
|
574
|
-
return this.request(`/api/workflows/${this.workflowId}/execute`, {
|
|
575
|
-
method: "POST",
|
|
576
|
-
body: params
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Creates a new workflow run
|
|
628
|
+
* Creates a new legacy workflow run
|
|
581
629
|
* @returns Promise containing the generated run ID
|
|
582
630
|
*/
|
|
583
631
|
createRun(params) {
|
|
@@ -585,34 +633,34 @@ var Workflow = class extends BaseResource {
|
|
|
585
633
|
if (!!params?.runId) {
|
|
586
634
|
searchParams.set("runId", params.runId);
|
|
587
635
|
}
|
|
588
|
-
return this.request(`/api/workflows/${this.workflowId}/
|
|
636
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
589
637
|
method: "POST"
|
|
590
638
|
});
|
|
591
639
|
}
|
|
592
640
|
/**
|
|
593
|
-
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
641
|
+
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
594
642
|
* @param params - Object containing the runId and triggerData
|
|
595
643
|
* @returns Promise containing success message
|
|
596
644
|
*/
|
|
597
645
|
start(params) {
|
|
598
|
-
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
646
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
599
647
|
method: "POST",
|
|
600
648
|
body: params?.triggerData
|
|
601
649
|
});
|
|
602
650
|
}
|
|
603
651
|
/**
|
|
604
|
-
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
652
|
+
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
605
653
|
* @param stepId - ID of the step to resume
|
|
606
|
-
* @param runId - ID of the workflow run
|
|
607
|
-
* @param context - Context to resume the workflow with
|
|
608
|
-
* @returns Promise containing the workflow resume results
|
|
654
|
+
* @param runId - ID of the legacy workflow run
|
|
655
|
+
* @param context - Context to resume the legacy workflow with
|
|
656
|
+
* @returns Promise containing the legacy workflow resume results
|
|
609
657
|
*/
|
|
610
658
|
resume({
|
|
611
659
|
stepId,
|
|
612
660
|
runId,
|
|
613
661
|
context
|
|
614
662
|
}) {
|
|
615
|
-
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
663
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
616
664
|
method: "POST",
|
|
617
665
|
body: {
|
|
618
666
|
stepId,
|
|
@@ -630,18 +678,18 @@ var Workflow = class extends BaseResource {
|
|
|
630
678
|
if (!!params?.runId) {
|
|
631
679
|
searchParams.set("runId", params.runId);
|
|
632
680
|
}
|
|
633
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
681
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
634
682
|
method: "POST",
|
|
635
683
|
body: params?.triggerData
|
|
636
684
|
});
|
|
637
685
|
}
|
|
638
686
|
/**
|
|
639
|
-
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
687
|
+
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
640
688
|
* @param params - Object containing the runId, stepId, and context
|
|
641
689
|
* @returns Promise containing the workflow resume results
|
|
642
690
|
*/
|
|
643
691
|
resumeAsync(params) {
|
|
644
|
-
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
692
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
645
693
|
method: "POST",
|
|
646
694
|
body: {
|
|
647
695
|
stepId: params.stepId,
|
|
@@ -695,16 +743,16 @@ var Workflow = class extends BaseResource {
|
|
|
695
743
|
}
|
|
696
744
|
}
|
|
697
745
|
/**
|
|
698
|
-
* Watches workflow transitions in real-time
|
|
746
|
+
* Watches legacy workflow transitions in real-time
|
|
699
747
|
* @param runId - Optional run ID to filter the watch stream
|
|
700
|
-
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
748
|
+
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
701
749
|
*/
|
|
702
750
|
async watch({ runId }, onRecord) {
|
|
703
|
-
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
751
|
+
const response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
704
752
|
stream: true
|
|
705
753
|
});
|
|
706
754
|
if (!response.ok) {
|
|
707
|
-
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
755
|
+
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
708
756
|
}
|
|
709
757
|
if (!response.body) {
|
|
710
758
|
throw new Error("Response body is null");
|
|
@@ -738,22 +786,26 @@ var Tool = class extends BaseResource {
|
|
|
738
786
|
if (params.runId) {
|
|
739
787
|
url.set("runId", params.runId);
|
|
740
788
|
}
|
|
789
|
+
const body = {
|
|
790
|
+
data: params.data,
|
|
791
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
792
|
+
};
|
|
741
793
|
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
742
794
|
method: "POST",
|
|
743
|
-
body
|
|
795
|
+
body
|
|
744
796
|
});
|
|
745
797
|
}
|
|
746
798
|
};
|
|
747
799
|
|
|
748
|
-
// src/resources/
|
|
800
|
+
// src/resources/workflow.ts
|
|
749
801
|
var RECORD_SEPARATOR2 = "";
|
|
750
|
-
var
|
|
802
|
+
var Workflow = class extends BaseResource {
|
|
751
803
|
constructor(options, workflowId) {
|
|
752
804
|
super(options);
|
|
753
805
|
this.workflowId = workflowId;
|
|
754
806
|
}
|
|
755
807
|
/**
|
|
756
|
-
* Creates an async generator that processes a readable stream and yields
|
|
808
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
757
809
|
* separated by the Record Separator character (\x1E)
|
|
758
810
|
*
|
|
759
811
|
* @param stream - The readable stream to process
|
|
@@ -798,16 +850,16 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
798
850
|
}
|
|
799
851
|
}
|
|
800
852
|
/**
|
|
801
|
-
* Retrieves details about the
|
|
802
|
-
* @returns Promise containing
|
|
853
|
+
* Retrieves details about the workflow
|
|
854
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
803
855
|
*/
|
|
804
856
|
details() {
|
|
805
|
-
return this.request(`/api/workflows
|
|
857
|
+
return this.request(`/api/workflows/${this.workflowId}`);
|
|
806
858
|
}
|
|
807
859
|
/**
|
|
808
|
-
* Retrieves all runs for a
|
|
860
|
+
* Retrieves all runs for a workflow
|
|
809
861
|
* @param params - Parameters for filtering runs
|
|
810
|
-
* @returns Promise containing
|
|
862
|
+
* @returns Promise containing workflow runs array
|
|
811
863
|
*/
|
|
812
864
|
runs(params) {
|
|
813
865
|
const searchParams = new URLSearchParams();
|
|
@@ -827,13 +879,13 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
827
879
|
searchParams.set("resourceId", params.resourceId);
|
|
828
880
|
}
|
|
829
881
|
if (searchParams.size) {
|
|
830
|
-
return this.request(`/api/workflows
|
|
882
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
831
883
|
} else {
|
|
832
|
-
return this.request(`/api/workflows
|
|
884
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
833
885
|
}
|
|
834
886
|
}
|
|
835
887
|
/**
|
|
836
|
-
* Creates a new
|
|
888
|
+
* Creates a new workflow run
|
|
837
889
|
* @param params - Optional object containing the optional runId
|
|
838
890
|
* @returns Promise containing the runId of the created run
|
|
839
891
|
*/
|
|
@@ -842,23 +894,24 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
842
894
|
if (!!params?.runId) {
|
|
843
895
|
searchParams.set("runId", params.runId);
|
|
844
896
|
}
|
|
845
|
-
return this.request(`/api/workflows
|
|
897
|
+
return this.request(`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
846
898
|
method: "POST"
|
|
847
899
|
});
|
|
848
900
|
}
|
|
849
901
|
/**
|
|
850
|
-
* Starts a
|
|
902
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
851
903
|
* @param params - Object containing the runId, inputData and runtimeContext
|
|
852
904
|
* @returns Promise containing success message
|
|
853
905
|
*/
|
|
854
906
|
start(params) {
|
|
855
|
-
|
|
907
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
908
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
856
909
|
method: "POST",
|
|
857
|
-
body: { inputData: params?.inputData, runtimeContext
|
|
910
|
+
body: { inputData: params?.inputData, runtimeContext }
|
|
858
911
|
});
|
|
859
912
|
}
|
|
860
913
|
/**
|
|
861
|
-
* Resumes a suspended
|
|
914
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
862
915
|
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
863
916
|
* @returns Promise containing success message
|
|
864
917
|
*/
|
|
@@ -866,9 +919,10 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
866
919
|
step,
|
|
867
920
|
runId,
|
|
868
921
|
resumeData,
|
|
869
|
-
|
|
922
|
+
...rest
|
|
870
923
|
}) {
|
|
871
|
-
|
|
924
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
925
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
872
926
|
method: "POST",
|
|
873
927
|
stream: true,
|
|
874
928
|
body: {
|
|
@@ -879,53 +933,237 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
879
933
|
});
|
|
880
934
|
}
|
|
881
935
|
/**
|
|
882
|
-
* Starts a
|
|
936
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
883
937
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
884
|
-
* @returns Promise containing the
|
|
938
|
+
* @returns Promise containing the workflow execution results
|
|
885
939
|
*/
|
|
886
940
|
startAsync(params) {
|
|
887
941
|
const searchParams = new URLSearchParams();
|
|
888
942
|
if (!!params?.runId) {
|
|
889
943
|
searchParams.set("runId", params.runId);
|
|
890
944
|
}
|
|
891
|
-
|
|
945
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
946
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
892
947
|
method: "POST",
|
|
893
|
-
body: { inputData: params.inputData, runtimeContext
|
|
948
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
894
949
|
});
|
|
895
950
|
}
|
|
896
951
|
/**
|
|
897
|
-
*
|
|
952
|
+
* Starts a vNext workflow run and returns a stream
|
|
953
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
954
|
+
* @returns Promise containing the vNext workflow execution results
|
|
955
|
+
*/
|
|
956
|
+
async stream(params) {
|
|
957
|
+
const searchParams = new URLSearchParams();
|
|
958
|
+
if (!!params?.runId) {
|
|
959
|
+
searchParams.set("runId", params.runId);
|
|
960
|
+
}
|
|
961
|
+
const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0;
|
|
962
|
+
const response = await this.request(
|
|
963
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
964
|
+
{
|
|
965
|
+
method: "POST",
|
|
966
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
967
|
+
stream: true
|
|
968
|
+
}
|
|
969
|
+
);
|
|
970
|
+
if (!response.ok) {
|
|
971
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
972
|
+
}
|
|
973
|
+
if (!response.body) {
|
|
974
|
+
throw new Error("Response body is null");
|
|
975
|
+
}
|
|
976
|
+
const transformStream = new TransformStream({
|
|
977
|
+
start() {
|
|
978
|
+
},
|
|
979
|
+
async transform(chunk, controller) {
|
|
980
|
+
try {
|
|
981
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
982
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
983
|
+
for (const chunk2 of chunks) {
|
|
984
|
+
if (chunk2) {
|
|
985
|
+
try {
|
|
986
|
+
const parsedChunk = JSON.parse(chunk2);
|
|
987
|
+
controller.enqueue(parsedChunk);
|
|
988
|
+
} catch {
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
} catch {
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
return response.body.pipeThrough(transformStream);
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
898
1000
|
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
899
|
-
* @returns Promise containing the
|
|
1001
|
+
* @returns Promise containing the workflow resume results
|
|
900
1002
|
*/
|
|
901
1003
|
resumeAsync(params) {
|
|
902
|
-
|
|
1004
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1005
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
903
1006
|
method: "POST",
|
|
904
1007
|
body: {
|
|
905
1008
|
step: params.step,
|
|
906
1009
|
resumeData: params.resumeData,
|
|
907
|
-
runtimeContext
|
|
1010
|
+
runtimeContext
|
|
908
1011
|
}
|
|
909
1012
|
});
|
|
910
1013
|
}
|
|
911
1014
|
/**
|
|
912
|
-
* Watches
|
|
1015
|
+
* Watches workflow transitions in real-time
|
|
913
1016
|
* @param runId - Optional run ID to filter the watch stream
|
|
914
|
-
* @returns AsyncGenerator that yields parsed records from the
|
|
1017
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
915
1018
|
*/
|
|
916
1019
|
async watch({ runId }, onRecord) {
|
|
917
|
-
const response = await this.request(`/api/workflows
|
|
1020
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
918
1021
|
stream: true
|
|
919
1022
|
});
|
|
920
1023
|
if (!response.ok) {
|
|
921
|
-
throw new Error(`Failed to watch
|
|
1024
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
922
1025
|
}
|
|
923
1026
|
if (!response.body) {
|
|
924
1027
|
throw new Error("Response body is null");
|
|
925
1028
|
}
|
|
926
1029
|
for await (const record of this.streamProcessor(response.body)) {
|
|
927
|
-
|
|
1030
|
+
if (typeof record === "string") {
|
|
1031
|
+
onRecord(JSON.parse(record));
|
|
1032
|
+
} else {
|
|
1033
|
+
onRecord(record);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
1039
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
1040
|
+
*
|
|
1041
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
1042
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
1043
|
+
*/
|
|
1044
|
+
static createRecordStream(records) {
|
|
1045
|
+
const encoder = new TextEncoder();
|
|
1046
|
+
return new ReadableStream({
|
|
1047
|
+
async start(controller) {
|
|
1048
|
+
try {
|
|
1049
|
+
for await (const record of records) {
|
|
1050
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1051
|
+
controller.enqueue(encoder.encode(json));
|
|
1052
|
+
}
|
|
1053
|
+
controller.close();
|
|
1054
|
+
} catch (err) {
|
|
1055
|
+
controller.error(err);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
// src/resources/a2a.ts
|
|
1063
|
+
var A2A = class extends BaseResource {
|
|
1064
|
+
constructor(options, agentId) {
|
|
1065
|
+
super(options);
|
|
1066
|
+
this.agentId = agentId;
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Get the agent card with metadata about the agent
|
|
1070
|
+
* @returns Promise containing the agent card information
|
|
1071
|
+
*/
|
|
1072
|
+
async getCard() {
|
|
1073
|
+
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Send a message to the agent and get a response
|
|
1077
|
+
* @param params - Parameters for the task
|
|
1078
|
+
* @returns Promise containing the task response
|
|
1079
|
+
*/
|
|
1080
|
+
async sendMessage(params) {
|
|
1081
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1082
|
+
method: "POST",
|
|
1083
|
+
body: {
|
|
1084
|
+
method: "tasks/send",
|
|
1085
|
+
params
|
|
1086
|
+
}
|
|
1087
|
+
});
|
|
1088
|
+
return { task: response.result };
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Get the status and result of a task
|
|
1092
|
+
* @param params - Parameters for querying the task
|
|
1093
|
+
* @returns Promise containing the task response
|
|
1094
|
+
*/
|
|
1095
|
+
async getTask(params) {
|
|
1096
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1097
|
+
method: "POST",
|
|
1098
|
+
body: {
|
|
1099
|
+
method: "tasks/get",
|
|
1100
|
+
params
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
return response.result;
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Cancel a running task
|
|
1107
|
+
* @param params - Parameters identifying the task to cancel
|
|
1108
|
+
* @returns Promise containing the task response
|
|
1109
|
+
*/
|
|
1110
|
+
async cancelTask(params) {
|
|
1111
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1112
|
+
method: "POST",
|
|
1113
|
+
body: {
|
|
1114
|
+
method: "tasks/cancel",
|
|
1115
|
+
params
|
|
1116
|
+
}
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Send a message and subscribe to streaming updates (not fully implemented)
|
|
1121
|
+
* @param params - Parameters for the task
|
|
1122
|
+
* @returns Promise containing the task response
|
|
1123
|
+
*/
|
|
1124
|
+
async sendAndSubscribe(params) {
|
|
1125
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1126
|
+
method: "POST",
|
|
1127
|
+
body: {
|
|
1128
|
+
method: "tasks/sendSubscribe",
|
|
1129
|
+
params
|
|
1130
|
+
},
|
|
1131
|
+
stream: true
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
|
|
1136
|
+
// src/resources/mcp-tool.ts
|
|
1137
|
+
var MCPTool = class extends BaseResource {
|
|
1138
|
+
serverId;
|
|
1139
|
+
toolId;
|
|
1140
|
+
constructor(options, serverId, toolId) {
|
|
1141
|
+
super(options);
|
|
1142
|
+
this.serverId = serverId;
|
|
1143
|
+
this.toolId = toolId;
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
1147
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
1148
|
+
*/
|
|
1149
|
+
details() {
|
|
1150
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Executes this specific tool on the MCP server.
|
|
1154
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
1155
|
+
* @returns Promise containing the result of the tool execution.
|
|
1156
|
+
*/
|
|
1157
|
+
execute(params) {
|
|
1158
|
+
const body = {};
|
|
1159
|
+
if (params.data !== void 0) body.data = params.data;
|
|
1160
|
+
if (params.runtimeContext !== void 0) {
|
|
1161
|
+
body.runtimeContext = params.runtimeContext;
|
|
928
1162
|
}
|
|
1163
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
1164
|
+
method: "POST",
|
|
1165
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
1166
|
+
});
|
|
929
1167
|
}
|
|
930
1168
|
};
|
|
931
1169
|
|
|
@@ -1021,6 +1259,21 @@ var MastraClient = class extends BaseResource {
|
|
|
1021
1259
|
getTool(toolId) {
|
|
1022
1260
|
return new Tool(this.options, toolId);
|
|
1023
1261
|
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Retrieves all available legacy workflows
|
|
1264
|
+
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
1265
|
+
*/
|
|
1266
|
+
getLegacyWorkflows() {
|
|
1267
|
+
return this.request("/api/workflows/legacy");
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Gets a legacy workflow instance by ID
|
|
1271
|
+
* @param workflowId - ID of the legacy workflow to retrieve
|
|
1272
|
+
* @returns Legacy Workflow instance
|
|
1273
|
+
*/
|
|
1274
|
+
getLegacyWorkflow(workflowId) {
|
|
1275
|
+
return new LegacyWorkflow(this.options, workflowId);
|
|
1276
|
+
}
|
|
1024
1277
|
/**
|
|
1025
1278
|
* Retrieves all available workflows
|
|
1026
1279
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
@@ -1036,21 +1289,6 @@ var MastraClient = class extends BaseResource {
|
|
|
1036
1289
|
getWorkflow(workflowId) {
|
|
1037
1290
|
return new Workflow(this.options, workflowId);
|
|
1038
1291
|
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Retrieves all available vNext workflows
|
|
1041
|
-
* @returns Promise containing map of vNext workflow IDs to vNext workflow details
|
|
1042
|
-
*/
|
|
1043
|
-
getVNextWorkflows() {
|
|
1044
|
-
return this.request("/api/workflows/v-next");
|
|
1045
|
-
}
|
|
1046
|
-
/**
|
|
1047
|
-
* Gets a vNext workflow instance by ID
|
|
1048
|
-
* @param workflowId - ID of the vNext workflow to retrieve
|
|
1049
|
-
* @returns vNext Workflow instance
|
|
1050
|
-
*/
|
|
1051
|
-
getVNextWorkflow(workflowId) {
|
|
1052
|
-
return new VNextWorkflow(this.options, workflowId);
|
|
1053
|
-
}
|
|
1054
1292
|
/**
|
|
1055
1293
|
* Gets a vector instance by name
|
|
1056
1294
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -1139,6 +1377,62 @@ var MastraClient = class extends BaseResource {
|
|
|
1139
1377
|
getNetwork(networkId) {
|
|
1140
1378
|
return new Network(this.options, networkId);
|
|
1141
1379
|
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Retrieves a list of available MCP servers.
|
|
1382
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
1383
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
1384
|
+
*/
|
|
1385
|
+
getMcpServers(params) {
|
|
1386
|
+
const searchParams = new URLSearchParams();
|
|
1387
|
+
if (params?.limit !== void 0) {
|
|
1388
|
+
searchParams.set("limit", String(params.limit));
|
|
1389
|
+
}
|
|
1390
|
+
if (params?.offset !== void 0) {
|
|
1391
|
+
searchParams.set("offset", String(params.offset));
|
|
1392
|
+
}
|
|
1393
|
+
const queryString = searchParams.toString();
|
|
1394
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ""}`);
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Retrieves detailed information for a specific MCP server.
|
|
1398
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
1399
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
1400
|
+
* @returns Promise containing the detailed MCP server information.
|
|
1401
|
+
*/
|
|
1402
|
+
getMcpServerDetails(serverId, params) {
|
|
1403
|
+
const searchParams = new URLSearchParams();
|
|
1404
|
+
if (params?.version) {
|
|
1405
|
+
searchParams.set("version", params.version);
|
|
1406
|
+
}
|
|
1407
|
+
const queryString = searchParams.toString();
|
|
1408
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ""}`);
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1411
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
1412
|
+
* @param serverId - The ID of the MCP server.
|
|
1413
|
+
* @returns Promise containing the list of tools.
|
|
1414
|
+
*/
|
|
1415
|
+
getMcpServerTools(serverId) {
|
|
1416
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
1420
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
1421
|
+
* @param serverId - The ID of the MCP server.
|
|
1422
|
+
* @param toolId - The ID of the tool.
|
|
1423
|
+
* @returns MCPTool instance.
|
|
1424
|
+
*/
|
|
1425
|
+
getMcpServerTool(serverId, toolId) {
|
|
1426
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
1430
|
+
* @param agentId - ID of the agent to interact with
|
|
1431
|
+
* @returns A2A client instance
|
|
1432
|
+
*/
|
|
1433
|
+
getA2A(agentId) {
|
|
1434
|
+
return new A2A(this.options, agentId);
|
|
1435
|
+
}
|
|
1142
1436
|
};
|
|
1143
1437
|
|
|
1144
1438
|
export { MastraClient };
|