@mastra/client-js 0.0.0-update-scorers-api-20250801170445 → 0.0.0-usechat-duplicate-20251016110554
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 +1050 -3
- package/README.md +6 -8
- package/dist/client.d.ts +57 -44
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +1735 -850
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1734 -851
- package/dist/index.js.map +1 -1
- package/dist/resources/a2a.d.ts +15 -18
- package/dist/resources/a2a.d.ts.map +1 -1
- package/dist/resources/agent-builder.d.ts +160 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +89 -17
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/base.d.ts +1 -1
- package/dist/resources/base.d.ts.map +1 -1
- package/dist/resources/index.d.ts +10 -10
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/resources/mcp-tool.d.ts +4 -3
- package/dist/resources/mcp-tool.d.ts.map +1 -1
- package/dist/resources/memory-thread.d.ts +3 -3
- package/dist/resources/memory-thread.d.ts.map +1 -1
- package/dist/resources/network-memory-thread.d.ts +3 -3
- package/dist/resources/network-memory-thread.d.ts.map +1 -1
- package/dist/resources/observability.d.ts +35 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +4 -3
- package/dist/resources/tool.d.ts.map +1 -1
- package/dist/resources/vector.d.ts +7 -4
- package/dist/resources/vector.d.ts.map +1 -1
- package/dist/resources/workflow.d.ts +129 -14
- package/dist/resources/workflow.d.ts.map +1 -1
- package/dist/tools.d.ts +22 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +100 -43
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/process-client-tools.d.ts.map +1 -1
- package/dist/utils/process-mastra-stream.d.ts +11 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +2 -104
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -1
- package/package.json +24 -16
- package/dist/adapters/agui.d.ts +0 -23
- package/dist/adapters/agui.d.ts.map +0 -1
- package/dist/resources/legacy-workflow.d.ts +0 -87
- package/dist/resources/legacy-workflow.d.ts.map +0 -1
- package/dist/resources/network.d.ts +0 -30
- package/dist/resources/network.d.ts.map +0 -1
- package/dist/resources/vNextNetwork.d.ts +0 -42
- package/dist/resources/vNextNetwork.d.ts.map +0 -1
- package/eslint.config.js +0 -6
- package/integration-tests/agui-adapter.test.ts +0 -122
- package/integration-tests/package.json +0 -18
- package/integration-tests/src/mastra/index.ts +0 -35
- package/integration-tests/vitest.config.ts +0 -9
- package/src/adapters/agui.test.ts +0 -322
- package/src/adapters/agui.ts +0 -239
- package/src/client.ts +0 -623
- package/src/example.ts +0 -95
- package/src/index.test.ts +0 -1226
- package/src/index.ts +0 -3
- package/src/resources/a2a.ts +0 -88
- package/src/resources/agent.ts +0 -785
- package/src/resources/base.ts +0 -76
- 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.test.ts +0 -285
- package/src/resources/memory-thread.ts +0 -99
- package/src/resources/network-memory-thread.test.ts +0 -269
- package/src/resources/network-memory-thread.ts +0 -81
- package/src/resources/network.ts +0 -86
- 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 -410
- package/src/types.ts +0 -502
- 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/src/v2-messages.test.ts +0 -180
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -22
- package/vitest.config.js +0 -8
package/dist/index.js
CHANGED
|
@@ -1,203 +1,49 @@
|
|
|
1
|
-
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
1
|
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
4
|
-
import { ZodSchema } from 'zod';
|
|
5
|
-
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
|
-
import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
7
2
|
import { v4 } from '@lukeed/uuid';
|
|
8
3
|
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
4
|
+
import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
9
7
|
|
|
10
|
-
// src/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
super({
|
|
16
|
-
agentId,
|
|
17
|
-
...rest
|
|
18
|
-
});
|
|
19
|
-
this.agent = agent;
|
|
20
|
-
this.resourceId = resourceId;
|
|
21
|
-
}
|
|
22
|
-
run(input) {
|
|
23
|
-
return new Observable((subscriber) => {
|
|
24
|
-
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
25
|
-
subscriber.next({
|
|
26
|
-
type: EventType.RUN_STARTED,
|
|
27
|
-
threadId: input.threadId,
|
|
28
|
-
runId: input.runId
|
|
29
|
-
});
|
|
30
|
-
this.agent.stream({
|
|
31
|
-
threadId: input.threadId,
|
|
32
|
-
resourceId: this.resourceId ?? "",
|
|
33
|
-
runId: input.runId,
|
|
34
|
-
messages: convertedMessages,
|
|
35
|
-
clientTools: input.tools.reduce(
|
|
36
|
-
(acc, tool) => {
|
|
37
|
-
acc[tool.name] = {
|
|
38
|
-
id: tool.name,
|
|
39
|
-
description: tool.description,
|
|
40
|
-
inputSchema: tool.parameters
|
|
41
|
-
};
|
|
42
|
-
return acc;
|
|
43
|
-
},
|
|
44
|
-
{}
|
|
45
|
-
)
|
|
46
|
-
}).then((response) => {
|
|
47
|
-
let currentMessageId = void 0;
|
|
48
|
-
let isInTextMessage = false;
|
|
49
|
-
return response.processDataStream({
|
|
50
|
-
onTextPart: (text) => {
|
|
51
|
-
if (currentMessageId === void 0) {
|
|
52
|
-
currentMessageId = generateUUID();
|
|
53
|
-
const message2 = {
|
|
54
|
-
type: EventType.TEXT_MESSAGE_START,
|
|
55
|
-
messageId: currentMessageId,
|
|
56
|
-
role: "assistant"
|
|
57
|
-
};
|
|
58
|
-
subscriber.next(message2);
|
|
59
|
-
isInTextMessage = true;
|
|
60
|
-
}
|
|
61
|
-
const message = {
|
|
62
|
-
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
63
|
-
messageId: currentMessageId,
|
|
64
|
-
delta: text
|
|
65
|
-
};
|
|
66
|
-
subscriber.next(message);
|
|
67
|
-
},
|
|
68
|
-
onFinishMessagePart: () => {
|
|
69
|
-
if (currentMessageId !== void 0) {
|
|
70
|
-
const message = {
|
|
71
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
72
|
-
messageId: currentMessageId
|
|
73
|
-
};
|
|
74
|
-
subscriber.next(message);
|
|
75
|
-
isInTextMessage = false;
|
|
76
|
-
}
|
|
77
|
-
subscriber.next({
|
|
78
|
-
type: EventType.RUN_FINISHED,
|
|
79
|
-
threadId: input.threadId,
|
|
80
|
-
runId: input.runId
|
|
81
|
-
});
|
|
82
|
-
subscriber.complete();
|
|
83
|
-
},
|
|
84
|
-
onToolCallPart(streamPart) {
|
|
85
|
-
const parentMessageId = currentMessageId || generateUUID();
|
|
86
|
-
if (isInTextMessage) {
|
|
87
|
-
const message = {
|
|
88
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
89
|
-
messageId: parentMessageId
|
|
90
|
-
};
|
|
91
|
-
subscriber.next(message);
|
|
92
|
-
isInTextMessage = false;
|
|
93
|
-
}
|
|
94
|
-
subscriber.next({
|
|
95
|
-
type: EventType.TOOL_CALL_START,
|
|
96
|
-
toolCallId: streamPart.toolCallId,
|
|
97
|
-
toolCallName: streamPart.toolName,
|
|
98
|
-
parentMessageId
|
|
99
|
-
});
|
|
100
|
-
subscriber.next({
|
|
101
|
-
type: EventType.TOOL_CALL_ARGS,
|
|
102
|
-
toolCallId: streamPart.toolCallId,
|
|
103
|
-
delta: JSON.stringify(streamPart.args),
|
|
104
|
-
parentMessageId
|
|
105
|
-
});
|
|
106
|
-
subscriber.next({
|
|
107
|
-
type: EventType.TOOL_CALL_END,
|
|
108
|
-
toolCallId: streamPart.toolCallId,
|
|
109
|
-
parentMessageId
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
}).catch((error) => {
|
|
114
|
-
console.error("error", error);
|
|
115
|
-
subscriber.error(error);
|
|
116
|
-
});
|
|
117
|
-
return () => {
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
function generateUUID() {
|
|
123
|
-
if (typeof crypto !== "undefined") {
|
|
124
|
-
if (typeof crypto.randomUUID === "function") {
|
|
125
|
-
return crypto.randomUUID();
|
|
126
|
-
}
|
|
127
|
-
if (typeof crypto.getRandomValues === "function") {
|
|
128
|
-
const buffer = new Uint8Array(16);
|
|
129
|
-
crypto.getRandomValues(buffer);
|
|
130
|
-
buffer[6] = buffer[6] & 15 | 64;
|
|
131
|
-
buffer[8] = buffer[8] & 63 | 128;
|
|
132
|
-
let hex = "";
|
|
133
|
-
for (let i = 0; i < 16; i++) {
|
|
134
|
-
hex += buffer[i].toString(16).padStart(2, "0");
|
|
135
|
-
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
136
|
-
}
|
|
137
|
-
return hex;
|
|
8
|
+
// src/resources/agent.ts
|
|
9
|
+
function parseClientRuntimeContext(runtimeContext) {
|
|
10
|
+
if (runtimeContext) {
|
|
11
|
+
if (runtimeContext instanceof RuntimeContext) {
|
|
12
|
+
return Object.fromEntries(runtimeContext.entries());
|
|
138
13
|
}
|
|
14
|
+
return runtimeContext;
|
|
139
15
|
}
|
|
140
|
-
return
|
|
141
|
-
const r = Math.random() * 16 | 0;
|
|
142
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
143
|
-
return v.toString(16);
|
|
144
|
-
});
|
|
16
|
+
return void 0;
|
|
145
17
|
}
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (message.role === "assistant") {
|
|
150
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
151
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
152
|
-
parts.push({
|
|
153
|
-
type: "tool-call",
|
|
154
|
-
toolCallId: toolCall.id,
|
|
155
|
-
toolName: toolCall.function.name,
|
|
156
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
result.push({
|
|
160
|
-
role: "assistant",
|
|
161
|
-
content: parts
|
|
162
|
-
});
|
|
163
|
-
if (message.toolCalls?.length) {
|
|
164
|
-
result.push({
|
|
165
|
-
role: "tool",
|
|
166
|
-
content: message.toolCalls.map((toolCall) => ({
|
|
167
|
-
type: "tool-result",
|
|
168
|
-
toolCallId: toolCall.id,
|
|
169
|
-
toolName: toolCall.function.name,
|
|
170
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
171
|
-
}))
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
} else if (message.role === "user") {
|
|
175
|
-
result.push({
|
|
176
|
-
role: "user",
|
|
177
|
-
content: message.content || ""
|
|
178
|
-
});
|
|
179
|
-
} else if (message.role === "tool") {
|
|
180
|
-
result.push({
|
|
181
|
-
role: "tool",
|
|
182
|
-
content: [
|
|
183
|
-
{
|
|
184
|
-
type: "tool-result",
|
|
185
|
-
toolCallId: message.toolCallId,
|
|
186
|
-
toolName: "unknown",
|
|
187
|
-
result: message.content
|
|
188
|
-
}
|
|
189
|
-
]
|
|
190
|
-
});
|
|
191
|
-
}
|
|
18
|
+
function base64RuntimeContext(runtimeContext) {
|
|
19
|
+
if (runtimeContext) {
|
|
20
|
+
return btoa(JSON.stringify(runtimeContext));
|
|
192
21
|
}
|
|
193
|
-
return
|
|
22
|
+
return void 0;
|
|
23
|
+
}
|
|
24
|
+
function runtimeContextQueryString(runtimeContext) {
|
|
25
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
26
|
+
if (!runtimeContextParam) return "";
|
|
27
|
+
const searchParams = new URLSearchParams();
|
|
28
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
29
|
+
const queryString = searchParams.toString();
|
|
30
|
+
return queryString ? `?${queryString}` : "";
|
|
31
|
+
}
|
|
32
|
+
function isZodType(value) {
|
|
33
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
194
34
|
}
|
|
195
35
|
function zodToJsonSchema(zodSchema) {
|
|
196
|
-
if (!(zodSchema
|
|
36
|
+
if (!isZodType(zodSchema)) {
|
|
197
37
|
return zodSchema;
|
|
198
38
|
}
|
|
199
|
-
|
|
39
|
+
if ("toJSONSchema" in z) {
|
|
40
|
+
const fn = "toJSONSchema";
|
|
41
|
+
return z[fn].call(z, zodSchema);
|
|
42
|
+
}
|
|
43
|
+
return originalZodToJsonSchema(zodSchema, { $refStrategy: "relative" });
|
|
200
44
|
}
|
|
45
|
+
|
|
46
|
+
// src/utils/process-client-tools.ts
|
|
201
47
|
function processClientTools(clientTools) {
|
|
202
48
|
if (!clientTools) {
|
|
203
49
|
return void 0;
|
|
@@ -226,6 +72,60 @@ function processClientTools(clientTools) {
|
|
|
226
72
|
);
|
|
227
73
|
}
|
|
228
74
|
|
|
75
|
+
// src/utils/process-mastra-stream.ts
|
|
76
|
+
async function sharedProcessMastraStream({
|
|
77
|
+
stream,
|
|
78
|
+
onChunk
|
|
79
|
+
}) {
|
|
80
|
+
const reader = stream.getReader();
|
|
81
|
+
const decoder = new TextDecoder();
|
|
82
|
+
let buffer = "";
|
|
83
|
+
try {
|
|
84
|
+
while (true) {
|
|
85
|
+
const { done, value } = await reader.read();
|
|
86
|
+
if (done) break;
|
|
87
|
+
buffer += decoder.decode(value, { stream: true });
|
|
88
|
+
const lines = buffer.split("\n\n");
|
|
89
|
+
buffer = lines.pop() || "";
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
if (line.startsWith("data: ")) {
|
|
92
|
+
const data = line.slice(6);
|
|
93
|
+
if (data === "[DONE]") {
|
|
94
|
+
console.info("\u{1F3C1} Stream finished");
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const json = JSON.parse(data);
|
|
99
|
+
await onChunk(json);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} finally {
|
|
107
|
+
reader.releaseLock();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async function processMastraNetworkStream({
|
|
111
|
+
stream,
|
|
112
|
+
onChunk
|
|
113
|
+
}) {
|
|
114
|
+
return sharedProcessMastraStream({
|
|
115
|
+
stream,
|
|
116
|
+
onChunk
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async function processMastraStream({
|
|
120
|
+
stream,
|
|
121
|
+
onChunk
|
|
122
|
+
}) {
|
|
123
|
+
return sharedProcessMastraStream({
|
|
124
|
+
stream,
|
|
125
|
+
onChunk
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
229
129
|
// src/resources/base.ts
|
|
230
130
|
var BaseResource = class {
|
|
231
131
|
options;
|
|
@@ -240,7 +140,7 @@ var BaseResource = class {
|
|
|
240
140
|
*/
|
|
241
141
|
async request(path, options = {}) {
|
|
242
142
|
let lastError = null;
|
|
243
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
143
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
244
144
|
let delay = backoffMs;
|
|
245
145
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
246
146
|
try {
|
|
@@ -254,6 +154,7 @@ var BaseResource = class {
|
|
|
254
154
|
// 'x-mastra-client-type': 'js',
|
|
255
155
|
},
|
|
256
156
|
signal: this.options.abortSignal,
|
|
157
|
+
credentials: options.credentials ?? credentials,
|
|
257
158
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
258
159
|
});
|
|
259
160
|
if (!response.ok) {
|
|
@@ -286,14 +187,62 @@ var BaseResource = class {
|
|
|
286
187
|
throw lastError || new Error("Request failed");
|
|
287
188
|
}
|
|
288
189
|
};
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
190
|
+
|
|
191
|
+
// src/resources/agent.ts
|
|
192
|
+
async function executeToolCallAndRespond({
|
|
193
|
+
response,
|
|
194
|
+
params,
|
|
195
|
+
runId,
|
|
196
|
+
resourceId,
|
|
197
|
+
threadId,
|
|
198
|
+
runtimeContext,
|
|
199
|
+
respondFn
|
|
200
|
+
}) {
|
|
201
|
+
if (response.finishReason === "tool-calls") {
|
|
202
|
+
const toolCalls = response.toolCalls;
|
|
203
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
204
|
+
return response;
|
|
205
|
+
}
|
|
206
|
+
for (const toolCall of toolCalls) {
|
|
207
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
208
|
+
if (clientTool && clientTool.execute) {
|
|
209
|
+
const result = await clientTool.execute(
|
|
210
|
+
{
|
|
211
|
+
context: toolCall?.args,
|
|
212
|
+
runId,
|
|
213
|
+
resourceId,
|
|
214
|
+
threadId,
|
|
215
|
+
runtimeContext,
|
|
216
|
+
tracingContext: { currentSpan: void 0 },
|
|
217
|
+
suspend: async () => {
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
messages: response.messages,
|
|
222
|
+
toolCallId: toolCall?.toolCallId
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
const updatedMessages = [
|
|
226
|
+
...response.response.messages || [],
|
|
227
|
+
{
|
|
228
|
+
role: "tool",
|
|
229
|
+
content: [
|
|
230
|
+
{
|
|
231
|
+
type: "tool-result",
|
|
232
|
+
toolCallId: toolCall.toolCallId,
|
|
233
|
+
toolName: toolCall.toolName,
|
|
234
|
+
result
|
|
235
|
+
}
|
|
236
|
+
]
|
|
237
|
+
}
|
|
238
|
+
];
|
|
239
|
+
return respondFn({
|
|
240
|
+
...params,
|
|
241
|
+
messages: updatedMessages
|
|
242
|
+
});
|
|
243
|
+
}
|
|
293
244
|
}
|
|
294
|
-
return runtimeContext;
|
|
295
245
|
}
|
|
296
|
-
return void 0;
|
|
297
246
|
}
|
|
298
247
|
var AgentVoice = class extends BaseResource {
|
|
299
248
|
constructor(options, agentId) {
|
|
@@ -336,17 +285,21 @@ var AgentVoice = class extends BaseResource {
|
|
|
336
285
|
}
|
|
337
286
|
/**
|
|
338
287
|
* Get available speakers for the agent's voice provider
|
|
288
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
289
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
339
290
|
* @returns Promise containing list of available speakers
|
|
340
291
|
*/
|
|
341
|
-
getSpeakers() {
|
|
342
|
-
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
292
|
+
getSpeakers(runtimeContext) {
|
|
293
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers${runtimeContextQueryString(runtimeContext)}`);
|
|
343
294
|
}
|
|
344
295
|
/**
|
|
345
296
|
* Get the listener configuration for the agent's voice provider
|
|
297
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
298
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
346
299
|
* @returns Promise containing a check if the agent has listening capabilities
|
|
347
300
|
*/
|
|
348
|
-
getListener() {
|
|
349
|
-
return this.request(`/api/agents/${this.agentId}/voice/listener`);
|
|
301
|
+
getListener(runtimeContext) {
|
|
302
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener${runtimeContextQueryString(runtimeContext)}`);
|
|
350
303
|
}
|
|
351
304
|
};
|
|
352
305
|
var Agent = class extends BaseResource {
|
|
@@ -358,12 +311,13 @@ var Agent = class extends BaseResource {
|
|
|
358
311
|
voice;
|
|
359
312
|
/**
|
|
360
313
|
* Retrieves details about the agent
|
|
314
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
361
315
|
* @returns Promise containing agent details including model and instructions
|
|
362
316
|
*/
|
|
363
|
-
details() {
|
|
364
|
-
return this.request(`/api/agents/${this.agentId}`);
|
|
317
|
+
details(runtimeContext) {
|
|
318
|
+
return this.request(`/api/agents/${this.agentId}${runtimeContextQueryString(runtimeContext)}`);
|
|
365
319
|
}
|
|
366
|
-
async
|
|
320
|
+
async generateLegacy(params) {
|
|
367
321
|
const processedParams = {
|
|
368
322
|
...params,
|
|
369
323
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -373,7 +327,7 @@ var Agent = class extends BaseResource {
|
|
|
373
327
|
};
|
|
374
328
|
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
375
329
|
const response = await this.request(
|
|
376
|
-
`/api/agents/${this.agentId}/generate`,
|
|
330
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
377
331
|
{
|
|
378
332
|
method: "POST",
|
|
379
333
|
body: processedParams
|
|
@@ -388,17 +342,22 @@ var Agent = class extends BaseResource {
|
|
|
388
342
|
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
389
343
|
if (clientTool && clientTool.execute) {
|
|
390
344
|
const result = await clientTool.execute(
|
|
391
|
-
{
|
|
345
|
+
{
|
|
346
|
+
context: toolCall?.args,
|
|
347
|
+
runId,
|
|
348
|
+
resourceId,
|
|
349
|
+
threadId,
|
|
350
|
+
runtimeContext,
|
|
351
|
+
tracingContext: { currentSpan: void 0 },
|
|
352
|
+
suspend: async () => {
|
|
353
|
+
}
|
|
354
|
+
},
|
|
392
355
|
{
|
|
393
356
|
messages: response.messages,
|
|
394
357
|
toolCallId: toolCall?.toolCallId
|
|
395
358
|
}
|
|
396
359
|
);
|
|
397
360
|
const updatedMessages = [
|
|
398
|
-
{
|
|
399
|
-
role: "user",
|
|
400
|
-
content: params.messages
|
|
401
|
-
},
|
|
402
361
|
...response.response.messages,
|
|
403
362
|
{
|
|
404
363
|
role: "tool",
|
|
@@ -421,6 +380,47 @@ var Agent = class extends BaseResource {
|
|
|
421
380
|
}
|
|
422
381
|
return response;
|
|
423
382
|
}
|
|
383
|
+
async generate(messagesOrParams, options) {
|
|
384
|
+
let params;
|
|
385
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
386
|
+
params = messagesOrParams;
|
|
387
|
+
} else {
|
|
388
|
+
params = {
|
|
389
|
+
messages: messagesOrParams,
|
|
390
|
+
...options
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
const processedParams = {
|
|
394
|
+
...params,
|
|
395
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
396
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
397
|
+
clientTools: processClientTools(params.clientTools),
|
|
398
|
+
structuredOutput: params.structuredOutput ? {
|
|
399
|
+
...params.structuredOutput,
|
|
400
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
401
|
+
} : void 0
|
|
402
|
+
};
|
|
403
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
404
|
+
const response = await this.request(
|
|
405
|
+
`/api/agents/${this.agentId}/generate`,
|
|
406
|
+
{
|
|
407
|
+
method: "POST",
|
|
408
|
+
body: processedParams
|
|
409
|
+
}
|
|
410
|
+
);
|
|
411
|
+
if (response.finishReason === "tool-calls") {
|
|
412
|
+
return executeToolCallAndRespond({
|
|
413
|
+
response,
|
|
414
|
+
params,
|
|
415
|
+
runId,
|
|
416
|
+
resourceId,
|
|
417
|
+
threadId,
|
|
418
|
+
runtimeContext,
|
|
419
|
+
respondFn: this.generate.bind(this)
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
return response;
|
|
423
|
+
}
|
|
424
424
|
async processChatResponse({
|
|
425
425
|
stream,
|
|
426
426
|
update,
|
|
@@ -688,7 +688,7 @@ var Agent = class extends BaseResource {
|
|
|
688
688
|
* @param params - Stream parameters including prompt
|
|
689
689
|
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
690
690
|
*/
|
|
691
|
-
async
|
|
691
|
+
async streamLegacy(params) {
|
|
692
692
|
const processedParams = {
|
|
693
693
|
...params,
|
|
694
694
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -697,7 +697,7 @@ var Agent = class extends BaseResource {
|
|
|
697
697
|
clientTools: processClientTools(params.clientTools)
|
|
698
698
|
};
|
|
699
699
|
const { readable, writable } = new TransformStream();
|
|
700
|
-
const response = await this.
|
|
700
|
+
const response = await this.processStreamResponseLegacy(processedParams, writable);
|
|
701
701
|
const streamResponse = new Response(readable, {
|
|
702
702
|
status: response.status,
|
|
703
703
|
statusText: response.statusText,
|
|
@@ -711,9 +711,267 @@ var Agent = class extends BaseResource {
|
|
|
711
711
|
};
|
|
712
712
|
return streamResponse;
|
|
713
713
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
714
|
+
async processChatResponse_vNext({
|
|
715
|
+
stream,
|
|
716
|
+
update,
|
|
717
|
+
onToolCall,
|
|
718
|
+
onFinish,
|
|
719
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
720
|
+
lastMessage
|
|
721
|
+
}) {
|
|
722
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
723
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
724
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
725
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
726
|
+
}, 0) ?? 0) : 0;
|
|
727
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
728
|
+
id: v4(),
|
|
729
|
+
createdAt: getCurrentDate(),
|
|
730
|
+
role: "assistant",
|
|
731
|
+
content: "",
|
|
732
|
+
parts: []
|
|
733
|
+
};
|
|
734
|
+
let currentTextPart = void 0;
|
|
735
|
+
let currentReasoningPart = void 0;
|
|
736
|
+
let currentReasoningTextDetail = void 0;
|
|
737
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
738
|
+
const part = message.parts.find(
|
|
739
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
740
|
+
);
|
|
741
|
+
if (part != null) {
|
|
742
|
+
part.toolInvocation = invocation;
|
|
743
|
+
} else {
|
|
744
|
+
message.parts.push({
|
|
745
|
+
type: "tool-invocation",
|
|
746
|
+
toolInvocation: invocation
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
const data = [];
|
|
751
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
752
|
+
const partialToolCalls = {};
|
|
753
|
+
let usage = {
|
|
754
|
+
completionTokens: NaN,
|
|
755
|
+
promptTokens: NaN,
|
|
756
|
+
totalTokens: NaN
|
|
757
|
+
};
|
|
758
|
+
let finishReason = "unknown";
|
|
759
|
+
function execUpdate() {
|
|
760
|
+
const copiedData = [...data];
|
|
761
|
+
if (messageAnnotations?.length) {
|
|
762
|
+
message.annotations = messageAnnotations;
|
|
763
|
+
}
|
|
764
|
+
const copiedMessage = {
|
|
765
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
766
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
767
|
+
...structuredClone(message),
|
|
768
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
769
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
770
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
771
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
772
|
+
// forwarded to rendering):
|
|
773
|
+
revisionId: v4()
|
|
774
|
+
};
|
|
775
|
+
update({
|
|
776
|
+
message: copiedMessage,
|
|
777
|
+
data: copiedData,
|
|
778
|
+
replaceLastMessage
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
await processMastraStream({
|
|
782
|
+
stream,
|
|
783
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
784
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
785
|
+
onChunk: async (chunk) => {
|
|
786
|
+
switch (chunk.type) {
|
|
787
|
+
case "tripwire": {
|
|
788
|
+
message.parts.push({
|
|
789
|
+
type: "text",
|
|
790
|
+
text: chunk.payload.tripwireReason
|
|
791
|
+
});
|
|
792
|
+
execUpdate();
|
|
793
|
+
break;
|
|
794
|
+
}
|
|
795
|
+
case "step-start": {
|
|
796
|
+
if (!replaceLastMessage) {
|
|
797
|
+
message.id = chunk.payload.messageId;
|
|
798
|
+
}
|
|
799
|
+
message.parts.push({ type: "step-start" });
|
|
800
|
+
execUpdate();
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
case "text-delta": {
|
|
804
|
+
if (currentTextPart == null) {
|
|
805
|
+
currentTextPart = {
|
|
806
|
+
type: "text",
|
|
807
|
+
text: chunk.payload.text
|
|
808
|
+
};
|
|
809
|
+
message.parts.push(currentTextPart);
|
|
810
|
+
} else {
|
|
811
|
+
currentTextPart.text += chunk.payload.text;
|
|
812
|
+
}
|
|
813
|
+
message.content += chunk.payload.text;
|
|
814
|
+
execUpdate();
|
|
815
|
+
break;
|
|
816
|
+
}
|
|
817
|
+
case "reasoning-delta": {
|
|
818
|
+
if (currentReasoningTextDetail == null) {
|
|
819
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
820
|
+
if (currentReasoningPart != null) {
|
|
821
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
822
|
+
}
|
|
823
|
+
} else {
|
|
824
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
825
|
+
}
|
|
826
|
+
if (currentReasoningPart == null) {
|
|
827
|
+
currentReasoningPart = {
|
|
828
|
+
type: "reasoning",
|
|
829
|
+
reasoning: chunk.payload.text,
|
|
830
|
+
details: [currentReasoningTextDetail]
|
|
831
|
+
};
|
|
832
|
+
message.parts.push(currentReasoningPart);
|
|
833
|
+
} else {
|
|
834
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
835
|
+
}
|
|
836
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
837
|
+
execUpdate();
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
case "file": {
|
|
841
|
+
message.parts.push({
|
|
842
|
+
type: "file",
|
|
843
|
+
mimeType: chunk.payload.mimeType,
|
|
844
|
+
data: chunk.payload.data
|
|
845
|
+
});
|
|
846
|
+
execUpdate();
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
case "source": {
|
|
850
|
+
message.parts.push({
|
|
851
|
+
type: "source",
|
|
852
|
+
source: chunk.payload.source
|
|
853
|
+
});
|
|
854
|
+
execUpdate();
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
case "tool-call": {
|
|
858
|
+
const invocation = {
|
|
859
|
+
state: "call",
|
|
860
|
+
step,
|
|
861
|
+
...chunk.payload
|
|
862
|
+
};
|
|
863
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
864
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
865
|
+
} else {
|
|
866
|
+
if (message.toolInvocations == null) {
|
|
867
|
+
message.toolInvocations = [];
|
|
868
|
+
}
|
|
869
|
+
message.toolInvocations.push(invocation);
|
|
870
|
+
}
|
|
871
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
872
|
+
execUpdate();
|
|
873
|
+
if (onToolCall) {
|
|
874
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
875
|
+
if (result != null) {
|
|
876
|
+
const invocation2 = {
|
|
877
|
+
state: "result",
|
|
878
|
+
step,
|
|
879
|
+
...chunk.payload,
|
|
880
|
+
result
|
|
881
|
+
};
|
|
882
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
883
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
884
|
+
execUpdate();
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
case "tool-call-input-streaming-start": {
|
|
889
|
+
if (message.toolInvocations == null) {
|
|
890
|
+
message.toolInvocations = [];
|
|
891
|
+
}
|
|
892
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
893
|
+
text: "",
|
|
894
|
+
step,
|
|
895
|
+
toolName: chunk.payload.toolName,
|
|
896
|
+
index: message.toolInvocations.length
|
|
897
|
+
};
|
|
898
|
+
const invocation = {
|
|
899
|
+
state: "partial-call",
|
|
900
|
+
step,
|
|
901
|
+
toolCallId: chunk.payload.toolCallId,
|
|
902
|
+
toolName: chunk.payload.toolName,
|
|
903
|
+
args: chunk.payload.args
|
|
904
|
+
};
|
|
905
|
+
message.toolInvocations.push(invocation);
|
|
906
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
907
|
+
execUpdate();
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
case "tool-call-delta": {
|
|
911
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
912
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
913
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
914
|
+
const invocation = {
|
|
915
|
+
state: "partial-call",
|
|
916
|
+
step: partialToolCall.step,
|
|
917
|
+
toolCallId: chunk.payload.toolCallId,
|
|
918
|
+
toolName: partialToolCall.toolName,
|
|
919
|
+
args: partialArgs
|
|
920
|
+
};
|
|
921
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
922
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
923
|
+
execUpdate();
|
|
924
|
+
break;
|
|
925
|
+
}
|
|
926
|
+
case "tool-result": {
|
|
927
|
+
const toolInvocations = message.toolInvocations;
|
|
928
|
+
if (toolInvocations == null) {
|
|
929
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
930
|
+
}
|
|
931
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
932
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
933
|
+
);
|
|
934
|
+
if (toolInvocationIndex === -1) {
|
|
935
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
936
|
+
}
|
|
937
|
+
const invocation = {
|
|
938
|
+
...toolInvocations[toolInvocationIndex],
|
|
939
|
+
state: "result",
|
|
940
|
+
...chunk.payload
|
|
941
|
+
};
|
|
942
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
943
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
944
|
+
execUpdate();
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
case "error": {
|
|
948
|
+
throw new Error(chunk.payload.error);
|
|
949
|
+
}
|
|
950
|
+
case "data": {
|
|
951
|
+
data.push(...chunk.payload.data);
|
|
952
|
+
execUpdate();
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
case "step-finish": {
|
|
956
|
+
step += 1;
|
|
957
|
+
currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
|
|
958
|
+
currentReasoningPart = void 0;
|
|
959
|
+
currentReasoningTextDetail = void 0;
|
|
960
|
+
execUpdate();
|
|
961
|
+
break;
|
|
962
|
+
}
|
|
963
|
+
case "finish": {
|
|
964
|
+
finishReason = chunk.payload.stepResult.reason;
|
|
965
|
+
if (chunk.payload.usage != null) {
|
|
966
|
+
usage = chunk.payload.usage;
|
|
967
|
+
}
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
});
|
|
973
|
+
onFinish?.({ message, finishReason, usage });
|
|
974
|
+
}
|
|
717
975
|
async processStreamResponse(processedParams, writable) {
|
|
718
976
|
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
719
977
|
method: "POST",
|
|
@@ -727,12 +985,30 @@ var Agent = class extends BaseResource {
|
|
|
727
985
|
let toolCalls = [];
|
|
728
986
|
let messages = [];
|
|
729
987
|
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
730
|
-
streamForWritable.pipeTo(
|
|
731
|
-
|
|
732
|
-
|
|
988
|
+
streamForWritable.pipeTo(
|
|
989
|
+
new WritableStream({
|
|
990
|
+
async write(chunk) {
|
|
991
|
+
let writer;
|
|
992
|
+
try {
|
|
993
|
+
writer = writable.getWriter();
|
|
994
|
+
const text = new TextDecoder().decode(chunk);
|
|
995
|
+
const lines = text.split("\n\n");
|
|
996
|
+
const readableLines = lines.filter((line) => line !== "[DONE]").join("\n\n");
|
|
997
|
+
await writer.write(new TextEncoder().encode(readableLines));
|
|
998
|
+
} catch {
|
|
999
|
+
await writer?.write(chunk);
|
|
1000
|
+
} finally {
|
|
1001
|
+
writer?.releaseLock();
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}),
|
|
1005
|
+
{
|
|
1006
|
+
preventClose: true
|
|
1007
|
+
}
|
|
1008
|
+
).catch((error) => {
|
|
733
1009
|
console.error("Error piping to writable stream:", error);
|
|
734
1010
|
});
|
|
735
|
-
this.
|
|
1011
|
+
this.processChatResponse_vNext({
|
|
736
1012
|
stream: streamForProcessing,
|
|
737
1013
|
update: ({ message }) => {
|
|
738
1014
|
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
@@ -748,23 +1024,30 @@ var Agent = class extends BaseResource {
|
|
|
748
1024
|
if (toolCall) {
|
|
749
1025
|
toolCalls.push(toolCall);
|
|
750
1026
|
}
|
|
1027
|
+
let shouldExecuteClientTool = false;
|
|
751
1028
|
for (const toolCall2 of toolCalls) {
|
|
752
1029
|
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
753
1030
|
if (clientTool && clientTool.execute) {
|
|
1031
|
+
shouldExecuteClientTool = true;
|
|
754
1032
|
const result = await clientTool.execute(
|
|
755
1033
|
{
|
|
756
1034
|
context: toolCall2?.args,
|
|
757
1035
|
runId: processedParams.runId,
|
|
758
1036
|
resourceId: processedParams.resourceId,
|
|
759
1037
|
threadId: processedParams.threadId,
|
|
760
|
-
runtimeContext: processedParams.runtimeContext
|
|
1038
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1039
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1040
|
+
tracingContext: { currentSpan: void 0 },
|
|
1041
|
+
suspend: async () => {
|
|
1042
|
+
}
|
|
761
1043
|
},
|
|
762
1044
|
{
|
|
763
1045
|
messages: response.messages,
|
|
764
1046
|
toolCallId: toolCall2?.toolCallId
|
|
765
1047
|
}
|
|
766
1048
|
);
|
|
767
|
-
const
|
|
1049
|
+
const lastMessageRaw = messages[messages.length - 1];
|
|
1050
|
+
const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
|
|
768
1051
|
const toolInvocationPart = lastMessage?.parts?.find(
|
|
769
1052
|
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
770
1053
|
);
|
|
@@ -782,10 +1065,178 @@ var Agent = class extends BaseResource {
|
|
|
782
1065
|
toolInvocation.state = "result";
|
|
783
1066
|
toolInvocation.result = result;
|
|
784
1067
|
}
|
|
785
|
-
const
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1068
|
+
const updatedMessages = lastMessage != null ? [...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messages];
|
|
1069
|
+
this.processStreamResponse(
|
|
1070
|
+
{
|
|
1071
|
+
...processedParams,
|
|
1072
|
+
messages: updatedMessages
|
|
1073
|
+
},
|
|
1074
|
+
writable
|
|
1075
|
+
).catch((error) => {
|
|
1076
|
+
console.error("Error processing stream response:", error);
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
if (!shouldExecuteClientTool) {
|
|
1081
|
+
setTimeout(() => {
|
|
1082
|
+
writable.close();
|
|
1083
|
+
}, 0);
|
|
1084
|
+
}
|
|
1085
|
+
} else {
|
|
1086
|
+
setTimeout(() => {
|
|
1087
|
+
writable.close();
|
|
1088
|
+
}, 0);
|
|
1089
|
+
}
|
|
1090
|
+
},
|
|
1091
|
+
lastMessage: void 0
|
|
1092
|
+
}).catch((error) => {
|
|
1093
|
+
console.error("Error processing stream response:", error);
|
|
1094
|
+
});
|
|
1095
|
+
} catch (error) {
|
|
1096
|
+
console.error("Error processing stream response:", error);
|
|
1097
|
+
}
|
|
1098
|
+
return response;
|
|
1099
|
+
}
|
|
1100
|
+
async network(params) {
|
|
1101
|
+
const response = await this.request(`/api/agents/${this.agentId}/network`, {
|
|
1102
|
+
method: "POST",
|
|
1103
|
+
body: params,
|
|
1104
|
+
stream: true
|
|
1105
|
+
});
|
|
1106
|
+
if (!response.body) {
|
|
1107
|
+
throw new Error("No response body");
|
|
1108
|
+
}
|
|
1109
|
+
const streamResponse = new Response(response.body, {
|
|
1110
|
+
status: response.status,
|
|
1111
|
+
statusText: response.statusText,
|
|
1112
|
+
headers: response.headers
|
|
1113
|
+
});
|
|
1114
|
+
streamResponse.processDataStream = async ({
|
|
1115
|
+
onChunk
|
|
1116
|
+
}) => {
|
|
1117
|
+
await processMastraNetworkStream({
|
|
1118
|
+
stream: streamResponse.body,
|
|
1119
|
+
onChunk
|
|
1120
|
+
});
|
|
1121
|
+
};
|
|
1122
|
+
return streamResponse;
|
|
1123
|
+
}
|
|
1124
|
+
async stream(messagesOrParams, options) {
|
|
1125
|
+
let params;
|
|
1126
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
1127
|
+
params = messagesOrParams;
|
|
1128
|
+
} else {
|
|
1129
|
+
params = {
|
|
1130
|
+
messages: messagesOrParams,
|
|
1131
|
+
...options
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
const processedParams = {
|
|
1135
|
+
...params,
|
|
1136
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
1137
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
1138
|
+
clientTools: processClientTools(params.clientTools),
|
|
1139
|
+
structuredOutput: params.structuredOutput ? {
|
|
1140
|
+
...params.structuredOutput,
|
|
1141
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
1142
|
+
} : void 0
|
|
1143
|
+
};
|
|
1144
|
+
const { readable, writable } = new TransformStream();
|
|
1145
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
1146
|
+
const streamResponse = new Response(readable, {
|
|
1147
|
+
status: response.status,
|
|
1148
|
+
statusText: response.statusText,
|
|
1149
|
+
headers: response.headers
|
|
1150
|
+
});
|
|
1151
|
+
streamResponse.processDataStream = async ({
|
|
1152
|
+
onChunk
|
|
1153
|
+
}) => {
|
|
1154
|
+
await processMastraStream({
|
|
1155
|
+
stream: streamResponse.body,
|
|
1156
|
+
onChunk
|
|
1157
|
+
});
|
|
1158
|
+
};
|
|
1159
|
+
return streamResponse;
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Processes the stream response and handles tool calls
|
|
1163
|
+
*/
|
|
1164
|
+
async processStreamResponseLegacy(processedParams, writable) {
|
|
1165
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
1166
|
+
method: "POST",
|
|
1167
|
+
body: processedParams,
|
|
1168
|
+
stream: true
|
|
1169
|
+
});
|
|
1170
|
+
if (!response.body) {
|
|
1171
|
+
throw new Error("No response body");
|
|
1172
|
+
}
|
|
1173
|
+
try {
|
|
1174
|
+
let toolCalls = [];
|
|
1175
|
+
let messages = [];
|
|
1176
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1177
|
+
streamForWritable.pipeTo(writable, {
|
|
1178
|
+
preventClose: true
|
|
1179
|
+
}).catch((error) => {
|
|
1180
|
+
console.error("Error piping to writable stream:", error);
|
|
1181
|
+
});
|
|
1182
|
+
this.processChatResponse({
|
|
1183
|
+
stream: streamForProcessing,
|
|
1184
|
+
update: ({ message }) => {
|
|
1185
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1186
|
+
if (existingIndex !== -1) {
|
|
1187
|
+
messages[existingIndex] = message;
|
|
1188
|
+
} else {
|
|
1189
|
+
messages.push(message);
|
|
1190
|
+
}
|
|
1191
|
+
},
|
|
1192
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1193
|
+
if (finishReason === "tool-calls") {
|
|
1194
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1195
|
+
if (toolCall) {
|
|
1196
|
+
toolCalls.push(toolCall);
|
|
1197
|
+
}
|
|
1198
|
+
for (const toolCall2 of toolCalls) {
|
|
1199
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1200
|
+
if (clientTool && clientTool.execute) {
|
|
1201
|
+
const result = await clientTool.execute(
|
|
1202
|
+
{
|
|
1203
|
+
context: toolCall2?.args,
|
|
1204
|
+
runId: processedParams.runId,
|
|
1205
|
+
resourceId: processedParams.resourceId,
|
|
1206
|
+
threadId: processedParams.threadId,
|
|
1207
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1208
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1209
|
+
tracingContext: { currentSpan: void 0 },
|
|
1210
|
+
suspend: async () => {
|
|
1211
|
+
}
|
|
1212
|
+
},
|
|
1213
|
+
{
|
|
1214
|
+
messages: response.messages,
|
|
1215
|
+
toolCallId: toolCall2?.toolCallId
|
|
1216
|
+
}
|
|
1217
|
+
);
|
|
1218
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1219
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1220
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1221
|
+
);
|
|
1222
|
+
if (toolInvocationPart) {
|
|
1223
|
+
toolInvocationPart.toolInvocation = {
|
|
1224
|
+
...toolInvocationPart.toolInvocation,
|
|
1225
|
+
state: "result",
|
|
1226
|
+
result
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1230
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1231
|
+
);
|
|
1232
|
+
if (toolInvocation) {
|
|
1233
|
+
toolInvocation.state = "result";
|
|
1234
|
+
toolInvocation.result = result;
|
|
1235
|
+
}
|
|
1236
|
+
const writer = writable.getWriter();
|
|
1237
|
+
try {
|
|
1238
|
+
await writer.write(
|
|
1239
|
+
new TextEncoder().encode(
|
|
789
1240
|
"a:" + JSON.stringify({
|
|
790
1241
|
toolCallId: toolCall2.toolCallId,
|
|
791
1242
|
result
|
|
@@ -795,12 +1246,10 @@ var Agent = class extends BaseResource {
|
|
|
795
1246
|
} finally {
|
|
796
1247
|
writer.releaseLock();
|
|
797
1248
|
}
|
|
798
|
-
|
|
799
|
-
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
800
|
-
this.processStreamResponse(
|
|
1249
|
+
this.processStreamResponseLegacy(
|
|
801
1250
|
{
|
|
802
1251
|
...processedParams,
|
|
803
|
-
messages: [...
|
|
1252
|
+
messages: [...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
804
1253
|
},
|
|
805
1254
|
writable
|
|
806
1255
|
).catch((error) => {
|
|
@@ -826,10 +1275,11 @@ var Agent = class extends BaseResource {
|
|
|
826
1275
|
/**
|
|
827
1276
|
* Gets details about a specific tool available to the agent
|
|
828
1277
|
* @param toolId - ID of the tool to retrieve
|
|
1278
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
829
1279
|
* @returns Promise containing tool details
|
|
830
1280
|
*/
|
|
831
|
-
getTool(toolId) {
|
|
832
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
1281
|
+
getTool(toolId, runtimeContext) {
|
|
1282
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
833
1283
|
}
|
|
834
1284
|
/**
|
|
835
1285
|
* Executes a tool for the agent
|
|
@@ -840,7 +1290,7 @@ var Agent = class extends BaseResource {
|
|
|
840
1290
|
executeTool(toolId, params) {
|
|
841
1291
|
const body = {
|
|
842
1292
|
data: params.data,
|
|
843
|
-
runtimeContext:
|
|
1293
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
844
1294
|
};
|
|
845
1295
|
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
846
1296
|
method: "POST",
|
|
@@ -849,73 +1299,58 @@ var Agent = class extends BaseResource {
|
|
|
849
1299
|
}
|
|
850
1300
|
/**
|
|
851
1301
|
* Retrieves evaluation results for the agent
|
|
1302
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
852
1303
|
* @returns Promise containing agent evaluations
|
|
853
1304
|
*/
|
|
854
|
-
evals() {
|
|
855
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
1305
|
+
evals(runtimeContext) {
|
|
1306
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci${runtimeContextQueryString(runtimeContext)}`);
|
|
856
1307
|
}
|
|
857
1308
|
/**
|
|
858
1309
|
* Retrieves live evaluation results for the agent
|
|
1310
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
859
1311
|
* @returns Promise containing live agent evaluations
|
|
860
1312
|
*/
|
|
861
|
-
liveEvals() {
|
|
862
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
863
|
-
}
|
|
864
|
-
};
|
|
865
|
-
var Network = class extends BaseResource {
|
|
866
|
-
constructor(options, networkId) {
|
|
867
|
-
super(options);
|
|
868
|
-
this.networkId = networkId;
|
|
1313
|
+
liveEvals(runtimeContext) {
|
|
1314
|
+
return this.request(`/api/agents/${this.agentId}/evals/live${runtimeContextQueryString(runtimeContext)}`);
|
|
869
1315
|
}
|
|
870
1316
|
/**
|
|
871
|
-
*
|
|
872
|
-
* @
|
|
1317
|
+
* Updates the model for the agent
|
|
1318
|
+
* @param params - Parameters for updating the model
|
|
1319
|
+
* @returns Promise containing the updated model
|
|
873
1320
|
*/
|
|
874
|
-
|
|
875
|
-
return this.request(`/api/
|
|
1321
|
+
updateModel(params) {
|
|
1322
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1323
|
+
method: "POST",
|
|
1324
|
+
body: params
|
|
1325
|
+
});
|
|
876
1326
|
}
|
|
877
1327
|
/**
|
|
878
|
-
*
|
|
879
|
-
* @param params -
|
|
880
|
-
* @returns Promise containing the
|
|
1328
|
+
* Updates the model for the agent in the model list
|
|
1329
|
+
* @param params - Parameters for updating the model
|
|
1330
|
+
* @returns Promise containing the updated model
|
|
881
1331
|
*/
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
...params,
|
|
885
|
-
output: zodToJsonSchema(params.output),
|
|
886
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
887
|
-
};
|
|
888
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
1332
|
+
updateModelInModelList({ modelConfigId, ...params }) {
|
|
1333
|
+
return this.request(`/api/agents/${this.agentId}/models/${modelConfigId}`, {
|
|
889
1334
|
method: "POST",
|
|
890
|
-
body:
|
|
1335
|
+
body: params
|
|
891
1336
|
});
|
|
892
1337
|
}
|
|
893
1338
|
/**
|
|
894
|
-
*
|
|
895
|
-
* @param params -
|
|
896
|
-
* @returns Promise containing the
|
|
1339
|
+
* Reorders the models for the agent
|
|
1340
|
+
* @param params - Parameters for reordering the model list
|
|
1341
|
+
* @returns Promise containing the updated model list
|
|
897
1342
|
*/
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
...params,
|
|
901
|
-
output: zodToJsonSchema(params.output),
|
|
902
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
903
|
-
};
|
|
904
|
-
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1343
|
+
reorderModelList(params) {
|
|
1344
|
+
return this.request(`/api/agents/${this.agentId}/models/reorder`, {
|
|
905
1345
|
method: "POST",
|
|
906
|
-
body:
|
|
907
|
-
stream: true
|
|
1346
|
+
body: params
|
|
908
1347
|
});
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
...options
|
|
916
|
-
});
|
|
917
|
-
};
|
|
918
|
-
return response;
|
|
1348
|
+
}
|
|
1349
|
+
async generateVNext(_messagesOrParams, _options) {
|
|
1350
|
+
throw new Error("generateVNext has been renamed to generate. Please use generate instead.");
|
|
1351
|
+
}
|
|
1352
|
+
async streamVNext(_messagesOrParams, _options) {
|
|
1353
|
+
throw new Error("streamVNext has been renamed to stream. Please use stream instead.");
|
|
919
1354
|
}
|
|
920
1355
|
};
|
|
921
1356
|
|
|
@@ -1006,10 +1441,13 @@ var Vector = class extends BaseResource {
|
|
|
1006
1441
|
/**
|
|
1007
1442
|
* Retrieves details about a specific vector index
|
|
1008
1443
|
* @param indexName - Name of the index to get details for
|
|
1444
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1009
1445
|
* @returns Promise containing vector index details
|
|
1010
1446
|
*/
|
|
1011
|
-
details(indexName) {
|
|
1012
|
-
return this.request(
|
|
1447
|
+
details(indexName, runtimeContext) {
|
|
1448
|
+
return this.request(
|
|
1449
|
+
`/api/vector/${this.vectorName}/indexes/${indexName}${runtimeContextQueryString(runtimeContext)}`
|
|
1450
|
+
);
|
|
1013
1451
|
}
|
|
1014
1452
|
/**
|
|
1015
1453
|
* Deletes a vector index
|
|
@@ -1023,10 +1461,11 @@ var Vector = class extends BaseResource {
|
|
|
1023
1461
|
}
|
|
1024
1462
|
/**
|
|
1025
1463
|
* Retrieves a list of all available indexes
|
|
1464
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1026
1465
|
* @returns Promise containing array of index names
|
|
1027
1466
|
*/
|
|
1028
|
-
getIndexes() {
|
|
1029
|
-
return this.request(`/api/vector/${this.vectorName}/indexes`);
|
|
1467
|
+
getIndexes(runtimeContext) {
|
|
1468
|
+
return this.request(`/api/vector/${this.vectorName}/indexes${runtimeContextQueryString(runtimeContext)}`);
|
|
1030
1469
|
}
|
|
1031
1470
|
/**
|
|
1032
1471
|
* Creates a new vector index
|
|
@@ -1063,26 +1502,109 @@ var Vector = class extends BaseResource {
|
|
|
1063
1502
|
}
|
|
1064
1503
|
};
|
|
1065
1504
|
|
|
1066
|
-
// src/resources/
|
|
1505
|
+
// src/resources/tool.ts
|
|
1506
|
+
var Tool = class extends BaseResource {
|
|
1507
|
+
constructor(options, toolId) {
|
|
1508
|
+
super(options);
|
|
1509
|
+
this.toolId = toolId;
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* Retrieves details about the tool
|
|
1513
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1514
|
+
* @returns Promise containing tool details including description and schemas
|
|
1515
|
+
*/
|
|
1516
|
+
details(runtimeContext) {
|
|
1517
|
+
return this.request(`/api/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Executes the tool with the provided parameters
|
|
1521
|
+
* @param params - Parameters required for tool execution
|
|
1522
|
+
* @returns Promise containing the tool execution results
|
|
1523
|
+
*/
|
|
1524
|
+
execute(params) {
|
|
1525
|
+
const url = new URLSearchParams();
|
|
1526
|
+
if (params.runId) {
|
|
1527
|
+
url.set("runId", params.runId);
|
|
1528
|
+
}
|
|
1529
|
+
const body = {
|
|
1530
|
+
data: params.data,
|
|
1531
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1532
|
+
};
|
|
1533
|
+
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
1534
|
+
method: "POST",
|
|
1535
|
+
body
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
|
|
1540
|
+
// src/resources/workflow.ts
|
|
1067
1541
|
var RECORD_SEPARATOR = "";
|
|
1068
|
-
var
|
|
1542
|
+
var Workflow = class extends BaseResource {
|
|
1069
1543
|
constructor(options, workflowId) {
|
|
1070
1544
|
super(options);
|
|
1071
1545
|
this.workflowId = workflowId;
|
|
1072
1546
|
}
|
|
1073
1547
|
/**
|
|
1074
|
-
*
|
|
1075
|
-
*
|
|
1548
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
1549
|
+
* separated by the Record Separator character (\x1E)
|
|
1550
|
+
*
|
|
1551
|
+
* @param stream - The readable stream to process
|
|
1552
|
+
* @returns An async generator that yields parsed records
|
|
1553
|
+
*/
|
|
1554
|
+
async *streamProcessor(stream) {
|
|
1555
|
+
const reader = stream.getReader();
|
|
1556
|
+
let doneReading = false;
|
|
1557
|
+
let buffer = "";
|
|
1558
|
+
try {
|
|
1559
|
+
while (!doneReading) {
|
|
1560
|
+
const { done, value } = await reader.read();
|
|
1561
|
+
doneReading = done;
|
|
1562
|
+
if (done && !value) continue;
|
|
1563
|
+
try {
|
|
1564
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1565
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
1566
|
+
buffer = chunks.pop() || "";
|
|
1567
|
+
for (const chunk of chunks) {
|
|
1568
|
+
if (chunk) {
|
|
1569
|
+
if (typeof chunk === "string") {
|
|
1570
|
+
try {
|
|
1571
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1572
|
+
yield parsedChunk;
|
|
1573
|
+
} catch {
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
} catch {
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
if (buffer) {
|
|
1582
|
+
try {
|
|
1583
|
+
yield JSON.parse(buffer);
|
|
1584
|
+
} catch {
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
} finally {
|
|
1588
|
+
reader.cancel().catch(() => {
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Retrieves details about the workflow
|
|
1594
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1595
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
1076
1596
|
*/
|
|
1077
|
-
details() {
|
|
1078
|
-
return this.request(`/api/workflows
|
|
1597
|
+
details(runtimeContext) {
|
|
1598
|
+
return this.request(`/api/workflows/${this.workflowId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1079
1599
|
}
|
|
1080
1600
|
/**
|
|
1081
|
-
* Retrieves all runs for a
|
|
1601
|
+
* Retrieves all runs for a workflow
|
|
1082
1602
|
* @param params - Parameters for filtering runs
|
|
1083
|
-
* @
|
|
1603
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1604
|
+
* @returns Promise containing workflow runs array
|
|
1084
1605
|
*/
|
|
1085
|
-
runs(params) {
|
|
1606
|
+
runs(params, runtimeContext) {
|
|
1607
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
1086
1608
|
const searchParams = new URLSearchParams();
|
|
1087
1609
|
if (params?.fromDate) {
|
|
1088
1610
|
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
@@ -1090,296 +1612,43 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
1090
1612
|
if (params?.toDate) {
|
|
1091
1613
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
1092
1614
|
}
|
|
1093
|
-
if (params?.limit) {
|
|
1615
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1094
1616
|
searchParams.set("limit", String(params.limit));
|
|
1095
1617
|
}
|
|
1096
|
-
if (params?.offset) {
|
|
1618
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1097
1619
|
searchParams.set("offset", String(params.offset));
|
|
1098
1620
|
}
|
|
1099
1621
|
if (params?.resourceId) {
|
|
1100
1622
|
searchParams.set("resourceId", params.resourceId);
|
|
1101
1623
|
}
|
|
1624
|
+
if (runtimeContextParam) {
|
|
1625
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
1626
|
+
}
|
|
1102
1627
|
if (searchParams.size) {
|
|
1103
|
-
return this.request(`/api/workflows
|
|
1628
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1104
1629
|
} else {
|
|
1105
|
-
return this.request(`/api/workflows
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
/**
|
|
1109
|
-
* Creates a new legacy workflow run
|
|
1110
|
-
* @returns Promise containing the generated run ID
|
|
1111
|
-
*/
|
|
1112
|
-
createRun(params) {
|
|
1113
|
-
const searchParams = new URLSearchParams();
|
|
1114
|
-
if (!!params?.runId) {
|
|
1115
|
-
searchParams.set("runId", params.runId);
|
|
1116
|
-
}
|
|
1117
|
-
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
1118
|
-
method: "POST"
|
|
1119
|
-
});
|
|
1120
|
-
}
|
|
1121
|
-
/**
|
|
1122
|
-
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
1123
|
-
* @param params - Object containing the runId and triggerData
|
|
1124
|
-
* @returns Promise containing success message
|
|
1125
|
-
*/
|
|
1126
|
-
start(params) {
|
|
1127
|
-
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1128
|
-
method: "POST",
|
|
1129
|
-
body: params?.triggerData
|
|
1130
|
-
});
|
|
1131
|
-
}
|
|
1132
|
-
/**
|
|
1133
|
-
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
1134
|
-
* @param stepId - ID of the step to resume
|
|
1135
|
-
* @param runId - ID of the legacy workflow run
|
|
1136
|
-
* @param context - Context to resume the legacy workflow with
|
|
1137
|
-
* @returns Promise containing the legacy workflow resume results
|
|
1138
|
-
*/
|
|
1139
|
-
resume({
|
|
1140
|
-
stepId,
|
|
1141
|
-
runId,
|
|
1142
|
-
context
|
|
1143
|
-
}) {
|
|
1144
|
-
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
1145
|
-
method: "POST",
|
|
1146
|
-
body: {
|
|
1147
|
-
stepId,
|
|
1148
|
-
context
|
|
1149
|
-
}
|
|
1150
|
-
});
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1154
|
-
* @param params - Object containing the optional runId and triggerData
|
|
1155
|
-
* @returns Promise containing the workflow execution results
|
|
1156
|
-
*/
|
|
1157
|
-
startAsync(params) {
|
|
1158
|
-
const searchParams = new URLSearchParams();
|
|
1159
|
-
if (!!params?.runId) {
|
|
1160
|
-
searchParams.set("runId", params.runId);
|
|
1161
|
-
}
|
|
1162
|
-
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1163
|
-
method: "POST",
|
|
1164
|
-
body: params?.triggerData
|
|
1165
|
-
});
|
|
1166
|
-
}
|
|
1167
|
-
/**
|
|
1168
|
-
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1169
|
-
* @param params - Object containing the runId, stepId, and context
|
|
1170
|
-
* @returns Promise containing the workflow resume results
|
|
1171
|
-
*/
|
|
1172
|
-
resumeAsync(params) {
|
|
1173
|
-
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1174
|
-
method: "POST",
|
|
1175
|
-
body: {
|
|
1176
|
-
stepId: params.stepId,
|
|
1177
|
-
context: params.context
|
|
1178
|
-
}
|
|
1179
|
-
});
|
|
1180
|
-
}
|
|
1181
|
-
/**
|
|
1182
|
-
* Creates an async generator that processes a readable stream and yields records
|
|
1183
|
-
* separated by the Record Separator character (\x1E)
|
|
1184
|
-
*
|
|
1185
|
-
* @param stream - The readable stream to process
|
|
1186
|
-
* @returns An async generator that yields parsed records
|
|
1187
|
-
*/
|
|
1188
|
-
async *streamProcessor(stream) {
|
|
1189
|
-
const reader = stream.getReader();
|
|
1190
|
-
let doneReading = false;
|
|
1191
|
-
let buffer = "";
|
|
1192
|
-
try {
|
|
1193
|
-
while (!doneReading) {
|
|
1194
|
-
const { done, value } = await reader.read();
|
|
1195
|
-
doneReading = done;
|
|
1196
|
-
if (done && !value) continue;
|
|
1197
|
-
try {
|
|
1198
|
-
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1199
|
-
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
1200
|
-
buffer = chunks.pop() || "";
|
|
1201
|
-
for (const chunk of chunks) {
|
|
1202
|
-
if (chunk) {
|
|
1203
|
-
if (typeof chunk === "string") {
|
|
1204
|
-
try {
|
|
1205
|
-
const parsedChunk = JSON.parse(chunk);
|
|
1206
|
-
yield parsedChunk;
|
|
1207
|
-
} catch {
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
} catch {
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
if (buffer) {
|
|
1216
|
-
try {
|
|
1217
|
-
yield JSON.parse(buffer);
|
|
1218
|
-
} catch {
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
} finally {
|
|
1222
|
-
reader.cancel().catch(() => {
|
|
1223
|
-
});
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
/**
|
|
1227
|
-
* Watches legacy workflow transitions in real-time
|
|
1228
|
-
* @param runId - Optional run ID to filter the watch stream
|
|
1229
|
-
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
1230
|
-
*/
|
|
1231
|
-
async watch({ runId }, onRecord) {
|
|
1232
|
-
const response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
1233
|
-
stream: true
|
|
1234
|
-
});
|
|
1235
|
-
if (!response.ok) {
|
|
1236
|
-
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
1237
|
-
}
|
|
1238
|
-
if (!response.body) {
|
|
1239
|
-
throw new Error("Response body is null");
|
|
1240
|
-
}
|
|
1241
|
-
for await (const record of this.streamProcessor(response.body)) {
|
|
1242
|
-
onRecord(record);
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
};
|
|
1246
|
-
|
|
1247
|
-
// src/resources/tool.ts
|
|
1248
|
-
var Tool2 = class extends BaseResource {
|
|
1249
|
-
constructor(options, toolId) {
|
|
1250
|
-
super(options);
|
|
1251
|
-
this.toolId = toolId;
|
|
1252
|
-
}
|
|
1253
|
-
/**
|
|
1254
|
-
* Retrieves details about the tool
|
|
1255
|
-
* @returns Promise containing tool details including description and schemas
|
|
1256
|
-
*/
|
|
1257
|
-
details() {
|
|
1258
|
-
return this.request(`/api/tools/${this.toolId}`);
|
|
1259
|
-
}
|
|
1260
|
-
/**
|
|
1261
|
-
* Executes the tool with the provided parameters
|
|
1262
|
-
* @param params - Parameters required for tool execution
|
|
1263
|
-
* @returns Promise containing the tool execution results
|
|
1264
|
-
*/
|
|
1265
|
-
execute(params) {
|
|
1266
|
-
const url = new URLSearchParams();
|
|
1267
|
-
if (params.runId) {
|
|
1268
|
-
url.set("runId", params.runId);
|
|
1269
|
-
}
|
|
1270
|
-
const body = {
|
|
1271
|
-
data: params.data,
|
|
1272
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1273
|
-
};
|
|
1274
|
-
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
1275
|
-
method: "POST",
|
|
1276
|
-
body
|
|
1277
|
-
});
|
|
1278
|
-
}
|
|
1279
|
-
};
|
|
1280
|
-
|
|
1281
|
-
// src/resources/workflow.ts
|
|
1282
|
-
var RECORD_SEPARATOR2 = "";
|
|
1283
|
-
var Workflow = class extends BaseResource {
|
|
1284
|
-
constructor(options, workflowId) {
|
|
1285
|
-
super(options);
|
|
1286
|
-
this.workflowId = workflowId;
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Creates an async generator that processes a readable stream and yields workflow records
|
|
1290
|
-
* separated by the Record Separator character (\x1E)
|
|
1291
|
-
*
|
|
1292
|
-
* @param stream - The readable stream to process
|
|
1293
|
-
* @returns An async generator that yields parsed records
|
|
1294
|
-
*/
|
|
1295
|
-
async *streamProcessor(stream) {
|
|
1296
|
-
const reader = stream.getReader();
|
|
1297
|
-
let doneReading = false;
|
|
1298
|
-
let buffer = "";
|
|
1299
|
-
try {
|
|
1300
|
-
while (!doneReading) {
|
|
1301
|
-
const { done, value } = await reader.read();
|
|
1302
|
-
doneReading = done;
|
|
1303
|
-
if (done && !value) continue;
|
|
1304
|
-
try {
|
|
1305
|
-
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1306
|
-
const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
|
|
1307
|
-
buffer = chunks.pop() || "";
|
|
1308
|
-
for (const chunk of chunks) {
|
|
1309
|
-
if (chunk) {
|
|
1310
|
-
if (typeof chunk === "string") {
|
|
1311
|
-
try {
|
|
1312
|
-
const parsedChunk = JSON.parse(chunk);
|
|
1313
|
-
yield parsedChunk;
|
|
1314
|
-
} catch {
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
} catch {
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
if (buffer) {
|
|
1323
|
-
try {
|
|
1324
|
-
yield JSON.parse(buffer);
|
|
1325
|
-
} catch {
|
|
1326
|
-
}
|
|
1327
|
-
}
|
|
1328
|
-
} finally {
|
|
1329
|
-
reader.cancel().catch(() => {
|
|
1330
|
-
});
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
/**
|
|
1334
|
-
* Retrieves details about the workflow
|
|
1335
|
-
* @returns Promise containing workflow details including steps and graphs
|
|
1336
|
-
*/
|
|
1337
|
-
details() {
|
|
1338
|
-
return this.request(`/api/workflows/${this.workflowId}`);
|
|
1339
|
-
}
|
|
1340
|
-
/**
|
|
1341
|
-
* Retrieves all runs for a workflow
|
|
1342
|
-
* @param params - Parameters for filtering runs
|
|
1343
|
-
* @returns Promise containing workflow runs array
|
|
1344
|
-
*/
|
|
1345
|
-
runs(params) {
|
|
1346
|
-
const searchParams = new URLSearchParams();
|
|
1347
|
-
if (params?.fromDate) {
|
|
1348
|
-
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1349
|
-
}
|
|
1350
|
-
if (params?.toDate) {
|
|
1351
|
-
searchParams.set("toDate", params.toDate.toISOString());
|
|
1352
|
-
}
|
|
1353
|
-
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1354
|
-
searchParams.set("limit", String(params.limit));
|
|
1355
|
-
}
|
|
1356
|
-
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1357
|
-
searchParams.set("offset", String(params.offset));
|
|
1358
|
-
}
|
|
1359
|
-
if (params?.resourceId) {
|
|
1360
|
-
searchParams.set("resourceId", params.resourceId);
|
|
1361
|
-
}
|
|
1362
|
-
if (searchParams.size) {
|
|
1363
|
-
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1364
|
-
} else {
|
|
1365
|
-
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1630
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1366
1631
|
}
|
|
1367
1632
|
}
|
|
1368
1633
|
/**
|
|
1369
1634
|
* Retrieves a specific workflow run by its ID
|
|
1370
1635
|
* @param runId - The ID of the workflow run to retrieve
|
|
1636
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1371
1637
|
* @returns Promise containing the workflow run details
|
|
1372
1638
|
*/
|
|
1373
|
-
runById(runId) {
|
|
1374
|
-
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1639
|
+
runById(runId, runtimeContext) {
|
|
1640
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1375
1641
|
}
|
|
1376
1642
|
/**
|
|
1377
1643
|
* Retrieves the execution result for a specific workflow run by its ID
|
|
1378
1644
|
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1645
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1379
1646
|
* @returns Promise containing the workflow run execution result
|
|
1380
1647
|
*/
|
|
1381
|
-
runExecutionResult(runId) {
|
|
1382
|
-
return this.request(
|
|
1648
|
+
runExecutionResult(runId, runtimeContext) {
|
|
1649
|
+
return this.request(
|
|
1650
|
+
`/api/workflows/${this.workflowId}/runs/${runId}/execution-result${runtimeContextQueryString(runtimeContext)}`
|
|
1651
|
+
);
|
|
1383
1652
|
}
|
|
1384
1653
|
/**
|
|
1385
1654
|
* Cancels a specific workflow run by its ID
|
|
@@ -1402,27 +1671,83 @@ var Workflow = class extends BaseResource {
|
|
|
1402
1671
|
body: { event: params.event, data: params.data }
|
|
1403
1672
|
});
|
|
1404
1673
|
}
|
|
1674
|
+
/**
|
|
1675
|
+
* @deprecated Use createRunAsync() instead.
|
|
1676
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
1677
|
+
*/
|
|
1678
|
+
async createRun(_params) {
|
|
1679
|
+
throw new Error(
|
|
1680
|
+
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
1681
|
+
);
|
|
1682
|
+
}
|
|
1405
1683
|
/**
|
|
1406
1684
|
* Creates a new workflow run
|
|
1407
1685
|
* @param params - Optional object containing the optional runId
|
|
1408
|
-
* @returns Promise containing the runId of the created run
|
|
1686
|
+
* @returns Promise containing the runId of the created run with methods to control execution
|
|
1409
1687
|
*/
|
|
1410
|
-
|
|
1688
|
+
async createRunAsync(params) {
|
|
1411
1689
|
const searchParams = new URLSearchParams();
|
|
1412
1690
|
if (!!params?.runId) {
|
|
1413
1691
|
searchParams.set("runId", params.runId);
|
|
1414
1692
|
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1693
|
+
const res = await this.request(
|
|
1694
|
+
`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`,
|
|
1695
|
+
{
|
|
1696
|
+
method: "POST"
|
|
1697
|
+
}
|
|
1698
|
+
);
|
|
1699
|
+
const runId = res.runId;
|
|
1700
|
+
return {
|
|
1701
|
+
runId,
|
|
1702
|
+
start: async (p) => {
|
|
1703
|
+
return this.start({
|
|
1704
|
+
runId,
|
|
1705
|
+
inputData: p.inputData,
|
|
1706
|
+
runtimeContext: p.runtimeContext,
|
|
1707
|
+
tracingOptions: p.tracingOptions
|
|
1708
|
+
});
|
|
1709
|
+
},
|
|
1710
|
+
startAsync: async (p) => {
|
|
1711
|
+
return this.startAsync({
|
|
1712
|
+
runId,
|
|
1713
|
+
inputData: p.inputData,
|
|
1714
|
+
runtimeContext: p.runtimeContext,
|
|
1715
|
+
tracingOptions: p.tracingOptions
|
|
1716
|
+
});
|
|
1717
|
+
},
|
|
1718
|
+
watch: async (onRecord) => {
|
|
1719
|
+
return this.watch({ runId }, onRecord);
|
|
1720
|
+
},
|
|
1721
|
+
stream: async (p) => {
|
|
1722
|
+
return this.stream({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1723
|
+
},
|
|
1724
|
+
resume: async (p) => {
|
|
1725
|
+
return this.resume({
|
|
1726
|
+
runId,
|
|
1727
|
+
step: p.step,
|
|
1728
|
+
resumeData: p.resumeData,
|
|
1729
|
+
runtimeContext: p.runtimeContext,
|
|
1730
|
+
tracingOptions: p.tracingOptions
|
|
1731
|
+
});
|
|
1732
|
+
},
|
|
1733
|
+
resumeAsync: async (p) => {
|
|
1734
|
+
return this.resumeAsync({
|
|
1735
|
+
runId,
|
|
1736
|
+
step: p.step,
|
|
1737
|
+
resumeData: p.resumeData,
|
|
1738
|
+
runtimeContext: p.runtimeContext,
|
|
1739
|
+
tracingOptions: p.tracingOptions
|
|
1740
|
+
});
|
|
1741
|
+
},
|
|
1742
|
+
resumeStreamVNext: async (p) => {
|
|
1743
|
+
return this.resumeStreamVNext({
|
|
1744
|
+
runId,
|
|
1745
|
+
step: p.step,
|
|
1746
|
+
resumeData: p.resumeData,
|
|
1747
|
+
runtimeContext: p.runtimeContext
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
};
|
|
1426
1751
|
}
|
|
1427
1752
|
/**
|
|
1428
1753
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
@@ -1433,7 +1758,7 @@ var Workflow = class extends BaseResource {
|
|
|
1433
1758
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1434
1759
|
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1435
1760
|
method: "POST",
|
|
1436
|
-
body: { inputData: params?.inputData, runtimeContext }
|
|
1761
|
+
body: { inputData: params?.inputData, runtimeContext, tracingOptions: params.tracingOptions }
|
|
1437
1762
|
});
|
|
1438
1763
|
}
|
|
1439
1764
|
/**
|
|
@@ -1445,16 +1770,17 @@ var Workflow = class extends BaseResource {
|
|
|
1445
1770
|
step,
|
|
1446
1771
|
runId,
|
|
1447
1772
|
resumeData,
|
|
1773
|
+
tracingOptions,
|
|
1448
1774
|
...rest
|
|
1449
1775
|
}) {
|
|
1450
1776
|
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
1451
1777
|
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1452
1778
|
method: "POST",
|
|
1453
|
-
stream: true,
|
|
1454
1779
|
body: {
|
|
1455
1780
|
step,
|
|
1456
1781
|
resumeData,
|
|
1457
|
-
runtimeContext
|
|
1782
|
+
runtimeContext,
|
|
1783
|
+
tracingOptions
|
|
1458
1784
|
}
|
|
1459
1785
|
});
|
|
1460
1786
|
}
|
|
@@ -1471,7 +1797,7 @@ var Workflow = class extends BaseResource {
|
|
|
1471
1797
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1472
1798
|
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1473
1799
|
method: "POST",
|
|
1474
|
-
body: { inputData: params.inputData, runtimeContext }
|
|
1800
|
+
body: { inputData: params.inputData, runtimeContext, tracingOptions: params.tracingOptions }
|
|
1475
1801
|
});
|
|
1476
1802
|
}
|
|
1477
1803
|
/**
|
|
@@ -1489,12 +1815,12 @@ var Workflow = class extends BaseResource {
|
|
|
1489
1815
|
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1490
1816
|
{
|
|
1491
1817
|
method: "POST",
|
|
1492
|
-
body: { inputData: params.inputData, runtimeContext },
|
|
1818
|
+
body: { inputData: params.inputData, runtimeContext, tracingOptions: params.tracingOptions },
|
|
1493
1819
|
stream: true
|
|
1494
1820
|
}
|
|
1495
1821
|
);
|
|
1496
1822
|
if (!response.ok) {
|
|
1497
|
-
throw new Error(`Failed to stream
|
|
1823
|
+
throw new Error(`Failed to stream workflow: ${response.statusText}`);
|
|
1498
1824
|
}
|
|
1499
1825
|
if (!response.body) {
|
|
1500
1826
|
throw new Error("Response body is null");
|
|
@@ -1506,7 +1832,7 @@ var Workflow = class extends BaseResource {
|
|
|
1506
1832
|
async transform(chunk, controller) {
|
|
1507
1833
|
try {
|
|
1508
1834
|
const decoded = new TextDecoder().decode(chunk);
|
|
1509
|
-
const chunks = decoded.split(
|
|
1835
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1510
1836
|
for (const chunk2 of chunks) {
|
|
1511
1837
|
if (chunk2) {
|
|
1512
1838
|
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
@@ -1514,7 +1840,7 @@ var Workflow = class extends BaseResource {
|
|
|
1514
1840
|
const parsedChunk = JSON.parse(newChunk);
|
|
1515
1841
|
controller.enqueue(parsedChunk);
|
|
1516
1842
|
failedChunk = void 0;
|
|
1517
|
-
} catch
|
|
1843
|
+
} catch {
|
|
1518
1844
|
failedChunk = newChunk;
|
|
1519
1845
|
}
|
|
1520
1846
|
}
|
|
@@ -1526,21 +1852,226 @@ var Workflow = class extends BaseResource {
|
|
|
1526
1852
|
return response.body.pipeThrough(transformStream);
|
|
1527
1853
|
}
|
|
1528
1854
|
/**
|
|
1529
|
-
*
|
|
1530
|
-
* @param params - Object containing the runId
|
|
1531
|
-
* @returns Promise containing the workflow
|
|
1855
|
+
* Observes workflow stream for a workflow run
|
|
1856
|
+
* @param params - Object containing the runId
|
|
1857
|
+
* @returns Promise containing the workflow execution results
|
|
1532
1858
|
*/
|
|
1533
|
-
|
|
1859
|
+
async observeStream(params) {
|
|
1860
|
+
const searchParams = new URLSearchParams();
|
|
1861
|
+
searchParams.set("runId", params.runId);
|
|
1862
|
+
const response = await this.request(
|
|
1863
|
+
`/api/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
|
|
1864
|
+
{
|
|
1865
|
+
method: "POST",
|
|
1866
|
+
stream: true
|
|
1867
|
+
}
|
|
1868
|
+
);
|
|
1869
|
+
if (!response.ok) {
|
|
1870
|
+
throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
|
|
1871
|
+
}
|
|
1872
|
+
if (!response.body) {
|
|
1873
|
+
throw new Error("Response body is null");
|
|
1874
|
+
}
|
|
1875
|
+
let failedChunk = void 0;
|
|
1876
|
+
const transformStream = new TransformStream({
|
|
1877
|
+
start() {
|
|
1878
|
+
},
|
|
1879
|
+
async transform(chunk, controller) {
|
|
1880
|
+
try {
|
|
1881
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1882
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1883
|
+
for (const chunk2 of chunks) {
|
|
1884
|
+
if (chunk2) {
|
|
1885
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1886
|
+
try {
|
|
1887
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1888
|
+
controller.enqueue(parsedChunk);
|
|
1889
|
+
failedChunk = void 0;
|
|
1890
|
+
} catch {
|
|
1891
|
+
failedChunk = newChunk;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
} catch {
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
});
|
|
1899
|
+
return response.body.pipeThrough(transformStream);
|
|
1900
|
+
}
|
|
1901
|
+
/**
|
|
1902
|
+
* Starts a workflow run and returns a stream
|
|
1903
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1904
|
+
* @returns Promise containing the workflow execution results
|
|
1905
|
+
*/
|
|
1906
|
+
async streamVNext(params) {
|
|
1907
|
+
const searchParams = new URLSearchParams();
|
|
1908
|
+
if (!!params?.runId) {
|
|
1909
|
+
searchParams.set("runId", params.runId);
|
|
1910
|
+
}
|
|
1911
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1912
|
+
const response = await this.request(
|
|
1913
|
+
`/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
|
|
1914
|
+
{
|
|
1915
|
+
method: "POST",
|
|
1916
|
+
body: {
|
|
1917
|
+
inputData: params.inputData,
|
|
1918
|
+
runtimeContext,
|
|
1919
|
+
closeOnSuspend: params.closeOnSuspend,
|
|
1920
|
+
tracingOptions: params.tracingOptions
|
|
1921
|
+
},
|
|
1922
|
+
stream: true
|
|
1923
|
+
}
|
|
1924
|
+
);
|
|
1925
|
+
if (!response.ok) {
|
|
1926
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1927
|
+
}
|
|
1928
|
+
if (!response.body) {
|
|
1929
|
+
throw new Error("Response body is null");
|
|
1930
|
+
}
|
|
1931
|
+
let failedChunk = void 0;
|
|
1932
|
+
const transformStream = new TransformStream({
|
|
1933
|
+
start() {
|
|
1934
|
+
},
|
|
1935
|
+
async transform(chunk, controller) {
|
|
1936
|
+
try {
|
|
1937
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1938
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1939
|
+
for (const chunk2 of chunks) {
|
|
1940
|
+
if (chunk2) {
|
|
1941
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1942
|
+
try {
|
|
1943
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1944
|
+
controller.enqueue(parsedChunk);
|
|
1945
|
+
failedChunk = void 0;
|
|
1946
|
+
} catch {
|
|
1947
|
+
failedChunk = newChunk;
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
} catch {
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
});
|
|
1955
|
+
return response.body.pipeThrough(transformStream);
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Observes workflow vNext stream for a workflow run
|
|
1959
|
+
* @param params - Object containing the runId
|
|
1960
|
+
* @returns Promise containing the workflow execution results
|
|
1961
|
+
*/
|
|
1962
|
+
async observeStreamVNext(params) {
|
|
1963
|
+
const searchParams = new URLSearchParams();
|
|
1964
|
+
searchParams.set("runId", params.runId);
|
|
1965
|
+
const response = await this.request(
|
|
1966
|
+
`/api/workflows/${this.workflowId}/observe-streamVNext?${searchParams.toString()}`,
|
|
1967
|
+
{
|
|
1968
|
+
method: "POST",
|
|
1969
|
+
stream: true
|
|
1970
|
+
}
|
|
1971
|
+
);
|
|
1972
|
+
if (!response.ok) {
|
|
1973
|
+
throw new Error(`Failed to observe stream vNext workflow: ${response.statusText}`);
|
|
1974
|
+
}
|
|
1975
|
+
if (!response.body) {
|
|
1976
|
+
throw new Error("Response body is null");
|
|
1977
|
+
}
|
|
1978
|
+
let failedChunk = void 0;
|
|
1979
|
+
const transformStream = new TransformStream({
|
|
1980
|
+
start() {
|
|
1981
|
+
},
|
|
1982
|
+
async transform(chunk, controller) {
|
|
1983
|
+
try {
|
|
1984
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1985
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1986
|
+
for (const chunk2 of chunks) {
|
|
1987
|
+
if (chunk2) {
|
|
1988
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1989
|
+
try {
|
|
1990
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1991
|
+
controller.enqueue(parsedChunk);
|
|
1992
|
+
failedChunk = void 0;
|
|
1993
|
+
} catch {
|
|
1994
|
+
failedChunk = newChunk;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
} catch {
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
2002
|
+
return response.body.pipeThrough(transformStream);
|
|
2003
|
+
}
|
|
2004
|
+
/**
|
|
2005
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
2006
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
2007
|
+
* @returns Promise containing the workflow resume results
|
|
2008
|
+
*/
|
|
2009
|
+
resumeAsync(params) {
|
|
1534
2010
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1535
2011
|
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1536
2012
|
method: "POST",
|
|
1537
2013
|
body: {
|
|
1538
2014
|
step: params.step,
|
|
1539
2015
|
resumeData: params.resumeData,
|
|
1540
|
-
runtimeContext
|
|
2016
|
+
runtimeContext,
|
|
2017
|
+
tracingOptions: params.tracingOptions
|
|
1541
2018
|
}
|
|
1542
2019
|
});
|
|
1543
2020
|
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Resumes a suspended workflow step that uses streamVNext asynchronously and returns a promise that resolves when the workflow is complete
|
|
2023
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
2024
|
+
* @returns Promise containing the workflow resume results
|
|
2025
|
+
*/
|
|
2026
|
+
async resumeStreamVNext(params) {
|
|
2027
|
+
const searchParams = new URLSearchParams();
|
|
2028
|
+
searchParams.set("runId", params.runId);
|
|
2029
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2030
|
+
const response = await this.request(
|
|
2031
|
+
`/api/workflows/${this.workflowId}/resume-stream?${searchParams.toString()}`,
|
|
2032
|
+
{
|
|
2033
|
+
method: "POST",
|
|
2034
|
+
body: {
|
|
2035
|
+
step: params.step,
|
|
2036
|
+
resumeData: params.resumeData,
|
|
2037
|
+
runtimeContext,
|
|
2038
|
+
tracingOptions: params.tracingOptions
|
|
2039
|
+
},
|
|
2040
|
+
stream: true
|
|
2041
|
+
}
|
|
2042
|
+
);
|
|
2043
|
+
if (!response.ok) {
|
|
2044
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
2045
|
+
}
|
|
2046
|
+
if (!response.body) {
|
|
2047
|
+
throw new Error("Response body is null");
|
|
2048
|
+
}
|
|
2049
|
+
let failedChunk = void 0;
|
|
2050
|
+
const transformStream = new TransformStream({
|
|
2051
|
+
start() {
|
|
2052
|
+
},
|
|
2053
|
+
async transform(chunk, controller) {
|
|
2054
|
+
try {
|
|
2055
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2056
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
2057
|
+
for (const chunk2 of chunks) {
|
|
2058
|
+
if (chunk2) {
|
|
2059
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2060
|
+
try {
|
|
2061
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2062
|
+
controller.enqueue(parsedChunk);
|
|
2063
|
+
failedChunk = void 0;
|
|
2064
|
+
} catch {
|
|
2065
|
+
failedChunk = newChunk;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
} catch {
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
});
|
|
2073
|
+
return response.body.pipeThrough(transformStream);
|
|
2074
|
+
}
|
|
1544
2075
|
/**
|
|
1545
2076
|
* Watches workflow transitions in real-time
|
|
1546
2077
|
* @param runId - Optional run ID to filter the watch stream
|
|
@@ -1577,7 +2108,7 @@ var Workflow = class extends BaseResource {
|
|
|
1577
2108
|
async start(controller) {
|
|
1578
2109
|
try {
|
|
1579
2110
|
for await (const record of records) {
|
|
1580
|
-
const json = JSON.stringify(record) +
|
|
2111
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR;
|
|
1581
2112
|
controller.enqueue(encoder.encode(json));
|
|
1582
2113
|
}
|
|
1583
2114
|
controller.close();
|
|
@@ -1600,99 +2131,546 @@ var A2A = class extends BaseResource {
|
|
|
1600
2131
|
* @returns Promise containing the agent card information
|
|
1601
2132
|
*/
|
|
1602
2133
|
async getCard() {
|
|
1603
|
-
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
2134
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
1604
2135
|
}
|
|
1605
2136
|
/**
|
|
1606
|
-
* Send a message to the agent and
|
|
2137
|
+
* Send a message to the agent and gets a message or task response
|
|
1607
2138
|
* @param params - Parameters for the task
|
|
1608
|
-
* @returns Promise containing the
|
|
2139
|
+
* @returns Promise containing the response
|
|
1609
2140
|
*/
|
|
1610
2141
|
async sendMessage(params) {
|
|
1611
2142
|
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1612
2143
|
method: "POST",
|
|
1613
2144
|
body: {
|
|
1614
|
-
method: "
|
|
2145
|
+
method: "message/send",
|
|
2146
|
+
params
|
|
2147
|
+
}
|
|
2148
|
+
});
|
|
2149
|
+
return response;
|
|
2150
|
+
}
|
|
2151
|
+
/**
|
|
2152
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2153
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2154
|
+
* @param params - Parameters for the task
|
|
2155
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2156
|
+
*/
|
|
2157
|
+
async sendStreamingMessage(params) {
|
|
2158
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2159
|
+
method: "POST",
|
|
2160
|
+
body: {
|
|
2161
|
+
method: "message/stream",
|
|
1615
2162
|
params
|
|
1616
2163
|
}
|
|
1617
2164
|
});
|
|
1618
|
-
return
|
|
2165
|
+
return response;
|
|
2166
|
+
}
|
|
2167
|
+
/**
|
|
2168
|
+
* Get the status and result of a task
|
|
2169
|
+
* @param params - Parameters for querying the task
|
|
2170
|
+
* @returns Promise containing the task response
|
|
2171
|
+
*/
|
|
2172
|
+
async getTask(params) {
|
|
2173
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2174
|
+
method: "POST",
|
|
2175
|
+
body: {
|
|
2176
|
+
method: "tasks/get",
|
|
2177
|
+
params
|
|
2178
|
+
}
|
|
2179
|
+
});
|
|
2180
|
+
return response;
|
|
2181
|
+
}
|
|
2182
|
+
/**
|
|
2183
|
+
* Cancel a running task
|
|
2184
|
+
* @param params - Parameters identifying the task to cancel
|
|
2185
|
+
* @returns Promise containing the task response
|
|
2186
|
+
*/
|
|
2187
|
+
async cancelTask(params) {
|
|
2188
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2189
|
+
method: "POST",
|
|
2190
|
+
body: {
|
|
2191
|
+
method: "tasks/cancel",
|
|
2192
|
+
params
|
|
2193
|
+
}
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
};
|
|
2197
|
+
|
|
2198
|
+
// src/resources/mcp-tool.ts
|
|
2199
|
+
var MCPTool = class extends BaseResource {
|
|
2200
|
+
serverId;
|
|
2201
|
+
toolId;
|
|
2202
|
+
constructor(options, serverId, toolId) {
|
|
2203
|
+
super(options);
|
|
2204
|
+
this.serverId = serverId;
|
|
2205
|
+
this.toolId = toolId;
|
|
2206
|
+
}
|
|
2207
|
+
/**
|
|
2208
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2209
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2210
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2211
|
+
*/
|
|
2212
|
+
details(runtimeContext) {
|
|
2213
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
2214
|
+
}
|
|
2215
|
+
/**
|
|
2216
|
+
* Executes this specific tool on the MCP server.
|
|
2217
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2218
|
+
* @returns Promise containing the result of the tool execution.
|
|
2219
|
+
*/
|
|
2220
|
+
execute(params) {
|
|
2221
|
+
const body = {};
|
|
2222
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2223
|
+
if (params.runtimeContext !== void 0) {
|
|
2224
|
+
body.runtimeContext = params.runtimeContext;
|
|
2225
|
+
}
|
|
2226
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2227
|
+
method: "POST",
|
|
2228
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
};
|
|
2232
|
+
|
|
2233
|
+
// src/resources/agent-builder.ts
|
|
2234
|
+
var RECORD_SEPARATOR2 = "";
|
|
2235
|
+
var AgentBuilder = class extends BaseResource {
|
|
2236
|
+
constructor(options, actionId) {
|
|
2237
|
+
super(options);
|
|
2238
|
+
this.actionId = actionId;
|
|
2239
|
+
}
|
|
2240
|
+
// Helper function to transform workflow result to action result
|
|
2241
|
+
transformWorkflowResult(result) {
|
|
2242
|
+
if (result.status === "success") {
|
|
2243
|
+
return {
|
|
2244
|
+
success: result.result.success || false,
|
|
2245
|
+
applied: result.result.applied || false,
|
|
2246
|
+
branchName: result.result.branchName,
|
|
2247
|
+
message: result.result.message || "Agent builder action completed",
|
|
2248
|
+
validationResults: result.result.validationResults,
|
|
2249
|
+
error: result.result.error,
|
|
2250
|
+
errors: result.result.errors,
|
|
2251
|
+
stepResults: result.result.stepResults
|
|
2252
|
+
};
|
|
2253
|
+
} else if (result.status === "failed") {
|
|
2254
|
+
return {
|
|
2255
|
+
success: false,
|
|
2256
|
+
applied: false,
|
|
2257
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2258
|
+
error: result.error.message
|
|
2259
|
+
};
|
|
2260
|
+
} else {
|
|
2261
|
+
return {
|
|
2262
|
+
success: false,
|
|
2263
|
+
applied: false,
|
|
2264
|
+
message: "Agent builder action was suspended",
|
|
2265
|
+
error: "Workflow suspended - manual intervention required"
|
|
2266
|
+
};
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
/**
|
|
2270
|
+
* @deprecated Use createRunAsync() instead.
|
|
2271
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
2272
|
+
*/
|
|
2273
|
+
async createRun(_params) {
|
|
2274
|
+
throw new Error(
|
|
2275
|
+
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = agentBuilder.createRun();\n After: const run = await agentBuilder.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
2276
|
+
);
|
|
2277
|
+
}
|
|
2278
|
+
/**
|
|
2279
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2280
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2281
|
+
*/
|
|
2282
|
+
async createRunAsync(params) {
|
|
2283
|
+
const searchParams = new URLSearchParams();
|
|
2284
|
+
if (!!params?.runId) {
|
|
2285
|
+
searchParams.set("runId", params.runId);
|
|
2286
|
+
}
|
|
2287
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2288
|
+
return this.request(url, {
|
|
2289
|
+
method: "POST"
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
2292
|
+
/**
|
|
2293
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2294
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2295
|
+
*/
|
|
2296
|
+
async startAsync(params, runId) {
|
|
2297
|
+
const searchParams = new URLSearchParams();
|
|
2298
|
+
if (runId) {
|
|
2299
|
+
searchParams.set("runId", runId);
|
|
2300
|
+
}
|
|
2301
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2302
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2303
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2304
|
+
const result = await this.request(url, {
|
|
2305
|
+
method: "POST",
|
|
2306
|
+
body: { ...actionParams, runtimeContext }
|
|
2307
|
+
});
|
|
2308
|
+
return this.transformWorkflowResult(result);
|
|
2309
|
+
}
|
|
2310
|
+
/**
|
|
2311
|
+
* Starts an existing agent builder action run.
|
|
2312
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2313
|
+
*/
|
|
2314
|
+
async startActionRun(params, runId) {
|
|
2315
|
+
const searchParams = new URLSearchParams();
|
|
2316
|
+
searchParams.set("runId", runId);
|
|
2317
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2318
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2319
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2320
|
+
return this.request(url, {
|
|
2321
|
+
method: "POST",
|
|
2322
|
+
body: { ...actionParams, runtimeContext }
|
|
2323
|
+
});
|
|
2324
|
+
}
|
|
2325
|
+
/**
|
|
2326
|
+
* Resumes a suspended agent builder action step.
|
|
2327
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2328
|
+
*/
|
|
2329
|
+
async resume(params, runId) {
|
|
2330
|
+
const searchParams = new URLSearchParams();
|
|
2331
|
+
searchParams.set("runId", runId);
|
|
2332
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2333
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2334
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2335
|
+
return this.request(url, {
|
|
2336
|
+
method: "POST",
|
|
2337
|
+
body: { ...resumeParams, runtimeContext }
|
|
2338
|
+
});
|
|
2339
|
+
}
|
|
2340
|
+
/**
|
|
2341
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2342
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2343
|
+
*/
|
|
2344
|
+
async resumeAsync(params, runId) {
|
|
2345
|
+
const searchParams = new URLSearchParams();
|
|
2346
|
+
searchParams.set("runId", runId);
|
|
2347
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2348
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2349
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2350
|
+
const result = await this.request(url, {
|
|
2351
|
+
method: "POST",
|
|
2352
|
+
body: { ...resumeParams, runtimeContext }
|
|
2353
|
+
});
|
|
2354
|
+
return this.transformWorkflowResult(result);
|
|
2355
|
+
}
|
|
2356
|
+
/**
|
|
2357
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2358
|
+
* separated by the Record Separator character (\x1E)
|
|
2359
|
+
*
|
|
2360
|
+
* @param stream - The readable stream to process
|
|
2361
|
+
* @returns An async generator that yields parsed records
|
|
2362
|
+
*/
|
|
2363
|
+
async *streamProcessor(stream) {
|
|
2364
|
+
const reader = stream.getReader();
|
|
2365
|
+
let doneReading = false;
|
|
2366
|
+
let buffer = "";
|
|
2367
|
+
try {
|
|
2368
|
+
while (!doneReading) {
|
|
2369
|
+
const { done, value } = await reader.read();
|
|
2370
|
+
doneReading = done;
|
|
2371
|
+
if (done && !value) continue;
|
|
2372
|
+
try {
|
|
2373
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2374
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
|
|
2375
|
+
buffer = chunks.pop() || "";
|
|
2376
|
+
for (const chunk of chunks) {
|
|
2377
|
+
if (chunk) {
|
|
2378
|
+
if (typeof chunk === "string") {
|
|
2379
|
+
try {
|
|
2380
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2381
|
+
yield parsedChunk;
|
|
2382
|
+
} catch {
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
} catch {
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
if (buffer) {
|
|
2391
|
+
try {
|
|
2392
|
+
yield JSON.parse(buffer);
|
|
2393
|
+
} catch {
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
} finally {
|
|
2397
|
+
reader.cancel().catch(() => {
|
|
2398
|
+
});
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
/**
|
|
2402
|
+
* Streams agent builder action progress in real-time.
|
|
2403
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
2404
|
+
*/
|
|
2405
|
+
async stream(params, runId) {
|
|
2406
|
+
const searchParams = new URLSearchParams();
|
|
2407
|
+
if (runId) {
|
|
2408
|
+
searchParams.set("runId", runId);
|
|
2409
|
+
}
|
|
2410
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2411
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2412
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2413
|
+
const response = await this.request(url, {
|
|
2414
|
+
method: "POST",
|
|
2415
|
+
body: { ...actionParams, runtimeContext },
|
|
2416
|
+
stream: true
|
|
2417
|
+
});
|
|
2418
|
+
if (!response.ok) {
|
|
2419
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
2420
|
+
}
|
|
2421
|
+
if (!response.body) {
|
|
2422
|
+
throw new Error("Response body is null");
|
|
2423
|
+
}
|
|
2424
|
+
let failedChunk = void 0;
|
|
2425
|
+
const transformStream = new TransformStream({
|
|
2426
|
+
start() {
|
|
2427
|
+
},
|
|
2428
|
+
async transform(chunk, controller) {
|
|
2429
|
+
try {
|
|
2430
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2431
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2432
|
+
for (const chunk2 of chunks) {
|
|
2433
|
+
if (chunk2) {
|
|
2434
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2435
|
+
try {
|
|
2436
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2437
|
+
controller.enqueue(parsedChunk);
|
|
2438
|
+
failedChunk = void 0;
|
|
2439
|
+
} catch {
|
|
2440
|
+
failedChunk = newChunk;
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
} catch {
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
});
|
|
2448
|
+
return response.body.pipeThrough(transformStream);
|
|
2449
|
+
}
|
|
2450
|
+
/**
|
|
2451
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2452
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
2453
|
+
*/
|
|
2454
|
+
async streamVNext(params, runId) {
|
|
2455
|
+
const searchParams = new URLSearchParams();
|
|
2456
|
+
if (runId) {
|
|
2457
|
+
searchParams.set("runId", runId);
|
|
2458
|
+
}
|
|
2459
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2460
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2461
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2462
|
+
const response = await this.request(url, {
|
|
2463
|
+
method: "POST",
|
|
2464
|
+
body: { ...actionParams, runtimeContext },
|
|
2465
|
+
stream: true
|
|
2466
|
+
});
|
|
2467
|
+
if (!response.ok) {
|
|
2468
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2469
|
+
}
|
|
2470
|
+
if (!response.body) {
|
|
2471
|
+
throw new Error("Response body is null");
|
|
2472
|
+
}
|
|
2473
|
+
let failedChunk = void 0;
|
|
2474
|
+
const transformStream = new TransformStream({
|
|
2475
|
+
start() {
|
|
2476
|
+
},
|
|
2477
|
+
async transform(chunk, controller) {
|
|
2478
|
+
try {
|
|
2479
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2480
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2481
|
+
for (const chunk2 of chunks) {
|
|
2482
|
+
if (chunk2) {
|
|
2483
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2484
|
+
try {
|
|
2485
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2486
|
+
controller.enqueue(parsedChunk);
|
|
2487
|
+
failedChunk = void 0;
|
|
2488
|
+
} catch {
|
|
2489
|
+
failedChunk = newChunk;
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
} catch {
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
});
|
|
2497
|
+
return response.body.pipeThrough(transformStream);
|
|
2498
|
+
}
|
|
2499
|
+
/**
|
|
2500
|
+
* Watches an existing agent builder action run by runId.
|
|
2501
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2502
|
+
* and streams any remaining progress.
|
|
2503
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
2504
|
+
*/
|
|
2505
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2506
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2507
|
+
const response = await this.request(url, {
|
|
2508
|
+
method: "GET",
|
|
2509
|
+
stream: true
|
|
2510
|
+
});
|
|
2511
|
+
if (!response.ok) {
|
|
2512
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
2513
|
+
}
|
|
2514
|
+
if (!response.body) {
|
|
2515
|
+
throw new Error("Response body is null");
|
|
2516
|
+
}
|
|
2517
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2518
|
+
if (typeof record === "string") {
|
|
2519
|
+
onRecord(JSON.parse(record));
|
|
2520
|
+
} else {
|
|
2521
|
+
onRecord(record);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
/**
|
|
2526
|
+
* Gets a specific action run by its ID.
|
|
2527
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
2528
|
+
*/
|
|
2529
|
+
async runById(runId) {
|
|
2530
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2531
|
+
return this.request(url, {
|
|
2532
|
+
method: "GET"
|
|
2533
|
+
});
|
|
2534
|
+
}
|
|
2535
|
+
/**
|
|
2536
|
+
* Gets details about this agent builder action.
|
|
2537
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
2538
|
+
*/
|
|
2539
|
+
async details() {
|
|
2540
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2541
|
+
return result;
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* Gets all runs for this agent builder action.
|
|
2545
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
2546
|
+
*/
|
|
2547
|
+
async runs(params) {
|
|
2548
|
+
const searchParams = new URLSearchParams();
|
|
2549
|
+
if (params?.fromDate) {
|
|
2550
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2551
|
+
}
|
|
2552
|
+
if (params?.toDate) {
|
|
2553
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2554
|
+
}
|
|
2555
|
+
if (params?.limit !== void 0) {
|
|
2556
|
+
searchParams.set("limit", String(params.limit));
|
|
2557
|
+
}
|
|
2558
|
+
if (params?.offset !== void 0) {
|
|
2559
|
+
searchParams.set("offset", String(params.offset));
|
|
2560
|
+
}
|
|
2561
|
+
if (params?.resourceId) {
|
|
2562
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2563
|
+
}
|
|
2564
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2565
|
+
return this.request(url, {
|
|
2566
|
+
method: "GET"
|
|
2567
|
+
});
|
|
1619
2568
|
}
|
|
1620
2569
|
/**
|
|
1621
|
-
*
|
|
1622
|
-
*
|
|
1623
|
-
* @returns Promise containing the task response
|
|
2570
|
+
* Gets the execution result of an agent builder action run.
|
|
2571
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
1624
2572
|
*/
|
|
1625
|
-
async
|
|
1626
|
-
const
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
method: "tasks/get",
|
|
1630
|
-
params
|
|
1631
|
-
}
|
|
2573
|
+
async runExecutionResult(runId) {
|
|
2574
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2575
|
+
return this.request(url, {
|
|
2576
|
+
method: "GET"
|
|
1632
2577
|
});
|
|
1633
|
-
return response.result;
|
|
1634
2578
|
}
|
|
1635
2579
|
/**
|
|
1636
|
-
*
|
|
1637
|
-
*
|
|
1638
|
-
* @returns Promise containing the task response
|
|
2580
|
+
* Cancels an agent builder action run.
|
|
2581
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
1639
2582
|
*/
|
|
1640
|
-
async
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
method: "tasks/cancel",
|
|
1645
|
-
params
|
|
1646
|
-
}
|
|
2583
|
+
async cancelRun(runId) {
|
|
2584
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2585
|
+
return this.request(url, {
|
|
2586
|
+
method: "POST"
|
|
1647
2587
|
});
|
|
1648
2588
|
}
|
|
1649
2589
|
/**
|
|
1650
|
-
*
|
|
1651
|
-
*
|
|
1652
|
-
* @returns Promise containing the task response
|
|
2590
|
+
* Sends an event to an agent builder action run.
|
|
2591
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
1653
2592
|
*/
|
|
1654
|
-
async
|
|
1655
|
-
|
|
2593
|
+
async sendRunEvent(params) {
|
|
2594
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2595
|
+
return this.request(url, {
|
|
1656
2596
|
method: "POST",
|
|
1657
|
-
body: {
|
|
1658
|
-
method: "tasks/sendSubscribe",
|
|
1659
|
-
params
|
|
1660
|
-
},
|
|
1661
|
-
stream: true
|
|
2597
|
+
body: { event: params.event, data: params.data }
|
|
1662
2598
|
});
|
|
1663
2599
|
}
|
|
1664
2600
|
};
|
|
1665
2601
|
|
|
1666
|
-
// src/resources/
|
|
1667
|
-
var
|
|
1668
|
-
|
|
1669
|
-
toolId;
|
|
1670
|
-
constructor(options, serverId, toolId) {
|
|
2602
|
+
// src/resources/observability.ts
|
|
2603
|
+
var Observability = class extends BaseResource {
|
|
2604
|
+
constructor(options) {
|
|
1671
2605
|
super(options);
|
|
1672
|
-
this.serverId = serverId;
|
|
1673
|
-
this.toolId = toolId;
|
|
1674
2606
|
}
|
|
1675
2607
|
/**
|
|
1676
|
-
* Retrieves
|
|
1677
|
-
* @
|
|
2608
|
+
* Retrieves a specific AI trace by ID
|
|
2609
|
+
* @param traceId - ID of the trace to retrieve
|
|
2610
|
+
* @returns Promise containing the AI trace with all its spans
|
|
1678
2611
|
*/
|
|
1679
|
-
|
|
1680
|
-
return this.request(`/api/
|
|
2612
|
+
getTrace(traceId) {
|
|
2613
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
1681
2614
|
}
|
|
1682
2615
|
/**
|
|
1683
|
-
*
|
|
1684
|
-
* @param params - Parameters for
|
|
1685
|
-
* @returns Promise containing
|
|
2616
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2617
|
+
* @param params - Parameters for pagination and filtering
|
|
2618
|
+
* @returns Promise containing paginated traces and pagination info
|
|
1686
2619
|
*/
|
|
1687
|
-
|
|
1688
|
-
const
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
2620
|
+
getTraces(params) {
|
|
2621
|
+
const { pagination, filters } = params;
|
|
2622
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2623
|
+
const { name, spanType, entityId, entityType } = filters || {};
|
|
2624
|
+
const searchParams = new URLSearchParams();
|
|
2625
|
+
if (page !== void 0) {
|
|
2626
|
+
searchParams.set("page", String(page));
|
|
1692
2627
|
}
|
|
1693
|
-
|
|
2628
|
+
if (perPage !== void 0) {
|
|
2629
|
+
searchParams.set("perPage", String(perPage));
|
|
2630
|
+
}
|
|
2631
|
+
if (name) {
|
|
2632
|
+
searchParams.set("name", name);
|
|
2633
|
+
}
|
|
2634
|
+
if (spanType !== void 0) {
|
|
2635
|
+
searchParams.set("spanType", String(spanType));
|
|
2636
|
+
}
|
|
2637
|
+
if (entityId && entityType) {
|
|
2638
|
+
searchParams.set("entityId", entityId);
|
|
2639
|
+
searchParams.set("entityType", entityType);
|
|
2640
|
+
}
|
|
2641
|
+
if (dateRange) {
|
|
2642
|
+
const dateRangeStr = JSON.stringify({
|
|
2643
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2644
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2645
|
+
});
|
|
2646
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2647
|
+
}
|
|
2648
|
+
const queryString = searchParams.toString();
|
|
2649
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
2650
|
+
}
|
|
2651
|
+
/**
|
|
2652
|
+
* Retrieves scores by trace ID and span ID
|
|
2653
|
+
* @param params - Parameters containing trace ID, span ID, and pagination options
|
|
2654
|
+
* @returns Promise containing scores and pagination info
|
|
2655
|
+
*/
|
|
2656
|
+
getScoresBySpan(params) {
|
|
2657
|
+
const { traceId, spanId, page, perPage } = params;
|
|
2658
|
+
const searchParams = new URLSearchParams();
|
|
2659
|
+
if (page !== void 0) {
|
|
2660
|
+
searchParams.set("page", String(page));
|
|
2661
|
+
}
|
|
2662
|
+
if (perPage !== void 0) {
|
|
2663
|
+
searchParams.set("perPage", String(perPage));
|
|
2664
|
+
}
|
|
2665
|
+
const queryString = searchParams.toString();
|
|
2666
|
+
return this.request(
|
|
2667
|
+
`/api/observability/traces/${encodeURIComponent(traceId)}/${encodeURIComponent(spanId)}/scores${queryString ? `?${queryString}` : ""}`
|
|
2668
|
+
);
|
|
2669
|
+
}
|
|
2670
|
+
score(params) {
|
|
2671
|
+
return this.request(`/api/observability/traces/score`, {
|
|
1694
2672
|
method: "POST",
|
|
1695
|
-
body:
|
|
2673
|
+
body: { ...params }
|
|
1696
2674
|
});
|
|
1697
2675
|
}
|
|
1698
2676
|
};
|
|
@@ -1760,170 +2738,26 @@ var NetworkMemoryThread = class extends BaseResource {
|
|
|
1760
2738
|
}
|
|
1761
2739
|
};
|
|
1762
2740
|
|
|
1763
|
-
// src/resources/vNextNetwork.ts
|
|
1764
|
-
var RECORD_SEPARATOR3 = "";
|
|
1765
|
-
var VNextNetwork = class extends BaseResource {
|
|
1766
|
-
constructor(options, networkId) {
|
|
1767
|
-
super(options);
|
|
1768
|
-
this.networkId = networkId;
|
|
1769
|
-
}
|
|
1770
|
-
/**
|
|
1771
|
-
* Retrieves details about the network
|
|
1772
|
-
* @returns Promise containing vNext network details
|
|
1773
|
-
*/
|
|
1774
|
-
details() {
|
|
1775
|
-
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1776
|
-
}
|
|
1777
|
-
/**
|
|
1778
|
-
* Generates a response from the v-next network
|
|
1779
|
-
* @param params - Generation parameters including message
|
|
1780
|
-
* @returns Promise containing the generated response
|
|
1781
|
-
*/
|
|
1782
|
-
generate(params) {
|
|
1783
|
-
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1784
|
-
method: "POST",
|
|
1785
|
-
body: {
|
|
1786
|
-
...params,
|
|
1787
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1788
|
-
}
|
|
1789
|
-
});
|
|
1790
|
-
}
|
|
1791
|
-
/**
|
|
1792
|
-
* Generates a response from the v-next network using multiple primitives
|
|
1793
|
-
* @param params - Generation parameters including message
|
|
1794
|
-
* @returns Promise containing the generated response
|
|
1795
|
-
*/
|
|
1796
|
-
loop(params) {
|
|
1797
|
-
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1798
|
-
method: "POST",
|
|
1799
|
-
body: {
|
|
1800
|
-
...params,
|
|
1801
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1802
|
-
}
|
|
1803
|
-
});
|
|
1804
|
-
}
|
|
1805
|
-
async *streamProcessor(stream) {
|
|
1806
|
-
const reader = stream.getReader();
|
|
1807
|
-
let doneReading = false;
|
|
1808
|
-
let buffer = "";
|
|
1809
|
-
try {
|
|
1810
|
-
while (!doneReading) {
|
|
1811
|
-
const { done, value } = await reader.read();
|
|
1812
|
-
doneReading = done;
|
|
1813
|
-
if (done && !value) continue;
|
|
1814
|
-
try {
|
|
1815
|
-
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1816
|
-
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1817
|
-
buffer = chunks.pop() || "";
|
|
1818
|
-
for (const chunk of chunks) {
|
|
1819
|
-
if (chunk) {
|
|
1820
|
-
if (typeof chunk === "string") {
|
|
1821
|
-
try {
|
|
1822
|
-
const parsedChunk = JSON.parse(chunk);
|
|
1823
|
-
yield parsedChunk;
|
|
1824
|
-
} catch {
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
} catch {
|
|
1830
|
-
}
|
|
1831
|
-
}
|
|
1832
|
-
if (buffer) {
|
|
1833
|
-
try {
|
|
1834
|
-
yield JSON.parse(buffer);
|
|
1835
|
-
} catch {
|
|
1836
|
-
}
|
|
1837
|
-
}
|
|
1838
|
-
} finally {
|
|
1839
|
-
reader.cancel().catch(() => {
|
|
1840
|
-
});
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
/**
|
|
1844
|
-
* Streams a response from the v-next network
|
|
1845
|
-
* @param params - Stream parameters including message
|
|
1846
|
-
* @returns Promise containing the results
|
|
1847
|
-
*/
|
|
1848
|
-
async stream(params, onRecord) {
|
|
1849
|
-
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1850
|
-
method: "POST",
|
|
1851
|
-
body: {
|
|
1852
|
-
...params,
|
|
1853
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1854
|
-
},
|
|
1855
|
-
stream: true
|
|
1856
|
-
});
|
|
1857
|
-
if (!response.ok) {
|
|
1858
|
-
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1859
|
-
}
|
|
1860
|
-
if (!response.body) {
|
|
1861
|
-
throw new Error("Response body is null");
|
|
1862
|
-
}
|
|
1863
|
-
for await (const record of this.streamProcessor(response.body)) {
|
|
1864
|
-
if (typeof record === "string") {
|
|
1865
|
-
onRecord(JSON.parse(record));
|
|
1866
|
-
} else {
|
|
1867
|
-
onRecord(record);
|
|
1868
|
-
}
|
|
1869
|
-
}
|
|
1870
|
-
}
|
|
1871
|
-
/**
|
|
1872
|
-
* Streams a response from the v-next network loop
|
|
1873
|
-
* @param params - Stream parameters including message
|
|
1874
|
-
* @returns Promise containing the results
|
|
1875
|
-
*/
|
|
1876
|
-
async loopStream(params, onRecord) {
|
|
1877
|
-
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1878
|
-
method: "POST",
|
|
1879
|
-
body: {
|
|
1880
|
-
...params,
|
|
1881
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1882
|
-
},
|
|
1883
|
-
stream: true
|
|
1884
|
-
});
|
|
1885
|
-
if (!response.ok) {
|
|
1886
|
-
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1887
|
-
}
|
|
1888
|
-
if (!response.body) {
|
|
1889
|
-
throw new Error("Response body is null");
|
|
1890
|
-
}
|
|
1891
|
-
for await (const record of this.streamProcessor(response.body)) {
|
|
1892
|
-
if (typeof record === "string") {
|
|
1893
|
-
onRecord(JSON.parse(record));
|
|
1894
|
-
} else {
|
|
1895
|
-
onRecord(record);
|
|
1896
|
-
}
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1899
|
-
};
|
|
1900
|
-
|
|
1901
2741
|
// src/client.ts
|
|
1902
2742
|
var MastraClient = class extends BaseResource {
|
|
2743
|
+
observability;
|
|
1903
2744
|
constructor(options) {
|
|
1904
2745
|
super(options);
|
|
2746
|
+
this.observability = new Observability(options);
|
|
1905
2747
|
}
|
|
1906
2748
|
/**
|
|
1907
2749
|
* Retrieves all available agents
|
|
2750
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1908
2751
|
* @returns Promise containing map of agent IDs to agent details
|
|
1909
2752
|
*/
|
|
1910
|
-
getAgents() {
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
acc[agentId] = new AGUIAdapter({
|
|
1919
|
-
agentId,
|
|
1920
|
-
agent,
|
|
1921
|
-
resourceId
|
|
1922
|
-
});
|
|
1923
|
-
return acc;
|
|
1924
|
-
},
|
|
1925
|
-
{}
|
|
1926
|
-
);
|
|
2753
|
+
getAgents(runtimeContext) {
|
|
2754
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2755
|
+
const searchParams = new URLSearchParams();
|
|
2756
|
+
if (runtimeContextParam) {
|
|
2757
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2758
|
+
}
|
|
2759
|
+
const queryString = searchParams.toString();
|
|
2760
|
+
return this.request(`/api/agents${queryString ? `?${queryString}` : ""}`);
|
|
1927
2761
|
}
|
|
1928
2762
|
/**
|
|
1929
2763
|
* Gets an agent instance by ID
|
|
@@ -1941,6 +2775,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1941
2775
|
getMemoryThreads(params) {
|
|
1942
2776
|
return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
|
|
1943
2777
|
}
|
|
2778
|
+
/**
|
|
2779
|
+
* Retrieves memory config for a resource
|
|
2780
|
+
* @param params - Parameters containing the resource ID
|
|
2781
|
+
* @returns Promise containing array of memory threads
|
|
2782
|
+
*/
|
|
2783
|
+
getMemoryConfig(params) {
|
|
2784
|
+
return this.request(`/api/memory/config?agentId=${params.agentId}`);
|
|
2785
|
+
}
|
|
1944
2786
|
/**
|
|
1945
2787
|
* Creates a new memory thread
|
|
1946
2788
|
* @param params - Parameters for creating the memory thread
|
|
@@ -1957,6 +2799,24 @@ var MastraClient = class extends BaseResource {
|
|
|
1957
2799
|
getMemoryThread(threadId, agentId) {
|
|
1958
2800
|
return new MemoryThread(this.options, threadId, agentId);
|
|
1959
2801
|
}
|
|
2802
|
+
getThreadMessages(threadId, opts = {}) {
|
|
2803
|
+
let url = "";
|
|
2804
|
+
if (opts.agentId) {
|
|
2805
|
+
url = `/api/memory/threads/${threadId}/messages?agentId=${opts.agentId}`;
|
|
2806
|
+
} else if (opts.networkId) {
|
|
2807
|
+
url = `/api/memory/network/threads/${threadId}/messages?networkId=${opts.networkId}`;
|
|
2808
|
+
}
|
|
2809
|
+
return this.request(url);
|
|
2810
|
+
}
|
|
2811
|
+
deleteThread(threadId, opts = {}) {
|
|
2812
|
+
let url = "";
|
|
2813
|
+
if (opts.agentId) {
|
|
2814
|
+
url = `/api/memory/threads/${threadId}?agentId=${opts.agentId}`;
|
|
2815
|
+
} else if (opts.networkId) {
|
|
2816
|
+
url = `/api/memory/network/threads/${threadId}?networkId=${opts.networkId}`;
|
|
2817
|
+
}
|
|
2818
|
+
return this.request(url, { method: "DELETE" });
|
|
2819
|
+
}
|
|
1960
2820
|
/**
|
|
1961
2821
|
* Saves messages to memory
|
|
1962
2822
|
* @param params - Parameters containing messages to save
|
|
@@ -2019,10 +2879,17 @@ var MastraClient = class extends BaseResource {
|
|
|
2019
2879
|
}
|
|
2020
2880
|
/**
|
|
2021
2881
|
* Retrieves all available tools
|
|
2882
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2022
2883
|
* @returns Promise containing map of tool IDs to tool details
|
|
2023
2884
|
*/
|
|
2024
|
-
getTools() {
|
|
2025
|
-
|
|
2885
|
+
getTools(runtimeContext) {
|
|
2886
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2887
|
+
const searchParams = new URLSearchParams();
|
|
2888
|
+
if (runtimeContextParam) {
|
|
2889
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2890
|
+
}
|
|
2891
|
+
const queryString = searchParams.toString();
|
|
2892
|
+
return this.request(`/api/tools${queryString ? `?${queryString}` : ""}`);
|
|
2026
2893
|
}
|
|
2027
2894
|
/**
|
|
2028
2895
|
* Gets a tool instance by ID
|
|
@@ -2030,29 +2897,21 @@ var MastraClient = class extends BaseResource {
|
|
|
2030
2897
|
* @returns Tool instance
|
|
2031
2898
|
*/
|
|
2032
2899
|
getTool(toolId) {
|
|
2033
|
-
return new
|
|
2034
|
-
}
|
|
2035
|
-
/**
|
|
2036
|
-
* Retrieves all available legacy workflows
|
|
2037
|
-
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
2038
|
-
*/
|
|
2039
|
-
getLegacyWorkflows() {
|
|
2040
|
-
return this.request("/api/workflows/legacy");
|
|
2041
|
-
}
|
|
2042
|
-
/**
|
|
2043
|
-
* Gets a legacy workflow instance by ID
|
|
2044
|
-
* @param workflowId - ID of the legacy workflow to retrieve
|
|
2045
|
-
* @returns Legacy Workflow instance
|
|
2046
|
-
*/
|
|
2047
|
-
getLegacyWorkflow(workflowId) {
|
|
2048
|
-
return new LegacyWorkflow(this.options, workflowId);
|
|
2900
|
+
return new Tool(this.options, toolId);
|
|
2049
2901
|
}
|
|
2050
2902
|
/**
|
|
2051
2903
|
* Retrieves all available workflows
|
|
2904
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2052
2905
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
2053
2906
|
*/
|
|
2054
|
-
getWorkflows() {
|
|
2055
|
-
|
|
2907
|
+
getWorkflows(runtimeContext) {
|
|
2908
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2909
|
+
const searchParams = new URLSearchParams();
|
|
2910
|
+
if (runtimeContextParam) {
|
|
2911
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2912
|
+
}
|
|
2913
|
+
const queryString = searchParams.toString();
|
|
2914
|
+
return this.request(`/api/workflows${queryString ? `?${queryString}` : ""}`);
|
|
2056
2915
|
}
|
|
2057
2916
|
/**
|
|
2058
2917
|
* Gets a workflow instance by ID
|
|
@@ -2062,6 +2921,20 @@ var MastraClient = class extends BaseResource {
|
|
|
2062
2921
|
getWorkflow(workflowId) {
|
|
2063
2922
|
return new Workflow(this.options, workflowId);
|
|
2064
2923
|
}
|
|
2924
|
+
/**
|
|
2925
|
+
* Gets all available agent builder actions
|
|
2926
|
+
* @returns Promise containing map of action IDs to action details
|
|
2927
|
+
*/
|
|
2928
|
+
getAgentBuilderActions() {
|
|
2929
|
+
return this.request("/api/agent-builder/");
|
|
2930
|
+
}
|
|
2931
|
+
/**
|
|
2932
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
2933
|
+
* @returns AgentBuilder instance
|
|
2934
|
+
*/
|
|
2935
|
+
getAgentBuilderAction(actionId) {
|
|
2936
|
+
return new AgentBuilder(this.options, actionId);
|
|
2937
|
+
}
|
|
2065
2938
|
/**
|
|
2066
2939
|
* Gets a vector instance by name
|
|
2067
2940
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -2206,36 +3079,6 @@ var MastraClient = class extends BaseResource {
|
|
|
2206
3079
|
return this.request(`/api/telemetry`);
|
|
2207
3080
|
}
|
|
2208
3081
|
}
|
|
2209
|
-
/**
|
|
2210
|
-
* Retrieves all available networks
|
|
2211
|
-
* @returns Promise containing map of network IDs to network details
|
|
2212
|
-
*/
|
|
2213
|
-
getNetworks() {
|
|
2214
|
-
return this.request("/api/networks");
|
|
2215
|
-
}
|
|
2216
|
-
/**
|
|
2217
|
-
* Retrieves all available vNext networks
|
|
2218
|
-
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2219
|
-
*/
|
|
2220
|
-
getVNextNetworks() {
|
|
2221
|
-
return this.request("/api/networks/v-next");
|
|
2222
|
-
}
|
|
2223
|
-
/**
|
|
2224
|
-
* Gets a network instance by ID
|
|
2225
|
-
* @param networkId - ID of the network to retrieve
|
|
2226
|
-
* @returns Network instance
|
|
2227
|
-
*/
|
|
2228
|
-
getNetwork(networkId) {
|
|
2229
|
-
return new Network(this.options, networkId);
|
|
2230
|
-
}
|
|
2231
|
-
/**
|
|
2232
|
-
* Gets a vNext network instance by ID
|
|
2233
|
-
* @param networkId - ID of the vNext network to retrieve
|
|
2234
|
-
* @returns vNext Network instance
|
|
2235
|
-
*/
|
|
2236
|
-
getVNextNetwork(networkId) {
|
|
2237
|
-
return new VNextNetwork(this.options, networkId);
|
|
2238
|
-
}
|
|
2239
3082
|
/**
|
|
2240
3083
|
* Retrieves a list of available MCP servers.
|
|
2241
3084
|
* @param params - Optional parameters for pagination (limit, offset).
|
|
@@ -2340,7 +3183,7 @@ var MastraClient = class extends BaseResource {
|
|
|
2340
3183
|
* @returns Promise containing the scorer
|
|
2341
3184
|
*/
|
|
2342
3185
|
getScorer(scorerId) {
|
|
2343
|
-
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3186
|
+
return this.request(`/api/scores/scorers/${encodeURIComponent(scorerId)}`);
|
|
2344
3187
|
}
|
|
2345
3188
|
getScoresByScorerId(params) {
|
|
2346
3189
|
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
@@ -2358,7 +3201,7 @@ var MastraClient = class extends BaseResource {
|
|
|
2358
3201
|
searchParams.set("perPage", String(perPage));
|
|
2359
3202
|
}
|
|
2360
3203
|
const queryString = searchParams.toString();
|
|
2361
|
-
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3204
|
+
return this.request(`/api/scores/scorer/${encodeURIComponent(scorerId)}${queryString ? `?${queryString}` : ""}`);
|
|
2362
3205
|
}
|
|
2363
3206
|
/**
|
|
2364
3207
|
* Retrieves scores by run ID
|
|
@@ -2375,7 +3218,7 @@ var MastraClient = class extends BaseResource {
|
|
|
2375
3218
|
searchParams.set("perPage", String(perPage));
|
|
2376
3219
|
}
|
|
2377
3220
|
const queryString = searchParams.toString();
|
|
2378
|
-
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3221
|
+
return this.request(`/api/scores/run/${encodeURIComponent(runId)}${queryString ? `?${queryString}` : ""}`);
|
|
2379
3222
|
}
|
|
2380
3223
|
/**
|
|
2381
3224
|
* Retrieves scores by entity ID and type
|
|
@@ -2392,7 +3235,9 @@ var MastraClient = class extends BaseResource {
|
|
|
2392
3235
|
searchParams.set("perPage", String(perPage));
|
|
2393
3236
|
}
|
|
2394
3237
|
const queryString = searchParams.toString();
|
|
2395
|
-
return this.request(
|
|
3238
|
+
return this.request(
|
|
3239
|
+
`/api/scores/entity/${encodeURIComponent(entityType)}/${encodeURIComponent(entityId)}${queryString ? `?${queryString}` : ""}`
|
|
3240
|
+
);
|
|
2396
3241
|
}
|
|
2397
3242
|
/**
|
|
2398
3243
|
* Saves a score
|
|
@@ -2405,8 +3250,46 @@ var MastraClient = class extends BaseResource {
|
|
|
2405
3250
|
body: params
|
|
2406
3251
|
});
|
|
2407
3252
|
}
|
|
3253
|
+
/**
|
|
3254
|
+
* Retrieves model providers with available keys
|
|
3255
|
+
* @returns Promise containing model providers with available keys
|
|
3256
|
+
*/
|
|
3257
|
+
getModelProviders() {
|
|
3258
|
+
return this.request(`/api/model-providers`);
|
|
3259
|
+
}
|
|
3260
|
+
getAITrace(traceId) {
|
|
3261
|
+
return this.observability.getTrace(traceId);
|
|
3262
|
+
}
|
|
3263
|
+
getAITraces(params) {
|
|
3264
|
+
return this.observability.getTraces(params);
|
|
3265
|
+
}
|
|
3266
|
+
getScoresBySpan(params) {
|
|
3267
|
+
return this.observability.getScoresBySpan(params);
|
|
3268
|
+
}
|
|
3269
|
+
score(params) {
|
|
3270
|
+
return this.observability.score(params);
|
|
3271
|
+
}
|
|
2408
3272
|
};
|
|
2409
3273
|
|
|
2410
|
-
|
|
3274
|
+
// src/tools.ts
|
|
3275
|
+
var ClientTool = class {
|
|
3276
|
+
id;
|
|
3277
|
+
description;
|
|
3278
|
+
inputSchema;
|
|
3279
|
+
outputSchema;
|
|
3280
|
+
execute;
|
|
3281
|
+
constructor(opts) {
|
|
3282
|
+
this.id = opts.id;
|
|
3283
|
+
this.description = opts.description;
|
|
3284
|
+
this.inputSchema = opts.inputSchema;
|
|
3285
|
+
this.outputSchema = opts.outputSchema;
|
|
3286
|
+
this.execute = opts.execute;
|
|
3287
|
+
}
|
|
3288
|
+
};
|
|
3289
|
+
function createTool(opts) {
|
|
3290
|
+
return new ClientTool(opts);
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
export { ClientTool, MastraClient, createTool };
|
|
2411
3294
|
//# sourceMappingURL=index.js.map
|
|
2412
3295
|
//# sourceMappingURL=index.js.map
|