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