@mastra/client-js 0.0.0-vector-sources-20250516175436 → 0.0.0-vector-extension-schema-20250922130418
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 +1743 -2
- package/LICENSE.md +11 -42
- package/README.md +8 -9
- package/dist/client.d.ts +290 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +2622 -504
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -883
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2623 -507
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent-builder.d.ts +160 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +175 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +12 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/legacy-workflow.d.ts +90 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +28 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +19 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +24 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +43 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +51 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +254 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/tools.d.ts +22 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +464 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +11 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/package.json +37 -20
- package/dist/index.d.cts +0 -883
- package/eslint.config.js +0 -6
- package/src/adapters/agui.test.ts +0 -180
- package/src/adapters/agui.ts +0 -239
- package/src/client.ts +0 -335
- package/src/example.ts +0 -64
- package/src/index.test.ts +0 -830
- package/src/index.ts +0 -2
- package/src/resources/a2a.ts +0 -88
- package/src/resources/agent.ts +0 -196
- package/src/resources/base.ts +0 -70
- package/src/resources/index.ts +0 -10
- package/src/resources/mcp-tool.ts +0 -48
- package/src/resources/memory-thread.ts +0 -63
- package/src/resources/network.ts +0 -86
- package/src/resources/tool.ts +0 -44
- package/src/resources/vector.ts +0 -83
- package/src/resources/vnext-workflow.ts +0 -261
- package/src/resources/workflow.ts +0 -251
- package/src/types.ts +0 -308
- package/src/utils/zod-to-json-schema.ts +0 -10
- package/tsconfig.json +0 -5
- package/vitest.config.js +0 -8
package/dist/index.js
CHANGED
|
@@ -1,199 +1,129 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
2
|
+
import { v4 } from '@lukeed/uuid';
|
|
3
|
+
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
4
|
+
import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
5
|
+
import { z } from 'zod';
|
|
5
6
|
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
7
|
|
|
7
|
-
// src/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
super({
|
|
13
|
-
agentId,
|
|
14
|
-
...rest
|
|
15
|
-
});
|
|
16
|
-
this.agent = agent;
|
|
17
|
-
this.resourceId = resourceId;
|
|
18
|
-
}
|
|
19
|
-
run(input) {
|
|
20
|
-
return new Observable((subscriber) => {
|
|
21
|
-
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
22
|
-
subscriber.next({
|
|
23
|
-
type: EventType.RUN_STARTED,
|
|
24
|
-
threadId: input.threadId,
|
|
25
|
-
runId: input.runId
|
|
26
|
-
});
|
|
27
|
-
this.agent.stream({
|
|
28
|
-
threadId: input.threadId,
|
|
29
|
-
resourceId: this.resourceId ?? "",
|
|
30
|
-
runId: input.runId,
|
|
31
|
-
messages: convertedMessages,
|
|
32
|
-
clientTools: input.tools.reduce(
|
|
33
|
-
(acc, tool) => {
|
|
34
|
-
acc[tool.name] = {
|
|
35
|
-
id: tool.name,
|
|
36
|
-
description: tool.description,
|
|
37
|
-
inputSchema: tool.parameters
|
|
38
|
-
};
|
|
39
|
-
return acc;
|
|
40
|
-
},
|
|
41
|
-
{}
|
|
42
|
-
)
|
|
43
|
-
}).then((response) => {
|
|
44
|
-
let currentMessageId = void 0;
|
|
45
|
-
let isInTextMessage = false;
|
|
46
|
-
return response.processDataStream({
|
|
47
|
-
onTextPart: (text) => {
|
|
48
|
-
if (currentMessageId === void 0) {
|
|
49
|
-
currentMessageId = generateUUID();
|
|
50
|
-
const message2 = {
|
|
51
|
-
type: EventType.TEXT_MESSAGE_START,
|
|
52
|
-
messageId: currentMessageId,
|
|
53
|
-
role: "assistant"
|
|
54
|
-
};
|
|
55
|
-
subscriber.next(message2);
|
|
56
|
-
isInTextMessage = true;
|
|
57
|
-
}
|
|
58
|
-
const message = {
|
|
59
|
-
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
60
|
-
messageId: currentMessageId,
|
|
61
|
-
delta: text
|
|
62
|
-
};
|
|
63
|
-
subscriber.next(message);
|
|
64
|
-
},
|
|
65
|
-
onFinishMessagePart: () => {
|
|
66
|
-
if (currentMessageId !== void 0) {
|
|
67
|
-
const message = {
|
|
68
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
69
|
-
messageId: currentMessageId
|
|
70
|
-
};
|
|
71
|
-
subscriber.next(message);
|
|
72
|
-
isInTextMessage = false;
|
|
73
|
-
}
|
|
74
|
-
subscriber.next({
|
|
75
|
-
type: EventType.RUN_FINISHED,
|
|
76
|
-
threadId: input.threadId,
|
|
77
|
-
runId: input.runId
|
|
78
|
-
});
|
|
79
|
-
subscriber.complete();
|
|
80
|
-
},
|
|
81
|
-
onToolCallPart(streamPart) {
|
|
82
|
-
const parentMessageId = currentMessageId || generateUUID();
|
|
83
|
-
if (isInTextMessage) {
|
|
84
|
-
const message = {
|
|
85
|
-
type: EventType.TEXT_MESSAGE_END,
|
|
86
|
-
messageId: parentMessageId
|
|
87
|
-
};
|
|
88
|
-
subscriber.next(message);
|
|
89
|
-
isInTextMessage = false;
|
|
90
|
-
}
|
|
91
|
-
subscriber.next({
|
|
92
|
-
type: EventType.TOOL_CALL_START,
|
|
93
|
-
toolCallId: streamPart.toolCallId,
|
|
94
|
-
toolCallName: streamPart.toolName,
|
|
95
|
-
parentMessageId
|
|
96
|
-
});
|
|
97
|
-
subscriber.next({
|
|
98
|
-
type: EventType.TOOL_CALL_ARGS,
|
|
99
|
-
toolCallId: streamPart.toolCallId,
|
|
100
|
-
delta: JSON.stringify(streamPart.args),
|
|
101
|
-
parentMessageId
|
|
102
|
-
});
|
|
103
|
-
subscriber.next({
|
|
104
|
-
type: EventType.TOOL_CALL_END,
|
|
105
|
-
toolCallId: streamPart.toolCallId,
|
|
106
|
-
parentMessageId
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
}).catch((error) => {
|
|
111
|
-
console.error("error", error);
|
|
112
|
-
subscriber.error(error);
|
|
113
|
-
});
|
|
114
|
-
return () => {
|
|
115
|
-
};
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
function generateUUID() {
|
|
120
|
-
if (typeof crypto !== "undefined") {
|
|
121
|
-
if (typeof crypto.randomUUID === "function") {
|
|
122
|
-
return crypto.randomUUID();
|
|
123
|
-
}
|
|
124
|
-
if (typeof crypto.getRandomValues === "function") {
|
|
125
|
-
const buffer = new Uint8Array(16);
|
|
126
|
-
crypto.getRandomValues(buffer);
|
|
127
|
-
buffer[6] = buffer[6] & 15 | 64;
|
|
128
|
-
buffer[8] = buffer[8] & 63 | 128;
|
|
129
|
-
let hex = "";
|
|
130
|
-
for (let i = 0; i < 16; i++) {
|
|
131
|
-
hex += buffer[i].toString(16).padStart(2, "0");
|
|
132
|
-
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
133
|
-
}
|
|
134
|
-
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());
|
|
135
13
|
}
|
|
14
|
+
return runtimeContext;
|
|
136
15
|
}
|
|
137
|
-
return
|
|
138
|
-
const r = Math.random() * 16 | 0;
|
|
139
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
140
|
-
return v.toString(16);
|
|
141
|
-
});
|
|
16
|
+
return void 0;
|
|
142
17
|
}
|
|
143
|
-
function
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (message.role === "assistant") {
|
|
147
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
148
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
149
|
-
parts.push({
|
|
150
|
-
type: "tool-call",
|
|
151
|
-
toolCallId: toolCall.id,
|
|
152
|
-
toolName: toolCall.function.name,
|
|
153
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
result.push({
|
|
157
|
-
role: "assistant",
|
|
158
|
-
content: parts
|
|
159
|
-
});
|
|
160
|
-
if (message.toolCalls?.length) {
|
|
161
|
-
result.push({
|
|
162
|
-
role: "tool",
|
|
163
|
-
content: message.toolCalls.map((toolCall) => ({
|
|
164
|
-
type: "tool-result",
|
|
165
|
-
toolCallId: toolCall.id,
|
|
166
|
-
toolName: toolCall.function.name,
|
|
167
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
168
|
-
}))
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
} else if (message.role === "user") {
|
|
172
|
-
result.push({
|
|
173
|
-
role: "user",
|
|
174
|
-
content: message.content || ""
|
|
175
|
-
});
|
|
176
|
-
} else if (message.role === "tool") {
|
|
177
|
-
result.push({
|
|
178
|
-
role: "tool",
|
|
179
|
-
content: [
|
|
180
|
-
{
|
|
181
|
-
type: "tool-result",
|
|
182
|
-
toolCallId: message.toolCallId,
|
|
183
|
-
toolName: "unknown",
|
|
184
|
-
result: message.content
|
|
185
|
-
}
|
|
186
|
-
]
|
|
187
|
-
});
|
|
188
|
-
}
|
|
18
|
+
function base64RuntimeContext(runtimeContext) {
|
|
19
|
+
if (runtimeContext) {
|
|
20
|
+
return btoa(JSON.stringify(runtimeContext));
|
|
189
21
|
}
|
|
190
|
-
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";
|
|
191
34
|
}
|
|
192
35
|
function zodToJsonSchema(zodSchema) {
|
|
193
|
-
if (!(zodSchema
|
|
36
|
+
if (!isZodType(zodSchema)) {
|
|
194
37
|
return zodSchema;
|
|
195
38
|
}
|
|
196
|
-
|
|
39
|
+
if ("toJSONSchema" in z) {
|
|
40
|
+
const fn = "toJSONSchema";
|
|
41
|
+
return z[fn].call(z, zodSchema);
|
|
42
|
+
}
|
|
43
|
+
return originalZodToJsonSchema(zodSchema, { $refStrategy: "relative" });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/utils/process-client-tools.ts
|
|
47
|
+
function processClientTools(clientTools) {
|
|
48
|
+
if (!clientTools) {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
return Object.fromEntries(
|
|
52
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
53
|
+
if (isVercelTool(value)) {
|
|
54
|
+
return [
|
|
55
|
+
key,
|
|
56
|
+
{
|
|
57
|
+
...value,
|
|
58
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
} else {
|
|
62
|
+
return [
|
|
63
|
+
key,
|
|
64
|
+
{
|
|
65
|
+
...value,
|
|
66
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
|
|
67
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
}
|
|
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
|
+
});
|
|
197
127
|
}
|
|
198
128
|
|
|
199
129
|
// src/resources/base.ts
|
|
@@ -210,18 +140,21 @@ var BaseResource = class {
|
|
|
210
140
|
*/
|
|
211
141
|
async request(path, options = {}) {
|
|
212
142
|
let lastError = null;
|
|
213
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
143
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
214
144
|
let delay = backoffMs;
|
|
215
145
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
216
146
|
try {
|
|
217
147
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
218
148
|
...options,
|
|
219
149
|
headers: {
|
|
150
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
220
151
|
...headers,
|
|
221
152
|
...options.headers
|
|
222
153
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
223
154
|
// 'x-mastra-client-type': 'js',
|
|
224
155
|
},
|
|
156
|
+
signal: this.options.abortSignal,
|
|
157
|
+
credentials: options.credentials ?? credentials,
|
|
225
158
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
226
159
|
});
|
|
227
160
|
if (!response.ok) {
|
|
@@ -256,6 +189,63 @@ var BaseResource = class {
|
|
|
256
189
|
};
|
|
257
190
|
|
|
258
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
|
+
},
|
|
218
|
+
{
|
|
219
|
+
messages: response.messages,
|
|
220
|
+
toolCallId: toolCall?.toolCallId
|
|
221
|
+
}
|
|
222
|
+
);
|
|
223
|
+
const updatedMessages = [
|
|
224
|
+
{
|
|
225
|
+
role: "user",
|
|
226
|
+
content: params.messages
|
|
227
|
+
},
|
|
228
|
+
...response.response.messages,
|
|
229
|
+
{
|
|
230
|
+
role: "tool",
|
|
231
|
+
content: [
|
|
232
|
+
{
|
|
233
|
+
type: "tool-result",
|
|
234
|
+
toolCallId: toolCall.toolCallId,
|
|
235
|
+
toolName: toolCall.toolName,
|
|
236
|
+
result
|
|
237
|
+
}
|
|
238
|
+
]
|
|
239
|
+
}
|
|
240
|
+
];
|
|
241
|
+
return respondFn({
|
|
242
|
+
...params,
|
|
243
|
+
messages: updatedMessages
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
259
249
|
var AgentVoice = class extends BaseResource {
|
|
260
250
|
constructor(options, agentId) {
|
|
261
251
|
super(options);
|
|
@@ -297,10 +287,21 @@ var AgentVoice = class extends BaseResource {
|
|
|
297
287
|
}
|
|
298
288
|
/**
|
|
299
289
|
* Get available speakers for the agent's voice provider
|
|
290
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
291
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
300
292
|
* @returns Promise containing list of available speakers
|
|
301
293
|
*/
|
|
302
|
-
getSpeakers() {
|
|
303
|
-
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
294
|
+
getSpeakers(runtimeContext) {
|
|
295
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers${runtimeContextQueryString(runtimeContext)}`);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Get the listener configuration for the agent's voice provider
|
|
299
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
300
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
301
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
302
|
+
*/
|
|
303
|
+
getListener(runtimeContext) {
|
|
304
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener${runtimeContextQueryString(runtimeContext)}`);
|
|
304
305
|
}
|
|
305
306
|
};
|
|
306
307
|
var Agent = class extends BaseResource {
|
|
@@ -312,178 +313,1052 @@ var Agent = class extends BaseResource {
|
|
|
312
313
|
voice;
|
|
313
314
|
/**
|
|
314
315
|
* Retrieves details about the agent
|
|
316
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
315
317
|
* @returns Promise containing agent details including model and instructions
|
|
316
318
|
*/
|
|
317
|
-
details() {
|
|
318
|
-
return this.request(`/api/agents/${this.agentId}`);
|
|
319
|
+
details(runtimeContext) {
|
|
320
|
+
return this.request(`/api/agents/${this.agentId}${runtimeContextQueryString(runtimeContext)}`);
|
|
319
321
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
322
|
+
async generate(params) {
|
|
323
|
+
console.warn(
|
|
324
|
+
"Deprecation NOTICE:Generate method will switch to use generateVNext implementation September 23rd, 2025. Please use generateLegacy if you don't want to upgrade just yet."
|
|
325
|
+
);
|
|
326
|
+
return this.generateLegacy(params);
|
|
327
|
+
}
|
|
328
|
+
async generateLegacy(params) {
|
|
326
329
|
const processedParams = {
|
|
327
330
|
...params,
|
|
328
331
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
329
332
|
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
330
|
-
runtimeContext:
|
|
333
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
334
|
+
clientTools: processClientTools(params.clientTools)
|
|
331
335
|
};
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
337
|
+
const response = await this.request(
|
|
338
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
339
|
+
{
|
|
340
|
+
method: "POST",
|
|
341
|
+
body: processedParams
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
if (response.finishReason === "tool-calls") {
|
|
345
|
+
const toolCalls = response.toolCalls;
|
|
346
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
347
|
+
return response;
|
|
348
|
+
}
|
|
349
|
+
for (const toolCall of toolCalls) {
|
|
350
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
351
|
+
if (clientTool && clientTool.execute) {
|
|
352
|
+
const result = await clientTool.execute(
|
|
353
|
+
{
|
|
354
|
+
context: toolCall?.args,
|
|
355
|
+
runId,
|
|
356
|
+
resourceId,
|
|
357
|
+
threadId,
|
|
358
|
+
runtimeContext,
|
|
359
|
+
tracingContext: { currentSpan: void 0 }
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
messages: response.messages,
|
|
363
|
+
toolCallId: toolCall?.toolCallId
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
const updatedMessages = [
|
|
367
|
+
{
|
|
368
|
+
role: "user",
|
|
369
|
+
content: params.messages
|
|
370
|
+
},
|
|
371
|
+
...response.response.messages,
|
|
372
|
+
{
|
|
373
|
+
role: "tool",
|
|
374
|
+
content: [
|
|
375
|
+
{
|
|
376
|
+
type: "tool-result",
|
|
377
|
+
toolCallId: toolCall.toolCallId,
|
|
378
|
+
toolName: toolCall.toolName,
|
|
379
|
+
result
|
|
380
|
+
}
|
|
381
|
+
]
|
|
382
|
+
}
|
|
383
|
+
];
|
|
384
|
+
return this.generate({
|
|
385
|
+
...params,
|
|
386
|
+
messages: updatedMessages
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return response;
|
|
336
392
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
393
|
+
async generateVNext(messagesOrParams, options) {
|
|
394
|
+
let params;
|
|
395
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
396
|
+
params = messagesOrParams;
|
|
397
|
+
} else {
|
|
398
|
+
params = {
|
|
399
|
+
messages: messagesOrParams,
|
|
400
|
+
...options
|
|
401
|
+
};
|
|
402
|
+
}
|
|
343
403
|
const processedParams = {
|
|
344
404
|
...params,
|
|
345
405
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
346
|
-
|
|
347
|
-
|
|
406
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
407
|
+
clientTools: processClientTools(params.clientTools),
|
|
408
|
+
structuredOutput: params.structuredOutput ? {
|
|
409
|
+
...params.structuredOutput,
|
|
410
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
411
|
+
} : void 0
|
|
348
412
|
};
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
response.
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
413
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
414
|
+
const response = await this.request(
|
|
415
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
416
|
+
{
|
|
417
|
+
method: "POST",
|
|
418
|
+
body: processedParams
|
|
419
|
+
}
|
|
420
|
+
);
|
|
421
|
+
if (response.finishReason === "tool-calls") {
|
|
422
|
+
return executeToolCallAndRespond({
|
|
423
|
+
response,
|
|
424
|
+
params,
|
|
425
|
+
runId,
|
|
426
|
+
resourceId,
|
|
427
|
+
threadId,
|
|
428
|
+
runtimeContext,
|
|
429
|
+
respondFn: this.generateVNext.bind(this)
|
|
361
430
|
});
|
|
362
|
-
}
|
|
431
|
+
}
|
|
363
432
|
return response;
|
|
364
433
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
434
|
+
async processChatResponse({
|
|
435
|
+
stream,
|
|
436
|
+
update,
|
|
437
|
+
onToolCall,
|
|
438
|
+
onFinish,
|
|
439
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
440
|
+
lastMessage
|
|
441
|
+
}) {
|
|
442
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
443
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
444
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
445
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
446
|
+
}, 0) ?? 0) : 0;
|
|
447
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
448
|
+
id: v4(),
|
|
449
|
+
createdAt: getCurrentDate(),
|
|
450
|
+
role: "assistant",
|
|
451
|
+
content: "",
|
|
452
|
+
parts: []
|
|
383
453
|
};
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
454
|
+
let currentTextPart = void 0;
|
|
455
|
+
let currentReasoningPart = void 0;
|
|
456
|
+
let currentReasoningTextDetail = void 0;
|
|
457
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
458
|
+
const part = message.parts.find(
|
|
459
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
460
|
+
);
|
|
461
|
+
if (part != null) {
|
|
462
|
+
part.toolInvocation = invocation;
|
|
463
|
+
} else {
|
|
464
|
+
message.parts.push({
|
|
465
|
+
type: "tool-invocation",
|
|
466
|
+
toolInvocation: invocation
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
const data = [];
|
|
471
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
472
|
+
const partialToolCalls = {};
|
|
473
|
+
let usage = {
|
|
474
|
+
completionTokens: NaN,
|
|
475
|
+
promptTokens: NaN,
|
|
476
|
+
totalTokens: NaN
|
|
477
|
+
};
|
|
478
|
+
let finishReason = "unknown";
|
|
479
|
+
function execUpdate() {
|
|
480
|
+
const copiedData = [...data];
|
|
481
|
+
if (messageAnnotations?.length) {
|
|
482
|
+
message.annotations = messageAnnotations;
|
|
483
|
+
}
|
|
484
|
+
const copiedMessage = {
|
|
485
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
486
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
487
|
+
...structuredClone(message),
|
|
488
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
489
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
490
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
491
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
492
|
+
// forwarded to rendering):
|
|
493
|
+
revisionId: v4()
|
|
494
|
+
};
|
|
495
|
+
update({
|
|
496
|
+
message: copiedMessage,
|
|
497
|
+
data: copiedData,
|
|
498
|
+
replaceLastMessage
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
await processDataStream({
|
|
502
|
+
stream,
|
|
503
|
+
onTextPart(value) {
|
|
504
|
+
if (currentTextPart == null) {
|
|
505
|
+
currentTextPart = {
|
|
506
|
+
type: "text",
|
|
507
|
+
text: value
|
|
508
|
+
};
|
|
509
|
+
message.parts.push(currentTextPart);
|
|
510
|
+
} else {
|
|
511
|
+
currentTextPart.text += value;
|
|
512
|
+
}
|
|
513
|
+
message.content += value;
|
|
514
|
+
execUpdate();
|
|
515
|
+
},
|
|
516
|
+
onReasoningPart(value) {
|
|
517
|
+
if (currentReasoningTextDetail == null) {
|
|
518
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
519
|
+
if (currentReasoningPart != null) {
|
|
520
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
521
|
+
}
|
|
522
|
+
} else {
|
|
523
|
+
currentReasoningTextDetail.text += value;
|
|
524
|
+
}
|
|
525
|
+
if (currentReasoningPart == null) {
|
|
526
|
+
currentReasoningPart = {
|
|
527
|
+
type: "reasoning",
|
|
528
|
+
reasoning: value,
|
|
529
|
+
details: [currentReasoningTextDetail]
|
|
530
|
+
};
|
|
531
|
+
message.parts.push(currentReasoningPart);
|
|
532
|
+
} else {
|
|
533
|
+
currentReasoningPart.reasoning += value;
|
|
534
|
+
}
|
|
535
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
536
|
+
execUpdate();
|
|
537
|
+
},
|
|
538
|
+
onReasoningSignaturePart(value) {
|
|
539
|
+
if (currentReasoningTextDetail != null) {
|
|
540
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
541
|
+
}
|
|
542
|
+
},
|
|
543
|
+
onRedactedReasoningPart(value) {
|
|
544
|
+
if (currentReasoningPart == null) {
|
|
545
|
+
currentReasoningPart = {
|
|
546
|
+
type: "reasoning",
|
|
547
|
+
reasoning: "",
|
|
548
|
+
details: []
|
|
549
|
+
};
|
|
550
|
+
message.parts.push(currentReasoningPart);
|
|
551
|
+
}
|
|
552
|
+
currentReasoningPart.details.push({
|
|
553
|
+
type: "redacted",
|
|
554
|
+
data: value.data
|
|
555
|
+
});
|
|
556
|
+
currentReasoningTextDetail = void 0;
|
|
557
|
+
execUpdate();
|
|
558
|
+
},
|
|
559
|
+
onFilePart(value) {
|
|
560
|
+
message.parts.push({
|
|
561
|
+
type: "file",
|
|
562
|
+
mimeType: value.mimeType,
|
|
563
|
+
data: value.data
|
|
564
|
+
});
|
|
565
|
+
execUpdate();
|
|
566
|
+
},
|
|
567
|
+
onSourcePart(value) {
|
|
568
|
+
message.parts.push({
|
|
569
|
+
type: "source",
|
|
570
|
+
source: value
|
|
571
|
+
});
|
|
572
|
+
execUpdate();
|
|
573
|
+
},
|
|
574
|
+
onToolCallStreamingStartPart(value) {
|
|
575
|
+
if (message.toolInvocations == null) {
|
|
576
|
+
message.toolInvocations = [];
|
|
577
|
+
}
|
|
578
|
+
partialToolCalls[value.toolCallId] = {
|
|
579
|
+
text: "",
|
|
580
|
+
step,
|
|
581
|
+
toolName: value.toolName,
|
|
582
|
+
index: message.toolInvocations.length
|
|
583
|
+
};
|
|
584
|
+
const invocation = {
|
|
585
|
+
state: "partial-call",
|
|
586
|
+
step,
|
|
587
|
+
toolCallId: value.toolCallId,
|
|
588
|
+
toolName: value.toolName,
|
|
589
|
+
args: void 0
|
|
590
|
+
};
|
|
591
|
+
message.toolInvocations.push(invocation);
|
|
592
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
593
|
+
execUpdate();
|
|
594
|
+
},
|
|
595
|
+
onToolCallDeltaPart(value) {
|
|
596
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
597
|
+
partialToolCall.text += value.argsTextDelta;
|
|
598
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
599
|
+
const invocation = {
|
|
600
|
+
state: "partial-call",
|
|
601
|
+
step: partialToolCall.step,
|
|
602
|
+
toolCallId: value.toolCallId,
|
|
603
|
+
toolName: partialToolCall.toolName,
|
|
604
|
+
args: partialArgs
|
|
605
|
+
};
|
|
606
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
607
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
608
|
+
execUpdate();
|
|
609
|
+
},
|
|
610
|
+
async onToolCallPart(value) {
|
|
611
|
+
const invocation = {
|
|
612
|
+
state: "call",
|
|
613
|
+
step,
|
|
614
|
+
...value
|
|
615
|
+
};
|
|
616
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
617
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
618
|
+
} else {
|
|
619
|
+
if (message.toolInvocations == null) {
|
|
620
|
+
message.toolInvocations = [];
|
|
621
|
+
}
|
|
622
|
+
message.toolInvocations.push(invocation);
|
|
623
|
+
}
|
|
624
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
625
|
+
execUpdate();
|
|
626
|
+
if (onToolCall) {
|
|
627
|
+
const result = await onToolCall({ toolCall: value });
|
|
628
|
+
if (result != null) {
|
|
629
|
+
const invocation2 = {
|
|
630
|
+
state: "result",
|
|
631
|
+
step,
|
|
632
|
+
...value,
|
|
633
|
+
result
|
|
634
|
+
};
|
|
635
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
636
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
637
|
+
execUpdate();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
onToolResultPart(value) {
|
|
642
|
+
const toolInvocations = message.toolInvocations;
|
|
643
|
+
if (toolInvocations == null) {
|
|
644
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
645
|
+
}
|
|
646
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
647
|
+
if (toolInvocationIndex === -1) {
|
|
648
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
649
|
+
}
|
|
650
|
+
const invocation = {
|
|
651
|
+
...toolInvocations[toolInvocationIndex],
|
|
652
|
+
state: "result",
|
|
653
|
+
...value
|
|
654
|
+
};
|
|
655
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
656
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
657
|
+
execUpdate();
|
|
658
|
+
},
|
|
659
|
+
onDataPart(value) {
|
|
660
|
+
data.push(...value);
|
|
661
|
+
execUpdate();
|
|
662
|
+
},
|
|
663
|
+
onMessageAnnotationsPart(value) {
|
|
664
|
+
if (messageAnnotations == null) {
|
|
665
|
+
messageAnnotations = [...value];
|
|
666
|
+
} else {
|
|
667
|
+
messageAnnotations.push(...value);
|
|
668
|
+
}
|
|
669
|
+
execUpdate();
|
|
670
|
+
},
|
|
671
|
+
onFinishStepPart(value) {
|
|
672
|
+
step += 1;
|
|
673
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
674
|
+
currentReasoningPart = void 0;
|
|
675
|
+
currentReasoningTextDetail = void 0;
|
|
676
|
+
},
|
|
677
|
+
onStartStepPart(value) {
|
|
678
|
+
if (!replaceLastMessage) {
|
|
679
|
+
message.id = value.messageId;
|
|
680
|
+
}
|
|
681
|
+
message.parts.push({ type: "step-start" });
|
|
682
|
+
execUpdate();
|
|
683
|
+
},
|
|
684
|
+
onFinishMessagePart(value) {
|
|
685
|
+
finishReason = value.finishReason;
|
|
686
|
+
if (value.usage != null) {
|
|
687
|
+
usage = value.usage;
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
onErrorPart(error) {
|
|
691
|
+
throw new Error(error);
|
|
692
|
+
}
|
|
387
693
|
});
|
|
694
|
+
onFinish?.({ message, finishReason, usage });
|
|
388
695
|
}
|
|
389
696
|
/**
|
|
390
|
-
*
|
|
391
|
-
* @
|
|
392
|
-
|
|
393
|
-
evals() {
|
|
394
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Retrieves live evaluation results for the agent
|
|
398
|
-
* @returns Promise containing live agent evaluations
|
|
399
|
-
*/
|
|
400
|
-
liveEvals() {
|
|
401
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
var Network = class extends BaseResource {
|
|
405
|
-
constructor(options, networkId) {
|
|
406
|
-
super(options);
|
|
407
|
-
this.networkId = networkId;
|
|
408
|
-
}
|
|
409
|
-
/**
|
|
410
|
-
* Retrieves details about the network
|
|
411
|
-
* @returns Promise containing network details
|
|
412
|
-
*/
|
|
413
|
-
details() {
|
|
414
|
-
return this.request(`/api/networks/${this.networkId}`);
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* Generates a response from the agent
|
|
418
|
-
* @param params - Generation parameters including prompt
|
|
419
|
-
* @returns Promise containing the generated response
|
|
697
|
+
* Streams a response from the agent
|
|
698
|
+
* @param params - Stream parameters including prompt
|
|
699
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
420
700
|
*/
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
};
|
|
427
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
428
|
-
method: "POST",
|
|
429
|
-
body: processedParams
|
|
430
|
-
});
|
|
701
|
+
async stream(params) {
|
|
702
|
+
console.warn(
|
|
703
|
+
"Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 23rd, 2025. Please use streamLegacy if you don't want to upgrade just yet."
|
|
704
|
+
);
|
|
705
|
+
return this.streamLegacy(params);
|
|
431
706
|
}
|
|
432
707
|
/**
|
|
433
708
|
* Streams a response from the agent
|
|
434
709
|
* @param params - Stream parameters including prompt
|
|
435
710
|
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
436
711
|
*/
|
|
437
|
-
async
|
|
712
|
+
async streamLegacy(params) {
|
|
438
713
|
const processedParams = {
|
|
439
714
|
...params,
|
|
440
|
-
output: zodToJsonSchema(params.output),
|
|
441
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
715
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
716
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
717
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
718
|
+
clientTools: processClientTools(params.clientTools)
|
|
442
719
|
};
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
720
|
+
const { readable, writable } = new TransformStream();
|
|
721
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
722
|
+
const streamResponse = new Response(readable, {
|
|
723
|
+
status: response.status,
|
|
724
|
+
statusText: response.statusText,
|
|
725
|
+
headers: response.headers
|
|
447
726
|
});
|
|
448
|
-
|
|
449
|
-
throw new Error("No response body");
|
|
450
|
-
}
|
|
451
|
-
response.processDataStream = async (options = {}) => {
|
|
727
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
452
728
|
await processDataStream({
|
|
453
|
-
stream:
|
|
729
|
+
stream: streamResponse.body,
|
|
454
730
|
...options
|
|
455
731
|
});
|
|
456
732
|
};
|
|
457
|
-
return
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
733
|
+
return streamResponse;
|
|
734
|
+
}
|
|
735
|
+
async processChatResponse_vNext({
|
|
736
|
+
stream,
|
|
737
|
+
update,
|
|
738
|
+
onToolCall,
|
|
739
|
+
onFinish,
|
|
740
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
741
|
+
lastMessage
|
|
742
|
+
}) {
|
|
743
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
744
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
745
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
746
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
747
|
+
}, 0) ?? 0) : 0;
|
|
748
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
749
|
+
id: v4(),
|
|
750
|
+
createdAt: getCurrentDate(),
|
|
751
|
+
role: "assistant",
|
|
752
|
+
content: "",
|
|
753
|
+
parts: []
|
|
754
|
+
};
|
|
755
|
+
let currentTextPart = void 0;
|
|
756
|
+
let currentReasoningPart = void 0;
|
|
757
|
+
let currentReasoningTextDetail = void 0;
|
|
758
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
759
|
+
const part = message.parts.find(
|
|
760
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
761
|
+
);
|
|
762
|
+
if (part != null) {
|
|
763
|
+
part.toolInvocation = invocation;
|
|
764
|
+
} else {
|
|
765
|
+
message.parts.push({
|
|
766
|
+
type: "tool-invocation",
|
|
767
|
+
toolInvocation: invocation
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
const data = [];
|
|
772
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
773
|
+
const partialToolCalls = {};
|
|
774
|
+
let usage = {
|
|
775
|
+
completionTokens: NaN,
|
|
776
|
+
promptTokens: NaN,
|
|
777
|
+
totalTokens: NaN
|
|
778
|
+
};
|
|
779
|
+
let finishReason = "unknown";
|
|
780
|
+
function execUpdate() {
|
|
781
|
+
const copiedData = [...data];
|
|
782
|
+
if (messageAnnotations?.length) {
|
|
783
|
+
message.annotations = messageAnnotations;
|
|
784
|
+
}
|
|
785
|
+
const copiedMessage = {
|
|
786
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
787
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
788
|
+
...structuredClone(message),
|
|
789
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
790
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
791
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
792
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
793
|
+
// forwarded to rendering):
|
|
794
|
+
revisionId: v4()
|
|
795
|
+
};
|
|
796
|
+
update({
|
|
797
|
+
message: copiedMessage,
|
|
798
|
+
data: copiedData,
|
|
799
|
+
replaceLastMessage
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
await processMastraStream({
|
|
803
|
+
stream,
|
|
804
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
805
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
806
|
+
onChunk: async (chunk) => {
|
|
807
|
+
switch (chunk.type) {
|
|
808
|
+
case "step-start": {
|
|
809
|
+
if (!replaceLastMessage) {
|
|
810
|
+
message.id = chunk.payload.messageId;
|
|
811
|
+
}
|
|
812
|
+
message.parts.push({ type: "step-start" });
|
|
813
|
+
execUpdate();
|
|
814
|
+
break;
|
|
815
|
+
}
|
|
816
|
+
case "text-delta": {
|
|
817
|
+
if (currentTextPart == null) {
|
|
818
|
+
currentTextPart = {
|
|
819
|
+
type: "text",
|
|
820
|
+
text: chunk.payload.text
|
|
821
|
+
};
|
|
822
|
+
message.parts.push(currentTextPart);
|
|
823
|
+
} else {
|
|
824
|
+
currentTextPart.text += chunk.payload.text;
|
|
825
|
+
}
|
|
826
|
+
message.content += chunk.payload.text;
|
|
827
|
+
execUpdate();
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
case "reasoning-delta": {
|
|
831
|
+
if (currentReasoningTextDetail == null) {
|
|
832
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
833
|
+
if (currentReasoningPart != null) {
|
|
834
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
835
|
+
}
|
|
836
|
+
} else {
|
|
837
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
838
|
+
}
|
|
839
|
+
if (currentReasoningPart == null) {
|
|
840
|
+
currentReasoningPart = {
|
|
841
|
+
type: "reasoning",
|
|
842
|
+
reasoning: chunk.payload.text,
|
|
843
|
+
details: [currentReasoningTextDetail]
|
|
844
|
+
};
|
|
845
|
+
message.parts.push(currentReasoningPart);
|
|
846
|
+
} else {
|
|
847
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
848
|
+
}
|
|
849
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
850
|
+
execUpdate();
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
case "file": {
|
|
854
|
+
message.parts.push({
|
|
855
|
+
type: "file",
|
|
856
|
+
mimeType: chunk.payload.mimeType,
|
|
857
|
+
data: chunk.payload.data
|
|
858
|
+
});
|
|
859
|
+
execUpdate();
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
case "source": {
|
|
863
|
+
message.parts.push({
|
|
864
|
+
type: "source",
|
|
865
|
+
source: chunk.payload.source
|
|
866
|
+
});
|
|
867
|
+
execUpdate();
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
case "tool-call": {
|
|
871
|
+
const invocation = {
|
|
872
|
+
state: "call",
|
|
873
|
+
step,
|
|
874
|
+
...chunk.payload
|
|
875
|
+
};
|
|
876
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
877
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
878
|
+
} else {
|
|
879
|
+
if (message.toolInvocations == null) {
|
|
880
|
+
message.toolInvocations = [];
|
|
881
|
+
}
|
|
882
|
+
message.toolInvocations.push(invocation);
|
|
883
|
+
}
|
|
884
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
885
|
+
execUpdate();
|
|
886
|
+
if (onToolCall) {
|
|
887
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
888
|
+
if (result != null) {
|
|
889
|
+
const invocation2 = {
|
|
890
|
+
state: "result",
|
|
891
|
+
step,
|
|
892
|
+
...chunk.payload,
|
|
893
|
+
result
|
|
894
|
+
};
|
|
895
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
896
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
897
|
+
execUpdate();
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
case "tool-call-input-streaming-start": {
|
|
902
|
+
if (message.toolInvocations == null) {
|
|
903
|
+
message.toolInvocations = [];
|
|
904
|
+
}
|
|
905
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
906
|
+
text: "",
|
|
907
|
+
step,
|
|
908
|
+
toolName: chunk.payload.toolName,
|
|
909
|
+
index: message.toolInvocations.length
|
|
910
|
+
};
|
|
911
|
+
const invocation = {
|
|
912
|
+
state: "partial-call",
|
|
913
|
+
step,
|
|
914
|
+
toolCallId: chunk.payload.toolCallId,
|
|
915
|
+
toolName: chunk.payload.toolName,
|
|
916
|
+
args: chunk.payload.args
|
|
917
|
+
};
|
|
918
|
+
message.toolInvocations.push(invocation);
|
|
919
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
920
|
+
execUpdate();
|
|
921
|
+
break;
|
|
922
|
+
}
|
|
923
|
+
case "tool-call-delta": {
|
|
924
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
925
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
926
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
927
|
+
const invocation = {
|
|
928
|
+
state: "partial-call",
|
|
929
|
+
step: partialToolCall.step,
|
|
930
|
+
toolCallId: chunk.payload.toolCallId,
|
|
931
|
+
toolName: partialToolCall.toolName,
|
|
932
|
+
args: partialArgs
|
|
933
|
+
};
|
|
934
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
935
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
936
|
+
execUpdate();
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
case "tool-result": {
|
|
940
|
+
const toolInvocations = message.toolInvocations;
|
|
941
|
+
if (toolInvocations == null) {
|
|
942
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
943
|
+
}
|
|
944
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
945
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
946
|
+
);
|
|
947
|
+
if (toolInvocationIndex === -1) {
|
|
948
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
949
|
+
}
|
|
950
|
+
const invocation = {
|
|
951
|
+
...toolInvocations[toolInvocationIndex],
|
|
952
|
+
state: "result",
|
|
953
|
+
...chunk.payload
|
|
954
|
+
};
|
|
955
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
956
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
957
|
+
execUpdate();
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
960
|
+
case "error": {
|
|
961
|
+
throw new Error(chunk.payload.error);
|
|
962
|
+
}
|
|
963
|
+
case "data": {
|
|
964
|
+
data.push(...chunk.payload.data);
|
|
965
|
+
execUpdate();
|
|
966
|
+
break;
|
|
967
|
+
}
|
|
968
|
+
case "step-finish": {
|
|
969
|
+
step += 1;
|
|
970
|
+
currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
|
|
971
|
+
currentReasoningPart = void 0;
|
|
972
|
+
currentReasoningTextDetail = void 0;
|
|
973
|
+
execUpdate();
|
|
974
|
+
break;
|
|
975
|
+
}
|
|
976
|
+
case "finish": {
|
|
977
|
+
finishReason = chunk.payload.stepResult.reason;
|
|
978
|
+
if (chunk.payload.usage != null) {
|
|
979
|
+
usage = chunk.payload.usage;
|
|
980
|
+
}
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
onFinish?.({ message, finishReason, usage });
|
|
987
|
+
}
|
|
988
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
989
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
990
|
+
method: "POST",
|
|
991
|
+
body: processedParams,
|
|
992
|
+
stream: true
|
|
993
|
+
});
|
|
994
|
+
if (!response.body) {
|
|
995
|
+
throw new Error("No response body");
|
|
996
|
+
}
|
|
997
|
+
try {
|
|
998
|
+
let toolCalls = [];
|
|
999
|
+
let messages = [];
|
|
1000
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1001
|
+
streamForWritable.pipeTo(
|
|
1002
|
+
new WritableStream({
|
|
1003
|
+
async write(chunk) {
|
|
1004
|
+
try {
|
|
1005
|
+
const text = new TextDecoder().decode(chunk);
|
|
1006
|
+
if (text.includes("[DONE]")) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
const writer = writable.getWriter();
|
|
1012
|
+
try {
|
|
1013
|
+
await writer.write(chunk);
|
|
1014
|
+
} finally {
|
|
1015
|
+
writer.releaseLock();
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
}),
|
|
1019
|
+
{
|
|
1020
|
+
preventClose: true
|
|
1021
|
+
}
|
|
1022
|
+
).catch((error) => {
|
|
1023
|
+
console.error("Error piping to writable stream:", error);
|
|
1024
|
+
});
|
|
1025
|
+
this.processChatResponse_vNext({
|
|
1026
|
+
stream: streamForProcessing,
|
|
1027
|
+
update: ({ message }) => {
|
|
1028
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1029
|
+
if (existingIndex !== -1) {
|
|
1030
|
+
messages[existingIndex] = message;
|
|
1031
|
+
} else {
|
|
1032
|
+
messages.push(message);
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1036
|
+
if (finishReason === "tool-calls") {
|
|
1037
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1038
|
+
if (toolCall) {
|
|
1039
|
+
toolCalls.push(toolCall);
|
|
1040
|
+
}
|
|
1041
|
+
for (const toolCall2 of toolCalls) {
|
|
1042
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1043
|
+
if (clientTool && clientTool.execute) {
|
|
1044
|
+
const result = await clientTool.execute(
|
|
1045
|
+
{
|
|
1046
|
+
context: toolCall2?.args,
|
|
1047
|
+
runId: processedParams.runId,
|
|
1048
|
+
resourceId: processedParams.resourceId,
|
|
1049
|
+
threadId: processedParams.threadId,
|
|
1050
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1051
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1052
|
+
tracingContext: { currentSpan: void 0 }
|
|
1053
|
+
},
|
|
1054
|
+
{
|
|
1055
|
+
messages: response.messages,
|
|
1056
|
+
toolCallId: toolCall2?.toolCallId
|
|
1057
|
+
}
|
|
1058
|
+
);
|
|
1059
|
+
const lastMessageRaw = messages[messages.length - 1];
|
|
1060
|
+
const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
|
|
1061
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1062
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1063
|
+
);
|
|
1064
|
+
if (toolInvocationPart) {
|
|
1065
|
+
toolInvocationPart.toolInvocation = {
|
|
1066
|
+
...toolInvocationPart.toolInvocation,
|
|
1067
|
+
state: "result",
|
|
1068
|
+
result
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1072
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1073
|
+
);
|
|
1074
|
+
if (toolInvocation) {
|
|
1075
|
+
toolInvocation.state = "result";
|
|
1076
|
+
toolInvocation.result = result;
|
|
1077
|
+
}
|
|
1078
|
+
const originalMessages = processedParams.messages;
|
|
1079
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1080
|
+
const updatedMessages = lastMessage != null ? [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messageArray, ...messages];
|
|
1081
|
+
this.processStreamResponse_vNext(
|
|
1082
|
+
{
|
|
1083
|
+
...processedParams,
|
|
1084
|
+
messages: updatedMessages
|
|
1085
|
+
},
|
|
1086
|
+
writable
|
|
1087
|
+
).catch((error) => {
|
|
1088
|
+
console.error("Error processing stream response:", error);
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
} else {
|
|
1093
|
+
setTimeout(() => {
|
|
1094
|
+
writable.close();
|
|
1095
|
+
}, 0);
|
|
1096
|
+
}
|
|
1097
|
+
},
|
|
1098
|
+
lastMessage: void 0
|
|
1099
|
+
}).catch((error) => {
|
|
1100
|
+
console.error("Error processing stream response:", error);
|
|
1101
|
+
});
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
console.error("Error processing stream response:", error);
|
|
1104
|
+
}
|
|
1105
|
+
return response;
|
|
1106
|
+
}
|
|
1107
|
+
async network(params) {
|
|
1108
|
+
const response = await this.request(`/api/agents/${this.agentId}/network`, {
|
|
1109
|
+
method: "POST",
|
|
1110
|
+
body: params,
|
|
1111
|
+
stream: true
|
|
1112
|
+
});
|
|
1113
|
+
if (!response.body) {
|
|
1114
|
+
throw new Error("No response body");
|
|
1115
|
+
}
|
|
1116
|
+
const streamResponse = new Response(response.body, {
|
|
1117
|
+
status: response.status,
|
|
1118
|
+
statusText: response.statusText,
|
|
1119
|
+
headers: response.headers
|
|
1120
|
+
});
|
|
1121
|
+
streamResponse.processDataStream = async ({
|
|
1122
|
+
onChunk
|
|
1123
|
+
}) => {
|
|
1124
|
+
await processMastraNetworkStream({
|
|
1125
|
+
stream: streamResponse.body,
|
|
1126
|
+
onChunk
|
|
1127
|
+
});
|
|
1128
|
+
};
|
|
1129
|
+
return streamResponse;
|
|
1130
|
+
}
|
|
1131
|
+
async streamVNext(messagesOrParams, options) {
|
|
1132
|
+
let params;
|
|
1133
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
1134
|
+
params = messagesOrParams;
|
|
1135
|
+
} else {
|
|
1136
|
+
params = {
|
|
1137
|
+
messages: messagesOrParams,
|
|
1138
|
+
...options
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
const processedParams = {
|
|
1142
|
+
...params,
|
|
1143
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
1144
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
1145
|
+
clientTools: processClientTools(params.clientTools),
|
|
1146
|
+
structuredOutput: params.structuredOutput ? {
|
|
1147
|
+
...params.structuredOutput,
|
|
1148
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
1149
|
+
} : void 0
|
|
1150
|
+
};
|
|
1151
|
+
const { readable, writable } = new TransformStream();
|
|
1152
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
1153
|
+
const streamResponse = new Response(readable, {
|
|
1154
|
+
status: response.status,
|
|
1155
|
+
statusText: response.statusText,
|
|
1156
|
+
headers: response.headers
|
|
1157
|
+
});
|
|
1158
|
+
streamResponse.processDataStream = async ({
|
|
1159
|
+
onChunk
|
|
1160
|
+
}) => {
|
|
1161
|
+
await processMastraStream({
|
|
1162
|
+
stream: streamResponse.body,
|
|
1163
|
+
onChunk
|
|
1164
|
+
});
|
|
1165
|
+
};
|
|
1166
|
+
return streamResponse;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Processes the stream response and handles tool calls
|
|
1170
|
+
*/
|
|
1171
|
+
async processStreamResponse(processedParams, writable) {
|
|
1172
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
1173
|
+
method: "POST",
|
|
1174
|
+
body: processedParams,
|
|
1175
|
+
stream: true
|
|
1176
|
+
});
|
|
1177
|
+
if (!response.body) {
|
|
1178
|
+
throw new Error("No response body");
|
|
1179
|
+
}
|
|
1180
|
+
try {
|
|
1181
|
+
let toolCalls = [];
|
|
1182
|
+
let messages = [];
|
|
1183
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1184
|
+
streamForWritable.pipeTo(writable, {
|
|
1185
|
+
preventClose: true
|
|
1186
|
+
}).catch((error) => {
|
|
1187
|
+
console.error("Error piping to writable stream:", error);
|
|
1188
|
+
});
|
|
1189
|
+
this.processChatResponse({
|
|
1190
|
+
stream: streamForProcessing,
|
|
1191
|
+
update: ({ message }) => {
|
|
1192
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1193
|
+
if (existingIndex !== -1) {
|
|
1194
|
+
messages[existingIndex] = message;
|
|
1195
|
+
} else {
|
|
1196
|
+
messages.push(message);
|
|
1197
|
+
}
|
|
1198
|
+
},
|
|
1199
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1200
|
+
if (finishReason === "tool-calls") {
|
|
1201
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1202
|
+
if (toolCall) {
|
|
1203
|
+
toolCalls.push(toolCall);
|
|
1204
|
+
}
|
|
1205
|
+
for (const toolCall2 of toolCalls) {
|
|
1206
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1207
|
+
if (clientTool && clientTool.execute) {
|
|
1208
|
+
const result = await clientTool.execute(
|
|
1209
|
+
{
|
|
1210
|
+
context: toolCall2?.args,
|
|
1211
|
+
runId: processedParams.runId,
|
|
1212
|
+
resourceId: processedParams.resourceId,
|
|
1213
|
+
threadId: processedParams.threadId,
|
|
1214
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1215
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1216
|
+
tracingContext: { currentSpan: void 0 }
|
|
1217
|
+
},
|
|
1218
|
+
{
|
|
1219
|
+
messages: response.messages,
|
|
1220
|
+
toolCallId: toolCall2?.toolCallId
|
|
1221
|
+
}
|
|
1222
|
+
);
|
|
1223
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1224
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1225
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1226
|
+
);
|
|
1227
|
+
if (toolInvocationPart) {
|
|
1228
|
+
toolInvocationPart.toolInvocation = {
|
|
1229
|
+
...toolInvocationPart.toolInvocation,
|
|
1230
|
+
state: "result",
|
|
1231
|
+
result
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1235
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1236
|
+
);
|
|
1237
|
+
if (toolInvocation) {
|
|
1238
|
+
toolInvocation.state = "result";
|
|
1239
|
+
toolInvocation.result = result;
|
|
1240
|
+
}
|
|
1241
|
+
const writer = writable.getWriter();
|
|
1242
|
+
try {
|
|
1243
|
+
await writer.write(
|
|
1244
|
+
new TextEncoder().encode(
|
|
1245
|
+
"a:" + JSON.stringify({
|
|
1246
|
+
toolCallId: toolCall2.toolCallId,
|
|
1247
|
+
result
|
|
1248
|
+
}) + "\n"
|
|
1249
|
+
)
|
|
1250
|
+
);
|
|
1251
|
+
} finally {
|
|
1252
|
+
writer.releaseLock();
|
|
1253
|
+
}
|
|
1254
|
+
const originalMessages = processedParams.messages;
|
|
1255
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1256
|
+
this.processStreamResponse(
|
|
1257
|
+
{
|
|
1258
|
+
...processedParams,
|
|
1259
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1260
|
+
},
|
|
1261
|
+
writable
|
|
1262
|
+
).catch((error) => {
|
|
1263
|
+
console.error("Error processing stream response:", error);
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
} else {
|
|
1268
|
+
setTimeout(() => {
|
|
1269
|
+
writable.close();
|
|
1270
|
+
}, 0);
|
|
1271
|
+
}
|
|
1272
|
+
},
|
|
1273
|
+
lastMessage: void 0
|
|
1274
|
+
}).catch((error) => {
|
|
1275
|
+
console.error("Error processing stream response:", error);
|
|
1276
|
+
});
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
console.error("Error processing stream response:", error);
|
|
1279
|
+
}
|
|
1280
|
+
return response;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Gets details about a specific tool available to the agent
|
|
1284
|
+
* @param toolId - ID of the tool to retrieve
|
|
1285
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1286
|
+
* @returns Promise containing tool details
|
|
1287
|
+
*/
|
|
1288
|
+
getTool(toolId, runtimeContext) {
|
|
1289
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Executes a tool for the agent
|
|
1293
|
+
* @param toolId - ID of the tool to execute
|
|
1294
|
+
* @param params - Parameters required for tool execution
|
|
1295
|
+
* @returns Promise containing the tool execution results
|
|
1296
|
+
*/
|
|
1297
|
+
executeTool(toolId, params) {
|
|
1298
|
+
const body = {
|
|
1299
|
+
data: params.data,
|
|
1300
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1301
|
+
};
|
|
1302
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
1303
|
+
method: "POST",
|
|
1304
|
+
body
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Retrieves evaluation results for the agent
|
|
1309
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1310
|
+
* @returns Promise containing agent evaluations
|
|
1311
|
+
*/
|
|
1312
|
+
evals(runtimeContext) {
|
|
1313
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci${runtimeContextQueryString(runtimeContext)}`);
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* Retrieves live evaluation results for the agent
|
|
1317
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1318
|
+
* @returns Promise containing live agent evaluations
|
|
1319
|
+
*/
|
|
1320
|
+
liveEvals(runtimeContext) {
|
|
1321
|
+
return this.request(`/api/agents/${this.agentId}/evals/live${runtimeContextQueryString(runtimeContext)}`);
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Updates the model for the agent
|
|
1325
|
+
* @param params - Parameters for updating the model
|
|
1326
|
+
* @returns Promise containing the updated model
|
|
1327
|
+
*/
|
|
1328
|
+
updateModel(params) {
|
|
1329
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1330
|
+
method: "POST",
|
|
1331
|
+
body: params
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
|
|
1336
|
+
// src/resources/memory-thread.ts
|
|
1337
|
+
var MemoryThread = class extends BaseResource {
|
|
1338
|
+
constructor(options, threadId, agentId) {
|
|
1339
|
+
super(options);
|
|
1340
|
+
this.threadId = threadId;
|
|
1341
|
+
this.agentId = agentId;
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Retrieves the memory thread details
|
|
1345
|
+
* @returns Promise containing thread details including title and metadata
|
|
1346
|
+
*/
|
|
1347
|
+
get() {
|
|
1348
|
+
return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`);
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Updates the memory thread properties
|
|
1352
|
+
* @param params - Update parameters including title and metadata
|
|
1353
|
+
* @returns Promise containing updated thread details
|
|
1354
|
+
*/
|
|
1355
|
+
update(params) {
|
|
1356
|
+
return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
|
|
1357
|
+
method: "PATCH",
|
|
1358
|
+
body: params
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
487
1362
|
* Deletes the memory thread
|
|
488
1363
|
* @returns Promise containing deletion result
|
|
489
1364
|
*/
|
|
@@ -504,6 +1379,36 @@ var MemoryThread = class extends BaseResource {
|
|
|
504
1379
|
});
|
|
505
1380
|
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
506
1381
|
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
1384
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
1385
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
1386
|
+
*/
|
|
1387
|
+
getMessagesPaginated({
|
|
1388
|
+
selectBy,
|
|
1389
|
+
...rest
|
|
1390
|
+
}) {
|
|
1391
|
+
const query = new URLSearchParams({
|
|
1392
|
+
...rest,
|
|
1393
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
1394
|
+
});
|
|
1395
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Deletes one or more messages from the thread
|
|
1399
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1400
|
+
* message object with id property, or array of message objects
|
|
1401
|
+
* @returns Promise containing deletion result
|
|
1402
|
+
*/
|
|
1403
|
+
deleteMessages(messageIds) {
|
|
1404
|
+
const query = new URLSearchParams({
|
|
1405
|
+
agentId: this.agentId
|
|
1406
|
+
});
|
|
1407
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1408
|
+
method: "POST",
|
|
1409
|
+
body: { messageIds }
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
507
1412
|
};
|
|
508
1413
|
|
|
509
1414
|
// src/resources/vector.ts
|
|
@@ -515,10 +1420,13 @@ var Vector = class extends BaseResource {
|
|
|
515
1420
|
/**
|
|
516
1421
|
* Retrieves details about a specific vector index
|
|
517
1422
|
* @param indexName - Name of the index to get details for
|
|
1423
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
518
1424
|
* @returns Promise containing vector index details
|
|
519
1425
|
*/
|
|
520
|
-
details(indexName) {
|
|
521
|
-
return this.request(
|
|
1426
|
+
details(indexName, runtimeContext) {
|
|
1427
|
+
return this.request(
|
|
1428
|
+
`/api/vector/${this.vectorName}/indexes/${indexName}${runtimeContextQueryString(runtimeContext)}`
|
|
1429
|
+
);
|
|
522
1430
|
}
|
|
523
1431
|
/**
|
|
524
1432
|
* Deletes a vector index
|
|
@@ -532,10 +1440,11 @@ var Vector = class extends BaseResource {
|
|
|
532
1440
|
}
|
|
533
1441
|
/**
|
|
534
1442
|
* Retrieves a list of all available indexes
|
|
1443
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
535
1444
|
* @returns Promise containing array of index names
|
|
536
1445
|
*/
|
|
537
|
-
getIndexes() {
|
|
538
|
-
return this.request(`/api/vector/${this.vectorName}/indexes`);
|
|
1446
|
+
getIndexes(runtimeContext) {
|
|
1447
|
+
return this.request(`/api/vector/${this.vectorName}/indexes${runtimeContextQueryString(runtimeContext)}`);
|
|
539
1448
|
}
|
|
540
1449
|
/**
|
|
541
1450
|
* Creates a new vector index
|
|
@@ -572,26 +1481,29 @@ var Vector = class extends BaseResource {
|
|
|
572
1481
|
}
|
|
573
1482
|
};
|
|
574
1483
|
|
|
575
|
-
// src/resources/workflow.ts
|
|
1484
|
+
// src/resources/legacy-workflow.ts
|
|
576
1485
|
var RECORD_SEPARATOR = "";
|
|
577
|
-
var
|
|
1486
|
+
var LegacyWorkflow = class extends BaseResource {
|
|
578
1487
|
constructor(options, workflowId) {
|
|
579
1488
|
super(options);
|
|
580
1489
|
this.workflowId = workflowId;
|
|
581
1490
|
}
|
|
582
1491
|
/**
|
|
583
|
-
* Retrieves details about the workflow
|
|
584
|
-
* @
|
|
1492
|
+
* Retrieves details about the legacy workflow
|
|
1493
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1494
|
+
* @returns Promise containing legacy workflow details including steps and graphs
|
|
585
1495
|
*/
|
|
586
|
-
details() {
|
|
587
|
-
return this.request(`/api/workflows/${this.workflowId}`);
|
|
1496
|
+
details(runtimeContext) {
|
|
1497
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}${runtimeContextQueryString(runtimeContext)}`);
|
|
588
1498
|
}
|
|
589
1499
|
/**
|
|
590
|
-
* Retrieves all runs for a workflow
|
|
1500
|
+
* Retrieves all runs for a legacy workflow
|
|
591
1501
|
* @param params - Parameters for filtering runs
|
|
592
|
-
* @
|
|
1502
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1503
|
+
* @returns Promise containing legacy workflow runs array
|
|
593
1504
|
*/
|
|
594
|
-
runs(params) {
|
|
1505
|
+
runs(params, runtimeContext) {
|
|
1506
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
595
1507
|
const searchParams = new URLSearchParams();
|
|
596
1508
|
if (params?.fromDate) {
|
|
597
1509
|
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
@@ -608,26 +1520,17 @@ var Workflow = class extends BaseResource {
|
|
|
608
1520
|
if (params?.resourceId) {
|
|
609
1521
|
searchParams.set("resourceId", params.resourceId);
|
|
610
1522
|
}
|
|
1523
|
+
if (runtimeContextParam) {
|
|
1524
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
1525
|
+
}
|
|
611
1526
|
if (searchParams.size) {
|
|
612
|
-
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1527
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs?${searchParams}`);
|
|
613
1528
|
} else {
|
|
614
|
-
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1529
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs`);
|
|
615
1530
|
}
|
|
616
1531
|
}
|
|
617
1532
|
/**
|
|
618
|
-
*
|
|
619
|
-
* Executes the workflow with the provided parameters
|
|
620
|
-
* @param params - Parameters required for workflow execution
|
|
621
|
-
* @returns Promise containing the workflow execution results
|
|
622
|
-
*/
|
|
623
|
-
execute(params) {
|
|
624
|
-
return this.request(`/api/workflows/${this.workflowId}/execute`, {
|
|
625
|
-
method: "POST",
|
|
626
|
-
body: params
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Creates a new workflow run
|
|
1533
|
+
* Creates a new legacy workflow run
|
|
631
1534
|
* @returns Promise containing the generated run ID
|
|
632
1535
|
*/
|
|
633
1536
|
createRun(params) {
|
|
@@ -635,34 +1538,34 @@ var Workflow = class extends BaseResource {
|
|
|
635
1538
|
if (!!params?.runId) {
|
|
636
1539
|
searchParams.set("runId", params.runId);
|
|
637
1540
|
}
|
|
638
|
-
return this.request(`/api/workflows/${this.workflowId}/
|
|
1541
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
639
1542
|
method: "POST"
|
|
640
1543
|
});
|
|
641
1544
|
}
|
|
642
1545
|
/**
|
|
643
|
-
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1546
|
+
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
644
1547
|
* @param params - Object containing the runId and triggerData
|
|
645
1548
|
* @returns Promise containing success message
|
|
646
1549
|
*/
|
|
647
1550
|
start(params) {
|
|
648
|
-
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1551
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
649
1552
|
method: "POST",
|
|
650
1553
|
body: params?.triggerData
|
|
651
1554
|
});
|
|
652
1555
|
}
|
|
653
1556
|
/**
|
|
654
|
-
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1557
|
+
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
655
1558
|
* @param stepId - ID of the step to resume
|
|
656
|
-
* @param runId - ID of the workflow run
|
|
657
|
-
* @param context - Context to resume the workflow with
|
|
658
|
-
* @returns Promise containing the workflow resume results
|
|
1559
|
+
* @param runId - ID of the legacy workflow run
|
|
1560
|
+
* @param context - Context to resume the legacy workflow with
|
|
1561
|
+
* @returns Promise containing the legacy workflow resume results
|
|
659
1562
|
*/
|
|
660
1563
|
resume({
|
|
661
1564
|
stepId,
|
|
662
1565
|
runId,
|
|
663
1566
|
context
|
|
664
1567
|
}) {
|
|
665
|
-
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1568
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
666
1569
|
method: "POST",
|
|
667
1570
|
body: {
|
|
668
1571
|
stepId,
|
|
@@ -680,18 +1583,18 @@ var Workflow = class extends BaseResource {
|
|
|
680
1583
|
if (!!params?.runId) {
|
|
681
1584
|
searchParams.set("runId", params.runId);
|
|
682
1585
|
}
|
|
683
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1586
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
684
1587
|
method: "POST",
|
|
685
1588
|
body: params?.triggerData
|
|
686
1589
|
});
|
|
687
1590
|
}
|
|
688
1591
|
/**
|
|
689
|
-
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1592
|
+
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
690
1593
|
* @param params - Object containing the runId, stepId, and context
|
|
691
1594
|
* @returns Promise containing the workflow resume results
|
|
692
1595
|
*/
|
|
693
1596
|
resumeAsync(params) {
|
|
694
|
-
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1597
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
695
1598
|
method: "POST",
|
|
696
1599
|
body: {
|
|
697
1600
|
stepId: params.stepId,
|
|
@@ -745,16 +1648,16 @@ var Workflow = class extends BaseResource {
|
|
|
745
1648
|
}
|
|
746
1649
|
}
|
|
747
1650
|
/**
|
|
748
|
-
* Watches workflow transitions in real-time
|
|
1651
|
+
* Watches legacy workflow transitions in real-time
|
|
749
1652
|
* @param runId - Optional run ID to filter the watch stream
|
|
750
|
-
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1653
|
+
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
751
1654
|
*/
|
|
752
1655
|
async watch({ runId }, onRecord) {
|
|
753
|
-
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1656
|
+
const response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
754
1657
|
stream: true
|
|
755
1658
|
});
|
|
756
1659
|
if (!response.ok) {
|
|
757
|
-
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1660
|
+
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
758
1661
|
}
|
|
759
1662
|
if (!response.body) {
|
|
760
1663
|
throw new Error("Response body is null");
|
|
@@ -773,10 +1676,11 @@ var Tool = class extends BaseResource {
|
|
|
773
1676
|
}
|
|
774
1677
|
/**
|
|
775
1678
|
* Retrieves details about the tool
|
|
1679
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
776
1680
|
* @returns Promise containing tool details including description and schemas
|
|
777
1681
|
*/
|
|
778
|
-
details() {
|
|
779
|
-
return this.request(`/api/tools/${this.toolId}`);
|
|
1682
|
+
details(runtimeContext) {
|
|
1683
|
+
return this.request(`/api/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
780
1684
|
}
|
|
781
1685
|
/**
|
|
782
1686
|
* Executes the tool with the provided parameters
|
|
@@ -790,7 +1694,7 @@ var Tool = class extends BaseResource {
|
|
|
790
1694
|
}
|
|
791
1695
|
const body = {
|
|
792
1696
|
data: params.data,
|
|
793
|
-
runtimeContext:
|
|
1697
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
794
1698
|
};
|
|
795
1699
|
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
796
1700
|
method: "POST",
|
|
@@ -799,15 +1703,15 @@ var Tool = class extends BaseResource {
|
|
|
799
1703
|
}
|
|
800
1704
|
};
|
|
801
1705
|
|
|
802
|
-
// src/resources/
|
|
1706
|
+
// src/resources/workflow.ts
|
|
803
1707
|
var RECORD_SEPARATOR2 = "";
|
|
804
|
-
var
|
|
1708
|
+
var Workflow = class extends BaseResource {
|
|
805
1709
|
constructor(options, workflowId) {
|
|
806
1710
|
super(options);
|
|
807
1711
|
this.workflowId = workflowId;
|
|
808
1712
|
}
|
|
809
1713
|
/**
|
|
810
|
-
* Creates an async generator that processes a readable stream and yields
|
|
1714
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
811
1715
|
* separated by the Record Separator character (\x1E)
|
|
812
1716
|
*
|
|
813
1717
|
* @param stream - The readable stream to process
|
|
@@ -852,18 +1756,21 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
852
1756
|
}
|
|
853
1757
|
}
|
|
854
1758
|
/**
|
|
855
|
-
* Retrieves details about the
|
|
856
|
-
* @
|
|
1759
|
+
* Retrieves details about the workflow
|
|
1760
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1761
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
857
1762
|
*/
|
|
858
|
-
details() {
|
|
859
|
-
return this.request(`/api/workflows
|
|
1763
|
+
details(runtimeContext) {
|
|
1764
|
+
return this.request(`/api/workflows/${this.workflowId}${runtimeContextQueryString(runtimeContext)}`);
|
|
860
1765
|
}
|
|
861
1766
|
/**
|
|
862
|
-
* Retrieves all runs for a
|
|
1767
|
+
* Retrieves all runs for a workflow
|
|
863
1768
|
* @param params - Parameters for filtering runs
|
|
864
|
-
* @
|
|
1769
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1770
|
+
* @returns Promise containing workflow runs array
|
|
865
1771
|
*/
|
|
866
|
-
runs(params) {
|
|
1772
|
+
runs(params, runtimeContext) {
|
|
1773
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
867
1774
|
const searchParams = new URLSearchParams();
|
|
868
1775
|
if (params?.fromDate) {
|
|
869
1776
|
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
@@ -871,49 +1778,135 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
871
1778
|
if (params?.toDate) {
|
|
872
1779
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
873
1780
|
}
|
|
874
|
-
if (params?.limit) {
|
|
1781
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
875
1782
|
searchParams.set("limit", String(params.limit));
|
|
876
1783
|
}
|
|
877
|
-
if (params?.offset) {
|
|
1784
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
878
1785
|
searchParams.set("offset", String(params.offset));
|
|
879
1786
|
}
|
|
880
1787
|
if (params?.resourceId) {
|
|
881
1788
|
searchParams.set("resourceId", params.resourceId);
|
|
882
1789
|
}
|
|
1790
|
+
if (runtimeContextParam) {
|
|
1791
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
1792
|
+
}
|
|
883
1793
|
if (searchParams.size) {
|
|
884
|
-
return this.request(`/api/workflows
|
|
1794
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
885
1795
|
} else {
|
|
886
|
-
return this.request(`/api/workflows
|
|
1796
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
887
1797
|
}
|
|
888
1798
|
}
|
|
889
1799
|
/**
|
|
890
|
-
*
|
|
891
|
-
* @param
|
|
892
|
-
* @
|
|
1800
|
+
* Retrieves a specific workflow run by its ID
|
|
1801
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1802
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1803
|
+
* @returns Promise containing the workflow run details
|
|
893
1804
|
*/
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
if (!!params?.runId) {
|
|
897
|
-
searchParams.set("runId", params.runId);
|
|
898
|
-
}
|
|
899
|
-
return this.request(`/api/workflows/v-next/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
900
|
-
method: "POST"
|
|
901
|
-
});
|
|
1805
|
+
runById(runId, runtimeContext) {
|
|
1806
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}${runtimeContextQueryString(runtimeContext)}`);
|
|
902
1807
|
}
|
|
903
1808
|
/**
|
|
904
|
-
*
|
|
905
|
-
* @param
|
|
906
|
-
* @
|
|
1809
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1810
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1811
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1812
|
+
* @returns Promise containing the workflow run execution result
|
|
1813
|
+
*/
|
|
1814
|
+
runExecutionResult(runId, runtimeContext) {
|
|
1815
|
+
return this.request(
|
|
1816
|
+
`/api/workflows/${this.workflowId}/runs/${runId}/execution-result${runtimeContextQueryString(runtimeContext)}`
|
|
1817
|
+
);
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Cancels a specific workflow run by its ID
|
|
1821
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1822
|
+
* @returns Promise containing a success message
|
|
1823
|
+
*/
|
|
1824
|
+
cancelRun(runId) {
|
|
1825
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1826
|
+
method: "POST"
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1829
|
+
/**
|
|
1830
|
+
* Sends an event to a specific workflow run by its ID
|
|
1831
|
+
* @param params - Object containing the runId, event and data
|
|
1832
|
+
* @returns Promise containing a success message
|
|
1833
|
+
*/
|
|
1834
|
+
sendRunEvent(params) {
|
|
1835
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1836
|
+
method: "POST",
|
|
1837
|
+
body: { event: params.event, data: params.data }
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* @deprecated Use createRunAsync() instead.
|
|
1842
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
1843
|
+
*/
|
|
1844
|
+
async createRun(_params) {
|
|
1845
|
+
throw new Error(
|
|
1846
|
+
"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."
|
|
1847
|
+
);
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Creates a new workflow run
|
|
1851
|
+
* @param params - Optional object containing the optional runId
|
|
1852
|
+
* @returns Promise containing the runId of the created run with methods to control execution
|
|
1853
|
+
*/
|
|
1854
|
+
async createRunAsync(params) {
|
|
1855
|
+
const searchParams = new URLSearchParams();
|
|
1856
|
+
if (!!params?.runId) {
|
|
1857
|
+
searchParams.set("runId", params.runId);
|
|
1858
|
+
}
|
|
1859
|
+
const res = await this.request(
|
|
1860
|
+
`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`,
|
|
1861
|
+
{
|
|
1862
|
+
method: "POST"
|
|
1863
|
+
}
|
|
1864
|
+
);
|
|
1865
|
+
const runId = res.runId;
|
|
1866
|
+
return {
|
|
1867
|
+
runId,
|
|
1868
|
+
start: async (p) => {
|
|
1869
|
+
return this.start({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1870
|
+
},
|
|
1871
|
+
startAsync: async (p) => {
|
|
1872
|
+
return this.startAsync({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1873
|
+
},
|
|
1874
|
+
watch: async (onRecord) => {
|
|
1875
|
+
return this.watch({ runId }, onRecord);
|
|
1876
|
+
},
|
|
1877
|
+
stream: async (p) => {
|
|
1878
|
+
return this.stream({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1879
|
+
},
|
|
1880
|
+
resume: async (p) => {
|
|
1881
|
+
return this.resume({ runId, step: p.step, resumeData: p.resumeData, runtimeContext: p.runtimeContext });
|
|
1882
|
+
},
|
|
1883
|
+
resumeAsync: async (p) => {
|
|
1884
|
+
return this.resumeAsync({ runId, step: p.step, resumeData: p.resumeData, runtimeContext: p.runtimeContext });
|
|
1885
|
+
},
|
|
1886
|
+
resumeStreamVNext: async (p) => {
|
|
1887
|
+
return this.resumeStreamVNext({
|
|
1888
|
+
runId,
|
|
1889
|
+
step: p.step,
|
|
1890
|
+
resumeData: p.resumeData,
|
|
1891
|
+
runtimeContext: p.runtimeContext
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
};
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1898
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
1899
|
+
* @returns Promise containing success message
|
|
907
1900
|
*/
|
|
908
1901
|
start(params) {
|
|
909
|
-
const runtimeContext =
|
|
910
|
-
return this.request(`/api/workflows
|
|
1902
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1903
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
911
1904
|
method: "POST",
|
|
912
1905
|
body: { inputData: params?.inputData, runtimeContext }
|
|
913
1906
|
});
|
|
914
1907
|
}
|
|
915
1908
|
/**
|
|
916
|
-
* Resumes a suspended
|
|
1909
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
917
1910
|
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
918
1911
|
* @returns Promise containing success message
|
|
919
1912
|
*/
|
|
@@ -923,10 +1916,9 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
923
1916
|
resumeData,
|
|
924
1917
|
...rest
|
|
925
1918
|
}) {
|
|
926
|
-
const runtimeContext =
|
|
927
|
-
return this.request(`/api/workflows
|
|
1919
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
1920
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
928
1921
|
method: "POST",
|
|
929
|
-
stream: true,
|
|
930
1922
|
body: {
|
|
931
1923
|
step,
|
|
932
1924
|
resumeData,
|
|
@@ -935,29 +1927,178 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
935
1927
|
});
|
|
936
1928
|
}
|
|
937
1929
|
/**
|
|
938
|
-
* Starts a
|
|
1930
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
939
1931
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
940
|
-
* @returns Promise containing the
|
|
1932
|
+
* @returns Promise containing the workflow execution results
|
|
941
1933
|
*/
|
|
942
1934
|
startAsync(params) {
|
|
943
1935
|
const searchParams = new URLSearchParams();
|
|
944
1936
|
if (!!params?.runId) {
|
|
945
1937
|
searchParams.set("runId", params.runId);
|
|
946
1938
|
}
|
|
947
|
-
const runtimeContext =
|
|
948
|
-
return this.request(`/api/workflows
|
|
1939
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1940
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
949
1941
|
method: "POST",
|
|
950
1942
|
body: { inputData: params.inputData, runtimeContext }
|
|
951
1943
|
});
|
|
952
1944
|
}
|
|
953
1945
|
/**
|
|
954
|
-
*
|
|
1946
|
+
* Starts a workflow run and returns a stream
|
|
1947
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1948
|
+
* @returns Promise containing the workflow execution results
|
|
1949
|
+
*/
|
|
1950
|
+
async stream(params) {
|
|
1951
|
+
const searchParams = new URLSearchParams();
|
|
1952
|
+
if (!!params?.runId) {
|
|
1953
|
+
searchParams.set("runId", params.runId);
|
|
1954
|
+
}
|
|
1955
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1956
|
+
const response = await this.request(
|
|
1957
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1958
|
+
{
|
|
1959
|
+
method: "POST",
|
|
1960
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1961
|
+
stream: true
|
|
1962
|
+
}
|
|
1963
|
+
);
|
|
1964
|
+
if (!response.ok) {
|
|
1965
|
+
throw new Error(`Failed to stream workflow: ${response.statusText}`);
|
|
1966
|
+
}
|
|
1967
|
+
if (!response.body) {
|
|
1968
|
+
throw new Error("Response body is null");
|
|
1969
|
+
}
|
|
1970
|
+
let failedChunk = void 0;
|
|
1971
|
+
const transformStream = new TransformStream({
|
|
1972
|
+
start() {
|
|
1973
|
+
},
|
|
1974
|
+
async transform(chunk, controller) {
|
|
1975
|
+
try {
|
|
1976
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1977
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1978
|
+
for (const chunk2 of chunks) {
|
|
1979
|
+
if (chunk2) {
|
|
1980
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1981
|
+
try {
|
|
1982
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1983
|
+
controller.enqueue(parsedChunk);
|
|
1984
|
+
failedChunk = void 0;
|
|
1985
|
+
} catch {
|
|
1986
|
+
failedChunk = newChunk;
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
} catch {
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
});
|
|
1994
|
+
return response.body.pipeThrough(transformStream);
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Observes workflow stream for a workflow run
|
|
1998
|
+
* @param params - Object containing the runId
|
|
1999
|
+
* @returns Promise containing the workflow execution results
|
|
2000
|
+
*/
|
|
2001
|
+
async observeStream(params) {
|
|
2002
|
+
const searchParams = new URLSearchParams();
|
|
2003
|
+
searchParams.set("runId", params.runId);
|
|
2004
|
+
const response = await this.request(
|
|
2005
|
+
`/api/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
|
|
2006
|
+
{
|
|
2007
|
+
method: "POST",
|
|
2008
|
+
stream: true
|
|
2009
|
+
}
|
|
2010
|
+
);
|
|
2011
|
+
if (!response.ok) {
|
|
2012
|
+
throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
|
|
2013
|
+
}
|
|
2014
|
+
if (!response.body) {
|
|
2015
|
+
throw new Error("Response body is null");
|
|
2016
|
+
}
|
|
2017
|
+
let failedChunk = void 0;
|
|
2018
|
+
const transformStream = new TransformStream({
|
|
2019
|
+
start() {
|
|
2020
|
+
},
|
|
2021
|
+
async transform(chunk, controller) {
|
|
2022
|
+
try {
|
|
2023
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2024
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2025
|
+
for (const chunk2 of chunks) {
|
|
2026
|
+
if (chunk2) {
|
|
2027
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2028
|
+
try {
|
|
2029
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2030
|
+
controller.enqueue(parsedChunk);
|
|
2031
|
+
failedChunk = void 0;
|
|
2032
|
+
} catch {
|
|
2033
|
+
failedChunk = newChunk;
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
} catch {
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
});
|
|
2041
|
+
return response.body.pipeThrough(transformStream);
|
|
2042
|
+
}
|
|
2043
|
+
/**
|
|
2044
|
+
* Starts a workflow run and returns a stream
|
|
2045
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
2046
|
+
* @returns Promise containing the workflow execution results
|
|
2047
|
+
*/
|
|
2048
|
+
async streamVNext(params) {
|
|
2049
|
+
const searchParams = new URLSearchParams();
|
|
2050
|
+
if (!!params?.runId) {
|
|
2051
|
+
searchParams.set("runId", params.runId);
|
|
2052
|
+
}
|
|
2053
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2054
|
+
const response = await this.request(
|
|
2055
|
+
`/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
|
|
2056
|
+
{
|
|
2057
|
+
method: "POST",
|
|
2058
|
+
body: { inputData: params.inputData, runtimeContext, closeOnSuspend: params.closeOnSuspend },
|
|
2059
|
+
stream: true
|
|
2060
|
+
}
|
|
2061
|
+
);
|
|
2062
|
+
if (!response.ok) {
|
|
2063
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
2064
|
+
}
|
|
2065
|
+
if (!response.body) {
|
|
2066
|
+
throw new Error("Response body is null");
|
|
2067
|
+
}
|
|
2068
|
+
let failedChunk = void 0;
|
|
2069
|
+
const transformStream = new TransformStream({
|
|
2070
|
+
start() {
|
|
2071
|
+
},
|
|
2072
|
+
async transform(chunk, controller) {
|
|
2073
|
+
try {
|
|
2074
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2075
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2076
|
+
for (const chunk2 of chunks) {
|
|
2077
|
+
if (chunk2) {
|
|
2078
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2079
|
+
try {
|
|
2080
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2081
|
+
controller.enqueue(parsedChunk);
|
|
2082
|
+
failedChunk = void 0;
|
|
2083
|
+
} catch {
|
|
2084
|
+
failedChunk = newChunk;
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
} catch {
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
});
|
|
2092
|
+
return response.body.pipeThrough(transformStream);
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
955
2096
|
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
956
|
-
* @returns Promise containing the
|
|
2097
|
+
* @returns Promise containing the workflow resume results
|
|
957
2098
|
*/
|
|
958
2099
|
resumeAsync(params) {
|
|
959
|
-
const runtimeContext =
|
|
960
|
-
return this.request(`/api/workflows
|
|
2100
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2101
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
961
2102
|
method: "POST",
|
|
962
2103
|
body: {
|
|
963
2104
|
step: params.step,
|
|
@@ -967,24 +2108,67 @@ var VNextWorkflow = class extends BaseResource {
|
|
|
967
2108
|
});
|
|
968
2109
|
}
|
|
969
2110
|
/**
|
|
970
|
-
*
|
|
2111
|
+
* Resumes a suspended workflow step that uses streamVNext asynchronously and returns a promise that resolves when the workflow is complete
|
|
2112
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
2113
|
+
* @returns Promise containing the workflow resume results
|
|
2114
|
+
*/
|
|
2115
|
+
resumeStreamVNext(params) {
|
|
2116
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2117
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-stream?runId=${params.runId}`, {
|
|
2118
|
+
method: "POST",
|
|
2119
|
+
body: {
|
|
2120
|
+
step: params.step,
|
|
2121
|
+
resumeData: params.resumeData,
|
|
2122
|
+
runtimeContext
|
|
2123
|
+
}
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* Watches workflow transitions in real-time
|
|
971
2128
|
* @param runId - Optional run ID to filter the watch stream
|
|
972
|
-
* @returns AsyncGenerator that yields parsed records from the
|
|
2129
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
973
2130
|
*/
|
|
974
2131
|
async watch({ runId }, onRecord) {
|
|
975
|
-
const response = await this.request(`/api/workflows
|
|
2132
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
976
2133
|
stream: true
|
|
977
2134
|
});
|
|
978
2135
|
if (!response.ok) {
|
|
979
|
-
throw new Error(`Failed to watch
|
|
2136
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
980
2137
|
}
|
|
981
2138
|
if (!response.body) {
|
|
982
2139
|
throw new Error("Response body is null");
|
|
983
2140
|
}
|
|
984
2141
|
for await (const record of this.streamProcessor(response.body)) {
|
|
985
|
-
|
|
2142
|
+
if (typeof record === "string") {
|
|
2143
|
+
onRecord(JSON.parse(record));
|
|
2144
|
+
} else {
|
|
2145
|
+
onRecord(record);
|
|
2146
|
+
}
|
|
986
2147
|
}
|
|
987
2148
|
}
|
|
2149
|
+
/**
|
|
2150
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
2151
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
2152
|
+
*
|
|
2153
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
2154
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
2155
|
+
*/
|
|
2156
|
+
static createRecordStream(records) {
|
|
2157
|
+
const encoder = new TextEncoder();
|
|
2158
|
+
return new ReadableStream({
|
|
2159
|
+
async start(controller) {
|
|
2160
|
+
try {
|
|
2161
|
+
for await (const record of records) {
|
|
2162
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
2163
|
+
controller.enqueue(encoder.encode(json));
|
|
2164
|
+
}
|
|
2165
|
+
controller.close();
|
|
2166
|
+
} catch (err) {
|
|
2167
|
+
controller.error(err);
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
});
|
|
2171
|
+
}
|
|
988
2172
|
};
|
|
989
2173
|
|
|
990
2174
|
// src/resources/a2a.ts
|
|
@@ -998,22 +2182,38 @@ var A2A = class extends BaseResource {
|
|
|
998
2182
|
* @returns Promise containing the agent card information
|
|
999
2183
|
*/
|
|
1000
2184
|
async getCard() {
|
|
1001
|
-
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
2185
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
1002
2186
|
}
|
|
1003
2187
|
/**
|
|
1004
|
-
* Send a message to the agent and
|
|
2188
|
+
* Send a message to the agent and gets a message or task response
|
|
1005
2189
|
* @param params - Parameters for the task
|
|
1006
|
-
* @returns Promise containing the
|
|
2190
|
+
* @returns Promise containing the response
|
|
1007
2191
|
*/
|
|
1008
2192
|
async sendMessage(params) {
|
|
1009
2193
|
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1010
2194
|
method: "POST",
|
|
1011
2195
|
body: {
|
|
1012
|
-
method: "
|
|
2196
|
+
method: "message/send",
|
|
1013
2197
|
params
|
|
1014
2198
|
}
|
|
1015
2199
|
});
|
|
1016
|
-
return
|
|
2200
|
+
return response;
|
|
2201
|
+
}
|
|
2202
|
+
/**
|
|
2203
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2204
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2205
|
+
* @param params - Parameters for the task
|
|
2206
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2207
|
+
*/
|
|
2208
|
+
async sendStreamingMessage(params) {
|
|
2209
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2210
|
+
method: "POST",
|
|
2211
|
+
body: {
|
|
2212
|
+
method: "message/stream",
|
|
2213
|
+
params
|
|
2214
|
+
}
|
|
2215
|
+
});
|
|
2216
|
+
return response;
|
|
1017
2217
|
}
|
|
1018
2218
|
/**
|
|
1019
2219
|
* Get the status and result of a task
|
|
@@ -1028,99 +2228,701 @@ var A2A = class extends BaseResource {
|
|
|
1028
2228
|
params
|
|
1029
2229
|
}
|
|
1030
2230
|
});
|
|
1031
|
-
return response
|
|
2231
|
+
return response;
|
|
1032
2232
|
}
|
|
1033
2233
|
/**
|
|
1034
2234
|
* Cancel a running task
|
|
1035
2235
|
* @param params - Parameters identifying the task to cancel
|
|
1036
2236
|
* @returns Promise containing the task response
|
|
1037
2237
|
*/
|
|
1038
|
-
async cancelTask(params) {
|
|
1039
|
-
return this.request(`/a2a/${this.agentId}`, {
|
|
2238
|
+
async cancelTask(params) {
|
|
2239
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2240
|
+
method: "POST",
|
|
2241
|
+
body: {
|
|
2242
|
+
method: "tasks/cancel",
|
|
2243
|
+
params
|
|
2244
|
+
}
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
|
|
2249
|
+
// src/resources/mcp-tool.ts
|
|
2250
|
+
var MCPTool = class extends BaseResource {
|
|
2251
|
+
serverId;
|
|
2252
|
+
toolId;
|
|
2253
|
+
constructor(options, serverId, toolId) {
|
|
2254
|
+
super(options);
|
|
2255
|
+
this.serverId = serverId;
|
|
2256
|
+
this.toolId = toolId;
|
|
2257
|
+
}
|
|
2258
|
+
/**
|
|
2259
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2260
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2261
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2262
|
+
*/
|
|
2263
|
+
details(runtimeContext) {
|
|
2264
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
2265
|
+
}
|
|
2266
|
+
/**
|
|
2267
|
+
* Executes this specific tool on the MCP server.
|
|
2268
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2269
|
+
* @returns Promise containing the result of the tool execution.
|
|
2270
|
+
*/
|
|
2271
|
+
execute(params) {
|
|
2272
|
+
const body = {};
|
|
2273
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2274
|
+
if (params.runtimeContext !== void 0) {
|
|
2275
|
+
body.runtimeContext = params.runtimeContext;
|
|
2276
|
+
}
|
|
2277
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2278
|
+
method: "POST",
|
|
2279
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2280
|
+
});
|
|
2281
|
+
}
|
|
2282
|
+
};
|
|
2283
|
+
|
|
2284
|
+
// src/resources/agent-builder.ts
|
|
2285
|
+
var RECORD_SEPARATOR3 = "";
|
|
2286
|
+
var AgentBuilder = class extends BaseResource {
|
|
2287
|
+
constructor(options, actionId) {
|
|
2288
|
+
super(options);
|
|
2289
|
+
this.actionId = actionId;
|
|
2290
|
+
}
|
|
2291
|
+
// Helper function to transform workflow result to action result
|
|
2292
|
+
transformWorkflowResult(result) {
|
|
2293
|
+
if (result.status === "success") {
|
|
2294
|
+
return {
|
|
2295
|
+
success: result.result.success || false,
|
|
2296
|
+
applied: result.result.applied || false,
|
|
2297
|
+
branchName: result.result.branchName,
|
|
2298
|
+
message: result.result.message || "Agent builder action completed",
|
|
2299
|
+
validationResults: result.result.validationResults,
|
|
2300
|
+
error: result.result.error,
|
|
2301
|
+
errors: result.result.errors,
|
|
2302
|
+
stepResults: result.result.stepResults
|
|
2303
|
+
};
|
|
2304
|
+
} else if (result.status === "failed") {
|
|
2305
|
+
return {
|
|
2306
|
+
success: false,
|
|
2307
|
+
applied: false,
|
|
2308
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2309
|
+
error: result.error.message
|
|
2310
|
+
};
|
|
2311
|
+
} else {
|
|
2312
|
+
return {
|
|
2313
|
+
success: false,
|
|
2314
|
+
applied: false,
|
|
2315
|
+
message: "Agent builder action was suspended",
|
|
2316
|
+
error: "Workflow suspended - manual intervention required"
|
|
2317
|
+
};
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
/**
|
|
2321
|
+
* @deprecated Use createRunAsync() instead.
|
|
2322
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
2323
|
+
*/
|
|
2324
|
+
async createRun(_params) {
|
|
2325
|
+
throw new Error(
|
|
2326
|
+
"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."
|
|
2327
|
+
);
|
|
2328
|
+
}
|
|
2329
|
+
/**
|
|
2330
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2331
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2332
|
+
*/
|
|
2333
|
+
async createRunAsync(params) {
|
|
2334
|
+
const searchParams = new URLSearchParams();
|
|
2335
|
+
if (!!params?.runId) {
|
|
2336
|
+
searchParams.set("runId", params.runId);
|
|
2337
|
+
}
|
|
2338
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2339
|
+
return this.request(url, {
|
|
2340
|
+
method: "POST"
|
|
2341
|
+
});
|
|
2342
|
+
}
|
|
2343
|
+
/**
|
|
2344
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2345
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2346
|
+
*/
|
|
2347
|
+
async startAsync(params, runId) {
|
|
2348
|
+
const searchParams = new URLSearchParams();
|
|
2349
|
+
if (runId) {
|
|
2350
|
+
searchParams.set("runId", runId);
|
|
2351
|
+
}
|
|
2352
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2353
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2354
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2355
|
+
const result = await this.request(url, {
|
|
2356
|
+
method: "POST",
|
|
2357
|
+
body: { ...actionParams, runtimeContext }
|
|
2358
|
+
});
|
|
2359
|
+
return this.transformWorkflowResult(result);
|
|
2360
|
+
}
|
|
2361
|
+
/**
|
|
2362
|
+
* Starts an existing agent builder action run.
|
|
2363
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2364
|
+
*/
|
|
2365
|
+
async startActionRun(params, runId) {
|
|
2366
|
+
const searchParams = new URLSearchParams();
|
|
2367
|
+
searchParams.set("runId", runId);
|
|
2368
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2369
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2370
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2371
|
+
return this.request(url, {
|
|
2372
|
+
method: "POST",
|
|
2373
|
+
body: { ...actionParams, runtimeContext }
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2376
|
+
/**
|
|
2377
|
+
* Resumes a suspended agent builder action step.
|
|
2378
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2379
|
+
*/
|
|
2380
|
+
async resume(params, runId) {
|
|
2381
|
+
const searchParams = new URLSearchParams();
|
|
2382
|
+
searchParams.set("runId", runId);
|
|
2383
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2384
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2385
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2386
|
+
return this.request(url, {
|
|
2387
|
+
method: "POST",
|
|
2388
|
+
body: { ...resumeParams, runtimeContext }
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
/**
|
|
2392
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2393
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2394
|
+
*/
|
|
2395
|
+
async resumeAsync(params, runId) {
|
|
2396
|
+
const searchParams = new URLSearchParams();
|
|
2397
|
+
searchParams.set("runId", runId);
|
|
2398
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2399
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2400
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2401
|
+
const result = await this.request(url, {
|
|
2402
|
+
method: "POST",
|
|
2403
|
+
body: { ...resumeParams, runtimeContext }
|
|
2404
|
+
});
|
|
2405
|
+
return this.transformWorkflowResult(result);
|
|
2406
|
+
}
|
|
2407
|
+
/**
|
|
2408
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2409
|
+
* separated by the Record Separator character (\x1E)
|
|
2410
|
+
*
|
|
2411
|
+
* @param stream - The readable stream to process
|
|
2412
|
+
* @returns An async generator that yields parsed records
|
|
2413
|
+
*/
|
|
2414
|
+
async *streamProcessor(stream) {
|
|
2415
|
+
const reader = stream.getReader();
|
|
2416
|
+
let doneReading = false;
|
|
2417
|
+
let buffer = "";
|
|
2418
|
+
try {
|
|
2419
|
+
while (!doneReading) {
|
|
2420
|
+
const { done, value } = await reader.read();
|
|
2421
|
+
doneReading = done;
|
|
2422
|
+
if (done && !value) continue;
|
|
2423
|
+
try {
|
|
2424
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2425
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
2426
|
+
buffer = chunks.pop() || "";
|
|
2427
|
+
for (const chunk of chunks) {
|
|
2428
|
+
if (chunk) {
|
|
2429
|
+
if (typeof chunk === "string") {
|
|
2430
|
+
try {
|
|
2431
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2432
|
+
yield parsedChunk;
|
|
2433
|
+
} catch {
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
} catch {
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
if (buffer) {
|
|
2442
|
+
try {
|
|
2443
|
+
yield JSON.parse(buffer);
|
|
2444
|
+
} catch {
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
} finally {
|
|
2448
|
+
reader.cancel().catch(() => {
|
|
2449
|
+
});
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
/**
|
|
2453
|
+
* Streams agent builder action progress in real-time.
|
|
2454
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
2455
|
+
*/
|
|
2456
|
+
async stream(params, runId) {
|
|
2457
|
+
const searchParams = new URLSearchParams();
|
|
2458
|
+
if (runId) {
|
|
2459
|
+
searchParams.set("runId", runId);
|
|
2460
|
+
}
|
|
2461
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2462
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2463
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2464
|
+
const response = await this.request(url, {
|
|
2465
|
+
method: "POST",
|
|
2466
|
+
body: { ...actionParams, runtimeContext },
|
|
2467
|
+
stream: true
|
|
2468
|
+
});
|
|
2469
|
+
if (!response.ok) {
|
|
2470
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
2471
|
+
}
|
|
2472
|
+
if (!response.body) {
|
|
2473
|
+
throw new Error("Response body is null");
|
|
2474
|
+
}
|
|
2475
|
+
let failedChunk = void 0;
|
|
2476
|
+
const transformStream = new TransformStream({
|
|
2477
|
+
start() {
|
|
2478
|
+
},
|
|
2479
|
+
async transform(chunk, controller) {
|
|
2480
|
+
try {
|
|
2481
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2482
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2483
|
+
for (const chunk2 of chunks) {
|
|
2484
|
+
if (chunk2) {
|
|
2485
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2486
|
+
try {
|
|
2487
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2488
|
+
controller.enqueue(parsedChunk);
|
|
2489
|
+
failedChunk = void 0;
|
|
2490
|
+
} catch {
|
|
2491
|
+
failedChunk = newChunk;
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
} catch {
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
});
|
|
2499
|
+
return response.body.pipeThrough(transformStream);
|
|
2500
|
+
}
|
|
2501
|
+
/**
|
|
2502
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2503
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
2504
|
+
*/
|
|
2505
|
+
async streamVNext(params, runId) {
|
|
2506
|
+
const searchParams = new URLSearchParams();
|
|
2507
|
+
if (runId) {
|
|
2508
|
+
searchParams.set("runId", runId);
|
|
2509
|
+
}
|
|
2510
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2511
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2512
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2513
|
+
const response = await this.request(url, {
|
|
2514
|
+
method: "POST",
|
|
2515
|
+
body: { ...actionParams, runtimeContext },
|
|
2516
|
+
stream: true
|
|
2517
|
+
});
|
|
2518
|
+
if (!response.ok) {
|
|
2519
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2520
|
+
}
|
|
2521
|
+
if (!response.body) {
|
|
2522
|
+
throw new Error("Response body is null");
|
|
2523
|
+
}
|
|
2524
|
+
let failedChunk = void 0;
|
|
2525
|
+
const transformStream = new TransformStream({
|
|
2526
|
+
start() {
|
|
2527
|
+
},
|
|
2528
|
+
async transform(chunk, controller) {
|
|
2529
|
+
try {
|
|
2530
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2531
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2532
|
+
for (const chunk2 of chunks) {
|
|
2533
|
+
if (chunk2) {
|
|
2534
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2535
|
+
try {
|
|
2536
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2537
|
+
controller.enqueue(parsedChunk);
|
|
2538
|
+
failedChunk = void 0;
|
|
2539
|
+
} catch {
|
|
2540
|
+
failedChunk = newChunk;
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
} catch {
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
});
|
|
2548
|
+
return response.body.pipeThrough(transformStream);
|
|
2549
|
+
}
|
|
2550
|
+
/**
|
|
2551
|
+
* Watches an existing agent builder action run by runId.
|
|
2552
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2553
|
+
* and streams any remaining progress.
|
|
2554
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
2555
|
+
*/
|
|
2556
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2557
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2558
|
+
const response = await this.request(url, {
|
|
2559
|
+
method: "GET",
|
|
2560
|
+
stream: true
|
|
2561
|
+
});
|
|
2562
|
+
if (!response.ok) {
|
|
2563
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
2564
|
+
}
|
|
2565
|
+
if (!response.body) {
|
|
2566
|
+
throw new Error("Response body is null");
|
|
2567
|
+
}
|
|
2568
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2569
|
+
if (typeof record === "string") {
|
|
2570
|
+
onRecord(JSON.parse(record));
|
|
2571
|
+
} else {
|
|
2572
|
+
onRecord(record);
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
/**
|
|
2577
|
+
* Gets a specific action run by its ID.
|
|
2578
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
2579
|
+
*/
|
|
2580
|
+
async runById(runId) {
|
|
2581
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2582
|
+
return this.request(url, {
|
|
2583
|
+
method: "GET"
|
|
2584
|
+
});
|
|
2585
|
+
}
|
|
2586
|
+
/**
|
|
2587
|
+
* Gets details about this agent builder action.
|
|
2588
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
2589
|
+
*/
|
|
2590
|
+
async details() {
|
|
2591
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2592
|
+
return result;
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Gets all runs for this agent builder action.
|
|
2596
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
2597
|
+
*/
|
|
2598
|
+
async runs(params) {
|
|
2599
|
+
const searchParams = new URLSearchParams();
|
|
2600
|
+
if (params?.fromDate) {
|
|
2601
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2602
|
+
}
|
|
2603
|
+
if (params?.toDate) {
|
|
2604
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2605
|
+
}
|
|
2606
|
+
if (params?.limit !== void 0) {
|
|
2607
|
+
searchParams.set("limit", String(params.limit));
|
|
2608
|
+
}
|
|
2609
|
+
if (params?.offset !== void 0) {
|
|
2610
|
+
searchParams.set("offset", String(params.offset));
|
|
2611
|
+
}
|
|
2612
|
+
if (params?.resourceId) {
|
|
2613
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2614
|
+
}
|
|
2615
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2616
|
+
return this.request(url, {
|
|
2617
|
+
method: "GET"
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* Gets the execution result of an agent builder action run.
|
|
2622
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
2623
|
+
*/
|
|
2624
|
+
async runExecutionResult(runId) {
|
|
2625
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2626
|
+
return this.request(url, {
|
|
2627
|
+
method: "GET"
|
|
2628
|
+
});
|
|
2629
|
+
}
|
|
2630
|
+
/**
|
|
2631
|
+
* Cancels an agent builder action run.
|
|
2632
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
2633
|
+
*/
|
|
2634
|
+
async cancelRun(runId) {
|
|
2635
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2636
|
+
return this.request(url, {
|
|
2637
|
+
method: "POST"
|
|
2638
|
+
});
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* Sends an event to an agent builder action run.
|
|
2642
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
2643
|
+
*/
|
|
2644
|
+
async sendRunEvent(params) {
|
|
2645
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2646
|
+
return this.request(url, {
|
|
2647
|
+
method: "POST",
|
|
2648
|
+
body: { event: params.event, data: params.data }
|
|
2649
|
+
});
|
|
2650
|
+
}
|
|
2651
|
+
};
|
|
2652
|
+
|
|
2653
|
+
// src/resources/observability.ts
|
|
2654
|
+
var Observability = class extends BaseResource {
|
|
2655
|
+
constructor(options) {
|
|
2656
|
+
super(options);
|
|
2657
|
+
}
|
|
2658
|
+
/**
|
|
2659
|
+
* Retrieves a specific AI trace by ID
|
|
2660
|
+
* @param traceId - ID of the trace to retrieve
|
|
2661
|
+
* @returns Promise containing the AI trace with all its spans
|
|
2662
|
+
*/
|
|
2663
|
+
getTrace(traceId) {
|
|
2664
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
2665
|
+
}
|
|
2666
|
+
/**
|
|
2667
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2668
|
+
* @param params - Parameters for pagination and filtering
|
|
2669
|
+
* @returns Promise containing paginated traces and pagination info
|
|
2670
|
+
*/
|
|
2671
|
+
getTraces(params) {
|
|
2672
|
+
const { pagination, filters } = params;
|
|
2673
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2674
|
+
const { name, spanType, entityId, entityType } = filters || {};
|
|
2675
|
+
const searchParams = new URLSearchParams();
|
|
2676
|
+
if (page !== void 0) {
|
|
2677
|
+
searchParams.set("page", String(page));
|
|
2678
|
+
}
|
|
2679
|
+
if (perPage !== void 0) {
|
|
2680
|
+
searchParams.set("perPage", String(perPage));
|
|
2681
|
+
}
|
|
2682
|
+
if (name) {
|
|
2683
|
+
searchParams.set("name", name);
|
|
2684
|
+
}
|
|
2685
|
+
if (spanType !== void 0) {
|
|
2686
|
+
searchParams.set("spanType", String(spanType));
|
|
2687
|
+
}
|
|
2688
|
+
if (entityId && entityType) {
|
|
2689
|
+
searchParams.set("entityId", entityId);
|
|
2690
|
+
searchParams.set("entityType", entityType);
|
|
2691
|
+
}
|
|
2692
|
+
if (dateRange) {
|
|
2693
|
+
const dateRangeStr = JSON.stringify({
|
|
2694
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2695
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2696
|
+
});
|
|
2697
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2698
|
+
}
|
|
2699
|
+
const queryString = searchParams.toString();
|
|
2700
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
2701
|
+
}
|
|
2702
|
+
};
|
|
2703
|
+
|
|
2704
|
+
// src/resources/network-memory-thread.ts
|
|
2705
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
2706
|
+
constructor(options, threadId, networkId) {
|
|
2707
|
+
super(options);
|
|
2708
|
+
this.threadId = threadId;
|
|
2709
|
+
this.networkId = networkId;
|
|
2710
|
+
}
|
|
2711
|
+
/**
|
|
2712
|
+
* Retrieves the memory thread details
|
|
2713
|
+
* @returns Promise containing thread details including title and metadata
|
|
2714
|
+
*/
|
|
2715
|
+
get() {
|
|
2716
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
2717
|
+
}
|
|
2718
|
+
/**
|
|
2719
|
+
* Updates the memory thread properties
|
|
2720
|
+
* @param params - Update parameters including title and metadata
|
|
2721
|
+
* @returns Promise containing updated thread details
|
|
2722
|
+
*/
|
|
2723
|
+
update(params) {
|
|
2724
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2725
|
+
method: "PATCH",
|
|
2726
|
+
body: params
|
|
2727
|
+
});
|
|
2728
|
+
}
|
|
2729
|
+
/**
|
|
2730
|
+
* Deletes the memory thread
|
|
2731
|
+
* @returns Promise containing deletion result
|
|
2732
|
+
*/
|
|
2733
|
+
delete() {
|
|
2734
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2735
|
+
method: "DELETE"
|
|
2736
|
+
});
|
|
2737
|
+
}
|
|
2738
|
+
/**
|
|
2739
|
+
* Retrieves messages associated with the thread
|
|
2740
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
2741
|
+
* @returns Promise containing thread messages and UI messages
|
|
2742
|
+
*/
|
|
2743
|
+
getMessages(params) {
|
|
2744
|
+
const query = new URLSearchParams({
|
|
2745
|
+
networkId: this.networkId,
|
|
2746
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
2747
|
+
});
|
|
2748
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Deletes one or more messages from the thread
|
|
2752
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
2753
|
+
* message object with id property, or array of message objects
|
|
2754
|
+
* @returns Promise containing deletion result
|
|
2755
|
+
*/
|
|
2756
|
+
deleteMessages(messageIds) {
|
|
2757
|
+
const query = new URLSearchParams({
|
|
2758
|
+
networkId: this.networkId
|
|
2759
|
+
});
|
|
2760
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
2761
|
+
method: "POST",
|
|
2762
|
+
body: { messageIds }
|
|
2763
|
+
});
|
|
2764
|
+
}
|
|
2765
|
+
};
|
|
2766
|
+
|
|
2767
|
+
// src/resources/vNextNetwork.ts
|
|
2768
|
+
var RECORD_SEPARATOR4 = "";
|
|
2769
|
+
var VNextNetwork = class extends BaseResource {
|
|
2770
|
+
constructor(options, networkId) {
|
|
2771
|
+
super(options);
|
|
2772
|
+
this.networkId = networkId;
|
|
2773
|
+
}
|
|
2774
|
+
/**
|
|
2775
|
+
* Retrieves details about the network
|
|
2776
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2777
|
+
* @returns Promise containing vNext network details
|
|
2778
|
+
*/
|
|
2779
|
+
details(runtimeContext) {
|
|
2780
|
+
return this.request(`/api/networks/v-next/${this.networkId}${runtimeContextQueryString(runtimeContext)}`);
|
|
2781
|
+
}
|
|
2782
|
+
/**
|
|
2783
|
+
* Generates a response from the v-next network
|
|
2784
|
+
* @param params - Generation parameters including message
|
|
2785
|
+
* @returns Promise containing the generated response
|
|
2786
|
+
*/
|
|
2787
|
+
generate(params) {
|
|
2788
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
2789
|
+
method: "POST",
|
|
2790
|
+
body: {
|
|
2791
|
+
...params,
|
|
2792
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2793
|
+
}
|
|
2794
|
+
});
|
|
2795
|
+
}
|
|
2796
|
+
/**
|
|
2797
|
+
* Generates a response from the v-next network using multiple primitives
|
|
2798
|
+
* @param params - Generation parameters including message
|
|
2799
|
+
* @returns Promise containing the generated response
|
|
2800
|
+
*/
|
|
2801
|
+
loop(params) {
|
|
2802
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1040
2803
|
method: "POST",
|
|
1041
2804
|
body: {
|
|
1042
|
-
|
|
1043
|
-
params
|
|
2805
|
+
...params,
|
|
2806
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1044
2807
|
}
|
|
1045
2808
|
});
|
|
1046
2809
|
}
|
|
2810
|
+
async *streamProcessor(stream) {
|
|
2811
|
+
const reader = stream.getReader();
|
|
2812
|
+
let doneReading = false;
|
|
2813
|
+
let buffer = "";
|
|
2814
|
+
try {
|
|
2815
|
+
while (!doneReading) {
|
|
2816
|
+
const { done, value } = await reader.read();
|
|
2817
|
+
doneReading = done;
|
|
2818
|
+
if (done && !value) continue;
|
|
2819
|
+
try {
|
|
2820
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2821
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR4);
|
|
2822
|
+
buffer = chunks.pop() || "";
|
|
2823
|
+
for (const chunk of chunks) {
|
|
2824
|
+
if (chunk) {
|
|
2825
|
+
if (typeof chunk === "string") {
|
|
2826
|
+
try {
|
|
2827
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2828
|
+
yield parsedChunk;
|
|
2829
|
+
} catch {
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
} catch {
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
if (buffer) {
|
|
2838
|
+
try {
|
|
2839
|
+
yield JSON.parse(buffer);
|
|
2840
|
+
} catch {
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
} finally {
|
|
2844
|
+
reader.cancel().catch(() => {
|
|
2845
|
+
});
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
1047
2848
|
/**
|
|
1048
|
-
*
|
|
1049
|
-
* @param params -
|
|
1050
|
-
* @returns Promise containing the
|
|
2849
|
+
* Streams a response from the v-next network
|
|
2850
|
+
* @param params - Stream parameters including message
|
|
2851
|
+
* @returns Promise containing the results
|
|
1051
2852
|
*/
|
|
1052
|
-
async
|
|
1053
|
-
|
|
2853
|
+
async stream(params, onRecord) {
|
|
2854
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1054
2855
|
method: "POST",
|
|
1055
2856
|
body: {
|
|
1056
|
-
|
|
1057
|
-
params
|
|
2857
|
+
...params,
|
|
2858
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1058
2859
|
},
|
|
1059
2860
|
stream: true
|
|
1060
2861
|
});
|
|
1061
|
-
|
|
1062
|
-
};
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
* Retrieves details about this specific tool from the MCP server.
|
|
1075
|
-
* @returns Promise containing the tool's information (name, description, schema).
|
|
1076
|
-
*/
|
|
1077
|
-
details() {
|
|
1078
|
-
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
2862
|
+
if (!response.ok) {
|
|
2863
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
2864
|
+
}
|
|
2865
|
+
if (!response.body) {
|
|
2866
|
+
throw new Error("Response body is null");
|
|
2867
|
+
}
|
|
2868
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2869
|
+
if (typeof record === "string") {
|
|
2870
|
+
onRecord(JSON.parse(record));
|
|
2871
|
+
} else {
|
|
2872
|
+
onRecord(record);
|
|
2873
|
+
}
|
|
2874
|
+
}
|
|
1079
2875
|
}
|
|
1080
2876
|
/**
|
|
1081
|
-
*
|
|
1082
|
-
* @param params -
|
|
1083
|
-
* @returns Promise containing the
|
|
2877
|
+
* Streams a response from the v-next network loop
|
|
2878
|
+
* @param params - Stream parameters including message
|
|
2879
|
+
* @returns Promise containing the results
|
|
1084
2880
|
*/
|
|
1085
|
-
|
|
1086
|
-
const
|
|
1087
|
-
if (params.data !== void 0) body.data = params.data;
|
|
1088
|
-
if (params.runtimeContext !== void 0) {
|
|
1089
|
-
body.runtimeContext = params.runtimeContext;
|
|
1090
|
-
}
|
|
1091
|
-
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2881
|
+
async loopStream(params, onRecord) {
|
|
2882
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1092
2883
|
method: "POST",
|
|
1093
|
-
body:
|
|
2884
|
+
body: {
|
|
2885
|
+
...params,
|
|
2886
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2887
|
+
},
|
|
2888
|
+
stream: true
|
|
1094
2889
|
});
|
|
2890
|
+
if (!response.ok) {
|
|
2891
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
2892
|
+
}
|
|
2893
|
+
if (!response.body) {
|
|
2894
|
+
throw new Error("Response body is null");
|
|
2895
|
+
}
|
|
2896
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2897
|
+
if (typeof record === "string") {
|
|
2898
|
+
onRecord(JSON.parse(record));
|
|
2899
|
+
} else {
|
|
2900
|
+
onRecord(record);
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
1095
2903
|
}
|
|
1096
2904
|
};
|
|
1097
2905
|
|
|
1098
2906
|
// src/client.ts
|
|
1099
2907
|
var MastraClient = class extends BaseResource {
|
|
2908
|
+
observability;
|
|
1100
2909
|
constructor(options) {
|
|
1101
2910
|
super(options);
|
|
2911
|
+
this.observability = new Observability(options);
|
|
1102
2912
|
}
|
|
1103
2913
|
/**
|
|
1104
2914
|
* Retrieves all available agents
|
|
2915
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1105
2916
|
* @returns Promise containing map of agent IDs to agent details
|
|
1106
2917
|
*/
|
|
1107
|
-
getAgents() {
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
acc[agentId] = new AGUIAdapter({
|
|
1116
|
-
agentId,
|
|
1117
|
-
agent,
|
|
1118
|
-
resourceId
|
|
1119
|
-
});
|
|
1120
|
-
return acc;
|
|
1121
|
-
},
|
|
1122
|
-
{}
|
|
1123
|
-
);
|
|
2918
|
+
getAgents(runtimeContext) {
|
|
2919
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2920
|
+
const searchParams = new URLSearchParams();
|
|
2921
|
+
if (runtimeContextParam) {
|
|
2922
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2923
|
+
}
|
|
2924
|
+
const queryString = searchParams.toString();
|
|
2925
|
+
return this.request(`/api/agents${queryString ? `?${queryString}` : ""}`);
|
|
1124
2926
|
}
|
|
1125
2927
|
/**
|
|
1126
2928
|
* Gets an agent instance by ID
|
|
@@ -1138,6 +2940,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1138
2940
|
getMemoryThreads(params) {
|
|
1139
2941
|
return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
|
|
1140
2942
|
}
|
|
2943
|
+
/**
|
|
2944
|
+
* Retrieves memory config for a resource
|
|
2945
|
+
* @param params - Parameters containing the resource ID
|
|
2946
|
+
* @returns Promise containing array of memory threads
|
|
2947
|
+
*/
|
|
2948
|
+
getMemoryConfig(params) {
|
|
2949
|
+
return this.request(`/api/memory/config?agentId=${params.agentId}`);
|
|
2950
|
+
}
|
|
1141
2951
|
/**
|
|
1142
2952
|
* Creates a new memory thread
|
|
1143
2953
|
* @param params - Parameters for creating the memory thread
|
|
@@ -1154,6 +2964,24 @@ var MastraClient = class extends BaseResource {
|
|
|
1154
2964
|
getMemoryThread(threadId, agentId) {
|
|
1155
2965
|
return new MemoryThread(this.options, threadId, agentId);
|
|
1156
2966
|
}
|
|
2967
|
+
getThreadMessages(threadId, opts = {}) {
|
|
2968
|
+
let url = "";
|
|
2969
|
+
if (opts.agentId) {
|
|
2970
|
+
url = `/api/memory/threads/${threadId}/messages?agentId=${opts.agentId}`;
|
|
2971
|
+
} else if (opts.networkId) {
|
|
2972
|
+
url = `/api/memory/network/threads/${threadId}/messages?networkId=${opts.networkId}`;
|
|
2973
|
+
}
|
|
2974
|
+
return this.request(url);
|
|
2975
|
+
}
|
|
2976
|
+
deleteThread(threadId, opts = {}) {
|
|
2977
|
+
let url = "";
|
|
2978
|
+
if (opts.agentId) {
|
|
2979
|
+
url = `/api/memory/threads/${threadId}?agentId=${opts.agentId}`;
|
|
2980
|
+
} else if (opts.networkId) {
|
|
2981
|
+
url = `/api/memory/network/threads/${threadId}?networkId=${opts.networkId}`;
|
|
2982
|
+
}
|
|
2983
|
+
return this.request(url, { method: "DELETE" });
|
|
2984
|
+
}
|
|
1157
2985
|
/**
|
|
1158
2986
|
* Saves messages to memory
|
|
1159
2987
|
* @param params - Parameters containing messages to save
|
|
@@ -1172,12 +3000,61 @@ var MastraClient = class extends BaseResource {
|
|
|
1172
3000
|
getMemoryStatus(agentId) {
|
|
1173
3001
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1174
3002
|
}
|
|
3003
|
+
/**
|
|
3004
|
+
* Retrieves memory threads for a resource
|
|
3005
|
+
* @param params - Parameters containing the resource ID
|
|
3006
|
+
* @returns Promise containing array of memory threads
|
|
3007
|
+
*/
|
|
3008
|
+
getNetworkMemoryThreads(params) {
|
|
3009
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Creates a new memory thread
|
|
3013
|
+
* @param params - Parameters for creating the memory thread
|
|
3014
|
+
* @returns Promise containing the created memory thread
|
|
3015
|
+
*/
|
|
3016
|
+
createNetworkMemoryThread(params) {
|
|
3017
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
3018
|
+
}
|
|
3019
|
+
/**
|
|
3020
|
+
* Gets a memory thread instance by ID
|
|
3021
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
3022
|
+
* @returns MemoryThread instance
|
|
3023
|
+
*/
|
|
3024
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
3025
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
3026
|
+
}
|
|
3027
|
+
/**
|
|
3028
|
+
* Saves messages to memory
|
|
3029
|
+
* @param params - Parameters containing messages to save
|
|
3030
|
+
* @returns Promise containing the saved messages
|
|
3031
|
+
*/
|
|
3032
|
+
saveNetworkMessageToMemory(params) {
|
|
3033
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
3034
|
+
method: "POST",
|
|
3035
|
+
body: params
|
|
3036
|
+
});
|
|
3037
|
+
}
|
|
3038
|
+
/**
|
|
3039
|
+
* Gets the status of the memory system
|
|
3040
|
+
* @returns Promise containing memory system status
|
|
3041
|
+
*/
|
|
3042
|
+
getNetworkMemoryStatus(networkId) {
|
|
3043
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
3044
|
+
}
|
|
1175
3045
|
/**
|
|
1176
3046
|
* Retrieves all available tools
|
|
3047
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1177
3048
|
* @returns Promise containing map of tool IDs to tool details
|
|
1178
3049
|
*/
|
|
1179
|
-
getTools() {
|
|
1180
|
-
|
|
3050
|
+
getTools(runtimeContext) {
|
|
3051
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
3052
|
+
const searchParams = new URLSearchParams();
|
|
3053
|
+
if (runtimeContextParam) {
|
|
3054
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
3055
|
+
}
|
|
3056
|
+
const queryString = searchParams.toString();
|
|
3057
|
+
return this.request(`/api/tools${queryString ? `?${queryString}` : ""}`);
|
|
1181
3058
|
}
|
|
1182
3059
|
/**
|
|
1183
3060
|
* Gets a tool instance by ID
|
|
@@ -1187,12 +3064,34 @@ var MastraClient = class extends BaseResource {
|
|
|
1187
3064
|
getTool(toolId) {
|
|
1188
3065
|
return new Tool(this.options, toolId);
|
|
1189
3066
|
}
|
|
3067
|
+
/**
|
|
3068
|
+
* Retrieves all available legacy workflows
|
|
3069
|
+
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
3070
|
+
*/
|
|
3071
|
+
getLegacyWorkflows() {
|
|
3072
|
+
return this.request("/api/workflows/legacy");
|
|
3073
|
+
}
|
|
3074
|
+
/**
|
|
3075
|
+
* Gets a legacy workflow instance by ID
|
|
3076
|
+
* @param workflowId - ID of the legacy workflow to retrieve
|
|
3077
|
+
* @returns Legacy Workflow instance
|
|
3078
|
+
*/
|
|
3079
|
+
getLegacyWorkflow(workflowId) {
|
|
3080
|
+
return new LegacyWorkflow(this.options, workflowId);
|
|
3081
|
+
}
|
|
1190
3082
|
/**
|
|
1191
3083
|
* Retrieves all available workflows
|
|
3084
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1192
3085
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
1193
3086
|
*/
|
|
1194
|
-
getWorkflows() {
|
|
1195
|
-
|
|
3087
|
+
getWorkflows(runtimeContext) {
|
|
3088
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
3089
|
+
const searchParams = new URLSearchParams();
|
|
3090
|
+
if (runtimeContextParam) {
|
|
3091
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
3092
|
+
}
|
|
3093
|
+
const queryString = searchParams.toString();
|
|
3094
|
+
return this.request(`/api/workflows${queryString ? `?${queryString}` : ""}`);
|
|
1196
3095
|
}
|
|
1197
3096
|
/**
|
|
1198
3097
|
* Gets a workflow instance by ID
|
|
@@ -1203,19 +3102,18 @@ var MastraClient = class extends BaseResource {
|
|
|
1203
3102
|
return new Workflow(this.options, workflowId);
|
|
1204
3103
|
}
|
|
1205
3104
|
/**
|
|
1206
|
-
*
|
|
1207
|
-
* @returns Promise containing map of
|
|
3105
|
+
* Gets all available agent builder actions
|
|
3106
|
+
* @returns Promise containing map of action IDs to action details
|
|
1208
3107
|
*/
|
|
1209
|
-
|
|
1210
|
-
return this.request("/api/
|
|
3108
|
+
getAgentBuilderActions() {
|
|
3109
|
+
return this.request("/api/agent-builder/");
|
|
1211
3110
|
}
|
|
1212
3111
|
/**
|
|
1213
|
-
* Gets
|
|
1214
|
-
* @
|
|
1215
|
-
* @returns vNext Workflow instance
|
|
3112
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
3113
|
+
* @returns AgentBuilder instance
|
|
1216
3114
|
*/
|
|
1217
|
-
|
|
1218
|
-
return new
|
|
3115
|
+
getAgentBuilderAction(actionId) {
|
|
3116
|
+
return new AgentBuilder(this.options, actionId);
|
|
1219
3117
|
}
|
|
1220
3118
|
/**
|
|
1221
3119
|
* Gets a vector instance by name
|
|
@@ -1231,7 +3129,41 @@ var MastraClient = class extends BaseResource {
|
|
|
1231
3129
|
* @returns Promise containing array of log messages
|
|
1232
3130
|
*/
|
|
1233
3131
|
getLogs(params) {
|
|
1234
|
-
|
|
3132
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
3133
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
3134
|
+
const searchParams = new URLSearchParams();
|
|
3135
|
+
if (transportId) {
|
|
3136
|
+
searchParams.set("transportId", transportId);
|
|
3137
|
+
}
|
|
3138
|
+
if (fromDate) {
|
|
3139
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
3140
|
+
}
|
|
3141
|
+
if (toDate) {
|
|
3142
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
3143
|
+
}
|
|
3144
|
+
if (logLevel) {
|
|
3145
|
+
searchParams.set("logLevel", logLevel);
|
|
3146
|
+
}
|
|
3147
|
+
if (page) {
|
|
3148
|
+
searchParams.set("page", String(page));
|
|
3149
|
+
}
|
|
3150
|
+
if (perPage) {
|
|
3151
|
+
searchParams.set("perPage", String(perPage));
|
|
3152
|
+
}
|
|
3153
|
+
if (_filters) {
|
|
3154
|
+
if (Array.isArray(_filters)) {
|
|
3155
|
+
for (const filter of _filters) {
|
|
3156
|
+
searchParams.append("filters", filter);
|
|
3157
|
+
}
|
|
3158
|
+
} else {
|
|
3159
|
+
searchParams.set("filters", _filters);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
if (searchParams.size) {
|
|
3163
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
3164
|
+
} else {
|
|
3165
|
+
return this.request(`/api/logs`);
|
|
3166
|
+
}
|
|
1235
3167
|
}
|
|
1236
3168
|
/**
|
|
1237
3169
|
* Gets logs for a specific run
|
|
@@ -1239,7 +3171,44 @@ var MastraClient = class extends BaseResource {
|
|
|
1239
3171
|
* @returns Promise containing array of log messages
|
|
1240
3172
|
*/
|
|
1241
3173
|
getLogForRun(params) {
|
|
1242
|
-
|
|
3174
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
3175
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
3176
|
+
const searchParams = new URLSearchParams();
|
|
3177
|
+
if (runId) {
|
|
3178
|
+
searchParams.set("runId", runId);
|
|
3179
|
+
}
|
|
3180
|
+
if (transportId) {
|
|
3181
|
+
searchParams.set("transportId", transportId);
|
|
3182
|
+
}
|
|
3183
|
+
if (fromDate) {
|
|
3184
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
3185
|
+
}
|
|
3186
|
+
if (toDate) {
|
|
3187
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
3188
|
+
}
|
|
3189
|
+
if (logLevel) {
|
|
3190
|
+
searchParams.set("logLevel", logLevel);
|
|
3191
|
+
}
|
|
3192
|
+
if (page) {
|
|
3193
|
+
searchParams.set("page", String(page));
|
|
3194
|
+
}
|
|
3195
|
+
if (perPage) {
|
|
3196
|
+
searchParams.set("perPage", String(perPage));
|
|
3197
|
+
}
|
|
3198
|
+
if (_filters) {
|
|
3199
|
+
if (Array.isArray(_filters)) {
|
|
3200
|
+
for (const filter of _filters) {
|
|
3201
|
+
searchParams.append("filters", filter);
|
|
3202
|
+
}
|
|
3203
|
+
} else {
|
|
3204
|
+
searchParams.set("filters", _filters);
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
if (searchParams.size) {
|
|
3208
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
3209
|
+
} else {
|
|
3210
|
+
return this.request(`/api/logs/${runId}`);
|
|
3211
|
+
}
|
|
1243
3212
|
}
|
|
1244
3213
|
/**
|
|
1245
3214
|
* List of all log transports
|
|
@@ -1291,19 +3260,19 @@ var MastraClient = class extends BaseResource {
|
|
|
1291
3260
|
}
|
|
1292
3261
|
}
|
|
1293
3262
|
/**
|
|
1294
|
-
* Retrieves all available networks
|
|
1295
|
-
* @returns Promise containing map of network IDs to network details
|
|
3263
|
+
* Retrieves all available vNext networks
|
|
3264
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
1296
3265
|
*/
|
|
1297
|
-
|
|
1298
|
-
return this.request("/api/networks");
|
|
3266
|
+
getVNextNetworks() {
|
|
3267
|
+
return this.request("/api/networks/v-next");
|
|
1299
3268
|
}
|
|
1300
3269
|
/**
|
|
1301
|
-
* Gets a network instance by ID
|
|
1302
|
-
* @param networkId - ID of the network to retrieve
|
|
1303
|
-
* @returns Network instance
|
|
3270
|
+
* Gets a vNext network instance by ID
|
|
3271
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
3272
|
+
* @returns vNext Network instance
|
|
1304
3273
|
*/
|
|
1305
|
-
|
|
1306
|
-
return new
|
|
3274
|
+
getVNextNetwork(networkId) {
|
|
3275
|
+
return new VNextNetwork(this.options, networkId);
|
|
1307
3276
|
}
|
|
1308
3277
|
/**
|
|
1309
3278
|
* Retrieves a list of available MCP servers.
|
|
@@ -1361,6 +3330,153 @@ var MastraClient = class extends BaseResource {
|
|
|
1361
3330
|
getA2A(agentId) {
|
|
1362
3331
|
return new A2A(this.options, agentId);
|
|
1363
3332
|
}
|
|
3333
|
+
/**
|
|
3334
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
3335
|
+
* @param agentId - ID of the agent.
|
|
3336
|
+
* @param threadId - ID of the thread.
|
|
3337
|
+
* @param resourceId - Optional ID of the resource.
|
|
3338
|
+
* @returns Working memory for the specified thread or resource.
|
|
3339
|
+
*/
|
|
3340
|
+
getWorkingMemory({
|
|
3341
|
+
agentId,
|
|
3342
|
+
threadId,
|
|
3343
|
+
resourceId
|
|
3344
|
+
}) {
|
|
3345
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
3346
|
+
}
|
|
3347
|
+
/**
|
|
3348
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
3349
|
+
* @param agentId - ID of the agent.
|
|
3350
|
+
* @param threadId - ID of the thread.
|
|
3351
|
+
* @param workingMemory - The new working memory content.
|
|
3352
|
+
* @param resourceId - Optional ID of the resource.
|
|
3353
|
+
*/
|
|
3354
|
+
updateWorkingMemory({
|
|
3355
|
+
agentId,
|
|
3356
|
+
threadId,
|
|
3357
|
+
workingMemory,
|
|
3358
|
+
resourceId
|
|
3359
|
+
}) {
|
|
3360
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
3361
|
+
method: "POST",
|
|
3362
|
+
body: {
|
|
3363
|
+
workingMemory,
|
|
3364
|
+
resourceId
|
|
3365
|
+
}
|
|
3366
|
+
});
|
|
3367
|
+
}
|
|
3368
|
+
/**
|
|
3369
|
+
* Retrieves all available scorers
|
|
3370
|
+
* @returns Promise containing list of available scorers
|
|
3371
|
+
*/
|
|
3372
|
+
getScorers() {
|
|
3373
|
+
return this.request("/api/scores/scorers");
|
|
3374
|
+
}
|
|
3375
|
+
/**
|
|
3376
|
+
* Retrieves a scorer by ID
|
|
3377
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
3378
|
+
* @returns Promise containing the scorer
|
|
3379
|
+
*/
|
|
3380
|
+
getScorer(scorerId) {
|
|
3381
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3382
|
+
}
|
|
3383
|
+
getScoresByScorerId(params) {
|
|
3384
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
3385
|
+
const searchParams = new URLSearchParams();
|
|
3386
|
+
if (entityId) {
|
|
3387
|
+
searchParams.set("entityId", entityId);
|
|
3388
|
+
}
|
|
3389
|
+
if (entityType) {
|
|
3390
|
+
searchParams.set("entityType", entityType);
|
|
3391
|
+
}
|
|
3392
|
+
if (page !== void 0) {
|
|
3393
|
+
searchParams.set("page", String(page));
|
|
3394
|
+
}
|
|
3395
|
+
if (perPage !== void 0) {
|
|
3396
|
+
searchParams.set("perPage", String(perPage));
|
|
3397
|
+
}
|
|
3398
|
+
const queryString = searchParams.toString();
|
|
3399
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3400
|
+
}
|
|
3401
|
+
/**
|
|
3402
|
+
* Retrieves scores by run ID
|
|
3403
|
+
* @param params - Parameters containing run ID and pagination options
|
|
3404
|
+
* @returns Promise containing scores and pagination info
|
|
3405
|
+
*/
|
|
3406
|
+
getScoresByRunId(params) {
|
|
3407
|
+
const { runId, page, perPage } = params;
|
|
3408
|
+
const searchParams = new URLSearchParams();
|
|
3409
|
+
if (page !== void 0) {
|
|
3410
|
+
searchParams.set("page", String(page));
|
|
3411
|
+
}
|
|
3412
|
+
if (perPage !== void 0) {
|
|
3413
|
+
searchParams.set("perPage", String(perPage));
|
|
3414
|
+
}
|
|
3415
|
+
const queryString = searchParams.toString();
|
|
3416
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3417
|
+
}
|
|
3418
|
+
/**
|
|
3419
|
+
* Retrieves scores by entity ID and type
|
|
3420
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
3421
|
+
* @returns Promise containing scores and pagination info
|
|
3422
|
+
*/
|
|
3423
|
+
getScoresByEntityId(params) {
|
|
3424
|
+
const { entityId, entityType, page, perPage } = params;
|
|
3425
|
+
const searchParams = new URLSearchParams();
|
|
3426
|
+
if (page !== void 0) {
|
|
3427
|
+
searchParams.set("page", String(page));
|
|
3428
|
+
}
|
|
3429
|
+
if (perPage !== void 0) {
|
|
3430
|
+
searchParams.set("perPage", String(perPage));
|
|
3431
|
+
}
|
|
3432
|
+
const queryString = searchParams.toString();
|
|
3433
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
3434
|
+
}
|
|
3435
|
+
/**
|
|
3436
|
+
* Saves a score
|
|
3437
|
+
* @param params - Parameters containing the score data to save
|
|
3438
|
+
* @returns Promise containing the saved score
|
|
3439
|
+
*/
|
|
3440
|
+
saveScore(params) {
|
|
3441
|
+
return this.request("/api/scores", {
|
|
3442
|
+
method: "POST",
|
|
3443
|
+
body: params
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
/**
|
|
3447
|
+
* Retrieves model providers with available keys
|
|
3448
|
+
* @returns Promise containing model providers with available keys
|
|
3449
|
+
*/
|
|
3450
|
+
getModelProviders() {
|
|
3451
|
+
return this.request(`/api/model-providers`);
|
|
3452
|
+
}
|
|
3453
|
+
getAITrace(traceId) {
|
|
3454
|
+
return this.observability.getTrace(traceId);
|
|
3455
|
+
}
|
|
3456
|
+
getAITraces(params) {
|
|
3457
|
+
return this.observability.getTraces(params);
|
|
3458
|
+
}
|
|
1364
3459
|
};
|
|
1365
3460
|
|
|
1366
|
-
|
|
3461
|
+
// src/tools.ts
|
|
3462
|
+
var ClientTool = class {
|
|
3463
|
+
id;
|
|
3464
|
+
description;
|
|
3465
|
+
inputSchema;
|
|
3466
|
+
outputSchema;
|
|
3467
|
+
execute;
|
|
3468
|
+
constructor(opts) {
|
|
3469
|
+
this.id = opts.id;
|
|
3470
|
+
this.description = opts.description;
|
|
3471
|
+
this.inputSchema = opts.inputSchema;
|
|
3472
|
+
this.outputSchema = opts.outputSchema;
|
|
3473
|
+
this.execute = opts.execute;
|
|
3474
|
+
}
|
|
3475
|
+
};
|
|
3476
|
+
function createTool(opts) {
|
|
3477
|
+
return new ClientTool(opts);
|
|
3478
|
+
}
|
|
3479
|
+
|
|
3480
|
+
export { ClientTool, MastraClient, createTool };
|
|
3481
|
+
//# sourceMappingURL=index.js.map
|
|
3482
|
+
//# sourceMappingURL=index.js.map
|