@robota-sdk/agent-tools 3.0.0-beta.63 → 3.0.0-beta.65

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.
@@ -1,1793 +1,1664 @@
1
- 'use strict';
2
-
3
- var promises = require('fs/promises');
4
- var path = require('path');
5
- var agentCore = require('@robota-sdk/agent-core');
6
- var child_process = require('child_process');
7
- var zod = require('zod');
8
- var crypto = require('crypto');
9
- var fg = require('fast-glob');
10
-
11
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
-
13
- var fg__default = /*#__PURE__*/_interopDefault(fg);
14
-
15
- // src/sandbox/e2b-sandbox-client.ts
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let node_fs_promises = require("node:fs/promises");
25
+ let node_path = require("node:path");
26
+ let _robota_sdk_agent_core = require("@robota-sdk/agent-core");
27
+ let node_child_process = require("node:child_process");
28
+ let zod = require("zod");
29
+ let node_crypto = require("node:crypto");
30
+ let fast_glob = require("fast-glob");
31
+ fast_glob = __toESM(fast_glob, 1);
32
+ //#region src/sandbox/e2b-sandbox-client.ts
16
33
  var E2BSandboxClient = class {
17
- sandbox;
18
- connectSandbox;
19
- createSandboxFromSnapshot;
20
- constructor(options) {
21
- this.sandbox = options.sandbox;
22
- this.connectSandbox = options.connectSandbox;
23
- this.createSandboxFromSnapshot = options.createSandboxFromSnapshot;
24
- }
25
- async run(command, options) {
26
- const result = await this.sandbox.commands.run(command, {
27
- background: false,
28
- timeoutMs: options?.timeoutMs,
29
- cwd: options?.workingDirectory
30
- });
31
- return {
32
- stdout: result.stdout ?? "",
33
- stderr: result.stderr ?? "",
34
- exitCode: result.exitCode ?? result.exit_code ?? 0
35
- };
36
- }
37
- async readFile(path) {
38
- const content = await this.sandbox.files.read(path);
39
- return typeof content === "string" ? content : Buffer.from(content).toString("utf8");
40
- }
41
- async writeFile(path, content) {
42
- await this.sandbox.files.write(path, content);
43
- }
44
- async snapshot() {
45
- if (this.sandbox.createSnapshot) {
46
- const snapshot = await this.sandbox.createSnapshot();
47
- const snapshotId = snapshot.snapshotId ?? snapshot.id;
48
- if (!snapshotId) {
49
- throw new Error("E2B createSnapshot() did not return a snapshot id.");
50
- }
51
- return snapshotId;
52
- }
53
- const sandboxId = this.sandbox.sandboxId;
54
- if (!sandboxId) {
55
- throw new Error("E2B sandboxId is required to create a resumable sandbox snapshot.");
56
- }
57
- if (!this.sandbox.pause) {
58
- throw new Error("E2B sandbox adapter does not expose pause().");
59
- }
60
- await this.sandbox.pause();
61
- return sandboxId;
62
- }
63
- async restore(snapshotId) {
64
- if (this.createSandboxFromSnapshot) {
65
- this.sandbox = await this.createSandboxFromSnapshot(snapshotId);
66
- return;
67
- }
68
- if (this.connectSandbox) {
69
- this.sandbox = await this.connectSandbox(snapshotId);
70
- return;
71
- }
72
- if (this.sandbox.sandboxId === snapshotId && this.sandbox.connect) {
73
- this.sandbox = await this.sandbox.connect();
74
- return;
75
- }
76
- throw new Error(
77
- "E2B sandbox restore requires connectSandbox(snapshotId) or sandbox.connect()."
78
- );
79
- }
34
+ sandbox;
35
+ connectSandbox;
36
+ createSandboxFromSnapshot;
37
+ constructor(options) {
38
+ this.sandbox = options.sandbox;
39
+ this.connectSandbox = options.connectSandbox;
40
+ this.createSandboxFromSnapshot = options.createSandboxFromSnapshot;
41
+ }
42
+ async run(command, options) {
43
+ const result = await this.sandbox.commands.run(command, {
44
+ background: false,
45
+ timeoutMs: options?.timeoutMs,
46
+ cwd: options?.workingDirectory
47
+ });
48
+ return {
49
+ stdout: result.stdout ?? "",
50
+ stderr: result.stderr ?? "",
51
+ exitCode: result.exitCode ?? result.exit_code ?? 0
52
+ };
53
+ }
54
+ async readFile(path) {
55
+ const content = await this.sandbox.files.read(path);
56
+ return typeof content === "string" ? content : Buffer.from(content).toString("utf8");
57
+ }
58
+ async writeFile(path, content) {
59
+ await this.sandbox.files.write(path, content);
60
+ }
61
+ async snapshot() {
62
+ if (this.sandbox.createSnapshot) {
63
+ const snapshot = await this.sandbox.createSnapshot();
64
+ const snapshotId = snapshot.snapshotId ?? snapshot.id;
65
+ if (!snapshotId) throw new Error("E2B createSnapshot() did not return a snapshot id.");
66
+ return snapshotId;
67
+ }
68
+ const sandboxId = this.sandbox.sandboxId;
69
+ if (!sandboxId) throw new Error("E2B sandboxId is required to create a resumable sandbox snapshot.");
70
+ if (!this.sandbox.pause) throw new Error("E2B sandbox adapter does not expose pause().");
71
+ await this.sandbox.pause();
72
+ return sandboxId;
73
+ }
74
+ async restore(snapshotId) {
75
+ if (this.createSandboxFromSnapshot) {
76
+ this.sandbox = await this.createSandboxFromSnapshot(snapshotId);
77
+ return;
78
+ }
79
+ if (this.connectSandbox) {
80
+ this.sandbox = await this.connectSandbox(snapshotId);
81
+ return;
82
+ }
83
+ if (this.sandbox.sandboxId === snapshotId && this.sandbox.connect) {
84
+ this.sandbox = await this.sandbox.connect();
85
+ return;
86
+ }
87
+ throw new Error("E2B sandbox restore requires connectSandbox(snapshotId) or sandbox.connect().");
88
+ }
80
89
  };
81
-
82
- // src/sandbox/in-memory-sandbox-client.ts
90
+ //#endregion
91
+ //#region src/sandbox/in-memory-sandbox-client.ts
83
92
  var InMemorySandboxClient = class {
84
- files = /* @__PURE__ */ new Map();
85
- snapshots = /* @__PURE__ */ new Map();
86
- runHandler;
87
- snapshotSequence = 0;
88
- constructor(options = {}) {
89
- for (const [path, content] of Object.entries(options.files ?? {})) {
90
- this.files.set(path, content);
91
- }
92
- this.runHandler = options.runHandler;
93
- }
94
- async run(command, options) {
95
- if (this.runHandler) {
96
- return this.runHandler(command, options, this.files);
97
- }
98
- return { stdout: "", stderr: "", exitCode: 0 };
99
- }
100
- async readFile(path) {
101
- const content = this.files.get(path);
102
- if (content === void 0) {
103
- throw new Error(`Sandbox file not found: ${path}`);
104
- }
105
- return content;
106
- }
107
- async writeFile(path, content) {
108
- this.files.set(path, content);
109
- }
110
- async snapshot() {
111
- const snapshotId = `snapshot-${++this.snapshotSequence}`;
112
- this.snapshots.set(snapshotId, new Map(this.files));
113
- return snapshotId;
114
- }
115
- async restore(snapshotId) {
116
- const snapshot = this.snapshots.get(snapshotId);
117
- if (!snapshot) {
118
- throw new Error(`Sandbox snapshot not found: ${snapshotId}`);
119
- }
120
- this.files.clear();
121
- for (const [path, content] of snapshot.entries()) {
122
- this.files.set(path, content);
123
- }
124
- }
125
- getFile(path) {
126
- return this.files.get(path);
127
- }
93
+ files = /* @__PURE__ */ new Map();
94
+ snapshots = /* @__PURE__ */ new Map();
95
+ runHandler;
96
+ snapshotSequence = 0;
97
+ constructor(options = {}) {
98
+ for (const [path, content] of Object.entries(options.files ?? {})) this.files.set(path, content);
99
+ this.runHandler = options.runHandler;
100
+ }
101
+ async run(command, options) {
102
+ if (this.runHandler) return this.runHandler(command, options, this.files);
103
+ return {
104
+ stdout: "",
105
+ stderr: "",
106
+ exitCode: 0
107
+ };
108
+ }
109
+ async readFile(path) {
110
+ const content = this.files.get(path);
111
+ if (content === void 0) throw new Error(`Sandbox file not found: ${path}`);
112
+ return content;
113
+ }
114
+ async writeFile(path, content) {
115
+ this.files.set(path, content);
116
+ }
117
+ async snapshot() {
118
+ const snapshotId = `snapshot-${++this.snapshotSequence}`;
119
+ this.snapshots.set(snapshotId, new Map(this.files));
120
+ return snapshotId;
121
+ }
122
+ async restore(snapshotId) {
123
+ const snapshot = this.snapshots.get(snapshotId);
124
+ if (!snapshot) throw new Error(`Sandbox snapshot not found: ${snapshotId}`);
125
+ this.files.clear();
126
+ for (const [path, content] of snapshot.entries()) this.files.set(path, content);
127
+ }
128
+ getFile(path) {
129
+ return this.files.get(path);
130
+ }
128
131
  };
129
- var DEFAULT_TARGET_ROOT = "/workspace";
130
- var WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
131
- var SHELL_QUOTE_PATTERN = /'/g;
132
+ //#endregion
133
+ //#region src/sandbox/workspace-manifest.ts
134
+ const DEFAULT_TARGET_ROOT = "/workspace";
135
+ const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
136
+ const SHELL_QUOTE_PATTERN = /'/g;
132
137
  async function applyWorkspaceManifest(sandboxClient, manifest, options = {}) {
133
- if (sandboxClient.applyManifest) {
134
- return sandboxClient.applyManifest(manifest, options);
135
- }
136
- const targetRoot = normalizeSandboxRoot(options.targetRoot ?? DEFAULT_TARGET_ROOT);
137
- const appliedEntries = [];
138
- for (const [rawPath, entry] of Object.entries(manifest.entries)) {
139
- const path = validateWorkspaceManifestPath(rawPath);
140
- const targetPath = joinSandboxPath(targetRoot, path);
141
- appliedEntries.push(
142
- await applyManifestEntry(sandboxClient, path, targetPath, targetRoot, entry, options)
143
- );
144
- }
145
- return { entries: appliedEntries };
138
+ if (sandboxClient.applyManifest) return sandboxClient.applyManifest(manifest, options);
139
+ const targetRoot = normalizeSandboxRoot(options.targetRoot ?? DEFAULT_TARGET_ROOT);
140
+ const appliedEntries = [];
141
+ for (const [rawPath, entry] of Object.entries(manifest.entries)) {
142
+ const path = validateWorkspaceManifestPath(rawPath);
143
+ const targetPath = joinSandboxPath(targetRoot, path);
144
+ appliedEntries.push(await applyManifestEntry(sandboxClient, path, targetPath, targetRoot, entry, options));
145
+ }
146
+ return { entries: appliedEntries };
146
147
  }
147
148
  function validateWorkspaceManifestPath(path) {
148
- if (path.length === 0) {
149
- throw new Error("workspace manifest path must not be empty");
150
- }
151
- if (path.includes("\0")) {
152
- throw new Error("workspace manifest path must not contain NUL bytes");
153
- }
154
- if (path.startsWith("/") || path.startsWith("\\") || WINDOWS_ABSOLUTE_PATH_PATTERN.test(path)) {
155
- throw new Error("workspace manifest path must be workspace-relative");
156
- }
157
- const parts = path.replace(/\\/g, "/").split("/").filter(Boolean);
158
- if (parts.length === 0) {
159
- throw new Error("workspace manifest path must not resolve to the workspace root");
160
- }
161
- if (parts.some((part) => part === "..")) {
162
- throw new Error("workspace manifest path cannot contain traversal segments");
163
- }
164
- const normalizedParts = parts.filter((part) => part !== ".");
165
- if (normalizedParts.length === 0) {
166
- throw new Error("workspace manifest path must not resolve to the workspace root");
167
- }
168
- return normalizedParts.join("/");
149
+ if (path.length === 0) throw new Error("workspace manifest path must not be empty");
150
+ if (path.includes("\0")) throw new Error("workspace manifest path must not contain NUL bytes");
151
+ if (path.startsWith("/") || path.startsWith("\\") || WINDOWS_ABSOLUTE_PATH_PATTERN.test(path)) throw new Error("workspace manifest path must be workspace-relative");
152
+ const parts = path.replace(/\\/g, "/").split("/").filter(Boolean);
153
+ if (parts.length === 0) throw new Error("workspace manifest path must not resolve to the workspace root");
154
+ if (parts.some((part) => part === "..")) throw new Error("workspace manifest path cannot contain traversal segments");
155
+ const normalizedParts = parts.filter((part) => part !== ".");
156
+ if (normalizedParts.length === 0) throw new Error("workspace manifest path must not resolve to the workspace root");
157
+ return normalizedParts.join("/");
169
158
  }
170
159
  async function applyManifestEntry(sandboxClient, path, targetPath, targetRoot, entry, options) {
171
- switch (entry.type) {
172
- case "file":
173
- await writeSandboxFile(sandboxClient, targetPath, targetRoot, entry.content);
174
- return createAppliedEntry(path, entry.type);
175
- case "dir":
176
- await createSandboxDirectory(sandboxClient, targetPath);
177
- return createAppliedEntry(path, entry.type);
178
- case "localFile":
179
- await copyLocalFile(sandboxClient, entry.src, targetPath, targetRoot, options);
180
- return createAppliedEntry(path, entry.type);
181
- case "localDir":
182
- await copyLocalDirectory(sandboxClient, entry.src, targetPath, options);
183
- return createAppliedEntry(path, entry.type);
184
- case "gitRepo":
185
- await cloneGitRepository(sandboxClient, entry, targetPath);
186
- return createAppliedEntry(path, entry.type);
187
- case "s3Mount":
188
- case "gcsMount":
189
- case "r2Mount":
190
- case "azureBlobMount":
191
- return {
192
- path,
193
- type: entry.type,
194
- status: "unsupported",
195
- message: `${entry.type} requires a provider-specific sandbox adapter.`
196
- };
197
- default:
198
- return assertUnreachable(entry);
199
- }
160
+ switch (entry.type) {
161
+ case "file":
162
+ await writeSandboxFile(sandboxClient, targetPath, targetRoot, entry.content);
163
+ return createAppliedEntry(path, entry.type);
164
+ case "dir":
165
+ await createSandboxDirectory(sandboxClient, targetPath);
166
+ return createAppliedEntry(path, entry.type);
167
+ case "localFile":
168
+ await copyLocalFile(sandboxClient, entry.src, targetPath, targetRoot, options);
169
+ return createAppliedEntry(path, entry.type);
170
+ case "localDir":
171
+ await copyLocalDirectory(sandboxClient, entry.src, targetPath, options);
172
+ return createAppliedEntry(path, entry.type);
173
+ case "gitRepo":
174
+ await cloneGitRepository(sandboxClient, entry, targetPath);
175
+ return createAppliedEntry(path, entry.type);
176
+ case "s3Mount":
177
+ case "gcsMount":
178
+ case "r2Mount":
179
+ case "azureBlobMount": return {
180
+ path,
181
+ type: entry.type,
182
+ status: "unsupported",
183
+ message: `${entry.type} requires a provider-specific sandbox adapter.`
184
+ };
185
+ default: return assertUnreachable(entry);
186
+ }
200
187
  }
201
188
  function createAppliedEntry(path, type) {
202
- return { path, type, status: "applied" };
189
+ return {
190
+ path,
191
+ type,
192
+ status: "applied"
193
+ };
203
194
  }
204
195
  async function copyLocalFile(sandboxClient, source, targetPath, targetRoot, options) {
205
- const hostSourcePath = resolveHostSourcePath(source, options.hostRoot);
206
- const content = await promises.readFile(hostSourcePath, "utf8");
207
- await writeSandboxFile(sandboxClient, targetPath, targetRoot, content);
196
+ await writeSandboxFile(sandboxClient, targetPath, targetRoot, await (0, node_fs_promises.readFile)(resolveHostSourcePath(source, options.hostRoot), "utf8"));
208
197
  }
209
198
  async function copyLocalDirectory(sandboxClient, source, targetPath, options) {
210
- const hostSourcePath = resolveHostSourcePath(source, options.hostRoot);
211
- await copyLocalDirectoryRecursive(sandboxClient, hostSourcePath, targetPath);
199
+ await copyLocalDirectoryRecursive(sandboxClient, resolveHostSourcePath(source, options.hostRoot), targetPath);
212
200
  }
213
201
  async function copyLocalDirectoryRecursive(sandboxClient, sourcePath, targetPath) {
214
- await createSandboxDirectory(sandboxClient, targetPath);
215
- const entries = await promises.readdir(sourcePath, { withFileTypes: true });
216
- for (const entry of entries) {
217
- const childSourcePath = path.join(sourcePath, entry.name);
218
- const childTargetPath = joinSandboxPath(targetPath, entry.name);
219
- if (entry.isDirectory()) {
220
- await copyLocalDirectoryRecursive(sandboxClient, childSourcePath, childTargetPath);
221
- continue;
222
- }
223
- if (entry.isFile()) {
224
- const content = await promises.readFile(childSourcePath, "utf8");
225
- await sandboxClient.writeFile(childTargetPath, content);
226
- }
227
- }
202
+ await createSandboxDirectory(sandboxClient, targetPath);
203
+ const entries = await (0, node_fs_promises.readdir)(sourcePath, { withFileTypes: true });
204
+ for (const entry of entries) {
205
+ const childSourcePath = (0, node_path.join)(sourcePath, entry.name);
206
+ const childTargetPath = joinSandboxPath(targetPath, entry.name);
207
+ if (entry.isDirectory()) {
208
+ await copyLocalDirectoryRecursive(sandboxClient, childSourcePath, childTargetPath);
209
+ continue;
210
+ }
211
+ if (entry.isFile()) {
212
+ const content = await (0, node_fs_promises.readFile)(childSourcePath, "utf8");
213
+ await sandboxClient.writeFile(childTargetPath, content);
214
+ }
215
+ }
228
216
  }
229
217
  async function cloneGitRepository(sandboxClient, entry, targetPath) {
230
- const shallowArgs = entry.shallow === false ? "" : " --depth 1";
231
- const refArgs = entry.ref ? ` --branch ${quoteShellArg(entry.ref)}` : "";
232
- await runSandboxCommand(
233
- sandboxClient,
234
- `git clone${shallowArgs}${refArgs} ${quoteShellArg(entry.url)} ${quoteShellArg(targetPath)}`
235
- );
218
+ await runSandboxCommand(sandboxClient, `git clone${entry.shallow === false ? "" : " --depth 1"}${entry.ref ? ` --branch ${quoteShellArg(entry.ref)}` : ""} ${quoteShellArg(entry.url)} ${quoteShellArg(targetPath)}`);
236
219
  }
237
220
  async function writeSandboxFile(sandboxClient, targetPath, targetRoot, content) {
238
- const parentPath = path.posix.dirname(targetPath);
239
- if (parentPath !== targetRoot) {
240
- await createSandboxDirectory(sandboxClient, parentPath);
241
- }
242
- await sandboxClient.writeFile(targetPath, content);
221
+ const parentPath = node_path.posix.dirname(targetPath);
222
+ if (parentPath !== targetRoot) await createSandboxDirectory(sandboxClient, parentPath);
223
+ await sandboxClient.writeFile(targetPath, content);
243
224
  }
244
225
  async function createSandboxDirectory(sandboxClient, targetPath) {
245
- await runSandboxCommand(sandboxClient, `mkdir -p ${quoteShellArg(targetPath)}`);
226
+ await runSandboxCommand(sandboxClient, `mkdir -p ${quoteShellArg(targetPath)}`);
246
227
  }
247
228
  async function runSandboxCommand(sandboxClient, command) {
248
- const result = await sandboxClient.run(command);
249
- if (result.exitCode !== 0) {
250
- throw new Error(
251
- `workspace manifest command failed: ${command}
252
- ${result.stderr ?? result.stdout}`
253
- );
254
- }
229
+ const result = await sandboxClient.run(command);
230
+ if (result.exitCode !== 0) throw new Error(`workspace manifest command failed: ${command}\n${result.stderr ?? result.stdout}`);
255
231
  }
256
232
  function resolveHostSourcePath(source, hostRoot) {
257
- return path.isAbsolute(source) ? path.resolve(source) : path.resolve(hostRoot ?? process.cwd(), source);
233
+ return (0, node_path.isAbsolute)(source) ? (0, node_path.resolve)(source) : (0, node_path.resolve)(hostRoot ?? process.cwd(), source);
258
234
  }
259
235
  function normalizeSandboxRoot(root) {
260
- const normalized = root.replace(/\\/g, "/").replace(/\/+$/, "");
261
- if (!normalized.startsWith("/")) {
262
- throw new Error("workspace manifest targetRoot must be an absolute sandbox path");
263
- }
264
- return normalized.length === 0 ? "/" : normalized;
236
+ const normalized = root.replace(/\\/g, "/").replace(/\/+$/, "");
237
+ if (!normalized.startsWith("/")) throw new Error("workspace manifest targetRoot must be an absolute sandbox path");
238
+ return normalized.length === 0 ? "/" : normalized;
265
239
  }
266
240
  function joinSandboxPath(root, path) {
267
- const normalizedRoot = normalizeSandboxRoot(root);
268
- if (normalizedRoot === "/") {
269
- return `/${path}`;
270
- }
271
- return `${normalizedRoot}/${path}`;
241
+ const normalizedRoot = normalizeSandboxRoot(root);
242
+ if (normalizedRoot === "/") return `/${path}`;
243
+ return `${normalizedRoot}/${path}`;
272
244
  }
273
245
  function quoteShellArg(value) {
274
- return `'${value.replace(SHELL_QUOTE_PATTERN, "'\\''")}'`;
246
+ return `'${value.replace(SHELL_QUOTE_PATTERN, "'\\''")}'`;
275
247
  }
276
248
  function assertUnreachable(value) {
277
- throw new Error(`unsupported workspace manifest entry: ${JSON.stringify(value)}`);
249
+ throw new Error(`unsupported workspace manifest entry: ${JSON.stringify(value)}`);
278
250
  }
251
+ //#endregion
252
+ //#region src/registry/tool-registry.ts
253
+ /**
254
+ * Tool registry implementation
255
+ * Manages tool registration, validation, and retrieval
256
+ */
279
257
  var ToolRegistry = class {
280
- tools = /* @__PURE__ */ new Map();
281
- /**
282
- * Register a tool
283
- */
284
- register(tool) {
285
- if (!tool.schema?.name) {
286
- throw new agentCore.ValidationError("Tool must have a valid schema with name");
287
- }
288
- const toolName = tool.schema.name;
289
- this.validateToolSchema(tool.schema);
290
- if (this.tools.has(toolName)) {
291
- agentCore.logger.warn(`Tool "${toolName}" is already registered, overriding`, {
292
- toolName,
293
- existingTool: this.tools.get(toolName)?.constructor.name
294
- });
295
- }
296
- this.tools.set(toolName, tool);
297
- agentCore.logger.debug(`Tool "${toolName}" registered successfully`, {
298
- toolName,
299
- toolType: tool.constructor.name,
300
- parameters: Object.keys(tool.schema.parameters?.properties || {})
301
- });
302
- }
303
- /**
304
- * Unregister a tool
305
- */
306
- unregister(name) {
307
- if (!this.tools.has(name)) {
308
- agentCore.logger.warn(`Attempted to unregister non-existent tool "${name}"`);
309
- return;
310
- }
311
- this.tools.delete(name);
312
- agentCore.logger.debug(`Tool "${name}" unregistered successfully`);
313
- }
314
- /**
315
- * Get tool by name
316
- */
317
- get(name) {
318
- return this.tools.get(name);
319
- }
320
- /**
321
- * Get all registered tools
322
- */
323
- getAll() {
324
- return Array.from(this.tools.values());
325
- }
326
- /**
327
- * Get tool schemas
328
- */
329
- getSchemas() {
330
- const tools = this.getAll();
331
- agentCore.logger.debug("[TOOL-FLOW] ToolRegistry.getSchemas() - Tools before schema extraction", {
332
- count: tools.length,
333
- tools: tools.map((t) => ({
334
- name: t.schema?.name ?? "unnamed",
335
- hasSchema: !!t.schema,
336
- schemaType: typeof t.schema,
337
- toolType: t.constructor?.name || "unknown"
338
- }))
339
- });
340
- return this.getAll().map((tool) => tool.schema);
341
- }
342
- /**
343
- * Check if tool exists
344
- */
345
- has(name) {
346
- return this.tools.has(name);
347
- }
348
- /**
349
- * Clear all tools
350
- */
351
- clear() {
352
- const toolCount = this.tools.size;
353
- this.tools.clear();
354
- agentCore.logger.debug(`Cleared ${toolCount} tools from registry`);
355
- }
356
- /**
357
- * Get tool names
358
- */
359
- getToolNames() {
360
- return Array.from(this.tools.keys());
361
- }
362
- /**
363
- * Get tools by pattern
364
- */
365
- getToolsByPattern(pattern) {
366
- const regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
367
- return this.getAll().filter((tool) => regex.test(tool.schema.name));
368
- }
369
- /**
370
- * Get tool count
371
- */
372
- size() {
373
- return this.tools.size;
374
- }
375
- /**
376
- * Validate tool schema
377
- */
378
- validateToolSchema(schema) {
379
- if (!schema.name || typeof schema.name !== "string") {
380
- throw new agentCore.ValidationError("Tool schema must have a valid name");
381
- }
382
- if (!schema.description || typeof schema.description !== "string") {
383
- throw new agentCore.ValidationError("Tool schema must have a description");
384
- }
385
- if (!schema.parameters || typeof schema.parameters !== "object" || schema.parameters === null || Array.isArray(schema.parameters)) {
386
- throw new agentCore.ValidationError("Tool schema must have parameters object");
387
- }
388
- if (schema.parameters.type !== "object") {
389
- throw new agentCore.ValidationError('Tool parameters type must be "object"');
390
- }
391
- if (schema.parameters.properties) {
392
- for (const propName of Object.keys(schema.parameters.properties)) {
393
- const propSchema = schema.parameters.properties[propName];
394
- if (!propSchema?.type) {
395
- throw new agentCore.ValidationError(`Parameter "${propName}" must have a type`);
396
- }
397
- const validTypes = ["string", "number", "boolean", "array", "object"];
398
- if (!validTypes.includes(propSchema.type)) {
399
- throw new agentCore.ValidationError(
400
- `Parameter "${propName}" has invalid type "${propSchema.type}"`
401
- );
402
- }
403
- }
404
- }
405
- if (schema.parameters.required) {
406
- const properties = schema.parameters.properties || {};
407
- for (const requiredField of schema.parameters.required) {
408
- if (!properties[requiredField]) {
409
- throw new agentCore.ValidationError(
410
- `Required parameter "${requiredField}" is not defined in properties`
411
- );
412
- }
413
- }
414
- }
415
- }
258
+ tools = /* @__PURE__ */ new Map();
259
+ /**
260
+ * Register a tool
261
+ */
262
+ register(tool) {
263
+ if (!tool.schema?.name) throw new _robota_sdk_agent_core.ValidationError("Tool must have a valid schema with name");
264
+ const toolName = tool.schema.name;
265
+ this.validateToolSchema(tool.schema);
266
+ if (this.tools.has(toolName)) _robota_sdk_agent_core.logger.warn(`Tool "${toolName}" is already registered, overriding`, {
267
+ toolName,
268
+ existingTool: this.tools.get(toolName)?.constructor.name
269
+ });
270
+ this.tools.set(toolName, tool);
271
+ _robota_sdk_agent_core.logger.debug(`Tool "${toolName}" registered successfully`, {
272
+ toolName,
273
+ toolType: tool.constructor.name,
274
+ parameters: Object.keys(tool.schema.parameters?.properties || {})
275
+ });
276
+ }
277
+ /**
278
+ * Unregister a tool
279
+ */
280
+ unregister(name) {
281
+ if (!this.tools.has(name)) {
282
+ _robota_sdk_agent_core.logger.warn(`Attempted to unregister non-existent tool "${name}"`);
283
+ return;
284
+ }
285
+ this.tools.delete(name);
286
+ _robota_sdk_agent_core.logger.debug(`Tool "${name}" unregistered successfully`);
287
+ }
288
+ /**
289
+ * Get tool by name
290
+ */
291
+ get(name) {
292
+ return this.tools.get(name);
293
+ }
294
+ /**
295
+ * Get all registered tools
296
+ */
297
+ getAll() {
298
+ return Array.from(this.tools.values());
299
+ }
300
+ /**
301
+ * Get tool schemas
302
+ */
303
+ getSchemas() {
304
+ const tools = this.getAll();
305
+ _robota_sdk_agent_core.logger.debug("[TOOL-FLOW] ToolRegistry.getSchemas() - Tools before schema extraction", {
306
+ count: tools.length,
307
+ tools: tools.map((t) => ({
308
+ name: t.schema?.name ?? "unnamed",
309
+ hasSchema: !!t.schema,
310
+ schemaType: typeof t.schema,
311
+ toolType: t.constructor?.name || "unknown"
312
+ }))
313
+ });
314
+ return this.getAll().map((tool) => tool.schema);
315
+ }
316
+ /**
317
+ * Check if tool exists
318
+ */
319
+ has(name) {
320
+ return this.tools.has(name);
321
+ }
322
+ /**
323
+ * Clear all tools
324
+ */
325
+ clear() {
326
+ const toolCount = this.tools.size;
327
+ this.tools.clear();
328
+ _robota_sdk_agent_core.logger.debug(`Cleared ${toolCount} tools from registry`);
329
+ }
330
+ /**
331
+ * Get tool names
332
+ */
333
+ getToolNames() {
334
+ return Array.from(this.tools.keys());
335
+ }
336
+ /**
337
+ * Get tools by pattern
338
+ */
339
+ getToolsByPattern(pattern) {
340
+ const regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
341
+ return this.getAll().filter((tool) => regex.test(tool.schema.name));
342
+ }
343
+ /**
344
+ * Get tool count
345
+ */
346
+ size() {
347
+ return this.tools.size;
348
+ }
349
+ /**
350
+ * Validate tool schema
351
+ */
352
+ validateToolSchema(schema) {
353
+ if (!schema.name || typeof schema.name !== "string") throw new _robota_sdk_agent_core.ValidationError("Tool schema must have a valid name");
354
+ if (!schema.description || typeof schema.description !== "string") throw new _robota_sdk_agent_core.ValidationError("Tool schema must have a description");
355
+ if (!schema.parameters || typeof schema.parameters !== "object" || schema.parameters === null || Array.isArray(schema.parameters)) throw new _robota_sdk_agent_core.ValidationError("Tool schema must have parameters object");
356
+ if (schema.parameters.type !== "object") throw new _robota_sdk_agent_core.ValidationError("Tool parameters type must be \"object\"");
357
+ if (schema.parameters.properties) for (const propName of Object.keys(schema.parameters.properties)) {
358
+ const propSchema = schema.parameters.properties[propName];
359
+ if (!propSchema?.type) throw new _robota_sdk_agent_core.ValidationError(`Parameter "${propName}" must have a type`);
360
+ if (![
361
+ "string",
362
+ "number",
363
+ "boolean",
364
+ "array",
365
+ "object"
366
+ ].includes(propSchema.type)) throw new _robota_sdk_agent_core.ValidationError(`Parameter "${propName}" has invalid type "${propSchema.type}"`);
367
+ }
368
+ if (schema.parameters.required) {
369
+ const properties = schema.parameters.properties || {};
370
+ for (const requiredField of schema.parameters.required) if (!properties[requiredField]) throw new _robota_sdk_agent_core.ValidationError(`Required parameter "${requiredField}" is not defined in properties`);
371
+ }
372
+ }
416
373
  };
417
-
418
- // src/implementations/function-tool/schema-converter.ts
374
+ //#endregion
375
+ //#region src/implementations/function-tool/schema-converter.ts
376
+ /**
377
+ * Convert Zod schema to JSON Schema format with safe undefined handling
378
+ */
419
379
  function zodToJsonSchema(schema, options = {}) {
420
- const properties = {};
421
- const required = [];
422
- const schemaDef = schema._def;
423
- if (!schemaDef) {
424
- throw new Error("Zod schema is missing _def; cannot convert to JSON schema.");
425
- }
426
- if (schemaDef.typeName === "ZodObject" && schemaDef.shape) {
427
- const shape = typeof schemaDef.shape === "function" ? schemaDef.shape() : schemaDef.shape;
428
- for (const [key, typeObj] of Object.entries(shape)) {
429
- const property = convertZodTypeToProperty(typeObj);
430
- properties[key] = property;
431
- if (isRequiredField(typeObj)) {
432
- required.push(key);
433
- }
434
- }
435
- }
436
- return {
437
- type: "object",
438
- properties,
439
- required,
440
- ...(options.allowAdditionalProperties || schemaDef.unknownKeys === "passthrough") && {
441
- additionalProperties: true
442
- }
443
- };
380
+ const properties = {};
381
+ const required = [];
382
+ const schemaDef = schema._def;
383
+ if (!schemaDef) throw new Error("Zod schema is missing _def; cannot convert to JSON schema.");
384
+ if (schemaDef.typeName === "ZodObject" && schemaDef.shape) {
385
+ const shape = typeof schemaDef.shape === "function" ? schemaDef.shape() : schemaDef.shape;
386
+ for (const [key, typeObj] of Object.entries(shape)) {
387
+ properties[key] = convertZodTypeToProperty(typeObj);
388
+ if (isRequiredField(typeObj)) required.push(key);
389
+ }
390
+ }
391
+ return {
392
+ type: "object",
393
+ properties,
394
+ required,
395
+ ...(options.allowAdditionalProperties || schemaDef.unknownKeys === "passthrough") && { additionalProperties: true }
396
+ };
444
397
  }
398
+ /**
399
+ * Convert individual Zod type to parameter schema with safe undefined handling
400
+ */
445
401
  function convertZodTypeToProperty(typeObj) {
446
- const typeDef = typeObj._def;
447
- if (!typeDef) {
448
- throw new Error("Zod type is missing _def; cannot convert to JSON schema.");
449
- }
450
- const base = {};
451
- if (typeDef.description) {
452
- base.description = typeDef.description;
453
- }
454
- switch (typeDef.typeName) {
455
- case "ZodString":
456
- return { type: "string", ...base };
457
- case "ZodNumber":
458
- return { type: "number", ...base };
459
- case "ZodBoolean":
460
- return { type: "boolean", ...base };
461
- case "ZodArray": {
462
- if (!typeDef.type) {
463
- throw new Error("ZodArray is missing item type; cannot convert to JSON schema.");
464
- }
465
- const arrayItems = convertZodTypeToProperty(typeDef.type);
466
- return {
467
- type: "array",
468
- items: arrayItems,
469
- ...base
470
- };
471
- }
472
- case "ZodObject":
473
- return { type: "object", ...base };
474
- case "ZodEnum": {
475
- const enumValues = typeDef.values;
476
- if (!enumValues || !Array.isArray(enumValues)) {
477
- throw new Error("ZodEnum is missing enum values; cannot convert to JSON schema.");
478
- }
479
- return {
480
- type: "string",
481
- enum: enumValues,
482
- ...base
483
- };
484
- }
485
- case "ZodOptional":
486
- if (typeDef.innerType) {
487
- const innerProperty = convertZodTypeToProperty(typeDef.innerType);
488
- return { ...innerProperty, ...base };
489
- }
490
- throw new Error("ZodOptional is missing innerType; cannot convert to JSON schema.");
491
- case "ZodNullable":
492
- if (typeDef.innerType) {
493
- const innerProperty = convertZodTypeToProperty(typeDef.innerType);
494
- return { ...innerProperty, ...base };
495
- }
496
- throw new Error("ZodNullable is missing innerType; cannot convert to JSON schema.");
497
- case "ZodDefault":
498
- if (typeDef.innerType) {
499
- const innerProperty = convertZodTypeToProperty(typeDef.innerType);
500
- return { ...innerProperty, ...base };
501
- }
502
- throw new Error("ZodDefault is missing innerType; cannot convert to JSON schema.");
503
- case "ZodRecord":
504
- if (typeDef.valueType) {
505
- const valueProperty = convertZodTypeToProperty(typeDef.valueType);
506
- return { type: "object", additionalProperties: valueProperty, ...base };
507
- }
508
- return { type: "object", additionalProperties: { type: "string" }, ...base };
509
- default:
510
- throw new Error(`Unsupported Zod type: ${String(typeDef.typeName)}`);
511
- }
402
+ const typeDef = typeObj._def;
403
+ if (!typeDef) throw new Error("Zod type is missing _def; cannot convert to JSON schema.");
404
+ const base = {};
405
+ if (typeDef.description) base.description = typeDef.description;
406
+ switch (typeDef.typeName) {
407
+ case "ZodString": return {
408
+ type: "string",
409
+ ...base
410
+ };
411
+ case "ZodNumber": return {
412
+ type: "number",
413
+ ...base
414
+ };
415
+ case "ZodBoolean": return {
416
+ type: "boolean",
417
+ ...base
418
+ };
419
+ case "ZodArray":
420
+ if (!typeDef.type) throw new Error("ZodArray is missing item type; cannot convert to JSON schema.");
421
+ return {
422
+ type: "array",
423
+ items: convertZodTypeToProperty(typeDef.type),
424
+ ...base
425
+ };
426
+ case "ZodObject": return {
427
+ type: "object",
428
+ ...base
429
+ };
430
+ case "ZodEnum": {
431
+ const enumValues = typeDef.values;
432
+ if (!enumValues || !Array.isArray(enumValues)) throw new Error("ZodEnum is missing enum values; cannot convert to JSON schema.");
433
+ return {
434
+ type: "string",
435
+ enum: enumValues,
436
+ ...base
437
+ };
438
+ }
439
+ case "ZodOptional":
440
+ if (typeDef.innerType) return {
441
+ ...convertZodTypeToProperty(typeDef.innerType),
442
+ ...base
443
+ };
444
+ throw new Error("ZodOptional is missing innerType; cannot convert to JSON schema.");
445
+ case "ZodNullable":
446
+ if (typeDef.innerType) return {
447
+ ...convertZodTypeToProperty(typeDef.innerType),
448
+ ...base
449
+ };
450
+ throw new Error("ZodNullable is missing innerType; cannot convert to JSON schema.");
451
+ case "ZodDefault":
452
+ if (typeDef.innerType) return {
453
+ ...convertZodTypeToProperty(typeDef.innerType),
454
+ ...base
455
+ };
456
+ throw new Error("ZodDefault is missing innerType; cannot convert to JSON schema.");
457
+ case "ZodRecord":
458
+ if (typeDef.valueType) return {
459
+ type: "object",
460
+ additionalProperties: convertZodTypeToProperty(typeDef.valueType),
461
+ ...base
462
+ };
463
+ return {
464
+ type: "object",
465
+ additionalProperties: { type: "string" },
466
+ ...base
467
+ };
468
+ default: throw new Error(`Unsupported Zod type: ${String(typeDef.typeName)}`);
469
+ }
512
470
  }
471
+ /**
472
+ * Check if a Zod field is required (not optional or nullable)
473
+ */
513
474
  function isRequiredField(typeObj) {
514
- const typeDef = typeObj._def;
515
- if (!typeDef) {
516
- throw new Error("Zod schema is missing _def; cannot determine required fields.");
517
- }
518
- return typeDef.typeName !== "ZodOptional" && typeDef.typeName !== "ZodNullable" && typeDef.typeName !== "ZodDefault";
475
+ const typeDef = typeObj._def;
476
+ if (!typeDef) throw new Error("Zod schema is missing _def; cannot determine required fields.");
477
+ return typeDef.typeName !== "ZodOptional" && typeDef.typeName !== "ZodNullable" && typeDef.typeName !== "ZodDefault";
519
478
  }
520
-
521
- // src/implementations/function-tool/parameter-validator.ts
479
+ //#endregion
480
+ //#region src/implementations/function-tool/parameter-validator.ts
481
+ /**
482
+ * Validate individual parameter type against its schema.
483
+ * Returns an error string if invalid, undefined if valid.
484
+ */
522
485
  function validateParameterType(key, value, schema) {
523
- const expectedType = schema["type"];
524
- switch (expectedType) {
525
- case "string":
526
- if (typeof value !== "string") {
527
- return `Parameter "${key}" must be a string, got ${typeof value}`;
528
- }
529
- break;
530
- case "number":
531
- if (typeof value !== "number" || isNaN(value)) {
532
- return `Parameter "${key}" must be a number, got ${typeof value}`;
533
- }
534
- break;
535
- case "boolean":
536
- if (typeof value !== "boolean") {
537
- return `Parameter "${key}" must be a boolean, got ${typeof value}`;
538
- }
539
- break;
540
- case "array":
541
- if (!Array.isArray(value)) {
542
- return `Parameter "${key}" must be an array, got ${typeof value}`;
543
- }
544
- if (schema.items) {
545
- for (let i = 0; i < value.length; i++) {
546
- const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);
547
- if (itemError) {
548
- return itemError;
549
- }
550
- }
551
- }
552
- break;
553
- case "object":
554
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
555
- return `Parameter "${key}" must be an object, got ${typeof value}`;
556
- }
557
- break;
558
- }
559
- if (schema.enum && schema.enum.length > 0) {
560
- const enumValues = schema.enum;
561
- let isValidEnum = false;
562
- for (const enumValue of enumValues) {
563
- if (value === enumValue) {
564
- isValidEnum = true;
565
- break;
566
- }
567
- }
568
- if (!isValidEnum) {
569
- return `Parameter "${key}" must be one of: ${enumValues.join(", ")}, got ${value}`;
570
- }
571
- }
572
- return void 0;
486
+ switch (schema["type"]) {
487
+ case "string":
488
+ if (typeof value !== "string") return `Parameter "${key}" must be a string, got ${typeof value}`;
489
+ break;
490
+ case "number":
491
+ if (typeof value !== "number" || isNaN(value)) return `Parameter "${key}" must be a number, got ${typeof value}`;
492
+ break;
493
+ case "boolean":
494
+ if (typeof value !== "boolean") return `Parameter "${key}" must be a boolean, got ${typeof value}`;
495
+ break;
496
+ case "array":
497
+ if (!Array.isArray(value)) return `Parameter "${key}" must be an array, got ${typeof value}`;
498
+ if (schema.items) for (let i = 0; i < value.length; i++) {
499
+ const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);
500
+ if (itemError) return itemError;
501
+ }
502
+ break;
503
+ case "object":
504
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return `Parameter "${key}" must be an object, got ${typeof value}`;
505
+ break;
506
+ }
507
+ if (schema.enum && schema.enum.length > 0) {
508
+ const enumValues = schema.enum;
509
+ let isValidEnum = false;
510
+ for (const enumValue of enumValues) if (value === enumValue) {
511
+ isValidEnum = true;
512
+ break;
513
+ }
514
+ if (!isValidEnum) return `Parameter "${key}" must be one of: ${enumValues.join(", ")}, got ${value}`;
515
+ }
573
516
  }
517
+ /**
518
+ * Collect all validation errors for the given parameters against a schema.
519
+ */
574
520
  function getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties) {
575
- const errors = [];
576
- for (const field of schemaRequired) {
577
- if (!(field in parameters)) {
578
- errors.push(`Missing required parameter: ${field}`);
579
- }
580
- }
581
- for (const [key, value] of Object.entries(parameters)) {
582
- const paramSchema = schemaProperties[key];
583
- if (!paramSchema) {
584
- if (additionalProperties === true) {
585
- continue;
586
- }
587
- if (additionalProperties && typeof additionalProperties === "object") {
588
- const additionalTypeError = validateParameterType(key, value, additionalProperties);
589
- if (additionalTypeError) errors.push(additionalTypeError);
590
- continue;
591
- }
592
- errors.push(`Unknown parameter: ${key}`);
593
- continue;
594
- }
595
- const typeError = validateParameterType(key, value, paramSchema);
596
- if (typeError) {
597
- errors.push(typeError);
598
- }
599
- }
600
- return errors;
521
+ const errors = [];
522
+ for (const field of schemaRequired) if (!(field in parameters)) errors.push(`Missing required parameter: ${field}`);
523
+ for (const [key, value] of Object.entries(parameters)) {
524
+ const paramSchema = schemaProperties[key];
525
+ if (!paramSchema) {
526
+ if (additionalProperties === true) continue;
527
+ if (additionalProperties && typeof additionalProperties === "object") {
528
+ const additionalTypeError = validateParameterType(key, value, additionalProperties);
529
+ if (additionalTypeError) errors.push(additionalTypeError);
530
+ continue;
531
+ }
532
+ errors.push(`Unknown parameter: ${key}`);
533
+ continue;
534
+ }
535
+ const typeError = validateParameterType(key, value, paramSchema);
536
+ if (typeError) errors.push(typeError);
537
+ }
538
+ return errors;
601
539
  }
540
+ /**
541
+ * Validate parameters and return a structured result.
542
+ */
602
543
  function validateToolParameters(parameters, schemaRequired, schemaProperties, additionalProperties) {
603
- const errors = getValidationErrors(
604
- parameters,
605
- schemaRequired,
606
- schemaProperties,
607
- additionalProperties
608
- );
609
- return {
610
- isValid: errors.length === 0,
611
- errors
612
- };
544
+ const errors = getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties);
545
+ return {
546
+ isValid: errors.length === 0,
547
+ errors
548
+ };
613
549
  }
614
-
615
- // src/implementations/function-tool.ts
550
+ //#endregion
551
+ //#region src/implementations/function-tool.ts
552
+ /**
553
+ * Function tool implementation
554
+ * Wraps a JavaScript function as a tool with schema validation
555
+ *
556
+ * Implements IFunctionTool without extending AbstractTool to avoid
557
+ * circular runtime dependency (tools → agents → tools).
558
+ */
616
559
  var FunctionTool = class {
617
- schema;
618
- fn;
619
- eventService;
620
- constructor(schema, fn) {
621
- this.schema = schema;
622
- this.fn = fn;
623
- this.validateConstructorInputs();
624
- }
625
- /**
626
- * Get tool name
627
- */
628
- getName() {
629
- return this.schema.name;
630
- }
631
- /**
632
- * Set EventService for post-construction injection.
633
- * Accepts EventService as-is without transformation.
634
- * Caller is responsible for providing properly configured EventService.
635
- */
636
- setEventService(eventService) {
637
- this.eventService = eventService;
638
- }
639
- /**
640
- * Execute the function tool
641
- */
642
- async execute(parameters, context) {
643
- const toolName = this.schema.name;
644
- if (!this.validate(parameters)) {
645
- const errors = getValidationErrors(
646
- parameters,
647
- this.schema.parameters.required || [],
648
- this.schema.parameters.properties || {},
649
- this.schema.parameters.additionalProperties
650
- );
651
- throw new agentCore.ValidationError(`Invalid parameters for tool "${toolName}": ${errors.join(", ")}`);
652
- }
653
- const startTime = Date.now();
654
- let result;
655
- try {
656
- result = await this.fn(parameters, context);
657
- } catch (error) {
658
- if (error instanceof agentCore.ToolExecutionError || error instanceof agentCore.ValidationError) {
659
- throw error;
660
- }
661
- throw new agentCore.ToolExecutionError(
662
- `Function tool execution failed: ${error instanceof Error ? error.message : String(error)}`,
663
- toolName,
664
- error instanceof Error ? error : new Error(String(error)),
665
- {
666
- parameterCount: Object.keys(parameters || {}).length,
667
- hasContext: !!context
668
- }
669
- );
670
- }
671
- const executionTime = Date.now() - startTime;
672
- return {
673
- success: true,
674
- data: result,
675
- metadata: {
676
- executionTime,
677
- toolName,
678
- parameters
679
- }
680
- };
681
- }
682
- /**
683
- * Validate parameters (simple boolean result)
684
- */
685
- validate(parameters) {
686
- return getValidationErrors(
687
- parameters,
688
- this.schema.parameters.required || [],
689
- this.schema.parameters.properties || {},
690
- this.schema.parameters.additionalProperties
691
- ).length === 0;
692
- }
693
- /**
694
- * Validate tool parameters with detailed result
695
- */
696
- validateParameters(parameters) {
697
- return validateToolParameters(
698
- parameters,
699
- this.schema.parameters.required || [],
700
- this.schema.parameters.properties || {},
701
- this.schema.parameters.additionalProperties
702
- );
703
- }
704
- /**
705
- * Get tool description
706
- */
707
- getDescription() {
708
- return this.schema.description;
709
- }
710
- /**
711
- * Validate constructor inputs
712
- */
713
- validateConstructorInputs() {
714
- if (!this.schema) {
715
- throw new agentCore.ValidationError("Tool schema is required");
716
- }
717
- if (!this.fn || typeof this.fn !== "function") {
718
- throw new agentCore.ValidationError("Tool function is required and must be a function");
719
- }
720
- if (!this.schema.name) {
721
- throw new agentCore.ValidationError("Tool schema must have a name");
722
- }
723
- }
560
+ schema;
561
+ fn;
562
+ eventService;
563
+ constructor(schema, fn) {
564
+ this.schema = schema;
565
+ this.fn = fn;
566
+ this.validateConstructorInputs();
567
+ }
568
+ /**
569
+ * Get tool name
570
+ */
571
+ getName() {
572
+ return this.schema.name;
573
+ }
574
+ /**
575
+ * Set EventService for post-construction injection.
576
+ * Accepts EventService as-is without transformation.
577
+ * Caller is responsible for providing properly configured EventService.
578
+ */
579
+ setEventService(eventService) {
580
+ this.eventService = eventService;
581
+ }
582
+ /**
583
+ * Execute the function tool
584
+ */
585
+ async execute(parameters, context) {
586
+ const toolName = this.schema.name;
587
+ if (!this.validate(parameters)) throw new _robota_sdk_agent_core.ValidationError(`Invalid parameters for tool "${toolName}": ${getValidationErrors(parameters, this.schema.parameters.required || [], this.schema.parameters.properties || {}, this.schema.parameters.additionalProperties).join(", ")}`);
588
+ const startTime = Date.now();
589
+ let result;
590
+ try {
591
+ result = await this.fn(parameters, context);
592
+ } catch (error) {
593
+ if (error instanceof _robota_sdk_agent_core.ToolExecutionError || error instanceof _robota_sdk_agent_core.ValidationError) throw error;
594
+ throw new _robota_sdk_agent_core.ToolExecutionError(`Function tool execution failed: ${error instanceof Error ? error.message : String(error)}`, toolName, error instanceof Error ? error : new Error(String(error)), {
595
+ parameterCount: Object.keys(parameters || {}).length,
596
+ hasContext: !!context
597
+ });
598
+ }
599
+ const executionTime = Date.now() - startTime;
600
+ return {
601
+ success: true,
602
+ data: result,
603
+ metadata: {
604
+ executionTime,
605
+ toolName,
606
+ parameters
607
+ }
608
+ };
609
+ }
610
+ /**
611
+ * Validate parameters (simple boolean result)
612
+ */
613
+ validate(parameters) {
614
+ return getValidationErrors(parameters, this.schema.parameters.required || [], this.schema.parameters.properties || {}, this.schema.parameters.additionalProperties).length === 0;
615
+ }
616
+ /**
617
+ * Validate tool parameters with detailed result
618
+ */
619
+ validateParameters(parameters) {
620
+ return validateToolParameters(parameters, this.schema.parameters.required || [], this.schema.parameters.properties || {}, this.schema.parameters.additionalProperties);
621
+ }
622
+ /**
623
+ * Get tool description
624
+ */
625
+ getDescription() {
626
+ return this.schema.description;
627
+ }
628
+ /**
629
+ * Validate constructor inputs
630
+ */
631
+ validateConstructorInputs() {
632
+ if (!this.schema) throw new _robota_sdk_agent_core.ValidationError("Tool schema is required");
633
+ if (!this.fn || typeof this.fn !== "function") throw new _robota_sdk_agent_core.ValidationError("Tool function is required and must be a function");
634
+ if (!this.schema.name) throw new _robota_sdk_agent_core.ValidationError("Tool schema must have a name");
635
+ }
724
636
  };
637
+ /**
638
+ * Helper function to create a function tool from a simple function
639
+ */
725
640
  function createFunctionTool(name, description, parameters, fn) {
726
- const schema = {
727
- name,
728
- description,
729
- parameters
730
- };
731
- return new FunctionTool(schema, fn);
641
+ return new FunctionTool({
642
+ name,
643
+ description,
644
+ parameters
645
+ }, fn);
732
646
  }
647
+ /**
648
+ * Helper function to create a function tool from Zod schema
649
+ */
733
650
  function createZodFunctionTool(name, description, zodSchema, fn) {
734
- const parameters = zodToJsonSchema(zodSchema);
735
- const schema = {
736
- name,
737
- description,
738
- parameters
739
- };
740
- const wrappedFn = async (parameters2, context) => {
741
- const parseResult = zodSchema.safeParse(parameters2);
742
- if (!parseResult.success) {
743
- throw new agentCore.ValidationError(`Zod validation failed: ${parseResult.error}`);
744
- }
745
- const result = await fn(parseResult.data || parameters2, context);
746
- return typeof result === "string" ? result : JSON.stringify(result);
747
- };
748
- return new FunctionTool(schema, wrappedFn);
651
+ const schema = {
652
+ name,
653
+ description,
654
+ parameters: zodToJsonSchema(zodSchema)
655
+ };
656
+ const wrappedFn = async (parameters, context) => {
657
+ const parseResult = zodSchema.safeParse(parameters);
658
+ if (!parseResult.success) throw new _robota_sdk_agent_core.ValidationError(`Zod validation failed: ${parseResult.error}`);
659
+ const result = await fn(parseResult.data || parameters, context);
660
+ return typeof result === "string" ? result : JSON.stringify(result);
661
+ };
662
+ return new FunctionTool(schema, wrappedFn);
749
663
  }
750
-
751
- // src/implementations/openapi-schema-converter.ts
752
- var HTTP_METHODS = [
753
- "get",
754
- "post",
755
- "put",
756
- "delete",
757
- "patch",
758
- "head",
759
- "options"
664
+ //#endregion
665
+ //#region src/implementations/openapi-schema-converter.ts
666
+ /**
667
+ * HTTP methods to search when scanning OpenAPI paths
668
+ */
669
+ const HTTP_METHODS = [
670
+ "get",
671
+ "post",
672
+ "put",
673
+ "delete",
674
+ "patch",
675
+ "head",
676
+ "options"
760
677
  ];
678
+ /**
679
+ * Find an operation in the OpenAPI spec by operationId
680
+ */
761
681
  function findOperation(apiSpec, operationId) {
762
- for (const [path, pathItem] of Object.entries(apiSpec.paths || {})) {
763
- if (!pathItem) continue;
764
- for (const method of HTTP_METHODS) {
765
- const operation = pathItem[method];
766
- if (operation?.operationId === operationId) {
767
- return { method, path, operation };
768
- }
769
- }
770
- }
771
- return void 0;
682
+ for (const [path, pathItem] of Object.entries(apiSpec.paths || {})) {
683
+ if (!pathItem) continue;
684
+ for (const method of HTTP_METHODS) {
685
+ const operation = pathItem[method];
686
+ if (operation?.operationId === operationId) return {
687
+ method,
688
+ path,
689
+ operation
690
+ };
691
+ }
692
+ }
772
693
  }
694
+ /**
695
+ * Map OpenAPI type to JSON schema type
696
+ */
773
697
  function mapOpenAPIType(type) {
774
- switch (type) {
775
- case "string":
776
- return "string";
777
- case "number":
778
- return "number";
779
- case "integer":
780
- return "integer";
781
- case "boolean":
782
- return "boolean";
783
- case "array":
784
- return "array";
785
- case "object":
786
- return "object";
787
- default:
788
- return "string";
789
- }
698
+ switch (type) {
699
+ case "string": return "string";
700
+ case "number": return "number";
701
+ case "integer": return "integer";
702
+ case "boolean": return "boolean";
703
+ case "array": return "array";
704
+ case "object": return "object";
705
+ default: return "string";
706
+ }
790
707
  }
708
+ /**
709
+ * Convert OpenAPI schema to parameter schema
710
+ */
791
711
  function convertOpenAPISchemaToParameterSchema(schema) {
792
- if ("$ref" in schema) {
793
- return { type: "object" };
794
- }
795
- const result = {
796
- type: mapOpenAPIType(schema.type)
797
- };
798
- if (schema.description) {
799
- result.description = schema.description;
800
- }
801
- if (schema.enum) {
802
- result.enum = schema.enum;
803
- }
804
- if (schema.minimum !== void 0) {
805
- result.minimum = schema.minimum;
806
- }
807
- if (schema.maximum !== void 0) {
808
- result.maximum = schema.maximum;
809
- }
810
- if (schema.pattern) {
811
- result.pattern = schema.pattern;
812
- }
813
- if (schema.format) {
814
- result.format = schema.format;
815
- }
816
- if (schema.default !== void 0) {
817
- result.default = schema.default;
818
- }
819
- if (schema.type === "array" && schema.items) {
820
- result.items = convertOpenAPISchemaToParameterSchema(schema.items);
821
- }
822
- if (schema.type === "object" && schema.properties) {
823
- result.properties = {};
824
- for (const [propName, propSchema] of Object.entries(schema.properties)) {
825
- result.properties[propName] = convertOpenAPISchemaToParameterSchema(propSchema);
826
- }
827
- if (schema.required && schema.required.length > 0) {
828
- result.required = schema.required;
829
- }
830
- }
831
- return result;
712
+ if ("$ref" in schema) return { type: "object" };
713
+ const result = { type: mapOpenAPIType(schema.type) };
714
+ if (schema.description) result.description = schema.description;
715
+ if (schema.enum) result.enum = schema.enum;
716
+ if (schema.minimum !== void 0) result.minimum = schema.minimum;
717
+ if (schema.maximum !== void 0) result.maximum = schema.maximum;
718
+ if (schema.pattern) result.pattern = schema.pattern;
719
+ if (schema.format) result.format = schema.format;
720
+ if (schema.default !== void 0) result.default = schema.default;
721
+ if (schema.type === "array" && schema.items) result.items = convertOpenAPISchemaToParameterSchema(schema.items);
722
+ if (schema.type === "object" && schema.properties) {
723
+ result.properties = {};
724
+ for (const [propName, propSchema] of Object.entries(schema.properties)) result.properties[propName] = convertOpenAPISchemaToParameterSchema(propSchema);
725
+ if (schema.required && schema.required.length > 0) result.required = schema.required;
726
+ }
727
+ return result;
832
728
  }
729
+ /**
730
+ * Convert OpenAPI parameter object to tool parameter schema
731
+ */
833
732
  function convertOpenAPIParamToSchema(param) {
834
- const schema = param.schema;
835
- return convertOpenAPISchemaToParameterSchema(schema);
733
+ const schema = param.schema;
734
+ return convertOpenAPISchemaToParameterSchema(schema);
836
735
  }
736
+ /**
737
+ * Create a tool schema from an OpenAPI operation specification
738
+ */
837
739
  function createSchemaFromOperation(operationId, opSpec) {
838
- const properties = {};
839
- const required = [];
840
- const params = opSpec.parameters || [];
841
- for (const param of params) {
842
- properties[param.name] = convertOpenAPIParamToSchema(param);
843
- if (param.required) {
844
- required.push(param.name);
845
- }
846
- }
847
- if (opSpec.requestBody) {
848
- const requestBody = opSpec.requestBody;
849
- const jsonContent = requestBody.content?.["application/json"];
850
- if (jsonContent?.schema) {
851
- const bodySchema = convertOpenAPISchemaToParameterSchema(jsonContent.schema);
852
- if (bodySchema.type === "object" && bodySchema.properties) {
853
- Object.assign(properties, bodySchema.properties);
854
- const schemaWithRequired = bodySchema;
855
- if (schemaWithRequired.required) {
856
- required.push(...schemaWithRequired.required);
857
- }
858
- }
859
- }
860
- }
861
- const schemaParams = {
862
- type: "object",
863
- properties
864
- };
865
- if (required.length > 0) {
866
- schemaParams.required = required;
867
- }
868
- return {
869
- name: operationId,
870
- description: opSpec.summary || opSpec.description || `OpenAPI operation: ${operationId}`,
871
- parameters: schemaParams
872
- };
740
+ const properties = {};
741
+ const required = [];
742
+ const params = opSpec.parameters || [];
743
+ for (const param of params) {
744
+ properties[param.name] = convertOpenAPIParamToSchema(param);
745
+ if (param.required) required.push(param.name);
746
+ }
747
+ if (opSpec.requestBody) {
748
+ const jsonContent = opSpec.requestBody.content?.["application/json"];
749
+ if (jsonContent?.schema) {
750
+ const bodySchema = convertOpenAPISchemaToParameterSchema(jsonContent.schema);
751
+ if (bodySchema.type === "object" && bodySchema.properties) {
752
+ Object.assign(properties, bodySchema.properties);
753
+ const schemaWithRequired = bodySchema;
754
+ if (schemaWithRequired.required) required.push(...schemaWithRequired.required);
755
+ }
756
+ }
757
+ }
758
+ const schemaParams = {
759
+ type: "object",
760
+ properties
761
+ };
762
+ if (required.length > 0) schemaParams.required = required;
763
+ return {
764
+ name: operationId,
765
+ description: opSpec.summary || opSpec.description || `OpenAPI operation: ${operationId}`,
766
+ parameters: schemaParams
767
+ };
873
768
  }
874
-
875
- // src/implementations/openapi-tool.ts
769
+ //#endregion
770
+ //#region src/implementations/openapi-tool.ts
771
+ /**
772
+ * OpenAPI tool implementation
773
+ * Executes API calls based on OpenAPI 3.0 specifications
774
+ *
775
+ * Implements ITool without extending AbstractTool to avoid
776
+ * circular runtime dependency (tools → agents → tools).
777
+ */
876
778
  var OpenAPITool = class {
877
- schema;
878
- apiSpec;
879
- operationId;
880
- baseURL;
881
- config;
882
- eventService;
883
- constructor(config) {
884
- this.config = config;
885
- if (typeof config.spec !== "object" || config.spec === null || typeof config.spec.openapi !== "string" || typeof config.spec.paths !== "object") {
886
- throw new Error(
887
- 'Invalid OpenAPI spec: must contain "openapi" (string) and "paths" (object) fields'
888
- );
889
- }
890
- this.apiSpec = config.spec;
891
- this.operationId = config.operationId;
892
- this.baseURL = config.baseURL;
893
- this.schema = this.createSchemaFromOpenAPI();
894
- }
895
- /**
896
- * Execute the OpenAPI tool
897
- */
898
- async execute(parameters, context) {
899
- const toolName = this.schema.name;
900
- const validation = this.validateParameters(parameters);
901
- if (!validation.isValid) {
902
- throw new agentCore.ValidationError(
903
- `Invalid parameters for OpenAPI tool "${toolName}": ${validation.errors.join(", ")}`
904
- );
905
- }
906
- try {
907
- const startTime = Date.now();
908
- const result = await this.executeAPICall(parameters, context);
909
- const executionTime = Date.now() - startTime;
910
- return {
911
- success: true,
912
- data: result,
913
- metadata: {
914
- executionTime,
915
- toolName,
916
- operationId: this.operationId,
917
- baseURL: this.baseURL
918
- }
919
- };
920
- } catch (error) {
921
- if (error instanceof agentCore.ToolExecutionError || error instanceof agentCore.ValidationError) {
922
- throw error;
923
- }
924
- const safeError = error instanceof Error ? error : new Error(String(error));
925
- throw new agentCore.ToolExecutionError(
926
- `OpenAPI tool execution failed: ${safeError.message}`,
927
- toolName,
928
- safeError,
929
- {
930
- operationId: this.operationId,
931
- baseURL: this.baseURL,
932
- parametersCount: Object.keys(parameters).length
933
- }
934
- );
935
- }
936
- }
937
- /**
938
- * Validate tool parameters
939
- */
940
- validate(parameters) {
941
- return this.validateParameters(parameters).isValid;
942
- }
943
- /**
944
- * Validate tool parameters with detailed result
945
- */
946
- validateParameters(parameters) {
947
- const required = this.schema.parameters.required || [];
948
- const errors = [];
949
- for (const field of required) {
950
- if (!(field in parameters)) {
951
- errors.push(`Missing required parameter: ${field}`);
952
- }
953
- }
954
- return {
955
- isValid: errors.length === 0,
956
- errors
957
- };
958
- }
959
- /**
960
- * Get tool name
961
- */
962
- getName() {
963
- return this.schema.name;
964
- }
965
- /**
966
- * Set EventService for post-construction injection.
967
- */
968
- setEventService(eventService) {
969
- this.eventService = eventService;
970
- }
971
- /**
972
- * Get tool description
973
- */
974
- getDescription() {
975
- return this.schema.description;
976
- }
977
- /**
978
- * Execute the actual API call
979
- * @private
980
- */
981
- async executeAPICall(parameters, _context) {
982
- const operation = findOperation(this.apiSpec, this.operationId);
983
- if (!operation) {
984
- throw new Error(`Operation ${this.operationId} not found in OpenAPI spec`);
985
- }
986
- this.buildRequestConfig(operation, parameters);
987
- throw new Error("Not implemented: actual API execution is not yet available");
988
- }
989
- /**
990
- * Build HTTP request configuration from OpenAPI operation and parameters
991
- */
992
- buildRequestConfig(opInfo, parameters) {
993
- const { method, path, operation } = opInfo;
994
- let url = this.baseURL + path;
995
- const headers = {};
996
- let body;
997
- const params = operation.parameters || [];
998
- for (const param of params) {
999
- const value = parameters[param.name];
1000
- if (value === void 0 && param.required) {
1001
- throw new Error(`Required parameter ${param.name} is missing`);
1002
- }
1003
- if (value !== void 0) {
1004
- switch (param.in) {
1005
- case "path":
1006
- url = url.replace(`{${param.name}}`, encodeURIComponent(String(value)));
1007
- break;
1008
- case "query": {
1009
- const separator = url.includes("?") ? "&" : "?";
1010
- url += `${separator}${param.name}=${encodeURIComponent(String(value))}`;
1011
- break;
1012
- }
1013
- case "header":
1014
- headers[param.name] = String(value);
1015
- break;
1016
- }
1017
- }
1018
- }
1019
- if (["post", "put", "patch"].includes(method) && operation.requestBody) {
1020
- const requestBody = operation.requestBody;
1021
- const jsonContent = requestBody.content?.["application/json"];
1022
- if (jsonContent) {
1023
- headers["Content-Type"] = "application/json";
1024
- const bodyParams = {};
1025
- for (const [key, value] of Object.entries(parameters)) {
1026
- const isParamUsed = params.some((p) => p.name === key);
1027
- if (!isParamUsed) {
1028
- bodyParams[key] = value;
1029
- }
1030
- }
1031
- body = JSON.stringify(bodyParams);
1032
- }
1033
- }
1034
- if (this.config.auth) {
1035
- switch (this.config.auth.type) {
1036
- case "bearer":
1037
- headers["Authorization"] = `Bearer ${this.config.auth.token}`;
1038
- break;
1039
- case "apiKey": {
1040
- const headerName = this.config.auth.header || "X-API-Key";
1041
- headers[headerName] = this.config.auth.apiKey || "";
1042
- break;
1043
- }
1044
- }
1045
- }
1046
- const result = {
1047
- method,
1048
- url,
1049
- headers
1050
- };
1051
- if (body !== void 0) {
1052
- result.body = body;
1053
- }
1054
- return result;
1055
- }
1056
- /**
1057
- * Create tool schema from OpenAPI operation specification
1058
- */
1059
- createSchemaFromOpenAPI() {
1060
- const operation = findOperation(this.apiSpec, this.operationId);
1061
- if (!operation) {
1062
- throw new Error(
1063
- `[STRICT-POLICY][EMITTER-CONTRACT] OpenAPI operation not found: ${this.operationId}. Emitter contract must provide a valid operationId present in the OpenAPI document.`
1064
- );
1065
- }
1066
- return createSchemaFromOperation(this.operationId, operation.operation);
1067
- }
779
+ schema;
780
+ apiSpec;
781
+ operationId;
782
+ baseURL;
783
+ config;
784
+ eventService;
785
+ constructor(config) {
786
+ this.config = config;
787
+ if (typeof config.spec !== "object" || config.spec === null || typeof config.spec.openapi !== "string" || typeof config.spec.paths !== "object") throw new Error("Invalid OpenAPI spec: must contain \"openapi\" (string) and \"paths\" (object) fields");
788
+ this.apiSpec = config.spec;
789
+ this.operationId = config.operationId;
790
+ this.baseURL = config.baseURL;
791
+ this.schema = this.createSchemaFromOpenAPI();
792
+ }
793
+ /**
794
+ * Execute the OpenAPI tool
795
+ */
796
+ async execute(parameters, context) {
797
+ const toolName = this.schema.name;
798
+ const validation = this.validateParameters(parameters);
799
+ if (!validation.isValid) throw new _robota_sdk_agent_core.ValidationError(`Invalid parameters for OpenAPI tool "${toolName}": ${validation.errors.join(", ")}`);
800
+ try {
801
+ const startTime = Date.now();
802
+ return {
803
+ success: true,
804
+ data: await this.executeAPICall(parameters, context),
805
+ metadata: {
806
+ executionTime: Date.now() - startTime,
807
+ toolName,
808
+ operationId: this.operationId,
809
+ baseURL: this.baseURL
810
+ }
811
+ };
812
+ } catch (error) {
813
+ if (error instanceof _robota_sdk_agent_core.ToolExecutionError || error instanceof _robota_sdk_agent_core.ValidationError) throw error;
814
+ const safeError = error instanceof Error ? error : new Error(String(error));
815
+ throw new _robota_sdk_agent_core.ToolExecutionError(`OpenAPI tool execution failed: ${safeError.message}`, toolName, safeError, {
816
+ operationId: this.operationId,
817
+ baseURL: this.baseURL,
818
+ parametersCount: Object.keys(parameters).length
819
+ });
820
+ }
821
+ }
822
+ /**
823
+ * Validate tool parameters
824
+ */
825
+ validate(parameters) {
826
+ return this.validateParameters(parameters).isValid;
827
+ }
828
+ /**
829
+ * Validate tool parameters with detailed result
830
+ */
831
+ validateParameters(parameters) {
832
+ const required = this.schema.parameters.required || [];
833
+ const errors = [];
834
+ for (const field of required) if (!(field in parameters)) errors.push(`Missing required parameter: ${field}`);
835
+ return {
836
+ isValid: errors.length === 0,
837
+ errors
838
+ };
839
+ }
840
+ /**
841
+ * Get tool name
842
+ */
843
+ getName() {
844
+ return this.schema.name;
845
+ }
846
+ /**
847
+ * Set EventService for post-construction injection.
848
+ */
849
+ setEventService(eventService) {
850
+ this.eventService = eventService;
851
+ }
852
+ /**
853
+ * Get tool description
854
+ */
855
+ getDescription() {
856
+ return this.schema.description;
857
+ }
858
+ /**
859
+ * Execute the actual API call
860
+ * @private
861
+ */
862
+ async executeAPICall(parameters, _context) {
863
+ const operation = findOperation(this.apiSpec, this.operationId);
864
+ if (!operation) throw new Error(`Operation ${this.operationId} not found in OpenAPI spec`);
865
+ this.buildRequestConfig(operation, parameters);
866
+ throw new Error("Not implemented: actual API execution is not yet available");
867
+ }
868
+ /**
869
+ * Build HTTP request configuration from OpenAPI operation and parameters
870
+ */
871
+ buildRequestConfig(opInfo, parameters) {
872
+ const { method, path, operation } = opInfo;
873
+ let url = this.baseURL + path;
874
+ const headers = {};
875
+ let body;
876
+ const params = operation.parameters || [];
877
+ for (const param of params) {
878
+ const value = parameters[param.name];
879
+ if (value === void 0 && param.required) throw new Error(`Required parameter ${param.name} is missing`);
880
+ if (value !== void 0) switch (param.in) {
881
+ case "path":
882
+ url = url.replace(`{${param.name}}`, encodeURIComponent(String(value)));
883
+ break;
884
+ case "query": {
885
+ const separator = url.includes("?") ? "&" : "?";
886
+ url += `${separator}${param.name}=${encodeURIComponent(String(value))}`;
887
+ break;
888
+ }
889
+ case "header":
890
+ headers[param.name] = String(value);
891
+ break;
892
+ }
893
+ }
894
+ if ([
895
+ "post",
896
+ "put",
897
+ "patch"
898
+ ].includes(method) && operation.requestBody) {
899
+ if (operation.requestBody.content?.["application/json"]) {
900
+ headers["Content-Type"] = "application/json";
901
+ const bodyParams = {};
902
+ for (const [key, value] of Object.entries(parameters)) if (!params.some((p) => p.name === key)) bodyParams[key] = value;
903
+ body = JSON.stringify(bodyParams);
904
+ }
905
+ }
906
+ if (this.config.auth) switch (this.config.auth.type) {
907
+ case "bearer":
908
+ headers["Authorization"] = `Bearer ${this.config.auth.token}`;
909
+ break;
910
+ case "apiKey": {
911
+ const headerName = this.config.auth.header || "X-API-Key";
912
+ headers[headerName] = this.config.auth.apiKey || "";
913
+ break;
914
+ }
915
+ }
916
+ const result = {
917
+ method,
918
+ url,
919
+ headers
920
+ };
921
+ if (body !== void 0) result.body = body;
922
+ return result;
923
+ }
924
+ /**
925
+ * Create tool schema from OpenAPI operation specification
926
+ */
927
+ createSchemaFromOpenAPI() {
928
+ const operation = findOperation(this.apiSpec, this.operationId);
929
+ if (!operation) throw new Error(`[STRICT-POLICY][EMITTER-CONTRACT] OpenAPI operation not found: ${this.operationId}. Emitter contract must provide a valid operationId present in the OpenAPI document.`);
930
+ return createSchemaFromOperation(this.operationId, operation.operation);
931
+ }
1068
932
  };
933
+ /**
934
+ * Factory function to create OpenAPI tools from specification
935
+ */
1069
936
  function createOpenAPITool(config) {
1070
- return new OpenAPITool(config);
937
+ return new OpenAPITool(config);
1071
938
  }
1072
- var DEFAULT_TIMEOUT_MS = 12e4;
1073
- var BashSchema = zod.z.object({
1074
- command: zod.z.string().describe("The bash command to execute"),
1075
- timeout: zod.z.number().optional().describe("Optional timeout in milliseconds (max 600000). Default is 120000 (2 minutes)"),
1076
- workingDirectory: zod.z.string().optional().describe("Working directory for the command. Defaults to the current working directory")
939
+ //#endregion
940
+ //#region src/builtins/bash-tool.ts
941
+ /**
942
+ * BashTool execute shell commands via child_process.spawn
943
+ *
944
+ * Returns TToolResult JSON string. Non-zero exit is returned as success:true
945
+ * with exitCode set, matching Claude Code behaviour (the command ran, it just
946
+ * exited non-zero — the LLM can decide what to do with that information).
947
+ */
948
+ const DEFAULT_TIMEOUT_MS$2 = 12e4;
949
+ const BashSchema = zod.z.object({
950
+ command: zod.z.string().describe("The bash command to execute"),
951
+ timeout: zod.z.number().optional().describe("Optional timeout in milliseconds (max 600000). Default is 120000 (2 minutes)"),
952
+ workingDirectory: zod.z.string().optional().describe("Working directory for the command. Defaults to the current working directory")
1077
953
  });
954
+ /**
955
+ * Run a shell command and return stdout + stderr.
956
+ * Resolves with the TToolResult JSON string.
957
+ */
1078
958
  async function runBash(args, options = {}) {
1079
- const { command, timeout = DEFAULT_TIMEOUT_MS, workingDirectory } = args;
1080
- if (options.sandboxClient) {
1081
- try {
1082
- const sandboxResult = await options.sandboxClient.run(command, {
1083
- timeoutMs: timeout,
1084
- workingDirectory
1085
- });
1086
- const output = sandboxResult.stderr ? `${sandboxResult.stdout}
1087
- stderr:
1088
- ${sandboxResult.stderr}` : sandboxResult.stdout;
1089
- const result = {
1090
- success: true,
1091
- output,
1092
- exitCode: sandboxResult.exitCode
1093
- };
1094
- return JSON.stringify(result);
1095
- } catch (err) {
1096
- const result = {
1097
- success: false,
1098
- output: "",
1099
- error: err instanceof Error ? err.message : String(err)
1100
- };
1101
- return JSON.stringify(result);
1102
- }
1103
- }
1104
- return new Promise((resolve4) => {
1105
- const stdoutChunks = [];
1106
- const stderrChunks = [];
1107
- let timedOut = false;
1108
- let settled = false;
1109
- const child = child_process.spawn("sh", ["-c", command], {
1110
- cwd: workingDirectory ?? process.cwd(),
1111
- env: process.env,
1112
- stdio: ["pipe", "pipe", "pipe"]
1113
- });
1114
- child.stdout.on("data", (chunk) => {
1115
- stdoutChunks.push(chunk);
1116
- });
1117
- child.stderr.on("data", (chunk) => {
1118
- stderrChunks.push(chunk);
1119
- });
1120
- const timer = setTimeout(() => {
1121
- timedOut = true;
1122
- child.kill("SIGTERM");
1123
- settle({
1124
- success: false,
1125
- output: Buffer.concat(stdoutChunks).toString("utf8"),
1126
- error: `Command timed out after ${timeout}ms`
1127
- });
1128
- }, timeout);
1129
- function settle(result) {
1130
- if (settled) return;
1131
- settled = true;
1132
- clearTimeout(timer);
1133
- resolve4(JSON.stringify(result));
1134
- }
1135
- child.on("error", (err) => {
1136
- settle({
1137
- success: false,
1138
- output: "",
1139
- error: err.message
1140
- });
1141
- });
1142
- child.on("close", (code) => {
1143
- if (timedOut) {
1144
- settle({
1145
- success: false,
1146
- output: Buffer.concat(stdoutChunks).toString("utf8"),
1147
- error: `Command timed out after ${timeout}ms`,
1148
- exitCode: code ?? void 0
1149
- });
1150
- return;
1151
- }
1152
- const stdout = Buffer.concat(stdoutChunks).toString("utf8");
1153
- const stderr = Buffer.concat(stderrChunks).toString("utf8");
1154
- const exitCode = code ?? 0;
1155
- const output = stderr ? `${stdout}
1156
- stderr:
1157
- ${stderr}` : stdout;
1158
- settle({
1159
- success: true,
1160
- output,
1161
- exitCode
1162
- });
1163
- });
1164
- });
959
+ const { command, timeout = DEFAULT_TIMEOUT_MS$2, workingDirectory } = args;
960
+ if (options.sandboxClient) try {
961
+ const sandboxResult = await options.sandboxClient.run(command, {
962
+ timeoutMs: timeout,
963
+ workingDirectory
964
+ });
965
+ const result = {
966
+ success: true,
967
+ output: sandboxResult.stderr ? `${sandboxResult.stdout}\nstderr:\n${sandboxResult.stderr}` : sandboxResult.stdout,
968
+ exitCode: sandboxResult.exitCode
969
+ };
970
+ return JSON.stringify(result);
971
+ } catch (err) {
972
+ const result = {
973
+ success: false,
974
+ output: "",
975
+ error: err instanceof Error ? err.message : String(err)
976
+ };
977
+ return JSON.stringify(result);
978
+ }
979
+ return new Promise((resolve) => {
980
+ const stdoutChunks = [];
981
+ const stderrChunks = [];
982
+ let timedOut = false;
983
+ let settled = false;
984
+ const child = (0, node_child_process.spawn)("sh", ["-c", command], {
985
+ cwd: workingDirectory ?? process.cwd(),
986
+ env: process.env,
987
+ stdio: [
988
+ "pipe",
989
+ "pipe",
990
+ "pipe"
991
+ ]
992
+ });
993
+ child.stdout.on("data", (chunk) => {
994
+ stdoutChunks.push(chunk);
995
+ });
996
+ child.stderr.on("data", (chunk) => {
997
+ stderrChunks.push(chunk);
998
+ });
999
+ const timer = setTimeout(() => {
1000
+ timedOut = true;
1001
+ child.kill("SIGTERM");
1002
+ settle({
1003
+ success: false,
1004
+ output: Buffer.concat(stdoutChunks).toString("utf8"),
1005
+ error: `Command timed out after ${timeout}ms`
1006
+ });
1007
+ }, timeout);
1008
+ function settle(result) {
1009
+ if (settled) return;
1010
+ settled = true;
1011
+ clearTimeout(timer);
1012
+ resolve(JSON.stringify(result));
1013
+ }
1014
+ child.on("error", (err) => {
1015
+ settle({
1016
+ success: false,
1017
+ output: "",
1018
+ error: err.message
1019
+ });
1020
+ });
1021
+ child.on("close", (code) => {
1022
+ if (timedOut) {
1023
+ settle({
1024
+ success: false,
1025
+ output: Buffer.concat(stdoutChunks).toString("utf8"),
1026
+ error: `Command timed out after ${timeout}ms`,
1027
+ exitCode: code ?? void 0
1028
+ });
1029
+ return;
1030
+ }
1031
+ const stdout = Buffer.concat(stdoutChunks).toString("utf8");
1032
+ const stderr = Buffer.concat(stderrChunks).toString("utf8");
1033
+ const exitCode = code ?? 0;
1034
+ settle({
1035
+ success: true,
1036
+ output: stderr ? `${stdout}\nstderr:\n${stderr}` : stdout,
1037
+ exitCode
1038
+ });
1039
+ });
1040
+ });
1165
1041
  }
1042
+ /**
1043
+ * Create a BashTool instance — register with Robota agent tools registry.
1044
+ */
1166
1045
  function createBashTool(options = {}) {
1167
- return createZodFunctionTool(
1168
- "Bash",
1169
- "Executes a given bash command and returns its output.\n\nThe working directory persists between commands, but shell state does not.\n\nIMPORTANT: Avoid using this tool to run `find`, `grep`, `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands. Instead, use the appropriate dedicated tool:\n - File search: Use Glob (NOT find or ls)\n - Content search: Use Grep (NOT grep or rg)\n - Read files: Use Read (NOT cat/head/tail)\n - Edit files: Use Edit (NOT sed/awk)\n\nFor simple commands, keep the description brief (5-10 words). For complex commands, include enough context to clarify what the command does.\n\nOutput is limited to 30,000 characters. Longer output will be middle-truncated.",
1170
- BashSchema,
1171
- async (params) => {
1172
- return runBash(params, options);
1173
- }
1174
- );
1046
+ return createZodFunctionTool("Bash", "Executes a given bash command and returns its output.\n\nThe working directory persists between commands, but shell state does not.\n\nIMPORTANT: Avoid using this tool to run `find`, `grep`, `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands. Instead, use the appropriate dedicated tool:\n - File search: Use Glob (NOT find or ls)\n - Content search: Use Grep (NOT grep or rg)\n - Read files: Use Read (NOT cat/head/tail)\n - Edit files: Use Edit (NOT sed/awk)\n\nFor simple commands, keep the description brief (5-10 words). For complex commands, include enough context to clarify what the command does.\n\nOutput is limited to 30,000 characters. Longer output will be middle-truncated.", BashSchema, async (params) => {
1047
+ return runBash(params, options);
1048
+ });
1175
1049
  }
1176
- var bashTool = createBashTool();
1177
- var DEFAULT_LIMIT = 2e3;
1178
- var ReadSchema = zod.z.object({
1179
- filePath: zod.z.string().describe("The absolute path to the file to read"),
1180
- offset: zod.z.number().optional().describe(
1181
- "The line number to start reading from (1-based). Only provide if the file is too large to read at once"
1182
- ),
1183
- limit: zod.z.number().optional().describe(
1184
- `The number of lines to read (default: ${DEFAULT_LIMIT}). Only provide if the file is too large to read at once`
1185
- )
1050
+ /**
1051
+ * BashTool instance — register with Robota agent tools registry.
1052
+ */
1053
+ const bashTool = createBashTool();
1054
+ //#endregion
1055
+ //#region src/builtins/read-tool.ts
1056
+ /**
1057
+ * ReadTool — read a file and return its contents with line numbers (cat -n style).
1058
+ *
1059
+ * Supports offset/limit for partial reads. Detects binary files and refuses to
1060
+ * return their raw bytes. Default limit is 2000 lines.
1061
+ */
1062
+ const DEFAULT_LIMIT$1 = 2e3;
1063
+ const ReadSchema = zod.z.object({
1064
+ filePath: zod.z.string().describe("The absolute path to the file to read"),
1065
+ offset: zod.z.number().optional().describe("The line number to start reading from (1-based). Only provide if the file is too large to read at once"),
1066
+ limit: zod.z.number().optional().describe(`The number of lines to read (default: ${DEFAULT_LIMIT$1}). Only provide if the file is too large to read at once`)
1186
1067
  });
1068
+ /**
1069
+ * Heuristic binary detection: scan the first 8 KB for null bytes.
1070
+ */
1187
1071
  function isBinary(buffer) {
1188
- const checkLength = Math.min(buffer.length, 8192);
1189
- for (let i = 0; i < checkLength; i++) {
1190
- if (buffer[i] === 0) return true;
1191
- }
1192
- return false;
1072
+ const checkLength = Math.min(buffer.length, 8192);
1073
+ for (let i = 0; i < checkLength; i++) if (buffer[i] === 0) return true;
1074
+ return false;
1193
1075
  }
1076
+ /**
1077
+ * Format lines with 1-based line numbers in cat -n style.
1078
+ * Pads line number to the width of the highest line number.
1079
+ */
1194
1080
  function formatWithLineNumbers(lines, startLine) {
1195
- const lastLineNum = startLine + lines.length - 1;
1196
- const width = String(lastLineNum).length;
1197
- return lines.map((line, idx) => {
1198
- const lineNum = String(startLine + idx).padStart(width, " ");
1199
- return `${lineNum} ${line}`;
1200
- }).join("\n");
1081
+ const lastLineNum = startLine + lines.length - 1;
1082
+ const width = String(lastLineNum).length;
1083
+ return lines.map((line, idx) => {
1084
+ return `${String(startLine + idx).padStart(width, " ")}\t${line}`;
1085
+ }).join("\n");
1201
1086
  }
1202
1087
  function formatReadResult(filePath, content, startLine, limit) {
1203
- const allLines = content.split("\n");
1204
- if (allLines[allLines.length - 1] === "") {
1205
- allLines.pop();
1206
- }
1207
- const zeroBasedStart = startLine - 1;
1208
- const selectedLines = allLines.slice(zeroBasedStart, zeroBasedStart + limit);
1209
- const output = formatWithLineNumbers(selectedLines, startLine);
1210
- const totalLines = allLines.length;
1211
- const returnedLines = selectedLines.length;
1212
- const header = returnedLines < totalLines ? `[File: ${filePath} (lines ${startLine}-${startLine + returnedLines - 1} of ${totalLines})]
1213
- ` : `[File: ${filePath} (${totalLines} lines)]
1214
- `;
1215
- const result = {
1216
- success: true,
1217
- output: header + output
1218
- };
1219
- return JSON.stringify(result);
1088
+ const allLines = content.split("\n");
1089
+ if (allLines[allLines.length - 1] === "") allLines.pop();
1090
+ const zeroBasedStart = startLine - 1;
1091
+ const selectedLines = allLines.slice(zeroBasedStart, zeroBasedStart + limit);
1092
+ const output = formatWithLineNumbers(selectedLines, startLine);
1093
+ const totalLines = allLines.length;
1094
+ const returnedLines = selectedLines.length;
1095
+ const result = {
1096
+ success: true,
1097
+ output: (returnedLines < totalLines ? `[File: ${filePath} (lines ${startLine}-${startLine + returnedLines - 1} of ${totalLines})]\n` : `[File: ${filePath} (${totalLines} lines)]\n`) + output
1098
+ };
1099
+ return JSON.stringify(result);
1220
1100
  }
1221
1101
  async function readFileTool(args, options = {}) {
1222
- const { filePath, offset, limit = DEFAULT_LIMIT } = args;
1223
- const startLine = offset !== void 0 && offset > 0 ? offset : 1;
1224
- if (options.sandboxClient) {
1225
- try {
1226
- const content2 = await options.sandboxClient.readFile(filePath);
1227
- return formatReadResult(filePath, content2, startLine, limit);
1228
- } catch (err) {
1229
- const result = {
1230
- success: false,
1231
- output: "",
1232
- error: err instanceof Error ? err.message : String(err)
1233
- };
1234
- return JSON.stringify(result);
1235
- }
1236
- }
1237
- let fileStats;
1238
- try {
1239
- fileStats = await promises.stat(filePath);
1240
- } catch (err) {
1241
- const result = {
1242
- success: false,
1243
- output: "",
1244
- error: `File not found: ${filePath}`
1245
- };
1246
- return JSON.stringify(result);
1247
- }
1248
- if (!fileStats.isFile()) {
1249
- const result = {
1250
- success: false,
1251
- output: "",
1252
- error: `Path is not a file: ${filePath}`
1253
- };
1254
- return JSON.stringify(result);
1255
- }
1256
- let buffer;
1257
- try {
1258
- buffer = await promises.readFile(filePath);
1259
- } catch (err) {
1260
- const result = {
1261
- success: false,
1262
- output: "",
1263
- error: err instanceof Error ? err.message : String(err)
1264
- };
1265
- return JSON.stringify(result);
1266
- }
1267
- if (isBinary(buffer)) {
1268
- const result = {
1269
- success: false,
1270
- output: "",
1271
- error: `Binary file not supported: ${filePath}`
1272
- };
1273
- return JSON.stringify(result);
1274
- }
1275
- const content = buffer.toString("utf8");
1276
- return formatReadResult(filePath, content, startLine, limit);
1102
+ const { filePath, offset, limit = DEFAULT_LIMIT$1 } = args;
1103
+ const startLine = offset !== void 0 && offset > 0 ? offset : 1;
1104
+ if (options.sandboxClient) try {
1105
+ return formatReadResult(filePath, await options.sandboxClient.readFile(filePath), startLine, limit);
1106
+ } catch (err) {
1107
+ const result = {
1108
+ success: false,
1109
+ output: "",
1110
+ error: err instanceof Error ? err.message : String(err)
1111
+ };
1112
+ return JSON.stringify(result);
1113
+ }
1114
+ let fileStats;
1115
+ try {
1116
+ fileStats = await (0, node_fs_promises.stat)(filePath);
1117
+ } catch (err) {
1118
+ const result = {
1119
+ success: false,
1120
+ output: "",
1121
+ error: `File not found: ${filePath}`
1122
+ };
1123
+ return JSON.stringify(result);
1124
+ }
1125
+ if (!fileStats.isFile()) {
1126
+ const result = {
1127
+ success: false,
1128
+ output: "",
1129
+ error: `Path is not a file: ${filePath}`
1130
+ };
1131
+ return JSON.stringify(result);
1132
+ }
1133
+ let buffer;
1134
+ try {
1135
+ buffer = await (0, node_fs_promises.readFile)(filePath);
1136
+ } catch (err) {
1137
+ const result = {
1138
+ success: false,
1139
+ output: "",
1140
+ error: err instanceof Error ? err.message : String(err)
1141
+ };
1142
+ return JSON.stringify(result);
1143
+ }
1144
+ if (isBinary(buffer)) {
1145
+ const result = {
1146
+ success: false,
1147
+ output: "",
1148
+ error: `Binary file not supported: ${filePath}`
1149
+ };
1150
+ return JSON.stringify(result);
1151
+ }
1152
+ return formatReadResult(filePath, buffer.toString("utf8"), startLine, limit);
1277
1153
  }
1154
+ /**
1155
+ * Create a ReadTool instance — register with Robota agent tools registry.
1156
+ */
1278
1157
  function createReadTool(options = {}) {
1279
- return createZodFunctionTool(
1280
- "Read",
1281
- "Reads a file from the local filesystem.\n\nBy default, reads up to 2000 lines from the beginning of the file. You can optionally specify offset and limit for partial reads.\n\nResults are returned using cat -n format, with line numbers starting at 1.\n\nThe file_path parameter must be an absolute path, not a relative path.",
1282
- ReadSchema,
1283
- async (params) => {
1284
- return readFileTool(params, options);
1285
- }
1286
- );
1158
+ return createZodFunctionTool("Read", "Reads a file from the local filesystem.\n\nBy default, reads up to 2000 lines from the beginning of the file. You can optionally specify offset and limit for partial reads.\n\nResults are returned using cat -n format, with line numbers starting at 1.\n\nThe file_path parameter must be an absolute path, not a relative path.", ReadSchema, async (params) => {
1159
+ return readFileTool(params, options);
1160
+ });
1287
1161
  }
1288
- var readTool = createReadTool();
1289
- var TEMP_RANDOM_BYTES = 6;
1290
- var PRESERVED_MODE_BITS = 4095;
1291
- var MISSING_FILE_ERROR_CODE = "ENOENT";
1162
+ /**
1163
+ * ReadTool instance — register with Robota agent tools registry.
1164
+ */
1165
+ const readTool = createReadTool();
1166
+ //#endregion
1167
+ //#region src/builtins/atomic-file-write.ts
1168
+ const TEMP_RANDOM_BYTES = 6;
1169
+ const PRESERVED_MODE_BITS = 4095;
1170
+ const MISSING_FILE_ERROR_CODE = "ENOENT";
1292
1171
  function createTempFilePath(filePath) {
1293
- const dir = path.dirname(filePath);
1294
- const name = path.basename(filePath);
1295
- const suffix = crypto.randomBytes(TEMP_RANDOM_BYTES).toString("hex");
1296
- return path.join(dir, `.${name}.robota-tmp-${process.pid}-${Date.now()}-${suffix}`);
1172
+ const dir = (0, node_path.dirname)(filePath);
1173
+ const name = (0, node_path.basename)(filePath);
1174
+ const suffix = (0, node_crypto.randomBytes)(TEMP_RANDOM_BYTES).toString("hex");
1175
+ return (0, node_path.join)(dir, `.${name}.robota-tmp-${process.pid}-${Date.now()}-${suffix}`);
1297
1176
  }
1298
1177
  async function readExistingMode(filePath) {
1299
- try {
1300
- const fileStats = await promises.stat(filePath);
1301
- return fileStats.mode & PRESERVED_MODE_BITS;
1302
- } catch (error) {
1303
- if (error instanceof Error && hasErrorCode(error, MISSING_FILE_ERROR_CODE)) return void 0;
1304
- throw error;
1305
- }
1178
+ try {
1179
+ return (await (0, node_fs_promises.stat)(filePath)).mode & PRESERVED_MODE_BITS;
1180
+ } catch (error) {
1181
+ if (error instanceof Error && hasErrorCode(error, MISSING_FILE_ERROR_CODE)) return void 0;
1182
+ throw error;
1183
+ }
1306
1184
  }
1307
1185
  function hasErrorCode(error, code) {
1308
- return "code" in error && error.code === code;
1186
+ return "code" in error && error.code === code;
1309
1187
  }
1310
1188
  async function atomicWriteUtf8File(filePath, content) {
1311
- const dir = path.dirname(filePath);
1312
- await promises.mkdir(dir, { recursive: true });
1313
- const existingMode = await readExistingMode(filePath);
1314
- const tempFilePath = createTempFilePath(filePath);
1315
- try {
1316
- await promises.writeFile(tempFilePath, content, "utf8");
1317
- if (existingMode !== void 0) {
1318
- await promises.chmod(tempFilePath, existingMode);
1319
- }
1320
- await promises.rename(tempFilePath, filePath);
1321
- } catch (error) {
1322
- await promises.rm(tempFilePath, { force: true }).catch(() => void 0);
1323
- throw error;
1324
- }
1189
+ await (0, node_fs_promises.mkdir)((0, node_path.dirname)(filePath), { recursive: true });
1190
+ const existingMode = await readExistingMode(filePath);
1191
+ const tempFilePath = createTempFilePath(filePath);
1192
+ try {
1193
+ await (0, node_fs_promises.writeFile)(tempFilePath, content, "utf8");
1194
+ if (existingMode !== void 0) await (0, node_fs_promises.chmod)(tempFilePath, existingMode);
1195
+ await (0, node_fs_promises.rename)(tempFilePath, filePath);
1196
+ } catch (error) {
1197
+ await (0, node_fs_promises.rm)(tempFilePath, { force: true }).catch(() => void 0);
1198
+ throw error;
1199
+ }
1325
1200
  }
1326
-
1327
- // src/builtins/write-tool.ts
1328
- var WriteSchema = zod.z.object({
1329
- filePath: zod.z.string().describe("The absolute path to the file to write"),
1330
- content: zod.z.string().describe("The content to write to the file")
1201
+ //#endregion
1202
+ //#region src/builtins/write-tool.ts
1203
+ /**
1204
+ * WriteTool write content to a file, auto-creating parent directories.
1205
+ */
1206
+ const WriteSchema = zod.z.object({
1207
+ filePath: zod.z.string().describe("The absolute path to the file to write"),
1208
+ content: zod.z.string().describe("The content to write to the file")
1331
1209
  });
1332
1210
  async function writeFileTool(args, options = {}) {
1333
- const { filePath, content } = args;
1334
- try {
1335
- if (options.sandboxClient) {
1336
- await options.sandboxClient.writeFile(filePath, content);
1337
- } else {
1338
- await atomicWriteUtf8File(filePath, content);
1339
- }
1340
- const result = {
1341
- success: true,
1342
- output: `Written ${Buffer.byteLength(content, "utf8")} bytes to ${filePath}`
1343
- };
1344
- return JSON.stringify(result);
1345
- } catch (err) {
1346
- const result = {
1347
- success: false,
1348
- output: "",
1349
- error: err instanceof Error ? err.message : String(err)
1350
- };
1351
- return JSON.stringify(result);
1352
- }
1211
+ const { filePath, content } = args;
1212
+ try {
1213
+ if (options.sandboxClient) await options.sandboxClient.writeFile(filePath, content);
1214
+ else await atomicWriteUtf8File(filePath, content);
1215
+ const result = {
1216
+ success: true,
1217
+ output: `Written ${Buffer.byteLength(content, "utf8")} bytes to ${filePath}`
1218
+ };
1219
+ return JSON.stringify(result);
1220
+ } catch (err) {
1221
+ const result = {
1222
+ success: false,
1223
+ output: "",
1224
+ error: err instanceof Error ? err.message : String(err)
1225
+ };
1226
+ return JSON.stringify(result);
1227
+ }
1353
1228
  }
1229
+ /**
1230
+ * Create a WriteTool instance — register with Robota agent tools registry.
1231
+ */
1354
1232
  function createWriteTool(options = {}) {
1355
- return createZodFunctionTool(
1356
- "Write",
1357
- "Writes a file to the local filesystem. This will overwrite an existing file if one exists.\n\nALWAYS prefer the Edit tool for modifying existing files \u2014 it only sends the diff. Only use this tool to create new files or for complete rewrites.\n\nNEVER create documentation files (*.md) or README files unless explicitly requested by the user.",
1358
- WriteSchema,
1359
- async (params) => {
1360
- return writeFileTool(params, options);
1361
- }
1362
- );
1233
+ return createZodFunctionTool("Write", "Writes a file to the local filesystem. This will overwrite an existing file if one exists.\n\nALWAYS prefer the Edit tool for modifying existing files — it only sends the diff. Only use this tool to create new files or for complete rewrites.\n\nNEVER create documentation files (*.md) or README files unless explicitly requested by the user.", WriteSchema, async (params) => {
1234
+ return writeFileTool(params, options);
1235
+ });
1363
1236
  }
1364
- var writeTool = createWriteTool();
1365
- var EditSchema = zod.z.object({
1366
- filePath: zod.z.string().describe("The absolute path to the file to modify"),
1367
- oldString: zod.z.string().describe("The text to replace (must be an exact match of existing content)"),
1368
- newString: zod.z.string().describe("The text to replace it with (must be different from old_string)"),
1369
- replaceAll: zod.z.boolean().optional().describe(
1370
- "Replace all occurrences of old_string (default: false). Useful for renaming variables"
1371
- )
1237
+ /**
1238
+ * WriteTool instance — register with Robota agent tools registry.
1239
+ */
1240
+ const writeTool = createWriteTool();
1241
+ //#endregion
1242
+ //#region src/builtins/edit-tool.ts
1243
+ /**
1244
+ * EditTool — perform string-replace edits on a file.
1245
+ *
1246
+ * By default, requires the oldString to appear exactly once in the file
1247
+ * (ensuring surgical edits). Pass replaceAll:true to replace all occurrences.
1248
+ */
1249
+ const EditSchema = zod.z.object({
1250
+ filePath: zod.z.string().describe("The absolute path to the file to modify"),
1251
+ oldString: zod.z.string().describe("The text to replace (must be an exact match of existing content)"),
1252
+ newString: zod.z.string().describe("The text to replace it with (must be different from old_string)"),
1253
+ replaceAll: zod.z.boolean().optional().describe("Replace all occurrences of old_string (default: false). Useful for renaming variables")
1372
1254
  });
1373
1255
  async function editFileTool(args, options = {}) {
1374
- const { filePath, oldString, newString, replaceAll = false } = args;
1375
- let content;
1376
- try {
1377
- content = options.sandboxClient ? await options.sandboxClient.readFile(filePath) : await promises.readFile(filePath, "utf8");
1378
- } catch (err) {
1379
- const result2 = {
1380
- success: false,
1381
- output: "",
1382
- error: `File not found: ${filePath}`
1383
- };
1384
- return JSON.stringify(result2);
1385
- }
1386
- if (!content.includes(oldString)) {
1387
- const result2 = {
1388
- success: false,
1389
- output: "",
1390
- error: `oldString not found in file: ${filePath}`
1391
- };
1392
- return JSON.stringify(result2);
1393
- }
1394
- if (!replaceAll) {
1395
- const firstIdx = content.indexOf(oldString);
1396
- const lastIdx = content.lastIndexOf(oldString);
1397
- if (firstIdx !== lastIdx) {
1398
- const occurrences = content.split(oldString).length - 1;
1399
- const result2 = {
1400
- success: false,
1401
- output: "",
1402
- error: `oldString is not unique in file (found ${occurrences} occurrences). Provide more context to make it unique, or use replaceAll:true.`
1403
- };
1404
- return JSON.stringify(result2);
1405
- }
1406
- }
1407
- const updated = replaceAll ? content.split(oldString).join(newString) : content.slice(0, content.indexOf(oldString)) + newString + content.slice(content.indexOf(oldString) + oldString.length);
1408
- try {
1409
- if (options.sandboxClient) {
1410
- await options.sandboxClient.writeFile(filePath, updated);
1411
- } else {
1412
- await atomicWriteUtf8File(filePath, updated);
1413
- }
1414
- } catch (err) {
1415
- const result2 = {
1416
- success: false,
1417
- output: "",
1418
- error: err instanceof Error ? err.message : String(err)
1419
- };
1420
- return JSON.stringify(result2);
1421
- }
1422
- const count = replaceAll ? content.split(oldString).length - 1 : 1;
1423
- const matchIdx = content.indexOf(oldString);
1424
- const startLine = matchIdx >= 0 ? content.substring(0, matchIdx).split("\n").length : 1;
1425
- const result = {
1426
- success: true,
1427
- output: `Replaced ${count} occurrence(s) in ${filePath}`,
1428
- startLine
1429
- };
1430
- return JSON.stringify(result);
1256
+ const { filePath, oldString, newString, replaceAll = false } = args;
1257
+ let content;
1258
+ try {
1259
+ content = options.sandboxClient ? await options.sandboxClient.readFile(filePath) : await (0, node_fs_promises.readFile)(filePath, "utf8");
1260
+ } catch (err) {
1261
+ const result = {
1262
+ success: false,
1263
+ output: "",
1264
+ error: `File not found: ${filePath}`
1265
+ };
1266
+ return JSON.stringify(result);
1267
+ }
1268
+ if (!content.includes(oldString)) {
1269
+ const result = {
1270
+ success: false,
1271
+ output: "",
1272
+ error: `oldString not found in file: ${filePath}`
1273
+ };
1274
+ return JSON.stringify(result);
1275
+ }
1276
+ if (!replaceAll) {
1277
+ if (content.indexOf(oldString) !== content.lastIndexOf(oldString)) {
1278
+ const result = {
1279
+ success: false,
1280
+ output: "",
1281
+ error: `oldString is not unique in file (found ${content.split(oldString).length - 1} occurrences). Provide more context to make it unique, or use replaceAll:true.`
1282
+ };
1283
+ return JSON.stringify(result);
1284
+ }
1285
+ }
1286
+ const updated = replaceAll ? content.split(oldString).join(newString) : content.slice(0, content.indexOf(oldString)) + newString + content.slice(content.indexOf(oldString) + oldString.length);
1287
+ try {
1288
+ if (options.sandboxClient) await options.sandboxClient.writeFile(filePath, updated);
1289
+ else await atomicWriteUtf8File(filePath, updated);
1290
+ } catch (err) {
1291
+ const result = {
1292
+ success: false,
1293
+ output: "",
1294
+ error: err instanceof Error ? err.message : String(err)
1295
+ };
1296
+ return JSON.stringify(result);
1297
+ }
1298
+ const count = replaceAll ? content.split(oldString).length - 1 : 1;
1299
+ const matchIdx = content.indexOf(oldString);
1300
+ const startLine = matchIdx >= 0 ? content.substring(0, matchIdx).split("\n").length : 1;
1301
+ const result = {
1302
+ success: true,
1303
+ output: `Replaced ${count} occurrence(s) in ${filePath}`,
1304
+ startLine
1305
+ };
1306
+ return JSON.stringify(result);
1431
1307
  }
1308
+ /**
1309
+ * Create an EditTool instance — register with Robota agent tools registry.
1310
+ */
1432
1311
  function createEditTool(options = {}) {
1433
- return createZodFunctionTool(
1434
- "Edit",
1435
- "Performs exact string replacements in files.\n\nYou must use the Read tool at least once before editing. When editing text from Read output, preserve the exact indentation.\n\nThe edit will FAIL if old_string is not unique in the file. Either provide more surrounding context to make it unique, or use replace_all to change every instance.\n\nALWAYS prefer editing existing files over creating new ones.",
1436
- EditSchema,
1437
- async (params) => {
1438
- return editFileTool(params, options);
1439
- }
1440
- );
1312
+ return createZodFunctionTool("Edit", "Performs exact string replacements in files.\n\nYou must use the Read tool at least once before editing. When editing text from Read output, preserve the exact indentation.\n\nThe edit will FAIL if old_string is not unique in the file. Either provide more surrounding context to make it unique, or use replace_all to change every instance.\n\nALWAYS prefer editing existing files over creating new ones.", EditSchema, async (params) => {
1313
+ return editFileTool(params, options);
1314
+ });
1441
1315
  }
1442
- var editTool = createEditTool();
1443
- var DEFAULT_MAX_RESULTS = 1e3;
1444
- var GlobSchema = zod.z.object({
1445
- pattern: zod.z.string().describe('The glob pattern to match files against (e.g. "**/*.ts", "src/**/*.tsx")'),
1446
- path: zod.z.string().optional().describe(
1447
- "The directory to search in. Defaults to the current working directory. Must be a valid directory path if provided"
1448
- ),
1449
- limit: zod.z.number().optional().describe(
1450
- "Maximum number of results to return (default: 1000). Use a smaller limit to save context space"
1451
- )
1316
+ /**
1317
+ * EditTool instance — register with Robota agent tools registry.
1318
+ */
1319
+ const editTool = createEditTool();
1320
+ //#endregion
1321
+ //#region src/builtins/glob-tool.ts
1322
+ /**
1323
+ * GlobTool — fast file pattern search using fast-glob.
1324
+ *
1325
+ * Excludes node_modules and .git by default.
1326
+ * Results are sorted by modification time (most recently modified first).
1327
+ */
1328
+ const DEFAULT_MAX_RESULTS = 1e3;
1329
+ const GlobSchema = zod.z.object({
1330
+ pattern: zod.z.string().describe("The glob pattern to match files against (e.g. \"**/*.ts\", \"src/**/*.tsx\")"),
1331
+ path: zod.z.string().optional().describe("The directory to search in. Defaults to the current working directory. Must be a valid directory path if provided"),
1332
+ limit: zod.z.number().optional().describe("Maximum number of results to return (default: 1000). Use a smaller limit to save context space")
1452
1333
  });
1453
1334
  async function globFileTool(args) {
1454
- const { pattern, path: basePath } = args;
1455
- const cwd = basePath ? path.resolve(basePath) : process.cwd();
1456
- let matches;
1457
- try {
1458
- matches = await fg__default.default(pattern, {
1459
- cwd,
1460
- ignore: ["**/node_modules/**", "**/.git/**"],
1461
- dot: true,
1462
- absolute: false
1463
- });
1464
- } catch (err) {
1465
- const result2 = {
1466
- success: false,
1467
- output: "",
1468
- error: err instanceof Error ? err.message : String(err)
1469
- };
1470
- return JSON.stringify(result2);
1471
- }
1472
- const withMtime = await Promise.all(
1473
- matches.map(async (p) => {
1474
- const absPath = path.resolve(cwd, p);
1475
- try {
1476
- const s = await promises.stat(absPath);
1477
- return { path: p, mtime: s.mtimeMs };
1478
- } catch {
1479
- return { path: p, mtime: 0 };
1480
- }
1481
- })
1482
- );
1483
- withMtime.sort((a, b) => b.mtime - a.mtime);
1484
- const maxResults = args.limit ?? DEFAULT_MAX_RESULTS;
1485
- const totalMatches = withMtime.length;
1486
- const truncated = totalMatches > maxResults;
1487
- const limited = truncated ? withMtime.slice(0, maxResults) : withMtime;
1488
- const sorted = limited.map((f) => f.path);
1489
- let output = sorted.length > 0 ? sorted.join("\n") : "(no matches)";
1490
- if (truncated) {
1491
- output += `
1492
-
1493
- [Showing ${maxResults} of ${totalMatches} matches. Use limit parameter to see more.]`;
1494
- }
1495
- const result = {
1496
- success: true,
1497
- output
1498
- };
1499
- return JSON.stringify(result);
1335
+ const { pattern, path: basePath } = args;
1336
+ const cwd = basePath ? (0, node_path.resolve)(basePath) : process.cwd();
1337
+ let matches;
1338
+ try {
1339
+ matches = await (0, fast_glob.default)(pattern, {
1340
+ cwd,
1341
+ ignore: ["**/node_modules/**", "**/.git/**"],
1342
+ dot: true,
1343
+ absolute: false
1344
+ });
1345
+ } catch (err) {
1346
+ const result = {
1347
+ success: false,
1348
+ output: "",
1349
+ error: err instanceof Error ? err.message : String(err)
1350
+ };
1351
+ return JSON.stringify(result);
1352
+ }
1353
+ const withMtime = await Promise.all(matches.map(async (p) => {
1354
+ const absPath = (0, node_path.resolve)(cwd, p);
1355
+ try {
1356
+ return {
1357
+ path: p,
1358
+ mtime: (await (0, node_fs_promises.stat)(absPath)).mtimeMs
1359
+ };
1360
+ } catch {
1361
+ return {
1362
+ path: p,
1363
+ mtime: 0
1364
+ };
1365
+ }
1366
+ }));
1367
+ withMtime.sort((a, b) => b.mtime - a.mtime);
1368
+ const maxResults = args.limit ?? DEFAULT_MAX_RESULTS;
1369
+ const totalMatches = withMtime.length;
1370
+ const truncated = totalMatches > maxResults;
1371
+ const sorted = (truncated ? withMtime.slice(0, maxResults) : withMtime).map((f) => f.path);
1372
+ let output = sorted.length > 0 ? sorted.join("\n") : "(no matches)";
1373
+ if (truncated) output += `\n\n[Showing ${maxResults} of ${totalMatches} matches. Use limit parameter to see more.]`;
1374
+ return JSON.stringify({
1375
+ success: true,
1376
+ output
1377
+ });
1500
1378
  }
1501
- var globTool = createZodFunctionTool(
1502
- "Glob",
1503
- "Fast file pattern matching tool that works with any codebase size.\n\nSupports glob patterns like '**/*.js' or 'src/**/*.ts'. Returns matching file paths sorted by modification time.\n\nUse this tool when you need to find files by name patterns. When doing an open-ended search that may require multiple rounds, use the Agent tool instead.\n\nDefault limit is 1000 results. Use the limit parameter if you need fewer results to save context space.",
1504
- GlobSchema,
1505
- async (params) => {
1506
- return globFileTool(params);
1507
- }
1508
- );
1509
- var GrepSchema = zod.z.object({
1510
- pattern: zod.z.string().describe("The regular expression pattern to search for in file contents"),
1511
- path: zod.z.string().optional().describe("File or directory to search in. Defaults to the current working directory"),
1512
- glob: zod.z.string().optional().describe(
1513
- 'Glob pattern to filter files (e.g. "*.ts", "*.{ts,tsx}"). Only files matching this pattern will be searched'
1514
- ),
1515
- contextLines: zod.z.number().optional().describe(
1516
- 'Number of context lines to show before and after each match. Only applies when outputMode is "content". Default: 0'
1517
- ),
1518
- outputMode: zod.z.enum(["files_with_matches", "content"]).optional().describe(
1519
- 'Output mode: "files_with_matches" shows only file paths (default), "content" shows matching lines with context'
1520
- )
1379
+ /**
1380
+ * GlobTool instance — register with Robota agent tools registry.
1381
+ */
1382
+ const globTool = createZodFunctionTool("Glob", "Fast file pattern matching tool that works with any codebase size.\n\nSupports glob patterns like '**/*.js' or 'src/**/*.ts'. Returns matching file paths sorted by modification time.\n\nUse this tool when you need to find files by name patterns. When doing an open-ended search that may require multiple rounds, use the Agent tool instead.\n\nDefault limit is 1000 results. Use the limit parameter if you need fewer results to save context space.", GlobSchema, async (params) => {
1383
+ return globFileTool(params);
1384
+ });
1385
+ //#endregion
1386
+ //#region src/builtins/grep-tool.ts
1387
+ /**
1388
+ * GrepTool recursive regex content search.
1389
+ *
1390
+ * Supports two output modes:
1391
+ * - files_with_matches (default): return only file paths that contain a match
1392
+ * - content: return matching lines with optional context lines
1393
+ */
1394
+ const GrepSchema = zod.z.object({
1395
+ pattern: zod.z.string().describe("The regular expression pattern to search for in file contents"),
1396
+ path: zod.z.string().optional().describe("File or directory to search in. Defaults to the current working directory"),
1397
+ glob: zod.z.string().optional().describe("Glob pattern to filter files (e.g. \"*.ts\", \"*.{ts,tsx}\"). Only files matching this pattern will be searched"),
1398
+ contextLines: zod.z.number().optional().describe("Number of context lines to show before and after each match. Only applies when outputMode is \"content\". Default: 0"),
1399
+ outputMode: zod.z.enum(["files_with_matches", "content"]).optional().describe("Output mode: \"files_with_matches\" shows only file paths (default), \"content\" shows matching lines with context")
1521
1400
  });
1401
+ /** Convert a simple glob to a RegExp for file name filtering. */
1522
1402
  function globToRegex(glob) {
1523
- const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".+").replace(/\*/g, "[^/]*");
1524
- return new RegExp(`^${escaped}$`);
1403
+ const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".+").replace(/\*/g, "[^/]*");
1404
+ return new RegExp(`^${escaped}$`);
1525
1405
  }
1406
+ /** Check if a file name matches an optional glob filter. */
1526
1407
  function matchesGlob(filename, glob) {
1527
- if (glob === void 0) return true;
1528
- return globToRegex(glob).test(filename);
1408
+ if (glob === void 0) return true;
1409
+ return globToRegex(glob).test(filename);
1529
1410
  }
1411
+ /** Gather all files under a directory recursively, excluding node_modules/.git. */
1530
1412
  async function collectFiles(dirPath, glob) {
1531
- const results = [];
1532
- async function walk(current) {
1533
- let entryNames;
1534
- try {
1535
- entryNames = await promises.readdir(current);
1536
- } catch {
1537
- return;
1538
- }
1539
- for (const name of entryNames) {
1540
- if (name === "node_modules" || name === ".git") continue;
1541
- const fullPath = path.join(current, name);
1542
- let fileStat;
1543
- try {
1544
- fileStat = await promises.stat(fullPath);
1545
- } catch {
1546
- continue;
1547
- }
1548
- if (fileStat.isDirectory()) {
1549
- await walk(fullPath);
1550
- } else if (fileStat.isFile()) {
1551
- if (matchesGlob(name, glob)) {
1552
- results.push(fullPath);
1553
- }
1554
- }
1555
- }
1556
- }
1557
- await walk(dirPath);
1558
- return results;
1413
+ const results = [];
1414
+ async function walk(current) {
1415
+ let entryNames;
1416
+ try {
1417
+ entryNames = await (0, node_fs_promises.readdir)(current);
1418
+ } catch {
1419
+ return;
1420
+ }
1421
+ for (const name of entryNames) {
1422
+ if (name === "node_modules" || name === ".git") continue;
1423
+ const fullPath = (0, node_path.join)(current, name);
1424
+ let fileStat;
1425
+ try {
1426
+ fileStat = await (0, node_fs_promises.stat)(fullPath);
1427
+ } catch {
1428
+ continue;
1429
+ }
1430
+ if (fileStat.isDirectory()) await walk(fullPath);
1431
+ else if (fileStat.isFile()) {
1432
+ if (matchesGlob(name, glob)) results.push(fullPath);
1433
+ }
1434
+ }
1435
+ }
1436
+ await walk(dirPath);
1437
+ return results;
1559
1438
  }
1439
+ /** Search a single file for lines matching the regex. */
1560
1440
  function searchFile(content, filePath, regex, contextLines, outputMode) {
1561
- const lines = content.split("\n");
1562
- const matchingIndices = [];
1563
- for (let i = 0; i < lines.length; i++) {
1564
- if (regex.test(lines[i])) {
1565
- matchingIndices.push(i);
1566
- }
1567
- }
1568
- if (matchingIndices.length === 0) return [];
1569
- if (outputMode === "files_with_matches") {
1570
- return [filePath];
1571
- }
1572
- const includedIndices = /* @__PURE__ */ new Set();
1573
- for (const idx of matchingIndices) {
1574
- for (let c = Math.max(0, idx - contextLines); c <= Math.min(lines.length - 1, idx + contextLines); c++) {
1575
- includedIndices.add(c);
1576
- }
1577
- }
1578
- const outputLines = [];
1579
- const sortedIndices = Array.from(includedIndices).sort((a, b) => a - b);
1580
- let prevIdx;
1581
- for (const idx of sortedIndices) {
1582
- if (prevIdx !== void 0 && idx > prevIdx + 1) {
1583
- outputLines.push("--");
1584
- }
1585
- const lineNum = idx + 1;
1586
- const marker = matchingIndices.includes(idx) ? ":" : "-";
1587
- outputLines.push(`${filePath}:${lineNum}${marker}${lines[idx]}`);
1588
- prevIdx = idx;
1589
- }
1590
- return outputLines;
1441
+ const lines = content.split("\n");
1442
+ const matchingIndices = [];
1443
+ for (let i = 0; i < lines.length; i++) if (regex.test(lines[i])) matchingIndices.push(i);
1444
+ if (matchingIndices.length === 0) return [];
1445
+ if (outputMode === "files_with_matches") return [filePath];
1446
+ const includedIndices = /* @__PURE__ */ new Set();
1447
+ for (const idx of matchingIndices) for (let c = Math.max(0, idx - contextLines); c <= Math.min(lines.length - 1, idx + contextLines); c++) includedIndices.add(c);
1448
+ const outputLines = [];
1449
+ const sortedIndices = Array.from(includedIndices).sort((a, b) => a - b);
1450
+ let prevIdx;
1451
+ for (const idx of sortedIndices) {
1452
+ if (prevIdx !== void 0 && idx > prevIdx + 1) outputLines.push("--");
1453
+ const lineNum = idx + 1;
1454
+ const marker = matchingIndices.includes(idx) ? ":" : "-";
1455
+ outputLines.push(`${filePath}:${lineNum}${marker}${lines[idx]}`);
1456
+ prevIdx = idx;
1457
+ }
1458
+ return outputLines;
1591
1459
  }
1592
1460
  async function grepFileTool(args) {
1593
- const {
1594
- pattern,
1595
- path: searchPath,
1596
- glob,
1597
- contextLines = 0,
1598
- outputMode = "files_with_matches"
1599
- } = args;
1600
- const targetPath = searchPath ? path.resolve(searchPath) : process.cwd();
1601
- let regex;
1602
- try {
1603
- regex = new RegExp(pattern);
1604
- } catch (err) {
1605
- const result2 = {
1606
- success: false,
1607
- output: "",
1608
- error: `Invalid regex pattern: ${pattern}`
1609
- };
1610
- return JSON.stringify(result2);
1611
- }
1612
- let targetStat;
1613
- try {
1614
- targetStat = await promises.stat(targetPath);
1615
- } catch {
1616
- const result2 = {
1617
- success: false,
1618
- output: "",
1619
- error: `Path not found: ${targetPath}`
1620
- };
1621
- return JSON.stringify(result2);
1622
- }
1623
- let files;
1624
- if (targetStat.isFile()) {
1625
- files = [targetPath];
1626
- } else {
1627
- files = await collectFiles(targetPath, glob);
1628
- }
1629
- const allOutputLines = [];
1630
- for (const filePath of files) {
1631
- let content;
1632
- try {
1633
- const buffer = await promises.readFile(filePath);
1634
- const checkLen = Math.min(buffer.length, 8192);
1635
- let hasBinary = false;
1636
- for (let i = 0; i < checkLen; i++) {
1637
- if (buffer[i] === 0) {
1638
- hasBinary = true;
1639
- break;
1640
- }
1641
- }
1642
- if (hasBinary) continue;
1643
- content = buffer.toString("utf8");
1644
- } catch {
1645
- continue;
1646
- }
1647
- const fileMatches = searchFile(content, filePath, regex, contextLines, outputMode);
1648
- allOutputLines.push(...fileMatches);
1649
- }
1650
- const result = {
1651
- success: true,
1652
- output: allOutputLines.length > 0 ? allOutputLines.join("\n") : "(no matches)"
1653
- };
1654
- return JSON.stringify(result);
1461
+ const { pattern, path: searchPath, glob, contextLines = 0, outputMode = "files_with_matches" } = args;
1462
+ const targetPath = searchPath ? (0, node_path.resolve)(searchPath) : process.cwd();
1463
+ let regex;
1464
+ try {
1465
+ regex = new RegExp(pattern);
1466
+ } catch (err) {
1467
+ const result = {
1468
+ success: false,
1469
+ output: "",
1470
+ error: `Invalid regex pattern: ${pattern}`
1471
+ };
1472
+ return JSON.stringify(result);
1473
+ }
1474
+ let targetStat;
1475
+ try {
1476
+ targetStat = await (0, node_fs_promises.stat)(targetPath);
1477
+ } catch {
1478
+ const result = {
1479
+ success: false,
1480
+ output: "",
1481
+ error: `Path not found: ${targetPath}`
1482
+ };
1483
+ return JSON.stringify(result);
1484
+ }
1485
+ let files;
1486
+ if (targetStat.isFile()) files = [targetPath];
1487
+ else files = await collectFiles(targetPath, glob);
1488
+ const allOutputLines = [];
1489
+ for (const filePath of files) {
1490
+ let content;
1491
+ try {
1492
+ const buffer = await (0, node_fs_promises.readFile)(filePath);
1493
+ const checkLen = Math.min(buffer.length, 8192);
1494
+ let hasBinary = false;
1495
+ for (let i = 0; i < checkLen; i++) if (buffer[i] === 0) {
1496
+ hasBinary = true;
1497
+ break;
1498
+ }
1499
+ if (hasBinary) continue;
1500
+ content = buffer.toString("utf8");
1501
+ } catch {
1502
+ continue;
1503
+ }
1504
+ const fileMatches = searchFile(content, filePath, regex, contextLines, outputMode);
1505
+ allOutputLines.push(...fileMatches);
1506
+ }
1507
+ const result = {
1508
+ success: true,
1509
+ output: allOutputLines.length > 0 ? allOutputLines.join("\n") : "(no matches)"
1510
+ };
1511
+ return JSON.stringify(result);
1655
1512
  }
1656
- var grepTool = createZodFunctionTool(
1657
- "Grep",
1658
- "A powerful search tool built on regex matching.\n\nSupports full regex syntax (e.g., 'log.*Error', 'function\\\\s+\\\\w+'). Filter files with glob parameter (e.g., '*.js', '**/*.tsx').\n\nOutput modes: 'content' shows matching lines with context, 'files_with_matches' shows only file paths (default), 'count' shows match counts.\n\nUse this tool for ALL search tasks. NEVER invoke grep or rg as a Bash command.\n\nUse head_limit to control result size and save context space.",
1659
- GrepSchema,
1660
- async (params) => {
1661
- return grepFileTool(params);
1662
- }
1663
- );
1664
- var DEFAULT_TIMEOUT_MS2 = 3e4;
1665
- var MAX_RESPONSE_BYTES = 5e6;
1666
- var WebFetchSchema = zod.z.object({
1667
- url: zod.z.string().describe("The URL to fetch"),
1668
- headers: zod.z.record(zod.z.string()).optional().describe("Optional HTTP headers as key-value pairs")
1513
+ /**
1514
+ * GrepTool instance — register with Robota agent tools registry.
1515
+ */
1516
+ const grepTool = createZodFunctionTool("Grep", "A powerful search tool built on regex matching.\n\nSupports full regex syntax (e.g., 'log.*Error', 'function\\\\s+\\\\w+'). Filter files with glob parameter (e.g., '*.js', '**/*.tsx').\n\nOutput modes: 'content' shows matching lines with context, 'files_with_matches' shows only file paths (default), 'count' shows match counts.\n\nUse this tool for ALL search tasks. NEVER invoke grep or rg as a Bash command.\n\nUse head_limit to control result size and save context space.", GrepSchema, async (params) => {
1517
+ return grepFileTool(params);
1518
+ });
1519
+ //#endregion
1520
+ //#region src/builtins/web-fetch-tool.ts
1521
+ /**
1522
+ * WebFetchTool fetch a URL and return its content as text.
1523
+ *
1524
+ * HTML is stripped to plain text for readability. Uses Node.js native fetch.
1525
+ * Output is capped at 30K chars (same as other tools).
1526
+ */
1527
+ const DEFAULT_TIMEOUT_MS$1 = 3e4;
1528
+ const MAX_RESPONSE_BYTES = 5e6;
1529
+ const WebFetchSchema = zod.z.object({
1530
+ url: zod.z.string().describe("The URL to fetch"),
1531
+ headers: zod.z.record(zod.z.string()).optional().describe("Optional HTTP headers as key-value pairs")
1669
1532
  });
1533
+ /** Strip HTML tags and decode common entities to produce readable text. */
1670
1534
  function htmlToText(html) {
1671
- return html.replace(/<script[\s\S]*?<\/script>/gi, "").replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<[^>]+>/g, " ").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ").replace(/\s+/g, " ").trim();
1535
+ return html.replace(/<script[\s\S]*?<\/script>/gi, "").replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<[^>]+>/g, " ").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&#39;/g, "'").replace(/&nbsp;/g, " ").replace(/\s+/g, " ").trim();
1672
1536
  }
1673
1537
  async function runWebFetch(args) {
1674
- const { url, headers } = args;
1675
- try {
1676
- new URL(url);
1677
- } catch {
1678
- const result = { success: false, output: "", error: `Invalid URL: ${url}` };
1679
- return JSON.stringify(result);
1680
- }
1681
- try {
1682
- const controller = new AbortController();
1683
- const timeout = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS2);
1684
- const response = await fetch(url, {
1685
- headers: {
1686
- "User-Agent": "Robota-CLI/3.0",
1687
- ...headers ?? {}
1688
- },
1689
- signal: controller.signal,
1690
- redirect: "follow"
1691
- });
1692
- clearTimeout(timeout);
1693
- if (!response.ok) {
1694
- const result2 = {
1695
- success: false,
1696
- output: "",
1697
- error: `HTTP ${response.status} ${response.statusText}`
1698
- };
1699
- return JSON.stringify(result2);
1700
- }
1701
- const contentType = response.headers.get("content-type") ?? "";
1702
- const buffer = await response.arrayBuffer();
1703
- if (buffer.byteLength > MAX_RESPONSE_BYTES) {
1704
- const result2 = {
1705
- success: false,
1706
- output: "",
1707
- error: `Response too large: ${buffer.byteLength} bytes (max ${MAX_RESPONSE_BYTES})`
1708
- };
1709
- return JSON.stringify(result2);
1710
- }
1711
- let text = new TextDecoder().decode(buffer);
1712
- if (contentType.includes("html")) {
1713
- text = htmlToText(text);
1714
- }
1715
- const result = { success: true, output: text };
1716
- return JSON.stringify(result);
1717
- } catch (err) {
1718
- const message = err instanceof Error ? err.message : String(err);
1719
- const result = { success: false, output: "", error: message };
1720
- return JSON.stringify(result);
1721
- }
1538
+ const { url, headers } = args;
1539
+ try {
1540
+ new URL(url);
1541
+ } catch {
1542
+ const result = {
1543
+ success: false,
1544
+ output: "",
1545
+ error: `Invalid URL: ${url}`
1546
+ };
1547
+ return JSON.stringify(result);
1548
+ }
1549
+ try {
1550
+ const controller = new AbortController();
1551
+ const timeout = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS$1);
1552
+ const response = await fetch(url, {
1553
+ headers: {
1554
+ "User-Agent": "Robota-CLI/3.0",
1555
+ ...headers ?? {}
1556
+ },
1557
+ signal: controller.signal,
1558
+ redirect: "follow"
1559
+ });
1560
+ clearTimeout(timeout);
1561
+ if (!response.ok) {
1562
+ const result = {
1563
+ success: false,
1564
+ output: "",
1565
+ error: `HTTP ${response.status} ${response.statusText}`
1566
+ };
1567
+ return JSON.stringify(result);
1568
+ }
1569
+ const contentType = response.headers.get("content-type") ?? "";
1570
+ const buffer = await response.arrayBuffer();
1571
+ if (buffer.byteLength > MAX_RESPONSE_BYTES) {
1572
+ const result = {
1573
+ success: false,
1574
+ output: "",
1575
+ error: `Response too large: ${buffer.byteLength} bytes (max ${MAX_RESPONSE_BYTES})`
1576
+ };
1577
+ return JSON.stringify(result);
1578
+ }
1579
+ let text = new TextDecoder().decode(buffer);
1580
+ if (contentType.includes("html")) text = htmlToText(text);
1581
+ return JSON.stringify({
1582
+ success: true,
1583
+ output: text
1584
+ });
1585
+ } catch (err) {
1586
+ const result = {
1587
+ success: false,
1588
+ output: "",
1589
+ error: err instanceof Error ? err.message : String(err)
1590
+ };
1591
+ return JSON.stringify(result);
1592
+ }
1722
1593
  }
1723
- var webFetchTool = createZodFunctionTool(
1724
- "WebFetch",
1725
- "Fetch a URL and return its content as text. HTML pages are converted to plain text.",
1726
- WebFetchSchema,
1727
- async (params) => runWebFetch(params)
1728
- );
1729
- var DEFAULT_LIMIT2 = 10;
1730
- var DEFAULT_TIMEOUT_MS3 = 15e3;
1731
- var WebSearchSchema = zod.z.object({
1732
- query: zod.z.string().describe("The search query"),
1733
- limit: zod.z.number().optional().describe(`Maximum number of results to return (default: ${DEFAULT_LIMIT2})`)
1594
+ const webFetchTool = createZodFunctionTool("WebFetch", "Fetch a URL and return its content as text. HTML pages are converted to plain text.", WebFetchSchema, async (params) => runWebFetch(params));
1595
+ //#endregion
1596
+ //#region src/builtins/web-search-tool.ts
1597
+ /**
1598
+ * WebSearchTool search the web and return results.
1599
+ *
1600
+ * Uses Brave Search API when BRAVE_API_KEY is set.
1601
+ * Returns an error with setup instructions otherwise.
1602
+ */
1603
+ const DEFAULT_LIMIT = 10;
1604
+ const DEFAULT_TIMEOUT_MS = 15e3;
1605
+ const WebSearchSchema = zod.z.object({
1606
+ query: zod.z.string().describe("The search query"),
1607
+ limit: zod.z.number().optional().describe(`Maximum number of results to return (default: ${DEFAULT_LIMIT})`)
1734
1608
  });
1735
1609
  async function runWebSearch(args) {
1736
- const { query, limit = DEFAULT_LIMIT2 } = args;
1737
- const apiKey = process.env["BRAVE_API_KEY"];
1738
- if (!apiKey) {
1739
- const result = {
1740
- success: false,
1741
- output: "",
1742
- error: "Web search requires BRAVE_API_KEY environment variable. Get a free API key at https://brave.com/search/api/ (2,000 queries/month free)."
1743
- };
1744
- return JSON.stringify(result);
1745
- }
1746
- try {
1747
- const controller = new AbortController();
1748
- const timeout = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS3);
1749
- const params = new URLSearchParams({
1750
- q: query,
1751
- count: String(Math.min(limit, 20))
1752
- });
1753
- const response = await fetch(`https://api.search.brave.com/res/v1/web/search?${params}`, {
1754
- headers: {
1755
- Accept: "application/json",
1756
- "Accept-Encoding": "gzip",
1757
- "X-Subscription-Token": apiKey
1758
- },
1759
- signal: controller.signal
1760
- });
1761
- clearTimeout(timeout);
1762
- if (!response.ok) {
1763
- const result2 = {
1764
- success: false,
1765
- output: "",
1766
- error: `Brave Search API error: HTTP ${response.status} ${response.statusText}`
1767
- };
1768
- return JSON.stringify(result2);
1769
- }
1770
- const data = await response.json();
1771
- const results = (data.web?.results ?? []).map((r) => ({
1772
- title: r.title,
1773
- url: r.url,
1774
- snippet: r.description
1775
- }));
1776
- const result = { success: true, output: JSON.stringify(results, null, 2) };
1777
- return JSON.stringify(result);
1778
- } catch (err) {
1779
- const message = err instanceof Error ? err.message : String(err);
1780
- const result = { success: false, output: "", error: message };
1781
- return JSON.stringify(result);
1782
- }
1610
+ const { query, limit = DEFAULT_LIMIT } = args;
1611
+ const apiKey = process.env["BRAVE_API_KEY"];
1612
+ if (!apiKey) return JSON.stringify({
1613
+ success: false,
1614
+ output: "",
1615
+ error: "Web search requires BRAVE_API_KEY environment variable. Get a free API key at https://brave.com/search/api/ (2,000 queries/month free)."
1616
+ });
1617
+ try {
1618
+ const controller = new AbortController();
1619
+ const timeout = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
1620
+ const params = new URLSearchParams({
1621
+ q: query,
1622
+ count: String(Math.min(limit, 20))
1623
+ });
1624
+ const response = await fetch(`https://api.search.brave.com/res/v1/web/search?${params}`, {
1625
+ headers: {
1626
+ Accept: "application/json",
1627
+ "Accept-Encoding": "gzip",
1628
+ "X-Subscription-Token": apiKey
1629
+ },
1630
+ signal: controller.signal
1631
+ });
1632
+ clearTimeout(timeout);
1633
+ if (!response.ok) {
1634
+ const result = {
1635
+ success: false,
1636
+ output: "",
1637
+ error: `Brave Search API error: HTTP ${response.status} ${response.statusText}`
1638
+ };
1639
+ return JSON.stringify(result);
1640
+ }
1641
+ const results = ((await response.json()).web?.results ?? []).map((r) => ({
1642
+ title: r.title,
1643
+ url: r.url,
1644
+ snippet: r.description
1645
+ }));
1646
+ const result = {
1647
+ success: true,
1648
+ output: JSON.stringify(results, null, 2)
1649
+ };
1650
+ return JSON.stringify(result);
1651
+ } catch (err) {
1652
+ const result = {
1653
+ success: false,
1654
+ output: "",
1655
+ error: err instanceof Error ? err.message : String(err)
1656
+ };
1657
+ return JSON.stringify(result);
1658
+ }
1783
1659
  }
1784
- var webSearchTool = createZodFunctionTool(
1785
- "WebSearch",
1786
- "Search the web and return results with title, URL, and snippet.",
1787
- WebSearchSchema,
1788
- async (params) => runWebSearch(params)
1789
- );
1790
-
1660
+ const webSearchTool = createZodFunctionTool("WebSearch", "Search the web and return results with title, URL, and snippet.", WebSearchSchema, async (params) => runWebSearch(params));
1661
+ //#endregion
1791
1662
  exports.E2BSandboxClient = E2BSandboxClient;
1792
1663
  exports.FunctionTool = FunctionTool;
1793
1664
  exports.InMemorySandboxClient = InMemorySandboxClient;