@mastra/client-js 0.0.0-message-list-update-20250715150321 → 0.0.0-message-file-url-handling-fix-20250904234524
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 +795 -3
- package/README.md +6 -3
- package/dist/client.d.ts +280 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +1627 -701
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -1194
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1627 -701
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent-builder.d.ts +161 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +155 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +13 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/legacy-workflow.d.ts +87 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +27 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/network.d.ts +30 -0
- package/dist/resources/network.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +19 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +23 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +42 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +48 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +154 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/types.d.ts +461 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +7 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/package.json +26 -14
- package/.turbo/turbo-build.log +0 -19
- package/dist/index.d.cts +0 -1194
- package/eslint.config.js +0 -6
- package/src/adapters/agui.test.ts +0 -180
- package/src/adapters/agui.ts +0 -239
- package/src/client.ts +0 -526
- package/src/example.ts +0 -95
- package/src/index.test.ts +0 -830
- package/src/index.ts +0 -2
- package/src/resources/a2a.ts +0 -88
- package/src/resources/agent.ts +0 -800
- package/src/resources/base.ts +0 -72
- package/src/resources/index.ts +0 -10
- package/src/resources/legacy-workflow.ts +0 -242
- package/src/resources/mcp-tool.ts +0 -48
- package/src/resources/memory-thread.ts +0 -63
- package/src/resources/network-memory-thread.ts +0 -63
- package/src/resources/network.ts +0 -85
- package/src/resources/tool.ts +0 -45
- package/src/resources/vNextNetwork.ts +0 -194
- package/src/resources/vector.ts +0 -83
- package/src/resources/workflow.ts +0 -396
- package/src/types.ts +0 -428
- package/src/utils/index.ts +0 -11
- package/src/utils/process-client-tools.ts +0 -32
- package/src/utils/zod-to-json-schema.ts +0 -10
- package/tsconfig.json +0 -5
- package/vitest.config.js +0 -8
package/dist/index.js
CHANGED
|
@@ -1,203 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
|
-
import { processTextStream, processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
4
|
-
import { ZodSchema } from 'zod';
|
|
5
|
-
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
|
-
import { isVercelTool } from '@mastra/core/tools';
|
|
1
|
+
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
7
2
|
import { v4 } from '@lukeed/uuid';
|
|
8
3
|
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
4
|
+
import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
9
7
|
|
|
10
|
-
// src/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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;
|
|
8
|
+
// src/resources/agent.ts
|
|
9
|
+
function parseClientRuntimeContext(runtimeContext) {
|
|
10
|
+
if (runtimeContext) {
|
|
11
|
+
if (runtimeContext instanceof RuntimeContext) {
|
|
12
|
+
return Object.fromEntries(runtimeContext.entries());
|
|
138
13
|
}
|
|
14
|
+
return runtimeContext;
|
|
139
15
|
}
|
|
140
|
-
return
|
|
141
|
-
const r = Math.random() * 16 | 0;
|
|
142
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
143
|
-
return v.toString(16);
|
|
144
|
-
});
|
|
16
|
+
return void 0;
|
|
145
17
|
}
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
for (const message of messages) {
|
|
149
|
-
if (message.role === "assistant") {
|
|
150
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
151
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
152
|
-
parts.push({
|
|
153
|
-
type: "tool-call",
|
|
154
|
-
toolCallId: toolCall.id,
|
|
155
|
-
toolName: toolCall.function.name,
|
|
156
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
result.push({
|
|
160
|
-
role: "assistant",
|
|
161
|
-
content: parts
|
|
162
|
-
});
|
|
163
|
-
if (message.toolCalls?.length) {
|
|
164
|
-
result.push({
|
|
165
|
-
role: "tool",
|
|
166
|
-
content: message.toolCalls.map((toolCall) => ({
|
|
167
|
-
type: "tool-result",
|
|
168
|
-
toolCallId: toolCall.id,
|
|
169
|
-
toolName: toolCall.function.name,
|
|
170
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
171
|
-
}))
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
} else if (message.role === "user") {
|
|
175
|
-
result.push({
|
|
176
|
-
role: "user",
|
|
177
|
-
content: message.content || ""
|
|
178
|
-
});
|
|
179
|
-
} else if (message.role === "tool") {
|
|
180
|
-
result.push({
|
|
181
|
-
role: "tool",
|
|
182
|
-
content: [
|
|
183
|
-
{
|
|
184
|
-
type: "tool-result",
|
|
185
|
-
toolCallId: message.toolCallId,
|
|
186
|
-
toolName: "unknown",
|
|
187
|
-
result: message.content
|
|
188
|
-
}
|
|
189
|
-
]
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return result;
|
|
18
|
+
function isZodType(value) {
|
|
19
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
194
20
|
}
|
|
195
21
|
function zodToJsonSchema(zodSchema) {
|
|
196
|
-
if (!(zodSchema
|
|
22
|
+
if (!isZodType(zodSchema)) {
|
|
197
23
|
return zodSchema;
|
|
198
24
|
}
|
|
25
|
+
if ("toJSONSchema" in z) {
|
|
26
|
+
const fn = "toJSONSchema";
|
|
27
|
+
return z[fn].call(z, zodSchema);
|
|
28
|
+
}
|
|
199
29
|
return originalZodToJsonSchema(zodSchema, { $refStrategy: "none" });
|
|
200
30
|
}
|
|
31
|
+
|
|
32
|
+
// src/utils/process-client-tools.ts
|
|
201
33
|
function processClientTools(clientTools) {
|
|
202
34
|
if (!clientTools) {
|
|
203
35
|
return void 0;
|
|
@@ -226,6 +58,42 @@ function processClientTools(clientTools) {
|
|
|
226
58
|
);
|
|
227
59
|
}
|
|
228
60
|
|
|
61
|
+
// src/utils/process-mastra-stream.ts
|
|
62
|
+
async function processMastraStream({
|
|
63
|
+
stream,
|
|
64
|
+
onChunk
|
|
65
|
+
}) {
|
|
66
|
+
const reader = stream.getReader();
|
|
67
|
+
const decoder = new TextDecoder();
|
|
68
|
+
let buffer = "";
|
|
69
|
+
try {
|
|
70
|
+
while (true) {
|
|
71
|
+
const { done, value } = await reader.read();
|
|
72
|
+
if (done) break;
|
|
73
|
+
buffer += decoder.decode(value, { stream: true });
|
|
74
|
+
const lines = buffer.split("\n\n");
|
|
75
|
+
buffer = lines.pop() || "";
|
|
76
|
+
for (const line of lines) {
|
|
77
|
+
if (line.startsWith("data: ")) {
|
|
78
|
+
const data = line.slice(6);
|
|
79
|
+
if (data === "[DONE]") {
|
|
80
|
+
console.log("\u{1F3C1} Stream finished");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const json = JSON.parse(data);
|
|
85
|
+
await onChunk(json);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} finally {
|
|
93
|
+
reader.releaseLock();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
229
97
|
// src/resources/base.ts
|
|
230
98
|
var BaseResource = class {
|
|
231
99
|
options;
|
|
@@ -240,20 +108,21 @@ var BaseResource = class {
|
|
|
240
108
|
*/
|
|
241
109
|
async request(path, options = {}) {
|
|
242
110
|
let lastError = null;
|
|
243
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
111
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
244
112
|
let delay = backoffMs;
|
|
245
113
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
246
114
|
try {
|
|
247
115
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
248
116
|
...options,
|
|
249
117
|
headers: {
|
|
250
|
-
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
118
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
251
119
|
...headers,
|
|
252
120
|
...options.headers
|
|
253
121
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
254
122
|
// 'x-mastra-client-type': 'js',
|
|
255
123
|
},
|
|
256
124
|
signal: this.options.abortSignal,
|
|
125
|
+
credentials: options.credentials ?? credentials,
|
|
257
126
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
258
127
|
});
|
|
259
128
|
if (!response.ok) {
|
|
@@ -286,14 +155,64 @@ var BaseResource = class {
|
|
|
286
155
|
throw lastError || new Error("Request failed");
|
|
287
156
|
}
|
|
288
157
|
};
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
158
|
+
|
|
159
|
+
// src/resources/agent.ts
|
|
160
|
+
async function executeToolCallAndRespond({
|
|
161
|
+
response,
|
|
162
|
+
params,
|
|
163
|
+
runId,
|
|
164
|
+
resourceId,
|
|
165
|
+
threadId,
|
|
166
|
+
runtimeContext,
|
|
167
|
+
respondFn
|
|
168
|
+
}) {
|
|
169
|
+
if (response.finishReason === "tool-calls") {
|
|
170
|
+
const toolCalls = response.toolCalls;
|
|
171
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
172
|
+
return response;
|
|
173
|
+
}
|
|
174
|
+
for (const toolCall of toolCalls) {
|
|
175
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
176
|
+
if (clientTool && clientTool.execute) {
|
|
177
|
+
const result = await clientTool.execute(
|
|
178
|
+
{
|
|
179
|
+
context: toolCall?.args,
|
|
180
|
+
runId,
|
|
181
|
+
resourceId,
|
|
182
|
+
threadId,
|
|
183
|
+
runtimeContext,
|
|
184
|
+
tracingContext: { currentSpan: void 0 }
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
messages: response.messages,
|
|
188
|
+
toolCallId: toolCall?.toolCallId
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
const updatedMessages = [
|
|
192
|
+
{
|
|
193
|
+
role: "user",
|
|
194
|
+
content: params.messages
|
|
195
|
+
},
|
|
196
|
+
...response.response.messages,
|
|
197
|
+
{
|
|
198
|
+
role: "tool",
|
|
199
|
+
content: [
|
|
200
|
+
{
|
|
201
|
+
type: "tool-result",
|
|
202
|
+
toolCallId: toolCall.toolCallId,
|
|
203
|
+
toolName: toolCall.toolName,
|
|
204
|
+
result
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
}
|
|
208
|
+
];
|
|
209
|
+
return respondFn({
|
|
210
|
+
...params,
|
|
211
|
+
messages: updatedMessages
|
|
212
|
+
});
|
|
213
|
+
}
|
|
293
214
|
}
|
|
294
|
-
return runtimeContext;
|
|
295
215
|
}
|
|
296
|
-
return void 0;
|
|
297
216
|
}
|
|
298
217
|
var AgentVoice = class extends BaseResource {
|
|
299
218
|
constructor(options, agentId) {
|
|
@@ -364,6 +283,12 @@ var Agent = class extends BaseResource {
|
|
|
364
283
|
return this.request(`/api/agents/${this.agentId}`);
|
|
365
284
|
}
|
|
366
285
|
async generate(params) {
|
|
286
|
+
console.warn(
|
|
287
|
+
"Deprecation NOTICE:Generate method will switch to use generateVNext implementation September 16th. Please use generateLegacy if you don't want to upgrade just yet."
|
|
288
|
+
);
|
|
289
|
+
return this.generateLegacy(params);
|
|
290
|
+
}
|
|
291
|
+
async generateLegacy(params) {
|
|
367
292
|
const processedParams = {
|
|
368
293
|
...params,
|
|
369
294
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -372,10 +297,13 @@ var Agent = class extends BaseResource {
|
|
|
372
297
|
clientTools: processClientTools(params.clientTools)
|
|
373
298
|
};
|
|
374
299
|
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
375
|
-
const response = await this.request(
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
300
|
+
const response = await this.request(
|
|
301
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
302
|
+
{
|
|
303
|
+
method: "POST",
|
|
304
|
+
body: processedParams
|
|
305
|
+
}
|
|
306
|
+
);
|
|
379
307
|
if (response.finishReason === "tool-calls") {
|
|
380
308
|
const toolCalls = response.toolCalls;
|
|
381
309
|
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
@@ -385,7 +313,14 @@ var Agent = class extends BaseResource {
|
|
|
385
313
|
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
386
314
|
if (clientTool && clientTool.execute) {
|
|
387
315
|
const result = await clientTool.execute(
|
|
388
|
-
{
|
|
316
|
+
{
|
|
317
|
+
context: toolCall?.args,
|
|
318
|
+
runId,
|
|
319
|
+
resourceId,
|
|
320
|
+
threadId,
|
|
321
|
+
runtimeContext,
|
|
322
|
+
tracingContext: { currentSpan: void 0 }
|
|
323
|
+
},
|
|
389
324
|
{
|
|
390
325
|
messages: response.messages,
|
|
391
326
|
toolCallId: toolCall?.toolCallId
|
|
@@ -418,14 +353,41 @@ var Agent = class extends BaseResource {
|
|
|
418
353
|
}
|
|
419
354
|
return response;
|
|
420
355
|
}
|
|
356
|
+
async generateVNext(params) {
|
|
357
|
+
const processedParams = {
|
|
358
|
+
...params,
|
|
359
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
360
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
361
|
+
clientTools: processClientTools(params.clientTools)
|
|
362
|
+
};
|
|
363
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
364
|
+
const response = await this.request(
|
|
365
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
366
|
+
{
|
|
367
|
+
method: "POST",
|
|
368
|
+
body: processedParams
|
|
369
|
+
}
|
|
370
|
+
);
|
|
371
|
+
if (response.finishReason === "tool-calls") {
|
|
372
|
+
return executeToolCallAndRespond({
|
|
373
|
+
response,
|
|
374
|
+
params,
|
|
375
|
+
runId,
|
|
376
|
+
resourceId,
|
|
377
|
+
threadId,
|
|
378
|
+
runtimeContext,
|
|
379
|
+
respondFn: this.generateVNext.bind(this)
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
return response;
|
|
383
|
+
}
|
|
421
384
|
async processChatResponse({
|
|
422
385
|
stream,
|
|
423
386
|
update,
|
|
424
387
|
onToolCall,
|
|
425
388
|
onFinish,
|
|
426
389
|
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
427
|
-
lastMessage
|
|
428
|
-
streamProtocol
|
|
390
|
+
lastMessage
|
|
429
391
|
}) {
|
|
430
392
|
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
431
393
|
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
@@ -486,257 +448,539 @@ var Agent = class extends BaseResource {
|
|
|
486
448
|
replaceLastMessage
|
|
487
449
|
});
|
|
488
450
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
451
|
+
await processDataStream({
|
|
452
|
+
stream,
|
|
453
|
+
onTextPart(value) {
|
|
454
|
+
if (currentTextPart == null) {
|
|
455
|
+
currentTextPart = {
|
|
456
|
+
type: "text",
|
|
457
|
+
text: value
|
|
458
|
+
};
|
|
459
|
+
message.parts.push(currentTextPart);
|
|
460
|
+
} else {
|
|
461
|
+
currentTextPart.text += value;
|
|
495
462
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
type: "text",
|
|
505
|
-
text: value
|
|
506
|
-
};
|
|
507
|
-
message.parts.push(currentTextPart);
|
|
508
|
-
} else {
|
|
509
|
-
currentTextPart.text += value;
|
|
510
|
-
}
|
|
511
|
-
message.content += value;
|
|
512
|
-
execUpdate();
|
|
513
|
-
},
|
|
514
|
-
onReasoningPart(value) {
|
|
515
|
-
if (currentReasoningTextDetail == null) {
|
|
516
|
-
currentReasoningTextDetail = { type: "text", text: value };
|
|
517
|
-
if (currentReasoningPart != null) {
|
|
518
|
-
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
519
|
-
}
|
|
520
|
-
} else {
|
|
521
|
-
currentReasoningTextDetail.text += value;
|
|
522
|
-
}
|
|
523
|
-
if (currentReasoningPart == null) {
|
|
524
|
-
currentReasoningPart = {
|
|
525
|
-
type: "reasoning",
|
|
526
|
-
reasoning: value,
|
|
527
|
-
details: [currentReasoningTextDetail]
|
|
528
|
-
};
|
|
529
|
-
message.parts.push(currentReasoningPart);
|
|
530
|
-
} else {
|
|
531
|
-
currentReasoningPart.reasoning += value;
|
|
532
|
-
}
|
|
533
|
-
message.reasoning = (message.reasoning ?? "") + value;
|
|
534
|
-
execUpdate();
|
|
535
|
-
},
|
|
536
|
-
onReasoningSignaturePart(value) {
|
|
537
|
-
if (currentReasoningTextDetail != null) {
|
|
538
|
-
currentReasoningTextDetail.signature = value.signature;
|
|
539
|
-
}
|
|
540
|
-
},
|
|
541
|
-
onRedactedReasoningPart(value) {
|
|
542
|
-
if (currentReasoningPart == null) {
|
|
543
|
-
currentReasoningPart = {
|
|
544
|
-
type: "reasoning",
|
|
545
|
-
reasoning: "",
|
|
546
|
-
details: []
|
|
547
|
-
};
|
|
548
|
-
message.parts.push(currentReasoningPart);
|
|
463
|
+
message.content += value;
|
|
464
|
+
execUpdate();
|
|
465
|
+
},
|
|
466
|
+
onReasoningPart(value) {
|
|
467
|
+
if (currentReasoningTextDetail == null) {
|
|
468
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
469
|
+
if (currentReasoningPart != null) {
|
|
470
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
549
471
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
472
|
+
} else {
|
|
473
|
+
currentReasoningTextDetail.text += value;
|
|
474
|
+
}
|
|
475
|
+
if (currentReasoningPart == null) {
|
|
476
|
+
currentReasoningPart = {
|
|
477
|
+
type: "reasoning",
|
|
478
|
+
reasoning: value,
|
|
479
|
+
details: [currentReasoningTextDetail]
|
|
480
|
+
};
|
|
481
|
+
message.parts.push(currentReasoningPart);
|
|
482
|
+
} else {
|
|
483
|
+
currentReasoningPart.reasoning += value;
|
|
484
|
+
}
|
|
485
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
486
|
+
execUpdate();
|
|
487
|
+
},
|
|
488
|
+
onReasoningSignaturePart(value) {
|
|
489
|
+
if (currentReasoningTextDetail != null) {
|
|
490
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
onRedactedReasoningPart(value) {
|
|
494
|
+
if (currentReasoningPart == null) {
|
|
495
|
+
currentReasoningPart = {
|
|
496
|
+
type: "reasoning",
|
|
497
|
+
reasoning: "",
|
|
498
|
+
details: []
|
|
499
|
+
};
|
|
500
|
+
message.parts.push(currentReasoningPart);
|
|
501
|
+
}
|
|
502
|
+
currentReasoningPart.details.push({
|
|
503
|
+
type: "redacted",
|
|
504
|
+
data: value.data
|
|
505
|
+
});
|
|
506
|
+
currentReasoningTextDetail = void 0;
|
|
507
|
+
execUpdate();
|
|
508
|
+
},
|
|
509
|
+
onFilePart(value) {
|
|
510
|
+
message.parts.push({
|
|
511
|
+
type: "file",
|
|
512
|
+
mimeType: value.mimeType,
|
|
513
|
+
data: value.data
|
|
514
|
+
});
|
|
515
|
+
execUpdate();
|
|
516
|
+
},
|
|
517
|
+
onSourcePart(value) {
|
|
518
|
+
message.parts.push({
|
|
519
|
+
type: "source",
|
|
520
|
+
source: value
|
|
521
|
+
});
|
|
522
|
+
execUpdate();
|
|
523
|
+
},
|
|
524
|
+
onToolCallStreamingStartPart(value) {
|
|
525
|
+
if (message.toolInvocations == null) {
|
|
526
|
+
message.toolInvocations = [];
|
|
527
|
+
}
|
|
528
|
+
partialToolCalls[value.toolCallId] = {
|
|
529
|
+
text: "",
|
|
530
|
+
step,
|
|
531
|
+
toolName: value.toolName,
|
|
532
|
+
index: message.toolInvocations.length
|
|
533
|
+
};
|
|
534
|
+
const invocation = {
|
|
535
|
+
state: "partial-call",
|
|
536
|
+
step,
|
|
537
|
+
toolCallId: value.toolCallId,
|
|
538
|
+
toolName: value.toolName,
|
|
539
|
+
args: void 0
|
|
540
|
+
};
|
|
541
|
+
message.toolInvocations.push(invocation);
|
|
542
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
543
|
+
execUpdate();
|
|
544
|
+
},
|
|
545
|
+
onToolCallDeltaPart(value) {
|
|
546
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
547
|
+
partialToolCall.text += value.argsTextDelta;
|
|
548
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
549
|
+
const invocation = {
|
|
550
|
+
state: "partial-call",
|
|
551
|
+
step: partialToolCall.step,
|
|
552
|
+
toolCallId: value.toolCallId,
|
|
553
|
+
toolName: partialToolCall.toolName,
|
|
554
|
+
args: partialArgs
|
|
555
|
+
};
|
|
556
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
557
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
558
|
+
execUpdate();
|
|
559
|
+
},
|
|
560
|
+
async onToolCallPart(value) {
|
|
561
|
+
const invocation = {
|
|
562
|
+
state: "call",
|
|
563
|
+
step,
|
|
564
|
+
...value
|
|
565
|
+
};
|
|
566
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
567
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
568
|
+
} else {
|
|
573
569
|
if (message.toolInvocations == null) {
|
|
574
570
|
message.toolInvocations = [];
|
|
575
571
|
}
|
|
576
|
-
partialToolCalls[value.toolCallId] = {
|
|
577
|
-
text: "",
|
|
578
|
-
step,
|
|
579
|
-
toolName: value.toolName,
|
|
580
|
-
index: message.toolInvocations.length
|
|
581
|
-
};
|
|
582
|
-
const invocation = {
|
|
583
|
-
state: "partial-call",
|
|
584
|
-
step,
|
|
585
|
-
toolCallId: value.toolCallId,
|
|
586
|
-
toolName: value.toolName,
|
|
587
|
-
args: void 0
|
|
588
|
-
};
|
|
589
572
|
message.toolInvocations.push(invocation);
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
606
|
-
execUpdate();
|
|
607
|
-
},
|
|
608
|
-
async onToolCallPart(value) {
|
|
609
|
-
const invocation = {
|
|
610
|
-
state: "call",
|
|
611
|
-
step,
|
|
612
|
-
...value
|
|
613
|
-
};
|
|
614
|
-
if (partialToolCalls[value.toolCallId] != null) {
|
|
615
|
-
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
616
|
-
} else {
|
|
617
|
-
if (message.toolInvocations == null) {
|
|
618
|
-
message.toolInvocations = [];
|
|
619
|
-
}
|
|
620
|
-
message.toolInvocations.push(invocation);
|
|
621
|
-
}
|
|
622
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
623
|
-
execUpdate();
|
|
624
|
-
if (onToolCall) {
|
|
625
|
-
const result = await onToolCall({ toolCall: value });
|
|
626
|
-
if (result != null) {
|
|
627
|
-
const invocation2 = {
|
|
628
|
-
state: "result",
|
|
629
|
-
step,
|
|
630
|
-
...value,
|
|
631
|
-
result
|
|
632
|
-
};
|
|
633
|
-
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
634
|
-
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
635
|
-
execUpdate();
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
},
|
|
639
|
-
onToolResultPart(value) {
|
|
640
|
-
const toolInvocations = message.toolInvocations;
|
|
641
|
-
if (toolInvocations == null) {
|
|
642
|
-
throw new Error("tool_result must be preceded by a tool_call");
|
|
643
|
-
}
|
|
644
|
-
const toolInvocationIndex = toolInvocations.findIndex(
|
|
645
|
-
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
646
|
-
);
|
|
647
|
-
if (toolInvocationIndex === -1) {
|
|
648
|
-
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
649
|
-
}
|
|
650
|
-
const invocation = {
|
|
651
|
-
...toolInvocations[toolInvocationIndex],
|
|
652
|
-
state: "result",
|
|
653
|
-
...value
|
|
654
|
-
};
|
|
655
|
-
toolInvocations[toolInvocationIndex] = invocation;
|
|
656
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
657
|
-
execUpdate();
|
|
658
|
-
},
|
|
659
|
-
onDataPart(value) {
|
|
660
|
-
data.push(...value);
|
|
661
|
-
execUpdate();
|
|
662
|
-
},
|
|
663
|
-
onMessageAnnotationsPart(value) {
|
|
664
|
-
if (messageAnnotations == null) {
|
|
665
|
-
messageAnnotations = [...value];
|
|
666
|
-
} else {
|
|
667
|
-
messageAnnotations.push(...value);
|
|
668
|
-
}
|
|
669
|
-
execUpdate();
|
|
670
|
-
},
|
|
671
|
-
onFinishStepPart(value) {
|
|
672
|
-
step += 1;
|
|
673
|
-
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
674
|
-
currentReasoningPart = void 0;
|
|
675
|
-
currentReasoningTextDetail = void 0;
|
|
676
|
-
},
|
|
677
|
-
onStartStepPart(value) {
|
|
678
|
-
if (!replaceLastMessage) {
|
|
679
|
-
message.id = value.messageId;
|
|
680
|
-
}
|
|
681
|
-
message.parts.push({ type: "step-start" });
|
|
682
|
-
execUpdate();
|
|
683
|
-
},
|
|
684
|
-
onFinishMessagePart(value) {
|
|
685
|
-
finishReason = value.finishReason;
|
|
686
|
-
if (value.usage != null) {
|
|
687
|
-
usage = value.usage;
|
|
573
|
+
}
|
|
574
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
575
|
+
execUpdate();
|
|
576
|
+
if (onToolCall) {
|
|
577
|
+
const result = await onToolCall({ toolCall: value });
|
|
578
|
+
if (result != null) {
|
|
579
|
+
const invocation2 = {
|
|
580
|
+
state: "result",
|
|
581
|
+
step,
|
|
582
|
+
...value,
|
|
583
|
+
result
|
|
584
|
+
};
|
|
585
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
586
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
587
|
+
execUpdate();
|
|
688
588
|
}
|
|
689
|
-
},
|
|
690
|
-
onErrorPart(error) {
|
|
691
|
-
throw new Error(error);
|
|
692
589
|
}
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
|
|
590
|
+
},
|
|
591
|
+
onToolResultPart(value) {
|
|
592
|
+
const toolInvocations = message.toolInvocations;
|
|
593
|
+
if (toolInvocations == null) {
|
|
594
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
595
|
+
}
|
|
596
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
597
|
+
if (toolInvocationIndex === -1) {
|
|
598
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
599
|
+
}
|
|
600
|
+
const invocation = {
|
|
601
|
+
...toolInvocations[toolInvocationIndex],
|
|
602
|
+
state: "result",
|
|
603
|
+
...value
|
|
604
|
+
};
|
|
605
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
606
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
607
|
+
execUpdate();
|
|
608
|
+
},
|
|
609
|
+
onDataPart(value) {
|
|
610
|
+
data.push(...value);
|
|
611
|
+
execUpdate();
|
|
612
|
+
},
|
|
613
|
+
onMessageAnnotationsPart(value) {
|
|
614
|
+
if (messageAnnotations == null) {
|
|
615
|
+
messageAnnotations = [...value];
|
|
616
|
+
} else {
|
|
617
|
+
messageAnnotations.push(...value);
|
|
618
|
+
}
|
|
619
|
+
execUpdate();
|
|
620
|
+
},
|
|
621
|
+
onFinishStepPart(value) {
|
|
622
|
+
step += 1;
|
|
623
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
624
|
+
currentReasoningPart = void 0;
|
|
625
|
+
currentReasoningTextDetail = void 0;
|
|
626
|
+
},
|
|
627
|
+
onStartStepPart(value) {
|
|
628
|
+
if (!replaceLastMessage) {
|
|
629
|
+
message.id = value.messageId;
|
|
630
|
+
}
|
|
631
|
+
message.parts.push({ type: "step-start" });
|
|
632
|
+
execUpdate();
|
|
633
|
+
},
|
|
634
|
+
onFinishMessagePart(value) {
|
|
635
|
+
finishReason = value.finishReason;
|
|
636
|
+
if (value.usage != null) {
|
|
637
|
+
usage = value.usage;
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
onErrorPart(error) {
|
|
641
|
+
throw new Error(error);
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
onFinish?.({ message, finishReason, usage });
|
|
696
645
|
}
|
|
697
646
|
/**
|
|
698
|
-
*
|
|
647
|
+
* Streams a response from the agent
|
|
648
|
+
* @param params - Stream parameters including prompt
|
|
649
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
699
650
|
*/
|
|
700
|
-
async
|
|
701
|
-
|
|
702
|
-
method
|
|
703
|
-
|
|
704
|
-
|
|
651
|
+
async stream(params) {
|
|
652
|
+
console.warn(
|
|
653
|
+
"Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 16th. Please use streamLegacy if you don't want to upgrade just yet."
|
|
654
|
+
);
|
|
655
|
+
return this.streamLegacy(params);
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Streams a response from the agent
|
|
659
|
+
* @param params - Stream parameters including prompt
|
|
660
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
661
|
+
*/
|
|
662
|
+
async streamLegacy(params) {
|
|
663
|
+
const processedParams = {
|
|
664
|
+
...params,
|
|
665
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
666
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
667
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
668
|
+
clientTools: processClientTools(params.clientTools)
|
|
669
|
+
};
|
|
670
|
+
const { readable, writable } = new TransformStream();
|
|
671
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
672
|
+
const streamResponse = new Response(readable, {
|
|
673
|
+
status: response.status,
|
|
674
|
+
statusText: response.statusText,
|
|
675
|
+
headers: response.headers
|
|
705
676
|
});
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
const streamProtocol = processedParams.output ? "text" : "data";
|
|
711
|
-
let toolCalls = [];
|
|
712
|
-
let messages = [];
|
|
713
|
-
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
714
|
-
streamForWritable.pipeTo(writable, {
|
|
715
|
-
preventClose: true
|
|
716
|
-
}).catch((error) => {
|
|
717
|
-
console.error("Error piping to writable stream:", error);
|
|
677
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
678
|
+
await processDataStream({
|
|
679
|
+
stream: streamResponse.body,
|
|
680
|
+
...options
|
|
718
681
|
});
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
682
|
+
};
|
|
683
|
+
return streamResponse;
|
|
684
|
+
}
|
|
685
|
+
async processChatResponse_vNext({
|
|
686
|
+
stream,
|
|
687
|
+
update,
|
|
688
|
+
onToolCall,
|
|
689
|
+
onFinish,
|
|
690
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
691
|
+
lastMessage
|
|
692
|
+
}) {
|
|
693
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
694
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
695
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
696
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
697
|
+
}, 0) ?? 0) : 0;
|
|
698
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
699
|
+
id: v4(),
|
|
700
|
+
createdAt: getCurrentDate(),
|
|
701
|
+
role: "assistant",
|
|
702
|
+
content: "",
|
|
703
|
+
parts: []
|
|
704
|
+
};
|
|
705
|
+
let currentTextPart = void 0;
|
|
706
|
+
let currentReasoningPart = void 0;
|
|
707
|
+
let currentReasoningTextDetail = void 0;
|
|
708
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
709
|
+
const part = message.parts.find(
|
|
710
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
711
|
+
);
|
|
712
|
+
if (part != null) {
|
|
713
|
+
part.toolInvocation = invocation;
|
|
714
|
+
} else {
|
|
715
|
+
message.parts.push({
|
|
716
|
+
type: "tool-invocation",
|
|
717
|
+
toolInvocation: invocation
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
const data = [];
|
|
722
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
723
|
+
const partialToolCalls = {};
|
|
724
|
+
let usage = {
|
|
725
|
+
completionTokens: NaN,
|
|
726
|
+
promptTokens: NaN,
|
|
727
|
+
totalTokens: NaN
|
|
728
|
+
};
|
|
729
|
+
let finishReason = "unknown";
|
|
730
|
+
function execUpdate() {
|
|
731
|
+
const copiedData = [...data];
|
|
732
|
+
if (messageAnnotations?.length) {
|
|
733
|
+
message.annotations = messageAnnotations;
|
|
734
|
+
}
|
|
735
|
+
const copiedMessage = {
|
|
736
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
737
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
738
|
+
...structuredClone(message),
|
|
739
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
740
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
741
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
742
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
743
|
+
// forwarded to rendering):
|
|
744
|
+
revisionId: v4()
|
|
745
|
+
};
|
|
746
|
+
update({
|
|
747
|
+
message: copiedMessage,
|
|
748
|
+
data: copiedData,
|
|
749
|
+
replaceLastMessage
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
await processMastraStream({
|
|
753
|
+
stream,
|
|
754
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
755
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
756
|
+
onChunk: async (chunk) => {
|
|
757
|
+
switch (chunk.type) {
|
|
758
|
+
case "step-start": {
|
|
759
|
+
if (!replaceLastMessage) {
|
|
760
|
+
message.id = chunk.payload.messageId;
|
|
761
|
+
}
|
|
762
|
+
message.parts.push({ type: "step-start" });
|
|
763
|
+
execUpdate();
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
case "text-delta": {
|
|
767
|
+
if (currentTextPart == null) {
|
|
768
|
+
currentTextPart = {
|
|
769
|
+
type: "text",
|
|
770
|
+
text: chunk.payload.text
|
|
771
|
+
};
|
|
772
|
+
message.parts.push(currentTextPart);
|
|
773
|
+
} else {
|
|
774
|
+
currentTextPart.text += chunk.payload.text;
|
|
775
|
+
}
|
|
776
|
+
message.content += chunk.payload.text;
|
|
777
|
+
execUpdate();
|
|
778
|
+
break;
|
|
779
|
+
}
|
|
780
|
+
case "reasoning-delta": {
|
|
781
|
+
if (currentReasoningTextDetail == null) {
|
|
782
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
783
|
+
if (currentReasoningPart != null) {
|
|
784
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
785
|
+
}
|
|
786
|
+
} else {
|
|
787
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
788
|
+
}
|
|
789
|
+
if (currentReasoningPart == null) {
|
|
790
|
+
currentReasoningPart = {
|
|
791
|
+
type: "reasoning",
|
|
792
|
+
reasoning: chunk.payload.text,
|
|
793
|
+
details: [currentReasoningTextDetail]
|
|
794
|
+
};
|
|
795
|
+
message.parts.push(currentReasoningPart);
|
|
796
|
+
} else {
|
|
797
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
798
|
+
}
|
|
799
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
800
|
+
execUpdate();
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
case "file": {
|
|
804
|
+
message.parts.push({
|
|
805
|
+
type: "file",
|
|
806
|
+
mimeType: chunk.payload.mimeType,
|
|
807
|
+
data: chunk.payload.data
|
|
808
|
+
});
|
|
809
|
+
execUpdate();
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
case "source": {
|
|
813
|
+
message.parts.push({
|
|
814
|
+
type: "source",
|
|
815
|
+
source: chunk.payload.source
|
|
816
|
+
});
|
|
817
|
+
execUpdate();
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
case "tool-call": {
|
|
821
|
+
const invocation = {
|
|
822
|
+
state: "call",
|
|
823
|
+
step,
|
|
824
|
+
...chunk.payload
|
|
825
|
+
};
|
|
826
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
827
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
828
|
+
} else {
|
|
829
|
+
if (message.toolInvocations == null) {
|
|
830
|
+
message.toolInvocations = [];
|
|
831
|
+
}
|
|
832
|
+
message.toolInvocations.push(invocation);
|
|
833
|
+
}
|
|
834
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
835
|
+
execUpdate();
|
|
836
|
+
if (onToolCall) {
|
|
837
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
838
|
+
if (result != null) {
|
|
839
|
+
const invocation2 = {
|
|
840
|
+
state: "result",
|
|
841
|
+
step,
|
|
842
|
+
...chunk.payload,
|
|
843
|
+
result
|
|
844
|
+
};
|
|
845
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
846
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
847
|
+
execUpdate();
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
case "tool-call-input-streaming-start": {
|
|
852
|
+
if (message.toolInvocations == null) {
|
|
853
|
+
message.toolInvocations = [];
|
|
854
|
+
}
|
|
855
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
856
|
+
text: "",
|
|
857
|
+
step,
|
|
858
|
+
toolName: chunk.payload.toolName,
|
|
859
|
+
index: message.toolInvocations.length
|
|
860
|
+
};
|
|
861
|
+
const invocation = {
|
|
862
|
+
state: "partial-call",
|
|
863
|
+
step,
|
|
864
|
+
toolCallId: chunk.payload.toolCallId,
|
|
865
|
+
toolName: chunk.payload.toolName,
|
|
866
|
+
args: void 0
|
|
867
|
+
};
|
|
868
|
+
message.toolInvocations.push(invocation);
|
|
869
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
870
|
+
execUpdate();
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
case "tool-call-delta": {
|
|
874
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
875
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
876
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
877
|
+
const invocation = {
|
|
878
|
+
state: "partial-call",
|
|
879
|
+
step: partialToolCall.step,
|
|
880
|
+
toolCallId: chunk.payload.toolCallId,
|
|
881
|
+
toolName: partialToolCall.toolName,
|
|
882
|
+
args: partialArgs
|
|
883
|
+
};
|
|
884
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
885
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
886
|
+
execUpdate();
|
|
887
|
+
break;
|
|
888
|
+
}
|
|
889
|
+
case "tool-result": {
|
|
890
|
+
const toolInvocations = message.toolInvocations;
|
|
891
|
+
if (toolInvocations == null) {
|
|
892
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
893
|
+
}
|
|
894
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
895
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
896
|
+
);
|
|
897
|
+
if (toolInvocationIndex === -1) {
|
|
898
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
899
|
+
}
|
|
900
|
+
const invocation = {
|
|
901
|
+
...toolInvocations[toolInvocationIndex],
|
|
902
|
+
state: "result",
|
|
903
|
+
...chunk.payload
|
|
904
|
+
};
|
|
905
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
906
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
907
|
+
execUpdate();
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
case "error": {
|
|
911
|
+
throw new Error(chunk.payload.error);
|
|
912
|
+
}
|
|
913
|
+
case "data": {
|
|
914
|
+
data.push(...chunk.payload.data);
|
|
915
|
+
execUpdate();
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
case "step-finish": {
|
|
919
|
+
step += 1;
|
|
920
|
+
currentTextPart = chunk.payload.isContinued ? currentTextPart : void 0;
|
|
921
|
+
currentReasoningPart = void 0;
|
|
922
|
+
currentReasoningTextDetail = void 0;
|
|
923
|
+
execUpdate();
|
|
924
|
+
break;
|
|
925
|
+
}
|
|
926
|
+
case "finish": {
|
|
927
|
+
finishReason = chunk.payload.finishReason;
|
|
928
|
+
if (chunk.payload.usage != null) {
|
|
929
|
+
usage = chunk.payload.usage;
|
|
930
|
+
}
|
|
931
|
+
break;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
onFinish?.({ message, finishReason, usage });
|
|
937
|
+
}
|
|
938
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
939
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
940
|
+
method: "POST",
|
|
941
|
+
body: processedParams,
|
|
942
|
+
stream: true
|
|
943
|
+
});
|
|
944
|
+
if (!response.body) {
|
|
945
|
+
throw new Error("No response body");
|
|
946
|
+
}
|
|
947
|
+
try {
|
|
948
|
+
let toolCalls = [];
|
|
949
|
+
let messages = [];
|
|
950
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
951
|
+
streamForWritable.pipeTo(writable, {
|
|
952
|
+
preventClose: true
|
|
953
|
+
}).catch((error) => {
|
|
954
|
+
console.error("Error piping to writable stream:", error);
|
|
955
|
+
});
|
|
956
|
+
this.processChatResponse_vNext({
|
|
957
|
+
stream: streamForProcessing,
|
|
958
|
+
update: ({ message }) => {
|
|
959
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
960
|
+
if (existingIndex !== -1) {
|
|
961
|
+
messages[existingIndex] = message;
|
|
962
|
+
} else {
|
|
963
|
+
messages.push(message);
|
|
964
|
+
}
|
|
965
|
+
},
|
|
966
|
+
onFinish: async ({ finishReason, message }) => {
|
|
967
|
+
if (finishReason === "tool-calls") {
|
|
968
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
969
|
+
if (toolCall) {
|
|
970
|
+
toolCalls.push(toolCall);
|
|
971
|
+
}
|
|
972
|
+
for (const toolCall2 of toolCalls) {
|
|
973
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
974
|
+
if (clientTool && clientTool.execute) {
|
|
975
|
+
const result = await clientTool.execute(
|
|
976
|
+
{
|
|
977
|
+
context: toolCall2?.args,
|
|
736
978
|
runId: processedParams.runId,
|
|
737
979
|
resourceId: processedParams.resourceId,
|
|
738
980
|
threadId: processedParams.threadId,
|
|
739
|
-
runtimeContext: processedParams.runtimeContext
|
|
981
|
+
runtimeContext: processedParams.runtimeContext,
|
|
982
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
983
|
+
tracingContext: { currentSpan: void 0 }
|
|
740
984
|
},
|
|
741
985
|
{
|
|
742
986
|
messages: response.messages,
|
|
@@ -776,145 +1020,61 @@ var Agent = class extends BaseResource {
|
|
|
776
1020
|
}
|
|
777
1021
|
const originalMessages = processedParams.messages;
|
|
778
1022
|
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
779
|
-
this.
|
|
1023
|
+
this.processStreamResponse_vNext(
|
|
780
1024
|
{
|
|
781
1025
|
...processedParams,
|
|
782
|
-
messages: [...messageArray, ...messages, lastMessage]
|
|
1026
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
783
1027
|
},
|
|
784
1028
|
writable
|
|
785
|
-
)
|
|
1029
|
+
).catch((error) => {
|
|
1030
|
+
console.error("Error processing stream response:", error);
|
|
1031
|
+
});
|
|
786
1032
|
}
|
|
787
1033
|
}
|
|
788
1034
|
} else {
|
|
789
1035
|
setTimeout(() => {
|
|
790
|
-
|
|
791
|
-
writable.close();
|
|
792
|
-
}
|
|
1036
|
+
writable.close();
|
|
793
1037
|
}, 0);
|
|
794
1038
|
}
|
|
795
1039
|
},
|
|
796
|
-
lastMessage: void 0
|
|
797
|
-
|
|
1040
|
+
lastMessage: void 0
|
|
1041
|
+
}).catch((error) => {
|
|
1042
|
+
console.error("Error processing stream response:", error);
|
|
798
1043
|
});
|
|
799
1044
|
} catch (error) {
|
|
800
1045
|
console.error("Error processing stream response:", error);
|
|
801
1046
|
}
|
|
802
1047
|
return response;
|
|
803
1048
|
}
|
|
804
|
-
|
|
805
|
-
* Streams a response from the agent
|
|
806
|
-
* @param params - Stream parameters including prompt
|
|
807
|
-
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
808
|
-
*/
|
|
809
|
-
async stream(params) {
|
|
1049
|
+
async streamVNext(params) {
|
|
810
1050
|
const processedParams = {
|
|
811
1051
|
...params,
|
|
812
1052
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
813
|
-
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
814
1053
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
815
1054
|
clientTools: processClientTools(params.clientTools)
|
|
816
1055
|
};
|
|
817
1056
|
const { readable, writable } = new TransformStream();
|
|
818
|
-
const response = await this.
|
|
1057
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
819
1058
|
const streamResponse = new Response(readable, {
|
|
820
1059
|
status: response.status,
|
|
821
1060
|
statusText: response.statusText,
|
|
822
1061
|
headers: response.headers
|
|
823
1062
|
});
|
|
824
|
-
streamResponse.processDataStream = async (
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
});
|
|
829
|
-
};
|
|
830
|
-
streamResponse.processTextStream = async (options) => {
|
|
831
|
-
await processTextStream({
|
|
1063
|
+
streamResponse.processDataStream = async ({
|
|
1064
|
+
onChunk
|
|
1065
|
+
}) => {
|
|
1066
|
+
await processMastraStream({
|
|
832
1067
|
stream: streamResponse.body,
|
|
833
|
-
|
|
834
|
-
})
|
|
1068
|
+
onChunk
|
|
835
1069
|
});
|
|
836
1070
|
};
|
|
837
1071
|
return streamResponse;
|
|
838
1072
|
}
|
|
839
1073
|
/**
|
|
840
|
-
*
|
|
841
|
-
* @param toolId - ID of the tool to retrieve
|
|
842
|
-
* @returns Promise containing tool details
|
|
843
|
-
*/
|
|
844
|
-
getTool(toolId) {
|
|
845
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
846
|
-
}
|
|
847
|
-
/**
|
|
848
|
-
* Executes a tool for the agent
|
|
849
|
-
* @param toolId - ID of the tool to execute
|
|
850
|
-
* @param params - Parameters required for tool execution
|
|
851
|
-
* @returns Promise containing the tool execution results
|
|
852
|
-
*/
|
|
853
|
-
executeTool(toolId, params) {
|
|
854
|
-
const body = {
|
|
855
|
-
data: params.data,
|
|
856
|
-
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
857
|
-
};
|
|
858
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
859
|
-
method: "POST",
|
|
860
|
-
body
|
|
861
|
-
});
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Retrieves evaluation results for the agent
|
|
865
|
-
* @returns Promise containing agent evaluations
|
|
866
|
-
*/
|
|
867
|
-
evals() {
|
|
868
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
869
|
-
}
|
|
870
|
-
/**
|
|
871
|
-
* Retrieves live evaluation results for the agent
|
|
872
|
-
* @returns Promise containing live agent evaluations
|
|
873
|
-
*/
|
|
874
|
-
liveEvals() {
|
|
875
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
876
|
-
}
|
|
877
|
-
};
|
|
878
|
-
var Network = class extends BaseResource {
|
|
879
|
-
constructor(options, networkId) {
|
|
880
|
-
super(options);
|
|
881
|
-
this.networkId = networkId;
|
|
882
|
-
}
|
|
883
|
-
/**
|
|
884
|
-
* Retrieves details about the network
|
|
885
|
-
* @returns Promise containing network details
|
|
886
|
-
*/
|
|
887
|
-
details() {
|
|
888
|
-
return this.request(`/api/networks/${this.networkId}`);
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* Generates a response from the agent
|
|
892
|
-
* @param params - Generation parameters including prompt
|
|
893
|
-
* @returns Promise containing the generated response
|
|
894
|
-
*/
|
|
895
|
-
generate(params) {
|
|
896
|
-
const processedParams = {
|
|
897
|
-
...params,
|
|
898
|
-
output: zodToJsonSchema(params.output),
|
|
899
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
900
|
-
};
|
|
901
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
902
|
-
method: "POST",
|
|
903
|
-
body: processedParams
|
|
904
|
-
});
|
|
905
|
-
}
|
|
906
|
-
/**
|
|
907
|
-
* Streams a response from the agent
|
|
908
|
-
* @param params - Stream parameters including prompt
|
|
909
|
-
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
1074
|
+
* Processes the stream response and handles tool calls
|
|
910
1075
|
*/
|
|
911
|
-
async
|
|
912
|
-
const
|
|
913
|
-
...params,
|
|
914
|
-
output: zodToJsonSchema(params.output),
|
|
915
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
916
|
-
};
|
|
917
|
-
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1076
|
+
async processStreamResponse(processedParams, writable) {
|
|
1077
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
918
1078
|
method: "POST",
|
|
919
1079
|
body: processedParams,
|
|
920
1080
|
stream: true
|
|
@@ -922,15 +1082,214 @@ var Network = class extends BaseResource {
|
|
|
922
1082
|
if (!response.body) {
|
|
923
1083
|
throw new Error("No response body");
|
|
924
1084
|
}
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
1085
|
+
try {
|
|
1086
|
+
let toolCalls = [];
|
|
1087
|
+
let messages = [];
|
|
1088
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1089
|
+
streamForWritable.pipeTo(writable, {
|
|
1090
|
+
preventClose: true
|
|
1091
|
+
}).catch((error) => {
|
|
1092
|
+
console.error("Error piping to writable stream:", error);
|
|
929
1093
|
});
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
1094
|
+
this.processChatResponse({
|
|
1095
|
+
stream: streamForProcessing,
|
|
1096
|
+
update: ({ message }) => {
|
|
1097
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1098
|
+
if (existingIndex !== -1) {
|
|
1099
|
+
messages[existingIndex] = message;
|
|
1100
|
+
} else {
|
|
1101
|
+
messages.push(message);
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1105
|
+
if (finishReason === "tool-calls") {
|
|
1106
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1107
|
+
if (toolCall) {
|
|
1108
|
+
toolCalls.push(toolCall);
|
|
1109
|
+
}
|
|
1110
|
+
for (const toolCall2 of toolCalls) {
|
|
1111
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1112
|
+
if (clientTool && clientTool.execute) {
|
|
1113
|
+
const result = await clientTool.execute(
|
|
1114
|
+
{
|
|
1115
|
+
context: toolCall2?.args,
|
|
1116
|
+
runId: processedParams.runId,
|
|
1117
|
+
resourceId: processedParams.resourceId,
|
|
1118
|
+
threadId: processedParams.threadId,
|
|
1119
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1120
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1121
|
+
tracingContext: { currentSpan: void 0 }
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
messages: response.messages,
|
|
1125
|
+
toolCallId: toolCall2?.toolCallId
|
|
1126
|
+
}
|
|
1127
|
+
);
|
|
1128
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1129
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1130
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1131
|
+
);
|
|
1132
|
+
if (toolInvocationPart) {
|
|
1133
|
+
toolInvocationPart.toolInvocation = {
|
|
1134
|
+
...toolInvocationPart.toolInvocation,
|
|
1135
|
+
state: "result",
|
|
1136
|
+
result
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1140
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1141
|
+
);
|
|
1142
|
+
if (toolInvocation) {
|
|
1143
|
+
toolInvocation.state = "result";
|
|
1144
|
+
toolInvocation.result = result;
|
|
1145
|
+
}
|
|
1146
|
+
const writer = writable.getWriter();
|
|
1147
|
+
try {
|
|
1148
|
+
await writer.write(
|
|
1149
|
+
new TextEncoder().encode(
|
|
1150
|
+
"a:" + JSON.stringify({
|
|
1151
|
+
toolCallId: toolCall2.toolCallId,
|
|
1152
|
+
result
|
|
1153
|
+
}) + "\n"
|
|
1154
|
+
)
|
|
1155
|
+
);
|
|
1156
|
+
} finally {
|
|
1157
|
+
writer.releaseLock();
|
|
1158
|
+
}
|
|
1159
|
+
const originalMessages = processedParams.messages;
|
|
1160
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1161
|
+
this.processStreamResponse(
|
|
1162
|
+
{
|
|
1163
|
+
...processedParams,
|
|
1164
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1165
|
+
},
|
|
1166
|
+
writable
|
|
1167
|
+
).catch((error) => {
|
|
1168
|
+
console.error("Error processing stream response:", error);
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
} else {
|
|
1173
|
+
setTimeout(() => {
|
|
1174
|
+
writable.close();
|
|
1175
|
+
}, 0);
|
|
1176
|
+
}
|
|
1177
|
+
},
|
|
1178
|
+
lastMessage: void 0
|
|
1179
|
+
}).catch((error) => {
|
|
1180
|
+
console.error("Error processing stream response:", error);
|
|
1181
|
+
});
|
|
1182
|
+
} catch (error) {
|
|
1183
|
+
console.error("Error processing stream response:", error);
|
|
1184
|
+
}
|
|
1185
|
+
return response;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* Gets details about a specific tool available to the agent
|
|
1189
|
+
* @param toolId - ID of the tool to retrieve
|
|
1190
|
+
* @returns Promise containing tool details
|
|
1191
|
+
*/
|
|
1192
|
+
getTool(toolId) {
|
|
1193
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Executes a tool for the agent
|
|
1197
|
+
* @param toolId - ID of the tool to execute
|
|
1198
|
+
* @param params - Parameters required for tool execution
|
|
1199
|
+
* @returns Promise containing the tool execution results
|
|
1200
|
+
*/
|
|
1201
|
+
executeTool(toolId, params) {
|
|
1202
|
+
const body = {
|
|
1203
|
+
data: params.data,
|
|
1204
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
1205
|
+
};
|
|
1206
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
1207
|
+
method: "POST",
|
|
1208
|
+
body
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Retrieves evaluation results for the agent
|
|
1213
|
+
* @returns Promise containing agent evaluations
|
|
1214
|
+
*/
|
|
1215
|
+
evals() {
|
|
1216
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Retrieves live evaluation results for the agent
|
|
1220
|
+
* @returns Promise containing live agent evaluations
|
|
1221
|
+
*/
|
|
1222
|
+
liveEvals() {
|
|
1223
|
+
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Updates the model for the agent
|
|
1227
|
+
* @param params - Parameters for updating the model
|
|
1228
|
+
* @returns Promise containing the updated model
|
|
1229
|
+
*/
|
|
1230
|
+
updateModel(params) {
|
|
1231
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1232
|
+
method: "POST",
|
|
1233
|
+
body: params
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
};
|
|
1237
|
+
var Network = class extends BaseResource {
|
|
1238
|
+
constructor(options, networkId) {
|
|
1239
|
+
super(options);
|
|
1240
|
+
this.networkId = networkId;
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Retrieves details about the network
|
|
1244
|
+
* @returns Promise containing network details
|
|
1245
|
+
*/
|
|
1246
|
+
details() {
|
|
1247
|
+
return this.request(`/api/networks/${this.networkId}`);
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Generates a response from the agent
|
|
1251
|
+
* @param params - Generation parameters including prompt
|
|
1252
|
+
* @returns Promise containing the generated response
|
|
1253
|
+
*/
|
|
1254
|
+
generate(params) {
|
|
1255
|
+
const processedParams = {
|
|
1256
|
+
...params,
|
|
1257
|
+
output: zodToJsonSchema(params.output),
|
|
1258
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
1259
|
+
};
|
|
1260
|
+
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
1261
|
+
method: "POST",
|
|
1262
|
+
body: processedParams
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Streams a response from the agent
|
|
1267
|
+
* @param params - Stream parameters including prompt
|
|
1268
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
1269
|
+
*/
|
|
1270
|
+
async stream(params) {
|
|
1271
|
+
const processedParams = {
|
|
1272
|
+
...params,
|
|
1273
|
+
output: zodToJsonSchema(params.output),
|
|
1274
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
1275
|
+
};
|
|
1276
|
+
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1277
|
+
method: "POST",
|
|
1278
|
+
body: processedParams,
|
|
1279
|
+
stream: true
|
|
1280
|
+
});
|
|
1281
|
+
if (!response.body) {
|
|
1282
|
+
throw new Error("No response body");
|
|
1283
|
+
}
|
|
1284
|
+
response.processDataStream = async (options = {}) => {
|
|
1285
|
+
await processDataStream({
|
|
1286
|
+
stream: response.body,
|
|
1287
|
+
...options
|
|
1288
|
+
});
|
|
1289
|
+
};
|
|
1290
|
+
return response;
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
934
1293
|
|
|
935
1294
|
// src/resources/memory-thread.ts
|
|
936
1295
|
var MemoryThread = class extends BaseResource {
|
|
@@ -978,6 +1337,36 @@ var MemoryThread = class extends BaseResource {
|
|
|
978
1337
|
});
|
|
979
1338
|
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
980
1339
|
}
|
|
1340
|
+
/**
|
|
1341
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
1342
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
1343
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
1344
|
+
*/
|
|
1345
|
+
getMessagesPaginated({
|
|
1346
|
+
selectBy,
|
|
1347
|
+
...rest
|
|
1348
|
+
}) {
|
|
1349
|
+
const query = new URLSearchParams({
|
|
1350
|
+
...rest,
|
|
1351
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
1352
|
+
});
|
|
1353
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
1354
|
+
}
|
|
1355
|
+
/**
|
|
1356
|
+
* Deletes one or more messages from the thread
|
|
1357
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1358
|
+
* message object with id property, or array of message objects
|
|
1359
|
+
* @returns Promise containing deletion result
|
|
1360
|
+
*/
|
|
1361
|
+
deleteMessages(messageIds) {
|
|
1362
|
+
const query = new URLSearchParams({
|
|
1363
|
+
agentId: this.agentId
|
|
1364
|
+
});
|
|
1365
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1366
|
+
method: "POST",
|
|
1367
|
+
body: { messageIds }
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
981
1370
|
};
|
|
982
1371
|
|
|
983
1372
|
// src/resources/vector.ts
|
|
@@ -1228,7 +1617,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
1228
1617
|
};
|
|
1229
1618
|
|
|
1230
1619
|
// src/resources/tool.ts
|
|
1231
|
-
var
|
|
1620
|
+
var Tool = class extends BaseResource {
|
|
1232
1621
|
constructor(options, toolId) {
|
|
1233
1622
|
super(options);
|
|
1234
1623
|
this.toolId = toolId;
|
|
@@ -1399,6 +1788,14 @@ var Workflow = class extends BaseResource {
|
|
|
1399
1788
|
method: "POST"
|
|
1400
1789
|
});
|
|
1401
1790
|
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Creates a new workflow run (alias for createRun)
|
|
1793
|
+
* @param params - Optional object containing the optional runId
|
|
1794
|
+
* @returns Promise containing the runId of the created run
|
|
1795
|
+
*/
|
|
1796
|
+
createRunAsync(params) {
|
|
1797
|
+
return this.createRun(params);
|
|
1798
|
+
}
|
|
1402
1799
|
/**
|
|
1403
1800
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1404
1801
|
* @param params - Object containing the runId, inputData and runtimeContext
|
|
@@ -1443,50 +1840,443 @@ var Workflow = class extends BaseResource {
|
|
|
1443
1840
|
if (!!params?.runId) {
|
|
1444
1841
|
searchParams.set("runId", params.runId);
|
|
1445
1842
|
}
|
|
1446
|
-
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1447
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1448
|
-
method: "POST",
|
|
1449
|
-
body: { inputData: params.inputData, runtimeContext }
|
|
1450
|
-
});
|
|
1843
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1844
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1845
|
+
method: "POST",
|
|
1846
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Starts a workflow run and returns a stream
|
|
1851
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1852
|
+
* @returns Promise containing the workflow execution results
|
|
1853
|
+
*/
|
|
1854
|
+
async stream(params) {
|
|
1855
|
+
const searchParams = new URLSearchParams();
|
|
1856
|
+
if (!!params?.runId) {
|
|
1857
|
+
searchParams.set("runId", params.runId);
|
|
1858
|
+
}
|
|
1859
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1860
|
+
const response = await this.request(
|
|
1861
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1862
|
+
{
|
|
1863
|
+
method: "POST",
|
|
1864
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1865
|
+
stream: true
|
|
1866
|
+
}
|
|
1867
|
+
);
|
|
1868
|
+
if (!response.ok) {
|
|
1869
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1870
|
+
}
|
|
1871
|
+
if (!response.body) {
|
|
1872
|
+
throw new Error("Response body is null");
|
|
1873
|
+
}
|
|
1874
|
+
let failedChunk = void 0;
|
|
1875
|
+
const transformStream = new TransformStream({
|
|
1876
|
+
start() {
|
|
1877
|
+
},
|
|
1878
|
+
async transform(chunk, controller) {
|
|
1879
|
+
try {
|
|
1880
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1881
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1882
|
+
for (const chunk2 of chunks) {
|
|
1883
|
+
if (chunk2) {
|
|
1884
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1885
|
+
try {
|
|
1886
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1887
|
+
controller.enqueue(parsedChunk);
|
|
1888
|
+
failedChunk = void 0;
|
|
1889
|
+
} catch {
|
|
1890
|
+
failedChunk = newChunk;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
} catch {
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
});
|
|
1898
|
+
return response.body.pipeThrough(transformStream);
|
|
1899
|
+
}
|
|
1900
|
+
/**
|
|
1901
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1902
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1903
|
+
* @returns Promise containing the workflow resume results
|
|
1904
|
+
*/
|
|
1905
|
+
resumeAsync(params) {
|
|
1906
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1907
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1908
|
+
method: "POST",
|
|
1909
|
+
body: {
|
|
1910
|
+
step: params.step,
|
|
1911
|
+
resumeData: params.resumeData,
|
|
1912
|
+
runtimeContext
|
|
1913
|
+
}
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Watches workflow transitions in real-time
|
|
1918
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1919
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1920
|
+
*/
|
|
1921
|
+
async watch({ runId }, onRecord) {
|
|
1922
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1923
|
+
stream: true
|
|
1924
|
+
});
|
|
1925
|
+
if (!response.ok) {
|
|
1926
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1927
|
+
}
|
|
1928
|
+
if (!response.body) {
|
|
1929
|
+
throw new Error("Response body is null");
|
|
1930
|
+
}
|
|
1931
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1932
|
+
if (typeof record === "string") {
|
|
1933
|
+
onRecord(JSON.parse(record));
|
|
1934
|
+
} else {
|
|
1935
|
+
onRecord(record);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
1941
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
1942
|
+
*
|
|
1943
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
1944
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
1945
|
+
*/
|
|
1946
|
+
static createRecordStream(records) {
|
|
1947
|
+
const encoder = new TextEncoder();
|
|
1948
|
+
return new ReadableStream({
|
|
1949
|
+
async start(controller) {
|
|
1950
|
+
try {
|
|
1951
|
+
for await (const record of records) {
|
|
1952
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1953
|
+
controller.enqueue(encoder.encode(json));
|
|
1954
|
+
}
|
|
1955
|
+
controller.close();
|
|
1956
|
+
} catch (err) {
|
|
1957
|
+
controller.error(err);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
};
|
|
1963
|
+
|
|
1964
|
+
// src/resources/a2a.ts
|
|
1965
|
+
var A2A = class extends BaseResource {
|
|
1966
|
+
constructor(options, agentId) {
|
|
1967
|
+
super(options);
|
|
1968
|
+
this.agentId = agentId;
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Get the agent card with metadata about the agent
|
|
1972
|
+
* @returns Promise containing the agent card information
|
|
1973
|
+
*/
|
|
1974
|
+
async getCard() {
|
|
1975
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
1976
|
+
}
|
|
1977
|
+
/**
|
|
1978
|
+
* Send a message to the agent and gets a message or task response
|
|
1979
|
+
* @param params - Parameters for the task
|
|
1980
|
+
* @returns Promise containing the response
|
|
1981
|
+
*/
|
|
1982
|
+
async sendMessage(params) {
|
|
1983
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1984
|
+
method: "POST",
|
|
1985
|
+
body: {
|
|
1986
|
+
method: "message/send",
|
|
1987
|
+
params
|
|
1988
|
+
}
|
|
1989
|
+
});
|
|
1990
|
+
return response;
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
1994
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
1995
|
+
* @param params - Parameters for the task
|
|
1996
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
1997
|
+
*/
|
|
1998
|
+
async sendStreamingMessage(params) {
|
|
1999
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2000
|
+
method: "POST",
|
|
2001
|
+
body: {
|
|
2002
|
+
method: "message/stream",
|
|
2003
|
+
params
|
|
2004
|
+
}
|
|
2005
|
+
});
|
|
2006
|
+
return response;
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Get the status and result of a task
|
|
2010
|
+
* @param params - Parameters for querying the task
|
|
2011
|
+
* @returns Promise containing the task response
|
|
2012
|
+
*/
|
|
2013
|
+
async getTask(params) {
|
|
2014
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2015
|
+
method: "POST",
|
|
2016
|
+
body: {
|
|
2017
|
+
method: "tasks/get",
|
|
2018
|
+
params
|
|
2019
|
+
}
|
|
2020
|
+
});
|
|
2021
|
+
return response;
|
|
2022
|
+
}
|
|
2023
|
+
/**
|
|
2024
|
+
* Cancel a running task
|
|
2025
|
+
* @param params - Parameters identifying the task to cancel
|
|
2026
|
+
* @returns Promise containing the task response
|
|
2027
|
+
*/
|
|
2028
|
+
async cancelTask(params) {
|
|
2029
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2030
|
+
method: "POST",
|
|
2031
|
+
body: {
|
|
2032
|
+
method: "tasks/cancel",
|
|
2033
|
+
params
|
|
2034
|
+
}
|
|
2035
|
+
});
|
|
2036
|
+
}
|
|
2037
|
+
};
|
|
2038
|
+
|
|
2039
|
+
// src/resources/mcp-tool.ts
|
|
2040
|
+
var MCPTool = class extends BaseResource {
|
|
2041
|
+
serverId;
|
|
2042
|
+
toolId;
|
|
2043
|
+
constructor(options, serverId, toolId) {
|
|
2044
|
+
super(options);
|
|
2045
|
+
this.serverId = serverId;
|
|
2046
|
+
this.toolId = toolId;
|
|
2047
|
+
}
|
|
2048
|
+
/**
|
|
2049
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2050
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2051
|
+
*/
|
|
2052
|
+
details() {
|
|
2053
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Executes this specific tool on the MCP server.
|
|
2057
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2058
|
+
* @returns Promise containing the result of the tool execution.
|
|
2059
|
+
*/
|
|
2060
|
+
execute(params) {
|
|
2061
|
+
const body = {};
|
|
2062
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2063
|
+
if (params.runtimeContext !== void 0) {
|
|
2064
|
+
body.runtimeContext = params.runtimeContext;
|
|
2065
|
+
}
|
|
2066
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2067
|
+
method: "POST",
|
|
2068
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2069
|
+
});
|
|
2070
|
+
}
|
|
2071
|
+
};
|
|
2072
|
+
|
|
2073
|
+
// src/resources/agent-builder.ts
|
|
2074
|
+
var RECORD_SEPARATOR3 = "";
|
|
2075
|
+
var AgentBuilder = class extends BaseResource {
|
|
2076
|
+
constructor(options, actionId) {
|
|
2077
|
+
super(options);
|
|
2078
|
+
this.actionId = actionId;
|
|
2079
|
+
}
|
|
2080
|
+
// Helper function to transform workflow result to action result
|
|
2081
|
+
transformWorkflowResult(result) {
|
|
2082
|
+
if (result.status === "success") {
|
|
2083
|
+
return {
|
|
2084
|
+
success: result.result.success || false,
|
|
2085
|
+
applied: result.result.applied || false,
|
|
2086
|
+
branchName: result.result.branchName,
|
|
2087
|
+
message: result.result.message || "Agent builder action completed",
|
|
2088
|
+
validationResults: result.result.validationResults,
|
|
2089
|
+
error: result.result.error,
|
|
2090
|
+
errors: result.result.errors,
|
|
2091
|
+
stepResults: result.result.stepResults
|
|
2092
|
+
};
|
|
2093
|
+
} else if (result.status === "failed") {
|
|
2094
|
+
return {
|
|
2095
|
+
success: false,
|
|
2096
|
+
applied: false,
|
|
2097
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2098
|
+
error: result.error.message
|
|
2099
|
+
};
|
|
2100
|
+
} else {
|
|
2101
|
+
return {
|
|
2102
|
+
success: false,
|
|
2103
|
+
applied: false,
|
|
2104
|
+
message: "Agent builder action was suspended",
|
|
2105
|
+
error: "Workflow suspended - manual intervention required"
|
|
2106
|
+
};
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2111
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2112
|
+
*/
|
|
2113
|
+
async createRun(params) {
|
|
2114
|
+
const searchParams = new URLSearchParams();
|
|
2115
|
+
if (!!params?.runId) {
|
|
2116
|
+
searchParams.set("runId", params.runId);
|
|
2117
|
+
}
|
|
2118
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2119
|
+
return this.request(url, {
|
|
2120
|
+
method: "POST"
|
|
2121
|
+
});
|
|
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
|
+
}
|
|
2131
|
+
/**
|
|
2132
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2133
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2134
|
+
*/
|
|
2135
|
+
async startAsync(params, runId) {
|
|
2136
|
+
const searchParams = new URLSearchParams();
|
|
2137
|
+
if (runId) {
|
|
2138
|
+
searchParams.set("runId", runId);
|
|
2139
|
+
}
|
|
2140
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2141
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2142
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2143
|
+
const result = await this.request(url, {
|
|
2144
|
+
method: "POST",
|
|
2145
|
+
body: { ...actionParams, runtimeContext }
|
|
2146
|
+
});
|
|
2147
|
+
return this.transformWorkflowResult(result);
|
|
2148
|
+
}
|
|
2149
|
+
/**
|
|
2150
|
+
* Starts an existing agent builder action run.
|
|
2151
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2152
|
+
*/
|
|
2153
|
+
async startActionRun(params, runId) {
|
|
2154
|
+
const searchParams = new URLSearchParams();
|
|
2155
|
+
searchParams.set("runId", runId);
|
|
2156
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2157
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2158
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2159
|
+
return this.request(url, {
|
|
2160
|
+
method: "POST",
|
|
2161
|
+
body: { ...actionParams, runtimeContext }
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
/**
|
|
2165
|
+
* Resumes a suspended agent builder action step.
|
|
2166
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2167
|
+
*/
|
|
2168
|
+
async resume(params, runId) {
|
|
2169
|
+
const searchParams = new URLSearchParams();
|
|
2170
|
+
searchParams.set("runId", runId);
|
|
2171
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2172
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2173
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2174
|
+
return this.request(url, {
|
|
2175
|
+
method: "POST",
|
|
2176
|
+
body: { ...resumeParams, runtimeContext }
|
|
2177
|
+
});
|
|
2178
|
+
}
|
|
2179
|
+
/**
|
|
2180
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2181
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2182
|
+
*/
|
|
2183
|
+
async resumeAsync(params, runId) {
|
|
2184
|
+
const searchParams = new URLSearchParams();
|
|
2185
|
+
searchParams.set("runId", runId);
|
|
2186
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2187
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2188
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2189
|
+
const result = await this.request(url, {
|
|
2190
|
+
method: "POST",
|
|
2191
|
+
body: { ...resumeParams, runtimeContext }
|
|
2192
|
+
});
|
|
2193
|
+
return this.transformWorkflowResult(result);
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2197
|
+
* separated by the Record Separator character (\x1E)
|
|
2198
|
+
*
|
|
2199
|
+
* @param stream - The readable stream to process
|
|
2200
|
+
* @returns An async generator that yields parsed records
|
|
2201
|
+
*/
|
|
2202
|
+
async *streamProcessor(stream) {
|
|
2203
|
+
const reader = stream.getReader();
|
|
2204
|
+
let doneReading = false;
|
|
2205
|
+
let buffer = "";
|
|
2206
|
+
try {
|
|
2207
|
+
while (!doneReading) {
|
|
2208
|
+
const { done, value } = await reader.read();
|
|
2209
|
+
doneReading = done;
|
|
2210
|
+
if (done && !value) continue;
|
|
2211
|
+
try {
|
|
2212
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2213
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
2214
|
+
buffer = chunks.pop() || "";
|
|
2215
|
+
for (const chunk of chunks) {
|
|
2216
|
+
if (chunk) {
|
|
2217
|
+
if (typeof chunk === "string") {
|
|
2218
|
+
try {
|
|
2219
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2220
|
+
yield parsedChunk;
|
|
2221
|
+
} catch {
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
} catch {
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
if (buffer) {
|
|
2230
|
+
try {
|
|
2231
|
+
yield JSON.parse(buffer);
|
|
2232
|
+
} catch {
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
} finally {
|
|
2236
|
+
reader.cancel().catch(() => {
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
1451
2239
|
}
|
|
1452
2240
|
/**
|
|
1453
|
-
*
|
|
1454
|
-
*
|
|
1455
|
-
* @returns Promise containing the workflow execution results
|
|
2241
|
+
* Streams agent builder action progress in real-time.
|
|
2242
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
1456
2243
|
*/
|
|
1457
|
-
async stream(params) {
|
|
2244
|
+
async stream(params, runId) {
|
|
1458
2245
|
const searchParams = new URLSearchParams();
|
|
1459
|
-
if (
|
|
1460
|
-
searchParams.set("runId",
|
|
2246
|
+
if (runId) {
|
|
2247
|
+
searchParams.set("runId", runId);
|
|
1461
2248
|
}
|
|
1462
2249
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1463
|
-
const
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
);
|
|
2250
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2251
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2252
|
+
const response = await this.request(url, {
|
|
2253
|
+
method: "POST",
|
|
2254
|
+
body: { ...actionParams, runtimeContext },
|
|
2255
|
+
stream: true
|
|
2256
|
+
});
|
|
1471
2257
|
if (!response.ok) {
|
|
1472
|
-
throw new Error(`Failed to stream
|
|
2258
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
1473
2259
|
}
|
|
1474
2260
|
if (!response.body) {
|
|
1475
2261
|
throw new Error("Response body is null");
|
|
1476
2262
|
}
|
|
2263
|
+
let failedChunk = void 0;
|
|
1477
2264
|
const transformStream = new TransformStream({
|
|
1478
2265
|
start() {
|
|
1479
2266
|
},
|
|
1480
2267
|
async transform(chunk, controller) {
|
|
1481
2268
|
try {
|
|
1482
2269
|
const decoded = new TextDecoder().decode(chunk);
|
|
1483
|
-
const chunks = decoded.split(
|
|
2270
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
1484
2271
|
for (const chunk2 of chunks) {
|
|
1485
2272
|
if (chunk2) {
|
|
2273
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1486
2274
|
try {
|
|
1487
|
-
const parsedChunk = JSON.parse(
|
|
2275
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1488
2276
|
controller.enqueue(parsedChunk);
|
|
2277
|
+
failedChunk = void 0;
|
|
1489
2278
|
} catch {
|
|
2279
|
+
failedChunk = newChunk;
|
|
1490
2280
|
}
|
|
1491
2281
|
}
|
|
1492
2282
|
}
|
|
@@ -1497,32 +2287,68 @@ var Workflow = class extends BaseResource {
|
|
|
1497
2287
|
return response.body.pipeThrough(transformStream);
|
|
1498
2288
|
}
|
|
1499
2289
|
/**
|
|
1500
|
-
*
|
|
1501
|
-
*
|
|
1502
|
-
* @returns Promise containing the workflow resume results
|
|
2290
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2291
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
1503
2292
|
*/
|
|
1504
|
-
|
|
2293
|
+
async streamVNext(params, runId) {
|
|
2294
|
+
const searchParams = new URLSearchParams();
|
|
2295
|
+
if (runId) {
|
|
2296
|
+
searchParams.set("runId", runId);
|
|
2297
|
+
}
|
|
1505
2298
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1506
|
-
|
|
2299
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2300
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2301
|
+
const response = await this.request(url, {
|
|
1507
2302
|
method: "POST",
|
|
1508
|
-
body: {
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
2303
|
+
body: { ...actionParams, runtimeContext },
|
|
2304
|
+
stream: true
|
|
2305
|
+
});
|
|
2306
|
+
if (!response.ok) {
|
|
2307
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2308
|
+
}
|
|
2309
|
+
if (!response.body) {
|
|
2310
|
+
throw new Error("Response body is null");
|
|
2311
|
+
}
|
|
2312
|
+
let failedChunk = void 0;
|
|
2313
|
+
const transformStream = new TransformStream({
|
|
2314
|
+
start() {
|
|
2315
|
+
},
|
|
2316
|
+
async transform(chunk, controller) {
|
|
2317
|
+
try {
|
|
2318
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2319
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2320
|
+
for (const chunk2 of chunks) {
|
|
2321
|
+
if (chunk2) {
|
|
2322
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2323
|
+
try {
|
|
2324
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2325
|
+
controller.enqueue(parsedChunk);
|
|
2326
|
+
failedChunk = void 0;
|
|
2327
|
+
} catch {
|
|
2328
|
+
failedChunk = newChunk;
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
} catch {
|
|
2333
|
+
}
|
|
1512
2334
|
}
|
|
1513
2335
|
});
|
|
2336
|
+
return response.body.pipeThrough(transformStream);
|
|
1514
2337
|
}
|
|
1515
2338
|
/**
|
|
1516
|
-
* Watches
|
|
1517
|
-
*
|
|
1518
|
-
*
|
|
2339
|
+
* Watches an existing agent builder action run by runId.
|
|
2340
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2341
|
+
* and streams any remaining progress.
|
|
2342
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
1519
2343
|
*/
|
|
1520
|
-
async watch({ runId }, onRecord) {
|
|
1521
|
-
const
|
|
2344
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2345
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2346
|
+
const response = await this.request(url, {
|
|
2347
|
+
method: "GET",
|
|
1522
2348
|
stream: true
|
|
1523
2349
|
});
|
|
1524
2350
|
if (!response.ok) {
|
|
1525
|
-
throw new Error(`Failed to watch
|
|
2351
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
1526
2352
|
}
|
|
1527
2353
|
if (!response.body) {
|
|
1528
2354
|
throw new Error("Response body is null");
|
|
@@ -1536,135 +2362,126 @@ var Workflow = class extends BaseResource {
|
|
|
1536
2362
|
}
|
|
1537
2363
|
}
|
|
1538
2364
|
/**
|
|
1539
|
-
*
|
|
1540
|
-
*
|
|
1541
|
-
*
|
|
1542
|
-
* @param records - An iterable or async iterable of objects to stream
|
|
1543
|
-
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
2365
|
+
* Gets a specific action run by its ID.
|
|
2366
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
1544
2367
|
*/
|
|
1545
|
-
|
|
1546
|
-
const
|
|
1547
|
-
return
|
|
1548
|
-
|
|
1549
|
-
try {
|
|
1550
|
-
for await (const record of records) {
|
|
1551
|
-
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1552
|
-
controller.enqueue(encoder.encode(json));
|
|
1553
|
-
}
|
|
1554
|
-
controller.close();
|
|
1555
|
-
} catch (err) {
|
|
1556
|
-
controller.error(err);
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
2368
|
+
async runById(runId) {
|
|
2369
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2370
|
+
return this.request(url, {
|
|
2371
|
+
method: "GET"
|
|
1559
2372
|
});
|
|
1560
2373
|
}
|
|
1561
|
-
};
|
|
1562
|
-
|
|
1563
|
-
// src/resources/a2a.ts
|
|
1564
|
-
var A2A = class extends BaseResource {
|
|
1565
|
-
constructor(options, agentId) {
|
|
1566
|
-
super(options);
|
|
1567
|
-
this.agentId = agentId;
|
|
1568
|
-
}
|
|
1569
2374
|
/**
|
|
1570
|
-
*
|
|
1571
|
-
*
|
|
2375
|
+
* Gets details about this agent builder action.
|
|
2376
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
1572
2377
|
*/
|
|
1573
|
-
async
|
|
1574
|
-
|
|
2378
|
+
async details() {
|
|
2379
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2380
|
+
return result;
|
|
1575
2381
|
}
|
|
1576
2382
|
/**
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1579
|
-
* @returns Promise containing the task response
|
|
2383
|
+
* Gets all runs for this agent builder action.
|
|
2384
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
1580
2385
|
*/
|
|
1581
|
-
async
|
|
1582
|
-
const
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
2386
|
+
async runs(params) {
|
|
2387
|
+
const searchParams = new URLSearchParams();
|
|
2388
|
+
if (params?.fromDate) {
|
|
2389
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2390
|
+
}
|
|
2391
|
+
if (params?.toDate) {
|
|
2392
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2393
|
+
}
|
|
2394
|
+
if (params?.limit !== void 0) {
|
|
2395
|
+
searchParams.set("limit", String(params.limit));
|
|
2396
|
+
}
|
|
2397
|
+
if (params?.offset !== void 0) {
|
|
2398
|
+
searchParams.set("offset", String(params.offset));
|
|
2399
|
+
}
|
|
2400
|
+
if (params?.resourceId) {
|
|
2401
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2402
|
+
}
|
|
2403
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2404
|
+
return this.request(url, {
|
|
2405
|
+
method: "GET"
|
|
1588
2406
|
});
|
|
1589
|
-
return { task: response.result };
|
|
1590
2407
|
}
|
|
1591
2408
|
/**
|
|
1592
|
-
*
|
|
1593
|
-
*
|
|
1594
|
-
* @returns Promise containing the task response
|
|
2409
|
+
* Gets the execution result of an agent builder action run.
|
|
2410
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
1595
2411
|
*/
|
|
1596
|
-
async
|
|
1597
|
-
const
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
method: "tasks/get",
|
|
1601
|
-
params
|
|
1602
|
-
}
|
|
2412
|
+
async runExecutionResult(runId) {
|
|
2413
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2414
|
+
return this.request(url, {
|
|
2415
|
+
method: "GET"
|
|
1603
2416
|
});
|
|
1604
|
-
return response.result;
|
|
1605
2417
|
}
|
|
1606
2418
|
/**
|
|
1607
|
-
*
|
|
1608
|
-
*
|
|
1609
|
-
* @returns Promise containing the task response
|
|
2419
|
+
* Cancels an agent builder action run.
|
|
2420
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
1610
2421
|
*/
|
|
1611
|
-
async
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
method: "tasks/cancel",
|
|
1616
|
-
params
|
|
1617
|
-
}
|
|
2422
|
+
async cancelRun(runId) {
|
|
2423
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2424
|
+
return this.request(url, {
|
|
2425
|
+
method: "POST"
|
|
1618
2426
|
});
|
|
1619
2427
|
}
|
|
1620
2428
|
/**
|
|
1621
|
-
*
|
|
1622
|
-
*
|
|
1623
|
-
* @returns Promise containing the task response
|
|
2429
|
+
* Sends an event to an agent builder action run.
|
|
2430
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
1624
2431
|
*/
|
|
1625
|
-
async
|
|
1626
|
-
|
|
2432
|
+
async sendRunEvent(params) {
|
|
2433
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2434
|
+
return this.request(url, {
|
|
1627
2435
|
method: "POST",
|
|
1628
|
-
body: {
|
|
1629
|
-
method: "tasks/sendSubscribe",
|
|
1630
|
-
params
|
|
1631
|
-
},
|
|
1632
|
-
stream: true
|
|
2436
|
+
body: { event: params.event, data: params.data }
|
|
1633
2437
|
});
|
|
1634
2438
|
}
|
|
1635
2439
|
};
|
|
1636
2440
|
|
|
1637
|
-
// src/resources/
|
|
1638
|
-
var
|
|
1639
|
-
|
|
1640
|
-
toolId;
|
|
1641
|
-
constructor(options, serverId, toolId) {
|
|
2441
|
+
// src/resources/observability.ts
|
|
2442
|
+
var Observability = class extends BaseResource {
|
|
2443
|
+
constructor(options) {
|
|
1642
2444
|
super(options);
|
|
1643
|
-
this.serverId = serverId;
|
|
1644
|
-
this.toolId = toolId;
|
|
1645
2445
|
}
|
|
1646
2446
|
/**
|
|
1647
|
-
* Retrieves
|
|
1648
|
-
* @
|
|
2447
|
+
* Retrieves a specific AI trace by ID
|
|
2448
|
+
* @param traceId - ID of the trace to retrieve
|
|
2449
|
+
* @returns Promise containing the AI trace with all its spans
|
|
1649
2450
|
*/
|
|
1650
|
-
|
|
1651
|
-
return this.request(`/api/
|
|
2451
|
+
getTrace(traceId) {
|
|
2452
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
1652
2453
|
}
|
|
1653
2454
|
/**
|
|
1654
|
-
*
|
|
1655
|
-
* @param params - Parameters for
|
|
1656
|
-
* @returns Promise containing
|
|
2455
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2456
|
+
* @param params - Parameters for pagination and filtering
|
|
2457
|
+
* @returns Promise containing paginated traces and pagination info
|
|
1657
2458
|
*/
|
|
1658
|
-
|
|
1659
|
-
const
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
2459
|
+
getTraces(params) {
|
|
2460
|
+
const { pagination, filters } = params;
|
|
2461
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2462
|
+
const { name, spanType } = filters || {};
|
|
2463
|
+
const searchParams = new URLSearchParams();
|
|
2464
|
+
if (page !== void 0) {
|
|
2465
|
+
searchParams.set("page", String(page));
|
|
1663
2466
|
}
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
2467
|
+
if (perPage !== void 0) {
|
|
2468
|
+
searchParams.set("perPage", String(perPage));
|
|
2469
|
+
}
|
|
2470
|
+
if (name) {
|
|
2471
|
+
searchParams.set("name", name);
|
|
2472
|
+
}
|
|
2473
|
+
if (spanType !== void 0) {
|
|
2474
|
+
searchParams.set("spanType", String(spanType));
|
|
2475
|
+
}
|
|
2476
|
+
if (dateRange) {
|
|
2477
|
+
const dateRangeStr = JSON.stringify({
|
|
2478
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2479
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2480
|
+
});
|
|
2481
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2482
|
+
}
|
|
2483
|
+
const queryString = searchParams.toString();
|
|
2484
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
1668
2485
|
}
|
|
1669
2486
|
};
|
|
1670
2487
|
|
|
@@ -1714,10 +2531,25 @@ var NetworkMemoryThread = class extends BaseResource {
|
|
|
1714
2531
|
});
|
|
1715
2532
|
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1716
2533
|
}
|
|
2534
|
+
/**
|
|
2535
|
+
* Deletes one or more messages from the thread
|
|
2536
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
2537
|
+
* message object with id property, or array of message objects
|
|
2538
|
+
* @returns Promise containing deletion result
|
|
2539
|
+
*/
|
|
2540
|
+
deleteMessages(messageIds) {
|
|
2541
|
+
const query = new URLSearchParams({
|
|
2542
|
+
networkId: this.networkId
|
|
2543
|
+
});
|
|
2544
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
2545
|
+
method: "POST",
|
|
2546
|
+
body: { messageIds }
|
|
2547
|
+
});
|
|
2548
|
+
}
|
|
1717
2549
|
};
|
|
1718
2550
|
|
|
1719
2551
|
// src/resources/vNextNetwork.ts
|
|
1720
|
-
var
|
|
2552
|
+
var RECORD_SEPARATOR4 = "";
|
|
1721
2553
|
var VNextNetwork = class extends BaseResource {
|
|
1722
2554
|
constructor(options, networkId) {
|
|
1723
2555
|
super(options);
|
|
@@ -1769,7 +2601,7 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1769
2601
|
if (done && !value) continue;
|
|
1770
2602
|
try {
|
|
1771
2603
|
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1772
|
-
const chunks = (buffer + decoded).split(
|
|
2604
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR4);
|
|
1773
2605
|
buffer = chunks.pop() || "";
|
|
1774
2606
|
for (const chunk of chunks) {
|
|
1775
2607
|
if (chunk) {
|
|
@@ -1856,8 +2688,10 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1856
2688
|
|
|
1857
2689
|
// src/client.ts
|
|
1858
2690
|
var MastraClient = class extends BaseResource {
|
|
2691
|
+
observability;
|
|
1859
2692
|
constructor(options) {
|
|
1860
2693
|
super(options);
|
|
2694
|
+
this.observability = new Observability(options);
|
|
1861
2695
|
}
|
|
1862
2696
|
/**
|
|
1863
2697
|
* Retrieves all available agents
|
|
@@ -1866,21 +2700,6 @@ var MastraClient = class extends BaseResource {
|
|
|
1866
2700
|
getAgents() {
|
|
1867
2701
|
return this.request("/api/agents");
|
|
1868
2702
|
}
|
|
1869
|
-
async getAGUI({ resourceId }) {
|
|
1870
|
-
const agents = await this.getAgents();
|
|
1871
|
-
return Object.entries(agents).reduce(
|
|
1872
|
-
(acc, [agentId]) => {
|
|
1873
|
-
const agent = this.getAgent(agentId);
|
|
1874
|
-
acc[agentId] = new AGUIAdapter({
|
|
1875
|
-
agentId,
|
|
1876
|
-
agent,
|
|
1877
|
-
resourceId
|
|
1878
|
-
});
|
|
1879
|
-
return acc;
|
|
1880
|
-
},
|
|
1881
|
-
{}
|
|
1882
|
-
);
|
|
1883
|
-
}
|
|
1884
2703
|
/**
|
|
1885
2704
|
* Gets an agent instance by ID
|
|
1886
2705
|
* @param agentId - ID of the agent to retrieve
|
|
@@ -1986,7 +2805,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1986
2805
|
* @returns Tool instance
|
|
1987
2806
|
*/
|
|
1988
2807
|
getTool(toolId) {
|
|
1989
|
-
return new
|
|
2808
|
+
return new Tool(this.options, toolId);
|
|
1990
2809
|
}
|
|
1991
2810
|
/**
|
|
1992
2811
|
* Retrieves all available legacy workflows
|
|
@@ -2018,6 +2837,20 @@ var MastraClient = class extends BaseResource {
|
|
|
2018
2837
|
getWorkflow(workflowId) {
|
|
2019
2838
|
return new Workflow(this.options, workflowId);
|
|
2020
2839
|
}
|
|
2840
|
+
/**
|
|
2841
|
+
* Gets all available agent builder actions
|
|
2842
|
+
* @returns Promise containing map of action IDs to action details
|
|
2843
|
+
*/
|
|
2844
|
+
getAgentBuilderActions() {
|
|
2845
|
+
return this.request("/api/agent-builder/");
|
|
2846
|
+
}
|
|
2847
|
+
/**
|
|
2848
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
2849
|
+
* @returns AgentBuilder instance
|
|
2850
|
+
*/
|
|
2851
|
+
getAgentBuilderAction(actionId) {
|
|
2852
|
+
return new AgentBuilder(this.options, actionId);
|
|
2853
|
+
}
|
|
2021
2854
|
/**
|
|
2022
2855
|
* Gets a vector instance by name
|
|
2023
2856
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -2283,6 +3116,99 @@ var MastraClient = class extends BaseResource {
|
|
|
2283
3116
|
}
|
|
2284
3117
|
});
|
|
2285
3118
|
}
|
|
3119
|
+
/**
|
|
3120
|
+
* Retrieves all available scorers
|
|
3121
|
+
* @returns Promise containing list of available scorers
|
|
3122
|
+
*/
|
|
3123
|
+
getScorers() {
|
|
3124
|
+
return this.request("/api/scores/scorers");
|
|
3125
|
+
}
|
|
3126
|
+
/**
|
|
3127
|
+
* Retrieves a scorer by ID
|
|
3128
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
3129
|
+
* @returns Promise containing the scorer
|
|
3130
|
+
*/
|
|
3131
|
+
getScorer(scorerId) {
|
|
3132
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3133
|
+
}
|
|
3134
|
+
getScoresByScorerId(params) {
|
|
3135
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
3136
|
+
const searchParams = new URLSearchParams();
|
|
3137
|
+
if (entityId) {
|
|
3138
|
+
searchParams.set("entityId", entityId);
|
|
3139
|
+
}
|
|
3140
|
+
if (entityType) {
|
|
3141
|
+
searchParams.set("entityType", entityType);
|
|
3142
|
+
}
|
|
3143
|
+
if (page !== void 0) {
|
|
3144
|
+
searchParams.set("page", String(page));
|
|
3145
|
+
}
|
|
3146
|
+
if (perPage !== void 0) {
|
|
3147
|
+
searchParams.set("perPage", String(perPage));
|
|
3148
|
+
}
|
|
3149
|
+
const queryString = searchParams.toString();
|
|
3150
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3151
|
+
}
|
|
3152
|
+
/**
|
|
3153
|
+
* Retrieves scores by run ID
|
|
3154
|
+
* @param params - Parameters containing run ID and pagination options
|
|
3155
|
+
* @returns Promise containing scores and pagination info
|
|
3156
|
+
*/
|
|
3157
|
+
getScoresByRunId(params) {
|
|
3158
|
+
const { runId, page, perPage } = params;
|
|
3159
|
+
const searchParams = new URLSearchParams();
|
|
3160
|
+
if (page !== void 0) {
|
|
3161
|
+
searchParams.set("page", String(page));
|
|
3162
|
+
}
|
|
3163
|
+
if (perPage !== void 0) {
|
|
3164
|
+
searchParams.set("perPage", String(perPage));
|
|
3165
|
+
}
|
|
3166
|
+
const queryString = searchParams.toString();
|
|
3167
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3168
|
+
}
|
|
3169
|
+
/**
|
|
3170
|
+
* Retrieves scores by entity ID and type
|
|
3171
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
3172
|
+
* @returns Promise containing scores and pagination info
|
|
3173
|
+
*/
|
|
3174
|
+
getScoresByEntityId(params) {
|
|
3175
|
+
const { entityId, entityType, page, perPage } = params;
|
|
3176
|
+
const searchParams = new URLSearchParams();
|
|
3177
|
+
if (page !== void 0) {
|
|
3178
|
+
searchParams.set("page", String(page));
|
|
3179
|
+
}
|
|
3180
|
+
if (perPage !== void 0) {
|
|
3181
|
+
searchParams.set("perPage", String(perPage));
|
|
3182
|
+
}
|
|
3183
|
+
const queryString = searchParams.toString();
|
|
3184
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
3185
|
+
}
|
|
3186
|
+
/**
|
|
3187
|
+
* Saves a score
|
|
3188
|
+
* @param params - Parameters containing the score data to save
|
|
3189
|
+
* @returns Promise containing the saved score
|
|
3190
|
+
*/
|
|
3191
|
+
saveScore(params) {
|
|
3192
|
+
return this.request("/api/scores", {
|
|
3193
|
+
method: "POST",
|
|
3194
|
+
body: params
|
|
3195
|
+
});
|
|
3196
|
+
}
|
|
3197
|
+
/**
|
|
3198
|
+
* Retrieves model providers with available keys
|
|
3199
|
+
* @returns Promise containing model providers with available keys
|
|
3200
|
+
*/
|
|
3201
|
+
getModelProviders() {
|
|
3202
|
+
return this.request(`/api/model-providers`);
|
|
3203
|
+
}
|
|
3204
|
+
getAITrace(traceId) {
|
|
3205
|
+
return this.observability.getTrace(traceId);
|
|
3206
|
+
}
|
|
3207
|
+
getAITraces(params) {
|
|
3208
|
+
return this.observability.getTraces(params);
|
|
3209
|
+
}
|
|
2286
3210
|
};
|
|
2287
3211
|
|
|
2288
3212
|
export { MastraClient };
|
|
3213
|
+
//# sourceMappingURL=index.js.map
|
|
3214
|
+
//# sourceMappingURL=index.js.map
|