@mastra/client-js 0.11.3 → 0.12.0-alpha.0
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 +20 -0
- package/dist/client.d.ts +0 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +15 -225
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +15 -225
- package/dist/index.js.map +1 -1
- package/dist/resources/agent-builder.d.ts +15 -2
- package/dist/resources/agent-builder.d.ts.map +1 -1
- package/dist/types.d.ts +0 -21
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -7
- package/dist/adapters/agui.d.ts +0 -23
- package/dist/adapters/agui.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
1
|
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
4
2
|
import { v4 } from '@lukeed/uuid';
|
|
5
3
|
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
@@ -7,205 +5,7 @@ import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
|
7
5
|
import { z } from 'zod';
|
|
8
6
|
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
9
7
|
|
|
10
|
-
// src/
|
|
11
|
-
var AGUIAdapter = class extends AbstractAgent {
|
|
12
|
-
agent;
|
|
13
|
-
resourceId;
|
|
14
|
-
constructor({ agent, agentId, resourceId, ...rest }) {
|
|
15
|
-
super({
|
|
16
|
-
agentId,
|
|
17
|
-
...rest
|
|
18
|
-
});
|
|
19
|
-
this.agent = agent;
|
|
20
|
-
this.resourceId = resourceId;
|
|
21
|
-
}
|
|
22
|
-
run(input) {
|
|
23
|
-
return new Observable((subscriber) => {
|
|
24
|
-
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
25
|
-
subscriber.next({
|
|
26
|
-
type: EventType.RUN_STARTED,
|
|
27
|
-
threadId: input.threadId,
|
|
28
|
-
runId: input.runId
|
|
29
|
-
});
|
|
30
|
-
this.agent.stream({
|
|
31
|
-
threadId: input.threadId,
|
|
32
|
-
resourceId: this.resourceId ?? "",
|
|
33
|
-
runId: input.runId,
|
|
34
|
-
messages: convertedMessages,
|
|
35
|
-
clientTools: input.tools.reduce(
|
|
36
|
-
(acc, tool) => {
|
|
37
|
-
acc[tool.name] = {
|
|
38
|
-
id: tool.name,
|
|
39
|
-
description: tool.description,
|
|
40
|
-
inputSchema: tool.parameters
|
|
41
|
-
};
|
|
42
|
-
return acc;
|
|
43
|
-
},
|
|
44
|
-
{}
|
|
45
|
-
)
|
|
46
|
-
}).then((response) => {
|
|
47
|
-
let currentMessageId = void 0;
|
|
48
|
-
let isInTextMessage = false;
|
|
49
|
-
return response.processDataStream({
|
|
50
|
-
onTextPart: (text) => {
|
|
51
|
-
if (currentMessageId === void 0) {
|
|
52
|
-
currentMessageId = generateUUID();
|
|
53
|
-
const message2 = {
|
|
54
|
-
type: EventType.TEXT_MESSAGE_START,
|
|
55
|
-
messageId: currentMessageId,
|
|
56
|
-
role: "assistant"
|
|
57
|
-
};
|
|
58
|
-
subscriber.next(message2);
|
|
59
|
-
isInTextMessage = true;
|
|
60
|
-
}
|
|
61
|
-
const message = {
|
|
62
|
-
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
63
|
-
messageId: currentMessageId,
|
|
64
|
-
delta: text
|
|
65
|
-
};
|
|
66
|
-
subscriber.next(message);
|
|
67
|
-
},
|
|
68
|
-
onFinishMessagePart: () => {
|
|
69
|
-
if (currentMessageId !== void 0) {
|
|
70
|
-
const message = {
|
|
71
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
72
|
-
messageId: currentMessageId
|
|
73
|
-
};
|
|
74
|
-
subscriber.next(message);
|
|
75
|
-
isInTextMessage = false;
|
|
76
|
-
}
|
|
77
|
-
subscriber.next({
|
|
78
|
-
type: EventType.RUN_FINISHED,
|
|
79
|
-
threadId: input.threadId,
|
|
80
|
-
runId: input.runId
|
|
81
|
-
});
|
|
82
|
-
subscriber.complete();
|
|
83
|
-
},
|
|
84
|
-
onToolCallPart(streamPart) {
|
|
85
|
-
const parentMessageId = currentMessageId || generateUUID();
|
|
86
|
-
if (isInTextMessage) {
|
|
87
|
-
const message = {
|
|
88
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
89
|
-
messageId: parentMessageId
|
|
90
|
-
};
|
|
91
|
-
subscriber.next(message);
|
|
92
|
-
isInTextMessage = false;
|
|
93
|
-
}
|
|
94
|
-
subscriber.next({
|
|
95
|
-
type: EventType.TOOL_CALL_START,
|
|
96
|
-
toolCallId: streamPart.toolCallId,
|
|
97
|
-
toolCallName: streamPart.toolName,
|
|
98
|
-
parentMessageId
|
|
99
|
-
});
|
|
100
|
-
subscriber.next({
|
|
101
|
-
type: EventType.TOOL_CALL_ARGS,
|
|
102
|
-
toolCallId: streamPart.toolCallId,
|
|
103
|
-
delta: JSON.stringify(streamPart.args),
|
|
104
|
-
parentMessageId
|
|
105
|
-
});
|
|
106
|
-
subscriber.next({
|
|
107
|
-
type: EventType.TOOL_CALL_END,
|
|
108
|
-
toolCallId: streamPart.toolCallId,
|
|
109
|
-
parentMessageId
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
}).catch((error) => {
|
|
114
|
-
console.error("error", error);
|
|
115
|
-
subscriber.error(error);
|
|
116
|
-
});
|
|
117
|
-
return () => {
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
function generateUUID() {
|
|
123
|
-
if (typeof crypto !== "undefined") {
|
|
124
|
-
if (typeof crypto.randomUUID === "function") {
|
|
125
|
-
return crypto.randomUUID();
|
|
126
|
-
}
|
|
127
|
-
if (typeof crypto.getRandomValues === "function") {
|
|
128
|
-
const buffer = new Uint8Array(16);
|
|
129
|
-
crypto.getRandomValues(buffer);
|
|
130
|
-
buffer[6] = buffer[6] & 15 | 64;
|
|
131
|
-
buffer[8] = buffer[8] & 63 | 128;
|
|
132
|
-
let hex = "";
|
|
133
|
-
for (let i = 0; i < 16; i++) {
|
|
134
|
-
hex += buffer[i].toString(16).padStart(2, "0");
|
|
135
|
-
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
136
|
-
}
|
|
137
|
-
return hex;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
141
|
-
const r = Math.random() * 16 | 0;
|
|
142
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
143
|
-
return v.toString(16);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
function convertMessagesToMastraMessages(messages) {
|
|
147
|
-
const result = [];
|
|
148
|
-
const toolCallsWithResults = /* @__PURE__ */ new Set();
|
|
149
|
-
for (const message of messages) {
|
|
150
|
-
if (message.role === "tool" && message.toolCallId) {
|
|
151
|
-
toolCallsWithResults.add(message.toolCallId);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
for (const message of messages) {
|
|
155
|
-
if (message.role === "assistant") {
|
|
156
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
157
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
158
|
-
parts.push({
|
|
159
|
-
type: "tool-call",
|
|
160
|
-
toolCallId: toolCall.id,
|
|
161
|
-
toolName: toolCall.function.name,
|
|
162
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
result.push({
|
|
166
|
-
role: "assistant",
|
|
167
|
-
content: parts
|
|
168
|
-
});
|
|
169
|
-
if (message.toolCalls?.length) {
|
|
170
|
-
for (const toolCall of message.toolCalls) {
|
|
171
|
-
if (!toolCallsWithResults.has(toolCall.id)) {
|
|
172
|
-
result.push({
|
|
173
|
-
role: "tool",
|
|
174
|
-
content: [
|
|
175
|
-
{
|
|
176
|
-
type: "tool-result",
|
|
177
|
-
toolCallId: toolCall.id,
|
|
178
|
-
toolName: toolCall.function.name,
|
|
179
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
180
|
-
// This is still wrong but matches test expectations
|
|
181
|
-
}
|
|
182
|
-
]
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
} else if (message.role === "user") {
|
|
188
|
-
result.push({
|
|
189
|
-
role: "user",
|
|
190
|
-
content: message.content || ""
|
|
191
|
-
});
|
|
192
|
-
} else if (message.role === "tool") {
|
|
193
|
-
result.push({
|
|
194
|
-
role: "tool",
|
|
195
|
-
content: [
|
|
196
|
-
{
|
|
197
|
-
type: "tool-result",
|
|
198
|
-
toolCallId: message.toolCallId || "unknown",
|
|
199
|
-
toolName: "unknown",
|
|
200
|
-
// toolName is not available in tool messages from CopilotKit
|
|
201
|
-
result: message.content
|
|
202
|
-
}
|
|
203
|
-
]
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return result;
|
|
208
|
-
}
|
|
8
|
+
// src/resources/agent.ts
|
|
209
9
|
function parseClientRuntimeContext(runtimeContext) {
|
|
210
10
|
if (runtimeContext) {
|
|
211
11
|
if (runtimeContext instanceof RuntimeContext) {
|
|
@@ -2310,19 +2110,24 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2310
2110
|
* Creates a new agent builder action run and returns the runId.
|
|
2311
2111
|
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2312
2112
|
*/
|
|
2313
|
-
async createRun(params
|
|
2113
|
+
async createRun(params) {
|
|
2314
2114
|
const searchParams = new URLSearchParams();
|
|
2315
|
-
if (runId) {
|
|
2316
|
-
searchParams.set("runId", runId);
|
|
2115
|
+
if (!!params?.runId) {
|
|
2116
|
+
searchParams.set("runId", params.runId);
|
|
2317
2117
|
}
|
|
2318
|
-
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2319
|
-
const { runtimeContext: _, ...actionParams } = params;
|
|
2320
2118
|
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2321
2119
|
return this.request(url, {
|
|
2322
|
-
method: "POST"
|
|
2323
|
-
body: { ...actionParams, runtimeContext }
|
|
2120
|
+
method: "POST"
|
|
2324
2121
|
});
|
|
2325
2122
|
}
|
|
2123
|
+
/**
|
|
2124
|
+
* Creates a new workflow run (alias for createRun)
|
|
2125
|
+
* @param params - Optional object containing the optional runId
|
|
2126
|
+
* @returns Promise containing the runId of the created run
|
|
2127
|
+
*/
|
|
2128
|
+
createRunAsync(params) {
|
|
2129
|
+
return this.createRun(params);
|
|
2130
|
+
}
|
|
2326
2131
|
/**
|
|
2327
2132
|
* Starts agent builder action asynchronously and waits for completion.
|
|
2328
2133
|
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
@@ -2536,8 +2341,8 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2536
2341
|
* and streams any remaining progress.
|
|
2537
2342
|
* This calls `/api/agent-builder/:actionId/watch`.
|
|
2538
2343
|
*/
|
|
2539
|
-
async watch({ runId }, onRecord) {
|
|
2540
|
-
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}`;
|
|
2344
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2345
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2541
2346
|
const response = await this.request(url, {
|
|
2542
2347
|
method: "GET",
|
|
2543
2348
|
stream: true
|
|
@@ -2895,21 +2700,6 @@ var MastraClient = class extends BaseResource {
|
|
|
2895
2700
|
getAgents() {
|
|
2896
2701
|
return this.request("/api/agents");
|
|
2897
2702
|
}
|
|
2898
|
-
async getAGUI({ resourceId }) {
|
|
2899
|
-
const agents = await this.getAgents();
|
|
2900
|
-
return Object.entries(agents).reduce(
|
|
2901
|
-
(acc, [agentId]) => {
|
|
2902
|
-
const agent = this.getAgent(agentId);
|
|
2903
|
-
acc[agentId] = new AGUIAdapter({
|
|
2904
|
-
agentId,
|
|
2905
|
-
agent,
|
|
2906
|
-
resourceId
|
|
2907
|
-
});
|
|
2908
|
-
return acc;
|
|
2909
|
-
},
|
|
2910
|
-
{}
|
|
2911
|
-
);
|
|
2912
|
-
}
|
|
2913
2703
|
/**
|
|
2914
2704
|
* Gets an agent instance by ID
|
|
2915
2705
|
* @param agentId - ID of the agent to retrieve
|