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:
@@ -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;
|
||||
}
|
||||
@@ -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 ?? '',
|
||||
|
||||
@@ -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' });
|
||||
|
||||
@@ -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' });
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
});
|
||||
|
||||
@@ -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' });
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { mkdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import { DATA_PATH } from './data-path';
|
||||
import { syncMarketplaceTools, syncMarketplaceTasks } from './sync-marketplace';
|
||||
import { DATA_PATH, ensureItemDirs } from './data-path';
|
||||
import { ensureToolLoader } from './ensure-tool-loader';
|
||||
// Queue is now owned by the sidecar process
|
||||
import { startDiscordBotIfConfigured } from './channels/discord/bot';
|
||||
@@ -10,6 +9,7 @@ import { startTelegramBotIfConfigured } from './channels/telegram/bot';
|
||||
import { startWhatsAppBotIfConfigured } from './channels/whatsapp/bot';
|
||||
|
||||
mkdirSync(DATA_PATH, { recursive: true });
|
||||
ensureItemDirs();
|
||||
|
||||
/** Check common locations for the Pi package directory. */
|
||||
async function findPiPackageDir(): Promise<string | null> {
|
||||
@@ -67,8 +67,6 @@ async function installPi(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
await syncMarketplaceTools();
|
||||
await syncMarketplaceTasks();
|
||||
ensureToolLoader();
|
||||
// Queue is initialized by the sidecar process
|
||||
|
||||
|
||||
+15
-31
@@ -1,8 +1,23 @@
|
||||
import { join, resolve } from 'node:path';
|
||||
import { mkdirSync } from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
|
||||
export const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
||||
|
||||
// Unified, file-based store for all agent items, living outside the repo. Every skill/tool/task/
|
||||
// process/extension is a directory under one of these type subfolders — no scope tiers, no DB.
|
||||
export const OFFICER_ITEMS_DIR = process.env.OFFICER_ITEMS_DIR ?? join(process.cwd(), 'officer-items');
|
||||
|
||||
export type ItemType = 'skills' | 'tools' | 'tasks' | 'processes' | 'extensions';
|
||||
|
||||
export const ITEM_TYPES: ItemType[] = ['skills', 'tools', 'tasks', 'processes', 'extensions'];
|
||||
|
||||
export const itemsDir = (type: ItemType) => join(OFFICER_ITEMS_DIR, type);
|
||||
|
||||
export const ensureItemDirs = () => {
|
||||
for (const type of ITEM_TYPES) mkdirSync(itemsDir(type), { recursive: true });
|
||||
};
|
||||
|
||||
export const SERVER_CONFIG_DIR = join(DATA_PATH, 'server-settings');
|
||||
|
||||
export const PI_CONFIG_DIR = join(homedir(), '.pi', 'agent');
|
||||
@@ -33,43 +48,12 @@ export const getUserPiConfigDir = (email: string) => join(DATA_PATH, email, 'hom
|
||||
|
||||
export const getUserProjectsDir = (email: string) => join(DATA_PATH, email, 'home', 'Projects');
|
||||
|
||||
export const getNativeSkillsDir = () => join(SEED_PATH, 'skills');
|
||||
|
||||
export const getGlobalSkillsDir = () => join(DATA_PATH, 'skills');
|
||||
|
||||
export const getUserSkillsDir = (email: string) => join(DATA_PATH, email, 'skills');
|
||||
|
||||
export const getNativeToolsDir = () => join(SEED_PATH, 'tools');
|
||||
|
||||
export const getGlobalToolsDir = () => join(DATA_PATH, 'tools');
|
||||
|
||||
export const getUserToolsDir = (email: string) => join(DATA_PATH, email, 'tools');
|
||||
|
||||
export const getNativeExtensionsDir = () => join(SEED_PATH, 'extensions');
|
||||
|
||||
export const getGlobalExtensionsDir = () => join(DATA_PATH, 'extensions');
|
||||
|
||||
export const getUserExtensionsDir = (email: string) => join(DATA_PATH, email, 'extensions');
|
||||
|
||||
export const getNativeTasksDir = () => join(SEED_PATH, 'tasks');
|
||||
|
||||
export const getGlobalTasksDir = () => join(DATA_PATH, 'tasks');
|
||||
|
||||
export const getUserTasksDir = (email: string) => join(DATA_PATH, email, 'tasks');
|
||||
|
||||
export const getNativeProcessesDir = () => join(SEED_PATH, 'processes');
|
||||
|
||||
export const getGlobalProcessesDir = () => join(DATA_PATH, 'processes');
|
||||
|
||||
export const getUserProcessesDir = (email: string) => join(DATA_PATH, email, 'processes');
|
||||
|
||||
export const getTmpAttachmentsDir = (email: string) => join(DATA_PATH, email, 'attachments', 'tmp');
|
||||
|
||||
export const getAttachmentsDir = (email: string, sessionId: string) => join(DATA_PATH, email, 'attachments', sessionId);
|
||||
|
||||
export const getUserEmailDir = (email: string) => join(DATA_PATH, email, 'Gmail', 'emails');
|
||||
|
||||
|
||||
/** Derive a valid Linux username from a display username or email. */
|
||||
export const toShellUsername = (username: string, email: string): string => {
|
||||
const raw = username || email.split('@')[0]!;
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
getGlobalToolsDir,
|
||||
getUserToolsDir,
|
||||
getGlobalSkillsDir,
|
||||
getUserSkillsDir,
|
||||
getGlobalTasksDir,
|
||||
getUserTasksDir,
|
||||
getHomeDir,
|
||||
DATA_PATH,
|
||||
} from '@@/data-path';
|
||||
import { itemsDir, getHomeDir, DATA_PATH } from '@@/data-path';
|
||||
|
||||
type HookEntry = { type: string; command: string };
|
||||
type HookRule = { matcher?: Record<string, unknown>; hooks: HookEntry[] };
|
||||
@@ -57,14 +48,13 @@ function formatList(entries: FrontmatterEntry[]): string {
|
||||
}
|
||||
|
||||
export function generateContainerContext(email: string): string {
|
||||
const tools = dedup([...scanDir(getGlobalToolsDir(), 'TOOL.md'), ...scanDir(getUserToolsDir(email), 'TOOL.md')]);
|
||||
const skills = dedup([...scanDir(getGlobalSkillsDir(), 'SKILL.md'), ...scanDir(getUserSkillsDir(email), 'SKILL.md')]);
|
||||
const tasks = dedup([...scanDir(getGlobalTasksDir(), 'TASK.md'), ...scanDir(getUserTasksDir(email), 'TASK.md')]);
|
||||
const toolsDir = itemsDir('tools');
|
||||
const skillsDir = itemsDir('skills');
|
||||
const tasksDir = itemsDir('tasks');
|
||||
const tools = dedup(scanDir(toolsDir, 'TOOL.md'));
|
||||
const skills = dedup(scanDir(skillsDir, 'SKILL.md'));
|
||||
const tasks = dedup(scanDir(tasksDir, 'TASK.md'));
|
||||
|
||||
const globalToolsDir = getGlobalToolsDir();
|
||||
const userToolsDir = getUserToolsDir(email);
|
||||
const globalSkillsDir = getGlobalSkillsDir();
|
||||
const userSkillsDir = getUserSkillsDir(email);
|
||||
const userDataDir = join(DATA_PATH, email);
|
||||
|
||||
const content = `# Officer — User Environment
|
||||
@@ -78,14 +68,13 @@ This is an isolated Linux user environment managed by the Officer platform.
|
||||
| \`~\` | User home directory (read-write) |
|
||||
| \`~/Projects/\` | User projects |
|
||||
| \`~/Downloads/\` | Downloaded files |
|
||||
| \`${globalToolsDir}/\` | Global tools |
|
||||
| \`${userToolsDir}/\` | User tools |
|
||||
| \`${globalSkillsDir}/\` | Reference skills |
|
||||
| \`${toolsDir}/\` | Tools |
|
||||
| \`${skillsDir}/\` | Reference skills |
|
||||
| \`${userDataDir}/\` | User data (emails.db, attachments, etc.) |
|
||||
|
||||
## Available Tools
|
||||
|
||||
Tools are callable capabilities used by the Officer AI agent (Pi). Each tool has a \`TOOL.md\` with documentation and an \`index.ts\` that exports an \`execute()\` function. Read individual tool docs at \`${globalToolsDir}/<name>/TOOL.md\` or \`${userToolsDir}/<name>/TOOL.md\`.
|
||||
Tools are callable capabilities used by the Officer AI agent (Pi). Each tool has a \`TOOL.md\` with documentation and an \`index.ts\` that exports an \`execute()\` function. Read individual tool docs at \`${toolsDir}/<name>/TOOL.md\`.
|
||||
|
||||
${formatList(tools)}
|
||||
## Available Skills
|
||||
@@ -100,7 +89,7 @@ Tasks are predefined instruction sets the AI agent can execute.
|
||||
${formatList(tasks)}
|
||||
## Creating New Tools
|
||||
|
||||
Create a directory in \`${userToolsDir}/<tool-name>/\` with two files:
|
||||
Create a directory in \`${toolsDir}/<tool-name>/\` with two files:
|
||||
|
||||
**TOOL.md** — Frontmatter metadata + markdown documentation:
|
||||
\`\`\`yaml
|
||||
|
||||
@@ -21,12 +21,8 @@ const PI_CONFIG_DIR = join(require('node:os').homedir(), '.pi', 'agent');
|
||||
const getHomeDir = (email: string) => join(DATA_PATH, email, 'home');
|
||||
const getHomeDirForRole = (email: string, role: string | null): string =>
|
||||
role === 'Super Admin' && process.env.HOME_DIR ? process.env.HOME_DIR : getHomeDir(email);
|
||||
const getGlobalSkillsDir = () => join(DATA_PATH, 'skills');
|
||||
const getUserSkillsDir = (email: string) => join(DATA_PATH, email, 'skills');
|
||||
const getGlobalExtensionsDir = () => join(DATA_PATH, 'extensions');
|
||||
const getUserExtensionsDir = (email: string) => join(DATA_PATH, email, 'extensions');
|
||||
const getGlobalToolsDir = () => join(DATA_PATH, 'tools');
|
||||
const getUserToolsDir = (email: string) => join(DATA_PATH, email, 'tools');
|
||||
const OFFICER_ITEMS_DIR = process.env.OFFICER_ITEMS_DIR ?? join(process.cwd(), 'officer-items');
|
||||
const itemsDir = (type: 'skills' | 'tools' | 'extensions') => join(OFFICER_ITEMS_DIR, type);
|
||||
|
||||
function isPidAlive(pid: number): boolean {
|
||||
try {
|
||||
@@ -97,11 +93,8 @@ function collectSkillFlagsFromDir(scanDir: string, targetDir: string): string[]
|
||||
return flags;
|
||||
}
|
||||
|
||||
function collectSkillFlags(email: string): string[] {
|
||||
return [
|
||||
...collectSkillFlagsFromDir(getGlobalSkillsDir(), getGlobalSkillsDir()),
|
||||
...collectSkillFlagsFromDir(getUserSkillsDir(email), getUserSkillsDir(email)),
|
||||
];
|
||||
function collectSkillFlags(): string[] {
|
||||
return collectSkillFlagsFromDir(itemsDir('skills'), itemsDir('skills'));
|
||||
}
|
||||
|
||||
function collectExtensionFlagsFromDir(scanDir: string, targetDir: string): string[] {
|
||||
@@ -118,11 +111,8 @@ function collectExtensionFlagsFromDir(scanDir: string, targetDir: string): strin
|
||||
return flags;
|
||||
}
|
||||
|
||||
function collectExtensionFlags(email: string): string[] {
|
||||
return [
|
||||
...collectExtensionFlagsFromDir(getGlobalExtensionsDir(), getGlobalExtensionsDir()),
|
||||
...collectExtensionFlagsFromDir(getUserExtensionsDir(email), getUserExtensionsDir(email)),
|
||||
];
|
||||
function collectExtensionFlags(): string[] {
|
||||
return collectExtensionFlagsFromDir(itemsDir('extensions'), itemsDir('extensions'));
|
||||
}
|
||||
|
||||
async function resolveApiKeyForModel(model: string): Promise<string | null> {
|
||||
@@ -278,17 +268,11 @@ export async function spawnPi(options: PiSpawnOptions): Promise<void> {
|
||||
|
||||
const isSuperAdmin = role === 'Super Admin';
|
||||
const skillFlags = isSuperAdmin
|
||||
? collectSkillFlags(email)
|
||||
: [
|
||||
...collectSkillFlagsFromDir(getGlobalSkillsDir(), SANDBOX_GLOBAL_SKILLS),
|
||||
...collectSkillFlagsFromDir(getUserSkillsDir(email), `${SANDBOX_DATA}/skills`),
|
||||
];
|
||||
? collectSkillFlags()
|
||||
: collectSkillFlagsFromDir(itemsDir('skills'), SANDBOX_GLOBAL_SKILLS);
|
||||
const extensionFlags = isSuperAdmin
|
||||
? collectExtensionFlags(email)
|
||||
: [
|
||||
...collectExtensionFlagsFromDir(getGlobalExtensionsDir(), SANDBOX_GLOBAL_EXTENSIONS),
|
||||
...collectExtensionFlagsFromDir(getUserExtensionsDir(email), `${SANDBOX_DATA}/extensions`),
|
||||
];
|
||||
? collectExtensionFlags()
|
||||
: collectExtensionFlagsFromDir(itemsDir('extensions'), SANDBOX_GLOBAL_EXTENSIONS);
|
||||
|
||||
const piArgs = [
|
||||
...PI_CMD,
|
||||
@@ -311,7 +295,7 @@ export async function spawnPi(options: PiSpawnOptions): Promise<void> {
|
||||
}
|
||||
|
||||
const homeDir = getHomeDirForRole(email, role);
|
||||
const toolsDirs = [getGlobalToolsDir(), getUserToolsDir(email)].join(':');
|
||||
const toolsDirs = itemsDir('tools');
|
||||
|
||||
// Per-session JWT so tools (e.g. the gmail proxy) can call back to dev-platform
|
||||
// as the owning user. Mirrors the signin payload shape so userMiddleware accepts it.
|
||||
@@ -338,7 +322,7 @@ export async function spawnPi(options: PiSpawnOptions): Promise<void> {
|
||||
proc = Bun.spawn(piArgs, { cwd, stdin: 'pipe', stdout: 'pipe', stderr: 'pipe', env });
|
||||
} else {
|
||||
// Non-admin: run inside bwrap sandbox
|
||||
const sandboxToolsDirs = [SANDBOX_GLOBAL_TOOLS, `${SANDBOX_DATA}/tools`].join(':');
|
||||
const sandboxToolsDirs = SANDBOX_GLOBAL_TOOLS;
|
||||
const prefix = buildSandboxPrefix(email);
|
||||
|
||||
// Pi-specific env vars
|
||||
|
||||
@@ -10,6 +10,7 @@ const BUN_DIR = (() => {
|
||||
|
||||
const PROJECT_ROOT = resolve(import.meta.dir, '../../..');
|
||||
const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
||||
const OFFICER_ITEMS_DIR = process.env.OFFICER_ITEMS_DIR ?? join(process.cwd(), 'officer-items');
|
||||
const HOST_HOME = process.env.HOME!;
|
||||
|
||||
// Resolve the OS username for runuser to drop privileges inside the sandbox
|
||||
@@ -31,9 +32,9 @@ export const SANDBOX_GLOBAL_TOOLS = `${SANDBOX_GLOBAL_ROOT}/tools`;
|
||||
// Callers can append extra `--setenv` args before calling `buildRunuserSuffix()`.
|
||||
export function buildSandboxPrefix(email: string): string[] {
|
||||
const userDataDir = join(DATA_PATH, email);
|
||||
const globalSkillsDir = join(DATA_PATH, 'skills');
|
||||
const globalToolsDir = join(DATA_PATH, 'tools');
|
||||
const globalExtensionsDir = join(DATA_PATH, 'extensions');
|
||||
const globalSkillsDir = join(OFFICER_ITEMS_DIR, 'skills');
|
||||
const globalToolsDir = join(OFFICER_ITEMS_DIR, 'tools');
|
||||
const globalExtensionsDir = join(OFFICER_ITEMS_DIR, 'extensions');
|
||||
|
||||
const args = [
|
||||
'sudo',
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { DATA_PATH } from './data-path';
|
||||
import { parseSeedVersion } from './sync-version';
|
||||
import { upsertNativeTask, deleteNativeTasksNotIn } from 'officerdb';
|
||||
|
||||
const MARKETPLACE_URL = process.env.MARKETPLACE_URL ?? 'https://marketplace.officer.dev';
|
||||
const GLOBAL_TOOLS_DIR = join(DATA_PATH, 'tools');
|
||||
|
||||
// ── Tool sync types ──
|
||||
|
||||
type ToolInput = {
|
||||
type: string;
|
||||
description: string;
|
||||
values?: string;
|
||||
optional?: string | boolean;
|
||||
};
|
||||
|
||||
type MarketplaceTool = {
|
||||
dirName: string;
|
||||
name: string;
|
||||
label: string;
|
||||
description: string;
|
||||
body: string;
|
||||
version: number;
|
||||
language: string;
|
||||
targets: string;
|
||||
inputs: Record<string, ToolInput>;
|
||||
implementation: string;
|
||||
};
|
||||
|
||||
type ToolSyncResponse = {
|
||||
categories: Array<{ name: string; tools: MarketplaceTool[] }>;
|
||||
uncategorized: MarketplaceTool[];
|
||||
};
|
||||
|
||||
// ── Task sync types ──
|
||||
|
||||
type MarketplaceTask = {
|
||||
dirName: string;
|
||||
name: string;
|
||||
description: string;
|
||||
body: string;
|
||||
version: number;
|
||||
mode: string;
|
||||
language: string | null;
|
||||
implementation: string | null;
|
||||
args: string[] | null;
|
||||
inputs: Record<string, unknown> | null;
|
||||
trigger: unknown[] | null;
|
||||
config: Record<string, unknown> | null;
|
||||
};
|
||||
|
||||
type TaskSyncResponse = {
|
||||
categories: Array<{ name: string; tasks: MarketplaceTask[] }>;
|
||||
uncategorized: MarketplaceTask[];
|
||||
};
|
||||
|
||||
// ── Tool sync (writes files to disk) ──
|
||||
|
||||
function buildToolMd(tool: MarketplaceTool): string {
|
||||
const lines = ['---'];
|
||||
lines.push(`name: ${tool.name}`);
|
||||
lines.push(`label: ${tool.label}`);
|
||||
lines.push(`description: ${tool.description}`);
|
||||
lines.push(`version: ${tool.version}`);
|
||||
lines.push(`language: ${tool.language}`);
|
||||
if (tool.targets && tool.targets !== 'all') {
|
||||
lines.push(`targets: ${tool.targets}`);
|
||||
}
|
||||
|
||||
if (tool.inputs && Object.keys(tool.inputs).length > 0) {
|
||||
lines.push('inputs:');
|
||||
for (const [key, input] of Object.entries(tool.inputs)) {
|
||||
lines.push(` ${key}:`);
|
||||
lines.push(` type: ${input.type}`);
|
||||
if (input.values) {
|
||||
lines.push(` values: ${input.values}`);
|
||||
}
|
||||
if (String(input.optional) === 'true') {
|
||||
lines.push(` optional: true`);
|
||||
}
|
||||
lines.push(` description: "${input.description}"`);
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('---');
|
||||
lines.push('');
|
||||
lines.push(tool.body);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
export async function syncMarketplaceTools(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch(`${MARKETPLACE_URL}/api/tools/native`);
|
||||
if (!res.ok) {
|
||||
console.error(`[marketplace] Failed to fetch tools: ${res.status} ${res.statusText}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = (await res.json()) as ToolSyncResponse;
|
||||
|
||||
mkdirSync(GLOBAL_TOOLS_DIR, { recursive: true });
|
||||
|
||||
const allTools: MarketplaceTool[] = [];
|
||||
for (const category of data.categories) {
|
||||
allTools.push(...category.tools);
|
||||
}
|
||||
if (data.uncategorized) {
|
||||
allTools.push(...data.uncategorized);
|
||||
}
|
||||
|
||||
for (const tool of allTools) {
|
||||
const targetDir = join(GLOBAL_TOOLS_DIR, tool.dirName);
|
||||
const targetToolMd = join(targetDir, 'TOOL.md');
|
||||
|
||||
if (existsSync(targetToolMd)) {
|
||||
const existingVersion = parseSeedVersion(readFileSync(targetToolMd, 'utf-8'));
|
||||
if (tool.version <= existingVersion) continue;
|
||||
}
|
||||
|
||||
mkdirSync(targetDir, { recursive: true });
|
||||
writeFileSync(targetToolMd, buildToolMd(tool), 'utf-8');
|
||||
writeFileSync(join(targetDir, 'index.ts'), tool.implementation, 'utf-8');
|
||||
|
||||
console.log(`[marketplace] Synced tool: ${tool.dirName} (v${tool.version})`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[marketplace] Failed to sync tools:', err instanceof Error ? err.message : err);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Task sync (upserts into officer_db) ──
|
||||
|
||||
export async function syncMarketplaceTasks(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch(`${MARKETPLACE_URL}/api/tasks/native`);
|
||||
if (!res.ok) {
|
||||
console.error(`[marketplace] Failed to fetch tasks: ${res.status} ${res.statusText}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = (await res.json()) as TaskSyncResponse;
|
||||
|
||||
const allTasks: MarketplaceTask[] = [];
|
||||
for (const category of data.categories) {
|
||||
allTasks.push(...category.tasks);
|
||||
}
|
||||
if (data.uncategorized) {
|
||||
allTasks.push(...data.uncategorized);
|
||||
}
|
||||
|
||||
for (const task of allTasks) {
|
||||
await upsertNativeTask({
|
||||
dirName: task.dirName,
|
||||
name: task.name,
|
||||
description: task.description,
|
||||
body: task.body,
|
||||
version: task.version,
|
||||
mode: task.mode,
|
||||
language: task.language,
|
||||
implementation: task.implementation,
|
||||
args: task.args,
|
||||
inputs: task.inputs,
|
||||
trigger: task.trigger,
|
||||
config: task.config,
|
||||
});
|
||||
|
||||
console.log(`[marketplace] Synced task: ${task.dirName} (v${task.version})`);
|
||||
}
|
||||
|
||||
// Delete native tasks that no longer exist in the marketplace
|
||||
const activeDirNames = allTasks.map((t) => t.dirName);
|
||||
const deleted = await deleteNativeTasksNotIn(activeDirNames);
|
||||
for (const row of deleted) {
|
||||
console.log(`[marketplace] Removed task: ${row.dirName}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[marketplace] Failed to sync tasks:', err instanceof Error ? err.message : err);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
/**
|
||||
* Extract the `version` field from YAML frontmatter.
|
||||
* Returns 0 if no version is found.
|
||||
*/
|
||||
export function parseSeedVersion(content: string): number {
|
||||
const match = content.match(/^---\n([\s\S]*?)\n---/);
|
||||
if (!match) return 0;
|
||||
const versionMatch = match[1]!.match(/^version:\s*(\d+)/m);
|
||||
return versionMatch ? parseInt(versionMatch[1]!, 10) : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user