resources

This commit is contained in:
2026-02-24 16:44:22 +00:00
parent d6ffe43a11
commit 05f0d0e8f7
39 changed files with 1385 additions and 1057 deletions
+199 -298
View File
@@ -1,259 +1,224 @@
import { readdir, mkdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { createRouter } from '../../create-router';
import { DATA_PATH, getResourcesDir } from '../../data-path';
import { getNativeResourcesDir, getGlobalResourcesDir } from '../../data-path';
import { parseFrontmatter } from '../skills/skills';
type ResourceCredentials = {
apiKey?: string;
username?: string;
password?: string;
};
export type ResourceConnectionConfig = {
url: string;
credentials?: ResourceCredentials;
};
type ResourcesConfig = Record<string, ResourceConnectionConfig>;
type Resource = {
id: string;
name: string;
subtitle: string;
type: string;
port: string | null;
path: string | null;
description: string;
installCommand: string | null;
uninstallCommand: string | null;
manageCommand: string | null;
verifyCommand: string | null;
updateCommand: string | null;
installed: boolean;
version: string | null;
connectionConfig: ResourceConnectionConfig | null;
};
const stripBackticks = (value: string) => value.replace(/^`(.+)`$/, '$1');
const resolveCommand = (command: string): string => {
return command.replace(/\$DATA_PATH/g, DATA_PATH);
};
function parseResourceFile(
filename: string,
content: string,
): Omit<Resource, 'installed' | 'version' | 'connectionConfig'> {
const id = filename
.replace(/^SERVICE_/, '')
.replace(/\.md$/, '')
.toLowerCase()
.replace(/_/g, '-');
const headingMatch = content.match(/^#\s+(.+?)\s+—\s+(.+)$/m);
const name = headingMatch?.[1] ?? id;
const subtitle = headingMatch?.[2] ?? '';
const field = (key: string): string | null => {
const match = content.match(new RegExp(`^-\\s+\\*\\*${key}:\\*\\*\\s+(.+)$`, 'm'));
return match?.[1]?.trim() ?? null;
};
const rawType = field('Type') ?? 'native';
const rawPort = field('Port');
const port = rawPort && !rawPort.startsWith('none') ? rawPort : null;
const rawPath = field('Path');
return {
id,
name,
subtitle,
type: rawType,
port,
path: rawPath ? stripBackticks(rawPath) : null,
description: field('Description') ?? '',
installCommand: field('Install') ? resolveCommand(stripBackticks(field('Install')!)) : null,
uninstallCommand: field('Uninstall') ? resolveCommand(stripBackticks(field('Uninstall')!)) : null,
manageCommand: field('Manage') ? resolveCommand(stripBackticks(field('Manage')!)) : null,
verifyCommand: field('Verify') ? resolveCommand(stripBackticks(field('Verify')!)) : null,
updateCommand: field('Update') ? resolveCommand(stripBackticks(field('Update')!)) : null,
};
async function readResourceDirs(dir: string): Promise<Map<string, string>> {
const result = new Map<string, string>();
try {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const resourceFile = join(dir, entry.name, 'RESOURCE.md');
if (await Bun.file(resourceFile).exists()) {
result.set(entry.name, resourceFile);
}
}
} catch {
// directory doesn't exist yet
}
return result;
}
const CONFIG_FILENAME = 'resources-config.json';
const getConfigPath = () => join(getResourcesDir(), CONFIG_FILENAME);
export async function readConfig(): Promise<ResourcesConfig> {
const path = getConfigPath();
if (!existsSync(path)) return {};
const text = await Bun.file(path).text();
return JSON.parse(text) as ResourcesConfig;
async function readConfigFile(dir: string): Promise<Record<string, string>> {
try {
return await Bun.file(join(dir, 'config.json')).json();
} catch {
return {};
}
}
async function writeConfig(config: ResourcesConfig): Promise<void> {
const dir = getResourcesDir();
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
await Bun.write(getConfigPath(), JSON.stringify(config, null, 2));
function mergeConfig(native: Record<string, string>, global: Record<string, string>): Record<string, string> {
const merged: Record<string, string> = {};
for (const key of Object.keys(native)) {
merged[key] = global[key] ?? native[key]!;
}
for (const key of Object.keys(global)) {
if (!(key in merged)) merged[key] = global[key]!;
}
return merged;
}
function isPrivileged(role: string) {
return role === 'Admin' || role === 'Owner' || role === 'Super Admin';
}
export async function readResourceConfig(name: string): Promise<Record<string, string>> {
const nativeConfig = await readConfigFile(join(getNativeResourcesDir(), name));
const globalConfig = await readConfigFile(join(getGlobalResourcesDir(), name));
return mergeConfig(nativeConfig, globalConfig);
}
const CHECK_TIMEOUT_MS = 3_000;
async function checkPort(port: number): Promise<boolean> {
try {
const socket = await Bun.connect({
hostname: '127.0.0.1',
port,
socket: {
data() {},
open(s) {
s.end();
},
error() {},
},
});
socket.end();
return true;
} catch {
return false;
}
}
async function checkVerifyCommand(command: string): Promise<{ installed: boolean; version: string | null }> {
try {
const proc = Bun.spawn(['sh', '-c', command], { stdout: 'pipe', stderr: 'pipe' });
const timeout = new Promise<never>((_, reject) =>
setTimeout(() => {
proc.kill();
reject(new Error('timeout'));
}, CHECK_TIMEOUT_MS),
);
const result = Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
const [stdout, stderr] = await Promise.race([result, timeout]);
if (proc.exitCode !== 0) return { installed: false, version: null };
const output = (stdout + stderr).trim();
const versionMatch = output.match(/(\d+\.\d+[\w.-]*)/);
return { installed: true, version: versionMatch?.[1] ?? null };
} catch {
return { installed: false, version: null };
}
}
function extractFirstPort(portStr: string): number | null {
const match = portStr.match(/(\d+)/);
return match ? parseInt(match[1]!, 10) : null;
}
async function checkUrl(url: string): Promise<boolean> {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), CHECK_TIMEOUT_MS);
await fetch(url, { signal: controller.signal });
clearTimeout(timeout);
return true;
} catch {
return false;
}
}
type CheckResourceStatusParams = {
resource: Omit<Resource, 'installed' | 'version' | 'connectionConfig'>;
configUrl?: string;
};
async function checkResourceStatus({
resource,
configUrl,
}: CheckResourceStatusParams): Promise<{ installed: boolean; version: string | null }> {
if (resource.path) {
const fullPath = `${getResourcesDir()}/${resource.path}`;
return { installed: existsSync(fullPath), version: null };
}
if (resource.port) {
if (configUrl) {
const reachable = await checkUrl(configUrl);
if (reachable) return { installed: true, version: null };
}
const port = extractFirstPort(resource.port);
if (port) {
const reachable = await checkPort(port);
return { installed: reachable, version: null };
}
}
if (resource.verifyCommand) {
return checkVerifyCommand(resource.verifyCommand);
}
return { installed: false, version: null };
}
function buildConnectionConfig(
resource: Omit<Resource, 'installed' | 'version' | 'connectionConfig'>,
config: ResourcesConfig,
): ResourceConnectionConfig | null {
if (!resource.port) return null;
if (config[resource.id]) return config[resource.id]!;
const port = extractFirstPort(resource.port);
return { url: `http://127.0.0.1:${port ?? resource.port}` };
}
async function parseResources() {
const dir = getResourcesDir();
if (!existsSync(dir)) return [];
const files = await readdir(dir);
const serviceFiles = files.filter((f) => f.startsWith('SERVICE_') && f.endsWith('.md'));
return Promise.all(
serviceFiles.map(async (filename) => {
const content = await Bun.file(`${dir}/${filename}`).text();
return parseResourceFile(filename, content);
}),
);
}
async function loadResources(): Promise<Resource[]> {
const [parsed, config] = await Promise.all([parseResources(), readConfig()]);
return Promise.all(
parsed.map(async (r) => {
const configUrl = config[r.id]?.url;
const status = await checkResourceStatus({ resource: r, configUrl });
const connectionConfig = buildConnectionConfig(r, config);
return { ...r, ...status, connectionConfig };
}),
);
}
export const resourcesRouter = createRouter();
resourcesRouter.get('/', async (ctx) => {
const resources = await loadResources();
const nativeResources = await readResourceDirs(getNativeResourcesDir());
const globalResources = await readResourceDirs(getGlobalResourcesDir());
const merged = new Map(nativeResources);
for (const [name, path] of globalResources) merged.set(name, path);
const resources = await Promise.all(
Array.from(merged.entries()).map(async ([dirName, filePath]) => {
const raw = await Bun.file(filePath).text();
const { frontmatter } = parseFrontmatter(raw);
const scope = globalResources.has(dirName) && !nativeResources.has(dirName) ? 'global' as const : nativeResources.has(dirName) ? 'native' as const : 'global' as const;
const config = await readResourceConfig(dirName);
return { dirName, name: frontmatter.name || dirName, description: frontmatter.description, scope, config };
}),
);
return ctx.json(resources);
});
resourcesRouter.get('/config', async (ctx) => {
const config = await readConfig();
return ctx.json(config);
resourcesRouter.get('/:name', async (ctx) => {
const name = ctx.req.param('name');
const nativeResources = await readResourceDirs(getNativeResourcesDir());
const globalResources = await readResourceDirs(getGlobalResourcesDir());
const filePath = globalResources.get(name) ?? nativeResources.get(name);
if (!filePath) return ctx.text('Not found', 404);
const scope = globalResources.has(name) && !nativeResources.has(name) ? 'global' as const : 'native' as const;
const raw = await Bun.file(filePath).text();
const { frontmatter, body, rawYaml } = parseFrontmatter(raw);
const config = await readResourceConfig(name);
const globalConfigPath = join(getGlobalResourcesDir(), name, 'config.json');
const chatMeta = join(dirname(filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta).json().then((m: { id: string }) => m.id).catch(() => null);
const guidePath = join(getNativeResourcesDir(), 'GUIDE.md');
return ctx.json({
dirName: name,
name: frontmatter.name || name,
description: frontmatter.description,
scope,
body,
rawFrontmatter: rawYaml,
filePath,
config,
configPath: globalConfigPath,
chatSessionId,
guidePath,
});
});
resourcesRouter.patch('/config/:id', async (ctx) => {
const id = ctx.req.param('id');
const body = await ctx.req.json<Partial<ResourceConnectionConfig>>();
const config = await readConfig();
const existing = config[id] ?? { url: '' };
config[id] = { ...existing, ...body };
await writeConfig(config);
return ctx.json(config[id]);
resourcesRouter.patch('/:name/config', async (ctx) => {
const user = ctx.get('user');
if (!isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const name = ctx.req.param('name');
const body = await ctx.req.json<Record<string, string | null>>();
const globalDir = join(getGlobalResourcesDir(), name);
await mkdir(globalDir, { recursive: true });
const existing = await readConfigFile(globalDir);
for (const [key, value] of Object.entries(body)) {
if (value === null) delete existing[key];
else existing[key] = value;
}
await Bun.write(join(globalDir, 'config.json'), JSON.stringify(existing, null, 2));
const merged = await readResourceConfig(name);
return ctx.json(merged);
});
resourcesRouter.post('/:id/ping', async (ctx) => {
const id = ctx.req.param('id');
resourcesRouter.post('/', async (ctx) => {
const user = ctx.get('user');
if (!isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const { name } = await ctx.req.json<{ name: string }>();
if (!name?.trim()) return ctx.text('Name is required', 400);
const dirName = name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
if (!dirName) return ctx.text('Invalid name', 400);
const dir = join(getGlobalResourcesDir(), dirName);
const filePath = join(dir, 'RESOURCE.md');
if (await Bun.file(filePath).exists()) {
return ctx.text('Resource already exists', 409);
}
await mkdir(dir, { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
await Bun.write(join(dir, 'config.json'), JSON.stringify({ url: '', api_key: '', username: '', password: '' }, null, 2));
return ctx.json({ name: name.trim(), dirName, filePath, scope: 'global' });
});
resourcesRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
if (!isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const name = ctx.req.param('name');
const nativeResources = await readResourceDirs(getNativeResourcesDir());
if (nativeResources.has(name)) return ctx.text('Cannot delete native resource', 400);
const dir = join(getGlobalResourcesDir(), name);
const filePath = join(dir, 'RESOURCE.md');
if (!(await Bun.file(filePath).exists())) return ctx.text('Not found', 404);
await rm(dir, { recursive: true });
return ctx.json({ ok: true });
});
resourcesRouter.get('/:name/chat', async (ctx) => {
const name = ctx.req.param('name');
const nativeResources = await readResourceDirs(getNativeResourcesDir());
const globalResources = await readResourceDirs(getGlobalResourcesDir());
const filePath = globalResources.get(name) ?? nativeResources.get(name);
if (!filePath) return ctx.text('Not found', 404);
const chatDir = join(getGlobalResourcesDir(), name, 'chat');
const sessionId = await Bun.file(join(chatDir, 'meta.json')).json().then((m: { id: string }) => m.id).catch(() => null);
const messages = await Bun.file(join(chatDir, 'messages.json')).json().catch(() => []);
return ctx.json({ sessionId, messages });
});
resourcesRouter.put('/:name/chat', async (ctx) => {
const user = ctx.get('user');
if (!isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const name = ctx.req.param('name');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
const chatDir = join(getGlobalResourcesDir(), name, 'chat');
await mkdir(chatDir, { recursive: true });
await Bun.write(join(chatDir, 'messages.json'), JSON.stringify(messages));
if (sessionId) await Bun.write(join(chatDir, 'meta.json'), JSON.stringify({ id: sessionId }));
return ctx.json({ ok: true });
});
resourcesRouter.delete('/:name/chat', async (ctx) => {
const user = ctx.get('user');
if (!isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const name = ctx.req.param('name');
const chatDir = join(getGlobalResourcesDir(), name, 'chat');
await rm(chatDir, { recursive: true, force: true });
return ctx.json({ ok: true });
});
resourcesRouter.post('/:name/ping', async (ctx) => {
const name = ctx.req.param('name');
const body = await ctx.req.json<{ url?: string }>().catch((): { url?: string } => ({}));
const config = await readConfig();
const resources = await loadResources();
const resource = resources.find((r) => r.id === id);
if (!resource) return ctx.json({ error: 'Resource not found' }, 404);
const config = await readResourceConfig(name);
const url = body.url ?? config[id]?.url ?? resource.connectionConfig?.url;
const url = body.url ?? config.url;
if (!url) return ctx.json({ error: 'No URL configured' }, 400);
try {
@@ -268,67 +233,3 @@ resourcesRouter.post('/:id/ping', async (ctx) => {
return ctx.json({ reachable: false, latencyMs: null });
}
});
resourcesRouter.post('/error-log', async (ctx) => {
const body = await ctx.req.json<{ command: string; output: string; exitCode: number }>();
const timestamp = Date.now();
const filePath = `/tmp/officer-error-${timestamp}.md`;
const md = [
`# Command Failed (exit code ${body.exitCode})`,
'',
'```',
body.command,
'```',
'',
'## Output',
'',
'```',
body.output,
'```',
].join('\n');
await Bun.write(filePath, md);
return ctx.json({ filePath });
});
resourcesRouter.post('/:id/run', async (ctx) => {
const id = ctx.req.param('id');
const { action } = await ctx.req.json<{ action: string }>();
const parsed = await parseResources();
const resource = parsed.find((r) => r.id === id);
if (!resource) return ctx.json({ error: 'Resource not found' }, 404);
const commands: Record<string, string | null> = {
install: resource.installCommand,
uninstall: resource.uninstallCommand,
verify: resource.verifyCommand,
update: resource.updateCommand,
manage: resource.manageCommand,
};
const command = commands[action];
if (!command) return ctx.json({ error: `No ${action} command for this resource` }, 400);
if (command.trimStart().startsWith('sudo')) {
return ctx.json({ error: 'Sudo commands must run in terminal' }, 400);
}
try {
const proc = Bun.spawn(['sh', '-c', command], { stdout: 'pipe', stderr: 'pipe' });
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
await proc.exited;
return ctx.json({ exitCode: proc.exitCode, output: (stdout + stderr).trim() });
} catch {
return ctx.json({ exitCode: 1, output: 'Failed to execute command' });
}
});
resourcesRouter.get('/:id', async (ctx) => {
const resources = await loadResources();
const resource = resources.find((r) => r.id === ctx.req.param('id'));
if (!resource) return ctx.json({ error: 'Resource not found' }, 404);
return ctx.json(resource);
});