@inkeep/agents-sdk 0.39.4 → 0.40.0
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/dist/_virtual/rolldown_runtime.js +7 -0
- package/dist/agent.d.ts +186 -0
- package/dist/agent.js +720 -0
- package/dist/agentFullClient.d.ts +22 -0
- package/dist/agentFullClient.js +120 -0
- package/dist/artifact-component.d.ts +34 -0
- package/dist/artifact-component.js +104 -0
- package/dist/builderFunctions.d.ts +283 -0
- package/dist/builderFunctions.js +327 -0
- package/dist/builderFunctionsExperimental.d.ts +24 -0
- package/dist/builderFunctionsExperimental.js +27 -0
- package/dist/builders.d.ts +111 -0
- package/dist/builders.js +52 -0
- package/dist/credential-provider.d.ts +176 -0
- package/dist/credential-provider.js +237 -0
- package/dist/credential-ref.d.ts +60 -0
- package/dist/credential-ref.js +33 -0
- package/dist/data-component.d.ts +39 -0
- package/dist/data-component.js +109 -0
- package/dist/environment-settings.d.ts +27 -0
- package/dist/environment-settings.js +41 -0
- package/dist/external-agent.d.ts +64 -0
- package/dist/external-agent.js +156 -0
- package/dist/function-tool.d.ts +37 -0
- package/dist/function-tool.js +66 -0
- package/dist/index.d.ts +19 -1825
- package/dist/index.js +19 -4058
- package/dist/module-hosted-tool-manager.d.ts +40 -0
- package/dist/module-hosted-tool-manager.js +359 -0
- package/dist/project.d.ts +214 -0
- package/dist/project.js +615 -0
- package/dist/projectFullClient.d.ts +23 -0
- package/dist/projectFullClient.js +162 -0
- package/dist/runner.d.ts +41 -0
- package/dist/runner.js +145 -0
- package/dist/status-component.d.ts +22 -0
- package/dist/status-component.js +36 -0
- package/dist/subAgent.d.ts +52 -0
- package/dist/subAgent.js +616 -0
- package/dist/telemetry-provider.d.ts +218 -0
- package/dist/telemetry-provider.js +390 -0
- package/dist/tool.d.ts +53 -0
- package/dist/tool.js +130 -0
- package/dist/types.d.ts +296 -0
- package/dist/types.js +39 -0
- package/dist/utils/generateIdFromName.d.ts +9 -0
- package/dist/utils/generateIdFromName.js +12 -0
- package/dist/utils/getFunctionToolDeps.d.ts +17 -0
- package/dist/utils/getFunctionToolDeps.js +131 -0
- package/dist/utils/tool-normalization.d.ts +42 -0
- package/dist/utils/tool-normalization.js +41 -0
- package/dist/utils/validateFunction.d.ts +10 -0
- package/dist/utils/validateFunction.js +13 -0
- package/package.json +11 -16
- package/dist/index.cjs +0 -4147
- package/dist/index.d.cts +0 -1825
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/subAgent.js
ADDED
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
import { FunctionTool } from "./function-tool.js";
|
|
2
|
+
import { ArtifactComponent } from "./artifact-component.js";
|
|
3
|
+
import { DataComponent } from "./data-component.js";
|
|
4
|
+
import { Tool } from "./tool.js";
|
|
5
|
+
import { isAgentMcpConfig, normalizeAgentCanUseType } from "./utils/tool-normalization.js";
|
|
6
|
+
import { getLogger } from "@inkeep/agents-core";
|
|
7
|
+
import { convertZodToJsonSchemaWithPreview, isZodSchema } from "@inkeep/agents-core/utils/schema-conversion";
|
|
8
|
+
|
|
9
|
+
//#region src/subAgent.ts
|
|
10
|
+
const logger = getLogger("agent");
|
|
11
|
+
function resolveGetter(value) {
|
|
12
|
+
if (typeof value === "function") return value();
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
var SubAgent = class {
|
|
16
|
+
config;
|
|
17
|
+
type = "internal";
|
|
18
|
+
baseURL;
|
|
19
|
+
tenantId;
|
|
20
|
+
projectId;
|
|
21
|
+
initialized = false;
|
|
22
|
+
constructor(config) {
|
|
23
|
+
this.config = {
|
|
24
|
+
...config,
|
|
25
|
+
type: "internal"
|
|
26
|
+
};
|
|
27
|
+
this.baseURL = process.env.INKEEP_API_URL || "http://localhost:3002";
|
|
28
|
+
this.tenantId = "default";
|
|
29
|
+
this.projectId = "default";
|
|
30
|
+
logger.info({
|
|
31
|
+
tenantId: this.tenantId,
|
|
32
|
+
subAgentId: this.config.id,
|
|
33
|
+
agentName: config.name
|
|
34
|
+
}, "Agent constructor initialized");
|
|
35
|
+
}
|
|
36
|
+
setContext(tenantId, projectId, baseURL) {
|
|
37
|
+
this.tenantId = tenantId;
|
|
38
|
+
this.projectId = projectId;
|
|
39
|
+
if (baseURL) this.baseURL = baseURL;
|
|
40
|
+
}
|
|
41
|
+
getId() {
|
|
42
|
+
return this.config.id;
|
|
43
|
+
}
|
|
44
|
+
getName() {
|
|
45
|
+
return this.config.name;
|
|
46
|
+
}
|
|
47
|
+
getInstructions() {
|
|
48
|
+
return this.config.prompt || "";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the agent's description (the human-readable description field)
|
|
52
|
+
*/
|
|
53
|
+
getDescription() {
|
|
54
|
+
return this.config.description || "";
|
|
55
|
+
}
|
|
56
|
+
getTools() {
|
|
57
|
+
const tools = resolveGetter(this.config.canUse);
|
|
58
|
+
if (!tools) return {};
|
|
59
|
+
if (!Array.isArray(tools)) throw new Error("tools getter must return an array");
|
|
60
|
+
const toolRecord = {};
|
|
61
|
+
for (const tool of tools) if (tool && typeof tool === "object") {
|
|
62
|
+
let id;
|
|
63
|
+
let toolInstance;
|
|
64
|
+
if (isAgentMcpConfig(tool)) {
|
|
65
|
+
id = tool.server.getId();
|
|
66
|
+
toolInstance = tool.server;
|
|
67
|
+
toolInstance.selectedTools = tool.selectedTools;
|
|
68
|
+
toolInstance.headers = tool.headers;
|
|
69
|
+
toolInstance.toolPolicies = tool.toolPolicies;
|
|
70
|
+
} else {
|
|
71
|
+
toolInstance = tool;
|
|
72
|
+
id = toolInstance.getId();
|
|
73
|
+
}
|
|
74
|
+
if (id) toolRecord[id] = toolInstance;
|
|
75
|
+
}
|
|
76
|
+
return toolRecord;
|
|
77
|
+
}
|
|
78
|
+
getModels() {
|
|
79
|
+
return this.config.models;
|
|
80
|
+
}
|
|
81
|
+
setModels(models) {
|
|
82
|
+
this.config.models = models;
|
|
83
|
+
}
|
|
84
|
+
getTransfers() {
|
|
85
|
+
return typeof this.config.canTransferTo === "function" ? this.config.canTransferTo() : [];
|
|
86
|
+
}
|
|
87
|
+
getSubAgentDelegates() {
|
|
88
|
+
return typeof this.config.canDelegateTo === "function" ? this.config.canDelegateTo().filter((delegate) => {
|
|
89
|
+
if (typeof delegate === "object" && "externalAgent" in delegate) return false;
|
|
90
|
+
if (typeof delegate === "object" && "agent" in delegate) return false;
|
|
91
|
+
if (typeof delegate === "object" && "toFullAgentDefinition" in delegate) return false;
|
|
92
|
+
if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") return false;
|
|
93
|
+
return true;
|
|
94
|
+
}) : [];
|
|
95
|
+
}
|
|
96
|
+
getExternalAgentDelegates() {
|
|
97
|
+
if (typeof this.config.canDelegateTo !== "function") return [];
|
|
98
|
+
return this.config.canDelegateTo().filter((delegate) => {
|
|
99
|
+
if (typeof delegate === "object" && "externalAgent" in delegate) return true;
|
|
100
|
+
if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") return true;
|
|
101
|
+
return false;
|
|
102
|
+
}).map((delegate) => {
|
|
103
|
+
if ("externalAgent" in delegate) return delegate;
|
|
104
|
+
return {
|
|
105
|
+
externalAgent: delegate,
|
|
106
|
+
headers: void 0
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
getTeamAgentDelegates() {
|
|
111
|
+
if (typeof this.config.canDelegateTo !== "function") return [];
|
|
112
|
+
return this.config.canDelegateTo().filter((delegate) => {
|
|
113
|
+
if (typeof delegate === "object" && "agent" in delegate) return true;
|
|
114
|
+
if (typeof delegate === "object" && "toFullAgentDefinition" in delegate) return true;
|
|
115
|
+
return false;
|
|
116
|
+
}).map((delegate) => {
|
|
117
|
+
if ("agent" in delegate) return delegate;
|
|
118
|
+
return {
|
|
119
|
+
agent: delegate,
|
|
120
|
+
headers: void 0
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
getDelegates() {
|
|
125
|
+
return [
|
|
126
|
+
...this.getSubAgentDelegates(),
|
|
127
|
+
...this.getTeamAgentDelegates(),
|
|
128
|
+
...this.getExternalAgentDelegates()
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
getDataComponents() {
|
|
132
|
+
return (resolveGetter(this.config.dataComponents) || []).map((comp) => {
|
|
133
|
+
if (comp && typeof comp.getId === "function") return {
|
|
134
|
+
id: comp.getId(),
|
|
135
|
+
name: comp.getName(),
|
|
136
|
+
description: comp.getDescription(),
|
|
137
|
+
props: comp.getProps(),
|
|
138
|
+
render: comp.getRender?.() || null
|
|
139
|
+
};
|
|
140
|
+
if (comp && typeof comp === "object" && comp.props && isZodSchema(comp.props)) return {
|
|
141
|
+
id: comp.id,
|
|
142
|
+
name: comp.name,
|
|
143
|
+
description: comp.description,
|
|
144
|
+
props: convertZodToJsonSchemaWithPreview(comp.props),
|
|
145
|
+
render: comp.render || null
|
|
146
|
+
};
|
|
147
|
+
return comp;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
getArtifactComponents() {
|
|
151
|
+
return (resolveGetter(this.config.artifactComponents) || []).map((comp) => {
|
|
152
|
+
if (comp && typeof comp.getId === "function") return {
|
|
153
|
+
id: comp.getId(),
|
|
154
|
+
name: comp.getName(),
|
|
155
|
+
description: comp.getDescription(),
|
|
156
|
+
props: comp.getProps?.() || comp.props
|
|
157
|
+
};
|
|
158
|
+
if (comp && typeof comp === "object" && comp.props && isZodSchema(comp.props)) return {
|
|
159
|
+
id: comp.id,
|
|
160
|
+
name: comp.name,
|
|
161
|
+
description: comp.description,
|
|
162
|
+
props: convertZodToJsonSchemaWithPreview(comp.props)
|
|
163
|
+
};
|
|
164
|
+
return comp;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
addTool(_name, tool) {
|
|
168
|
+
const existingTools = this.config.canUse ? this.config.canUse() : [];
|
|
169
|
+
this.config.canUse = () => [...existingTools, tool];
|
|
170
|
+
}
|
|
171
|
+
addTransfer(...agents) {
|
|
172
|
+
if (typeof this.config.canTransferTo === "function") {
|
|
173
|
+
const existingTransfers = this.config.canTransferTo;
|
|
174
|
+
this.config.canTransferTo = () => [...existingTransfers(), ...agents];
|
|
175
|
+
} else this.config.canTransferTo = () => agents;
|
|
176
|
+
}
|
|
177
|
+
addDelegate(...agents) {
|
|
178
|
+
if (typeof this.config.canDelegateTo === "function") {
|
|
179
|
+
const existingDelegates = this.config.canDelegateTo;
|
|
180
|
+
this.config.canDelegateTo = () => [...existingDelegates(), ...agents];
|
|
181
|
+
} else this.config.canDelegateTo = () => agents;
|
|
182
|
+
}
|
|
183
|
+
async init() {
|
|
184
|
+
if (this.initialized) return;
|
|
185
|
+
try {
|
|
186
|
+
await this.upsertAgent();
|
|
187
|
+
await this.loadDataComponents();
|
|
188
|
+
await this.loadArtifactComponents();
|
|
189
|
+
await this.saveToolsAndRelations();
|
|
190
|
+
await this.saveDataComponents();
|
|
191
|
+
await this.saveArtifactComponents();
|
|
192
|
+
logger.info({ subAgentId: this.getId() }, "Agent initialized successfully");
|
|
193
|
+
this.initialized = true;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
logger.error({
|
|
196
|
+
subAgentId: this.getId(),
|
|
197
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
198
|
+
}, "Failed to initialize agent");
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async upsertAgent() {
|
|
203
|
+
const agentData = {
|
|
204
|
+
id: this.getId(),
|
|
205
|
+
name: this.config.name,
|
|
206
|
+
description: this.config.description || "",
|
|
207
|
+
prompt: this.config.prompt || "",
|
|
208
|
+
conversationHistoryConfig: this.config.conversationHistoryConfig,
|
|
209
|
+
models: this.config.models,
|
|
210
|
+
stopWhen: this.config.stopWhen
|
|
211
|
+
};
|
|
212
|
+
const updateResponse = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agents/${this.getId()}`, {
|
|
213
|
+
method: "PUT",
|
|
214
|
+
headers: { "Content-Type": "application/json" },
|
|
215
|
+
body: JSON.stringify(agentData)
|
|
216
|
+
});
|
|
217
|
+
if (updateResponse.ok) {
|
|
218
|
+
logger.info({ subAgentId: this.getId() }, "Agent updated successfully");
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (updateResponse.status === 404) {
|
|
222
|
+
logger.info({ subAgentId: this.getId() }, "Agent not found, creating new agent");
|
|
223
|
+
const createResponse = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agents`, {
|
|
224
|
+
method: "POST",
|
|
225
|
+
headers: { "Content-Type": "application/json" },
|
|
226
|
+
body: JSON.stringify(agentData)
|
|
227
|
+
});
|
|
228
|
+
if (!createResponse.ok) {
|
|
229
|
+
const errorText$1 = await createResponse.text().catch(() => "Unknown error");
|
|
230
|
+
throw new Error(`Failed to create agent: ${createResponse.status} ${createResponse.statusText} - ${errorText$1}`);
|
|
231
|
+
}
|
|
232
|
+
logger.info({ subAgentId: this.getId() }, "Agent created successfully");
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const errorText = await updateResponse.text().catch(() => "Unknown error");
|
|
236
|
+
throw new Error(`Failed to update agent: ${updateResponse.status} ${updateResponse.statusText} - ${errorText}`);
|
|
237
|
+
}
|
|
238
|
+
async saveToolsAndRelations() {
|
|
239
|
+
if (this.config.canUse) {
|
|
240
|
+
const tools = resolveGetter(this.config.canUse);
|
|
241
|
+
if (tools && Array.isArray(tools)) for (let i = 0; i < tools.length; i++) {
|
|
242
|
+
const toolConfig = tools[i];
|
|
243
|
+
try {
|
|
244
|
+
const normalizedTool = normalizeAgentCanUseType(toolConfig, `tool-${i}`);
|
|
245
|
+
await this.createTool(normalizedTool.toolId, toolConfig);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
logger.error({
|
|
248
|
+
toolId: isAgentMcpConfig(toolConfig) ? toolConfig.server.getId() : toolConfig.getId?.(),
|
|
249
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
250
|
+
}, "Tool creation failed");
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async saveDataComponents() {
|
|
257
|
+
logger.info({ dataComponents: this.config.dataComponents }, "dataComponents and config");
|
|
258
|
+
const components = resolveGetter(this.config.dataComponents);
|
|
259
|
+
if (components) for (const dataComponent of components) {
|
|
260
|
+
const plainComponent = dataComponent && typeof dataComponent.getId === "function" ? {
|
|
261
|
+
id: dataComponent.getId(),
|
|
262
|
+
name: dataComponent.getName(),
|
|
263
|
+
description: dataComponent.getDescription(),
|
|
264
|
+
props: dataComponent.getProps()
|
|
265
|
+
} : dataComponent;
|
|
266
|
+
await this.createDataComponent(plainComponent);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async saveArtifactComponents() {
|
|
270
|
+
logger.info({ artifactComponents: this.config.artifactComponents }, "artifactComponents and config");
|
|
271
|
+
const components = resolveGetter(this.config.artifactComponents);
|
|
272
|
+
if (components) for (const artifactComponent of components) {
|
|
273
|
+
const plainComponent = artifactComponent && typeof artifactComponent.getId === "function" ? {
|
|
274
|
+
id: artifactComponent.getId(),
|
|
275
|
+
name: artifactComponent.getName(),
|
|
276
|
+
description: artifactComponent.getDescription(),
|
|
277
|
+
props: artifactComponent.getProps?.() || artifactComponent.props
|
|
278
|
+
} : artifactComponent;
|
|
279
|
+
await this.createArtifactComponent(plainComponent);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async loadDataComponents() {
|
|
283
|
+
try {
|
|
284
|
+
const dbDataComponents = [].map((component) => ({
|
|
285
|
+
id: component.id,
|
|
286
|
+
name: component.name,
|
|
287
|
+
description: component.description,
|
|
288
|
+
props: component.props,
|
|
289
|
+
createdAt: component.createdAt,
|
|
290
|
+
updatedAt: component.updatedAt
|
|
291
|
+
}));
|
|
292
|
+
const configComponents = resolveGetter(this.config.dataComponents) || [];
|
|
293
|
+
const normalizedConfigComponents = configComponents.map((comp) => {
|
|
294
|
+
if (comp && typeof comp.getId === "function") return {
|
|
295
|
+
id: comp.getId(),
|
|
296
|
+
name: comp.getName(),
|
|
297
|
+
description: comp.getDescription(),
|
|
298
|
+
props: comp.getProps()
|
|
299
|
+
};
|
|
300
|
+
return comp;
|
|
301
|
+
});
|
|
302
|
+
const uniqueComponents = [...dbDataComponents, ...normalizedConfigComponents].reduce((acc, component) => {
|
|
303
|
+
const componentId = typeof component.getId === "function" ? component.getId() : component.id;
|
|
304
|
+
const existingIndex = acc.findIndex((c) => {
|
|
305
|
+
return (typeof c.getId === "function" ? c.getId() : c.id) === componentId;
|
|
306
|
+
});
|
|
307
|
+
if (existingIndex >= 0) acc[existingIndex] = component;
|
|
308
|
+
else acc.push(component);
|
|
309
|
+
return acc;
|
|
310
|
+
}, []);
|
|
311
|
+
this.config.dataComponents = () => uniqueComponents;
|
|
312
|
+
logger.info({
|
|
313
|
+
subAgentId: this.getId(),
|
|
314
|
+
dbComponentCount: dbDataComponents.length,
|
|
315
|
+
configComponentCount: configComponents.length,
|
|
316
|
+
totalComponentCount: uniqueComponents.length
|
|
317
|
+
}, "Loaded and merged data components");
|
|
318
|
+
} catch (error) {
|
|
319
|
+
logger.error({
|
|
320
|
+
subAgentId: this.getId(),
|
|
321
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
322
|
+
}, "Failed to load data components from database");
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async loadArtifactComponents() {
|
|
326
|
+
try {
|
|
327
|
+
const dbArtifactComponents = [].map((component) => ({
|
|
328
|
+
id: component.id,
|
|
329
|
+
name: component.name,
|
|
330
|
+
description: component.description,
|
|
331
|
+
props: component.props,
|
|
332
|
+
createdAt: component.createdAt,
|
|
333
|
+
updatedAt: component.updatedAt
|
|
334
|
+
}));
|
|
335
|
+
const configComponents = resolveGetter(this.config.artifactComponents) || [];
|
|
336
|
+
const normalizedConfigComponents = configComponents.map((comp) => {
|
|
337
|
+
if (comp && typeof comp.getId === "function") return {
|
|
338
|
+
id: comp.getId(),
|
|
339
|
+
name: comp.getName(),
|
|
340
|
+
description: comp.getDescription(),
|
|
341
|
+
props: comp.getProps?.() || comp.props
|
|
342
|
+
};
|
|
343
|
+
return comp;
|
|
344
|
+
});
|
|
345
|
+
const uniqueComponents = [...dbArtifactComponents, ...normalizedConfigComponents].reduce((acc, component) => {
|
|
346
|
+
const componentId = typeof component.getId === "function" ? component.getId() : component.id;
|
|
347
|
+
const existingIndex = acc.findIndex((c) => {
|
|
348
|
+
return (typeof c.getId === "function" ? c.getId() : c.id) === componentId;
|
|
349
|
+
});
|
|
350
|
+
if (existingIndex >= 0) acc[existingIndex] = component;
|
|
351
|
+
else acc.push(component);
|
|
352
|
+
return acc;
|
|
353
|
+
}, []);
|
|
354
|
+
this.config.artifactComponents = () => uniqueComponents;
|
|
355
|
+
logger.info({
|
|
356
|
+
subAgentId: this.getId(),
|
|
357
|
+
dbComponentCount: dbArtifactComponents.length,
|
|
358
|
+
configComponentCount: configComponents.length,
|
|
359
|
+
totalComponentCount: uniqueComponents.length
|
|
360
|
+
}, "Loaded and merged artifact components");
|
|
361
|
+
} catch (error) {
|
|
362
|
+
logger.error({
|
|
363
|
+
subAgentId: this.getId(),
|
|
364
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
365
|
+
}, "Failed to load artifact components from database");
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async createFunctionTool(toolId, functionTool) {
|
|
369
|
+
try {
|
|
370
|
+
const functionData = functionTool.serializeFunction();
|
|
371
|
+
const toolData = functionTool.serializeTool();
|
|
372
|
+
const functionUrl = `${this.baseURL}/tenants/${this.tenantId}/crud/projects/${this.projectId}/functions`;
|
|
373
|
+
logger.info({
|
|
374
|
+
agentId: this.getId(),
|
|
375
|
+
toolId,
|
|
376
|
+
functionUrl
|
|
377
|
+
}, "Attempting to create global function");
|
|
378
|
+
const functionResponse = await fetch(functionUrl, {
|
|
379
|
+
method: "POST",
|
|
380
|
+
headers: { "Content-Type": "application/json" },
|
|
381
|
+
body: JSON.stringify({
|
|
382
|
+
id: functionData.id,
|
|
383
|
+
inputSchema: functionData.inputSchema,
|
|
384
|
+
executeCode: functionData.executeCode,
|
|
385
|
+
dependencies: functionData.dependencies
|
|
386
|
+
})
|
|
387
|
+
});
|
|
388
|
+
logger.info({
|
|
389
|
+
agentId: this.getId(),
|
|
390
|
+
toolId,
|
|
391
|
+
functionStatus: functionResponse.status,
|
|
392
|
+
functionStatusText: functionResponse.statusText
|
|
393
|
+
}, "Function creation response received");
|
|
394
|
+
if (!functionResponse.ok) {
|
|
395
|
+
const errorText = await functionResponse.text();
|
|
396
|
+
logger.error({
|
|
397
|
+
agentId: this.getId(),
|
|
398
|
+
toolId,
|
|
399
|
+
functionStatus: functionResponse.status,
|
|
400
|
+
functionStatusText: functionResponse.statusText,
|
|
401
|
+
errorText
|
|
402
|
+
}, "Function creation failed");
|
|
403
|
+
throw new Error(`Failed to create function: ${functionResponse.status} ${errorText}`);
|
|
404
|
+
}
|
|
405
|
+
const toolUrl = `${this.baseURL}/tenants/${this.tenantId}/crud/projects/${this.projectId}/tools`;
|
|
406
|
+
logger.info({
|
|
407
|
+
agentId: this.getId(),
|
|
408
|
+
toolId,
|
|
409
|
+
toolUrl
|
|
410
|
+
}, "Attempting to create function tool");
|
|
411
|
+
const toolResponse = await fetch(toolUrl, {
|
|
412
|
+
method: "POST",
|
|
413
|
+
headers: { "Content-Type": "application/json" },
|
|
414
|
+
body: JSON.stringify({
|
|
415
|
+
id: toolData.id,
|
|
416
|
+
name: toolData.name,
|
|
417
|
+
description: toolData.description,
|
|
418
|
+
functionId: toolData.functionId,
|
|
419
|
+
config: { type: "function" }
|
|
420
|
+
})
|
|
421
|
+
});
|
|
422
|
+
logger.info({
|
|
423
|
+
agentId: this.getId(),
|
|
424
|
+
toolId,
|
|
425
|
+
toolStatus: toolResponse.status,
|
|
426
|
+
toolStatusText: toolResponse.statusText
|
|
427
|
+
}, "Tool creation response received");
|
|
428
|
+
if (!toolResponse.ok) {
|
|
429
|
+
const errorText = await toolResponse.text();
|
|
430
|
+
logger.error({
|
|
431
|
+
agentId: this.getId(),
|
|
432
|
+
toolId,
|
|
433
|
+
toolStatus: toolResponse.status,
|
|
434
|
+
toolStatusText: toolResponse.statusText,
|
|
435
|
+
errorText
|
|
436
|
+
}, "Tool creation failed");
|
|
437
|
+
throw new Error(`Failed to create tool: ${toolResponse.status} ${errorText}`);
|
|
438
|
+
}
|
|
439
|
+
await this.createAgentToolRelation(toolData.id);
|
|
440
|
+
logger.info({
|
|
441
|
+
agentId: this.getId(),
|
|
442
|
+
functionId: functionData.id,
|
|
443
|
+
toolId: toolData.id
|
|
444
|
+
}, "Function and tool created successfully");
|
|
445
|
+
} catch (error) {
|
|
446
|
+
logger.error({
|
|
447
|
+
agentId: this.getId(),
|
|
448
|
+
toolId,
|
|
449
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
450
|
+
stack: error instanceof Error ? error.stack : void 0
|
|
451
|
+
}, "Failed to create function tool");
|
|
452
|
+
throw error;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
async createTool(toolId, toolConfig) {
|
|
456
|
+
try {
|
|
457
|
+
if (toolConfig instanceof FunctionTool) {
|
|
458
|
+
await this.createFunctionTool(toolId, toolConfig);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
if (toolConfig.type === "function") {
|
|
462
|
+
logger.info({
|
|
463
|
+
subAgentId: this.getId(),
|
|
464
|
+
toolId
|
|
465
|
+
}, "Skipping function tool creation - will be handled at runtime");
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
let tool;
|
|
469
|
+
let selectedTools;
|
|
470
|
+
let headers;
|
|
471
|
+
let toolPolicies;
|
|
472
|
+
try {
|
|
473
|
+
const normalizedTool = normalizeAgentCanUseType(toolConfig, toolId);
|
|
474
|
+
tool = normalizedTool.tool;
|
|
475
|
+
selectedTools = normalizedTool.selectedTools;
|
|
476
|
+
headers = normalizedTool.headers;
|
|
477
|
+
toolPolicies = normalizedTool.toolPolicies;
|
|
478
|
+
tool.setContext(this.tenantId, this.projectId);
|
|
479
|
+
await tool.init();
|
|
480
|
+
} catch (_) {
|
|
481
|
+
tool = new Tool({
|
|
482
|
+
id: toolId,
|
|
483
|
+
name: toolConfig.name || toolId,
|
|
484
|
+
description: toolConfig.description || `MCP tool: ${toolId}`,
|
|
485
|
+
serverUrl: toolConfig.config?.serverUrl || toolConfig.serverUrl || "http://localhost:3000",
|
|
486
|
+
activeTools: toolConfig.config?.mcp?.activeTools,
|
|
487
|
+
credential: toolConfig.credential
|
|
488
|
+
});
|
|
489
|
+
tool.setContext(this.tenantId, this.projectId);
|
|
490
|
+
await tool.init();
|
|
491
|
+
}
|
|
492
|
+
await this.createAgentToolRelation(tool.getId(), selectedTools, headers, toolPolicies);
|
|
493
|
+
logger.info({
|
|
494
|
+
subAgentId: this.getId(),
|
|
495
|
+
toolId: tool.getId()
|
|
496
|
+
}, "Tool created and linked to agent");
|
|
497
|
+
} catch (error) {
|
|
498
|
+
logger.error({
|
|
499
|
+
subAgentId: this.getId(),
|
|
500
|
+
toolId,
|
|
501
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
502
|
+
}, "Failed to create tool");
|
|
503
|
+
throw error;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
async createDataComponent(dataComponent) {
|
|
507
|
+
try {
|
|
508
|
+
const dc = new DataComponent({
|
|
509
|
+
id: dataComponent.id,
|
|
510
|
+
name: dataComponent.name,
|
|
511
|
+
description: dataComponent.description,
|
|
512
|
+
props: dataComponent.props,
|
|
513
|
+
render: dataComponent.render
|
|
514
|
+
});
|
|
515
|
+
dc.setContext(this.tenantId, this.projectId);
|
|
516
|
+
await dc.init();
|
|
517
|
+
await this.createAgentDataComponentRelation(dc.getId());
|
|
518
|
+
logger.info({
|
|
519
|
+
subAgentId: this.getId(),
|
|
520
|
+
dataComponentId: dc.getId()
|
|
521
|
+
}, "DataComponent created and linked to agent");
|
|
522
|
+
} catch (error) {
|
|
523
|
+
logger.error({
|
|
524
|
+
subAgentId: this.getId(),
|
|
525
|
+
dataComponentName: dataComponent.name,
|
|
526
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
527
|
+
}, "Failed to create data component");
|
|
528
|
+
throw error;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
async createArtifactComponent(artifactComponent) {
|
|
532
|
+
try {
|
|
533
|
+
const ac = new ArtifactComponent({
|
|
534
|
+
id: artifactComponent.id,
|
|
535
|
+
name: artifactComponent.name,
|
|
536
|
+
description: artifactComponent.description,
|
|
537
|
+
props: artifactComponent.props
|
|
538
|
+
});
|
|
539
|
+
ac.setContext(this.tenantId, this.projectId);
|
|
540
|
+
await ac.init();
|
|
541
|
+
await this.createAgentArtifactComponentRelation(ac.getId());
|
|
542
|
+
logger.info({
|
|
543
|
+
subAgentId: this.getId(),
|
|
544
|
+
artifactComponentId: ac.getId()
|
|
545
|
+
}, "ArtifactComponent created and linked to agent");
|
|
546
|
+
} catch (error) {
|
|
547
|
+
logger.error({
|
|
548
|
+
subAgentId: this.getId(),
|
|
549
|
+
artifactComponentName: artifactComponent.name,
|
|
550
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
551
|
+
}, "Failed to create artifact component");
|
|
552
|
+
throw error;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
async createAgentDataComponentRelation(dataComponentId) {
|
|
556
|
+
const relationResponse = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-data-components`, {
|
|
557
|
+
method: "POST",
|
|
558
|
+
headers: { "Content-Type": "application/json" },
|
|
559
|
+
body: JSON.stringify({
|
|
560
|
+
id: `${this.getId()}-dc-${dataComponentId}`,
|
|
561
|
+
tenantId: this.tenantId,
|
|
562
|
+
subAgentId: this.getId(),
|
|
563
|
+
dataComponentId
|
|
564
|
+
})
|
|
565
|
+
});
|
|
566
|
+
if (!relationResponse.ok) throw new Error(`Failed to create agent-dataComponent relation: ${relationResponse.status} ${relationResponse.statusText}`);
|
|
567
|
+
logger.info({
|
|
568
|
+
subAgentId: this.getId(),
|
|
569
|
+
dataComponentId
|
|
570
|
+
}, "Created agent-dataComponent relation");
|
|
571
|
+
}
|
|
572
|
+
async createAgentArtifactComponentRelation(artifactComponentId) {
|
|
573
|
+
const relationResponse = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-artifact-components`, {
|
|
574
|
+
method: "POST",
|
|
575
|
+
headers: { "Content-Type": "application/json" },
|
|
576
|
+
body: JSON.stringify({
|
|
577
|
+
id: crypto.randomUUID(),
|
|
578
|
+
tenantId: this.tenantId,
|
|
579
|
+
subAgentId: this.getId(),
|
|
580
|
+
artifactComponentId
|
|
581
|
+
})
|
|
582
|
+
});
|
|
583
|
+
if (!relationResponse.ok) throw new Error(`Failed to create agent-artifactComponent relation: ${relationResponse.status} ${relationResponse.statusText}`);
|
|
584
|
+
logger.info({
|
|
585
|
+
subAgentId: this.getId(),
|
|
586
|
+
artifactComponentId
|
|
587
|
+
}, "Created agent-artifactComponent relation");
|
|
588
|
+
}
|
|
589
|
+
async createAgentToolRelation(toolId, selectedTools, headers, toolPolicies) {
|
|
590
|
+
const relationData = {
|
|
591
|
+
id: `${this.getId()}-tool-${toolId}`,
|
|
592
|
+
tenantId: this.tenantId,
|
|
593
|
+
projectId: this.projectId,
|
|
594
|
+
subAgentId: this.getId(),
|
|
595
|
+
toolId
|
|
596
|
+
};
|
|
597
|
+
if (selectedTools !== void 0) relationData.selectedTools = selectedTools;
|
|
598
|
+
if (headers !== void 0) relationData.headers = headers;
|
|
599
|
+
if (toolPolicies !== void 0 && Object.keys(toolPolicies).length > 0) relationData.toolPolicies = toolPolicies;
|
|
600
|
+
const relationResponse = await fetch(`${this.baseURL}/tenants/${this.tenantId}/projects/${this.projectId}/agent-tool-relations`, {
|
|
601
|
+
method: "POST",
|
|
602
|
+
headers: { "Content-Type": "application/json" },
|
|
603
|
+
body: JSON.stringify(relationData)
|
|
604
|
+
});
|
|
605
|
+
if (!relationResponse.ok) {
|
|
606
|
+
const errorBody = await relationResponse.text().catch(() => "Unknown error");
|
|
607
|
+
throw new Error(`Failed to create agent-tool relation: ${relationResponse.status} - ${errorBody}`);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
function agent(config) {
|
|
612
|
+
return new SubAgent(config);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
//#endregion
|
|
616
|
+
export { SubAgent, agent };
|