unify agent items into a flat file-based store, drop the marketplace

Replace the marketplace service dependency and the native/global/user
scope tiers with a single external directory ($OFFICER_ITEMS_DIR) holding
skills, tools, tasks, processes and extensions as plain files.

- tasks move from Postgres to TASK.md files (new file-backed task layer);
  task editing now works, which the DB path never supported
- skills/tools/processes collapse into one shared file router (single dir)
- remove the marketplace client (sync-marketplace/sync-version) and the
  boot-time sync; pi-bridge/pi-manager/sandbox point at the flat store
- drop the dead tasks + vestigial skills/tools/processes/extensions +
  item_chats tables (migration 0004)
- one-time migration script exports DB tasks and consolidates disk items

Migration verified: all 6 tasks round-trip through the runtime parser
identically to their DB rows (pipeline steps, triggers, script impls and
agentic bodies all intact).

NOTE: not yet functionally tested end-to-end — every item (each task mode,
tool, skill, extension) still needs to be run/exercised in the app before
this is trusted. To be done manually.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 00:39:17 +00:00
co-authored by Claude Opus 4.8
parent 5fd3d4faac
commit f3492512ba
29 changed files with 2366 additions and 1357 deletions
+158
View File
@@ -0,0 +1,158 @@
import { createRouter } from '../create-router';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { itemsDir, type ItemType } from '../data-path';
// Shared router for the flat, file-based item types (skills, tools, processes). Each item is a
// directory under $OFFICER_ITEMS_DIR/<type>/<dirName>/ holding a single markdown file (SKILL.md /
// TOOL.md / PROCESS.md) plus an optional chat/ subdir. No scope tiers — every item is editable.
type Frontmatter = { name: string; description: string };
export function parseFrontmatter(raw: string): { frontmatter: Frontmatter; body: string; rawYaml: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { frontmatter: { name: '', description: '' }, body: raw, rawYaml: '' };
const yaml = match[1]!;
const body = match[2]!;
const name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
const description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
return { frontmatter: { name, description }, body, rawYaml: yaml };
}
async function readItemDirs(dir: string, fileName: 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 file = join(dir, entry.name, fileName);
if (await Bun.file(file).exists()) result.set(entry.name, file);
}
} catch {
// directory doesn't exist yet
}
return result;
}
const slugify = (name: string) =>
name
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
type ItemRouterConfig = { type: ItemType; fileName: string; label: string };
export function createItemRouter({ type, fileName, label }: ItemRouterConfig) {
const router = createRouter();
const rootDir = () => itemsDir(type);
const filePathFor = (name: string) => join(rootDir(), name, fileName);
const resolve = async (name: string): Promise<string | null> => {
const filePath = filePathFor(name);
return (await Bun.file(filePath).exists()) ? filePath : null;
};
router.get('/', async (ctx) => {
const dirs = await readItemDirs(rootDir(), fileName);
const items = await Promise.all(
Array.from(dirs.entries()).map(async ([dirName, filePath]) => {
const { frontmatter } = parseFrontmatter(await Bun.file(filePath).text());
return { dirName, name: frontmatter.name || dirName, description: frontmatter.description, filePath };
}),
);
items.sort((a, b) => a.name.localeCompare(b.name));
return ctx.json(items);
});
router.get('/:name', async (ctx) => {
const name = ctx.req.param('name');
const filePath = await resolve(name);
if (!filePath) return ctx.text('Not found', 404);
const { frontmatter, body, rawYaml } = parseFrontmatter(await Bun.file(filePath).text());
const chatMeta = join(dirname(filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta)
.json()
.then((m: { id: string }) => m.id)
.catch(() => null);
return ctx.json({
name: frontmatter.name || name,
description: frontmatter.description,
body,
rawFrontmatter: rawYaml,
filePath,
chatSessionId,
});
});
router.get('/:name/chat', async (ctx) => {
const name = ctx.req.param('name');
const filePath = await resolve(name);
if (!filePath) return ctx.text('Not found', 404);
const chatDir = join(dirname(filePath), '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 });
});
router.put('/:name/chat', async (ctx) => {
const name = ctx.req.param('name');
const filePath = await resolve(name);
if (!filePath) return ctx.text('Not found', 404);
const chatDir = join(dirname(filePath), 'chat');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
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 });
});
router.delete('/:name/chat', async (ctx) => {
const name = ctx.req.param('name');
const filePath = await resolve(name);
if (!filePath) return ctx.text('Not found', 404);
await rm(join(dirname(filePath), 'chat'), { recursive: true, force: true });
return ctx.json({ ok: true });
});
router.post('/', async (ctx) => {
const { name } = await ctx.req.json<{ name: string }>();
if (!name?.trim()) return ctx.text('Name is required', 400);
const dirName = slugify(name);
if (!dirName) return ctx.text('Invalid name', 400);
const filePath = filePathFor(dirName);
if (await Bun.file(filePath).exists()) return ctx.text(`${label} already exists`, 409);
await mkdir(dirname(filePath), { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
return ctx.json({ name: name.trim(), dirName, filePath });
});
router.delete('/:name', async (ctx) => {
const name = ctx.req.param('name');
const filePath = await resolve(name);
if (!filePath) return ctx.text('Not found', 404);
await rm(dirname(filePath), { recursive: true });
return ctx.json({ ok: true });
});
return router;
}
+8 -19
View File
@@ -2,17 +2,7 @@ import { join } from 'path';
import { readdirSync, existsSync, mkdirSync } from 'node:fs';
import type { Subprocess } from 'bun';
import type { PiEvent, MessageCost } from './types';
import {
PI_CONFIG_DIR,
DATA_PATH,
getHomeDirForRole,
getGlobalSkillsDir,
getUserSkillsDir,
getGlobalExtensionsDir,
getUserExtensionsDir,
getGlobalToolsDir,
getUserToolsDir,
} from '../../data-path';
import { PI_CONFIG_DIR, DATA_PATH, getHomeDirForRole, itemsDir } from '../../data-path';
import { logger } from './logger';
// Resolve pi as [node, cli.js] — Bun.spawn async pipes break with shebang scripts under pm2
@@ -33,9 +23,9 @@ const PI_CMD = (() => {
export type PiEventHandler = (event: PiEvent) => void;
function collectSkillFlags(email: string): string[] {
function collectSkillFlags(): string[] {
const flags: string[] = [];
const dirs = [getGlobalSkillsDir(), getUserSkillsDir(email)];
const dirs = [itemsDir('skills')];
for (const dir of dirs) {
if (!existsSync(dir)) continue;
@@ -50,9 +40,9 @@ function collectSkillFlags(email: string): string[] {
return flags;
}
function collectExtensionFlags(email: string): string[] {
function collectExtensionFlags(): string[] {
const flags: string[] = [];
const dirs = [getGlobalExtensionsDir(), getUserExtensionsDir(email)];
const dirs = [itemsDir('extensions')];
for (const dir of dirs) {
if (!existsSync(dir)) continue;
@@ -67,7 +57,6 @@ function collectExtensionFlags(email: string): string[] {
return flags;
}
async function resolveApiKeyForModel(model: string): Promise<string | null> {
const provider = model.split('/')[0];
if (!provider) return null;
@@ -94,8 +83,8 @@ export async function spawnPi(
onEvent: PiEventHandler,
options?: SpawnPiOptions,
): Promise<Subprocess> {
const skillFlags = collectSkillFlags(email);
const extensionFlags = collectExtensionFlags(email);
const skillFlags = collectSkillFlags();
const extensionFlags = collectExtensionFlags();
const piArgs = [
...PI_CMD,
@@ -118,7 +107,7 @@ export async function spawnPi(
}
const homeDir = getHomeDirForRole(email, options?.role ?? null);
const toolsDirs = [getGlobalToolsDir(), getUserToolsDir(email)].join(':');
const toolsDirs = itemsDir('tools');
const env: Record<string, string> = {
HOME: process.env.HOME ?? '',
+2 -210
View File
@@ -1,211 +1,3 @@
import { createRouter } from '../../create-router';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { getNativeProcessesDir, getGlobalProcessesDir, getUserProcessesDir } from '../../data-path';
import { createItemRouter } from '../item-router';
type Frontmatter = {
name: string;
description: string;
};
export function parseFrontmatter(raw: string): { frontmatter: Frontmatter; body: string; rawYaml: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { frontmatter: { name: '', description: '' }, body: raw, rawYaml: '' };
const yaml = match[1]!;
const body = match[2]!;
const name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
const description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
return { frontmatter: { name, description }, body, rawYaml: yaml };
}
export async function readProcessDirs(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 processFile = join(dir, entry.name, 'PROCESS.md');
if (await Bun.file(processFile).exists()) {
result.set(entry.name, processFile);
}
}
} catch {
// directory doesn't exist yet
}
return result;
}
type Scope = 'native' | 'global' | 'user';
function resolveScope(dirName: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): Scope {
if (user.has(dirName)) return 'user';
if (global.has(dirName)) return 'global';
return 'native';
}
function resolveFile(name: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): { filePath: string; scope: Scope } | null {
if (user.has(name)) return { filePath: user.get(name)!, scope: 'user' };
if (global.has(name)) return { filePath: global.get(name)!, scope: 'global' };
if (native.has(name)) return { filePath: native.get(name)!, scope: 'native' };
return null;
}
function isPrivileged(role: string) {
return role === 'Super Admin';
}
export const processesRouter = createRouter();
processesRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const merged = new Map(nativeProcesses);
for (const [name, path] of globalProcesses) merged.set(name, path);
for (const [name, path] of userProcesses) merged.set(name, path);
const processes = await Promise.all(
Array.from(merged.entries()).map(async ([dirName, filePath]) => {
const raw = await Bun.file(filePath).text();
const { frontmatter } = parseFrontmatter(raw);
const scope = resolveScope(dirName, nativeProcesses, globalProcesses, userProcesses);
return { dirName, name: frontmatter.name || dirName, description: frontmatter.description, scope };
}),
);
return ctx.json(processes);
});
processesRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const resolved = resolveFile(name, nativeProcesses, globalProcesses, userProcesses);
if (!resolved) return ctx.text('Not found', 404);
const raw = await Bun.file(resolved.filePath).text();
const { frontmatter, body, rawYaml } = parseFrontmatter(raw);
const chatMeta = join(dirname(resolved.filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta).json().then((m: { id: string }) => m.id).catch(() => null);
return ctx.json({
name: frontmatter.name || name,
description: frontmatter.description,
scope: resolved.scope,
body,
rawFrontmatter: rawYaml,
filePath: resolved.filePath,
chatSessionId,
});
});
processesRouter.get('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const resolved = resolveFile(name, nativeProcesses, globalProcesses, userProcesses);
if (!resolved) return ctx.text('Not found', 404);
const chatDir = join(dirname(resolved.filePath), '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 });
});
processesRouter.put('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const resolved = resolveFile(name, nativeProcesses, globalProcesses, userProcesses);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
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 });
});
processesRouter.delete('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const resolved = resolveFile(name, nativeProcesses, globalProcesses, userProcesses);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
await rm(chatDir, { recursive: true, force: true });
return ctx.json({ ok: true });
});
processesRouter.post('/', async (ctx) => {
const user = ctx.get('user');
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 targetDir = isPrivileged(user.role) ? getGlobalProcessesDir() : getUserProcessesDir(user.email);
const scope: Scope = isPrivileged(user.role) ? 'global' : 'user';
const dir = join(targetDir, dirName);
const filePath = join(dir, 'PROCESS.md');
if (await Bun.file(filePath).exists()) {
return ctx.text('Process already exists', 409);
}
await mkdir(dir, { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
return ctx.json({ name: name.trim(), dirName, filePath, scope });
});
processesRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeProcesses = await readProcessDirs(getNativeProcessesDir());
const globalProcesses = await readProcessDirs(getGlobalProcessesDir());
const userProcesses = await readProcessDirs(getUserProcessesDir(user.email));
const resolved = resolveFile(name, nativeProcesses, globalProcesses, userProcesses);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
await rm(dirname(resolved.filePath), { recursive: true });
return ctx.json({ ok: true });
});
export const processesRouter = createItemRouter({ type: 'processes', fileName: 'PROCESS.md', label: 'Process' });
+2 -213
View File
@@ -1,214 +1,3 @@
import { createRouter } from '../../create-router';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { getNativeSkillsDir, getGlobalSkillsDir, getUserSkillsDir } from '../../data-path';
import { createItemRouter } from '../item-router';
type Frontmatter = {
name: string;
description: string;
};
export function parseFrontmatter(raw: string): { frontmatter: Frontmatter; body: string; rawYaml: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { frontmatter: { name: '', description: '' }, body: raw, rawYaml: '' };
const yaml = match[1]!;
const body = match[2]!;
const name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
const description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
return { frontmatter: { name, description }, body, rawYaml: yaml };
}
export async function readSkillDirs(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 skillFile = join(dir, entry.name, 'SKILL.md');
if (await Bun.file(skillFile).exists()) {
result.set(entry.name, skillFile);
}
}
} catch {
// directory doesn't exist yet
}
return result;
}
type Scope = 'native' | 'global' | 'user';
function isPrivileged(role: string) {
return role === 'Super Admin';
}
function resolveScope(dirName: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): Scope {
if (user.has(dirName)) return 'user';
if (global.has(dirName)) return 'global';
return 'native';
}
function resolveFile(name: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): { filePath: string; scope: Scope } | null {
if (user.has(name)) return { filePath: user.get(name)!, scope: 'user' };
if (global.has(name)) return { filePath: global.get(name)!, scope: 'global' };
if (native.has(name)) return { filePath: native.get(name)!, scope: 'native' };
return null;
}
export const skillsRouter = createRouter();
skillsRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const merged = new Map(nativeSkills);
for (const [name, path] of globalSkills) merged.set(name, path);
for (const [name, path] of userSkills) merged.set(name, path);
const skills = await Promise.all(
Array.from(merged.entries()).map(async ([dirName, filePath]) => {
const raw = await Bun.file(filePath).text();
const { frontmatter } = parseFrontmatter(raw);
const scope = resolveScope(dirName, nativeSkills, globalSkills, userSkills);
return { dirName, name: frontmatter.name || dirName, description: frontmatter.description, scope };
}),
);
return ctx.json(skills);
});
skillsRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const resolved = resolveFile(name, nativeSkills, globalSkills, userSkills);
if (!resolved) return ctx.text('Not found', 404);
const raw = await Bun.file(resolved.filePath).text();
const { frontmatter, body, rawYaml } = parseFrontmatter(raw);
const chatMeta = join(dirname(resolved.filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta).json().then((m: { id: string }) => m.id).catch(() => null);
return ctx.json({
name: frontmatter.name || name,
description: frontmatter.description,
scope: resolved.scope,
body,
rawFrontmatter: rawYaml,
filePath: resolved.filePath,
chatSessionId,
});
});
skillsRouter.get('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const resolved = resolveFile(name, nativeSkills, globalSkills, userSkills);
if (!resolved) return ctx.text('Not found', 404);
const chatDir = join(dirname(resolved.filePath), '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 });
});
skillsRouter.put('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const resolved = resolveFile(name, nativeSkills, globalSkills, userSkills);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
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 });
});
skillsRouter.delete('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const resolved = resolveFile(name, nativeSkills, globalSkills, userSkills);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
await rm(chatDir, { recursive: true, force: true });
return ctx.json({ ok: true });
});
skillsRouter.post('/', async (ctx) => {
const user = ctx.get('user');
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);
// Members save to their own scope; Admins and above save to global
const targetDir = isPrivileged(user.role) ? getGlobalSkillsDir() : getUserSkillsDir(user.email);
const scope: Scope = isPrivileged(user.role) ? 'global' : 'user';
const dir = join(targetDir, dirName);
const filePath = join(dir, 'SKILL.md');
if (await Bun.file(filePath).exists()) {
return ctx.text('Skill already exists', 409);
}
await mkdir(dir, { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
return ctx.json({ name: name.trim(), dirName, filePath, scope });
});
skillsRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeSkills = await readSkillDirs(getNativeSkillsDir());
const globalSkills = await readSkillDirs(getGlobalSkillsDir());
const userSkills = await readSkillDirs(getUserSkillsDir(user.email));
const resolved = resolveFile(name, nativeSkills, globalSkills, userSkills);
if (!resolved) return ctx.text('Not found', 404);
// Members can only delete their own skills
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const dir = dirname(resolved.filePath);
await rm(dir, { recursive: true });
return ctx.json({ ok: true });
});
export const skillsRouter = createItemRouter({ type: 'skills', fileName: 'SKILL.md', label: 'Skill' });
+5 -4
View File
@@ -3,7 +3,8 @@ import { randomUUID } from 'crypto';
import { readdirSync, existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { getTaskByDirName, getUserSettings } from 'officerdb';
import { getUserSettings } from 'officerdb';
import { getTaskByDirName } from './task-files';
import { getHomeDirForRole, getHomeDir } from '../../data-path';
import { resolveBaseCwd } from '../pi/websocket';
import { SANDBOX_HOME } from '../../sidecar/sandbox';
@@ -471,7 +472,7 @@ export type ExecutePipelineParams = {
};
export async function executePipeline({ userId, email, username, role, taskDirName, inputs, cwd, model: modelOverride, startAt, abortSignal, emit }: ExecutePipelineParams): Promise<void> {
const pipelineTask = await getTaskByDirName(taskDirName, userId);
const pipelineTask = await getTaskByDirName(taskDirName);
if (!pipelineTask) {
emit({ type: 'error', message: `Task not found: ${taskDirName}` });
return;
@@ -514,7 +515,7 @@ export async function executePipeline({ userId, email, username, role, taskDirNa
const step = config.steps[stepIdx]!;
const stepTask = await getTaskByDirName(step.task, userId);
const stepTask = await getTaskByDirName(step.task);
if (!stepTask) {
emit({ type: 'error', message: `Step task not found: ${step.task}` });
return;
@@ -648,7 +649,7 @@ export async function message(ws: ServerWebSocket<WSData>, raw: string | Buffer)
const { userId, email, username, role } = ws.data;
// Resolve task name for the DB record
const task = await getTaskByDirName(msg.taskDirName, userId);
const task = await getTaskByDirName(msg.taskDirName);
if (!task) {
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
return;
+4 -4
View File
@@ -1,7 +1,7 @@
import type { ServerWebSocket } from 'bun';
import { join, isAbsolute } from 'node:path';
import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
import { getTaskByDirName } from 'officerdb';
import { getTaskByDirName } from './task-files';
import { getHomeDirForRole, DATA_PATH } from '../../data-path';
import { buildSandboxPrefix, buildRunuserSuffix } from '../../sidecar/sandbox';
@@ -90,10 +90,10 @@ function buildArgs(inputs: Record<string, string>, argsOrder?: string[] | null):
}
async function handleRun(ws: ServerWebSocket<WSData>, msg: RunMessage) {
const { email, role, sandboxed, userId } = ws.data;
const { email, role, sandboxed } = ws.data;
// Resolve task from database
const task = await getTaskByDirName(msg.taskDirName, userId);
// Resolve task from the file-backed store
const task = await getTaskByDirName(msg.taskDirName);
if (!task) {
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
return;
+247
View File
@@ -0,0 +1,247 @@
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { itemsDir } from '../../data-path';
// File-backed task store. Every task is a directory under $OFFICER_ITEMS_DIR/tasks/<dirName>/ with a
// TASK.md (metadata + prose body) and, for script-mode tasks, a sibling implementation file. There are
// no scope tiers and no database — the directory name is the task's identity.
const YAML = (Bun as unknown as { YAML: { parse(input: string): unknown } }).YAML;
export type TaskFrontmatter = {
name: string;
description: string | null;
version: number;
mode: string;
language: string | null;
args: string[] | null;
tags: string[] | null;
tools: string[] | null;
skills: string[] | null;
inputs: unknown;
outputs: unknown;
dependencies: unknown;
config: unknown;
trigger: unknown;
};
export type TaskRecord = TaskFrontmatter & {
dirName: string;
body: string;
implementation: string | null;
filePath: string;
};
export type TaskSummary = {
dirName: string;
name: string;
description: string | null;
mode: string;
version: number;
trigger: unknown;
};
// Frontmatter keys, in write order. These mirror the old `tasks` table columns 1:1.
const FM_KEYS = [
'name',
'description',
'version',
'mode',
'language',
'args',
'tags',
'tools',
'skills',
'inputs',
'outputs',
'dependencies',
'config',
'trigger',
] as const;
// Script implementation lives in a sibling file named by language, matching the marketplace registry.
const IMPL_FILE: Record<string, string> = {
bash: 'run.sh',
python: 'run.py',
typescript: 'index.ts',
javascript: 'index.js',
};
const implFileName = (language: string | null) => IMPL_FILE[language ?? 'bash'] ?? 'run.sh';
const tasksRoot = () => itemsDir('tasks');
const taskDir = (dirName: string) => join(tasksRoot(), dirName);
const taskFile = (dirName: string) => join(taskDir(dirName), 'TASK.md');
const asArray = (v: unknown): string[] | null => (Array.isArray(v) ? v.map(String) : null);
export const slugify = (name: string) =>
name
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
function parseTaskMd(raw: string): { fm: TaskFrontmatter; body: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
const body = match ? match[2]! : raw;
const yaml = match ? match[1]! : '';
let parsed: Record<string, unknown> = {};
if (yaml.trim()) {
try {
parsed = (YAML.parse(yaml) as Record<string, unknown>) ?? {};
} catch {
parsed = {};
}
}
const versionRaw = parsed.version;
const version = typeof versionRaw === 'number' ? versionRaw : Number(versionRaw) || 1;
return {
fm: {
name: parsed.name == null ? '' : String(parsed.name),
description: parsed.description == null ? null : String(parsed.description),
version,
mode: parsed.mode == null ? 'agentic' : String(parsed.mode),
language: parsed.language == null ? null : String(parsed.language),
args: asArray(parsed.args),
tags: asArray(parsed.tags),
tools: asArray(parsed.tools),
skills: asArray(parsed.skills),
inputs: parsed.inputs ?? null,
outputs: parsed.outputs ?? null,
dependencies: parsed.dependencies ?? null,
config: parsed.config ?? null,
trigger: parsed.trigger ?? null,
},
body,
};
}
// Serialize as JSON-flow YAML: every value is JSON.stringify'd, which is valid YAML and round-trips
// losslessly. Bun.YAML.parse also accepts hand-written multi-line YAML, so files stay editable.
function buildTaskMd(fm: Partial<TaskFrontmatter>, body: string): string {
const lines: string[] = ['---'];
for (const key of FM_KEYS) {
const val = (fm as Record<string, unknown>)[key];
if (val === undefined || val === null) continue;
if (Array.isArray(val) && val.length === 0) continue;
lines.push(`${key}: ${JSON.stringify(val)}`);
}
lines.push('---', '');
lines.push(body ?? '');
return lines.join('\n');
}
async function readImplementation(dirName: string, language: string | null): Promise<string | null> {
const implPath = join(taskDir(dirName), implFileName(language));
const file = Bun.file(implPath);
return (await file.exists()) ? file.text() : null;
}
export async function listTasks(): Promise<TaskSummary[]> {
let entries;
try {
entries = await readdir(tasksRoot(), { withFileTypes: true });
} catch {
return [];
}
const summaries: TaskSummary[] = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const file = Bun.file(taskFile(entry.name));
if (!(await file.exists())) continue;
const { fm } = parseTaskMd(await file.text());
summaries.push({
dirName: entry.name,
name: fm.name || entry.name,
description: fm.description,
mode: fm.mode,
version: fm.version,
trigger: fm.trigger ?? [],
});
}
summaries.sort((a, b) => a.name.localeCompare(b.name));
return summaries;
}
export async function getTaskByDirName(dirName: string): Promise<TaskRecord | null> {
const file = Bun.file(taskFile(dirName));
if (!(await file.exists())) return null;
const { fm, body } = parseTaskMd(await file.text());
const implementation = await readImplementation(dirName, fm.language);
return { ...fm, dirName, body, implementation, filePath: taskFile(dirName) };
}
type CreateTaskInput = {
name: string;
description?: string | null;
mode?: string;
language?: string | null;
};
export async function createTask(input: CreateTaskInput): Promise<TaskSummary & { filePath: string }> {
const name = input.name.trim();
const dirName = slugify(name);
if (!dirName) throw new Error('Invalid name');
if (await Bun.file(taskFile(dirName)).exists()) throw new Error('Task already exists');
const mode = input.mode ?? 'agentic';
const language = input.language ?? null;
await mkdir(taskDir(dirName), { recursive: true });
await Bun.write(
taskFile(dirName),
buildTaskMd({ name, description: input.description ?? null, version: 1, mode, language }, ''),
);
if (mode === 'script') await Bun.write(join(taskDir(dirName), implFileName(language)), '');
return {
dirName,
name,
description: input.description ?? null,
mode,
version: 1,
trigger: [],
filePath: taskFile(dirName),
};
}
export async function updateTask(
dirName: string,
patch: Partial<TaskFrontmatter> & { body?: string; implementation?: string | null },
): Promise<TaskRecord | null> {
const existing = await getTaskByDirName(dirName);
if (!existing) return null;
const merged: TaskFrontmatter = { ...existing, ...patch };
const body = patch.body ?? existing.body;
await Bun.write(taskFile(dirName), buildTaskMd(merged, body));
if (patch.implementation !== undefined && patch.implementation !== null) {
await Bun.write(join(taskDir(dirName), implFileName(merged.language)), patch.implementation);
}
return getTaskByDirName(dirName);
}
export async function deleteTask(dirName: string): Promise<void> {
await rm(taskDir(dirName), { recursive: true, force: true });
}
// Write a full task record to disk (used by the one-time DB→files migration). Overwrites any
// existing task with the same dirName so callers can apply their own precedence via write order.
export async function importTask(
dirName: string,
record: Partial<TaskFrontmatter> & { body?: string; implementation?: string | null },
): Promise<void> {
await mkdir(taskDir(dirName), { recursive: true });
await Bun.write(taskFile(dirName), buildTaskMd(record, record.body ?? ''));
if (record.implementation != null && record.implementation !== '') {
await Bun.write(join(taskDir(dirName), implFileName(record.language ?? null)), record.implementation);
}
}
+19 -40
View File
@@ -1,45 +1,34 @@
import { createRouter } from '../../create-router';
import { getTasksForUser, getTaskByDirName, getTaskById, createTask, updateTask, deleteTask } from 'officerdb';
import { listTasks, getTaskByDirName, createTask, deleteTask } from './task-files';
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
function isPrivileged(role: string) {
return role === 'Super Admin';
}
export const tasksRouter = createRouter();
tasksRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const rows = await getTasksForUser(user.id);
const summaries = await listTasks();
const tasks = rows.map((row) => ({
id: row.id,
const tasks = summaries.map((row) => ({
dirName: row.dirName,
name: row.name,
description: row.description,
scope: row.scope,
triggers: (row.trigger as TriggerConfig[]) ?? [],
mode: row.mode ?? 'agentic',
userId: row.userId,
}));
return ctx.json(tasks);
});
tasksRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
const task = await getTaskByDirName(name);
if (!task) return ctx.text('Not found', 404);
return ctx.json({
id: task.id,
dirName: task.dirName,
name: task.name,
description: task.description,
scope: task.scope,
mode: task.mode,
language: task.language,
body: task.body,
@@ -49,45 +38,35 @@ tasksRouter.get('/:name', async (ctx) => {
trigger: task.trigger,
config: task.config,
version: task.version,
userId: task.userId,
filePath: task.filePath,
});
});
tasksRouter.post('/', async (ctx) => {
const user = ctx.get('user');
const body = await ctx.req.json<{ name: string; description?: string; mode?: string; language?: string }>();
if (!body.name?.trim()) return ctx.text('Name is required', 400);
const dirName = body.name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
if (!dirName) return ctx.text('Invalid name', 400);
const scope = isPrivileged(user.role) ? 'global' : 'user';
const task = await createTask({
scope,
userId: user.id,
dirName,
name: body.name.trim(),
description: body.description ?? null,
mode: body.mode ?? 'agentic',
language: body.language ?? null,
});
return ctx.json(task);
try {
const task = await createTask({
name: body.name,
description: body.description ?? null,
mode: body.mode ?? 'agentic',
language: body.language ?? null,
});
return ctx.json(task);
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to create task';
return ctx.text(message, message === 'Task already exists' ? 409 : 400);
}
});
tasksRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
const task = await getTaskByDirName(name);
if (!task) return ctx.text('Not found', 404);
// Only owner or Super Admin can delete
if (task.scope === 'native') return ctx.text('Cannot delete native tasks', 403);
if (task.userId !== user.id && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
await deleteTask(task.id);
await deleteTask(task.dirName);
return ctx.json({ ok: true });
});
+2 -216
View File
@@ -1,217 +1,3 @@
import { createRouter } from '../../create-router';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { getNativeToolsDir, getGlobalToolsDir, getUserToolsDir } from '../../data-path';
import { createItemRouter } from '../item-router';
type Frontmatter = {
name: string;
description: string;
};
export function parseFrontmatter(raw: string): { frontmatter: Frontmatter; body: string; rawYaml: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { frontmatter: { name: '', description: '' }, body: raw, rawYaml: '' };
const yaml = match[1]!;
const body = match[2]!;
const name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
const description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
return { frontmatter: { name, description }, body, rawYaml: yaml };
}
export async function readToolDirs(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 toolFile = join(dir, entry.name, 'TOOL.md');
if (await Bun.file(toolFile).exists()) {
result.set(entry.name, toolFile);
}
}
} catch {
// directory doesn't exist yet
}
return result;
}
type Scope = 'native' | 'global' | 'user';
function resolveScope(dirName: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): Scope {
if (user.has(dirName)) return 'user';
if (global.has(dirName)) return 'global';
return 'native';
}
function resolveFile(name: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): { filePath: string; scope: Scope } | null {
if (user.has(name)) return { filePath: user.get(name)!, scope: 'user' };
if (global.has(name)) return { filePath: global.get(name)!, scope: 'global' };
if (native.has(name)) return { filePath: native.get(name)!, scope: 'native' };
return null;
}
function isPrivileged(role: string) {
return role === 'Super Admin';
}
export const toolsRouter = createRouter();
toolsRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const merged = new Map(nativeTools);
for (const [name, path] of globalTools) merged.set(name, path);
for (const [name, path] of userTools) merged.set(name, path);
const tools = await Promise.all(
Array.from(merged.entries()).map(async ([dirName, filePath]) => {
const raw = await Bun.file(filePath).text();
const { frontmatter } = parseFrontmatter(raw);
const scope = resolveScope(dirName, nativeTools, globalTools, userTools);
return {
dirName,
name: frontmatter.name || dirName,
description: frontmatter.description,
scope,
filePath,
};
}),
);
return ctx.json(tools);
});
toolsRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const resolved = resolveFile(name, nativeTools, globalTools, userTools);
if (!resolved) return ctx.text('Not found', 404);
const raw = await Bun.file(resolved.filePath).text();
const { frontmatter, body, rawYaml } = parseFrontmatter(raw);
const chatMeta = join(dirname(resolved.filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta).json().then((m: { id: string }) => m.id).catch(() => null);
return ctx.json({
name: frontmatter.name || name,
description: frontmatter.description,
scope: resolved.scope,
body,
rawFrontmatter: rawYaml,
filePath: resolved.filePath,
chatSessionId,
});
});
toolsRouter.get('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const resolved = resolveFile(name, nativeTools, globalTools, userTools);
if (!resolved) return ctx.text('Not found', 404);
const chatDir = join(dirname(resolved.filePath), '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 });
});
toolsRouter.put('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const resolved = resolveFile(name, nativeTools, globalTools, userTools);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
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 });
});
toolsRouter.delete('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const resolved = resolveFile(name, nativeTools, globalTools, userTools);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
await rm(chatDir, { recursive: true, force: true });
return ctx.json({ ok: true });
});
toolsRouter.post('/', async (ctx) => {
const user = ctx.get('user');
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 targetDir = isPrivileged(user.role) ? getGlobalToolsDir() : getUserToolsDir(user.email);
const scope: Scope = isPrivileged(user.role) ? 'global' : 'user';
const dir = join(targetDir, dirName);
const filePath = join(dir, 'TOOL.md');
if (await Bun.file(filePath).exists()) {
return ctx.text('Tool already exists', 409);
}
await mkdir(dir, { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
return ctx.json({ name: name.trim(), dirName, filePath, scope });
});
toolsRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTools = await readToolDirs(getNativeToolsDir());
const globalTools = await readToolDirs(getGlobalToolsDir());
const userTools = await readToolDirs(getUserToolsDir(user.email));
const resolved = resolveFile(name, nativeTools, globalTools, userTools);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
await rm(dirname(resolved.filePath), { recursive: true });
return ctx.json({ ok: true });
});
export const toolsRouter = createItemRouter({ type: 'tools', fileName: 'TOOL.md', label: 'Tool' });