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:
@@ -4,5 +4,6 @@ POSTGRES_URL="postgres://postgres:password@localhost:5432/officer"
|
|||||||
MAIL_TRANSPORT="smtp://localhost:1025"
|
MAIL_TRANSPORT="smtp://localhost:1025"
|
||||||
PUBLIC_URL=http://localhost:9000
|
PUBLIC_URL=http://localhost:9000
|
||||||
DATA_PATH=/path/to/data
|
DATA_PATH=/path/to/data
|
||||||
|
OFFICER_ITEMS_DIR=/path/to/officer-items
|
||||||
HOME_DIR=/home/user
|
HOME_DIR=/home/user
|
||||||
BROWSER_RELAY_PORT=18792
|
BROWSER_RELAY_PORT=18792
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/**
|
||||||
|
* One-time migration: consolidate every agent item into the flat, file-based store
|
||||||
|
* ($OFFICER_ITEMS_DIR) and export the DB-backed `tasks` table to TASK.md files.
|
||||||
|
*
|
||||||
|
* Idempotent — safe to re-run. Run this BEFORE applying the drop-tables DB migration
|
||||||
|
* (it reads the `tasks` table, which still exists until that migration runs).
|
||||||
|
*
|
||||||
|
* Sources, in precedence order (later overwrites earlier on a dirName collision):
|
||||||
|
* - tasks: officer_db.tasks rows (native → global → user)
|
||||||
|
* - skills / tools / processes / extensions: $DATA_PATH/<type> then $DATA_PATH/<email>/<type>
|
||||||
|
* - tools: marketplace registry tools not already present (archive safety)
|
||||||
|
*
|
||||||
|
* Usage: bun run scripts/migrate-items-to-files.ts
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { join, resolve } from 'node:path';
|
||||||
|
import { readdir, cp } from 'node:fs/promises';
|
||||||
|
import { existsSync } from 'node:fs';
|
||||||
|
import { db } from 'officerdb/db';
|
||||||
|
import { sql } from 'drizzle-orm';
|
||||||
|
import { itemsDir, ensureItemDirs, DATA_PATH, OFFICER_ITEMS_DIR, type ItemType } from '../src/servers/data-path';
|
||||||
|
import { importTask } from '../src/servers/api/tasks/task-files';
|
||||||
|
|
||||||
|
ensureItemDirs();
|
||||||
|
console.log(`Target store: ${OFFICER_ITEMS_DIR}`);
|
||||||
|
|
||||||
|
async function listSubdirs(dir: string): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
return (await readdir(dir, { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => e.name);
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 1. Tasks: Postgres → TASK.md files ──
|
||||||
|
// Order native → global → user so user/global overwrite native on a dirName collision.
|
||||||
|
const scopeRank = (s: string) => (s === 'user' ? 2 : s === 'global' ? 1 : 0);
|
||||||
|
|
||||||
|
console.log('\n── Tasks (DB → files) ──');
|
||||||
|
let taskRows: Record<string, unknown>[] = [];
|
||||||
|
try {
|
||||||
|
taskRows = (await db.execute(sql.raw('SELECT * FROM tasks'))) as unknown as Record<string, unknown>[];
|
||||||
|
} catch (err) {
|
||||||
|
console.log(` could not read tasks table (already dropped?): ${err instanceof Error ? err.message : err}`);
|
||||||
|
}
|
||||||
|
taskRows.sort((a, b) => scopeRank(String(a.scope)) - scopeRank(String(b.scope)));
|
||||||
|
|
||||||
|
for (const row of taskRows) {
|
||||||
|
const dirName = String(row.dir_name);
|
||||||
|
await importTask(dirName, {
|
||||||
|
name: String(row.name ?? dirName),
|
||||||
|
description: row.description == null ? null : String(row.description),
|
||||||
|
version: Number(row.version) || 1,
|
||||||
|
mode: String(row.mode ?? 'agentic'),
|
||||||
|
language: row.language == null ? null : String(row.language),
|
||||||
|
args: (row.args as string[] | null) ?? null,
|
||||||
|
tags: (row.tags as string[] | null) ?? null,
|
||||||
|
tools: (row.tools as string[] | null) ?? null,
|
||||||
|
skills: (row.skills as string[] | null) ?? null,
|
||||||
|
inputs: row.inputs ?? null,
|
||||||
|
outputs: row.outputs ?? null,
|
||||||
|
dependencies: row.dependencies ?? null,
|
||||||
|
config: row.config ?? null,
|
||||||
|
trigger: row.trigger ?? null,
|
||||||
|
body: row.body == null ? '' : String(row.body),
|
||||||
|
implementation: row.implementation == null ? null : String(row.implementation),
|
||||||
|
});
|
||||||
|
console.log(` ${dirName} (${row.scope})`);
|
||||||
|
}
|
||||||
|
console.log(` ${taskRows.length} task file(s) written`);
|
||||||
|
|
||||||
|
// ── 2. On-disk items → flat store ──
|
||||||
|
const DISK_TYPES: ItemType[] = ['skills', 'tools', 'processes', 'extensions'];
|
||||||
|
|
||||||
|
async function copyItemsFrom(srcTypeDir: string, type: ItemType): Promise<number> {
|
||||||
|
let n = 0;
|
||||||
|
for (const name of await listSubdirs(srcTypeDir)) {
|
||||||
|
await cp(join(srcTypeDir, name), join(itemsDir(type), name), { recursive: true, force: true });
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n── Disk items (DATA_PATH → flat store) ──');
|
||||||
|
const emailDirs = (await listSubdirs(DATA_PATH)).filter((n) => n.includes('@'));
|
||||||
|
|
||||||
|
for (const type of DISK_TYPES) {
|
||||||
|
let n = await copyItemsFrom(join(DATA_PATH, type), type); // global
|
||||||
|
for (const email of emailDirs) n += await copyItemsFrom(join(DATA_PATH, email, type), type); // user (overwrites)
|
||||||
|
console.log(` ${type}: ${n} item(s) copied`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 3. Marketplace registry tools not already present (archive safety) ──
|
||||||
|
const MARKETPLACE_REGISTRY = process.env.MARKETPLACE_REGISTRY ?? resolve(import.meta.dir, '../../marketplace/registry');
|
||||||
|
console.log(`\n── Marketplace registry (${MARKETPLACE_REGISTRY}) ──`);
|
||||||
|
if (existsSync(MARKETPLACE_REGISTRY)) {
|
||||||
|
let n = 0;
|
||||||
|
for (const name of await listSubdirs(join(MARKETPLACE_REGISTRY, 'tools'))) {
|
||||||
|
const target = join(itemsDir('tools'), name);
|
||||||
|
if (existsSync(target)) continue; // don't clobber a synced/user version
|
||||||
|
await cp(join(MARKETPLACE_REGISTRY, 'tools', name), target, { recursive: true });
|
||||||
|
n++;
|
||||||
|
console.log(` tool ${name} (from registry)`);
|
||||||
|
}
|
||||||
|
console.log(` ${n} registry tool(s) added`);
|
||||||
|
console.log(' registry tasks come from the DB export above (native scope) — skipped here');
|
||||||
|
} else {
|
||||||
|
console.log(' registry not found, skipping');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\nDone. Verify counts in the UI, then apply the drop-tables DB migration.');
|
||||||
|
process.exit(0);
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
* - DB: user_settings, user_state, user_integrations, dock_configs,
|
* - DB: user_settings, user_state, user_integrations, dock_configs,
|
||||||
* chat_sessions (cascades chat_messages), chat_groups,
|
* chat_sessions (cascades chat_messages), chat_groups,
|
||||||
* dashboards, screens, projects,
|
* dashboards, screens, projects,
|
||||||
* tasks, skills, processes, tools, extensions (+ item_chats),
|
|
||||||
* task_logs, queue_jobs, terminal_containers
|
* task_logs, queue_jobs, terminal_containers
|
||||||
* - Filesystem: entire $DATA_PATH/<email>/ directory
|
* - Filesystem: entire $DATA_PATH/<email>/ directory
|
||||||
* (home, settings, state, dashboards, chat_sessions, emails.db,
|
* (home, settings, state, dashboards, chat_sessions, emails.db,
|
||||||
@@ -76,7 +75,7 @@ const tables = [
|
|||||||
'user_state',
|
'user_state',
|
||||||
'user_integrations',
|
'user_integrations',
|
||||||
'dock_configs',
|
'dock_configs',
|
||||||
'chat_sessions', // cascades chat_messages
|
'chat_sessions', // cascades chat_messages
|
||||||
'chat_groups',
|
'chat_groups',
|
||||||
'dashboards',
|
'dashboards',
|
||||||
'screens',
|
'screens',
|
||||||
@@ -92,21 +91,8 @@ for (const table of tables) {
|
|||||||
console.log(` ${table}: ${count} rows deleted`);
|
console.log(` ${table}: ${count} rows deleted`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Agent items: tasks, skills, processes, tools, extensions
|
// Agent items (skills, tools, tasks, processes, extensions) are now flat files in
|
||||||
// These have nullable user_id — delete only rows belonging to this user
|
// $OFFICER_ITEMS_DIR, shared and not user-owned — intentionally left untouched by a user reset.
|
||||||
const agentTables = ['tasks', 'skills', 'processes', 'tools', 'extensions'];
|
|
||||||
|
|
||||||
for (const table of agentTables) {
|
|
||||||
// First collect item IDs to clean up item_chats
|
|
||||||
const items = await db.execute(sql.raw(`SELECT id FROM ${table} WHERE user_id = ${userId}`));
|
|
||||||
if (items.length > 0) {
|
|
||||||
const ids = items.map((r: Record<string, unknown>) => r.id).join(',');
|
|
||||||
const chatResult = await db.execute(sql.raw(`DELETE FROM item_chats WHERE item_type = '${table}' AND item_id IN (${ids})`));
|
|
||||||
console.log(` item_chats (${table}): ${chatResult.length ?? 0} rows deleted`);
|
|
||||||
}
|
|
||||||
const result = await db.execute(sql.raw(`DELETE FROM ${table} WHERE user_id = ${userId}`));
|
|
||||||
console.log(` ${table}: ${result.length ?? 0} rows deleted`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Queue job files ──
|
// ── Queue job files ──
|
||||||
|
|
||||||
|
|||||||
@@ -8,25 +8,21 @@ import { Play, Trash2, Terminal, Bot, Workflow } from 'lucide-react';
|
|||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||||
import { useClient } from 'hooks/useClient';
|
import { useClient } from 'hooks/useClient';
|
||||||
import { useAuth } from 'hooks/useAuth';
|
|
||||||
import { usePanelChannel } from 'hooks/usePanelChannel';
|
import { usePanelChannel } from 'hooks/usePanelChannel';
|
||||||
import { Card } from '@/components/Card';
|
import { Card } from '@/components/Card';
|
||||||
import { TaskRunnerModal } from 'officerdev';
|
import { TaskRunnerModal } from 'officerdev';
|
||||||
import type { TaskSummary } from 'officerdev';
|
import type { TaskSummary } from 'officerdev';
|
||||||
|
|
||||||
type TaskDetail = {
|
type TaskDetail = {
|
||||||
id: number;
|
|
||||||
dirName: string;
|
dirName: string;
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
scope: string;
|
|
||||||
mode: string;
|
mode: string;
|
||||||
language: string | null;
|
language: string | null;
|
||||||
body: string | null;
|
body: string | null;
|
||||||
inputs: Record<string, unknown> | null;
|
inputs: Record<string, unknown> | null;
|
||||||
config: { steps?: Array<{ task: string; foreach?: string }> } | null;
|
config: { steps?: Array<{ task: string; foreach?: string }> } | null;
|
||||||
version: number | null;
|
version: number | null;
|
||||||
userId: number | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const modeLabels: Record<string, { label: string; icon: typeof Terminal; color: string }> = {
|
const modeLabels: Record<string, { label: string; icon: typeof Terminal; color: string }> = {
|
||||||
@@ -38,13 +34,14 @@ const modeLabels: Record<string, { label: string; icon: typeof Terminal; color:
|
|||||||
export const AutomationDetail = () => {
|
export const AutomationDetail = () => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
const qc = useQueryClient();
|
const qc = useQueryClient();
|
||||||
const { user } = useAuth();
|
|
||||||
const [selected, setSelected] = usePanelChannel<TaskSummary | null>('automation:selected-task', null);
|
const [selected, setSelected] = usePanelChannel<TaskSummary | null>('automation:selected-task', null);
|
||||||
const [deleteConfirm, setDeleteConfirm] = useState(false);
|
const [deleteConfirm, setDeleteConfirm] = useState(false);
|
||||||
const [runTask, setRunTask] = useState<TaskSummary | null>(null);
|
const [runTask, setRunTask] = useState<TaskSummary | null>(null);
|
||||||
|
|
||||||
// Reset delete confirm when selection changes
|
// Reset delete confirm when selection changes
|
||||||
useEffect(() => { setDeleteConfirm(false); }, [selected?.dirName]);
|
useEffect(() => {
|
||||||
|
setDeleteConfirm(false);
|
||||||
|
}, [selected?.dirName]);
|
||||||
|
|
||||||
const { data: detail } = useQuery<TaskDetail>({
|
const { data: detail } = useQuery<TaskDetail>({
|
||||||
queryKey: ['tasks', selected?.dirName],
|
queryKey: ['tasks', selected?.dirName],
|
||||||
@@ -73,7 +70,6 @@ export const AutomationDetail = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const canModify = selected.scope === 'user' || user?.role === 'Super Admin';
|
|
||||||
const mode = modeLabels[selected.mode] ?? modeLabels.agentic!;
|
const mode = modeLabels[selected.mode] ?? modeLabels.agentic!;
|
||||||
const ModeIcon = mode.icon;
|
const ModeIcon = mode.icon;
|
||||||
|
|
||||||
@@ -88,7 +84,9 @@ export const AutomationDetail = () => {
|
|||||||
<span className="text-sm font-medium text-duck-dark/70 dark:text-foreground/70 flex-1 truncate">
|
<span className="text-sm font-medium text-duck-dark/70 dark:text-foreground/70 flex-1 truncate">
|
||||||
{detail?.name ?? selected.name}
|
{detail?.name ?? selected.name}
|
||||||
</span>
|
</span>
|
||||||
<span className={`shrink-0 inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] font-medium ${mode.color}`}>
|
<span
|
||||||
|
className={`shrink-0 inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] font-medium ${mode.color}`}
|
||||||
|
>
|
||||||
<ModeIcon className="h-2.5 w-2.5" />
|
<ModeIcon className="h-2.5 w-2.5" />
|
||||||
{mode.label}
|
{mode.label}
|
||||||
</span>
|
</span>
|
||||||
@@ -99,15 +97,13 @@ export const AutomationDetail = () => {
|
|||||||
>
|
>
|
||||||
<Play className="h-3.5 w-3.5 text-duck-teal" />
|
<Play className="h-3.5 w-3.5 text-duck-teal" />
|
||||||
</button>
|
</button>
|
||||||
{canModify && (
|
<button
|
||||||
<button
|
onClick={() => setDeleteConfirm(true)}
|
||||||
onClick={() => setDeleteConfirm(true)}
|
className="p-1 rounded hover:bg-red-50 dark:hover:bg-red-500/10 cursor-pointer transition-colors"
|
||||||
className="p-1 rounded hover:bg-red-50 dark:hover:bg-red-500/10 cursor-pointer transition-colors"
|
title="Delete"
|
||||||
title="Delete"
|
>
|
||||||
>
|
<Trash2 className="h-3.5 w-3.5 text-duck-dark/50 dark:text-foreground/50 hover:text-red-500" />
|
||||||
<Trash2 className="h-3.5 w-3.5 text-duck-dark/50 dark:text-foreground/50 hover:text-red-500" />
|
</button>
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Content */}
|
{/* Content */}
|
||||||
@@ -119,11 +115,6 @@ export const AutomationDetail = () => {
|
|||||||
|
|
||||||
{/* Meta badges */}
|
{/* Meta badges */}
|
||||||
<div className="flex flex-wrap gap-2 mb-4">
|
<div className="flex flex-wrap gap-2 mb-4">
|
||||||
{detail?.scope && detail.scope !== 'user' && (
|
|
||||||
<span className="text-[10px] px-1.5 py-0.5 rounded bg-duck-dark/8 dark:bg-foreground/8 text-duck-dark/50 dark:text-foreground/50 font-medium">
|
|
||||||
{detail.scope}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
{detail?.language && (
|
{detail?.language && (
|
||||||
<span className="text-[10px] px-1.5 py-0.5 rounded bg-duck-dark/8 dark:bg-foreground/8 text-duck-dark/50 dark:text-foreground/50 font-medium">
|
<span className="text-[10px] px-1.5 py-0.5 rounded bg-duck-dark/8 dark:bg-foreground/8 text-duck-dark/50 dark:text-foreground/50 font-medium">
|
||||||
{detail.language}
|
{detail.language}
|
||||||
@@ -139,13 +130,24 @@ export const AutomationDetail = () => {
|
|||||||
{/* Pipeline steps */}
|
{/* Pipeline steps */}
|
||||||
{hasSteps && (
|
{hasSteps && (
|
||||||
<div className="mb-4">
|
<div className="mb-4">
|
||||||
<h3 className="text-xs font-medium text-duck-dark/50 dark:text-foreground/50 uppercase tracking-wider mb-2">Pipeline Steps</h3>
|
<h3 className="text-xs font-medium text-duck-dark/50 dark:text-foreground/50 uppercase tracking-wider mb-2">
|
||||||
|
Pipeline Steps
|
||||||
|
</h3>
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
{detail!.config!.steps!.map((step, i) => (
|
{detail!.config!.steps!.map((step, i) => (
|
||||||
<div key={i} className="flex items-center gap-2 text-sm text-duck-dark/70 dark:text-foreground/70 px-2 py-1 rounded bg-duck-dark/3 dark:bg-foreground/3">
|
<div
|
||||||
<span className="w-5 text-center font-mono text-xs text-duck-dark/40 dark:text-foreground/40">{i + 1}</span>
|
key={i}
|
||||||
|
className="flex items-center gap-2 text-sm text-duck-dark/70 dark:text-foreground/70 px-2 py-1 rounded bg-duck-dark/3 dark:bg-foreground/3"
|
||||||
|
>
|
||||||
|
<span className="w-5 text-center font-mono text-xs text-duck-dark/40 dark:text-foreground/40">
|
||||||
|
{i + 1}
|
||||||
|
</span>
|
||||||
<span className="font-medium">{step.task}</span>
|
<span className="font-medium">{step.task}</span>
|
||||||
{step.foreach && <span className="text-xs text-duck-dark/40 dark:text-foreground/40">(foreach: {step.foreach})</span>}
|
{step.foreach && (
|
||||||
|
<span className="text-xs text-duck-dark/40 dark:text-foreground/40">
|
||||||
|
(foreach: {step.foreach})
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -155,15 +157,26 @@ export const AutomationDetail = () => {
|
|||||||
{/* Inputs */}
|
{/* Inputs */}
|
||||||
{hasInputs && (
|
{hasInputs && (
|
||||||
<div className="mb-4">
|
<div className="mb-4">
|
||||||
<h3 className="text-xs font-medium text-duck-dark/50 dark:text-foreground/50 uppercase tracking-wider mb-2">Inputs</h3>
|
<h3 className="text-xs font-medium text-duck-dark/50 dark:text-foreground/50 uppercase tracking-wider mb-2">
|
||||||
|
Inputs
|
||||||
|
</h3>
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
{Object.entries(detail!.inputs!).map(([key, def]) => {
|
{Object.entries(detail!.inputs!).map(([key, def]) => {
|
||||||
const d = def as { type?: string; description?: string; default?: string };
|
const d = def as { type?: string; description?: string; default?: string };
|
||||||
return (
|
return (
|
||||||
<div key={key} className="flex items-baseline gap-2 text-sm px-2 py-1 rounded bg-duck-dark/3 dark:bg-foreground/3">
|
<div
|
||||||
|
key={key}
|
||||||
|
className="flex items-baseline gap-2 text-sm px-2 py-1 rounded bg-duck-dark/3 dark:bg-foreground/3"
|
||||||
|
>
|
||||||
<span className="font-mono text-xs text-duck-teal">{key}</span>
|
<span className="font-mono text-xs text-duck-teal">{key}</span>
|
||||||
{d.type && <span className="text-[10px] text-duck-dark/40 dark:text-foreground/40">{d.type}</span>}
|
{d.type && (
|
||||||
{d.description && <span className="text-xs text-duck-dark/50 dark:text-foreground/50 flex-1">{d.description}</span>}
|
<span className="text-[10px] text-duck-dark/40 dark:text-foreground/40">{d.type}</span>
|
||||||
|
)}
|
||||||
|
{d.description && (
|
||||||
|
<span className="text-xs text-duck-dark/50 dark:text-foreground/50 flex-1">
|
||||||
|
{d.description}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
@@ -208,7 +221,9 @@ export const AutomationDetail = () => {
|
|||||||
{runTask && (
|
{runTask && (
|
||||||
<TaskRunnerModal
|
<TaskRunnerModal
|
||||||
open
|
open
|
||||||
onOpenChange={(open) => { if (!open) setRunTask(null); }}
|
onOpenChange={(open) => {
|
||||||
|
if (!open) setRunTask(null);
|
||||||
|
}}
|
||||||
task={runTask}
|
task={runTask}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -8,14 +8,12 @@ import { ArrowLeft, Pencil, Plus, Check, X, Trash2, Search, ChevronRight } from
|
|||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||||
import { useClient } from 'hooks/useClient';
|
import { useClient } from 'hooks/useClient';
|
||||||
import { useAuth } from 'hooks/useAuth';
|
|
||||||
import { Card } from '@/components/Card';
|
import { Card } from '@/components/Card';
|
||||||
import { usePiChat, EmbeddableChat } from 'officerdev';
|
import { usePiChat, EmbeddableChat } from 'officerdev';
|
||||||
type CapabilitySummary = {
|
type CapabilitySummary = {
|
||||||
dirName: string;
|
dirName: string;
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
scope: 'native' | 'global' | 'user';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CapabilityDetail = CapabilitySummary & {
|
export type CapabilityDetail = CapabilitySummary & {
|
||||||
@@ -276,9 +274,9 @@ export const CapabilityChat = ({
|
|||||||
}: CapabilityChatProps) => {
|
}: CapabilityChatProps) => {
|
||||||
const seedFile = `${kind.toUpperCase()}.md`;
|
const seedFile = `${kind.toUpperCase()}.md`;
|
||||||
const genericPrefix = `<frontmatter>\ninput file: ${filePath}\n${seedFile}: ${filePath}\ndir: ${resourceDir}\n\nBe aware of any extra files alongside the same dir as the ${kind} file we're handling, for possible extra context. You can also, if pertinent, create scripts or other files that will help you in the future.\n</frontmatter>`;
|
const genericPrefix = `<frontmatter>\ninput file: ${filePath}\n${seedFile}: ${filePath}\ndir: ${resourceDir}\n\nBe aware of any extra files alongside the same dir as the ${kind} file we're handling, for possible extra context. You can also, if pertinent, create scripts or other files that will help you in the future.\n</frontmatter>`;
|
||||||
const promptFrontmatter = isNew ? buildCreationPrefix(kind, filePath, resourceDir) ?? genericPrefix : genericPrefix;
|
const promptFrontmatter = isNew ? (buildCreationPrefix(kind, filePath, resourceDir) ?? genericPrefix) : genericPrefix;
|
||||||
const defaultInput = isNew
|
const defaultInput = isNew
|
||||||
? description ?? `Help me create the content for this new ${kind} file`
|
? (description ?? `Help me create the content for this new ${kind} file`)
|
||||||
: `Help me understand and improve this ${kind} file`;
|
: `Help me understand and improve this ${kind} file`;
|
||||||
|
|
||||||
const pi = usePiChat(undefined, undefined, { replaceUrl: false });
|
const pi = usePiChat(undefined, undefined, { replaceUrl: false });
|
||||||
@@ -294,14 +292,7 @@ export const CapabilityChat = ({
|
|||||||
wasGenerating.current = pi.isGenerating;
|
wasGenerating.current = pi.isGenerating;
|
||||||
}, [pi.isGenerating]);
|
}, [pi.isGenerating]);
|
||||||
|
|
||||||
return (
|
return <EmbeddableChat chat={pi} defaultInput={defaultInput} promptPrefix={promptFrontmatter} className="h-full" />;
|
||||||
<EmbeddableChat
|
|
||||||
chat={pi}
|
|
||||||
defaultInput={defaultInput}
|
|
||||||
promptPrefix={promptFrontmatter}
|
|
||||||
className="h-full"
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const FrontmatterBlock = ({ yaml }: { yaml: string }) => {
|
export const FrontmatterBlock = ({ yaml }: { yaml: string }) => {
|
||||||
@@ -323,7 +314,17 @@ export const FrontmatterBlock = ({ yaml }: { yaml: string }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CapabilityList = ({ kind, endpoint, queryKey, selected, onSelect, onCreate, search: externalSearch, showCreate, onShowCreateChange }: CapabilityListProps) => {
|
export const CapabilityList = ({
|
||||||
|
kind,
|
||||||
|
endpoint,
|
||||||
|
queryKey,
|
||||||
|
selected,
|
||||||
|
onSelect,
|
||||||
|
onCreate,
|
||||||
|
search: externalSearch,
|
||||||
|
showCreate,
|
||||||
|
onShowCreateChange,
|
||||||
|
}: CapabilityListProps) => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
const qc = useQueryClient();
|
const qc = useQueryClient();
|
||||||
const [internalCreating, setInternalCreating] = useState(false);
|
const [internalCreating, setInternalCreating] = useState(false);
|
||||||
@@ -437,13 +438,6 @@ export const CapabilityList = ({ kind, endpoint, queryKey, selected, onSelect, o
|
|||||||
>
|
>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className="text-sm font-medium text-duck-dark truncate">{item.name}</span>
|
<span className="text-sm font-medium text-duck-dark truncate">{item.name}</span>
|
||||||
<span
|
|
||||||
className={`shrink-0 text-[10px] px-1.5 py-0.5 rounded-full font-medium ${
|
|
||||||
item.scope === 'user' ? 'bg-duck-teal/20 text-duck-teal' : 'bg-duck-dark/10 text-duck-dark/60'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{item.scope}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
{item.description && <p className="text-xs text-duck-dark/50 mt-1 line-clamp-2">{item.description}</p>}
|
{item.description && <p className="text-xs text-duck-dark/50 mt-1 line-clamp-2">{item.description}</p>}
|
||||||
</button>
|
</button>
|
||||||
@@ -462,7 +456,6 @@ export const CapabilityList = ({ kind, endpoint, queryKey, selected, onSelect, o
|
|||||||
export const CapabilityPage = ({ kind, endpoint, queryKey }: CapabilityPageProps) => {
|
export const CapabilityPage = ({ kind, endpoint, queryKey }: CapabilityPageProps) => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
const qc = useQueryClient();
|
const qc = useQueryClient();
|
||||||
const { user } = useAuth();
|
|
||||||
const [selected, setSelected] = useState<string | null>(null);
|
const [selected, setSelected] = useState<string | null>(null);
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [isNew, setIsNew] = useState(false);
|
const [isNew, setIsNew] = useState(false);
|
||||||
@@ -544,7 +537,7 @@ export const CapabilityPage = ({ kind, endpoint, queryKey }: CapabilityPageProps
|
|||||||
<span className="text-sm font-medium text-duck-dark/70 flex-1">
|
<span className="text-sm font-medium text-duck-dark/70 flex-1">
|
||||||
{detail?.name ?? `Select a ${kind.toLowerCase()}`}
|
{detail?.name ?? `Select a ${kind.toLowerCase()}`}
|
||||||
</span>
|
</span>
|
||||||
{detail && (detail.scope === 'user' || user?.role === 'Super Admin') && (
|
{detail && (
|
||||||
<>
|
<>
|
||||||
<button
|
<button
|
||||||
onClick={() => setEditing((e) => !e)}
|
onClick={() => setEditing((e) => !e)}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
DROP TABLE "extensions" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "item_chats" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "processes" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "skills" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "tasks" CASCADE;--> statement-breakpoint
|
||||||
|
DROP TABLE "tools" CASCADE;
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -29,6 +29,13 @@
|
|||||||
"when": 1773040399200,
|
"when": 1773040399200,
|
||||||
"tag": "0003_perpetual_james_howlett",
|
"tag": "0003_perpetual_james_howlett",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 4,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1784593790333,
|
||||||
|
"tag": "0004_true_annihilus",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -72,17 +72,6 @@ export {
|
|||||||
deleteSavedSession,
|
deleteSavedSession,
|
||||||
} from './queries/saved-sessions';
|
} from './queries/saved-sessions';
|
||||||
|
|
||||||
export {
|
|
||||||
getTasksForUser,
|
|
||||||
getTaskById,
|
|
||||||
getTaskByDirName,
|
|
||||||
createTask,
|
|
||||||
updateTask,
|
|
||||||
deleteTask,
|
|
||||||
upsertNativeTask,
|
|
||||||
deleteNativeTasksNotIn,
|
|
||||||
} from './queries/tasks';
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
createPipelineJob,
|
createPipelineJob,
|
||||||
getPipelineJob,
|
getPipelineJob,
|
||||||
|
|||||||
@@ -1,100 +0,0 @@
|
|||||||
import { eq, or, and, isNull, notInArray, sql } from 'drizzle-orm';
|
|
||||||
import { db } from '../db';
|
|
||||||
import { tasks } from '../schema/agent-items';
|
|
||||||
|
|
||||||
export async function getTasksForUser(userId: number) {
|
|
||||||
return db
|
|
||||||
.select({
|
|
||||||
id: tasks.id,
|
|
||||||
dirName: tasks.dirName,
|
|
||||||
name: tasks.name,
|
|
||||||
description: tasks.description,
|
|
||||||
mode: tasks.mode,
|
|
||||||
language: tasks.language,
|
|
||||||
version: tasks.version,
|
|
||||||
scope: tasks.scope,
|
|
||||||
trigger: tasks.trigger,
|
|
||||||
userId: tasks.userId,
|
|
||||||
})
|
|
||||||
.from(tasks)
|
|
||||||
.where(
|
|
||||||
or(
|
|
||||||
eq(tasks.scope, 'native'),
|
|
||||||
eq(tasks.scope, 'global'),
|
|
||||||
and(eq(tasks.scope, 'user'), eq(tasks.userId, userId)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.orderBy(tasks.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getTaskById(id: number) {
|
|
||||||
const rows = await db.select().from(tasks).where(eq(tasks.id, id)).limit(1);
|
|
||||||
return rows[0] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getTaskByDirName(dirName: string, userId: number) {
|
|
||||||
// User scope takes priority over global, which takes priority over native
|
|
||||||
const rows = await db
|
|
||||||
.select()
|
|
||||||
.from(tasks)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(tasks.dirName, dirName),
|
|
||||||
or(
|
|
||||||
eq(tasks.scope, 'native'),
|
|
||||||
eq(tasks.scope, 'global'),
|
|
||||||
and(eq(tasks.scope, 'user'), eq(tasks.userId, userId)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.orderBy(sql`CASE scope WHEN 'user' THEN 0 WHEN 'global' THEN 1 ELSE 2 END`)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
return rows[0] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
type TaskInsert = typeof tasks.$inferInsert;
|
|
||||||
|
|
||||||
export async function createTask(data: TaskInsert) {
|
|
||||||
const rows = await db.insert(tasks).values(data).returning();
|
|
||||||
return rows[0]!;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function updateTask(id: number, data: Partial<TaskInsert>) {
|
|
||||||
const rows = await db
|
|
||||||
.update(tasks)
|
|
||||||
.set({ ...data, updatedAt: new Date() })
|
|
||||||
.where(eq(tasks.id, id))
|
|
||||||
.returning();
|
|
||||||
return rows[0] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteTask(id: number) {
|
|
||||||
await db.delete(tasks).where(eq(tasks.id, id));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteNativeTasksNotIn(dirNames: string[]) {
|
|
||||||
if (dirNames.length === 0) return [];
|
|
||||||
const deleted = await db
|
|
||||||
.delete(tasks)
|
|
||||||
.where(and(eq(tasks.scope, 'native'), notInArray(tasks.dirName, dirNames)))
|
|
||||||
.returning({ dirName: tasks.dirName });
|
|
||||||
return deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function upsertNativeTask(data: Omit<TaskInsert, 'scope' | 'userId'>) {
|
|
||||||
const existing = await db
|
|
||||||
.select({ id: tasks.id })
|
|
||||||
.from(tasks)
|
|
||||||
.where(and(eq(tasks.dirName, data.dirName), eq(tasks.scope, 'native')))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (existing.length > 0) {
|
|
||||||
await db
|
|
||||||
.update(tasks)
|
|
||||||
.set({ ...data, updatedAt: new Date() })
|
|
||||||
.where(eq(tasks.id, existing[0]!.id));
|
|
||||||
} else {
|
|
||||||
await db.insert(tasks).values({ ...data, scope: 'native', userId: null });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,159 +0,0 @@
|
|||||||
import { pgTable, serial, text, integer, timestamp, jsonb, index, unique } from 'drizzle-orm/pg-core';
|
|
||||||
import { users } from './auth';
|
|
||||||
|
|
||||||
// ── Shared columns pattern ──
|
|
||||||
// Each table has: id, scope, userId, dirName, name, description, body, version, timestamps
|
|
||||||
// Type-specific columns are added per table
|
|
||||||
|
|
||||||
// ── Tasks ──
|
|
||||||
// Complex frontmatter: inputs, outputs, dependencies, triggers, config, tags, tools, skills
|
|
||||||
|
|
||||||
export const tasks = pgTable(
|
|
||||||
'tasks',
|
|
||||||
{
|
|
||||||
id: serial('id').primaryKey(),
|
|
||||||
scope: text('scope').notNull(),
|
|
||||||
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
dirName: text('dir_name').notNull(),
|
|
||||||
name: text('name').notNull(),
|
|
||||||
description: text('description'),
|
|
||||||
body: text('body'),
|
|
||||||
version: integer('version').notNull().default(1),
|
|
||||||
mode: text('mode').notNull().default('agentic'),
|
|
||||||
language: text('language'),
|
|
||||||
implementation: text('implementation'),
|
|
||||||
args: jsonb('args').$type<string[]>(),
|
|
||||||
tags: jsonb('tags').$type<string[]>(),
|
|
||||||
tools: jsonb('tools').$type<string[]>(),
|
|
||||||
skills: jsonb('skills').$type<string[]>(),
|
|
||||||
inputs: jsonb('inputs'),
|
|
||||||
outputs: jsonb('outputs'),
|
|
||||||
dependencies: jsonb('dependencies'),
|
|
||||||
config: jsonb('config'),
|
|
||||||
trigger: jsonb('trigger'),
|
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_tasks_scope_user_dir').on(table.scope, table.userId, table.dirName),
|
|
||||||
index('idx_tasks_scope').on(table.scope),
|
|
||||||
index('idx_tasks_user').on(table.userId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// ── Skills ──
|
|
||||||
// Minimal frontmatter: name, description only. Rich markdown body.
|
|
||||||
|
|
||||||
export const skills = pgTable(
|
|
||||||
'skills',
|
|
||||||
{
|
|
||||||
id: serial('id').primaryKey(),
|
|
||||||
scope: text('scope').notNull(),
|
|
||||||
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
dirName: text('dir_name').notNull(),
|
|
||||||
name: text('name').notNull(),
|
|
||||||
description: text('description'),
|
|
||||||
body: text('body'),
|
|
||||||
version: integer('version').notNull().default(1),
|
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_skills_scope_user_dir').on(table.scope, table.userId, table.dirName),
|
|
||||||
index('idx_skills_scope').on(table.scope),
|
|
||||||
index('idx_skills_user').on(table.userId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// ── Processes ──
|
|
||||||
// Same shape as skills. Represents documented workflows.
|
|
||||||
|
|
||||||
export const processes = pgTable(
|
|
||||||
'processes',
|
|
||||||
{
|
|
||||||
id: serial('id').primaryKey(),
|
|
||||||
scope: text('scope').notNull(),
|
|
||||||
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
dirName: text('dir_name').notNull(),
|
|
||||||
name: text('name').notNull(),
|
|
||||||
description: text('description'),
|
|
||||||
body: text('body'),
|
|
||||||
version: integer('version').notNull().default(1),
|
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_processes_scope_user_dir').on(table.scope, table.userId, table.dirName),
|
|
||||||
index('idx_processes_scope').on(table.scope),
|
|
||||||
index('idx_processes_user').on(table.userId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// ── Tools ──
|
|
||||||
// Has implementation code, language, structured input params, label.
|
|
||||||
|
|
||||||
export const tools = pgTable(
|
|
||||||
'tools',
|
|
||||||
{
|
|
||||||
id: serial('id').primaryKey(),
|
|
||||||
scope: text('scope').notNull(),
|
|
||||||
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
dirName: text('dir_name').notNull(),
|
|
||||||
name: text('name').notNull(),
|
|
||||||
label: text('label'),
|
|
||||||
description: text('description'),
|
|
||||||
body: text('body'),
|
|
||||||
version: integer('version').notNull().default(1),
|
|
||||||
language: text('language'),
|
|
||||||
inputs: jsonb('inputs'),
|
|
||||||
implementation: text('implementation'),
|
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_tools_scope_user_dir').on(table.scope, table.userId, table.dirName),
|
|
||||||
index('idx_tools_scope').on(table.scope),
|
|
||||||
index('idx_tools_user').on(table.userId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// ── Extensions ──
|
|
||||||
// Code-only, no markdown, no chat. Just implementation.
|
|
||||||
|
|
||||||
export const extensions = pgTable(
|
|
||||||
'extensions',
|
|
||||||
{
|
|
||||||
id: serial('id').primaryKey(),
|
|
||||||
scope: text('scope').notNull(),
|
|
||||||
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
|
|
||||||
dirName: text('dir_name').notNull(),
|
|
||||||
name: text('name').notNull(),
|
|
||||||
implementation: text('implementation'),
|
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_extensions_scope_user_dir').on(table.scope, table.userId, table.dirName),
|
|
||||||
index('idx_extensions_scope').on(table.scope),
|
|
||||||
index('idx_extensions_user').on(table.userId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// ── Item Chats ──
|
|
||||||
// Chat history for tasks, skills, processes, resources, tools.
|
|
||||||
// Uses polymorphic reference (item_type + item_id) instead of per-table FKs.
|
|
||||||
|
|
||||||
export const itemChats = pgTable(
|
|
||||||
'item_chats',
|
|
||||||
{
|
|
||||||
id: text('id').primaryKey(),
|
|
||||||
itemType: text('item_type').notNull(),
|
|
||||||
itemId: integer('item_id').notNull(),
|
|
||||||
messages: jsonb('messages').notNull().default([]),
|
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
||||||
},
|
|
||||||
(table) => [
|
|
||||||
unique('uq_item_chats_type_item').on(table.itemType, table.itemId),
|
|
||||||
index('idx_item_chats_type_item').on(table.itemType, table.itemId),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
@@ -2,7 +2,6 @@ export * from './auth';
|
|||||||
export * from './user-data';
|
export * from './user-data';
|
||||||
export * from './chat';
|
export * from './chat';
|
||||||
export * from './dashboards';
|
export * from './dashboards';
|
||||||
export * from './agent-items';
|
|
||||||
export * from './operations';
|
export * from './operations';
|
||||||
export * from './server';
|
export * from './server';
|
||||||
export * from './email';
|
export * from './email';
|
||||||
|
|||||||
@@ -50,36 +50,6 @@ export type ScreenInsert = typeof Schema.screens.$inferInsert;
|
|||||||
export type ProjectSelect = typeof Schema.projects.$inferSelect;
|
export type ProjectSelect = typeof Schema.projects.$inferSelect;
|
||||||
export type ProjectInsert = typeof Schema.projects.$inferInsert;
|
export type ProjectInsert = typeof Schema.projects.$inferInsert;
|
||||||
|
|
||||||
// ── Tasks ──
|
|
||||||
|
|
||||||
export type TaskSelect = typeof Schema.tasks.$inferSelect;
|
|
||||||
export type TaskInsert = typeof Schema.tasks.$inferInsert;
|
|
||||||
|
|
||||||
// ── Skills ──
|
|
||||||
|
|
||||||
export type SkillSelect = typeof Schema.skills.$inferSelect;
|
|
||||||
export type SkillInsert = typeof Schema.skills.$inferInsert;
|
|
||||||
|
|
||||||
// ── Processes ──
|
|
||||||
|
|
||||||
export type ProcessSelect = typeof Schema.processes.$inferSelect;
|
|
||||||
export type ProcessInsert = typeof Schema.processes.$inferInsert;
|
|
||||||
|
|
||||||
// ── Tools ──
|
|
||||||
|
|
||||||
export type ToolSelect = typeof Schema.tools.$inferSelect;
|
|
||||||
export type ToolInsert = typeof Schema.tools.$inferInsert;
|
|
||||||
|
|
||||||
// ── Extensions ──
|
|
||||||
|
|
||||||
export type ExtensionSelect = typeof Schema.extensions.$inferSelect;
|
|
||||||
export type ExtensionInsert = typeof Schema.extensions.$inferInsert;
|
|
||||||
|
|
||||||
// ── Item Chats ──
|
|
||||||
|
|
||||||
export type ItemChatSelect = typeof Schema.itemChats.$inferSelect;
|
|
||||||
export type ItemChatInsert = typeof Schema.itemChats.$inferInsert;
|
|
||||||
|
|
||||||
// ── Operations ──
|
// ── Operations ──
|
||||||
|
|
||||||
export type TaskLogSelect = typeof Schema.taskLogs.$inferSelect;
|
export type TaskLogSelect = typeof Schema.taskLogs.$inferSelect;
|
||||||
|
|||||||
@@ -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 { readdirSync, existsSync, mkdirSync } from 'node:fs';
|
||||||
import type { Subprocess } from 'bun';
|
import type { Subprocess } from 'bun';
|
||||||
import type { PiEvent, MessageCost } from './types';
|
import type { PiEvent, MessageCost } from './types';
|
||||||
import {
|
import { PI_CONFIG_DIR, DATA_PATH, getHomeDirForRole, itemsDir } from '../../data-path';
|
||||||
PI_CONFIG_DIR,
|
|
||||||
DATA_PATH,
|
|
||||||
getHomeDirForRole,
|
|
||||||
getGlobalSkillsDir,
|
|
||||||
getUserSkillsDir,
|
|
||||||
getGlobalExtensionsDir,
|
|
||||||
getUserExtensionsDir,
|
|
||||||
getGlobalToolsDir,
|
|
||||||
getUserToolsDir,
|
|
||||||
} from '../../data-path';
|
|
||||||
import { logger } from './logger';
|
import { logger } from './logger';
|
||||||
|
|
||||||
// Resolve pi as [node, cli.js] — Bun.spawn async pipes break with shebang scripts under pm2
|
// 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;
|
export type PiEventHandler = (event: PiEvent) => void;
|
||||||
|
|
||||||
function collectSkillFlags(email: string): string[] {
|
function collectSkillFlags(): string[] {
|
||||||
const flags: string[] = [];
|
const flags: string[] = [];
|
||||||
const dirs = [getGlobalSkillsDir(), getUserSkillsDir(email)];
|
const dirs = [itemsDir('skills')];
|
||||||
|
|
||||||
for (const dir of dirs) {
|
for (const dir of dirs) {
|
||||||
if (!existsSync(dir)) continue;
|
if (!existsSync(dir)) continue;
|
||||||
@@ -50,9 +40,9 @@ function collectSkillFlags(email: string): string[] {
|
|||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectExtensionFlags(email: string): string[] {
|
function collectExtensionFlags(): string[] {
|
||||||
const flags: string[] = [];
|
const flags: string[] = [];
|
||||||
const dirs = [getGlobalExtensionsDir(), getUserExtensionsDir(email)];
|
const dirs = [itemsDir('extensions')];
|
||||||
|
|
||||||
for (const dir of dirs) {
|
for (const dir of dirs) {
|
||||||
if (!existsSync(dir)) continue;
|
if (!existsSync(dir)) continue;
|
||||||
@@ -67,7 +57,6 @@ function collectExtensionFlags(email: string): string[] {
|
|||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function resolveApiKeyForModel(model: string): Promise<string | null> {
|
async function resolveApiKeyForModel(model: string): Promise<string | null> {
|
||||||
const provider = model.split('/')[0];
|
const provider = model.split('/')[0];
|
||||||
if (!provider) return null;
|
if (!provider) return null;
|
||||||
@@ -94,8 +83,8 @@ export async function spawnPi(
|
|||||||
onEvent: PiEventHandler,
|
onEvent: PiEventHandler,
|
||||||
options?: SpawnPiOptions,
|
options?: SpawnPiOptions,
|
||||||
): Promise<Subprocess> {
|
): Promise<Subprocess> {
|
||||||
const skillFlags = collectSkillFlags(email);
|
const skillFlags = collectSkillFlags();
|
||||||
const extensionFlags = collectExtensionFlags(email);
|
const extensionFlags = collectExtensionFlags();
|
||||||
|
|
||||||
const piArgs = [
|
const piArgs = [
|
||||||
...PI_CMD,
|
...PI_CMD,
|
||||||
@@ -118,7 +107,7 @@ export async function spawnPi(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const homeDir = getHomeDirForRole(email, options?.role ?? null);
|
const homeDir = getHomeDirForRole(email, options?.role ?? null);
|
||||||
const toolsDirs = [getGlobalToolsDir(), getUserToolsDir(email)].join(':');
|
const toolsDirs = itemsDir('tools');
|
||||||
|
|
||||||
const env: Record<string, string> = {
|
const env: Record<string, string> = {
|
||||||
HOME: process.env.HOME ?? '',
|
HOME: process.env.HOME ?? '',
|
||||||
|
|||||||
@@ -1,211 +1,3 @@
|
|||||||
import { createRouter } from '../../create-router';
|
import { createItemRouter } from '../item-router';
|
||||||
import { readdir, mkdir, rm } from 'node:fs/promises';
|
|
||||||
import { join, dirname } from 'node:path';
|
|
||||||
import { getNativeProcessesDir, getGlobalProcessesDir, getUserProcessesDir } from '../../data-path';
|
|
||||||
|
|
||||||
type Frontmatter = {
|
export const processesRouter = createItemRouter({ type: 'processes', fileName: 'PROCESS.md', label: 'Process' });
|
||||||
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 });
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -1,214 +1,3 @@
|
|||||||
import { createRouter } from '../../create-router';
|
import { createItemRouter } from '../item-router';
|
||||||
import { readdir, mkdir, rm } from 'node:fs/promises';
|
|
||||||
import { join, dirname } from 'node:path';
|
|
||||||
import { getNativeSkillsDir, getGlobalSkillsDir, getUserSkillsDir } from '../../data-path';
|
|
||||||
|
|
||||||
type Frontmatter = {
|
export const skillsRouter = createItemRouter({ type: 'skills', fileName: 'SKILL.md', label: 'Skill' });
|
||||||
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 });
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import { randomUUID } from 'crypto';
|
|||||||
import { readdirSync, existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
|
import { readdirSync, existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { tmpdir } from 'node:os';
|
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 { getHomeDirForRole, getHomeDir } from '../../data-path';
|
||||||
import { resolveBaseCwd } from '../pi/websocket';
|
import { resolveBaseCwd } from '../pi/websocket';
|
||||||
import { SANDBOX_HOME } from '../../sidecar/sandbox';
|
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> {
|
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) {
|
if (!pipelineTask) {
|
||||||
emit({ type: 'error', message: `Task not found: ${taskDirName}` });
|
emit({ type: 'error', message: `Task not found: ${taskDirName}` });
|
||||||
return;
|
return;
|
||||||
@@ -514,7 +515,7 @@ export async function executePipeline({ userId, email, username, role, taskDirNa
|
|||||||
|
|
||||||
const step = config.steps[stepIdx]!;
|
const step = config.steps[stepIdx]!;
|
||||||
|
|
||||||
const stepTask = await getTaskByDirName(step.task, userId);
|
const stepTask = await getTaskByDirName(step.task);
|
||||||
if (!stepTask) {
|
if (!stepTask) {
|
||||||
emit({ type: 'error', message: `Step task not found: ${step.task}` });
|
emit({ type: 'error', message: `Step task not found: ${step.task}` });
|
||||||
return;
|
return;
|
||||||
@@ -648,7 +649,7 @@ export async function message(ws: ServerWebSocket<WSData>, raw: string | Buffer)
|
|||||||
const { userId, email, username, role } = ws.data;
|
const { userId, email, username, role } = ws.data;
|
||||||
|
|
||||||
// Resolve task name for the DB record
|
// Resolve task name for the DB record
|
||||||
const task = await getTaskByDirName(msg.taskDirName, userId);
|
const task = await getTaskByDirName(msg.taskDirName);
|
||||||
if (!task) {
|
if (!task) {
|
||||||
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
|
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { ServerWebSocket } from 'bun';
|
import type { ServerWebSocket } from 'bun';
|
||||||
import { join, isAbsolute } from 'node:path';
|
import { join, isAbsolute } from 'node:path';
|
||||||
import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
|
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 { getHomeDirForRole, DATA_PATH } from '../../data-path';
|
||||||
import { buildSandboxPrefix, buildRunuserSuffix } from '../../sidecar/sandbox';
|
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) {
|
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
|
// Resolve task from the file-backed store
|
||||||
const task = await getTaskByDirName(msg.taskDirName, userId);
|
const task = await getTaskByDirName(msg.taskDirName);
|
||||||
if (!task) {
|
if (!task) {
|
||||||
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
|
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
|
||||||
return;
|
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 { 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' };
|
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
|
||||||
|
|
||||||
function isPrivileged(role: string) {
|
|
||||||
return role === 'Super Admin';
|
|
||||||
}
|
|
||||||
|
|
||||||
export const tasksRouter = createRouter();
|
export const tasksRouter = createRouter();
|
||||||
|
|
||||||
tasksRouter.get('/', async (ctx) => {
|
tasksRouter.get('/', async (ctx) => {
|
||||||
const user = ctx.get('user');
|
const summaries = await listTasks();
|
||||||
const rows = await getTasksForUser(user.id);
|
|
||||||
|
|
||||||
const tasks = rows.map((row) => ({
|
const tasks = summaries.map((row) => ({
|
||||||
id: row.id,
|
|
||||||
dirName: row.dirName,
|
dirName: row.dirName,
|
||||||
name: row.name,
|
name: row.name,
|
||||||
description: row.description,
|
description: row.description,
|
||||||
scope: row.scope,
|
|
||||||
triggers: (row.trigger as TriggerConfig[]) ?? [],
|
triggers: (row.trigger as TriggerConfig[]) ?? [],
|
||||||
mode: row.mode ?? 'agentic',
|
mode: row.mode ?? 'agentic',
|
||||||
userId: row.userId,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return ctx.json(tasks);
|
return ctx.json(tasks);
|
||||||
});
|
});
|
||||||
|
|
||||||
tasksRouter.get('/:name', async (ctx) => {
|
tasksRouter.get('/:name', async (ctx) => {
|
||||||
const user = ctx.get('user');
|
|
||||||
const name = ctx.req.param('name');
|
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);
|
if (!task) return ctx.text('Not found', 404);
|
||||||
|
|
||||||
return ctx.json({
|
return ctx.json({
|
||||||
id: task.id,
|
|
||||||
dirName: task.dirName,
|
dirName: task.dirName,
|
||||||
name: task.name,
|
name: task.name,
|
||||||
description: task.description,
|
description: task.description,
|
||||||
scope: task.scope,
|
|
||||||
mode: task.mode,
|
mode: task.mode,
|
||||||
language: task.language,
|
language: task.language,
|
||||||
body: task.body,
|
body: task.body,
|
||||||
@@ -49,45 +38,35 @@ tasksRouter.get('/:name', async (ctx) => {
|
|||||||
trigger: task.trigger,
|
trigger: task.trigger,
|
||||||
config: task.config,
|
config: task.config,
|
||||||
version: task.version,
|
version: task.version,
|
||||||
userId: task.userId,
|
filePath: task.filePath,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tasksRouter.post('/', async (ctx) => {
|
tasksRouter.post('/', async (ctx) => {
|
||||||
const user = ctx.get('user');
|
|
||||||
const body = await ctx.req.json<{ name: string; description?: string; mode?: string; language?: string }>();
|
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);
|
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, '');
|
try {
|
||||||
if (!dirName) return ctx.text('Invalid name', 400);
|
const task = await createTask({
|
||||||
|
name: body.name,
|
||||||
const scope = isPrivileged(user.role) ? 'global' : 'user';
|
description: body.description ?? null,
|
||||||
|
mode: body.mode ?? 'agentic',
|
||||||
const task = await createTask({
|
language: body.language ?? null,
|
||||||
scope,
|
});
|
||||||
userId: user.id,
|
return ctx.json(task);
|
||||||
dirName,
|
} catch (err) {
|
||||||
name: body.name.trim(),
|
const message = err instanceof Error ? err.message : 'Failed to create task';
|
||||||
description: body.description ?? null,
|
return ctx.text(message, message === 'Task already exists' ? 409 : 400);
|
||||||
mode: body.mode ?? 'agentic',
|
}
|
||||||
language: body.language ?? null,
|
|
||||||
});
|
|
||||||
|
|
||||||
return ctx.json(task);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tasksRouter.delete('/:name', async (ctx) => {
|
tasksRouter.delete('/:name', async (ctx) => {
|
||||||
const user = ctx.get('user');
|
|
||||||
const name = ctx.req.param('name');
|
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);
|
if (!task) return ctx.text('Not found', 404);
|
||||||
|
|
||||||
// Only owner or Super Admin can delete
|
await deleteTask(task.dirName);
|
||||||
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);
|
|
||||||
return ctx.json({ ok: true });
|
return ctx.json({ ok: true });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,217 +1,3 @@
|
|||||||
import { createRouter } from '../../create-router';
|
import { createItemRouter } from '../item-router';
|
||||||
import { readdir, mkdir, rm } from 'node:fs/promises';
|
|
||||||
import { join, dirname } from 'node:path';
|
|
||||||
import { getNativeToolsDir, getGlobalToolsDir, getUserToolsDir } from '../../data-path';
|
|
||||||
|
|
||||||
type Frontmatter = {
|
export const toolsRouter = createItemRouter({ type: 'tools', fileName: 'TOOL.md', label: 'Tool' });
|
||||||
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 });
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { mkdirSync } from 'node:fs';
|
import { mkdirSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
import { DATA_PATH } from './data-path';
|
import { DATA_PATH, ensureItemDirs } from './data-path';
|
||||||
import { syncMarketplaceTools, syncMarketplaceTasks } from './sync-marketplace';
|
|
||||||
import { ensureToolLoader } from './ensure-tool-loader';
|
import { ensureToolLoader } from './ensure-tool-loader';
|
||||||
// Queue is now owned by the sidecar process
|
// Queue is now owned by the sidecar process
|
||||||
import { startDiscordBotIfConfigured } from './channels/discord/bot';
|
import { startDiscordBotIfConfigured } from './channels/discord/bot';
|
||||||
@@ -10,6 +9,7 @@ import { startTelegramBotIfConfigured } from './channels/telegram/bot';
|
|||||||
import { startWhatsAppBotIfConfigured } from './channels/whatsapp/bot';
|
import { startWhatsAppBotIfConfigured } from './channels/whatsapp/bot';
|
||||||
|
|
||||||
mkdirSync(DATA_PATH, { recursive: true });
|
mkdirSync(DATA_PATH, { recursive: true });
|
||||||
|
ensureItemDirs();
|
||||||
|
|
||||||
/** Check common locations for the Pi package directory. */
|
/** Check common locations for the Pi package directory. */
|
||||||
async function findPiPackageDir(): Promise<string | null> {
|
async function findPiPackageDir(): Promise<string | null> {
|
||||||
@@ -67,8 +67,6 @@ async function installPi(): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await syncMarketplaceTools();
|
|
||||||
await syncMarketplaceTasks();
|
|
||||||
ensureToolLoader();
|
ensureToolLoader();
|
||||||
// Queue is initialized by the sidecar process
|
// Queue is initialized by the sidecar process
|
||||||
|
|
||||||
|
|||||||
+15
-31
@@ -1,8 +1,23 @@
|
|||||||
import { join, resolve } from 'node:path';
|
import { join, resolve } from 'node:path';
|
||||||
|
import { mkdirSync } from 'node:fs';
|
||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
|
|
||||||
export const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
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 SERVER_CONFIG_DIR = join(DATA_PATH, 'server-settings');
|
||||||
|
|
||||||
export const PI_CONFIG_DIR = join(homedir(), '.pi', 'agent');
|
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 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 getTmpAttachmentsDir = (email: string) => join(DATA_PATH, email, 'attachments', 'tmp');
|
||||||
|
|
||||||
export const getAttachmentsDir = (email: string, sessionId: string) => join(DATA_PATH, email, 'attachments', sessionId);
|
export const getAttachmentsDir = (email: string, sessionId: string) => join(DATA_PATH, email, 'attachments', sessionId);
|
||||||
|
|
||||||
export const getUserEmailDir = (email: string) => join(DATA_PATH, email, 'Gmail', 'emails');
|
export const getUserEmailDir = (email: string) => join(DATA_PATH, email, 'Gmail', 'emails');
|
||||||
|
|
||||||
|
|
||||||
/** Derive a valid Linux username from a display username or email. */
|
/** Derive a valid Linux username from a display username or email. */
|
||||||
export const toShellUsername = (username: string, email: string): string => {
|
export const toShellUsername = (username: string, email: string): string => {
|
||||||
const raw = username || email.split('@')[0]!;
|
const raw = username || email.split('@')[0]!;
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import {
|
import { itemsDir, getHomeDir, DATA_PATH } from '@@/data-path';
|
||||||
getGlobalToolsDir,
|
|
||||||
getUserToolsDir,
|
|
||||||
getGlobalSkillsDir,
|
|
||||||
getUserSkillsDir,
|
|
||||||
getGlobalTasksDir,
|
|
||||||
getUserTasksDir,
|
|
||||||
getHomeDir,
|
|
||||||
DATA_PATH,
|
|
||||||
} from '@@/data-path';
|
|
||||||
|
|
||||||
type HookEntry = { type: string; command: string };
|
type HookEntry = { type: string; command: string };
|
||||||
type HookRule = { matcher?: Record<string, unknown>; hooks: HookEntry[] };
|
type HookRule = { matcher?: Record<string, unknown>; hooks: HookEntry[] };
|
||||||
@@ -57,14 +48,13 @@ function formatList(entries: FrontmatterEntry[]): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function generateContainerContext(email: string): string {
|
export function generateContainerContext(email: string): string {
|
||||||
const tools = dedup([...scanDir(getGlobalToolsDir(), 'TOOL.md'), ...scanDir(getUserToolsDir(email), 'TOOL.md')]);
|
const toolsDir = itemsDir('tools');
|
||||||
const skills = dedup([...scanDir(getGlobalSkillsDir(), 'SKILL.md'), ...scanDir(getUserSkillsDir(email), 'SKILL.md')]);
|
const skillsDir = itemsDir('skills');
|
||||||
const tasks = dedup([...scanDir(getGlobalTasksDir(), 'TASK.md'), ...scanDir(getUserTasksDir(email), 'TASK.md')]);
|
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 userDataDir = join(DATA_PATH, email);
|
||||||
|
|
||||||
const content = `# Officer — User Environment
|
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) |
|
| \`~\` | User home directory (read-write) |
|
||||||
| \`~/Projects/\` | User projects |
|
| \`~/Projects/\` | User projects |
|
||||||
| \`~/Downloads/\` | Downloaded files |
|
| \`~/Downloads/\` | Downloaded files |
|
||||||
| \`${globalToolsDir}/\` | Global tools |
|
| \`${toolsDir}/\` | Tools |
|
||||||
| \`${userToolsDir}/\` | User tools |
|
| \`${skillsDir}/\` | Reference skills |
|
||||||
| \`${globalSkillsDir}/\` | Reference skills |
|
|
||||||
| \`${userDataDir}/\` | User data (emails.db, attachments, etc.) |
|
| \`${userDataDir}/\` | User data (emails.db, attachments, etc.) |
|
||||||
|
|
||||||
## Available Tools
|
## 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)}
|
${formatList(tools)}
|
||||||
## Available Skills
|
## Available Skills
|
||||||
@@ -100,7 +89,7 @@ Tasks are predefined instruction sets the AI agent can execute.
|
|||||||
${formatList(tasks)}
|
${formatList(tasks)}
|
||||||
## Creating New Tools
|
## 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:
|
**TOOL.md** — Frontmatter metadata + markdown documentation:
|
||||||
\`\`\`yaml
|
\`\`\`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 getHomeDir = (email: string) => join(DATA_PATH, email, 'home');
|
||||||
const getHomeDirForRole = (email: string, role: string | null): string =>
|
const getHomeDirForRole = (email: string, role: string | null): string =>
|
||||||
role === 'Super Admin' && process.env.HOME_DIR ? process.env.HOME_DIR : getHomeDir(email);
|
role === 'Super Admin' && process.env.HOME_DIR ? process.env.HOME_DIR : getHomeDir(email);
|
||||||
const getGlobalSkillsDir = () => join(DATA_PATH, 'skills');
|
const OFFICER_ITEMS_DIR = process.env.OFFICER_ITEMS_DIR ?? join(process.cwd(), 'officer-items');
|
||||||
const getUserSkillsDir = (email: string) => join(DATA_PATH, email, 'skills');
|
const itemsDir = (type: 'skills' | 'tools' | 'extensions') => join(OFFICER_ITEMS_DIR, type);
|
||||||
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');
|
|
||||||
|
|
||||||
function isPidAlive(pid: number): boolean {
|
function isPidAlive(pid: number): boolean {
|
||||||
try {
|
try {
|
||||||
@@ -97,11 +93,8 @@ function collectSkillFlagsFromDir(scanDir: string, targetDir: string): string[]
|
|||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectSkillFlags(email: string): string[] {
|
function collectSkillFlags(): string[] {
|
||||||
return [
|
return collectSkillFlagsFromDir(itemsDir('skills'), itemsDir('skills'));
|
||||||
...collectSkillFlagsFromDir(getGlobalSkillsDir(), getGlobalSkillsDir()),
|
|
||||||
...collectSkillFlagsFromDir(getUserSkillsDir(email), getUserSkillsDir(email)),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectExtensionFlagsFromDir(scanDir: string, targetDir: string): string[] {
|
function collectExtensionFlagsFromDir(scanDir: string, targetDir: string): string[] {
|
||||||
@@ -118,11 +111,8 @@ function collectExtensionFlagsFromDir(scanDir: string, targetDir: string): strin
|
|||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectExtensionFlags(email: string): string[] {
|
function collectExtensionFlags(): string[] {
|
||||||
return [
|
return collectExtensionFlagsFromDir(itemsDir('extensions'), itemsDir('extensions'));
|
||||||
...collectExtensionFlagsFromDir(getGlobalExtensionsDir(), getGlobalExtensionsDir()),
|
|
||||||
...collectExtensionFlagsFromDir(getUserExtensionsDir(email), getUserExtensionsDir(email)),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveApiKeyForModel(model: string): Promise<string | null> {
|
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 isSuperAdmin = role === 'Super Admin';
|
||||||
const skillFlags = isSuperAdmin
|
const skillFlags = isSuperAdmin
|
||||||
? collectSkillFlags(email)
|
? collectSkillFlags()
|
||||||
: [
|
: collectSkillFlagsFromDir(itemsDir('skills'), SANDBOX_GLOBAL_SKILLS);
|
||||||
...collectSkillFlagsFromDir(getGlobalSkillsDir(), SANDBOX_GLOBAL_SKILLS),
|
|
||||||
...collectSkillFlagsFromDir(getUserSkillsDir(email), `${SANDBOX_DATA}/skills`),
|
|
||||||
];
|
|
||||||
const extensionFlags = isSuperAdmin
|
const extensionFlags = isSuperAdmin
|
||||||
? collectExtensionFlags(email)
|
? collectExtensionFlags()
|
||||||
: [
|
: collectExtensionFlagsFromDir(itemsDir('extensions'), SANDBOX_GLOBAL_EXTENSIONS);
|
||||||
...collectExtensionFlagsFromDir(getGlobalExtensionsDir(), SANDBOX_GLOBAL_EXTENSIONS),
|
|
||||||
...collectExtensionFlagsFromDir(getUserExtensionsDir(email), `${SANDBOX_DATA}/extensions`),
|
|
||||||
];
|
|
||||||
|
|
||||||
const piArgs = [
|
const piArgs = [
|
||||||
...PI_CMD,
|
...PI_CMD,
|
||||||
@@ -311,7 +295,7 @@ export async function spawnPi(options: PiSpawnOptions): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const homeDir = getHomeDirForRole(email, role);
|
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
|
// 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.
|
// 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 });
|
proc = Bun.spawn(piArgs, { cwd, stdin: 'pipe', stdout: 'pipe', stderr: 'pipe', env });
|
||||||
} else {
|
} else {
|
||||||
// Non-admin: run inside bwrap sandbox
|
// Non-admin: run inside bwrap sandbox
|
||||||
const sandboxToolsDirs = [SANDBOX_GLOBAL_TOOLS, `${SANDBOX_DATA}/tools`].join(':');
|
const sandboxToolsDirs = SANDBOX_GLOBAL_TOOLS;
|
||||||
const prefix = buildSandboxPrefix(email);
|
const prefix = buildSandboxPrefix(email);
|
||||||
|
|
||||||
// Pi-specific env vars
|
// Pi-specific env vars
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const BUN_DIR = (() => {
|
|||||||
|
|
||||||
const PROJECT_ROOT = resolve(import.meta.dir, '../../..');
|
const PROJECT_ROOT = resolve(import.meta.dir, '../../..');
|
||||||
const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
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!;
|
const HOST_HOME = process.env.HOME!;
|
||||||
|
|
||||||
// Resolve the OS username for runuser to drop privileges inside the sandbox
|
// 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()`.
|
// Callers can append extra `--setenv` args before calling `buildRunuserSuffix()`.
|
||||||
export function buildSandboxPrefix(email: string): string[] {
|
export function buildSandboxPrefix(email: string): string[] {
|
||||||
const userDataDir = join(DATA_PATH, email);
|
const userDataDir = join(DATA_PATH, email);
|
||||||
const globalSkillsDir = join(DATA_PATH, 'skills');
|
const globalSkillsDir = join(OFFICER_ITEMS_DIR, 'skills');
|
||||||
const globalToolsDir = join(DATA_PATH, 'tools');
|
const globalToolsDir = join(OFFICER_ITEMS_DIR, 'tools');
|
||||||
const globalExtensionsDir = join(DATA_PATH, 'extensions');
|
const globalExtensionsDir = join(OFFICER_ITEMS_DIR, 'extensions');
|
||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
'sudo',
|
'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