Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Tools Registry Service
*
* Manages onboard tools (image generation, etc.) that CoS agents can discover and use.
* Tools are stored as individual JSON files in data/tools/.
* In-memory cache avoids disk I/O on hot paths (agent spawning).
*/
import { readdir, unlink } from 'fs/promises';
import { join } from 'path';
import { randomUUID } from 'crypto';
import { atomicWrite, ensureDir, readJSONFile, PATHS } from '../lib/fileUtils.js';
const TOOL_ID_PATTERN = /^[a-zA-Z0-9_-]{1,100}$/;
function validateToolId(id) {
if (typeof id !== 'string' || !TOOL_ID_PATTERN.test(id)) {
throw new Error(`Invalid tool id: must match ${TOOL_ID_PATTERN}`);
}
}
const toolPath = (id) => {
validateToolId(id);
return join(PATHS.tools, `${id}.json`);
};
let cache = null;
// In-flight dedup: two concurrent cold-cache getTools() calls share one load
// rather than each kicking off a parallel readdir+readJSONFile storm.
let loadAllPromise = null;
async function loadAll() {
await ensureDir(PATHS.tools);
const files = await readdir(PATHS.tools);
const jsonFiles = files.filter((f) => f.endsWith('.json'));
// Parallelize all per-file reads instead of sequential awaits.
const results = await Promise.all(
jsonFiles.map((f) => readJSONFile(join(PATHS.tools, f), null))
);
const tools = results.filter(Boolean);
cache = tools;
return tools;
}
function invalidateCache() {
cache = null;
loadAllPromise = null;
}
export async function getTools() {
if (cache) return cache;
if (loadAllPromise) return loadAllPromise;
loadAllPromise = loadAll().catch((err) => {
loadAllPromise = null;
throw err;
});
return loadAllPromise;
}
// Non-strict by design: updateTool returns without writing when this read fails;
// registerTool is an explicit complete replacement and does not merge this fallback.
export async function getTool(id) {
return readJSONFile(toolPath(id), null);
}
export async function getEnabledTools() {
const all = await getTools();
return all.filter(t => t.enabled);
}
export async function registerTool(config) {
await ensureDir(PATHS.tools);
const now = new Date().toISOString();
const tool = {
id: config.id || randomUUID(),
name: config.name,
category: config.category,
description: config.description || '',
enabled: config.enabled ?? true,
config: config.config || {},
promptHints: config.promptHints || '',
createdAt: now,
updatedAt: now
};
await atomicWrite(toolPath(tool.id), tool);
invalidateCache();
console.log(`🔧 Tool registered: ${tool.name} (${tool.id})`);
return tool;
}
export async function updateTool(id, updates) {
const existing = await getTool(id);
if (!existing) return null;
const merged = {
...existing,
...updates,
id,
updatedAt: new Date().toISOString()
};
await atomicWrite(toolPath(id), merged);
invalidateCache();
console.log(`🔧 Tool updated: ${merged.name} (${id})`);
return merged;
}
export async function deleteTool(id) {
await unlink(toolPath(id)).catch(() => null);
invalidateCache();
console.log(`🗑️ Tool deleted: ${id}`);
}
export async function getToolsSummaryForPrompt() {
const tools = await getEnabledTools();
if (tools.length === 0) return '';
const lines = tools.map(t => {
const hint = t.promptHints ? ` — ${t.promptHints}` : '';
return `- **${t.name}** (${t.category}): ${t.description}${hint}`;
});
return `## Available Tools\nThe following onboard tools are available for this instance:\n\n${lines.join('\n')}\n`;
}