@mastra/client-js 0.0.0-share-agent-metadata-with-cloud-20250718123411 → 0.0.0-stream-vnext-usage-20250908171242
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 +781 -2
- 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 +1688 -707
- 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 +1688 -707
- 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 +169 -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 -836
- 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 -401
- 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.cjs
CHANGED
|
@@ -1,216 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var client = require('@ag-ui/client');
|
|
4
|
-
var rxjs = require('rxjs');
|
|
5
3
|
var uiUtils = require('@ai-sdk/ui-utils');
|
|
6
|
-
var zod = require('zod');
|
|
7
|
-
var originalZodToJsonSchema = require('zod-to-json-schema');
|
|
8
|
-
var tools = require('@mastra/core/tools');
|
|
9
4
|
var uuid = require('@lukeed/uuid');
|
|
10
5
|
var runtimeContext = require('@mastra/core/runtime-context');
|
|
6
|
+
var isVercelTool = require('@mastra/core/tools/is-vercel-tool');
|
|
7
|
+
var zod = require('zod');
|
|
8
|
+
var originalZodToJsonSchema = require('zod-to-json-schema');
|
|
11
9
|
|
|
12
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
11
|
|
|
14
12
|
var originalZodToJsonSchema__default = /*#__PURE__*/_interopDefault(originalZodToJsonSchema);
|
|
15
13
|
|
|
16
|
-
// src/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
super({
|
|
22
|
-
agentId,
|
|
23
|
-
...rest
|
|
24
|
-
});
|
|
25
|
-
this.agent = agent;
|
|
26
|
-
this.resourceId = resourceId;
|
|
27
|
-
}
|
|
28
|
-
run(input) {
|
|
29
|
-
return new rxjs.Observable((subscriber) => {
|
|
30
|
-
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
31
|
-
subscriber.next({
|
|
32
|
-
type: client.EventType.RUN_STARTED,
|
|
33
|
-
threadId: input.threadId,
|
|
34
|
-
runId: input.runId
|
|
35
|
-
});
|
|
36
|
-
this.agent.stream({
|
|
37
|
-
threadId: input.threadId,
|
|
38
|
-
resourceId: this.resourceId ?? "",
|
|
39
|
-
runId: input.runId,
|
|
40
|
-
messages: convertedMessages,
|
|
41
|
-
clientTools: input.tools.reduce(
|
|
42
|
-
(acc, tool) => {
|
|
43
|
-
acc[tool.name] = {
|
|
44
|
-
id: tool.name,
|
|
45
|
-
description: tool.description,
|
|
46
|
-
inputSchema: tool.parameters
|
|
47
|
-
};
|
|
48
|
-
return acc;
|
|
49
|
-
},
|
|
50
|
-
{}
|
|
51
|
-
)
|
|
52
|
-
}).then((response) => {
|
|
53
|
-
let currentMessageId = void 0;
|
|
54
|
-
let isInTextMessage = false;
|
|
55
|
-
return response.processDataStream({
|
|
56
|
-
onTextPart: (text) => {
|
|
57
|
-
if (currentMessageId === void 0) {
|
|
58
|
-
currentMessageId = generateUUID();
|
|
59
|
-
const message2 = {
|
|
60
|
-
type: client.EventType.TEXT_MESSAGE_START,
|
|
61
|
-
messageId: currentMessageId,
|
|
62
|
-
role: "assistant"
|
|
63
|
-
};
|
|
64
|
-
subscriber.next(message2);
|
|
65
|
-
isInTextMessage = true;
|
|
66
|
-
}
|
|
67
|
-
const message = {
|
|
68
|
-
type: client.EventType.TEXT_MESSAGE_CONTENT,
|
|
69
|
-
messageId: currentMessageId,
|
|
70
|
-
delta: text
|
|
71
|
-
};
|
|
72
|
-
subscriber.next(message);
|
|
73
|
-
},
|
|
74
|
-
onFinishMessagePart: () => {
|
|
75
|
-
if (currentMessageId !== void 0) {
|
|
76
|
-
const message = {
|
|
77
|
-
type: client.EventType.TEXT_MESSAGE_END,
|
|
78
|
-
messageId: currentMessageId
|
|
79
|
-
};
|
|
80
|
-
subscriber.next(message);
|
|
81
|
-
isInTextMessage = false;
|
|
82
|
-
}
|
|
83
|
-
subscriber.next({
|
|
84
|
-
type: client.EventType.RUN_FINISHED,
|
|
85
|
-
threadId: input.threadId,
|
|
86
|
-
runId: input.runId
|
|
87
|
-
});
|
|
88
|
-
subscriber.complete();
|
|
89
|
-
},
|
|
90
|
-
onToolCallPart(streamPart) {
|
|
91
|
-
const parentMessageId = currentMessageId || generateUUID();
|
|
92
|
-
if (isInTextMessage) {
|
|
93
|
-
const message = {
|
|
94
|
-
type: client.EventType.TEXT_MESSAGE_END,
|
|
95
|
-
messageId: parentMessageId
|
|
96
|
-
};
|
|
97
|
-
subscriber.next(message);
|
|
98
|
-
isInTextMessage = false;
|
|
99
|
-
}
|
|
100
|
-
subscriber.next({
|
|
101
|
-
type: client.EventType.TOOL_CALL_START,
|
|
102
|
-
toolCallId: streamPart.toolCallId,
|
|
103
|
-
toolCallName: streamPart.toolName,
|
|
104
|
-
parentMessageId
|
|
105
|
-
});
|
|
106
|
-
subscriber.next({
|
|
107
|
-
type: client.EventType.TOOL_CALL_ARGS,
|
|
108
|
-
toolCallId: streamPart.toolCallId,
|
|
109
|
-
delta: JSON.stringify(streamPart.args),
|
|
110
|
-
parentMessageId
|
|
111
|
-
});
|
|
112
|
-
subscriber.next({
|
|
113
|
-
type: client.EventType.TOOL_CALL_END,
|
|
114
|
-
toolCallId: streamPart.toolCallId,
|
|
115
|
-
parentMessageId
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}).catch((error) => {
|
|
120
|
-
console.error("error", error);
|
|
121
|
-
subscriber.error(error);
|
|
122
|
-
});
|
|
123
|
-
return () => {
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
function generateUUID() {
|
|
129
|
-
if (typeof crypto !== "undefined") {
|
|
130
|
-
if (typeof crypto.randomUUID === "function") {
|
|
131
|
-
return crypto.randomUUID();
|
|
132
|
-
}
|
|
133
|
-
if (typeof crypto.getRandomValues === "function") {
|
|
134
|
-
const buffer = new Uint8Array(16);
|
|
135
|
-
crypto.getRandomValues(buffer);
|
|
136
|
-
buffer[6] = buffer[6] & 15 | 64;
|
|
137
|
-
buffer[8] = buffer[8] & 63 | 128;
|
|
138
|
-
let hex = "";
|
|
139
|
-
for (let i = 0; i < 16; i++) {
|
|
140
|
-
hex += buffer[i].toString(16).padStart(2, "0");
|
|
141
|
-
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
142
|
-
}
|
|
143
|
-
return hex;
|
|
14
|
+
// src/resources/agent.ts
|
|
15
|
+
function parseClientRuntimeContext(runtimeContext$1) {
|
|
16
|
+
if (runtimeContext$1) {
|
|
17
|
+
if (runtimeContext$1 instanceof runtimeContext.RuntimeContext) {
|
|
18
|
+
return Object.fromEntries(runtimeContext$1.entries());
|
|
144
19
|
}
|
|
20
|
+
return runtimeContext$1;
|
|
145
21
|
}
|
|
146
|
-
return
|
|
147
|
-
const r = Math.random() * 16 | 0;
|
|
148
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
149
|
-
return v.toString(16);
|
|
150
|
-
});
|
|
22
|
+
return void 0;
|
|
151
23
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
154
|
-
for (const message of messages) {
|
|
155
|
-
if (message.role === "assistant") {
|
|
156
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
157
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
158
|
-
parts.push({
|
|
159
|
-
type: "tool-call",
|
|
160
|
-
toolCallId: toolCall.id,
|
|
161
|
-
toolName: toolCall.function.name,
|
|
162
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
result.push({
|
|
166
|
-
role: "assistant",
|
|
167
|
-
content: parts
|
|
168
|
-
});
|
|
169
|
-
if (message.toolCalls?.length) {
|
|
170
|
-
result.push({
|
|
171
|
-
role: "tool",
|
|
172
|
-
content: message.toolCalls.map((toolCall) => ({
|
|
173
|
-
type: "tool-result",
|
|
174
|
-
toolCallId: toolCall.id,
|
|
175
|
-
toolName: toolCall.function.name,
|
|
176
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
177
|
-
}))
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
} else if (message.role === "user") {
|
|
181
|
-
result.push({
|
|
182
|
-
role: "user",
|
|
183
|
-
content: message.content || ""
|
|
184
|
-
});
|
|
185
|
-
} else if (message.role === "tool") {
|
|
186
|
-
result.push({
|
|
187
|
-
role: "tool",
|
|
188
|
-
content: [
|
|
189
|
-
{
|
|
190
|
-
type: "tool-result",
|
|
191
|
-
toolCallId: message.toolCallId,
|
|
192
|
-
toolName: "unknown",
|
|
193
|
-
result: message.content
|
|
194
|
-
}
|
|
195
|
-
]
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return result;
|
|
24
|
+
function isZodType(value) {
|
|
25
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
200
26
|
}
|
|
201
27
|
function zodToJsonSchema(zodSchema) {
|
|
202
|
-
if (!(zodSchema
|
|
28
|
+
if (!isZodType(zodSchema)) {
|
|
203
29
|
return zodSchema;
|
|
204
30
|
}
|
|
31
|
+
if ("toJSONSchema" in zod.z) {
|
|
32
|
+
const fn = "toJSONSchema";
|
|
33
|
+
return zod.z[fn].call(zod.z, zodSchema);
|
|
34
|
+
}
|
|
205
35
|
return originalZodToJsonSchema__default.default(zodSchema, { $refStrategy: "none" });
|
|
206
36
|
}
|
|
37
|
+
|
|
38
|
+
// src/utils/process-client-tools.ts
|
|
207
39
|
function processClientTools(clientTools) {
|
|
208
40
|
if (!clientTools) {
|
|
209
41
|
return void 0;
|
|
210
42
|
}
|
|
211
43
|
return Object.fromEntries(
|
|
212
44
|
Object.entries(clientTools).map(([key, value]) => {
|
|
213
|
-
if (
|
|
45
|
+
if (isVercelTool.isVercelTool(value)) {
|
|
214
46
|
return [
|
|
215
47
|
key,
|
|
216
48
|
{
|
|
@@ -232,6 +64,42 @@ function processClientTools(clientTools) {
|
|
|
232
64
|
);
|
|
233
65
|
}
|
|
234
66
|
|
|
67
|
+
// src/utils/process-mastra-stream.ts
|
|
68
|
+
async function processMastraStream({
|
|
69
|
+
stream,
|
|
70
|
+
onChunk
|
|
71
|
+
}) {
|
|
72
|
+
const reader = stream.getReader();
|
|
73
|
+
const decoder = new TextDecoder();
|
|
74
|
+
let buffer = "";
|
|
75
|
+
try {
|
|
76
|
+
while (true) {
|
|
77
|
+
const { done, value } = await reader.read();
|
|
78
|
+
if (done) break;
|
|
79
|
+
buffer += decoder.decode(value, { stream: true });
|
|
80
|
+
const lines = buffer.split("\n\n");
|
|
81
|
+
buffer = lines.pop() || "";
|
|
82
|
+
for (const line of lines) {
|
|
83
|
+
if (line.startsWith("data: ")) {
|
|
84
|
+
const data = line.slice(6);
|
|
85
|
+
if (data === "[DONE]") {
|
|
86
|
+
console.log("\u{1F3C1} Stream finished");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const json = JSON.parse(data);
|
|
91
|
+
await onChunk(json);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} finally {
|
|
99
|
+
reader.releaseLock();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
235
103
|
// src/resources/base.ts
|
|
236
104
|
var BaseResource = class {
|
|
237
105
|
options;
|
|
@@ -246,20 +114,21 @@ var BaseResource = class {
|
|
|
246
114
|
*/
|
|
247
115
|
async request(path, options = {}) {
|
|
248
116
|
let lastError = null;
|
|
249
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
117
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
250
118
|
let delay = backoffMs;
|
|
251
119
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
252
120
|
try {
|
|
253
121
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
254
122
|
...options,
|
|
255
123
|
headers: {
|
|
256
|
-
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
124
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
257
125
|
...headers,
|
|
258
126
|
...options.headers
|
|
259
127
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
260
128
|
// 'x-mastra-client-type': 'js',
|
|
261
129
|
},
|
|
262
130
|
signal: this.options.abortSignal,
|
|
131
|
+
credentials: options.credentials ?? credentials,
|
|
263
132
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
264
133
|
});
|
|
265
134
|
if (!response.ok) {
|
|
@@ -292,14 +161,64 @@ var BaseResource = class {
|
|
|
292
161
|
throw lastError || new Error("Request failed");
|
|
293
162
|
}
|
|
294
163
|
};
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
164
|
+
|
|
165
|
+
// src/resources/agent.ts
|
|
166
|
+
async function executeToolCallAndRespond({
|
|
167
|
+
response,
|
|
168
|
+
params,
|
|
169
|
+
runId,
|
|
170
|
+
resourceId,
|
|
171
|
+
threadId,
|
|
172
|
+
runtimeContext,
|
|
173
|
+
respondFn
|
|
174
|
+
}) {
|
|
175
|
+
if (response.finishReason === "tool-calls") {
|
|
176
|
+
const toolCalls = response.toolCalls;
|
|
177
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
178
|
+
return response;
|
|
179
|
+
}
|
|
180
|
+
for (const toolCall of toolCalls) {
|
|
181
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
182
|
+
if (clientTool && clientTool.execute) {
|
|
183
|
+
const result = await clientTool.execute(
|
|
184
|
+
{
|
|
185
|
+
context: toolCall?.args,
|
|
186
|
+
runId,
|
|
187
|
+
resourceId,
|
|
188
|
+
threadId,
|
|
189
|
+
runtimeContext,
|
|
190
|
+
tracingContext: { currentSpan: void 0 }
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
messages: response.messages,
|
|
194
|
+
toolCallId: toolCall?.toolCallId
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
const updatedMessages = [
|
|
198
|
+
{
|
|
199
|
+
role: "user",
|
|
200
|
+
content: params.messages
|
|
201
|
+
},
|
|
202
|
+
...response.response.messages,
|
|
203
|
+
{
|
|
204
|
+
role: "tool",
|
|
205
|
+
content: [
|
|
206
|
+
{
|
|
207
|
+
type: "tool-result",
|
|
208
|
+
toolCallId: toolCall.toolCallId,
|
|
209
|
+
toolName: toolCall.toolName,
|
|
210
|
+
result
|
|
211
|
+
}
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
];
|
|
215
|
+
return respondFn({
|
|
216
|
+
...params,
|
|
217
|
+
messages: updatedMessages
|
|
218
|
+
});
|
|
219
|
+
}
|
|
299
220
|
}
|
|
300
|
-
return runtimeContext$1;
|
|
301
221
|
}
|
|
302
|
-
return void 0;
|
|
303
222
|
}
|
|
304
223
|
var AgentVoice = class extends BaseResource {
|
|
305
224
|
constructor(options, agentId) {
|
|
@@ -370,6 +289,12 @@ var Agent = class extends BaseResource {
|
|
|
370
289
|
return this.request(`/api/agents/${this.agentId}`);
|
|
371
290
|
}
|
|
372
291
|
async generate(params) {
|
|
292
|
+
console.warn(
|
|
293
|
+
"Deprecation NOTICE:Generate method will switch to use generateVNext implementation September 16th. Please use generateLegacy if you don't want to upgrade just yet."
|
|
294
|
+
);
|
|
295
|
+
return this.generateLegacy(params);
|
|
296
|
+
}
|
|
297
|
+
async generateLegacy(params) {
|
|
373
298
|
const processedParams = {
|
|
374
299
|
...params,
|
|
375
300
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -378,10 +303,13 @@ var Agent = class extends BaseResource {
|
|
|
378
303
|
clientTools: processClientTools(params.clientTools)
|
|
379
304
|
};
|
|
380
305
|
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
381
|
-
const response = await this.request(
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
306
|
+
const response = await this.request(
|
|
307
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
308
|
+
{
|
|
309
|
+
method: "POST",
|
|
310
|
+
body: processedParams
|
|
311
|
+
}
|
|
312
|
+
);
|
|
385
313
|
if (response.finishReason === "tool-calls") {
|
|
386
314
|
const toolCalls = response.toolCalls;
|
|
387
315
|
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
@@ -391,7 +319,14 @@ var Agent = class extends BaseResource {
|
|
|
391
319
|
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
392
320
|
if (clientTool && clientTool.execute) {
|
|
393
321
|
const result = await clientTool.execute(
|
|
394
|
-
{
|
|
322
|
+
{
|
|
323
|
+
context: toolCall?.args,
|
|
324
|
+
runId,
|
|
325
|
+
resourceId,
|
|
326
|
+
threadId,
|
|
327
|
+
runtimeContext,
|
|
328
|
+
tracingContext: { currentSpan: void 0 }
|
|
329
|
+
},
|
|
395
330
|
{
|
|
396
331
|
messages: response.messages,
|
|
397
332
|
toolCallId: toolCall?.toolCallId
|
|
@@ -424,14 +359,41 @@ var Agent = class extends BaseResource {
|
|
|
424
359
|
}
|
|
425
360
|
return response;
|
|
426
361
|
}
|
|
362
|
+
async generateVNext(params) {
|
|
363
|
+
const processedParams = {
|
|
364
|
+
...params,
|
|
365
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
366
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
367
|
+
clientTools: processClientTools(params.clientTools)
|
|
368
|
+
};
|
|
369
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
370
|
+
const response = await this.request(
|
|
371
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
372
|
+
{
|
|
373
|
+
method: "POST",
|
|
374
|
+
body: processedParams
|
|
375
|
+
}
|
|
376
|
+
);
|
|
377
|
+
if (response.finishReason === "tool-calls") {
|
|
378
|
+
return executeToolCallAndRespond({
|
|
379
|
+
response,
|
|
380
|
+
params,
|
|
381
|
+
runId,
|
|
382
|
+
resourceId,
|
|
383
|
+
threadId,
|
|
384
|
+
runtimeContext,
|
|
385
|
+
respondFn: this.generateVNext.bind(this)
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
return response;
|
|
389
|
+
}
|
|
427
390
|
async processChatResponse({
|
|
428
391
|
stream,
|
|
429
392
|
update,
|
|
430
393
|
onToolCall,
|
|
431
394
|
onFinish,
|
|
432
395
|
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
433
|
-
lastMessage
|
|
434
|
-
streamProtocol
|
|
396
|
+
lastMessage
|
|
435
397
|
}) {
|
|
436
398
|
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
437
399
|
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
@@ -492,264 +454,566 @@ var Agent = class extends BaseResource {
|
|
|
492
454
|
replaceLastMessage
|
|
493
455
|
});
|
|
494
456
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
457
|
+
await uiUtils.processDataStream({
|
|
458
|
+
stream,
|
|
459
|
+
onTextPart(value) {
|
|
460
|
+
if (currentTextPart == null) {
|
|
461
|
+
currentTextPart = {
|
|
462
|
+
type: "text",
|
|
463
|
+
text: value
|
|
464
|
+
};
|
|
465
|
+
message.parts.push(currentTextPart);
|
|
466
|
+
} else {
|
|
467
|
+
currentTextPart.text += value;
|
|
501
468
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if (
|
|
509
|
-
|
|
510
|
-
type: "text",
|
|
511
|
-
text: value
|
|
512
|
-
};
|
|
513
|
-
message.parts.push(currentTextPart);
|
|
514
|
-
} else {
|
|
515
|
-
currentTextPart.text += value;
|
|
516
|
-
}
|
|
517
|
-
message.content += value;
|
|
518
|
-
execUpdate();
|
|
519
|
-
},
|
|
520
|
-
onReasoningPart(value) {
|
|
521
|
-
if (currentReasoningTextDetail == null) {
|
|
522
|
-
currentReasoningTextDetail = { type: "text", text: value };
|
|
523
|
-
if (currentReasoningPart != null) {
|
|
524
|
-
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
525
|
-
}
|
|
526
|
-
} else {
|
|
527
|
-
currentReasoningTextDetail.text += value;
|
|
528
|
-
}
|
|
529
|
-
if (currentReasoningPart == null) {
|
|
530
|
-
currentReasoningPart = {
|
|
531
|
-
type: "reasoning",
|
|
532
|
-
reasoning: value,
|
|
533
|
-
details: [currentReasoningTextDetail]
|
|
534
|
-
};
|
|
535
|
-
message.parts.push(currentReasoningPart);
|
|
536
|
-
} else {
|
|
537
|
-
currentReasoningPart.reasoning += value;
|
|
538
|
-
}
|
|
539
|
-
message.reasoning = (message.reasoning ?? "") + value;
|
|
540
|
-
execUpdate();
|
|
541
|
-
},
|
|
542
|
-
onReasoningSignaturePart(value) {
|
|
543
|
-
if (currentReasoningTextDetail != null) {
|
|
544
|
-
currentReasoningTextDetail.signature = value.signature;
|
|
545
|
-
}
|
|
546
|
-
},
|
|
547
|
-
onRedactedReasoningPart(value) {
|
|
548
|
-
if (currentReasoningPart == null) {
|
|
549
|
-
currentReasoningPart = {
|
|
550
|
-
type: "reasoning",
|
|
551
|
-
reasoning: "",
|
|
552
|
-
details: []
|
|
553
|
-
};
|
|
554
|
-
message.parts.push(currentReasoningPart);
|
|
469
|
+
message.content += value;
|
|
470
|
+
execUpdate();
|
|
471
|
+
},
|
|
472
|
+
onReasoningPart(value) {
|
|
473
|
+
if (currentReasoningTextDetail == null) {
|
|
474
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
475
|
+
if (currentReasoningPart != null) {
|
|
476
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
555
477
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
478
|
+
} else {
|
|
479
|
+
currentReasoningTextDetail.text += value;
|
|
480
|
+
}
|
|
481
|
+
if (currentReasoningPart == null) {
|
|
482
|
+
currentReasoningPart = {
|
|
483
|
+
type: "reasoning",
|
|
484
|
+
reasoning: value,
|
|
485
|
+
details: [currentReasoningTextDetail]
|
|
486
|
+
};
|
|
487
|
+
message.parts.push(currentReasoningPart);
|
|
488
|
+
} else {
|
|
489
|
+
currentReasoningPart.reasoning += value;
|
|
490
|
+
}
|
|
491
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
492
|
+
execUpdate();
|
|
493
|
+
},
|
|
494
|
+
onReasoningSignaturePart(value) {
|
|
495
|
+
if (currentReasoningTextDetail != null) {
|
|
496
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
onRedactedReasoningPart(value) {
|
|
500
|
+
if (currentReasoningPart == null) {
|
|
501
|
+
currentReasoningPart = {
|
|
502
|
+
type: "reasoning",
|
|
503
|
+
reasoning: "",
|
|
504
|
+
details: []
|
|
505
|
+
};
|
|
506
|
+
message.parts.push(currentReasoningPart);
|
|
507
|
+
}
|
|
508
|
+
currentReasoningPart.details.push({
|
|
509
|
+
type: "redacted",
|
|
510
|
+
data: value.data
|
|
511
|
+
});
|
|
512
|
+
currentReasoningTextDetail = void 0;
|
|
513
|
+
execUpdate();
|
|
514
|
+
},
|
|
515
|
+
onFilePart(value) {
|
|
516
|
+
message.parts.push({
|
|
517
|
+
type: "file",
|
|
518
|
+
mimeType: value.mimeType,
|
|
519
|
+
data: value.data
|
|
520
|
+
});
|
|
521
|
+
execUpdate();
|
|
522
|
+
},
|
|
523
|
+
onSourcePart(value) {
|
|
524
|
+
message.parts.push({
|
|
525
|
+
type: "source",
|
|
526
|
+
source: value
|
|
527
|
+
});
|
|
528
|
+
execUpdate();
|
|
529
|
+
},
|
|
530
|
+
onToolCallStreamingStartPart(value) {
|
|
531
|
+
if (message.toolInvocations == null) {
|
|
532
|
+
message.toolInvocations = [];
|
|
533
|
+
}
|
|
534
|
+
partialToolCalls[value.toolCallId] = {
|
|
535
|
+
text: "",
|
|
536
|
+
step,
|
|
537
|
+
toolName: value.toolName,
|
|
538
|
+
index: message.toolInvocations.length
|
|
539
|
+
};
|
|
540
|
+
const invocation = {
|
|
541
|
+
state: "partial-call",
|
|
542
|
+
step,
|
|
543
|
+
toolCallId: value.toolCallId,
|
|
544
|
+
toolName: value.toolName,
|
|
545
|
+
args: void 0
|
|
546
|
+
};
|
|
547
|
+
message.toolInvocations.push(invocation);
|
|
548
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
549
|
+
execUpdate();
|
|
550
|
+
},
|
|
551
|
+
onToolCallDeltaPart(value) {
|
|
552
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
553
|
+
partialToolCall.text += value.argsTextDelta;
|
|
554
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
555
|
+
const invocation = {
|
|
556
|
+
state: "partial-call",
|
|
557
|
+
step: partialToolCall.step,
|
|
558
|
+
toolCallId: value.toolCallId,
|
|
559
|
+
toolName: partialToolCall.toolName,
|
|
560
|
+
args: partialArgs
|
|
561
|
+
};
|
|
562
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
563
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
564
|
+
execUpdate();
|
|
565
|
+
},
|
|
566
|
+
async onToolCallPart(value) {
|
|
567
|
+
const invocation = {
|
|
568
|
+
state: "call",
|
|
569
|
+
step,
|
|
570
|
+
...value
|
|
571
|
+
};
|
|
572
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
573
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
574
|
+
} else {
|
|
579
575
|
if (message.toolInvocations == null) {
|
|
580
576
|
message.toolInvocations = [];
|
|
581
577
|
}
|
|
582
|
-
partialToolCalls[value.toolCallId] = {
|
|
583
|
-
text: "",
|
|
584
|
-
step,
|
|
585
|
-
toolName: value.toolName,
|
|
586
|
-
index: message.toolInvocations.length
|
|
587
|
-
};
|
|
588
|
-
const invocation = {
|
|
589
|
-
state: "partial-call",
|
|
590
|
-
step,
|
|
591
|
-
toolCallId: value.toolCallId,
|
|
592
|
-
toolName: value.toolName,
|
|
593
|
-
args: void 0
|
|
594
|
-
};
|
|
595
578
|
message.toolInvocations.push(invocation);
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
const
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
612
|
-
execUpdate();
|
|
613
|
-
},
|
|
614
|
-
async onToolCallPart(value) {
|
|
615
|
-
const invocation = {
|
|
616
|
-
state: "call",
|
|
617
|
-
step,
|
|
618
|
-
...value
|
|
619
|
-
};
|
|
620
|
-
if (partialToolCalls[value.toolCallId] != null) {
|
|
621
|
-
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
622
|
-
} else {
|
|
623
|
-
if (message.toolInvocations == null) {
|
|
624
|
-
message.toolInvocations = [];
|
|
625
|
-
}
|
|
626
|
-
message.toolInvocations.push(invocation);
|
|
627
|
-
}
|
|
628
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
629
|
-
execUpdate();
|
|
630
|
-
if (onToolCall) {
|
|
631
|
-
const result = await onToolCall({ toolCall: value });
|
|
632
|
-
if (result != null) {
|
|
633
|
-
const invocation2 = {
|
|
634
|
-
state: "result",
|
|
635
|
-
step,
|
|
636
|
-
...value,
|
|
637
|
-
result
|
|
638
|
-
};
|
|
639
|
-
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
640
|
-
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
641
|
-
execUpdate();
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
},
|
|
645
|
-
onToolResultPart(value) {
|
|
646
|
-
const toolInvocations = message.toolInvocations;
|
|
647
|
-
if (toolInvocations == null) {
|
|
648
|
-
throw new Error("tool_result must be preceded by a tool_call");
|
|
649
|
-
}
|
|
650
|
-
const toolInvocationIndex = toolInvocations.findIndex(
|
|
651
|
-
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
652
|
-
);
|
|
653
|
-
if (toolInvocationIndex === -1) {
|
|
654
|
-
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
655
|
-
}
|
|
656
|
-
const invocation = {
|
|
657
|
-
...toolInvocations[toolInvocationIndex],
|
|
658
|
-
state: "result",
|
|
659
|
-
...value
|
|
660
|
-
};
|
|
661
|
-
toolInvocations[toolInvocationIndex] = invocation;
|
|
662
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
663
|
-
execUpdate();
|
|
664
|
-
},
|
|
665
|
-
onDataPart(value) {
|
|
666
|
-
data.push(...value);
|
|
667
|
-
execUpdate();
|
|
668
|
-
},
|
|
669
|
-
onMessageAnnotationsPart(value) {
|
|
670
|
-
if (messageAnnotations == null) {
|
|
671
|
-
messageAnnotations = [...value];
|
|
672
|
-
} else {
|
|
673
|
-
messageAnnotations.push(...value);
|
|
674
|
-
}
|
|
675
|
-
execUpdate();
|
|
676
|
-
},
|
|
677
|
-
onFinishStepPart(value) {
|
|
678
|
-
step += 1;
|
|
679
|
-
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
680
|
-
currentReasoningPart = void 0;
|
|
681
|
-
currentReasoningTextDetail = void 0;
|
|
682
|
-
},
|
|
683
|
-
onStartStepPart(value) {
|
|
684
|
-
if (!replaceLastMessage) {
|
|
685
|
-
message.id = value.messageId;
|
|
686
|
-
}
|
|
687
|
-
message.parts.push({ type: "step-start" });
|
|
688
|
-
execUpdate();
|
|
689
|
-
},
|
|
690
|
-
onFinishMessagePart(value) {
|
|
691
|
-
finishReason = value.finishReason;
|
|
692
|
-
if (value.usage != null) {
|
|
693
|
-
usage = value.usage;
|
|
579
|
+
}
|
|
580
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
581
|
+
execUpdate();
|
|
582
|
+
if (onToolCall) {
|
|
583
|
+
const result = await onToolCall({ toolCall: value });
|
|
584
|
+
if (result != null) {
|
|
585
|
+
const invocation2 = {
|
|
586
|
+
state: "result",
|
|
587
|
+
step,
|
|
588
|
+
...value,
|
|
589
|
+
result
|
|
590
|
+
};
|
|
591
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
592
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
593
|
+
execUpdate();
|
|
694
594
|
}
|
|
695
|
-
},
|
|
696
|
-
onErrorPart(error) {
|
|
697
|
-
throw new Error(error);
|
|
698
595
|
}
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
|
|
596
|
+
},
|
|
597
|
+
onToolResultPart(value) {
|
|
598
|
+
const toolInvocations = message.toolInvocations;
|
|
599
|
+
if (toolInvocations == null) {
|
|
600
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
601
|
+
}
|
|
602
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
603
|
+
if (toolInvocationIndex === -1) {
|
|
604
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
605
|
+
}
|
|
606
|
+
const invocation = {
|
|
607
|
+
...toolInvocations[toolInvocationIndex],
|
|
608
|
+
state: "result",
|
|
609
|
+
...value
|
|
610
|
+
};
|
|
611
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
612
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
613
|
+
execUpdate();
|
|
614
|
+
},
|
|
615
|
+
onDataPart(value) {
|
|
616
|
+
data.push(...value);
|
|
617
|
+
execUpdate();
|
|
618
|
+
},
|
|
619
|
+
onMessageAnnotationsPart(value) {
|
|
620
|
+
if (messageAnnotations == null) {
|
|
621
|
+
messageAnnotations = [...value];
|
|
622
|
+
} else {
|
|
623
|
+
messageAnnotations.push(...value);
|
|
624
|
+
}
|
|
625
|
+
execUpdate();
|
|
626
|
+
},
|
|
627
|
+
onFinishStepPart(value) {
|
|
628
|
+
step += 1;
|
|
629
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
630
|
+
currentReasoningPart = void 0;
|
|
631
|
+
currentReasoningTextDetail = void 0;
|
|
632
|
+
},
|
|
633
|
+
onStartStepPart(value) {
|
|
634
|
+
if (!replaceLastMessage) {
|
|
635
|
+
message.id = value.messageId;
|
|
636
|
+
}
|
|
637
|
+
message.parts.push({ type: "step-start" });
|
|
638
|
+
execUpdate();
|
|
639
|
+
},
|
|
640
|
+
onFinishMessagePart(value) {
|
|
641
|
+
finishReason = value.finishReason;
|
|
642
|
+
if (value.usage != null) {
|
|
643
|
+
usage = value.usage;
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
onErrorPart(error) {
|
|
647
|
+
throw new Error(error);
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
onFinish?.({ message, finishReason, usage });
|
|
702
651
|
}
|
|
703
652
|
/**
|
|
704
|
-
*
|
|
653
|
+
* Streams a response from the agent
|
|
654
|
+
* @param params - Stream parameters including prompt
|
|
655
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
705
656
|
*/
|
|
706
|
-
async
|
|
707
|
-
|
|
708
|
-
method
|
|
709
|
-
|
|
710
|
-
|
|
657
|
+
async stream(params) {
|
|
658
|
+
console.warn(
|
|
659
|
+
"Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 16th. Please use streamLegacy if you don't want to upgrade just yet."
|
|
660
|
+
);
|
|
661
|
+
return this.streamLegacy(params);
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Streams a response from the agent
|
|
665
|
+
* @param params - Stream parameters including prompt
|
|
666
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
667
|
+
*/
|
|
668
|
+
async streamLegacy(params) {
|
|
669
|
+
const processedParams = {
|
|
670
|
+
...params,
|
|
671
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
672
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
673
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
674
|
+
clientTools: processClientTools(params.clientTools)
|
|
675
|
+
};
|
|
676
|
+
const { readable, writable } = new TransformStream();
|
|
677
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
678
|
+
const streamResponse = new Response(readable, {
|
|
679
|
+
status: response.status,
|
|
680
|
+
statusText: response.statusText,
|
|
681
|
+
headers: response.headers
|
|
711
682
|
});
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
const streamProtocol = processedParams.output ? "text" : "data";
|
|
717
|
-
let toolCalls = [];
|
|
718
|
-
let messages = [];
|
|
719
|
-
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
720
|
-
streamForWritable.pipeTo(writable, {
|
|
721
|
-
preventClose: true
|
|
722
|
-
}).catch((error) => {
|
|
723
|
-
console.error("Error piping to writable stream:", error);
|
|
683
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
684
|
+
await uiUtils.processDataStream({
|
|
685
|
+
stream: streamResponse.body,
|
|
686
|
+
...options
|
|
724
687
|
});
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
688
|
+
};
|
|
689
|
+
return streamResponse;
|
|
690
|
+
}
|
|
691
|
+
async processChatResponse_vNext({
|
|
692
|
+
stream,
|
|
693
|
+
update,
|
|
694
|
+
onToolCall,
|
|
695
|
+
onFinish,
|
|
696
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
697
|
+
lastMessage
|
|
698
|
+
}) {
|
|
699
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
700
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
701
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
702
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
703
|
+
}, 0) ?? 0) : 0;
|
|
704
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
705
|
+
id: uuid.v4(),
|
|
706
|
+
createdAt: getCurrentDate(),
|
|
707
|
+
role: "assistant",
|
|
708
|
+
content: "",
|
|
709
|
+
parts: []
|
|
710
|
+
};
|
|
711
|
+
let currentTextPart = void 0;
|
|
712
|
+
let currentReasoningPart = void 0;
|
|
713
|
+
let currentReasoningTextDetail = void 0;
|
|
714
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
715
|
+
const part = message.parts.find(
|
|
716
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
717
|
+
);
|
|
718
|
+
if (part != null) {
|
|
719
|
+
part.toolInvocation = invocation;
|
|
720
|
+
} else {
|
|
721
|
+
message.parts.push({
|
|
722
|
+
type: "tool-invocation",
|
|
723
|
+
toolInvocation: invocation
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
const data = [];
|
|
728
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
729
|
+
const partialToolCalls = {};
|
|
730
|
+
let usage = {
|
|
731
|
+
completionTokens: NaN,
|
|
732
|
+
promptTokens: NaN,
|
|
733
|
+
totalTokens: NaN
|
|
734
|
+
};
|
|
735
|
+
let finishReason = "unknown";
|
|
736
|
+
function execUpdate() {
|
|
737
|
+
const copiedData = [...data];
|
|
738
|
+
if (messageAnnotations?.length) {
|
|
739
|
+
message.annotations = messageAnnotations;
|
|
740
|
+
}
|
|
741
|
+
const copiedMessage = {
|
|
742
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
743
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
744
|
+
...structuredClone(message),
|
|
745
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
746
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
747
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
748
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
749
|
+
// forwarded to rendering):
|
|
750
|
+
revisionId: uuid.v4()
|
|
751
|
+
};
|
|
752
|
+
update({
|
|
753
|
+
message: copiedMessage,
|
|
754
|
+
data: copiedData,
|
|
755
|
+
replaceLastMessage
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
await processMastraStream({
|
|
759
|
+
stream,
|
|
760
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
761
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
762
|
+
onChunk: async (chunk) => {
|
|
763
|
+
switch (chunk.type) {
|
|
764
|
+
case "step-start": {
|
|
765
|
+
if (!replaceLastMessage) {
|
|
766
|
+
message.id = chunk.payload.messageId;
|
|
767
|
+
}
|
|
768
|
+
message.parts.push({ type: "step-start" });
|
|
769
|
+
execUpdate();
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
case "text-delta": {
|
|
773
|
+
if (currentTextPart == null) {
|
|
774
|
+
currentTextPart = {
|
|
775
|
+
type: "text",
|
|
776
|
+
text: chunk.payload.text
|
|
777
|
+
};
|
|
778
|
+
message.parts.push(currentTextPart);
|
|
779
|
+
} else {
|
|
780
|
+
currentTextPart.text += chunk.payload.text;
|
|
781
|
+
}
|
|
782
|
+
message.content += chunk.payload.text;
|
|
783
|
+
execUpdate();
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
case "reasoning-delta": {
|
|
787
|
+
if (currentReasoningTextDetail == null) {
|
|
788
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
789
|
+
if (currentReasoningPart != null) {
|
|
790
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
791
|
+
}
|
|
792
|
+
} else {
|
|
793
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
794
|
+
}
|
|
795
|
+
if (currentReasoningPart == null) {
|
|
796
|
+
currentReasoningPart = {
|
|
797
|
+
type: "reasoning",
|
|
798
|
+
reasoning: chunk.payload.text,
|
|
799
|
+
details: [currentReasoningTextDetail]
|
|
800
|
+
};
|
|
801
|
+
message.parts.push(currentReasoningPart);
|
|
802
|
+
} else {
|
|
803
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
804
|
+
}
|
|
805
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
806
|
+
execUpdate();
|
|
807
|
+
break;
|
|
808
|
+
}
|
|
809
|
+
case "file": {
|
|
810
|
+
message.parts.push({
|
|
811
|
+
type: "file",
|
|
812
|
+
mimeType: chunk.payload.mimeType,
|
|
813
|
+
data: chunk.payload.data
|
|
814
|
+
});
|
|
815
|
+
execUpdate();
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
case "source": {
|
|
819
|
+
message.parts.push({
|
|
820
|
+
type: "source",
|
|
821
|
+
source: chunk.payload.source
|
|
822
|
+
});
|
|
823
|
+
execUpdate();
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
case "tool-call": {
|
|
827
|
+
const invocation = {
|
|
828
|
+
state: "call",
|
|
829
|
+
step,
|
|
830
|
+
...chunk.payload
|
|
831
|
+
};
|
|
832
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
833
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
834
|
+
} else {
|
|
835
|
+
if (message.toolInvocations == null) {
|
|
836
|
+
message.toolInvocations = [];
|
|
837
|
+
}
|
|
838
|
+
message.toolInvocations.push(invocation);
|
|
839
|
+
}
|
|
840
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
841
|
+
execUpdate();
|
|
842
|
+
if (onToolCall) {
|
|
843
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
844
|
+
if (result != null) {
|
|
845
|
+
const invocation2 = {
|
|
846
|
+
state: "result",
|
|
847
|
+
step,
|
|
848
|
+
...chunk.payload,
|
|
849
|
+
result
|
|
850
|
+
};
|
|
851
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
852
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
853
|
+
execUpdate();
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
case "tool-call-input-streaming-start": {
|
|
858
|
+
if (message.toolInvocations == null) {
|
|
859
|
+
message.toolInvocations = [];
|
|
860
|
+
}
|
|
861
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
862
|
+
text: "",
|
|
863
|
+
step,
|
|
864
|
+
toolName: chunk.payload.toolName,
|
|
865
|
+
index: message.toolInvocations.length
|
|
866
|
+
};
|
|
867
|
+
const invocation = {
|
|
868
|
+
state: "partial-call",
|
|
869
|
+
step,
|
|
870
|
+
toolCallId: chunk.payload.toolCallId,
|
|
871
|
+
toolName: chunk.payload.toolName,
|
|
872
|
+
args: chunk.payload.args
|
|
873
|
+
};
|
|
874
|
+
message.toolInvocations.push(invocation);
|
|
875
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
876
|
+
execUpdate();
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
case "tool-call-delta": {
|
|
880
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
881
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
882
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
883
|
+
const invocation = {
|
|
884
|
+
state: "partial-call",
|
|
885
|
+
step: partialToolCall.step,
|
|
886
|
+
toolCallId: chunk.payload.toolCallId,
|
|
887
|
+
toolName: partialToolCall.toolName,
|
|
888
|
+
args: partialArgs
|
|
889
|
+
};
|
|
890
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
891
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
892
|
+
execUpdate();
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
case "tool-result": {
|
|
896
|
+
const toolInvocations = message.toolInvocations;
|
|
897
|
+
if (toolInvocations == null) {
|
|
898
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
899
|
+
}
|
|
900
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
901
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
902
|
+
);
|
|
903
|
+
if (toolInvocationIndex === -1) {
|
|
904
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
905
|
+
}
|
|
906
|
+
const invocation = {
|
|
907
|
+
...toolInvocations[toolInvocationIndex],
|
|
908
|
+
state: "result",
|
|
909
|
+
...chunk.payload
|
|
910
|
+
};
|
|
911
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
912
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
913
|
+
execUpdate();
|
|
914
|
+
break;
|
|
915
|
+
}
|
|
916
|
+
case "error": {
|
|
917
|
+
throw new Error(chunk.payload.error);
|
|
918
|
+
}
|
|
919
|
+
case "data": {
|
|
920
|
+
data.push(...chunk.payload.data);
|
|
921
|
+
execUpdate();
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
case "step-finish": {
|
|
925
|
+
step += 1;
|
|
926
|
+
currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
|
|
927
|
+
currentReasoningPart = void 0;
|
|
928
|
+
currentReasoningTextDetail = void 0;
|
|
929
|
+
execUpdate();
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
case "finish": {
|
|
933
|
+
finishReason = chunk.payload.stepResult.reason;
|
|
934
|
+
if (chunk.payload.usage != null) {
|
|
935
|
+
usage = chunk.payload.usage;
|
|
936
|
+
}
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
onFinish?.({ message, finishReason, usage });
|
|
943
|
+
}
|
|
944
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
945
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
946
|
+
method: "POST",
|
|
947
|
+
body: processedParams,
|
|
948
|
+
stream: true
|
|
949
|
+
});
|
|
950
|
+
if (!response.body) {
|
|
951
|
+
throw new Error("No response body");
|
|
952
|
+
}
|
|
953
|
+
try {
|
|
954
|
+
let toolCalls = [];
|
|
955
|
+
let messages = [];
|
|
956
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
957
|
+
streamForWritable.pipeTo(
|
|
958
|
+
new WritableStream({
|
|
959
|
+
async write(chunk) {
|
|
960
|
+
try {
|
|
961
|
+
const text = new TextDecoder().decode(chunk);
|
|
962
|
+
if (text.includes("[DONE]")) {
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
} catch {
|
|
966
|
+
}
|
|
967
|
+
const writer = writable.getWriter();
|
|
968
|
+
try {
|
|
969
|
+
await writer.write(chunk);
|
|
970
|
+
} finally {
|
|
971
|
+
writer.releaseLock();
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}),
|
|
975
|
+
{
|
|
976
|
+
preventClose: true
|
|
977
|
+
}
|
|
978
|
+
).catch((error) => {
|
|
979
|
+
console.error("Error piping to writable stream:", error);
|
|
980
|
+
});
|
|
981
|
+
this.processChatResponse_vNext({
|
|
982
|
+
stream: streamForProcessing,
|
|
983
|
+
update: ({ message }) => {
|
|
984
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
985
|
+
if (existingIndex !== -1) {
|
|
986
|
+
messages[existingIndex] = message;
|
|
987
|
+
} else {
|
|
988
|
+
messages.push(message);
|
|
989
|
+
}
|
|
990
|
+
},
|
|
991
|
+
onFinish: async ({ finishReason, message }) => {
|
|
992
|
+
if (finishReason === "tool-calls") {
|
|
993
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
994
|
+
if (toolCall) {
|
|
995
|
+
toolCalls.push(toolCall);
|
|
996
|
+
}
|
|
997
|
+
for (const toolCall2 of toolCalls) {
|
|
998
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
999
|
+
if (clientTool && clientTool.execute) {
|
|
1000
|
+
const result = await clientTool.execute(
|
|
740
1001
|
{
|
|
741
1002
|
context: toolCall2?.args,
|
|
742
1003
|
runId: processedParams.runId,
|
|
743
1004
|
resourceId: processedParams.resourceId,
|
|
744
1005
|
threadId: processedParams.threadId,
|
|
745
|
-
runtimeContext: processedParams.runtimeContext
|
|
1006
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1007
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1008
|
+
tracingContext: { currentSpan: void 0 }
|
|
746
1009
|
},
|
|
747
1010
|
{
|
|
748
1011
|
messages: response.messages,
|
|
749
1012
|
toolCallId: toolCall2?.toolCallId
|
|
750
1013
|
}
|
|
751
1014
|
);
|
|
752
|
-
const
|
|
1015
|
+
const lastMessageRaw = messages[messages.length - 1];
|
|
1016
|
+
const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
|
|
753
1017
|
const toolInvocationPart = lastMessage?.parts?.find(
|
|
754
1018
|
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
755
1019
|
);
|
|
@@ -767,160 +1031,64 @@ var Agent = class extends BaseResource {
|
|
|
767
1031
|
toolInvocation.state = "result";
|
|
768
1032
|
toolInvocation.result = result;
|
|
769
1033
|
}
|
|
770
|
-
const writer = writable.getWriter();
|
|
771
|
-
try {
|
|
772
|
-
await writer.write(
|
|
773
|
-
new TextEncoder().encode(
|
|
774
|
-
"a:" + JSON.stringify({
|
|
775
|
-
toolCallId: toolCall2.toolCallId,
|
|
776
|
-
result
|
|
777
|
-
}) + "\n"
|
|
778
|
-
)
|
|
779
|
-
);
|
|
780
|
-
} finally {
|
|
781
|
-
writer.releaseLock();
|
|
782
|
-
}
|
|
783
1034
|
const originalMessages = processedParams.messages;
|
|
784
1035
|
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
785
|
-
|
|
1036
|
+
const updatedMessages = lastMessage != null ? [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messageArray, ...messages];
|
|
1037
|
+
this.processStreamResponse_vNext(
|
|
786
1038
|
{
|
|
787
1039
|
...processedParams,
|
|
788
|
-
messages:
|
|
1040
|
+
messages: updatedMessages
|
|
789
1041
|
},
|
|
790
1042
|
writable
|
|
791
|
-
)
|
|
1043
|
+
).catch((error) => {
|
|
1044
|
+
console.error("Error processing stream response:", error);
|
|
1045
|
+
});
|
|
792
1046
|
}
|
|
793
1047
|
}
|
|
794
1048
|
} else {
|
|
795
1049
|
setTimeout(() => {
|
|
796
|
-
|
|
797
|
-
writable.close();
|
|
798
|
-
}
|
|
1050
|
+
writable.close();
|
|
799
1051
|
}, 0);
|
|
800
1052
|
}
|
|
801
1053
|
},
|
|
802
|
-
lastMessage: void 0
|
|
803
|
-
|
|
1054
|
+
lastMessage: void 0
|
|
1055
|
+
}).catch((error) => {
|
|
1056
|
+
console.error("Error processing stream response:", error);
|
|
804
1057
|
});
|
|
805
1058
|
} catch (error) {
|
|
806
1059
|
console.error("Error processing stream response:", error);
|
|
807
1060
|
}
|
|
808
1061
|
return response;
|
|
809
1062
|
}
|
|
810
|
-
|
|
811
|
-
* Streams a response from the agent
|
|
812
|
-
* @param params - Stream parameters including prompt
|
|
813
|
-
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
814
|
-
*/
|
|
815
|
-
async stream(params) {
|
|
1063
|
+
async streamVNext(params) {
|
|
816
1064
|
const processedParams = {
|
|
817
1065
|
...params,
|
|
818
1066
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
819
|
-
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
820
1067
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
821
1068
|
clientTools: processClientTools(params.clientTools)
|
|
822
1069
|
};
|
|
823
1070
|
const { readable, writable } = new TransformStream();
|
|
824
|
-
const response = await this.
|
|
1071
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
825
1072
|
const streamResponse = new Response(readable, {
|
|
826
1073
|
status: response.status,
|
|
827
1074
|
statusText: response.statusText,
|
|
828
1075
|
headers: response.headers
|
|
829
1076
|
});
|
|
830
|
-
streamResponse.processDataStream = async (
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
});
|
|
835
|
-
};
|
|
836
|
-
streamResponse.processTextStream = async (options) => {
|
|
837
|
-
await uiUtils.processTextStream({
|
|
1077
|
+
streamResponse.processDataStream = async ({
|
|
1078
|
+
onChunk
|
|
1079
|
+
}) => {
|
|
1080
|
+
await processMastraStream({
|
|
838
1081
|
stream: streamResponse.body,
|
|
839
|
-
|
|
840
|
-
})
|
|
1082
|
+
onChunk
|
|
841
1083
|
});
|
|
842
1084
|
};
|
|
843
1085
|
return streamResponse;
|
|
844
1086
|
}
|
|
845
1087
|
/**
|
|
846
|
-
*
|
|
847
|
-
* @param toolId - ID of the tool to retrieve
|
|
848
|
-
* @returns Promise containing tool details
|
|
849
|
-
*/
|
|
850
|
-
getTool(toolId) {
|
|
851
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
852
|
-
}
|
|
853
|
-
/**
|
|
854
|
-
* Executes a tool for the agent
|
|
855
|
-
* @param toolId - ID of the tool to execute
|
|
856
|
-
* @param params - Parameters required for tool execution
|
|
857
|
-
* @returns Promise containing the tool execution results
|
|
858
|
-
*/
|
|
859
|
-
executeTool(toolId, params) {
|
|
860
|
-
const body = {
|
|
861
|
-
data: params.data,
|
|
862
|
-
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
863
|
-
};
|
|
864
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
865
|
-
method: "POST",
|
|
866
|
-
body
|
|
867
|
-
});
|
|
868
|
-
}
|
|
869
|
-
/**
|
|
870
|
-
* Retrieves evaluation results for the agent
|
|
871
|
-
* @returns Promise containing agent evaluations
|
|
872
|
-
*/
|
|
873
|
-
evals() {
|
|
874
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
875
|
-
}
|
|
876
|
-
/**
|
|
877
|
-
* Retrieves live evaluation results for the agent
|
|
878
|
-
* @returns Promise containing live agent evaluations
|
|
879
|
-
*/
|
|
880
|
-
liveEvals() {
|
|
881
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
var Network = class extends BaseResource {
|
|
885
|
-
constructor(options, networkId) {
|
|
886
|
-
super(options);
|
|
887
|
-
this.networkId = networkId;
|
|
888
|
-
}
|
|
889
|
-
/**
|
|
890
|
-
* Retrieves details about the network
|
|
891
|
-
* @returns Promise containing network details
|
|
892
|
-
*/
|
|
893
|
-
details() {
|
|
894
|
-
return this.request(`/api/networks/${this.networkId}`);
|
|
895
|
-
}
|
|
896
|
-
/**
|
|
897
|
-
* Generates a response from the agent
|
|
898
|
-
* @param params - Generation parameters including prompt
|
|
899
|
-
* @returns Promise containing the generated response
|
|
900
|
-
*/
|
|
901
|
-
generate(params) {
|
|
902
|
-
const processedParams = {
|
|
903
|
-
...params,
|
|
904
|
-
output: zodToJsonSchema(params.output),
|
|
905
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
906
|
-
};
|
|
907
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
908
|
-
method: "POST",
|
|
909
|
-
body: processedParams
|
|
910
|
-
});
|
|
911
|
-
}
|
|
912
|
-
/**
|
|
913
|
-
* Streams a response from the agent
|
|
914
|
-
* @param params - Stream parameters including prompt
|
|
915
|
-
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
1088
|
+
* Processes the stream response and handles tool calls
|
|
916
1089
|
*/
|
|
917
|
-
async
|
|
918
|
-
const
|
|
919
|
-
...params,
|
|
920
|
-
output: zodToJsonSchema(params.output),
|
|
921
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
922
|
-
};
|
|
923
|
-
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1090
|
+
async processStreamResponse(processedParams, writable) {
|
|
1091
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
924
1092
|
method: "POST",
|
|
925
1093
|
body: processedParams,
|
|
926
1094
|
stream: true
|
|
@@ -928,8 +1096,207 @@ var Network = class extends BaseResource {
|
|
|
928
1096
|
if (!response.body) {
|
|
929
1097
|
throw new Error("No response body");
|
|
930
1098
|
}
|
|
931
|
-
|
|
932
|
-
|
|
1099
|
+
try {
|
|
1100
|
+
let toolCalls = [];
|
|
1101
|
+
let messages = [];
|
|
1102
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1103
|
+
streamForWritable.pipeTo(writable, {
|
|
1104
|
+
preventClose: true
|
|
1105
|
+
}).catch((error) => {
|
|
1106
|
+
console.error("Error piping to writable stream:", error);
|
|
1107
|
+
});
|
|
1108
|
+
this.processChatResponse({
|
|
1109
|
+
stream: streamForProcessing,
|
|
1110
|
+
update: ({ message }) => {
|
|
1111
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1112
|
+
if (existingIndex !== -1) {
|
|
1113
|
+
messages[existingIndex] = message;
|
|
1114
|
+
} else {
|
|
1115
|
+
messages.push(message);
|
|
1116
|
+
}
|
|
1117
|
+
},
|
|
1118
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1119
|
+
if (finishReason === "tool-calls") {
|
|
1120
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1121
|
+
if (toolCall) {
|
|
1122
|
+
toolCalls.push(toolCall);
|
|
1123
|
+
}
|
|
1124
|
+
for (const toolCall2 of toolCalls) {
|
|
1125
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1126
|
+
if (clientTool && clientTool.execute) {
|
|
1127
|
+
const result = await clientTool.execute(
|
|
1128
|
+
{
|
|
1129
|
+
context: toolCall2?.args,
|
|
1130
|
+
runId: processedParams.runId,
|
|
1131
|
+
resourceId: processedParams.resourceId,
|
|
1132
|
+
threadId: processedParams.threadId,
|
|
1133
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1134
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1135
|
+
tracingContext: { currentSpan: void 0 }
|
|
1136
|
+
},
|
|
1137
|
+
{
|
|
1138
|
+
messages: response.messages,
|
|
1139
|
+
toolCallId: toolCall2?.toolCallId
|
|
1140
|
+
}
|
|
1141
|
+
);
|
|
1142
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1143
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1144
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1145
|
+
);
|
|
1146
|
+
if (toolInvocationPart) {
|
|
1147
|
+
toolInvocationPart.toolInvocation = {
|
|
1148
|
+
...toolInvocationPart.toolInvocation,
|
|
1149
|
+
state: "result",
|
|
1150
|
+
result
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1154
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1155
|
+
);
|
|
1156
|
+
if (toolInvocation) {
|
|
1157
|
+
toolInvocation.state = "result";
|
|
1158
|
+
toolInvocation.result = result;
|
|
1159
|
+
}
|
|
1160
|
+
const writer = writable.getWriter();
|
|
1161
|
+
try {
|
|
1162
|
+
await writer.write(
|
|
1163
|
+
new TextEncoder().encode(
|
|
1164
|
+
"a:" + JSON.stringify({
|
|
1165
|
+
toolCallId: toolCall2.toolCallId,
|
|
1166
|
+
result
|
|
1167
|
+
}) + "\n"
|
|
1168
|
+
)
|
|
1169
|
+
);
|
|
1170
|
+
} finally {
|
|
1171
|
+
writer.releaseLock();
|
|
1172
|
+
}
|
|
1173
|
+
const originalMessages = processedParams.messages;
|
|
1174
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1175
|
+
this.processStreamResponse(
|
|
1176
|
+
{
|
|
1177
|
+
...processedParams,
|
|
1178
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1179
|
+
},
|
|
1180
|
+
writable
|
|
1181
|
+
).catch((error) => {
|
|
1182
|
+
console.error("Error processing stream response:", error);
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
} else {
|
|
1187
|
+
setTimeout(() => {
|
|
1188
|
+
writable.close();
|
|
1189
|
+
}, 0);
|
|
1190
|
+
}
|
|
1191
|
+
},
|
|
1192
|
+
lastMessage: void 0
|
|
1193
|
+
}).catch((error) => {
|
|
1194
|
+
console.error("Error processing stream response:", error);
|
|
1195
|
+
});
|
|
1196
|
+
} catch (error) {
|
|
1197
|
+
console.error("Error processing stream response:", error);
|
|
1198
|
+
}
|
|
1199
|
+
return response;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Gets details about a specific tool available to the agent
|
|
1203
|
+
* @param toolId - ID of the tool to retrieve
|
|
1204
|
+
* @returns Promise containing tool details
|
|
1205
|
+
*/
|
|
1206
|
+
getTool(toolId) {
|
|
1207
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Executes a tool for the agent
|
|
1211
|
+
* @param toolId - ID of the tool to execute
|
|
1212
|
+
* @param params - Parameters required for tool execution
|
|
1213
|
+
* @returns Promise containing the tool execution results
|
|
1214
|
+
*/
|
|
1215
|
+
executeTool(toolId, params) {
|
|
1216
|
+
const body = {
|
|
1217
|
+
data: params.data,
|
|
1218
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
1219
|
+
};
|
|
1220
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
1221
|
+
method: "POST",
|
|
1222
|
+
body
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Retrieves evaluation results for the agent
|
|
1227
|
+
* @returns Promise containing agent evaluations
|
|
1228
|
+
*/
|
|
1229
|
+
evals() {
|
|
1230
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
1231
|
+
}
|
|
1232
|
+
/**
|
|
1233
|
+
* Retrieves live evaluation results for the agent
|
|
1234
|
+
* @returns Promise containing live agent evaluations
|
|
1235
|
+
*/
|
|
1236
|
+
liveEvals() {
|
|
1237
|
+
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Updates the model for the agent
|
|
1241
|
+
* @param params - Parameters for updating the model
|
|
1242
|
+
* @returns Promise containing the updated model
|
|
1243
|
+
*/
|
|
1244
|
+
updateModel(params) {
|
|
1245
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1246
|
+
method: "POST",
|
|
1247
|
+
body: params
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
};
|
|
1251
|
+
var Network = class extends BaseResource {
|
|
1252
|
+
constructor(options, networkId) {
|
|
1253
|
+
super(options);
|
|
1254
|
+
this.networkId = networkId;
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Retrieves details about the network
|
|
1258
|
+
* @returns Promise containing network details
|
|
1259
|
+
*/
|
|
1260
|
+
details() {
|
|
1261
|
+
return this.request(`/api/networks/${this.networkId}`);
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Generates a response from the agent
|
|
1265
|
+
* @param params - Generation parameters including prompt
|
|
1266
|
+
* @returns Promise containing the generated response
|
|
1267
|
+
*/
|
|
1268
|
+
generate(params) {
|
|
1269
|
+
const processedParams = {
|
|
1270
|
+
...params,
|
|
1271
|
+
output: zodToJsonSchema(params.output),
|
|
1272
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
1273
|
+
};
|
|
1274
|
+
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
1275
|
+
method: "POST",
|
|
1276
|
+
body: processedParams
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* Streams a response from the agent
|
|
1281
|
+
* @param params - Stream parameters including prompt
|
|
1282
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
1283
|
+
*/
|
|
1284
|
+
async stream(params) {
|
|
1285
|
+
const processedParams = {
|
|
1286
|
+
...params,
|
|
1287
|
+
output: zodToJsonSchema(params.output),
|
|
1288
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
1289
|
+
};
|
|
1290
|
+
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1291
|
+
method: "POST",
|
|
1292
|
+
body: processedParams,
|
|
1293
|
+
stream: true
|
|
1294
|
+
});
|
|
1295
|
+
if (!response.body) {
|
|
1296
|
+
throw new Error("No response body");
|
|
1297
|
+
}
|
|
1298
|
+
response.processDataStream = async (options = {}) => {
|
|
1299
|
+
await uiUtils.processDataStream({
|
|
933
1300
|
stream: response.body,
|
|
934
1301
|
...options
|
|
935
1302
|
});
|
|
@@ -984,6 +1351,36 @@ var MemoryThread = class extends BaseResource {
|
|
|
984
1351
|
});
|
|
985
1352
|
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
986
1353
|
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
1356
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
1357
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
1358
|
+
*/
|
|
1359
|
+
getMessagesPaginated({
|
|
1360
|
+
selectBy,
|
|
1361
|
+
...rest
|
|
1362
|
+
}) {
|
|
1363
|
+
const query = new URLSearchParams({
|
|
1364
|
+
...rest,
|
|
1365
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
1366
|
+
});
|
|
1367
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Deletes one or more messages from the thread
|
|
1371
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1372
|
+
* message object with id property, or array of message objects
|
|
1373
|
+
* @returns Promise containing deletion result
|
|
1374
|
+
*/
|
|
1375
|
+
deleteMessages(messageIds) {
|
|
1376
|
+
const query = new URLSearchParams({
|
|
1377
|
+
agentId: this.agentId
|
|
1378
|
+
});
|
|
1379
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1380
|
+
method: "POST",
|
|
1381
|
+
body: { messageIds }
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
987
1384
|
};
|
|
988
1385
|
|
|
989
1386
|
// src/resources/vector.ts
|
|
@@ -1234,7 +1631,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
1234
1631
|
};
|
|
1235
1632
|
|
|
1236
1633
|
// src/resources/tool.ts
|
|
1237
|
-
var
|
|
1634
|
+
var Tool = class extends BaseResource {
|
|
1238
1635
|
constructor(options, toolId) {
|
|
1239
1636
|
super(options);
|
|
1240
1637
|
this.toolId = toolId;
|
|
@@ -1405,6 +1802,14 @@ var Workflow = class extends BaseResource {
|
|
|
1405
1802
|
method: "POST"
|
|
1406
1803
|
});
|
|
1407
1804
|
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Creates a new workflow run (alias for createRun)
|
|
1807
|
+
* @param params - Optional object containing the optional runId
|
|
1808
|
+
* @returns Promise containing the runId of the created run
|
|
1809
|
+
*/
|
|
1810
|
+
createRunAsync(params) {
|
|
1811
|
+
return this.createRun(params);
|
|
1812
|
+
}
|
|
1408
1813
|
/**
|
|
1409
1814
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1410
1815
|
* @param params - Object containing the runId, inputData and runtimeContext
|
|
@@ -1449,33 +1854,473 @@ var Workflow = class extends BaseResource {
|
|
|
1449
1854
|
if (!!params?.runId) {
|
|
1450
1855
|
searchParams.set("runId", params.runId);
|
|
1451
1856
|
}
|
|
1452
|
-
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1453
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1454
|
-
method: "POST",
|
|
1455
|
-
body: { inputData: params.inputData, runtimeContext }
|
|
1456
|
-
});
|
|
1857
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1858
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1859
|
+
method: "POST",
|
|
1860
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
/**
|
|
1864
|
+
* Starts a workflow run and returns a stream
|
|
1865
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1866
|
+
* @returns Promise containing the workflow execution results
|
|
1867
|
+
*/
|
|
1868
|
+
async stream(params) {
|
|
1869
|
+
const searchParams = new URLSearchParams();
|
|
1870
|
+
if (!!params?.runId) {
|
|
1871
|
+
searchParams.set("runId", params.runId);
|
|
1872
|
+
}
|
|
1873
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1874
|
+
const response = await this.request(
|
|
1875
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1876
|
+
{
|
|
1877
|
+
method: "POST",
|
|
1878
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1879
|
+
stream: true
|
|
1880
|
+
}
|
|
1881
|
+
);
|
|
1882
|
+
if (!response.ok) {
|
|
1883
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1884
|
+
}
|
|
1885
|
+
if (!response.body) {
|
|
1886
|
+
throw new Error("Response body is null");
|
|
1887
|
+
}
|
|
1888
|
+
let failedChunk = void 0;
|
|
1889
|
+
const transformStream = new TransformStream({
|
|
1890
|
+
start() {
|
|
1891
|
+
},
|
|
1892
|
+
async transform(chunk, controller) {
|
|
1893
|
+
try {
|
|
1894
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1895
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1896
|
+
for (const chunk2 of chunks) {
|
|
1897
|
+
if (chunk2) {
|
|
1898
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1899
|
+
try {
|
|
1900
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1901
|
+
controller.enqueue(parsedChunk);
|
|
1902
|
+
failedChunk = void 0;
|
|
1903
|
+
} catch {
|
|
1904
|
+
failedChunk = newChunk;
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
} catch {
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
});
|
|
1912
|
+
return response.body.pipeThrough(transformStream);
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* Starts a workflow run and returns a stream
|
|
1916
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1917
|
+
* @returns Promise containing the workflow execution results
|
|
1918
|
+
*/
|
|
1919
|
+
async streamVNext(params) {
|
|
1920
|
+
const searchParams = new URLSearchParams();
|
|
1921
|
+
if (!!params?.runId) {
|
|
1922
|
+
searchParams.set("runId", params.runId);
|
|
1923
|
+
}
|
|
1924
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1925
|
+
const response = await this.request(
|
|
1926
|
+
`/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
|
|
1927
|
+
{
|
|
1928
|
+
method: "POST",
|
|
1929
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1930
|
+
stream: true
|
|
1931
|
+
}
|
|
1932
|
+
);
|
|
1933
|
+
if (!response.ok) {
|
|
1934
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1935
|
+
}
|
|
1936
|
+
if (!response.body) {
|
|
1937
|
+
throw new Error("Response body is null");
|
|
1938
|
+
}
|
|
1939
|
+
let failedChunk = void 0;
|
|
1940
|
+
const transformStream = new TransformStream({
|
|
1941
|
+
start() {
|
|
1942
|
+
},
|
|
1943
|
+
async transform(chunk, controller) {
|
|
1944
|
+
try {
|
|
1945
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1946
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1947
|
+
for (const chunk2 of chunks) {
|
|
1948
|
+
if (chunk2) {
|
|
1949
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1950
|
+
try {
|
|
1951
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1952
|
+
controller.enqueue(parsedChunk);
|
|
1953
|
+
failedChunk = void 0;
|
|
1954
|
+
} catch {
|
|
1955
|
+
failedChunk = newChunk;
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
} catch {
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
});
|
|
1963
|
+
return response.body.pipeThrough(transformStream);
|
|
1964
|
+
}
|
|
1965
|
+
/**
|
|
1966
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1967
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1968
|
+
* @returns Promise containing the workflow resume results
|
|
1969
|
+
*/
|
|
1970
|
+
resumeAsync(params) {
|
|
1971
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1972
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1973
|
+
method: "POST",
|
|
1974
|
+
body: {
|
|
1975
|
+
step: params.step,
|
|
1976
|
+
resumeData: params.resumeData,
|
|
1977
|
+
runtimeContext
|
|
1978
|
+
}
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
/**
|
|
1982
|
+
* Watches workflow transitions in real-time
|
|
1983
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1984
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1985
|
+
*/
|
|
1986
|
+
async watch({ runId }, onRecord) {
|
|
1987
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1988
|
+
stream: true
|
|
1989
|
+
});
|
|
1990
|
+
if (!response.ok) {
|
|
1991
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1992
|
+
}
|
|
1993
|
+
if (!response.body) {
|
|
1994
|
+
throw new Error("Response body is null");
|
|
1995
|
+
}
|
|
1996
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1997
|
+
if (typeof record === "string") {
|
|
1998
|
+
onRecord(JSON.parse(record));
|
|
1999
|
+
} else {
|
|
2000
|
+
onRecord(record);
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
}
|
|
2004
|
+
/**
|
|
2005
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
2006
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
2007
|
+
*
|
|
2008
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
2009
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
2010
|
+
*/
|
|
2011
|
+
static createRecordStream(records) {
|
|
2012
|
+
const encoder = new TextEncoder();
|
|
2013
|
+
return new ReadableStream({
|
|
2014
|
+
async start(controller) {
|
|
2015
|
+
try {
|
|
2016
|
+
for await (const record of records) {
|
|
2017
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
2018
|
+
controller.enqueue(encoder.encode(json));
|
|
2019
|
+
}
|
|
2020
|
+
controller.close();
|
|
2021
|
+
} catch (err) {
|
|
2022
|
+
controller.error(err);
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
});
|
|
2026
|
+
}
|
|
2027
|
+
};
|
|
2028
|
+
|
|
2029
|
+
// src/resources/a2a.ts
|
|
2030
|
+
var A2A = class extends BaseResource {
|
|
2031
|
+
constructor(options, agentId) {
|
|
2032
|
+
super(options);
|
|
2033
|
+
this.agentId = agentId;
|
|
2034
|
+
}
|
|
2035
|
+
/**
|
|
2036
|
+
* Get the agent card with metadata about the agent
|
|
2037
|
+
* @returns Promise containing the agent card information
|
|
2038
|
+
*/
|
|
2039
|
+
async getCard() {
|
|
2040
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
2041
|
+
}
|
|
2042
|
+
/**
|
|
2043
|
+
* Send a message to the agent and gets a message or task response
|
|
2044
|
+
* @param params - Parameters for the task
|
|
2045
|
+
* @returns Promise containing the response
|
|
2046
|
+
*/
|
|
2047
|
+
async sendMessage(params) {
|
|
2048
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2049
|
+
method: "POST",
|
|
2050
|
+
body: {
|
|
2051
|
+
method: "message/send",
|
|
2052
|
+
params
|
|
2053
|
+
}
|
|
2054
|
+
});
|
|
2055
|
+
return response;
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2059
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2060
|
+
* @param params - Parameters for the task
|
|
2061
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2062
|
+
*/
|
|
2063
|
+
async sendStreamingMessage(params) {
|
|
2064
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2065
|
+
method: "POST",
|
|
2066
|
+
body: {
|
|
2067
|
+
method: "message/stream",
|
|
2068
|
+
params
|
|
2069
|
+
}
|
|
2070
|
+
});
|
|
2071
|
+
return response;
|
|
2072
|
+
}
|
|
2073
|
+
/**
|
|
2074
|
+
* Get the status and result of a task
|
|
2075
|
+
* @param params - Parameters for querying the task
|
|
2076
|
+
* @returns Promise containing the task response
|
|
2077
|
+
*/
|
|
2078
|
+
async getTask(params) {
|
|
2079
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2080
|
+
method: "POST",
|
|
2081
|
+
body: {
|
|
2082
|
+
method: "tasks/get",
|
|
2083
|
+
params
|
|
2084
|
+
}
|
|
2085
|
+
});
|
|
2086
|
+
return response;
|
|
2087
|
+
}
|
|
2088
|
+
/**
|
|
2089
|
+
* Cancel a running task
|
|
2090
|
+
* @param params - Parameters identifying the task to cancel
|
|
2091
|
+
* @returns Promise containing the task response
|
|
2092
|
+
*/
|
|
2093
|
+
async cancelTask(params) {
|
|
2094
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2095
|
+
method: "POST",
|
|
2096
|
+
body: {
|
|
2097
|
+
method: "tasks/cancel",
|
|
2098
|
+
params
|
|
2099
|
+
}
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
};
|
|
2103
|
+
|
|
2104
|
+
// src/resources/mcp-tool.ts
|
|
2105
|
+
var MCPTool = class extends BaseResource {
|
|
2106
|
+
serverId;
|
|
2107
|
+
toolId;
|
|
2108
|
+
constructor(options, serverId, toolId) {
|
|
2109
|
+
super(options);
|
|
2110
|
+
this.serverId = serverId;
|
|
2111
|
+
this.toolId = toolId;
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2115
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2116
|
+
*/
|
|
2117
|
+
details() {
|
|
2118
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
2119
|
+
}
|
|
2120
|
+
/**
|
|
2121
|
+
* Executes this specific tool on the MCP server.
|
|
2122
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2123
|
+
* @returns Promise containing the result of the tool execution.
|
|
2124
|
+
*/
|
|
2125
|
+
execute(params) {
|
|
2126
|
+
const body = {};
|
|
2127
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2128
|
+
if (params.runtimeContext !== void 0) {
|
|
2129
|
+
body.runtimeContext = params.runtimeContext;
|
|
2130
|
+
}
|
|
2131
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2132
|
+
method: "POST",
|
|
2133
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
|
|
2138
|
+
// src/resources/agent-builder.ts
|
|
2139
|
+
var RECORD_SEPARATOR3 = "";
|
|
2140
|
+
var AgentBuilder = class extends BaseResource {
|
|
2141
|
+
constructor(options, actionId) {
|
|
2142
|
+
super(options);
|
|
2143
|
+
this.actionId = actionId;
|
|
2144
|
+
}
|
|
2145
|
+
// Helper function to transform workflow result to action result
|
|
2146
|
+
transformWorkflowResult(result) {
|
|
2147
|
+
if (result.status === "success") {
|
|
2148
|
+
return {
|
|
2149
|
+
success: result.result.success || false,
|
|
2150
|
+
applied: result.result.applied || false,
|
|
2151
|
+
branchName: result.result.branchName,
|
|
2152
|
+
message: result.result.message || "Agent builder action completed",
|
|
2153
|
+
validationResults: result.result.validationResults,
|
|
2154
|
+
error: result.result.error,
|
|
2155
|
+
errors: result.result.errors,
|
|
2156
|
+
stepResults: result.result.stepResults
|
|
2157
|
+
};
|
|
2158
|
+
} else if (result.status === "failed") {
|
|
2159
|
+
return {
|
|
2160
|
+
success: false,
|
|
2161
|
+
applied: false,
|
|
2162
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2163
|
+
error: result.error.message
|
|
2164
|
+
};
|
|
2165
|
+
} else {
|
|
2166
|
+
return {
|
|
2167
|
+
success: false,
|
|
2168
|
+
applied: false,
|
|
2169
|
+
message: "Agent builder action was suspended",
|
|
2170
|
+
error: "Workflow suspended - manual intervention required"
|
|
2171
|
+
};
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
/**
|
|
2175
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2176
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2177
|
+
*/
|
|
2178
|
+
async createRun(params) {
|
|
2179
|
+
const searchParams = new URLSearchParams();
|
|
2180
|
+
if (!!params?.runId) {
|
|
2181
|
+
searchParams.set("runId", params.runId);
|
|
2182
|
+
}
|
|
2183
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2184
|
+
return this.request(url, {
|
|
2185
|
+
method: "POST"
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
/**
|
|
2189
|
+
* Creates a new workflow run (alias for createRun)
|
|
2190
|
+
* @param params - Optional object containing the optional runId
|
|
2191
|
+
* @returns Promise containing the runId of the created run
|
|
2192
|
+
*/
|
|
2193
|
+
createRunAsync(params) {
|
|
2194
|
+
return this.createRun(params);
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2198
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2199
|
+
*/
|
|
2200
|
+
async startAsync(params, runId) {
|
|
2201
|
+
const searchParams = new URLSearchParams();
|
|
2202
|
+
if (runId) {
|
|
2203
|
+
searchParams.set("runId", runId);
|
|
2204
|
+
}
|
|
2205
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2206
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2207
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2208
|
+
const result = await this.request(url, {
|
|
2209
|
+
method: "POST",
|
|
2210
|
+
body: { ...actionParams, runtimeContext }
|
|
2211
|
+
});
|
|
2212
|
+
return this.transformWorkflowResult(result);
|
|
2213
|
+
}
|
|
2214
|
+
/**
|
|
2215
|
+
* Starts an existing agent builder action run.
|
|
2216
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2217
|
+
*/
|
|
2218
|
+
async startActionRun(params, runId) {
|
|
2219
|
+
const searchParams = new URLSearchParams();
|
|
2220
|
+
searchParams.set("runId", runId);
|
|
2221
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2222
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2223
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2224
|
+
return this.request(url, {
|
|
2225
|
+
method: "POST",
|
|
2226
|
+
body: { ...actionParams, runtimeContext }
|
|
2227
|
+
});
|
|
2228
|
+
}
|
|
2229
|
+
/**
|
|
2230
|
+
* Resumes a suspended agent builder action step.
|
|
2231
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2232
|
+
*/
|
|
2233
|
+
async resume(params, runId) {
|
|
2234
|
+
const searchParams = new URLSearchParams();
|
|
2235
|
+
searchParams.set("runId", runId);
|
|
2236
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2237
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2238
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2239
|
+
return this.request(url, {
|
|
2240
|
+
method: "POST",
|
|
2241
|
+
body: { ...resumeParams, runtimeContext }
|
|
2242
|
+
});
|
|
2243
|
+
}
|
|
2244
|
+
/**
|
|
2245
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2246
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2247
|
+
*/
|
|
2248
|
+
async resumeAsync(params, runId) {
|
|
2249
|
+
const searchParams = new URLSearchParams();
|
|
2250
|
+
searchParams.set("runId", runId);
|
|
2251
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2252
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2253
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2254
|
+
const result = await this.request(url, {
|
|
2255
|
+
method: "POST",
|
|
2256
|
+
body: { ...resumeParams, runtimeContext }
|
|
2257
|
+
});
|
|
2258
|
+
return this.transformWorkflowResult(result);
|
|
2259
|
+
}
|
|
2260
|
+
/**
|
|
2261
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2262
|
+
* separated by the Record Separator character (\x1E)
|
|
2263
|
+
*
|
|
2264
|
+
* @param stream - The readable stream to process
|
|
2265
|
+
* @returns An async generator that yields parsed records
|
|
2266
|
+
*/
|
|
2267
|
+
async *streamProcessor(stream) {
|
|
2268
|
+
const reader = stream.getReader();
|
|
2269
|
+
let doneReading = false;
|
|
2270
|
+
let buffer = "";
|
|
2271
|
+
try {
|
|
2272
|
+
while (!doneReading) {
|
|
2273
|
+
const { done, value } = await reader.read();
|
|
2274
|
+
doneReading = done;
|
|
2275
|
+
if (done && !value) continue;
|
|
2276
|
+
try {
|
|
2277
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2278
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
2279
|
+
buffer = chunks.pop() || "";
|
|
2280
|
+
for (const chunk of chunks) {
|
|
2281
|
+
if (chunk) {
|
|
2282
|
+
if (typeof chunk === "string") {
|
|
2283
|
+
try {
|
|
2284
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2285
|
+
yield parsedChunk;
|
|
2286
|
+
} catch {
|
|
2287
|
+
}
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
} catch {
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
if (buffer) {
|
|
2295
|
+
try {
|
|
2296
|
+
yield JSON.parse(buffer);
|
|
2297
|
+
} catch {
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
} finally {
|
|
2301
|
+
reader.cancel().catch(() => {
|
|
2302
|
+
});
|
|
2303
|
+
}
|
|
1457
2304
|
}
|
|
1458
2305
|
/**
|
|
1459
|
-
*
|
|
1460
|
-
*
|
|
1461
|
-
* @returns Promise containing the workflow execution results
|
|
2306
|
+
* Streams agent builder action progress in real-time.
|
|
2307
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
1462
2308
|
*/
|
|
1463
|
-
async stream(params) {
|
|
2309
|
+
async stream(params, runId) {
|
|
1464
2310
|
const searchParams = new URLSearchParams();
|
|
1465
|
-
if (
|
|
1466
|
-
searchParams.set("runId",
|
|
2311
|
+
if (runId) {
|
|
2312
|
+
searchParams.set("runId", runId);
|
|
1467
2313
|
}
|
|
1468
2314
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1469
|
-
const
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
);
|
|
2315
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2316
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2317
|
+
const response = await this.request(url, {
|
|
2318
|
+
method: "POST",
|
|
2319
|
+
body: { ...actionParams, runtimeContext },
|
|
2320
|
+
stream: true
|
|
2321
|
+
});
|
|
1477
2322
|
if (!response.ok) {
|
|
1478
|
-
throw new Error(`Failed to stream
|
|
2323
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
1479
2324
|
}
|
|
1480
2325
|
if (!response.body) {
|
|
1481
2326
|
throw new Error("Response body is null");
|
|
@@ -1487,7 +2332,7 @@ var Workflow = class extends BaseResource {
|
|
|
1487
2332
|
async transform(chunk, controller) {
|
|
1488
2333
|
try {
|
|
1489
2334
|
const decoded = new TextDecoder().decode(chunk);
|
|
1490
|
-
const chunks = decoded.split(
|
|
2335
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
1491
2336
|
for (const chunk2 of chunks) {
|
|
1492
2337
|
if (chunk2) {
|
|
1493
2338
|
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
@@ -1495,7 +2340,7 @@ var Workflow = class extends BaseResource {
|
|
|
1495
2340
|
const parsedChunk = JSON.parse(newChunk);
|
|
1496
2341
|
controller.enqueue(parsedChunk);
|
|
1497
2342
|
failedChunk = void 0;
|
|
1498
|
-
} catch
|
|
2343
|
+
} catch {
|
|
1499
2344
|
failedChunk = newChunk;
|
|
1500
2345
|
}
|
|
1501
2346
|
}
|
|
@@ -1507,32 +2352,68 @@ var Workflow = class extends BaseResource {
|
|
|
1507
2352
|
return response.body.pipeThrough(transformStream);
|
|
1508
2353
|
}
|
|
1509
2354
|
/**
|
|
1510
|
-
*
|
|
1511
|
-
*
|
|
1512
|
-
* @returns Promise containing the workflow resume results
|
|
2355
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2356
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
1513
2357
|
*/
|
|
1514
|
-
|
|
2358
|
+
async streamVNext(params, runId) {
|
|
2359
|
+
const searchParams = new URLSearchParams();
|
|
2360
|
+
if (runId) {
|
|
2361
|
+
searchParams.set("runId", runId);
|
|
2362
|
+
}
|
|
1515
2363
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1516
|
-
|
|
2364
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2365
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2366
|
+
const response = await this.request(url, {
|
|
1517
2367
|
method: "POST",
|
|
1518
|
-
body: {
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
2368
|
+
body: { ...actionParams, runtimeContext },
|
|
2369
|
+
stream: true
|
|
2370
|
+
});
|
|
2371
|
+
if (!response.ok) {
|
|
2372
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2373
|
+
}
|
|
2374
|
+
if (!response.body) {
|
|
2375
|
+
throw new Error("Response body is null");
|
|
2376
|
+
}
|
|
2377
|
+
let failedChunk = void 0;
|
|
2378
|
+
const transformStream = new TransformStream({
|
|
2379
|
+
start() {
|
|
2380
|
+
},
|
|
2381
|
+
async transform(chunk, controller) {
|
|
2382
|
+
try {
|
|
2383
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2384
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2385
|
+
for (const chunk2 of chunks) {
|
|
2386
|
+
if (chunk2) {
|
|
2387
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2388
|
+
try {
|
|
2389
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2390
|
+
controller.enqueue(parsedChunk);
|
|
2391
|
+
failedChunk = void 0;
|
|
2392
|
+
} catch {
|
|
2393
|
+
failedChunk = newChunk;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
} catch {
|
|
2398
|
+
}
|
|
1522
2399
|
}
|
|
1523
2400
|
});
|
|
2401
|
+
return response.body.pipeThrough(transformStream);
|
|
1524
2402
|
}
|
|
1525
2403
|
/**
|
|
1526
|
-
* Watches
|
|
1527
|
-
*
|
|
1528
|
-
*
|
|
2404
|
+
* Watches an existing agent builder action run by runId.
|
|
2405
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2406
|
+
* and streams any remaining progress.
|
|
2407
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
1529
2408
|
*/
|
|
1530
|
-
async watch({ runId }, onRecord) {
|
|
1531
|
-
const
|
|
2409
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2410
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2411
|
+
const response = await this.request(url, {
|
|
2412
|
+
method: "GET",
|
|
1532
2413
|
stream: true
|
|
1533
2414
|
});
|
|
1534
2415
|
if (!response.ok) {
|
|
1535
|
-
throw new Error(`Failed to watch
|
|
2416
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
1536
2417
|
}
|
|
1537
2418
|
if (!response.body) {
|
|
1538
2419
|
throw new Error("Response body is null");
|
|
@@ -1546,135 +2427,126 @@ var Workflow = class extends BaseResource {
|
|
|
1546
2427
|
}
|
|
1547
2428
|
}
|
|
1548
2429
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
*
|
|
1551
|
-
*
|
|
1552
|
-
* @param records - An iterable or async iterable of objects to stream
|
|
1553
|
-
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
2430
|
+
* Gets a specific action run by its ID.
|
|
2431
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
1554
2432
|
*/
|
|
1555
|
-
|
|
1556
|
-
const
|
|
1557
|
-
return
|
|
1558
|
-
|
|
1559
|
-
try {
|
|
1560
|
-
for await (const record of records) {
|
|
1561
|
-
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1562
|
-
controller.enqueue(encoder.encode(json));
|
|
1563
|
-
}
|
|
1564
|
-
controller.close();
|
|
1565
|
-
} catch (err) {
|
|
1566
|
-
controller.error(err);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
2433
|
+
async runById(runId) {
|
|
2434
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2435
|
+
return this.request(url, {
|
|
2436
|
+
method: "GET"
|
|
1569
2437
|
});
|
|
1570
2438
|
}
|
|
1571
|
-
};
|
|
1572
|
-
|
|
1573
|
-
// src/resources/a2a.ts
|
|
1574
|
-
var A2A = class extends BaseResource {
|
|
1575
|
-
constructor(options, agentId) {
|
|
1576
|
-
super(options);
|
|
1577
|
-
this.agentId = agentId;
|
|
1578
|
-
}
|
|
1579
2439
|
/**
|
|
1580
|
-
*
|
|
1581
|
-
*
|
|
2440
|
+
* Gets details about this agent builder action.
|
|
2441
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
1582
2442
|
*/
|
|
1583
|
-
async
|
|
1584
|
-
|
|
2443
|
+
async details() {
|
|
2444
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2445
|
+
return result;
|
|
1585
2446
|
}
|
|
1586
2447
|
/**
|
|
1587
|
-
*
|
|
1588
|
-
*
|
|
1589
|
-
* @returns Promise containing the task response
|
|
2448
|
+
* Gets all runs for this agent builder action.
|
|
2449
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
1590
2450
|
*/
|
|
1591
|
-
async
|
|
1592
|
-
const
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
2451
|
+
async runs(params) {
|
|
2452
|
+
const searchParams = new URLSearchParams();
|
|
2453
|
+
if (params?.fromDate) {
|
|
2454
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2455
|
+
}
|
|
2456
|
+
if (params?.toDate) {
|
|
2457
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2458
|
+
}
|
|
2459
|
+
if (params?.limit !== void 0) {
|
|
2460
|
+
searchParams.set("limit", String(params.limit));
|
|
2461
|
+
}
|
|
2462
|
+
if (params?.offset !== void 0) {
|
|
2463
|
+
searchParams.set("offset", String(params.offset));
|
|
2464
|
+
}
|
|
2465
|
+
if (params?.resourceId) {
|
|
2466
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2467
|
+
}
|
|
2468
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2469
|
+
return this.request(url, {
|
|
2470
|
+
method: "GET"
|
|
1598
2471
|
});
|
|
1599
|
-
return { task: response.result };
|
|
1600
2472
|
}
|
|
1601
2473
|
/**
|
|
1602
|
-
*
|
|
1603
|
-
*
|
|
1604
|
-
* @returns Promise containing the task response
|
|
2474
|
+
* Gets the execution result of an agent builder action run.
|
|
2475
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
1605
2476
|
*/
|
|
1606
|
-
async
|
|
1607
|
-
const
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
method: "tasks/get",
|
|
1611
|
-
params
|
|
1612
|
-
}
|
|
2477
|
+
async runExecutionResult(runId) {
|
|
2478
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2479
|
+
return this.request(url, {
|
|
2480
|
+
method: "GET"
|
|
1613
2481
|
});
|
|
1614
|
-
return response.result;
|
|
1615
2482
|
}
|
|
1616
2483
|
/**
|
|
1617
|
-
*
|
|
1618
|
-
*
|
|
1619
|
-
* @returns Promise containing the task response
|
|
2484
|
+
* Cancels an agent builder action run.
|
|
2485
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
1620
2486
|
*/
|
|
1621
|
-
async
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
method: "tasks/cancel",
|
|
1626
|
-
params
|
|
1627
|
-
}
|
|
2487
|
+
async cancelRun(runId) {
|
|
2488
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2489
|
+
return this.request(url, {
|
|
2490
|
+
method: "POST"
|
|
1628
2491
|
});
|
|
1629
2492
|
}
|
|
1630
2493
|
/**
|
|
1631
|
-
*
|
|
1632
|
-
*
|
|
1633
|
-
* @returns Promise containing the task response
|
|
2494
|
+
* Sends an event to an agent builder action run.
|
|
2495
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
1634
2496
|
*/
|
|
1635
|
-
async
|
|
1636
|
-
|
|
2497
|
+
async sendRunEvent(params) {
|
|
2498
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2499
|
+
return this.request(url, {
|
|
1637
2500
|
method: "POST",
|
|
1638
|
-
body: {
|
|
1639
|
-
method: "tasks/sendSubscribe",
|
|
1640
|
-
params
|
|
1641
|
-
},
|
|
1642
|
-
stream: true
|
|
2501
|
+
body: { event: params.event, data: params.data }
|
|
1643
2502
|
});
|
|
1644
2503
|
}
|
|
1645
2504
|
};
|
|
1646
2505
|
|
|
1647
|
-
// src/resources/
|
|
1648
|
-
var
|
|
1649
|
-
|
|
1650
|
-
toolId;
|
|
1651
|
-
constructor(options, serverId, toolId) {
|
|
2506
|
+
// src/resources/observability.ts
|
|
2507
|
+
var Observability = class extends BaseResource {
|
|
2508
|
+
constructor(options) {
|
|
1652
2509
|
super(options);
|
|
1653
|
-
this.serverId = serverId;
|
|
1654
|
-
this.toolId = toolId;
|
|
1655
2510
|
}
|
|
1656
2511
|
/**
|
|
1657
|
-
* Retrieves
|
|
1658
|
-
* @
|
|
2512
|
+
* Retrieves a specific AI trace by ID
|
|
2513
|
+
* @param traceId - ID of the trace to retrieve
|
|
2514
|
+
* @returns Promise containing the AI trace with all its spans
|
|
1659
2515
|
*/
|
|
1660
|
-
|
|
1661
|
-
return this.request(`/api/
|
|
2516
|
+
getTrace(traceId) {
|
|
2517
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
1662
2518
|
}
|
|
1663
2519
|
/**
|
|
1664
|
-
*
|
|
1665
|
-
* @param params - Parameters for
|
|
1666
|
-
* @returns Promise containing
|
|
2520
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2521
|
+
* @param params - Parameters for pagination and filtering
|
|
2522
|
+
* @returns Promise containing paginated traces and pagination info
|
|
1667
2523
|
*/
|
|
1668
|
-
|
|
1669
|
-
const
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
2524
|
+
getTraces(params) {
|
|
2525
|
+
const { pagination, filters } = params;
|
|
2526
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2527
|
+
const { name, spanType } = filters || {};
|
|
2528
|
+
const searchParams = new URLSearchParams();
|
|
2529
|
+
if (page !== void 0) {
|
|
2530
|
+
searchParams.set("page", String(page));
|
|
1673
2531
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
2532
|
+
if (perPage !== void 0) {
|
|
2533
|
+
searchParams.set("perPage", String(perPage));
|
|
2534
|
+
}
|
|
2535
|
+
if (name) {
|
|
2536
|
+
searchParams.set("name", name);
|
|
2537
|
+
}
|
|
2538
|
+
if (spanType !== void 0) {
|
|
2539
|
+
searchParams.set("spanType", String(spanType));
|
|
2540
|
+
}
|
|
2541
|
+
if (dateRange) {
|
|
2542
|
+
const dateRangeStr = JSON.stringify({
|
|
2543
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2544
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2545
|
+
});
|
|
2546
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2547
|
+
}
|
|
2548
|
+
const queryString = searchParams.toString();
|
|
2549
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
1678
2550
|
}
|
|
1679
2551
|
};
|
|
1680
2552
|
|
|
@@ -1724,10 +2596,25 @@ var NetworkMemoryThread = class extends BaseResource {
|
|
|
1724
2596
|
});
|
|
1725
2597
|
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1726
2598
|
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Deletes one or more messages from the thread
|
|
2601
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
2602
|
+
* message object with id property, or array of message objects
|
|
2603
|
+
* @returns Promise containing deletion result
|
|
2604
|
+
*/
|
|
2605
|
+
deleteMessages(messageIds) {
|
|
2606
|
+
const query = new URLSearchParams({
|
|
2607
|
+
networkId: this.networkId
|
|
2608
|
+
});
|
|
2609
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
2610
|
+
method: "POST",
|
|
2611
|
+
body: { messageIds }
|
|
2612
|
+
});
|
|
2613
|
+
}
|
|
1727
2614
|
};
|
|
1728
2615
|
|
|
1729
2616
|
// src/resources/vNextNetwork.ts
|
|
1730
|
-
var
|
|
2617
|
+
var RECORD_SEPARATOR4 = "";
|
|
1731
2618
|
var VNextNetwork = class extends BaseResource {
|
|
1732
2619
|
constructor(options, networkId) {
|
|
1733
2620
|
super(options);
|
|
@@ -1779,7 +2666,7 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1779
2666
|
if (done && !value) continue;
|
|
1780
2667
|
try {
|
|
1781
2668
|
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1782
|
-
const chunks = (buffer + decoded).split(
|
|
2669
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR4);
|
|
1783
2670
|
buffer = chunks.pop() || "";
|
|
1784
2671
|
for (const chunk of chunks) {
|
|
1785
2672
|
if (chunk) {
|
|
@@ -1866,8 +2753,10 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1866
2753
|
|
|
1867
2754
|
// src/client.ts
|
|
1868
2755
|
var MastraClient = class extends BaseResource {
|
|
2756
|
+
observability;
|
|
1869
2757
|
constructor(options) {
|
|
1870
2758
|
super(options);
|
|
2759
|
+
this.observability = new Observability(options);
|
|
1871
2760
|
}
|
|
1872
2761
|
/**
|
|
1873
2762
|
* Retrieves all available agents
|
|
@@ -1876,21 +2765,6 @@ var MastraClient = class extends BaseResource {
|
|
|
1876
2765
|
getAgents() {
|
|
1877
2766
|
return this.request("/api/agents");
|
|
1878
2767
|
}
|
|
1879
|
-
async getAGUI({ resourceId }) {
|
|
1880
|
-
const agents = await this.getAgents();
|
|
1881
|
-
return Object.entries(agents).reduce(
|
|
1882
|
-
(acc, [agentId]) => {
|
|
1883
|
-
const agent = this.getAgent(agentId);
|
|
1884
|
-
acc[agentId] = new AGUIAdapter({
|
|
1885
|
-
agentId,
|
|
1886
|
-
agent,
|
|
1887
|
-
resourceId
|
|
1888
|
-
});
|
|
1889
|
-
return acc;
|
|
1890
|
-
},
|
|
1891
|
-
{}
|
|
1892
|
-
);
|
|
1893
|
-
}
|
|
1894
2768
|
/**
|
|
1895
2769
|
* Gets an agent instance by ID
|
|
1896
2770
|
* @param agentId - ID of the agent to retrieve
|
|
@@ -1996,7 +2870,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1996
2870
|
* @returns Tool instance
|
|
1997
2871
|
*/
|
|
1998
2872
|
getTool(toolId) {
|
|
1999
|
-
return new
|
|
2873
|
+
return new Tool(this.options, toolId);
|
|
2000
2874
|
}
|
|
2001
2875
|
/**
|
|
2002
2876
|
* Retrieves all available legacy workflows
|
|
@@ -2028,6 +2902,20 @@ var MastraClient = class extends BaseResource {
|
|
|
2028
2902
|
getWorkflow(workflowId) {
|
|
2029
2903
|
return new Workflow(this.options, workflowId);
|
|
2030
2904
|
}
|
|
2905
|
+
/**
|
|
2906
|
+
* Gets all available agent builder actions
|
|
2907
|
+
* @returns Promise containing map of action IDs to action details
|
|
2908
|
+
*/
|
|
2909
|
+
getAgentBuilderActions() {
|
|
2910
|
+
return this.request("/api/agent-builder/");
|
|
2911
|
+
}
|
|
2912
|
+
/**
|
|
2913
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
2914
|
+
* @returns AgentBuilder instance
|
|
2915
|
+
*/
|
|
2916
|
+
getAgentBuilderAction(actionId) {
|
|
2917
|
+
return new AgentBuilder(this.options, actionId);
|
|
2918
|
+
}
|
|
2031
2919
|
/**
|
|
2032
2920
|
* Gets a vector instance by name
|
|
2033
2921
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -2293,6 +3181,99 @@ var MastraClient = class extends BaseResource {
|
|
|
2293
3181
|
}
|
|
2294
3182
|
});
|
|
2295
3183
|
}
|
|
3184
|
+
/**
|
|
3185
|
+
* Retrieves all available scorers
|
|
3186
|
+
* @returns Promise containing list of available scorers
|
|
3187
|
+
*/
|
|
3188
|
+
getScorers() {
|
|
3189
|
+
return this.request("/api/scores/scorers");
|
|
3190
|
+
}
|
|
3191
|
+
/**
|
|
3192
|
+
* Retrieves a scorer by ID
|
|
3193
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
3194
|
+
* @returns Promise containing the scorer
|
|
3195
|
+
*/
|
|
3196
|
+
getScorer(scorerId) {
|
|
3197
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3198
|
+
}
|
|
3199
|
+
getScoresByScorerId(params) {
|
|
3200
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
3201
|
+
const searchParams = new URLSearchParams();
|
|
3202
|
+
if (entityId) {
|
|
3203
|
+
searchParams.set("entityId", entityId);
|
|
3204
|
+
}
|
|
3205
|
+
if (entityType) {
|
|
3206
|
+
searchParams.set("entityType", entityType);
|
|
3207
|
+
}
|
|
3208
|
+
if (page !== void 0) {
|
|
3209
|
+
searchParams.set("page", String(page));
|
|
3210
|
+
}
|
|
3211
|
+
if (perPage !== void 0) {
|
|
3212
|
+
searchParams.set("perPage", String(perPage));
|
|
3213
|
+
}
|
|
3214
|
+
const queryString = searchParams.toString();
|
|
3215
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3216
|
+
}
|
|
3217
|
+
/**
|
|
3218
|
+
* Retrieves scores by run ID
|
|
3219
|
+
* @param params - Parameters containing run ID and pagination options
|
|
3220
|
+
* @returns Promise containing scores and pagination info
|
|
3221
|
+
*/
|
|
3222
|
+
getScoresByRunId(params) {
|
|
3223
|
+
const { runId, page, perPage } = params;
|
|
3224
|
+
const searchParams = new URLSearchParams();
|
|
3225
|
+
if (page !== void 0) {
|
|
3226
|
+
searchParams.set("page", String(page));
|
|
3227
|
+
}
|
|
3228
|
+
if (perPage !== void 0) {
|
|
3229
|
+
searchParams.set("perPage", String(perPage));
|
|
3230
|
+
}
|
|
3231
|
+
const queryString = searchParams.toString();
|
|
3232
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3233
|
+
}
|
|
3234
|
+
/**
|
|
3235
|
+
* Retrieves scores by entity ID and type
|
|
3236
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
3237
|
+
* @returns Promise containing scores and pagination info
|
|
3238
|
+
*/
|
|
3239
|
+
getScoresByEntityId(params) {
|
|
3240
|
+
const { entityId, entityType, page, perPage } = params;
|
|
3241
|
+
const searchParams = new URLSearchParams();
|
|
3242
|
+
if (page !== void 0) {
|
|
3243
|
+
searchParams.set("page", String(page));
|
|
3244
|
+
}
|
|
3245
|
+
if (perPage !== void 0) {
|
|
3246
|
+
searchParams.set("perPage", String(perPage));
|
|
3247
|
+
}
|
|
3248
|
+
const queryString = searchParams.toString();
|
|
3249
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
3250
|
+
}
|
|
3251
|
+
/**
|
|
3252
|
+
* Saves a score
|
|
3253
|
+
* @param params - Parameters containing the score data to save
|
|
3254
|
+
* @returns Promise containing the saved score
|
|
3255
|
+
*/
|
|
3256
|
+
saveScore(params) {
|
|
3257
|
+
return this.request("/api/scores", {
|
|
3258
|
+
method: "POST",
|
|
3259
|
+
body: params
|
|
3260
|
+
});
|
|
3261
|
+
}
|
|
3262
|
+
/**
|
|
3263
|
+
* Retrieves model providers with available keys
|
|
3264
|
+
* @returns Promise containing model providers with available keys
|
|
3265
|
+
*/
|
|
3266
|
+
getModelProviders() {
|
|
3267
|
+
return this.request(`/api/model-providers`);
|
|
3268
|
+
}
|
|
3269
|
+
getAITrace(traceId) {
|
|
3270
|
+
return this.observability.getTrace(traceId);
|
|
3271
|
+
}
|
|
3272
|
+
getAITraces(params) {
|
|
3273
|
+
return this.observability.getTraces(params);
|
|
3274
|
+
}
|
|
2296
3275
|
};
|
|
2297
3276
|
|
|
2298
3277
|
exports.MastraClient = MastraClient;
|
|
3278
|
+
//# sourceMappingURL=index.cjs.map
|
|
3279
|
+
//# sourceMappingURL=index.cjs.map
|