@mastra/client-js 0.0.0-inject-middleware-20250528222017 → 0.0.0-issue-7087-20250909202326
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 +1375 -2
- package/LICENSE.md +11 -42
- package/README.md +7 -4
- package/dist/client.d.ts +280 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +2222 -375
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -900
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2224 -377
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent-builder.d.ts +161 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +155 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +13 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/legacy-workflow.d.ts +87 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +27 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/network.d.ts +30 -0
- package/dist/resources/network.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +19 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +23 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +42 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +48 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +169 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/types.d.ts +462 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +7 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/package.json +38 -21
- package/dist/index.d.cts +0 -900
- package/eslint.config.js +0 -6
- package/src/adapters/agui.test.ts +0 -180
- package/src/adapters/agui.ts +0 -239
- package/src/client.ts +0 -334
- package/src/example.ts +0 -64
- package/src/index.test.ts +0 -830
- package/src/index.ts +0 -2
- package/src/resources/a2a.ts +0 -88
- package/src/resources/agent.ts +0 -197
- package/src/resources/base.ts +0 -70
- package/src/resources/index.ts +0 -10
- package/src/resources/legacy-workflow.ts +0 -242
- package/src/resources/mcp-tool.ts +0 -48
- package/src/resources/memory-thread.ts +0 -63
- package/src/resources/network.ts +0 -86
- package/src/resources/tool.ts +0 -45
- package/src/resources/vector.ts +0 -83
- package/src/resources/workflow.ts +0 -353
- package/src/types.ts +0 -314
- package/src/utils/index.ts +0 -11
- package/src/utils/zod-to-json-schema.ts +0 -10
- package/tsconfig.json +0 -5
- package/vitest.config.js +0 -8
package/dist/index.cjs
CHANGED
|
@@ -1,208 +1,105 @@
|
|
|
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
8
|
var originalZodToJsonSchema = require('zod-to-json-schema');
|
|
8
|
-
var runtimeContext = require('@mastra/core/runtime-context');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
|
|
12
12
|
var originalZodToJsonSchema__default = /*#__PURE__*/_interopDefault(originalZodToJsonSchema);
|
|
13
13
|
|
|
14
|
-
// src/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
super({
|
|
20
|
-
agentId,
|
|
21
|
-
...rest
|
|
22
|
-
});
|
|
23
|
-
this.agent = agent;
|
|
24
|
-
this.resourceId = resourceId;
|
|
25
|
-
}
|
|
26
|
-
run(input) {
|
|
27
|
-
return new rxjs.Observable((subscriber) => {
|
|
28
|
-
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
29
|
-
subscriber.next({
|
|
30
|
-
type: client.EventType.RUN_STARTED,
|
|
31
|
-
threadId: input.threadId,
|
|
32
|
-
runId: input.runId
|
|
33
|
-
});
|
|
34
|
-
this.agent.stream({
|
|
35
|
-
threadId: input.threadId,
|
|
36
|
-
resourceId: this.resourceId ?? "",
|
|
37
|
-
runId: input.runId,
|
|
38
|
-
messages: convertedMessages,
|
|
39
|
-
clientTools: input.tools.reduce(
|
|
40
|
-
(acc, tool) => {
|
|
41
|
-
acc[tool.name] = {
|
|
42
|
-
id: tool.name,
|
|
43
|
-
description: tool.description,
|
|
44
|
-
inputSchema: tool.parameters
|
|
45
|
-
};
|
|
46
|
-
return acc;
|
|
47
|
-
},
|
|
48
|
-
{}
|
|
49
|
-
)
|
|
50
|
-
}).then((response) => {
|
|
51
|
-
let currentMessageId = void 0;
|
|
52
|
-
let isInTextMessage = false;
|
|
53
|
-
return response.processDataStream({
|
|
54
|
-
onTextPart: (text) => {
|
|
55
|
-
if (currentMessageId === void 0) {
|
|
56
|
-
currentMessageId = generateUUID();
|
|
57
|
-
const message2 = {
|
|
58
|
-
type: client.EventType.TEXT_MESSAGE_START,
|
|
59
|
-
messageId: currentMessageId,
|
|
60
|
-
role: "assistant"
|
|
61
|
-
};
|
|
62
|
-
subscriber.next(message2);
|
|
63
|
-
isInTextMessage = true;
|
|
64
|
-
}
|
|
65
|
-
const message = {
|
|
66
|
-
type: client.EventType.TEXT_MESSAGE_CONTENT,
|
|
67
|
-
messageId: currentMessageId,
|
|
68
|
-
delta: text
|
|
69
|
-
};
|
|
70
|
-
subscriber.next(message);
|
|
71
|
-
},
|
|
72
|
-
onFinishMessagePart: () => {
|
|
73
|
-
if (currentMessageId !== void 0) {
|
|
74
|
-
const message = {
|
|
75
|
-
type: client.EventType.TEXT_MESSAGE_END,
|
|
76
|
-
messageId: currentMessageId
|
|
77
|
-
};
|
|
78
|
-
subscriber.next(message);
|
|
79
|
-
isInTextMessage = false;
|
|
80
|
-
}
|
|
81
|
-
subscriber.next({
|
|
82
|
-
type: client.EventType.RUN_FINISHED,
|
|
83
|
-
threadId: input.threadId,
|
|
84
|
-
runId: input.runId
|
|
85
|
-
});
|
|
86
|
-
subscriber.complete();
|
|
87
|
-
},
|
|
88
|
-
onToolCallPart(streamPart) {
|
|
89
|
-
const parentMessageId = currentMessageId || generateUUID();
|
|
90
|
-
if (isInTextMessage) {
|
|
91
|
-
const message = {
|
|
92
|
-
type: client.EventType.TEXT_MESSAGE_END,
|
|
93
|
-
messageId: parentMessageId
|
|
94
|
-
};
|
|
95
|
-
subscriber.next(message);
|
|
96
|
-
isInTextMessage = false;
|
|
97
|
-
}
|
|
98
|
-
subscriber.next({
|
|
99
|
-
type: client.EventType.TOOL_CALL_START,
|
|
100
|
-
toolCallId: streamPart.toolCallId,
|
|
101
|
-
toolCallName: streamPart.toolName,
|
|
102
|
-
parentMessageId
|
|
103
|
-
});
|
|
104
|
-
subscriber.next({
|
|
105
|
-
type: client.EventType.TOOL_CALL_ARGS,
|
|
106
|
-
toolCallId: streamPart.toolCallId,
|
|
107
|
-
delta: JSON.stringify(streamPart.args),
|
|
108
|
-
parentMessageId
|
|
109
|
-
});
|
|
110
|
-
subscriber.next({
|
|
111
|
-
type: client.EventType.TOOL_CALL_END,
|
|
112
|
-
toolCallId: streamPart.toolCallId,
|
|
113
|
-
parentMessageId
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}).catch((error) => {
|
|
118
|
-
console.error("error", error);
|
|
119
|
-
subscriber.error(error);
|
|
120
|
-
});
|
|
121
|
-
return () => {
|
|
122
|
-
};
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
function generateUUID() {
|
|
127
|
-
if (typeof crypto !== "undefined") {
|
|
128
|
-
if (typeof crypto.randomUUID === "function") {
|
|
129
|
-
return crypto.randomUUID();
|
|
130
|
-
}
|
|
131
|
-
if (typeof crypto.getRandomValues === "function") {
|
|
132
|
-
const buffer = new Uint8Array(16);
|
|
133
|
-
crypto.getRandomValues(buffer);
|
|
134
|
-
buffer[6] = buffer[6] & 15 | 64;
|
|
135
|
-
buffer[8] = buffer[8] & 63 | 128;
|
|
136
|
-
let hex = "";
|
|
137
|
-
for (let i = 0; i < 16; i++) {
|
|
138
|
-
hex += buffer[i].toString(16).padStart(2, "0");
|
|
139
|
-
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
140
|
-
}
|
|
141
|
-
return hex;
|
|
14
|
+
// src/resources/agent.ts
|
|
15
|
+
function parseClientRuntimeContext(runtimeContext$1) {
|
|
16
|
+
if (runtimeContext$1) {
|
|
17
|
+
if (runtimeContext$1 instanceof runtimeContext.RuntimeContext) {
|
|
18
|
+
return Object.fromEntries(runtimeContext$1.entries());
|
|
142
19
|
}
|
|
20
|
+
return runtimeContext$1;
|
|
143
21
|
}
|
|
144
|
-
return
|
|
145
|
-
const r = Math.random() * 16 | 0;
|
|
146
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
147
|
-
return v.toString(16);
|
|
148
|
-
});
|
|
22
|
+
return void 0;
|
|
149
23
|
}
|
|
150
|
-
function
|
|
151
|
-
|
|
152
|
-
for (const message of messages) {
|
|
153
|
-
if (message.role === "assistant") {
|
|
154
|
-
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
155
|
-
for (const toolCall of message.toolCalls ?? []) {
|
|
156
|
-
parts.push({
|
|
157
|
-
type: "tool-call",
|
|
158
|
-
toolCallId: toolCall.id,
|
|
159
|
-
toolName: toolCall.function.name,
|
|
160
|
-
args: JSON.parse(toolCall.function.arguments)
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
result.push({
|
|
164
|
-
role: "assistant",
|
|
165
|
-
content: parts
|
|
166
|
-
});
|
|
167
|
-
if (message.toolCalls?.length) {
|
|
168
|
-
result.push({
|
|
169
|
-
role: "tool",
|
|
170
|
-
content: message.toolCalls.map((toolCall) => ({
|
|
171
|
-
type: "tool-result",
|
|
172
|
-
toolCallId: toolCall.id,
|
|
173
|
-
toolName: toolCall.function.name,
|
|
174
|
-
result: JSON.parse(toolCall.function.arguments)
|
|
175
|
-
}))
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
} else if (message.role === "user") {
|
|
179
|
-
result.push({
|
|
180
|
-
role: "user",
|
|
181
|
-
content: message.content || ""
|
|
182
|
-
});
|
|
183
|
-
} else if (message.role === "tool") {
|
|
184
|
-
result.push({
|
|
185
|
-
role: "tool",
|
|
186
|
-
content: [
|
|
187
|
-
{
|
|
188
|
-
type: "tool-result",
|
|
189
|
-
toolCallId: message.toolCallId,
|
|
190
|
-
toolName: "unknown",
|
|
191
|
-
result: message.content
|
|
192
|
-
}
|
|
193
|
-
]
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return result;
|
|
24
|
+
function isZodType(value) {
|
|
25
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
198
26
|
}
|
|
199
27
|
function zodToJsonSchema(zodSchema) {
|
|
200
|
-
if (!(zodSchema
|
|
28
|
+
if (!isZodType(zodSchema)) {
|
|
201
29
|
return zodSchema;
|
|
202
30
|
}
|
|
31
|
+
if ("toJSONSchema" in zod.z) {
|
|
32
|
+
const fn = "toJSONSchema";
|
|
33
|
+
return zod.z[fn].call(zod.z, zodSchema);
|
|
34
|
+
}
|
|
203
35
|
return originalZodToJsonSchema__default.default(zodSchema, { $refStrategy: "none" });
|
|
204
36
|
}
|
|
205
37
|
|
|
38
|
+
// src/utils/process-client-tools.ts
|
|
39
|
+
function processClientTools(clientTools) {
|
|
40
|
+
if (!clientTools) {
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
return Object.fromEntries(
|
|
44
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
45
|
+
if (isVercelTool.isVercelTool(value)) {
|
|
46
|
+
return [
|
|
47
|
+
key,
|
|
48
|
+
{
|
|
49
|
+
...value,
|
|
50
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
} else {
|
|
54
|
+
return [
|
|
55
|
+
key,
|
|
56
|
+
{
|
|
57
|
+
...value,
|
|
58
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
|
|
59
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
|
|
60
|
+
}
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/utils/process-mastra-stream.ts
|
|
68
|
+
async function processMastraStream({
|
|
69
|
+
stream,
|
|
70
|
+
onChunk
|
|
71
|
+
}) {
|
|
72
|
+
const reader = stream.getReader();
|
|
73
|
+
const decoder = new TextDecoder();
|
|
74
|
+
let buffer = "";
|
|
75
|
+
try {
|
|
76
|
+
while (true) {
|
|
77
|
+
const { done, value } = await reader.read();
|
|
78
|
+
if (done) break;
|
|
79
|
+
buffer += decoder.decode(value, { stream: true });
|
|
80
|
+
const lines = buffer.split("\n\n");
|
|
81
|
+
buffer = lines.pop() || "";
|
|
82
|
+
for (const line of lines) {
|
|
83
|
+
if (line.startsWith("data: ")) {
|
|
84
|
+
const data = line.slice(6);
|
|
85
|
+
if (data === "[DONE]") {
|
|
86
|
+
console.log("\u{1F3C1} Stream finished");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const json = JSON.parse(data);
|
|
91
|
+
await onChunk(json);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} finally {
|
|
99
|
+
reader.releaseLock();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
206
103
|
// src/resources/base.ts
|
|
207
104
|
var BaseResource = class {
|
|
208
105
|
options;
|
|
@@ -217,18 +114,21 @@ var BaseResource = class {
|
|
|
217
114
|
*/
|
|
218
115
|
async request(path, options = {}) {
|
|
219
116
|
let lastError = null;
|
|
220
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
117
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
221
118
|
let delay = backoffMs;
|
|
222
119
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
223
120
|
try {
|
|
224
121
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
225
122
|
...options,
|
|
226
123
|
headers: {
|
|
124
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
227
125
|
...headers,
|
|
228
126
|
...options.headers
|
|
229
127
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
230
128
|
// 'x-mastra-client-type': 'js',
|
|
231
129
|
},
|
|
130
|
+
signal: this.options.abortSignal,
|
|
131
|
+
credentials: options.credentials ?? credentials,
|
|
232
132
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
233
133
|
});
|
|
234
134
|
if (!response.ok) {
|
|
@@ -261,17 +161,65 @@ var BaseResource = class {
|
|
|
261
161
|
throw lastError || new Error("Request failed");
|
|
262
162
|
}
|
|
263
163
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
164
|
+
|
|
165
|
+
// src/resources/agent.ts
|
|
166
|
+
async function executeToolCallAndRespond({
|
|
167
|
+
response,
|
|
168
|
+
params,
|
|
169
|
+
runId,
|
|
170
|
+
resourceId,
|
|
171
|
+
threadId,
|
|
172
|
+
runtimeContext,
|
|
173
|
+
respondFn
|
|
174
|
+
}) {
|
|
175
|
+
if (response.finishReason === "tool-calls") {
|
|
176
|
+
const toolCalls = response.toolCalls;
|
|
177
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
178
|
+
return response;
|
|
179
|
+
}
|
|
180
|
+
for (const toolCall of toolCalls) {
|
|
181
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
182
|
+
if (clientTool && clientTool.execute) {
|
|
183
|
+
const result = await clientTool.execute(
|
|
184
|
+
{
|
|
185
|
+
context: toolCall?.args,
|
|
186
|
+
runId,
|
|
187
|
+
resourceId,
|
|
188
|
+
threadId,
|
|
189
|
+
runtimeContext,
|
|
190
|
+
tracingContext: { currentSpan: void 0 }
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
messages: response.messages,
|
|
194
|
+
toolCallId: toolCall?.toolCallId
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
const updatedMessages = [
|
|
198
|
+
{
|
|
199
|
+
role: "user",
|
|
200
|
+
content: params.messages
|
|
201
|
+
},
|
|
202
|
+
...response.response.messages,
|
|
203
|
+
{
|
|
204
|
+
role: "tool",
|
|
205
|
+
content: [
|
|
206
|
+
{
|
|
207
|
+
type: "tool-result",
|
|
208
|
+
toolCallId: toolCall.toolCallId,
|
|
209
|
+
toolName: toolCall.toolName,
|
|
210
|
+
result
|
|
211
|
+
}
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
];
|
|
215
|
+
return respondFn({
|
|
216
|
+
...params,
|
|
217
|
+
messages: updatedMessages
|
|
218
|
+
});
|
|
219
|
+
}
|
|
268
220
|
}
|
|
269
|
-
return runtimeContext$1;
|
|
270
221
|
}
|
|
271
|
-
return void 0;
|
|
272
222
|
}
|
|
273
|
-
|
|
274
|
-
// src/resources/agent.ts
|
|
275
223
|
var AgentVoice = class extends BaseResource {
|
|
276
224
|
constructor(options, agentId) {
|
|
277
225
|
super(options);
|
|
@@ -318,6 +266,13 @@ var AgentVoice = class extends BaseResource {
|
|
|
318
266
|
getSpeakers() {
|
|
319
267
|
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
320
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Get the listener configuration for the agent's voice provider
|
|
271
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
272
|
+
*/
|
|
273
|
+
getListener() {
|
|
274
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener`);
|
|
275
|
+
}
|
|
321
276
|
};
|
|
322
277
|
var Agent = class extends BaseResource {
|
|
323
278
|
constructor(options, agentId) {
|
|
@@ -333,128 +288,1012 @@ var Agent = class extends BaseResource {
|
|
|
333
288
|
details() {
|
|
334
289
|
return this.request(`/api/agents/${this.agentId}`);
|
|
335
290
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
291
|
+
async generate(params) {
|
|
292
|
+
console.warn(
|
|
293
|
+
"Deprecation NOTICE:Generate method will switch to use generateVNext implementation September 16th. Please use generateLegacy if you don't want to upgrade just yet."
|
|
294
|
+
);
|
|
295
|
+
return this.generateLegacy(params);
|
|
296
|
+
}
|
|
297
|
+
async generateLegacy(params) {
|
|
342
298
|
const processedParams = {
|
|
343
299
|
...params,
|
|
344
300
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
345
301
|
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
346
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
302
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
303
|
+
clientTools: processClientTools(params.clientTools)
|
|
347
304
|
};
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
305
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
306
|
+
const response = await this.request(
|
|
307
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
308
|
+
{
|
|
309
|
+
method: "POST",
|
|
310
|
+
body: processedParams
|
|
311
|
+
}
|
|
312
|
+
);
|
|
313
|
+
if (response.finishReason === "tool-calls") {
|
|
314
|
+
const toolCalls = response.toolCalls;
|
|
315
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
316
|
+
return response;
|
|
317
|
+
}
|
|
318
|
+
for (const toolCall of toolCalls) {
|
|
319
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
320
|
+
if (clientTool && clientTool.execute) {
|
|
321
|
+
const result = await clientTool.execute(
|
|
322
|
+
{
|
|
323
|
+
context: toolCall?.args,
|
|
324
|
+
runId,
|
|
325
|
+
resourceId,
|
|
326
|
+
threadId,
|
|
327
|
+
runtimeContext,
|
|
328
|
+
tracingContext: { currentSpan: void 0 }
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
messages: response.messages,
|
|
332
|
+
toolCallId: toolCall?.toolCallId
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
const updatedMessages = [
|
|
336
|
+
{
|
|
337
|
+
role: "user",
|
|
338
|
+
content: params.messages
|
|
339
|
+
},
|
|
340
|
+
...response.response.messages,
|
|
341
|
+
{
|
|
342
|
+
role: "tool",
|
|
343
|
+
content: [
|
|
344
|
+
{
|
|
345
|
+
type: "tool-result",
|
|
346
|
+
toolCallId: toolCall.toolCallId,
|
|
347
|
+
toolName: toolCall.toolName,
|
|
348
|
+
result
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
}
|
|
352
|
+
];
|
|
353
|
+
return this.generate({
|
|
354
|
+
...params,
|
|
355
|
+
messages: updatedMessages
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return response;
|
|
352
361
|
}
|
|
353
|
-
|
|
354
|
-
* Streams a response from the agent
|
|
355
|
-
* @param params - Stream parameters including prompt
|
|
356
|
-
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
357
|
-
*/
|
|
358
|
-
async stream(params) {
|
|
362
|
+
async generateVNext(params) {
|
|
359
363
|
const processedParams = {
|
|
360
364
|
...params,
|
|
361
365
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
362
|
-
|
|
363
|
-
|
|
366
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
367
|
+
clientTools: processClientTools(params.clientTools),
|
|
368
|
+
structuredOutput: params.structuredOutput ? {
|
|
369
|
+
...params.structuredOutput,
|
|
370
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
371
|
+
} : void 0
|
|
364
372
|
};
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
response.
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
373
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
374
|
+
const response = await this.request(
|
|
375
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
376
|
+
{
|
|
377
|
+
method: "POST",
|
|
378
|
+
body: processedParams
|
|
379
|
+
}
|
|
380
|
+
);
|
|
381
|
+
if (response.finishReason === "tool-calls") {
|
|
382
|
+
return executeToolCallAndRespond({
|
|
383
|
+
response,
|
|
384
|
+
params,
|
|
385
|
+
runId,
|
|
386
|
+
resourceId,
|
|
387
|
+
threadId,
|
|
388
|
+
runtimeContext,
|
|
389
|
+
respondFn: this.generateVNext.bind(this)
|
|
377
390
|
});
|
|
378
|
-
}
|
|
391
|
+
}
|
|
379
392
|
return response;
|
|
380
393
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
394
|
+
async processChatResponse({
|
|
395
|
+
stream,
|
|
396
|
+
update,
|
|
397
|
+
onToolCall,
|
|
398
|
+
onFinish,
|
|
399
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
400
|
+
lastMessage
|
|
401
|
+
}) {
|
|
402
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
403
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
404
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
405
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
406
|
+
}, 0) ?? 0) : 0;
|
|
407
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
408
|
+
id: uuid.v4(),
|
|
409
|
+
createdAt: getCurrentDate(),
|
|
410
|
+
role: "assistant",
|
|
411
|
+
content: "",
|
|
412
|
+
parts: []
|
|
399
413
|
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
414
|
+
let currentTextPart = void 0;
|
|
415
|
+
let currentReasoningPart = void 0;
|
|
416
|
+
let currentReasoningTextDetail = void 0;
|
|
417
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
418
|
+
const part = message.parts.find(
|
|
419
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
420
|
+
);
|
|
421
|
+
if (part != null) {
|
|
422
|
+
part.toolInvocation = invocation;
|
|
423
|
+
} else {
|
|
424
|
+
message.parts.push({
|
|
425
|
+
type: "tool-invocation",
|
|
426
|
+
toolInvocation: invocation
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const data = [];
|
|
431
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
432
|
+
const partialToolCalls = {};
|
|
433
|
+
let usage = {
|
|
434
|
+
completionTokens: NaN,
|
|
435
|
+
promptTokens: NaN,
|
|
436
|
+
totalTokens: NaN
|
|
437
|
+
};
|
|
438
|
+
let finishReason = "unknown";
|
|
439
|
+
function execUpdate() {
|
|
440
|
+
const copiedData = [...data];
|
|
441
|
+
if (messageAnnotations?.length) {
|
|
442
|
+
message.annotations = messageAnnotations;
|
|
443
|
+
}
|
|
444
|
+
const copiedMessage = {
|
|
445
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
446
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
447
|
+
...structuredClone(message),
|
|
448
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
449
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
450
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
451
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
452
|
+
// forwarded to rendering):
|
|
453
|
+
revisionId: uuid.v4()
|
|
454
|
+
};
|
|
455
|
+
update({
|
|
456
|
+
message: copiedMessage,
|
|
457
|
+
data: copiedData,
|
|
458
|
+
replaceLastMessage
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
await uiUtils.processDataStream({
|
|
462
|
+
stream,
|
|
463
|
+
onTextPart(value) {
|
|
464
|
+
if (currentTextPart == null) {
|
|
465
|
+
currentTextPart = {
|
|
466
|
+
type: "text",
|
|
467
|
+
text: value
|
|
468
|
+
};
|
|
469
|
+
message.parts.push(currentTextPart);
|
|
470
|
+
} else {
|
|
471
|
+
currentTextPart.text += value;
|
|
472
|
+
}
|
|
473
|
+
message.content += value;
|
|
474
|
+
execUpdate();
|
|
475
|
+
},
|
|
476
|
+
onReasoningPart(value) {
|
|
477
|
+
if (currentReasoningTextDetail == null) {
|
|
478
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
479
|
+
if (currentReasoningPart != null) {
|
|
480
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
481
|
+
}
|
|
482
|
+
} else {
|
|
483
|
+
currentReasoningTextDetail.text += value;
|
|
484
|
+
}
|
|
485
|
+
if (currentReasoningPart == null) {
|
|
486
|
+
currentReasoningPart = {
|
|
487
|
+
type: "reasoning",
|
|
488
|
+
reasoning: value,
|
|
489
|
+
details: [currentReasoningTextDetail]
|
|
490
|
+
};
|
|
491
|
+
message.parts.push(currentReasoningPart);
|
|
492
|
+
} else {
|
|
493
|
+
currentReasoningPart.reasoning += value;
|
|
494
|
+
}
|
|
495
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
496
|
+
execUpdate();
|
|
497
|
+
},
|
|
498
|
+
onReasoningSignaturePart(value) {
|
|
499
|
+
if (currentReasoningTextDetail != null) {
|
|
500
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
onRedactedReasoningPart(value) {
|
|
504
|
+
if (currentReasoningPart == null) {
|
|
505
|
+
currentReasoningPart = {
|
|
506
|
+
type: "reasoning",
|
|
507
|
+
reasoning: "",
|
|
508
|
+
details: []
|
|
509
|
+
};
|
|
510
|
+
message.parts.push(currentReasoningPart);
|
|
511
|
+
}
|
|
512
|
+
currentReasoningPart.details.push({
|
|
513
|
+
type: "redacted",
|
|
514
|
+
data: value.data
|
|
515
|
+
});
|
|
516
|
+
currentReasoningTextDetail = void 0;
|
|
517
|
+
execUpdate();
|
|
518
|
+
},
|
|
519
|
+
onFilePart(value) {
|
|
520
|
+
message.parts.push({
|
|
521
|
+
type: "file",
|
|
522
|
+
mimeType: value.mimeType,
|
|
523
|
+
data: value.data
|
|
524
|
+
});
|
|
525
|
+
execUpdate();
|
|
526
|
+
},
|
|
527
|
+
onSourcePart(value) {
|
|
528
|
+
message.parts.push({
|
|
529
|
+
type: "source",
|
|
530
|
+
source: value
|
|
531
|
+
});
|
|
532
|
+
execUpdate();
|
|
533
|
+
},
|
|
534
|
+
onToolCallStreamingStartPart(value) {
|
|
535
|
+
if (message.toolInvocations == null) {
|
|
536
|
+
message.toolInvocations = [];
|
|
537
|
+
}
|
|
538
|
+
partialToolCalls[value.toolCallId] = {
|
|
539
|
+
text: "",
|
|
540
|
+
step,
|
|
541
|
+
toolName: value.toolName,
|
|
542
|
+
index: message.toolInvocations.length
|
|
543
|
+
};
|
|
544
|
+
const invocation = {
|
|
545
|
+
state: "partial-call",
|
|
546
|
+
step,
|
|
547
|
+
toolCallId: value.toolCallId,
|
|
548
|
+
toolName: value.toolName,
|
|
549
|
+
args: void 0
|
|
550
|
+
};
|
|
551
|
+
message.toolInvocations.push(invocation);
|
|
552
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
553
|
+
execUpdate();
|
|
554
|
+
},
|
|
555
|
+
onToolCallDeltaPart(value) {
|
|
556
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
557
|
+
partialToolCall.text += value.argsTextDelta;
|
|
558
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
559
|
+
const invocation = {
|
|
560
|
+
state: "partial-call",
|
|
561
|
+
step: partialToolCall.step,
|
|
562
|
+
toolCallId: value.toolCallId,
|
|
563
|
+
toolName: partialToolCall.toolName,
|
|
564
|
+
args: partialArgs
|
|
565
|
+
};
|
|
566
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
567
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
568
|
+
execUpdate();
|
|
569
|
+
},
|
|
570
|
+
async onToolCallPart(value) {
|
|
571
|
+
const invocation = {
|
|
572
|
+
state: "call",
|
|
573
|
+
step,
|
|
574
|
+
...value
|
|
575
|
+
};
|
|
576
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
577
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
578
|
+
} else {
|
|
579
|
+
if (message.toolInvocations == null) {
|
|
580
|
+
message.toolInvocations = [];
|
|
581
|
+
}
|
|
582
|
+
message.toolInvocations.push(invocation);
|
|
583
|
+
}
|
|
584
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
585
|
+
execUpdate();
|
|
586
|
+
if (onToolCall) {
|
|
587
|
+
const result = await onToolCall({ toolCall: value });
|
|
588
|
+
if (result != null) {
|
|
589
|
+
const invocation2 = {
|
|
590
|
+
state: "result",
|
|
591
|
+
step,
|
|
592
|
+
...value,
|
|
593
|
+
result
|
|
594
|
+
};
|
|
595
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
596
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
597
|
+
execUpdate();
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
},
|
|
601
|
+
onToolResultPart(value) {
|
|
602
|
+
const toolInvocations = message.toolInvocations;
|
|
603
|
+
if (toolInvocations == null) {
|
|
604
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
605
|
+
}
|
|
606
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
607
|
+
if (toolInvocationIndex === -1) {
|
|
608
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
609
|
+
}
|
|
610
|
+
const invocation = {
|
|
611
|
+
...toolInvocations[toolInvocationIndex],
|
|
612
|
+
state: "result",
|
|
613
|
+
...value
|
|
614
|
+
};
|
|
615
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
616
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
617
|
+
execUpdate();
|
|
618
|
+
},
|
|
619
|
+
onDataPart(value) {
|
|
620
|
+
data.push(...value);
|
|
621
|
+
execUpdate();
|
|
622
|
+
},
|
|
623
|
+
onMessageAnnotationsPart(value) {
|
|
624
|
+
if (messageAnnotations == null) {
|
|
625
|
+
messageAnnotations = [...value];
|
|
626
|
+
} else {
|
|
627
|
+
messageAnnotations.push(...value);
|
|
628
|
+
}
|
|
629
|
+
execUpdate();
|
|
630
|
+
},
|
|
631
|
+
onFinishStepPart(value) {
|
|
632
|
+
step += 1;
|
|
633
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
634
|
+
currentReasoningPart = void 0;
|
|
635
|
+
currentReasoningTextDetail = void 0;
|
|
636
|
+
},
|
|
637
|
+
onStartStepPart(value) {
|
|
638
|
+
if (!replaceLastMessage) {
|
|
639
|
+
message.id = value.messageId;
|
|
640
|
+
}
|
|
641
|
+
message.parts.push({ type: "step-start" });
|
|
642
|
+
execUpdate();
|
|
643
|
+
},
|
|
644
|
+
onFinishMessagePart(value) {
|
|
645
|
+
finishReason = value.finishReason;
|
|
646
|
+
if (value.usage != null) {
|
|
647
|
+
usage = value.usage;
|
|
648
|
+
}
|
|
649
|
+
},
|
|
650
|
+
onErrorPart(error) {
|
|
651
|
+
throw new Error(error);
|
|
652
|
+
}
|
|
403
653
|
});
|
|
654
|
+
onFinish?.({ message, finishReason, usage });
|
|
404
655
|
}
|
|
405
656
|
/**
|
|
406
|
-
*
|
|
407
|
-
* @
|
|
408
|
-
|
|
409
|
-
evals() {
|
|
410
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* Retrieves live evaluation results for the agent
|
|
414
|
-
* @returns Promise containing live agent evaluations
|
|
415
|
-
*/
|
|
416
|
-
liveEvals() {
|
|
417
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
var Network = class extends BaseResource {
|
|
421
|
-
constructor(options, networkId) {
|
|
422
|
-
super(options);
|
|
423
|
-
this.networkId = networkId;
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* Retrieves details about the network
|
|
427
|
-
* @returns Promise containing network details
|
|
428
|
-
*/
|
|
429
|
-
details() {
|
|
430
|
-
return this.request(`/api/networks/${this.networkId}`);
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Generates a response from the agent
|
|
434
|
-
* @param params - Generation parameters including prompt
|
|
435
|
-
* @returns Promise containing the generated response
|
|
657
|
+
* Streams a response from the agent
|
|
658
|
+
* @param params - Stream parameters including prompt
|
|
659
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
436
660
|
*/
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
};
|
|
443
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
444
|
-
method: "POST",
|
|
445
|
-
body: processedParams
|
|
446
|
-
});
|
|
661
|
+
async stream(params) {
|
|
662
|
+
console.warn(
|
|
663
|
+
"Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 16th. Please use streamLegacy if you don't want to upgrade just yet."
|
|
664
|
+
);
|
|
665
|
+
return this.streamLegacy(params);
|
|
447
666
|
}
|
|
448
667
|
/**
|
|
449
668
|
* Streams a response from the agent
|
|
450
669
|
* @param params - Stream parameters including prompt
|
|
451
670
|
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
452
671
|
*/
|
|
453
|
-
async
|
|
672
|
+
async streamLegacy(params) {
|
|
454
673
|
const processedParams = {
|
|
455
674
|
...params,
|
|
456
|
-
output: zodToJsonSchema(params.output),
|
|
457
|
-
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
675
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
676
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
677
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
678
|
+
clientTools: processClientTools(params.clientTools)
|
|
679
|
+
};
|
|
680
|
+
const { readable, writable } = new TransformStream();
|
|
681
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
682
|
+
const streamResponse = new Response(readable, {
|
|
683
|
+
status: response.status,
|
|
684
|
+
statusText: response.statusText,
|
|
685
|
+
headers: response.headers
|
|
686
|
+
});
|
|
687
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
688
|
+
await uiUtils.processDataStream({
|
|
689
|
+
stream: streamResponse.body,
|
|
690
|
+
...options
|
|
691
|
+
});
|
|
692
|
+
};
|
|
693
|
+
return streamResponse;
|
|
694
|
+
}
|
|
695
|
+
async processChatResponse_vNext({
|
|
696
|
+
stream,
|
|
697
|
+
update,
|
|
698
|
+
onToolCall,
|
|
699
|
+
onFinish,
|
|
700
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
701
|
+
lastMessage
|
|
702
|
+
}) {
|
|
703
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
704
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
705
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
706
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
707
|
+
}, 0) ?? 0) : 0;
|
|
708
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
709
|
+
id: uuid.v4(),
|
|
710
|
+
createdAt: getCurrentDate(),
|
|
711
|
+
role: "assistant",
|
|
712
|
+
content: "",
|
|
713
|
+
parts: []
|
|
714
|
+
};
|
|
715
|
+
let currentTextPart = void 0;
|
|
716
|
+
let currentReasoningPart = void 0;
|
|
717
|
+
let currentReasoningTextDetail = void 0;
|
|
718
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
719
|
+
const part = message.parts.find(
|
|
720
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
721
|
+
);
|
|
722
|
+
if (part != null) {
|
|
723
|
+
part.toolInvocation = invocation;
|
|
724
|
+
} else {
|
|
725
|
+
message.parts.push({
|
|
726
|
+
type: "tool-invocation",
|
|
727
|
+
toolInvocation: invocation
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const data = [];
|
|
732
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
733
|
+
const partialToolCalls = {};
|
|
734
|
+
let usage = {
|
|
735
|
+
completionTokens: NaN,
|
|
736
|
+
promptTokens: NaN,
|
|
737
|
+
totalTokens: NaN
|
|
738
|
+
};
|
|
739
|
+
let finishReason = "unknown";
|
|
740
|
+
function execUpdate() {
|
|
741
|
+
const copiedData = [...data];
|
|
742
|
+
if (messageAnnotations?.length) {
|
|
743
|
+
message.annotations = messageAnnotations;
|
|
744
|
+
}
|
|
745
|
+
const copiedMessage = {
|
|
746
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
747
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
748
|
+
...structuredClone(message),
|
|
749
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
750
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
751
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
752
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
753
|
+
// forwarded to rendering):
|
|
754
|
+
revisionId: uuid.v4()
|
|
755
|
+
};
|
|
756
|
+
update({
|
|
757
|
+
message: copiedMessage,
|
|
758
|
+
data: copiedData,
|
|
759
|
+
replaceLastMessage
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
await processMastraStream({
|
|
763
|
+
stream,
|
|
764
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
765
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
766
|
+
onChunk: async (chunk) => {
|
|
767
|
+
switch (chunk.type) {
|
|
768
|
+
case "step-start": {
|
|
769
|
+
if (!replaceLastMessage) {
|
|
770
|
+
message.id = chunk.payload.messageId;
|
|
771
|
+
}
|
|
772
|
+
message.parts.push({ type: "step-start" });
|
|
773
|
+
execUpdate();
|
|
774
|
+
break;
|
|
775
|
+
}
|
|
776
|
+
case "text-delta": {
|
|
777
|
+
if (currentTextPart == null) {
|
|
778
|
+
currentTextPart = {
|
|
779
|
+
type: "text",
|
|
780
|
+
text: chunk.payload.text
|
|
781
|
+
};
|
|
782
|
+
message.parts.push(currentTextPart);
|
|
783
|
+
} else {
|
|
784
|
+
currentTextPart.text += chunk.payload.text;
|
|
785
|
+
}
|
|
786
|
+
message.content += chunk.payload.text;
|
|
787
|
+
execUpdate();
|
|
788
|
+
break;
|
|
789
|
+
}
|
|
790
|
+
case "reasoning-delta": {
|
|
791
|
+
if (currentReasoningTextDetail == null) {
|
|
792
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
793
|
+
if (currentReasoningPart != null) {
|
|
794
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
795
|
+
}
|
|
796
|
+
} else {
|
|
797
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
798
|
+
}
|
|
799
|
+
if (currentReasoningPart == null) {
|
|
800
|
+
currentReasoningPart = {
|
|
801
|
+
type: "reasoning",
|
|
802
|
+
reasoning: chunk.payload.text,
|
|
803
|
+
details: [currentReasoningTextDetail]
|
|
804
|
+
};
|
|
805
|
+
message.parts.push(currentReasoningPart);
|
|
806
|
+
} else {
|
|
807
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
808
|
+
}
|
|
809
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
810
|
+
execUpdate();
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
case "file": {
|
|
814
|
+
message.parts.push({
|
|
815
|
+
type: "file",
|
|
816
|
+
mimeType: chunk.payload.mimeType,
|
|
817
|
+
data: chunk.payload.data
|
|
818
|
+
});
|
|
819
|
+
execUpdate();
|
|
820
|
+
break;
|
|
821
|
+
}
|
|
822
|
+
case "source": {
|
|
823
|
+
message.parts.push({
|
|
824
|
+
type: "source",
|
|
825
|
+
source: chunk.payload.source
|
|
826
|
+
});
|
|
827
|
+
execUpdate();
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
case "tool-call": {
|
|
831
|
+
const invocation = {
|
|
832
|
+
state: "call",
|
|
833
|
+
step,
|
|
834
|
+
...chunk.payload
|
|
835
|
+
};
|
|
836
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
837
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
838
|
+
} else {
|
|
839
|
+
if (message.toolInvocations == null) {
|
|
840
|
+
message.toolInvocations = [];
|
|
841
|
+
}
|
|
842
|
+
message.toolInvocations.push(invocation);
|
|
843
|
+
}
|
|
844
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
845
|
+
execUpdate();
|
|
846
|
+
if (onToolCall) {
|
|
847
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
848
|
+
if (result != null) {
|
|
849
|
+
const invocation2 = {
|
|
850
|
+
state: "result",
|
|
851
|
+
step,
|
|
852
|
+
...chunk.payload,
|
|
853
|
+
result
|
|
854
|
+
};
|
|
855
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
856
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
857
|
+
execUpdate();
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
case "tool-call-input-streaming-start": {
|
|
862
|
+
if (message.toolInvocations == null) {
|
|
863
|
+
message.toolInvocations = [];
|
|
864
|
+
}
|
|
865
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
866
|
+
text: "",
|
|
867
|
+
step,
|
|
868
|
+
toolName: chunk.payload.toolName,
|
|
869
|
+
index: message.toolInvocations.length
|
|
870
|
+
};
|
|
871
|
+
const invocation = {
|
|
872
|
+
state: "partial-call",
|
|
873
|
+
step,
|
|
874
|
+
toolCallId: chunk.payload.toolCallId,
|
|
875
|
+
toolName: chunk.payload.toolName,
|
|
876
|
+
args: chunk.payload.args
|
|
877
|
+
};
|
|
878
|
+
message.toolInvocations.push(invocation);
|
|
879
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
880
|
+
execUpdate();
|
|
881
|
+
break;
|
|
882
|
+
}
|
|
883
|
+
case "tool-call-delta": {
|
|
884
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
885
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
886
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
887
|
+
const invocation = {
|
|
888
|
+
state: "partial-call",
|
|
889
|
+
step: partialToolCall.step,
|
|
890
|
+
toolCallId: chunk.payload.toolCallId,
|
|
891
|
+
toolName: partialToolCall.toolName,
|
|
892
|
+
args: partialArgs
|
|
893
|
+
};
|
|
894
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
895
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
896
|
+
execUpdate();
|
|
897
|
+
break;
|
|
898
|
+
}
|
|
899
|
+
case "tool-result": {
|
|
900
|
+
const toolInvocations = message.toolInvocations;
|
|
901
|
+
if (toolInvocations == null) {
|
|
902
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
903
|
+
}
|
|
904
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
905
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
906
|
+
);
|
|
907
|
+
if (toolInvocationIndex === -1) {
|
|
908
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
909
|
+
}
|
|
910
|
+
const invocation = {
|
|
911
|
+
...toolInvocations[toolInvocationIndex],
|
|
912
|
+
state: "result",
|
|
913
|
+
...chunk.payload
|
|
914
|
+
};
|
|
915
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
916
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
917
|
+
execUpdate();
|
|
918
|
+
break;
|
|
919
|
+
}
|
|
920
|
+
case "error": {
|
|
921
|
+
throw new Error(chunk.payload.error);
|
|
922
|
+
}
|
|
923
|
+
case "data": {
|
|
924
|
+
data.push(...chunk.payload.data);
|
|
925
|
+
execUpdate();
|
|
926
|
+
break;
|
|
927
|
+
}
|
|
928
|
+
case "step-finish": {
|
|
929
|
+
step += 1;
|
|
930
|
+
currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
|
|
931
|
+
currentReasoningPart = void 0;
|
|
932
|
+
currentReasoningTextDetail = void 0;
|
|
933
|
+
execUpdate();
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
case "finish": {
|
|
937
|
+
finishReason = chunk.payload.stepResult.reason;
|
|
938
|
+
if (chunk.payload.usage != null) {
|
|
939
|
+
usage = chunk.payload.usage;
|
|
940
|
+
}
|
|
941
|
+
break;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
onFinish?.({ message, finishReason, usage });
|
|
947
|
+
}
|
|
948
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
949
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
950
|
+
method: "POST",
|
|
951
|
+
body: processedParams,
|
|
952
|
+
stream: true
|
|
953
|
+
});
|
|
954
|
+
if (!response.body) {
|
|
955
|
+
throw new Error("No response body");
|
|
956
|
+
}
|
|
957
|
+
try {
|
|
958
|
+
let toolCalls = [];
|
|
959
|
+
let messages = [];
|
|
960
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
961
|
+
streamForWritable.pipeTo(
|
|
962
|
+
new WritableStream({
|
|
963
|
+
async write(chunk) {
|
|
964
|
+
try {
|
|
965
|
+
const text = new TextDecoder().decode(chunk);
|
|
966
|
+
if (text.includes("[DONE]")) {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
} catch {
|
|
970
|
+
}
|
|
971
|
+
const writer = writable.getWriter();
|
|
972
|
+
try {
|
|
973
|
+
await writer.write(chunk);
|
|
974
|
+
} finally {
|
|
975
|
+
writer.releaseLock();
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
}),
|
|
979
|
+
{
|
|
980
|
+
preventClose: true
|
|
981
|
+
}
|
|
982
|
+
).catch((error) => {
|
|
983
|
+
console.error("Error piping to writable stream:", error);
|
|
984
|
+
});
|
|
985
|
+
this.processChatResponse_vNext({
|
|
986
|
+
stream: streamForProcessing,
|
|
987
|
+
update: ({ message }) => {
|
|
988
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
989
|
+
if (existingIndex !== -1) {
|
|
990
|
+
messages[existingIndex] = message;
|
|
991
|
+
} else {
|
|
992
|
+
messages.push(message);
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
onFinish: async ({ finishReason, message }) => {
|
|
996
|
+
if (finishReason === "tool-calls") {
|
|
997
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
998
|
+
if (toolCall) {
|
|
999
|
+
toolCalls.push(toolCall);
|
|
1000
|
+
}
|
|
1001
|
+
for (const toolCall2 of toolCalls) {
|
|
1002
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1003
|
+
if (clientTool && clientTool.execute) {
|
|
1004
|
+
const result = await clientTool.execute(
|
|
1005
|
+
{
|
|
1006
|
+
context: toolCall2?.args,
|
|
1007
|
+
runId: processedParams.runId,
|
|
1008
|
+
resourceId: processedParams.resourceId,
|
|
1009
|
+
threadId: processedParams.threadId,
|
|
1010
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1011
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1012
|
+
tracingContext: { currentSpan: void 0 }
|
|
1013
|
+
},
|
|
1014
|
+
{
|
|
1015
|
+
messages: response.messages,
|
|
1016
|
+
toolCallId: toolCall2?.toolCallId
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
const lastMessageRaw = messages[messages.length - 1];
|
|
1020
|
+
const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
|
|
1021
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1022
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1023
|
+
);
|
|
1024
|
+
if (toolInvocationPart) {
|
|
1025
|
+
toolInvocationPart.toolInvocation = {
|
|
1026
|
+
...toolInvocationPart.toolInvocation,
|
|
1027
|
+
state: "result",
|
|
1028
|
+
result
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1032
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1033
|
+
);
|
|
1034
|
+
if (toolInvocation) {
|
|
1035
|
+
toolInvocation.state = "result";
|
|
1036
|
+
toolInvocation.result = result;
|
|
1037
|
+
}
|
|
1038
|
+
const originalMessages = processedParams.messages;
|
|
1039
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1040
|
+
const updatedMessages = lastMessage != null ? [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messageArray, ...messages];
|
|
1041
|
+
this.processStreamResponse_vNext(
|
|
1042
|
+
{
|
|
1043
|
+
...processedParams,
|
|
1044
|
+
messages: updatedMessages
|
|
1045
|
+
},
|
|
1046
|
+
writable
|
|
1047
|
+
).catch((error) => {
|
|
1048
|
+
console.error("Error processing stream response:", error);
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
} else {
|
|
1053
|
+
setTimeout(() => {
|
|
1054
|
+
writable.close();
|
|
1055
|
+
}, 0);
|
|
1056
|
+
}
|
|
1057
|
+
},
|
|
1058
|
+
lastMessage: void 0
|
|
1059
|
+
}).catch((error) => {
|
|
1060
|
+
console.error("Error processing stream response:", error);
|
|
1061
|
+
});
|
|
1062
|
+
} catch (error) {
|
|
1063
|
+
console.error("Error processing stream response:", error);
|
|
1064
|
+
}
|
|
1065
|
+
return response;
|
|
1066
|
+
}
|
|
1067
|
+
async streamVNext(params) {
|
|
1068
|
+
const processedParams = {
|
|
1069
|
+
...params,
|
|
1070
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
1071
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
1072
|
+
clientTools: processClientTools(params.clientTools),
|
|
1073
|
+
structuredOutput: params.structuredOutput ? {
|
|
1074
|
+
...params.structuredOutput,
|
|
1075
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
1076
|
+
} : void 0
|
|
1077
|
+
};
|
|
1078
|
+
const { readable, writable } = new TransformStream();
|
|
1079
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
1080
|
+
const streamResponse = new Response(readable, {
|
|
1081
|
+
status: response.status,
|
|
1082
|
+
statusText: response.statusText,
|
|
1083
|
+
headers: response.headers
|
|
1084
|
+
});
|
|
1085
|
+
streamResponse.processDataStream = async ({
|
|
1086
|
+
onChunk
|
|
1087
|
+
}) => {
|
|
1088
|
+
await processMastraStream({
|
|
1089
|
+
stream: streamResponse.body,
|
|
1090
|
+
onChunk
|
|
1091
|
+
});
|
|
1092
|
+
};
|
|
1093
|
+
return streamResponse;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Processes the stream response and handles tool calls
|
|
1097
|
+
*/
|
|
1098
|
+
async processStreamResponse(processedParams, writable) {
|
|
1099
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
1100
|
+
method: "POST",
|
|
1101
|
+
body: processedParams,
|
|
1102
|
+
stream: true
|
|
1103
|
+
});
|
|
1104
|
+
if (!response.body) {
|
|
1105
|
+
throw new Error("No response body");
|
|
1106
|
+
}
|
|
1107
|
+
try {
|
|
1108
|
+
let toolCalls = [];
|
|
1109
|
+
let messages = [];
|
|
1110
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1111
|
+
streamForWritable.pipeTo(writable, {
|
|
1112
|
+
preventClose: true
|
|
1113
|
+
}).catch((error) => {
|
|
1114
|
+
console.error("Error piping to writable stream:", error);
|
|
1115
|
+
});
|
|
1116
|
+
this.processChatResponse({
|
|
1117
|
+
stream: streamForProcessing,
|
|
1118
|
+
update: ({ message }) => {
|
|
1119
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1120
|
+
if (existingIndex !== -1) {
|
|
1121
|
+
messages[existingIndex] = message;
|
|
1122
|
+
} else {
|
|
1123
|
+
messages.push(message);
|
|
1124
|
+
}
|
|
1125
|
+
},
|
|
1126
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1127
|
+
if (finishReason === "tool-calls") {
|
|
1128
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1129
|
+
if (toolCall) {
|
|
1130
|
+
toolCalls.push(toolCall);
|
|
1131
|
+
}
|
|
1132
|
+
for (const toolCall2 of toolCalls) {
|
|
1133
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1134
|
+
if (clientTool && clientTool.execute) {
|
|
1135
|
+
const result = await clientTool.execute(
|
|
1136
|
+
{
|
|
1137
|
+
context: toolCall2?.args,
|
|
1138
|
+
runId: processedParams.runId,
|
|
1139
|
+
resourceId: processedParams.resourceId,
|
|
1140
|
+
threadId: processedParams.threadId,
|
|
1141
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1142
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1143
|
+
tracingContext: { currentSpan: void 0 }
|
|
1144
|
+
},
|
|
1145
|
+
{
|
|
1146
|
+
messages: response.messages,
|
|
1147
|
+
toolCallId: toolCall2?.toolCallId
|
|
1148
|
+
}
|
|
1149
|
+
);
|
|
1150
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1151
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1152
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1153
|
+
);
|
|
1154
|
+
if (toolInvocationPart) {
|
|
1155
|
+
toolInvocationPart.toolInvocation = {
|
|
1156
|
+
...toolInvocationPart.toolInvocation,
|
|
1157
|
+
state: "result",
|
|
1158
|
+
result
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1162
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1163
|
+
);
|
|
1164
|
+
if (toolInvocation) {
|
|
1165
|
+
toolInvocation.state = "result";
|
|
1166
|
+
toolInvocation.result = result;
|
|
1167
|
+
}
|
|
1168
|
+
const writer = writable.getWriter();
|
|
1169
|
+
try {
|
|
1170
|
+
await writer.write(
|
|
1171
|
+
new TextEncoder().encode(
|
|
1172
|
+
"a:" + JSON.stringify({
|
|
1173
|
+
toolCallId: toolCall2.toolCallId,
|
|
1174
|
+
result
|
|
1175
|
+
}) + "\n"
|
|
1176
|
+
)
|
|
1177
|
+
);
|
|
1178
|
+
} finally {
|
|
1179
|
+
writer.releaseLock();
|
|
1180
|
+
}
|
|
1181
|
+
const originalMessages = processedParams.messages;
|
|
1182
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1183
|
+
this.processStreamResponse(
|
|
1184
|
+
{
|
|
1185
|
+
...processedParams,
|
|
1186
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1187
|
+
},
|
|
1188
|
+
writable
|
|
1189
|
+
).catch((error) => {
|
|
1190
|
+
console.error("Error processing stream response:", error);
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
} else {
|
|
1195
|
+
setTimeout(() => {
|
|
1196
|
+
writable.close();
|
|
1197
|
+
}, 0);
|
|
1198
|
+
}
|
|
1199
|
+
},
|
|
1200
|
+
lastMessage: void 0
|
|
1201
|
+
}).catch((error) => {
|
|
1202
|
+
console.error("Error processing stream response:", error);
|
|
1203
|
+
});
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
console.error("Error processing stream response:", error);
|
|
1206
|
+
}
|
|
1207
|
+
return response;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Gets details about a specific tool available to the agent
|
|
1211
|
+
* @param toolId - ID of the tool to retrieve
|
|
1212
|
+
* @returns Promise containing tool details
|
|
1213
|
+
*/
|
|
1214
|
+
getTool(toolId) {
|
|
1215
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Executes a tool for the agent
|
|
1219
|
+
* @param toolId - ID of the tool to execute
|
|
1220
|
+
* @param params - Parameters required for tool execution
|
|
1221
|
+
* @returns Promise containing the tool execution results
|
|
1222
|
+
*/
|
|
1223
|
+
executeTool(toolId, params) {
|
|
1224
|
+
const body = {
|
|
1225
|
+
data: params.data,
|
|
1226
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
1227
|
+
};
|
|
1228
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
1229
|
+
method: "POST",
|
|
1230
|
+
body
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Retrieves evaluation results for the agent
|
|
1235
|
+
* @returns Promise containing agent evaluations
|
|
1236
|
+
*/
|
|
1237
|
+
evals() {
|
|
1238
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Retrieves live evaluation results for the agent
|
|
1242
|
+
* @returns Promise containing live agent evaluations
|
|
1243
|
+
*/
|
|
1244
|
+
liveEvals() {
|
|
1245
|
+
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Updates the model for the agent
|
|
1249
|
+
* @param params - Parameters for updating the model
|
|
1250
|
+
* @returns Promise containing the updated model
|
|
1251
|
+
*/
|
|
1252
|
+
updateModel(params) {
|
|
1253
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1254
|
+
method: "POST",
|
|
1255
|
+
body: params
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
var Network = class extends BaseResource {
|
|
1260
|
+
constructor(options, networkId) {
|
|
1261
|
+
super(options);
|
|
1262
|
+
this.networkId = networkId;
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Retrieves details about the network
|
|
1266
|
+
* @returns Promise containing network details
|
|
1267
|
+
*/
|
|
1268
|
+
details() {
|
|
1269
|
+
return this.request(`/api/networks/${this.networkId}`);
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Generates a response from the agent
|
|
1273
|
+
* @param params - Generation parameters including prompt
|
|
1274
|
+
* @returns Promise containing the generated response
|
|
1275
|
+
*/
|
|
1276
|
+
generate(params) {
|
|
1277
|
+
const processedParams = {
|
|
1278
|
+
...params,
|
|
1279
|
+
output: zodToJsonSchema(params.output),
|
|
1280
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
1281
|
+
};
|
|
1282
|
+
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
1283
|
+
method: "POST",
|
|
1284
|
+
body: processedParams
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
/**
|
|
1288
|
+
* Streams a response from the agent
|
|
1289
|
+
* @param params - Stream parameters including prompt
|
|
1290
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
1291
|
+
*/
|
|
1292
|
+
async stream(params) {
|
|
1293
|
+
const processedParams = {
|
|
1294
|
+
...params,
|
|
1295
|
+
output: zodToJsonSchema(params.output),
|
|
1296
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
458
1297
|
};
|
|
459
1298
|
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
460
1299
|
method: "POST",
|
|
@@ -520,6 +1359,36 @@ var MemoryThread = class extends BaseResource {
|
|
|
520
1359
|
});
|
|
521
1360
|
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
522
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
1364
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
1365
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
1366
|
+
*/
|
|
1367
|
+
getMessagesPaginated({
|
|
1368
|
+
selectBy,
|
|
1369
|
+
...rest
|
|
1370
|
+
}) {
|
|
1371
|
+
const query = new URLSearchParams({
|
|
1372
|
+
...rest,
|
|
1373
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
1374
|
+
});
|
|
1375
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Deletes one or more messages from the thread
|
|
1379
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1380
|
+
* message object with id property, or array of message objects
|
|
1381
|
+
* @returns Promise containing deletion result
|
|
1382
|
+
*/
|
|
1383
|
+
deleteMessages(messageIds) {
|
|
1384
|
+
const query = new URLSearchParams({
|
|
1385
|
+
agentId: this.agentId
|
|
1386
|
+
});
|
|
1387
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1388
|
+
method: "POST",
|
|
1389
|
+
body: { messageIds }
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
523
1392
|
};
|
|
524
1393
|
|
|
525
1394
|
// src/resources/vector.ts
|
|
@@ -875,10 +1744,10 @@ var Workflow = class extends BaseResource {
|
|
|
875
1744
|
if (params?.toDate) {
|
|
876
1745
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
877
1746
|
}
|
|
878
|
-
if (params?.limit) {
|
|
1747
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
879
1748
|
searchParams.set("limit", String(params.limit));
|
|
880
1749
|
}
|
|
881
|
-
if (params?.offset) {
|
|
1750
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
882
1751
|
searchParams.set("offset", String(params.offset));
|
|
883
1752
|
}
|
|
884
1753
|
if (params?.resourceId) {
|
|
@@ -890,6 +1759,43 @@ var Workflow = class extends BaseResource {
|
|
|
890
1759
|
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
891
1760
|
}
|
|
892
1761
|
}
|
|
1762
|
+
/**
|
|
1763
|
+
* Retrieves a specific workflow run by its ID
|
|
1764
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1765
|
+
* @returns Promise containing the workflow run details
|
|
1766
|
+
*/
|
|
1767
|
+
runById(runId) {
|
|
1768
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1772
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1773
|
+
* @returns Promise containing the workflow run execution result
|
|
1774
|
+
*/
|
|
1775
|
+
runExecutionResult(runId) {
|
|
1776
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1777
|
+
}
|
|
1778
|
+
/**
|
|
1779
|
+
* Cancels a specific workflow run by its ID
|
|
1780
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1781
|
+
* @returns Promise containing a success message
|
|
1782
|
+
*/
|
|
1783
|
+
cancelRun(runId) {
|
|
1784
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1785
|
+
method: "POST"
|
|
1786
|
+
});
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Sends an event to a specific workflow run by its ID
|
|
1790
|
+
* @param params - Object containing the runId, event and data
|
|
1791
|
+
* @returns Promise containing a success message
|
|
1792
|
+
*/
|
|
1793
|
+
sendRunEvent(params) {
|
|
1794
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1795
|
+
method: "POST",
|
|
1796
|
+
body: { event: params.event, data: params.data }
|
|
1797
|
+
});
|
|
1798
|
+
}
|
|
893
1799
|
/**
|
|
894
1800
|
* Creates a new workflow run
|
|
895
1801
|
* @param params - Optional object containing the optional runId
|
|
@@ -904,6 +1810,14 @@ var Workflow = class extends BaseResource {
|
|
|
904
1810
|
method: "POST"
|
|
905
1811
|
});
|
|
906
1812
|
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Creates a new workflow run (alias for createRun)
|
|
1815
|
+
* @param params - Optional object containing the optional runId
|
|
1816
|
+
* @returns Promise containing the runId of the created run
|
|
1817
|
+
*/
|
|
1818
|
+
createRunAsync(params) {
|
|
1819
|
+
return this.createRun(params);
|
|
1820
|
+
}
|
|
907
1821
|
/**
|
|
908
1822
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
909
1823
|
* @param params - Object containing the runId, inputData and runtimeContext
|
|
@@ -955,16 +1869,16 @@ var Workflow = class extends BaseResource {
|
|
|
955
1869
|
});
|
|
956
1870
|
}
|
|
957
1871
|
/**
|
|
958
|
-
* Starts a
|
|
1872
|
+
* Starts a workflow run and returns a stream
|
|
959
1873
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
960
|
-
* @returns Promise containing the
|
|
1874
|
+
* @returns Promise containing the workflow execution results
|
|
961
1875
|
*/
|
|
962
1876
|
async stream(params) {
|
|
963
1877
|
const searchParams = new URLSearchParams();
|
|
964
1878
|
if (!!params?.runId) {
|
|
965
1879
|
searchParams.set("runId", params.runId);
|
|
966
1880
|
}
|
|
967
|
-
const runtimeContext =
|
|
1881
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
968
1882
|
const response = await this.request(
|
|
969
1883
|
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
970
1884
|
{
|
|
@@ -979,6 +1893,7 @@ var Workflow = class extends BaseResource {
|
|
|
979
1893
|
if (!response.body) {
|
|
980
1894
|
throw new Error("Response body is null");
|
|
981
1895
|
}
|
|
1896
|
+
let failedChunk = void 0;
|
|
982
1897
|
const transformStream = new TransformStream({
|
|
983
1898
|
start() {
|
|
984
1899
|
},
|
|
@@ -988,10 +1903,13 @@ var Workflow = class extends BaseResource {
|
|
|
988
1903
|
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
989
1904
|
for (const chunk2 of chunks) {
|
|
990
1905
|
if (chunk2) {
|
|
1906
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
991
1907
|
try {
|
|
992
|
-
const parsedChunk = JSON.parse(
|
|
1908
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
993
1909
|
controller.enqueue(parsedChunk);
|
|
1910
|
+
failedChunk = void 0;
|
|
994
1911
|
} catch {
|
|
1912
|
+
failedChunk = newChunk;
|
|
995
1913
|
}
|
|
996
1914
|
}
|
|
997
1915
|
}
|
|
@@ -1002,19 +1920,70 @@ var Workflow = class extends BaseResource {
|
|
|
1002
1920
|
return response.body.pipeThrough(transformStream);
|
|
1003
1921
|
}
|
|
1004
1922
|
/**
|
|
1005
|
-
*
|
|
1006
|
-
* @param params - Object containing the runId,
|
|
1007
|
-
* @returns Promise containing the workflow
|
|
1923
|
+
* Starts a workflow run and returns a stream
|
|
1924
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1925
|
+
* @returns Promise containing the workflow execution results
|
|
1008
1926
|
*/
|
|
1009
|
-
|
|
1927
|
+
async streamVNext(params) {
|
|
1928
|
+
const searchParams = new URLSearchParams();
|
|
1929
|
+
if (!!params?.runId) {
|
|
1930
|
+
searchParams.set("runId", params.runId);
|
|
1931
|
+
}
|
|
1010
1932
|
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
}
|
|
1933
|
+
const response = await this.request(
|
|
1934
|
+
`/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
|
|
1935
|
+
{
|
|
1936
|
+
method: "POST",
|
|
1937
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1938
|
+
stream: true
|
|
1939
|
+
}
|
|
1940
|
+
);
|
|
1941
|
+
if (!response.ok) {
|
|
1942
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1943
|
+
}
|
|
1944
|
+
if (!response.body) {
|
|
1945
|
+
throw new Error("Response body is null");
|
|
1946
|
+
}
|
|
1947
|
+
let failedChunk = void 0;
|
|
1948
|
+
const transformStream = new TransformStream({
|
|
1949
|
+
start() {
|
|
1950
|
+
},
|
|
1951
|
+
async transform(chunk, controller) {
|
|
1952
|
+
try {
|
|
1953
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1954
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1955
|
+
for (const chunk2 of chunks) {
|
|
1956
|
+
if (chunk2) {
|
|
1957
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1958
|
+
try {
|
|
1959
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1960
|
+
controller.enqueue(parsedChunk);
|
|
1961
|
+
failedChunk = void 0;
|
|
1962
|
+
} catch {
|
|
1963
|
+
failedChunk = newChunk;
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
} catch {
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
});
|
|
1971
|
+
return response.body.pipeThrough(transformStream);
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1975
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1976
|
+
* @returns Promise containing the workflow resume results
|
|
1977
|
+
*/
|
|
1978
|
+
resumeAsync(params) {
|
|
1979
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1980
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1981
|
+
method: "POST",
|
|
1982
|
+
body: {
|
|
1983
|
+
step: params.step,
|
|
1984
|
+
resumeData: params.resumeData,
|
|
1985
|
+
runtimeContext
|
|
1986
|
+
}
|
|
1018
1987
|
});
|
|
1019
1988
|
}
|
|
1020
1989
|
/**
|
|
@@ -1076,22 +2045,38 @@ var A2A = class extends BaseResource {
|
|
|
1076
2045
|
* @returns Promise containing the agent card information
|
|
1077
2046
|
*/
|
|
1078
2047
|
async getCard() {
|
|
1079
|
-
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
2048
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
1080
2049
|
}
|
|
1081
2050
|
/**
|
|
1082
|
-
* Send a message to the agent and
|
|
2051
|
+
* Send a message to the agent and gets a message or task response
|
|
1083
2052
|
* @param params - Parameters for the task
|
|
1084
|
-
* @returns Promise containing the
|
|
2053
|
+
* @returns Promise containing the response
|
|
1085
2054
|
*/
|
|
1086
2055
|
async sendMessage(params) {
|
|
1087
2056
|
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1088
2057
|
method: "POST",
|
|
1089
2058
|
body: {
|
|
1090
|
-
method: "
|
|
2059
|
+
method: "message/send",
|
|
2060
|
+
params
|
|
2061
|
+
}
|
|
2062
|
+
});
|
|
2063
|
+
return response;
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2067
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2068
|
+
* @param params - Parameters for the task
|
|
2069
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2070
|
+
*/
|
|
2071
|
+
async sendStreamingMessage(params) {
|
|
2072
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2073
|
+
method: "POST",
|
|
2074
|
+
body: {
|
|
2075
|
+
method: "message/stream",
|
|
1091
2076
|
params
|
|
1092
2077
|
}
|
|
1093
2078
|
});
|
|
1094
|
-
return
|
|
2079
|
+
return response;
|
|
1095
2080
|
}
|
|
1096
2081
|
/**
|
|
1097
2082
|
* Get the status and result of a task
|
|
@@ -1102,81 +2087,688 @@ var A2A = class extends BaseResource {
|
|
|
1102
2087
|
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1103
2088
|
method: "POST",
|
|
1104
2089
|
body: {
|
|
1105
|
-
method: "tasks/get",
|
|
1106
|
-
params
|
|
2090
|
+
method: "tasks/get",
|
|
2091
|
+
params
|
|
2092
|
+
}
|
|
2093
|
+
});
|
|
2094
|
+
return response;
|
|
2095
|
+
}
|
|
2096
|
+
/**
|
|
2097
|
+
* Cancel a running task
|
|
2098
|
+
* @param params - Parameters identifying the task to cancel
|
|
2099
|
+
* @returns Promise containing the task response
|
|
2100
|
+
*/
|
|
2101
|
+
async cancelTask(params) {
|
|
2102
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2103
|
+
method: "POST",
|
|
2104
|
+
body: {
|
|
2105
|
+
method: "tasks/cancel",
|
|
2106
|
+
params
|
|
2107
|
+
}
|
|
2108
|
+
});
|
|
2109
|
+
}
|
|
2110
|
+
};
|
|
2111
|
+
|
|
2112
|
+
// src/resources/mcp-tool.ts
|
|
2113
|
+
var MCPTool = class extends BaseResource {
|
|
2114
|
+
serverId;
|
|
2115
|
+
toolId;
|
|
2116
|
+
constructor(options, serverId, toolId) {
|
|
2117
|
+
super(options);
|
|
2118
|
+
this.serverId = serverId;
|
|
2119
|
+
this.toolId = toolId;
|
|
2120
|
+
}
|
|
2121
|
+
/**
|
|
2122
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2123
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2124
|
+
*/
|
|
2125
|
+
details() {
|
|
2126
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* Executes this specific tool on the MCP server.
|
|
2130
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2131
|
+
* @returns Promise containing the result of the tool execution.
|
|
2132
|
+
*/
|
|
2133
|
+
execute(params) {
|
|
2134
|
+
const body = {};
|
|
2135
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2136
|
+
if (params.runtimeContext !== void 0) {
|
|
2137
|
+
body.runtimeContext = params.runtimeContext;
|
|
2138
|
+
}
|
|
2139
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2140
|
+
method: "POST",
|
|
2141
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2142
|
+
});
|
|
2143
|
+
}
|
|
2144
|
+
};
|
|
2145
|
+
|
|
2146
|
+
// src/resources/agent-builder.ts
|
|
2147
|
+
var RECORD_SEPARATOR3 = "";
|
|
2148
|
+
var AgentBuilder = class extends BaseResource {
|
|
2149
|
+
constructor(options, actionId) {
|
|
2150
|
+
super(options);
|
|
2151
|
+
this.actionId = actionId;
|
|
2152
|
+
}
|
|
2153
|
+
// Helper function to transform workflow result to action result
|
|
2154
|
+
transformWorkflowResult(result) {
|
|
2155
|
+
if (result.status === "success") {
|
|
2156
|
+
return {
|
|
2157
|
+
success: result.result.success || false,
|
|
2158
|
+
applied: result.result.applied || false,
|
|
2159
|
+
branchName: result.result.branchName,
|
|
2160
|
+
message: result.result.message || "Agent builder action completed",
|
|
2161
|
+
validationResults: result.result.validationResults,
|
|
2162
|
+
error: result.result.error,
|
|
2163
|
+
errors: result.result.errors,
|
|
2164
|
+
stepResults: result.result.stepResults
|
|
2165
|
+
};
|
|
2166
|
+
} else if (result.status === "failed") {
|
|
2167
|
+
return {
|
|
2168
|
+
success: false,
|
|
2169
|
+
applied: false,
|
|
2170
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2171
|
+
error: result.error.message
|
|
2172
|
+
};
|
|
2173
|
+
} else {
|
|
2174
|
+
return {
|
|
2175
|
+
success: false,
|
|
2176
|
+
applied: false,
|
|
2177
|
+
message: "Agent builder action was suspended",
|
|
2178
|
+
error: "Workflow suspended - manual intervention required"
|
|
2179
|
+
};
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
/**
|
|
2183
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2184
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2185
|
+
*/
|
|
2186
|
+
async createRun(params) {
|
|
2187
|
+
const searchParams = new URLSearchParams();
|
|
2188
|
+
if (!!params?.runId) {
|
|
2189
|
+
searchParams.set("runId", params.runId);
|
|
2190
|
+
}
|
|
2191
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2192
|
+
return this.request(url, {
|
|
2193
|
+
method: "POST"
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* Creates a new workflow run (alias for createRun)
|
|
2198
|
+
* @param params - Optional object containing the optional runId
|
|
2199
|
+
* @returns Promise containing the runId of the created run
|
|
2200
|
+
*/
|
|
2201
|
+
createRunAsync(params) {
|
|
2202
|
+
return this.createRun(params);
|
|
2203
|
+
}
|
|
2204
|
+
/**
|
|
2205
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2206
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2207
|
+
*/
|
|
2208
|
+
async startAsync(params, runId) {
|
|
2209
|
+
const searchParams = new URLSearchParams();
|
|
2210
|
+
if (runId) {
|
|
2211
|
+
searchParams.set("runId", runId);
|
|
2212
|
+
}
|
|
2213
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2214
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2215
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2216
|
+
const result = await this.request(url, {
|
|
2217
|
+
method: "POST",
|
|
2218
|
+
body: { ...actionParams, runtimeContext }
|
|
2219
|
+
});
|
|
2220
|
+
return this.transformWorkflowResult(result);
|
|
2221
|
+
}
|
|
2222
|
+
/**
|
|
2223
|
+
* Starts an existing agent builder action run.
|
|
2224
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2225
|
+
*/
|
|
2226
|
+
async startActionRun(params, runId) {
|
|
2227
|
+
const searchParams = new URLSearchParams();
|
|
2228
|
+
searchParams.set("runId", runId);
|
|
2229
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2230
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2231
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2232
|
+
return this.request(url, {
|
|
2233
|
+
method: "POST",
|
|
2234
|
+
body: { ...actionParams, runtimeContext }
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
2237
|
+
/**
|
|
2238
|
+
* Resumes a suspended agent builder action step.
|
|
2239
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2240
|
+
*/
|
|
2241
|
+
async resume(params, runId) {
|
|
2242
|
+
const searchParams = new URLSearchParams();
|
|
2243
|
+
searchParams.set("runId", runId);
|
|
2244
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2245
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2246
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2247
|
+
return this.request(url, {
|
|
2248
|
+
method: "POST",
|
|
2249
|
+
body: { ...resumeParams, runtimeContext }
|
|
2250
|
+
});
|
|
2251
|
+
}
|
|
2252
|
+
/**
|
|
2253
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2254
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2255
|
+
*/
|
|
2256
|
+
async resumeAsync(params, runId) {
|
|
2257
|
+
const searchParams = new URLSearchParams();
|
|
2258
|
+
searchParams.set("runId", runId);
|
|
2259
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2260
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2261
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2262
|
+
const result = await this.request(url, {
|
|
2263
|
+
method: "POST",
|
|
2264
|
+
body: { ...resumeParams, runtimeContext }
|
|
2265
|
+
});
|
|
2266
|
+
return this.transformWorkflowResult(result);
|
|
2267
|
+
}
|
|
2268
|
+
/**
|
|
2269
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2270
|
+
* separated by the Record Separator character (\x1E)
|
|
2271
|
+
*
|
|
2272
|
+
* @param stream - The readable stream to process
|
|
2273
|
+
* @returns An async generator that yields parsed records
|
|
2274
|
+
*/
|
|
2275
|
+
async *streamProcessor(stream) {
|
|
2276
|
+
const reader = stream.getReader();
|
|
2277
|
+
let doneReading = false;
|
|
2278
|
+
let buffer = "";
|
|
2279
|
+
try {
|
|
2280
|
+
while (!doneReading) {
|
|
2281
|
+
const { done, value } = await reader.read();
|
|
2282
|
+
doneReading = done;
|
|
2283
|
+
if (done && !value) continue;
|
|
2284
|
+
try {
|
|
2285
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2286
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
2287
|
+
buffer = chunks.pop() || "";
|
|
2288
|
+
for (const chunk of chunks) {
|
|
2289
|
+
if (chunk) {
|
|
2290
|
+
if (typeof chunk === "string") {
|
|
2291
|
+
try {
|
|
2292
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2293
|
+
yield parsedChunk;
|
|
2294
|
+
} catch {
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
} catch {
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
if (buffer) {
|
|
2303
|
+
try {
|
|
2304
|
+
yield JSON.parse(buffer);
|
|
2305
|
+
} catch {
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
} finally {
|
|
2309
|
+
reader.cancel().catch(() => {
|
|
2310
|
+
});
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
/**
|
|
2314
|
+
* Streams agent builder action progress in real-time.
|
|
2315
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
2316
|
+
*/
|
|
2317
|
+
async stream(params, runId) {
|
|
2318
|
+
const searchParams = new URLSearchParams();
|
|
2319
|
+
if (runId) {
|
|
2320
|
+
searchParams.set("runId", runId);
|
|
2321
|
+
}
|
|
2322
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2323
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2324
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2325
|
+
const response = await this.request(url, {
|
|
2326
|
+
method: "POST",
|
|
2327
|
+
body: { ...actionParams, runtimeContext },
|
|
2328
|
+
stream: true
|
|
2329
|
+
});
|
|
2330
|
+
if (!response.ok) {
|
|
2331
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
2332
|
+
}
|
|
2333
|
+
if (!response.body) {
|
|
2334
|
+
throw new Error("Response body is null");
|
|
2335
|
+
}
|
|
2336
|
+
let failedChunk = void 0;
|
|
2337
|
+
const transformStream = new TransformStream({
|
|
2338
|
+
start() {
|
|
2339
|
+
},
|
|
2340
|
+
async transform(chunk, controller) {
|
|
2341
|
+
try {
|
|
2342
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2343
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2344
|
+
for (const chunk2 of chunks) {
|
|
2345
|
+
if (chunk2) {
|
|
2346
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2347
|
+
try {
|
|
2348
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2349
|
+
controller.enqueue(parsedChunk);
|
|
2350
|
+
failedChunk = void 0;
|
|
2351
|
+
} catch {
|
|
2352
|
+
failedChunk = newChunk;
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
} catch {
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
});
|
|
2360
|
+
return response.body.pipeThrough(transformStream);
|
|
2361
|
+
}
|
|
2362
|
+
/**
|
|
2363
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2364
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
2365
|
+
*/
|
|
2366
|
+
async streamVNext(params, runId) {
|
|
2367
|
+
const searchParams = new URLSearchParams();
|
|
2368
|
+
if (runId) {
|
|
2369
|
+
searchParams.set("runId", runId);
|
|
2370
|
+
}
|
|
2371
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2372
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2373
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2374
|
+
const response = await this.request(url, {
|
|
2375
|
+
method: "POST",
|
|
2376
|
+
body: { ...actionParams, runtimeContext },
|
|
2377
|
+
stream: true
|
|
2378
|
+
});
|
|
2379
|
+
if (!response.ok) {
|
|
2380
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2381
|
+
}
|
|
2382
|
+
if (!response.body) {
|
|
2383
|
+
throw new Error("Response body is null");
|
|
2384
|
+
}
|
|
2385
|
+
let failedChunk = void 0;
|
|
2386
|
+
const transformStream = new TransformStream({
|
|
2387
|
+
start() {
|
|
2388
|
+
},
|
|
2389
|
+
async transform(chunk, controller) {
|
|
2390
|
+
try {
|
|
2391
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2392
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2393
|
+
for (const chunk2 of chunks) {
|
|
2394
|
+
if (chunk2) {
|
|
2395
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2396
|
+
try {
|
|
2397
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2398
|
+
controller.enqueue(parsedChunk);
|
|
2399
|
+
failedChunk = void 0;
|
|
2400
|
+
} catch {
|
|
2401
|
+
failedChunk = newChunk;
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2404
|
+
}
|
|
2405
|
+
} catch {
|
|
2406
|
+
}
|
|
2407
|
+
}
|
|
2408
|
+
});
|
|
2409
|
+
return response.body.pipeThrough(transformStream);
|
|
2410
|
+
}
|
|
2411
|
+
/**
|
|
2412
|
+
* Watches an existing agent builder action run by runId.
|
|
2413
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2414
|
+
* and streams any remaining progress.
|
|
2415
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
2416
|
+
*/
|
|
2417
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2418
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2419
|
+
const response = await this.request(url, {
|
|
2420
|
+
method: "GET",
|
|
2421
|
+
stream: true
|
|
2422
|
+
});
|
|
2423
|
+
if (!response.ok) {
|
|
2424
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
2425
|
+
}
|
|
2426
|
+
if (!response.body) {
|
|
2427
|
+
throw new Error("Response body is null");
|
|
2428
|
+
}
|
|
2429
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2430
|
+
if (typeof record === "string") {
|
|
2431
|
+
onRecord(JSON.parse(record));
|
|
2432
|
+
} else {
|
|
2433
|
+
onRecord(record);
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
/**
|
|
2438
|
+
* Gets a specific action run by its ID.
|
|
2439
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
2440
|
+
*/
|
|
2441
|
+
async runById(runId) {
|
|
2442
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2443
|
+
return this.request(url, {
|
|
2444
|
+
method: "GET"
|
|
2445
|
+
});
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Gets details about this agent builder action.
|
|
2449
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
2450
|
+
*/
|
|
2451
|
+
async details() {
|
|
2452
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2453
|
+
return result;
|
|
2454
|
+
}
|
|
2455
|
+
/**
|
|
2456
|
+
* Gets all runs for this agent builder action.
|
|
2457
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
2458
|
+
*/
|
|
2459
|
+
async runs(params) {
|
|
2460
|
+
const searchParams = new URLSearchParams();
|
|
2461
|
+
if (params?.fromDate) {
|
|
2462
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2463
|
+
}
|
|
2464
|
+
if (params?.toDate) {
|
|
2465
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2466
|
+
}
|
|
2467
|
+
if (params?.limit !== void 0) {
|
|
2468
|
+
searchParams.set("limit", String(params.limit));
|
|
2469
|
+
}
|
|
2470
|
+
if (params?.offset !== void 0) {
|
|
2471
|
+
searchParams.set("offset", String(params.offset));
|
|
2472
|
+
}
|
|
2473
|
+
if (params?.resourceId) {
|
|
2474
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2475
|
+
}
|
|
2476
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2477
|
+
return this.request(url, {
|
|
2478
|
+
method: "GET"
|
|
2479
|
+
});
|
|
2480
|
+
}
|
|
2481
|
+
/**
|
|
2482
|
+
* Gets the execution result of an agent builder action run.
|
|
2483
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
2484
|
+
*/
|
|
2485
|
+
async runExecutionResult(runId) {
|
|
2486
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2487
|
+
return this.request(url, {
|
|
2488
|
+
method: "GET"
|
|
2489
|
+
});
|
|
2490
|
+
}
|
|
2491
|
+
/**
|
|
2492
|
+
* Cancels an agent builder action run.
|
|
2493
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
2494
|
+
*/
|
|
2495
|
+
async cancelRun(runId) {
|
|
2496
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2497
|
+
return this.request(url, {
|
|
2498
|
+
method: "POST"
|
|
2499
|
+
});
|
|
2500
|
+
}
|
|
2501
|
+
/**
|
|
2502
|
+
* Sends an event to an agent builder action run.
|
|
2503
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
2504
|
+
*/
|
|
2505
|
+
async sendRunEvent(params) {
|
|
2506
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2507
|
+
return this.request(url, {
|
|
2508
|
+
method: "POST",
|
|
2509
|
+
body: { event: params.event, data: params.data }
|
|
2510
|
+
});
|
|
2511
|
+
}
|
|
2512
|
+
};
|
|
2513
|
+
|
|
2514
|
+
// src/resources/observability.ts
|
|
2515
|
+
var Observability = class extends BaseResource {
|
|
2516
|
+
constructor(options) {
|
|
2517
|
+
super(options);
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Retrieves a specific AI trace by ID
|
|
2521
|
+
* @param traceId - ID of the trace to retrieve
|
|
2522
|
+
* @returns Promise containing the AI trace with all its spans
|
|
2523
|
+
*/
|
|
2524
|
+
getTrace(traceId) {
|
|
2525
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2529
|
+
* @param params - Parameters for pagination and filtering
|
|
2530
|
+
* @returns Promise containing paginated traces and pagination info
|
|
2531
|
+
*/
|
|
2532
|
+
getTraces(params) {
|
|
2533
|
+
const { pagination, filters } = params;
|
|
2534
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2535
|
+
const { name, spanType, entityId, entityType } = filters || {};
|
|
2536
|
+
const searchParams = new URLSearchParams();
|
|
2537
|
+
if (page !== void 0) {
|
|
2538
|
+
searchParams.set("page", String(page));
|
|
2539
|
+
}
|
|
2540
|
+
if (perPage !== void 0) {
|
|
2541
|
+
searchParams.set("perPage", String(perPage));
|
|
2542
|
+
}
|
|
2543
|
+
if (name) {
|
|
2544
|
+
searchParams.set("name", name);
|
|
2545
|
+
}
|
|
2546
|
+
if (spanType !== void 0) {
|
|
2547
|
+
searchParams.set("spanType", String(spanType));
|
|
2548
|
+
}
|
|
2549
|
+
if (entityId && entityType) {
|
|
2550
|
+
searchParams.set("entityId", entityId);
|
|
2551
|
+
searchParams.set("entityType", entityType);
|
|
2552
|
+
}
|
|
2553
|
+
if (dateRange) {
|
|
2554
|
+
const dateRangeStr = JSON.stringify({
|
|
2555
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2556
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2557
|
+
});
|
|
2558
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2559
|
+
}
|
|
2560
|
+
const queryString = searchParams.toString();
|
|
2561
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
2562
|
+
}
|
|
2563
|
+
};
|
|
2564
|
+
|
|
2565
|
+
// src/resources/network-memory-thread.ts
|
|
2566
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
2567
|
+
constructor(options, threadId, networkId) {
|
|
2568
|
+
super(options);
|
|
2569
|
+
this.threadId = threadId;
|
|
2570
|
+
this.networkId = networkId;
|
|
2571
|
+
}
|
|
2572
|
+
/**
|
|
2573
|
+
* Retrieves the memory thread details
|
|
2574
|
+
* @returns Promise containing thread details including title and metadata
|
|
2575
|
+
*/
|
|
2576
|
+
get() {
|
|
2577
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
2578
|
+
}
|
|
2579
|
+
/**
|
|
2580
|
+
* Updates the memory thread properties
|
|
2581
|
+
* @param params - Update parameters including title and metadata
|
|
2582
|
+
* @returns Promise containing updated thread details
|
|
2583
|
+
*/
|
|
2584
|
+
update(params) {
|
|
2585
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2586
|
+
method: "PATCH",
|
|
2587
|
+
body: params
|
|
2588
|
+
});
|
|
2589
|
+
}
|
|
2590
|
+
/**
|
|
2591
|
+
* Deletes the memory thread
|
|
2592
|
+
* @returns Promise containing deletion result
|
|
2593
|
+
*/
|
|
2594
|
+
delete() {
|
|
2595
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2596
|
+
method: "DELETE"
|
|
2597
|
+
});
|
|
2598
|
+
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Retrieves messages associated with the thread
|
|
2601
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
2602
|
+
* @returns Promise containing thread messages and UI messages
|
|
2603
|
+
*/
|
|
2604
|
+
getMessages(params) {
|
|
2605
|
+
const query = new URLSearchParams({
|
|
2606
|
+
networkId: this.networkId,
|
|
2607
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
2608
|
+
});
|
|
2609
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
2610
|
+
}
|
|
2611
|
+
/**
|
|
2612
|
+
* Deletes one or more messages from the thread
|
|
2613
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
2614
|
+
* message object with id property, or array of message objects
|
|
2615
|
+
* @returns Promise containing deletion result
|
|
2616
|
+
*/
|
|
2617
|
+
deleteMessages(messageIds) {
|
|
2618
|
+
const query = new URLSearchParams({
|
|
2619
|
+
networkId: this.networkId
|
|
2620
|
+
});
|
|
2621
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
2622
|
+
method: "POST",
|
|
2623
|
+
body: { messageIds }
|
|
2624
|
+
});
|
|
2625
|
+
}
|
|
2626
|
+
};
|
|
2627
|
+
|
|
2628
|
+
// src/resources/vNextNetwork.ts
|
|
2629
|
+
var RECORD_SEPARATOR4 = "";
|
|
2630
|
+
var VNextNetwork = class extends BaseResource {
|
|
2631
|
+
constructor(options, networkId) {
|
|
2632
|
+
super(options);
|
|
2633
|
+
this.networkId = networkId;
|
|
2634
|
+
}
|
|
2635
|
+
/**
|
|
2636
|
+
* Retrieves details about the network
|
|
2637
|
+
* @returns Promise containing vNext network details
|
|
2638
|
+
*/
|
|
2639
|
+
details() {
|
|
2640
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* Generates a response from the v-next network
|
|
2644
|
+
* @param params - Generation parameters including message
|
|
2645
|
+
* @returns Promise containing the generated response
|
|
2646
|
+
*/
|
|
2647
|
+
generate(params) {
|
|
2648
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
2649
|
+
method: "POST",
|
|
2650
|
+
body: {
|
|
2651
|
+
...params,
|
|
2652
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1107
2653
|
}
|
|
1108
2654
|
});
|
|
1109
|
-
return response.result;
|
|
1110
2655
|
}
|
|
1111
2656
|
/**
|
|
1112
|
-
*
|
|
1113
|
-
* @param params -
|
|
1114
|
-
* @returns Promise containing the
|
|
2657
|
+
* Generates a response from the v-next network using multiple primitives
|
|
2658
|
+
* @param params - Generation parameters including message
|
|
2659
|
+
* @returns Promise containing the generated response
|
|
1115
2660
|
*/
|
|
1116
|
-
|
|
1117
|
-
return this.request(`/
|
|
2661
|
+
loop(params) {
|
|
2662
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1118
2663
|
method: "POST",
|
|
1119
2664
|
body: {
|
|
1120
|
-
|
|
1121
|
-
params
|
|
2665
|
+
...params,
|
|
2666
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1122
2667
|
}
|
|
1123
2668
|
});
|
|
1124
2669
|
}
|
|
2670
|
+
async *streamProcessor(stream) {
|
|
2671
|
+
const reader = stream.getReader();
|
|
2672
|
+
let doneReading = false;
|
|
2673
|
+
let buffer = "";
|
|
2674
|
+
try {
|
|
2675
|
+
while (!doneReading) {
|
|
2676
|
+
const { done, value } = await reader.read();
|
|
2677
|
+
doneReading = done;
|
|
2678
|
+
if (done && !value) continue;
|
|
2679
|
+
try {
|
|
2680
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2681
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR4);
|
|
2682
|
+
buffer = chunks.pop() || "";
|
|
2683
|
+
for (const chunk of chunks) {
|
|
2684
|
+
if (chunk) {
|
|
2685
|
+
if (typeof chunk === "string") {
|
|
2686
|
+
try {
|
|
2687
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2688
|
+
yield parsedChunk;
|
|
2689
|
+
} catch {
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
} catch {
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
if (buffer) {
|
|
2698
|
+
try {
|
|
2699
|
+
yield JSON.parse(buffer);
|
|
2700
|
+
} catch {
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
} finally {
|
|
2704
|
+
reader.cancel().catch(() => {
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
1125
2708
|
/**
|
|
1126
|
-
*
|
|
1127
|
-
* @param params -
|
|
1128
|
-
* @returns Promise containing the
|
|
2709
|
+
* Streams a response from the v-next network
|
|
2710
|
+
* @param params - Stream parameters including message
|
|
2711
|
+
* @returns Promise containing the results
|
|
1129
2712
|
*/
|
|
1130
|
-
async
|
|
1131
|
-
|
|
2713
|
+
async stream(params, onRecord) {
|
|
2714
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1132
2715
|
method: "POST",
|
|
1133
2716
|
body: {
|
|
1134
|
-
|
|
1135
|
-
params
|
|
2717
|
+
...params,
|
|
2718
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1136
2719
|
},
|
|
1137
2720
|
stream: true
|
|
1138
2721
|
});
|
|
1139
|
-
|
|
1140
|
-
};
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
* Retrieves details about this specific tool from the MCP server.
|
|
1153
|
-
* @returns Promise containing the tool's information (name, description, schema).
|
|
1154
|
-
*/
|
|
1155
|
-
details() {
|
|
1156
|
-
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
2722
|
+
if (!response.ok) {
|
|
2723
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
2724
|
+
}
|
|
2725
|
+
if (!response.body) {
|
|
2726
|
+
throw new Error("Response body is null");
|
|
2727
|
+
}
|
|
2728
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2729
|
+
if (typeof record === "string") {
|
|
2730
|
+
onRecord(JSON.parse(record));
|
|
2731
|
+
} else {
|
|
2732
|
+
onRecord(record);
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
1157
2735
|
}
|
|
1158
2736
|
/**
|
|
1159
|
-
*
|
|
1160
|
-
* @param params -
|
|
1161
|
-
* @returns Promise containing the
|
|
2737
|
+
* Streams a response from the v-next network loop
|
|
2738
|
+
* @param params - Stream parameters including message
|
|
2739
|
+
* @returns Promise containing the results
|
|
1162
2740
|
*/
|
|
1163
|
-
|
|
1164
|
-
const
|
|
1165
|
-
if (params.data !== void 0) body.data = params.data;
|
|
1166
|
-
if (params.runtimeContext !== void 0) {
|
|
1167
|
-
body.runtimeContext = params.runtimeContext;
|
|
1168
|
-
}
|
|
1169
|
-
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2741
|
+
async loopStream(params, onRecord) {
|
|
2742
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1170
2743
|
method: "POST",
|
|
1171
|
-
body:
|
|
2744
|
+
body: {
|
|
2745
|
+
...params,
|
|
2746
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2747
|
+
},
|
|
2748
|
+
stream: true
|
|
1172
2749
|
});
|
|
2750
|
+
if (!response.ok) {
|
|
2751
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
2752
|
+
}
|
|
2753
|
+
if (!response.body) {
|
|
2754
|
+
throw new Error("Response body is null");
|
|
2755
|
+
}
|
|
2756
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2757
|
+
if (typeof record === "string") {
|
|
2758
|
+
onRecord(JSON.parse(record));
|
|
2759
|
+
} else {
|
|
2760
|
+
onRecord(record);
|
|
2761
|
+
}
|
|
2762
|
+
}
|
|
1173
2763
|
}
|
|
1174
2764
|
};
|
|
1175
2765
|
|
|
1176
2766
|
// src/client.ts
|
|
1177
2767
|
var MastraClient = class extends BaseResource {
|
|
2768
|
+
observability;
|
|
1178
2769
|
constructor(options) {
|
|
1179
2770
|
super(options);
|
|
2771
|
+
this.observability = new Observability(options);
|
|
1180
2772
|
}
|
|
1181
2773
|
/**
|
|
1182
2774
|
* Retrieves all available agents
|
|
@@ -1185,21 +2777,6 @@ var MastraClient = class extends BaseResource {
|
|
|
1185
2777
|
getAgents() {
|
|
1186
2778
|
return this.request("/api/agents");
|
|
1187
2779
|
}
|
|
1188
|
-
async getAGUI({ resourceId }) {
|
|
1189
|
-
const agents = await this.getAgents();
|
|
1190
|
-
return Object.entries(agents).reduce(
|
|
1191
|
-
(acc, [agentId]) => {
|
|
1192
|
-
const agent = this.getAgent(agentId);
|
|
1193
|
-
acc[agentId] = new AGUIAdapter({
|
|
1194
|
-
agentId,
|
|
1195
|
-
agent,
|
|
1196
|
-
resourceId
|
|
1197
|
-
});
|
|
1198
|
-
return acc;
|
|
1199
|
-
},
|
|
1200
|
-
{}
|
|
1201
|
-
);
|
|
1202
|
-
}
|
|
1203
2780
|
/**
|
|
1204
2781
|
* Gets an agent instance by ID
|
|
1205
2782
|
* @param agentId - ID of the agent to retrieve
|
|
@@ -1250,6 +2827,48 @@ var MastraClient = class extends BaseResource {
|
|
|
1250
2827
|
getMemoryStatus(agentId) {
|
|
1251
2828
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1252
2829
|
}
|
|
2830
|
+
/**
|
|
2831
|
+
* Retrieves memory threads for a resource
|
|
2832
|
+
* @param params - Parameters containing the resource ID
|
|
2833
|
+
* @returns Promise containing array of memory threads
|
|
2834
|
+
*/
|
|
2835
|
+
getNetworkMemoryThreads(params) {
|
|
2836
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
2837
|
+
}
|
|
2838
|
+
/**
|
|
2839
|
+
* Creates a new memory thread
|
|
2840
|
+
* @param params - Parameters for creating the memory thread
|
|
2841
|
+
* @returns Promise containing the created memory thread
|
|
2842
|
+
*/
|
|
2843
|
+
createNetworkMemoryThread(params) {
|
|
2844
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
2845
|
+
}
|
|
2846
|
+
/**
|
|
2847
|
+
* Gets a memory thread instance by ID
|
|
2848
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
2849
|
+
* @returns MemoryThread instance
|
|
2850
|
+
*/
|
|
2851
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
2852
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
2853
|
+
}
|
|
2854
|
+
/**
|
|
2855
|
+
* Saves messages to memory
|
|
2856
|
+
* @param params - Parameters containing messages to save
|
|
2857
|
+
* @returns Promise containing the saved messages
|
|
2858
|
+
*/
|
|
2859
|
+
saveNetworkMessageToMemory(params) {
|
|
2860
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
2861
|
+
method: "POST",
|
|
2862
|
+
body: params
|
|
2863
|
+
});
|
|
2864
|
+
}
|
|
2865
|
+
/**
|
|
2866
|
+
* Gets the status of the memory system
|
|
2867
|
+
* @returns Promise containing memory system status
|
|
2868
|
+
*/
|
|
2869
|
+
getNetworkMemoryStatus(networkId) {
|
|
2870
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
2871
|
+
}
|
|
1253
2872
|
/**
|
|
1254
2873
|
* Retrieves all available tools
|
|
1255
2874
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -1295,6 +2914,20 @@ var MastraClient = class extends BaseResource {
|
|
|
1295
2914
|
getWorkflow(workflowId) {
|
|
1296
2915
|
return new Workflow(this.options, workflowId);
|
|
1297
2916
|
}
|
|
2917
|
+
/**
|
|
2918
|
+
* Gets all available agent builder actions
|
|
2919
|
+
* @returns Promise containing map of action IDs to action details
|
|
2920
|
+
*/
|
|
2921
|
+
getAgentBuilderActions() {
|
|
2922
|
+
return this.request("/api/agent-builder/");
|
|
2923
|
+
}
|
|
2924
|
+
/**
|
|
2925
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
2926
|
+
* @returns AgentBuilder instance
|
|
2927
|
+
*/
|
|
2928
|
+
getAgentBuilderAction(actionId) {
|
|
2929
|
+
return new AgentBuilder(this.options, actionId);
|
|
2930
|
+
}
|
|
1298
2931
|
/**
|
|
1299
2932
|
* Gets a vector instance by name
|
|
1300
2933
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -1309,7 +2942,41 @@ var MastraClient = class extends BaseResource {
|
|
|
1309
2942
|
* @returns Promise containing array of log messages
|
|
1310
2943
|
*/
|
|
1311
2944
|
getLogs(params) {
|
|
1312
|
-
|
|
2945
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2946
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2947
|
+
const searchParams = new URLSearchParams();
|
|
2948
|
+
if (transportId) {
|
|
2949
|
+
searchParams.set("transportId", transportId);
|
|
2950
|
+
}
|
|
2951
|
+
if (fromDate) {
|
|
2952
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2953
|
+
}
|
|
2954
|
+
if (toDate) {
|
|
2955
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2956
|
+
}
|
|
2957
|
+
if (logLevel) {
|
|
2958
|
+
searchParams.set("logLevel", logLevel);
|
|
2959
|
+
}
|
|
2960
|
+
if (page) {
|
|
2961
|
+
searchParams.set("page", String(page));
|
|
2962
|
+
}
|
|
2963
|
+
if (perPage) {
|
|
2964
|
+
searchParams.set("perPage", String(perPage));
|
|
2965
|
+
}
|
|
2966
|
+
if (_filters) {
|
|
2967
|
+
if (Array.isArray(_filters)) {
|
|
2968
|
+
for (const filter of _filters) {
|
|
2969
|
+
searchParams.append("filters", filter);
|
|
2970
|
+
}
|
|
2971
|
+
} else {
|
|
2972
|
+
searchParams.set("filters", _filters);
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
if (searchParams.size) {
|
|
2976
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
2977
|
+
} else {
|
|
2978
|
+
return this.request(`/api/logs`);
|
|
2979
|
+
}
|
|
1313
2980
|
}
|
|
1314
2981
|
/**
|
|
1315
2982
|
* Gets logs for a specific run
|
|
@@ -1317,7 +2984,44 @@ var MastraClient = class extends BaseResource {
|
|
|
1317
2984
|
* @returns Promise containing array of log messages
|
|
1318
2985
|
*/
|
|
1319
2986
|
getLogForRun(params) {
|
|
1320
|
-
|
|
2987
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2988
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2989
|
+
const searchParams = new URLSearchParams();
|
|
2990
|
+
if (runId) {
|
|
2991
|
+
searchParams.set("runId", runId);
|
|
2992
|
+
}
|
|
2993
|
+
if (transportId) {
|
|
2994
|
+
searchParams.set("transportId", transportId);
|
|
2995
|
+
}
|
|
2996
|
+
if (fromDate) {
|
|
2997
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2998
|
+
}
|
|
2999
|
+
if (toDate) {
|
|
3000
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
3001
|
+
}
|
|
3002
|
+
if (logLevel) {
|
|
3003
|
+
searchParams.set("logLevel", logLevel);
|
|
3004
|
+
}
|
|
3005
|
+
if (page) {
|
|
3006
|
+
searchParams.set("page", String(page));
|
|
3007
|
+
}
|
|
3008
|
+
if (perPage) {
|
|
3009
|
+
searchParams.set("perPage", String(perPage));
|
|
3010
|
+
}
|
|
3011
|
+
if (_filters) {
|
|
3012
|
+
if (Array.isArray(_filters)) {
|
|
3013
|
+
for (const filter of _filters) {
|
|
3014
|
+
searchParams.append("filters", filter);
|
|
3015
|
+
}
|
|
3016
|
+
} else {
|
|
3017
|
+
searchParams.set("filters", _filters);
|
|
3018
|
+
}
|
|
3019
|
+
}
|
|
3020
|
+
if (searchParams.size) {
|
|
3021
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
3022
|
+
} else {
|
|
3023
|
+
return this.request(`/api/logs/${runId}`);
|
|
3024
|
+
}
|
|
1321
3025
|
}
|
|
1322
3026
|
/**
|
|
1323
3027
|
* List of all log transports
|
|
@@ -1375,6 +3079,13 @@ var MastraClient = class extends BaseResource {
|
|
|
1375
3079
|
getNetworks() {
|
|
1376
3080
|
return this.request("/api/networks");
|
|
1377
3081
|
}
|
|
3082
|
+
/**
|
|
3083
|
+
* Retrieves all available vNext networks
|
|
3084
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
3085
|
+
*/
|
|
3086
|
+
getVNextNetworks() {
|
|
3087
|
+
return this.request("/api/networks/v-next");
|
|
3088
|
+
}
|
|
1378
3089
|
/**
|
|
1379
3090
|
* Gets a network instance by ID
|
|
1380
3091
|
* @param networkId - ID of the network to retrieve
|
|
@@ -1383,6 +3094,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1383
3094
|
getNetwork(networkId) {
|
|
1384
3095
|
return new Network(this.options, networkId);
|
|
1385
3096
|
}
|
|
3097
|
+
/**
|
|
3098
|
+
* Gets a vNext network instance by ID
|
|
3099
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
3100
|
+
* @returns vNext Network instance
|
|
3101
|
+
*/
|
|
3102
|
+
getVNextNetwork(networkId) {
|
|
3103
|
+
return new VNextNetwork(this.options, networkId);
|
|
3104
|
+
}
|
|
1386
3105
|
/**
|
|
1387
3106
|
* Retrieves a list of available MCP servers.
|
|
1388
3107
|
* @param params - Optional parameters for pagination (limit, offset).
|
|
@@ -1439,6 +3158,134 @@ var MastraClient = class extends BaseResource {
|
|
|
1439
3158
|
getA2A(agentId) {
|
|
1440
3159
|
return new A2A(this.options, agentId);
|
|
1441
3160
|
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
3163
|
+
* @param agentId - ID of the agent.
|
|
3164
|
+
* @param threadId - ID of the thread.
|
|
3165
|
+
* @param resourceId - Optional ID of the resource.
|
|
3166
|
+
* @returns Working memory for the specified thread or resource.
|
|
3167
|
+
*/
|
|
3168
|
+
getWorkingMemory({
|
|
3169
|
+
agentId,
|
|
3170
|
+
threadId,
|
|
3171
|
+
resourceId
|
|
3172
|
+
}) {
|
|
3173
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
3174
|
+
}
|
|
3175
|
+
/**
|
|
3176
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
3177
|
+
* @param agentId - ID of the agent.
|
|
3178
|
+
* @param threadId - ID of the thread.
|
|
3179
|
+
* @param workingMemory - The new working memory content.
|
|
3180
|
+
* @param resourceId - Optional ID of the resource.
|
|
3181
|
+
*/
|
|
3182
|
+
updateWorkingMemory({
|
|
3183
|
+
agentId,
|
|
3184
|
+
threadId,
|
|
3185
|
+
workingMemory,
|
|
3186
|
+
resourceId
|
|
3187
|
+
}) {
|
|
3188
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
3189
|
+
method: "POST",
|
|
3190
|
+
body: {
|
|
3191
|
+
workingMemory,
|
|
3192
|
+
resourceId
|
|
3193
|
+
}
|
|
3194
|
+
});
|
|
3195
|
+
}
|
|
3196
|
+
/**
|
|
3197
|
+
* Retrieves all available scorers
|
|
3198
|
+
* @returns Promise containing list of available scorers
|
|
3199
|
+
*/
|
|
3200
|
+
getScorers() {
|
|
3201
|
+
return this.request("/api/scores/scorers");
|
|
3202
|
+
}
|
|
3203
|
+
/**
|
|
3204
|
+
* Retrieves a scorer by ID
|
|
3205
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
3206
|
+
* @returns Promise containing the scorer
|
|
3207
|
+
*/
|
|
3208
|
+
getScorer(scorerId) {
|
|
3209
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3210
|
+
}
|
|
3211
|
+
getScoresByScorerId(params) {
|
|
3212
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
3213
|
+
const searchParams = new URLSearchParams();
|
|
3214
|
+
if (entityId) {
|
|
3215
|
+
searchParams.set("entityId", entityId);
|
|
3216
|
+
}
|
|
3217
|
+
if (entityType) {
|
|
3218
|
+
searchParams.set("entityType", entityType);
|
|
3219
|
+
}
|
|
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/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
* Retrieves scores by run ID
|
|
3231
|
+
* @param params - Parameters containing run ID and pagination options
|
|
3232
|
+
* @returns Promise containing scores and pagination info
|
|
3233
|
+
*/
|
|
3234
|
+
getScoresByRunId(params) {
|
|
3235
|
+
const { runId, 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(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3245
|
+
}
|
|
3246
|
+
/**
|
|
3247
|
+
* Retrieves scores by entity ID and type
|
|
3248
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
3249
|
+
* @returns Promise containing scores and pagination info
|
|
3250
|
+
*/
|
|
3251
|
+
getScoresByEntityId(params) {
|
|
3252
|
+
const { entityId, entityType, page, perPage } = params;
|
|
3253
|
+
const searchParams = new URLSearchParams();
|
|
3254
|
+
if (page !== void 0) {
|
|
3255
|
+
searchParams.set("page", String(page));
|
|
3256
|
+
}
|
|
3257
|
+
if (perPage !== void 0) {
|
|
3258
|
+
searchParams.set("perPage", String(perPage));
|
|
3259
|
+
}
|
|
3260
|
+
const queryString = searchParams.toString();
|
|
3261
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
3262
|
+
}
|
|
3263
|
+
/**
|
|
3264
|
+
* Saves a score
|
|
3265
|
+
* @param params - Parameters containing the score data to save
|
|
3266
|
+
* @returns Promise containing the saved score
|
|
3267
|
+
*/
|
|
3268
|
+
saveScore(params) {
|
|
3269
|
+
return this.request("/api/scores", {
|
|
3270
|
+
method: "POST",
|
|
3271
|
+
body: params
|
|
3272
|
+
});
|
|
3273
|
+
}
|
|
3274
|
+
/**
|
|
3275
|
+
* Retrieves model providers with available keys
|
|
3276
|
+
* @returns Promise containing model providers with available keys
|
|
3277
|
+
*/
|
|
3278
|
+
getModelProviders() {
|
|
3279
|
+
return this.request(`/api/model-providers`);
|
|
3280
|
+
}
|
|
3281
|
+
getAITrace(traceId) {
|
|
3282
|
+
return this.observability.getTrace(traceId);
|
|
3283
|
+
}
|
|
3284
|
+
getAITraces(params) {
|
|
3285
|
+
return this.observability.getTraces(params);
|
|
3286
|
+
}
|
|
1442
3287
|
};
|
|
1443
3288
|
|
|
1444
3289
|
exports.MastraClient = MastraClient;
|
|
3290
|
+
//# sourceMappingURL=index.cjs.map
|
|
3291
|
+
//# sourceMappingURL=index.cjs.map
|