refactor(01-01): extract tool registration into src/daemon/tools.ts

- Create initTools() factory encapsulating ToolRegistry, allBuiltinTools, web search tools, ProcessManager, BrowserManager, ToolExecutor, and ToolPolicy
- Replace ~70 lines of inline tool setup in startDaemon() with single initTools() call
- Clean up tool-specific imports from daemon/index.ts (ToolPolicy, allBuiltinTools, createWebSearchTools, createProcessTools, ProcessManager, createBrowserTools)
- Tier 1 agent tools (session, agents list, message send, cron) remain in daemon/index.ts as intended
- daemon/index.ts reduced to 457 lines (from 1088 baseline)
This commit is contained in:
William Valentin
2026-02-09 20:12:46 -08:00
parent efceb38cb6
commit fb1199a1da
2 changed files with 93 additions and 1 deletions
+89
View File
@@ -0,0 +1,89 @@
import type { Config } from '../config/index.js';
import type { Lifecycle } from './lifecycle.js';
import { HookEngine } from '../hooks/index.js';
import { ToolRegistry, ToolExecutor, ToolPolicy, allBuiltinTools, createWebSearchTools, createProcessTools, ProcessManager, BrowserManager, createBrowserTools } from '../tools/index.js';
export interface ToolsDeps {
config: Config;
lifecycle: Lifecycle;
hookEngine: HookEngine;
}
export interface ToolsResult {
toolRegistry: ToolRegistry;
toolExecutor: ToolExecutor;
browserManager?: BrowserManager;
}
export function initTools(deps: ToolsDeps): ToolsResult {
const { config, lifecycle, hookEngine } = deps;
// Initialize tool registry and register all builtin tools
const toolRegistry = new ToolRegistry();
for (const tool of allBuiltinTools) {
toolRegistry.register(tool);
}
// Register web search tool if configured with credentials
if (config.web_search.api_key || config.web_search.endpoint) {
for (const tool of createWebSearchTools({
provider: config.web_search.provider,
apiKey: config.web_search.api_key,
endpoint: config.web_search.endpoint,
maxResults: config.web_search.max_results,
})) {
toolRegistry.register(tool);
}
}
// Initialize process manager and register process tools
const processManager = new ProcessManager({
maxConcurrent: config.process.max_concurrent,
maxRuntimeMinutes: config.process.max_runtime_minutes,
bufferSize: config.process.buffer_size,
});
for (const tool of createProcessTools(processManager)) {
toolRegistry.register(tool);
}
lifecycle.onShutdown(async () => {
await processManager.shutdown();
console.log('Process manager stopped');
});
// Initialize browser manager and register browser tools (if enabled)
let browserManager: BrowserManager | undefined;
if (config.browser?.enabled) {
browserManager = new BrowserManager({
executablePath: config.browser.executable_path,
wsEndpoint: config.browser.ws_endpoint,
headless: config.browser.headless,
maxPages: config.browser.max_pages,
defaultTimeout: config.browser.default_timeout,
});
for (const tool of createBrowserTools(browserManager)) {
toolRegistry.register(tool);
}
console.log(`Browser tools enabled (headless=${config.browser.headless})`);
lifecycle.onShutdown(async () => {
await browserManager!.shutdown();
console.log('Browser manager stopped');
});
}
const toolExecutor = new ToolExecutor(toolRegistry, hookEngine);
// Initialize tool policy from config
const toolPolicy = new ToolPolicy(config.tools);
toolRegistry.setPolicy(toolPolicy);
const effectiveProfile = toolPolicy.getEffectiveProfile();
if (effectiveProfile !== 'full') {
console.log(`Tool policy: profile=${effectiveProfile}, deny=[${config.tools.deny.join(', ')}]`);
}
return { toolRegistry, toolExecutor, browserManager };
}