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