move tasks to marketplace, sync native tasks on bootstrap

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 13:57:40 +00:00
co-authored by Claude Opus 4.6
parent ef6402a827
commit ee1b816d66
13 changed files with 92 additions and 819 deletions
-250
View File
@@ -1,250 +0,0 @@
/**
* Seed native tasks into the database.
* Run: bun src/databases/officer_db/seed-tasks.ts
*/
import { eq, and } from 'drizzle-orm';
import { db } from './src/db';
import { tasks } from './src/schema/agent-items';
import { readdirSync, readFileSync, existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
const SEED_TASKS_DIR = resolve(import.meta.dir, '../../../seed/tasks');
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
function parseFrontmatter(content: string) {
const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { meta: {} as Record<string, unknown>, body: content };
const yaml = match[1]!;
const body = match[2]!;
const meta: Record<string, unknown> = {};
meta.name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
meta.description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
meta.mode = yaml.match(/^mode:\s*(.+)$/m)?.[1]?.trim() ?? 'agentic';
meta.language = yaml.match(/^language:\s*(.+)$/m)?.[1]?.trim() ?? undefined;
meta.version = parseInt(yaml.match(/^version:\s*(.+)$/m)?.[1]?.trim() ?? '1', 10);
// Parse args
const argsMatch = yaml.match(/^args:\s*\[([^\]]*)\]/m);
meta.args = argsMatch ? argsMatch[1]!.split(',').map((a) => a.trim()).filter(Boolean) : undefined;
// Parse triggers
const triggers: TriggerConfig[] = [];
const triggerMatch = yaml.match(/^triggers?:\s*\n((?:[ \t]+.+\n?)*)/m);
if (triggerMatch) {
const items = triggerMatch[1]!.split(/(?=^\s+-\s*type:)/m);
for (const item of items) {
const type = item.match(/type:\s*(.+)/)?.[1]?.trim();
if (type === 'directory') {
triggers.push({ type: 'directory' });
} else if (type === 'file') {
const extBlock = item.match(/extensions:\s*\n((?:\s+-\s*.+\n?)*)/);
const extensions = extBlock ? [...extBlock[1]!.matchAll(/^\s+-\s*(.+)$/gm)].map((m) => m[1]!.trim()) : [];
if (extensions.length > 0) triggers.push({ type: 'file', extensions });
}
}
}
meta.trigger = triggers.length > 0 ? triggers : undefined;
// Parse inputs
const inputsMatch = yaml.match(/^inputs:\s*\n((?:[ \t]+.+\n?)*)/m);
if (inputsMatch) {
const inputBlock = inputsMatch[1]!;
const inputEntries: Record<string, Record<string, string | string[]>> = {};
let currentInput: string | null = null;
let currentListKey: string | null = null;
let currentList: string[] = [];
const flushList = () => {
if (currentInput && currentListKey && currentList.length > 0) {
inputEntries[currentInput]![currentListKey] = currentList;
}
currentListKey = null;
currentList = [];
};
for (const line of inputBlock.split('\n')) {
const topMatch = line.match(/^\s{2}(\w[\w_-]*):\s*$/);
if (topMatch) {
flushList();
currentInput = topMatch[1]!;
inputEntries[currentInput] = {};
continue;
}
// List item (6 spaces + dash)
const listItemMatch = line.match(/^\s{6}-\s*(.+)$/);
if (listItemMatch && currentInput && currentListKey) {
currentList.push(listItemMatch[1]!.trim());
continue;
}
// Property with value or start of list
const propMatch = line.match(/^\s{4}(\w[\w_-]*):\s*(.*)$/);
if (propMatch && currentInput) {
flushList();
const value = propMatch[2]!.trim();
if (value === '') {
// Start of a list (e.g. "options:")
currentListKey = propMatch[1]!;
currentList = [];
} else {
inputEntries[currentInput]![propMatch[1]!] = value;
}
}
}
flushList();
if (Object.keys(inputEntries).length > 0) {
meta.inputs = inputEntries;
}
}
// Parse pipeline steps
const stepsMatch = yaml.match(/^steps:\s*\n((?:[ \t]+.+\n?)*)/m);
if (stepsMatch) {
const stepsBlock = stepsMatch[1]!;
const steps: Record<string, unknown>[] = [];
let currentStep: Record<string, unknown> | null = null;
let inInputs = false;
let stepInputs: Record<string, string> = {};
for (const line of stepsBlock.split('\n')) {
// New step entry (2 spaces + dash)
const stepStart = line.match(/^\s{2}-\s+task:\s*(.+)$/);
if (stepStart) {
if (currentStep) {
if (Object.keys(stepInputs).length > 0) currentStep.inputs = stepInputs;
steps.push(currentStep);
}
currentStep = { task: stepStart[1]!.trim() };
stepInputs = {};
inInputs = false;
continue;
}
if (!currentStep) continue;
// Step-level properties (4 spaces)
const propMatch = line.match(/^\s{4}(\w[\w_-]*):\s*(.*)$/);
if (propMatch) {
const key = propMatch[1]!;
const value = propMatch[2]!.trim();
if (key === 'inputs' && value === '') {
inInputs = true;
} else {
inInputs = false;
currentStep[key] = value;
}
continue;
}
// Step input entries (6 spaces)
const inputMatch = line.match(/^\s{6}(\w[\w_-]*):\s*(.+)$/);
if (inputMatch && inInputs) {
const raw = inputMatch[2]!.trim();
stepInputs[inputMatch[1]!] = raw.replace(/^["'](.*)["']$/, '$1');
}
}
if (currentStep) {
if (Object.keys(stepInputs).length > 0) currentStep.inputs = stepInputs;
steps.push(currentStep);
}
if (steps.length > 0) {
meta.config = { steps };
}
}
return { meta, body };
}
async function seedTasks() {
if (!existsSync(SEED_TASKS_DIR)) {
console.log('No seed tasks directory found');
return;
}
const entries = readdirSync(SEED_TASKS_DIR, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const taskDir = join(SEED_TASKS_DIR, entry.name);
const taskMdPath = join(taskDir, 'TASK.md');
if (!existsSync(taskMdPath)) continue;
const content = readFileSync(taskMdPath, 'utf-8');
const { meta, body } = parseFrontmatter(content);
// Find implementation file
let implementation: string | null = null;
const lang = meta.language as string | undefined;
const implCandidates = [
{ file: 'run.sh', lang: 'bash' },
{ file: 'index.ts', lang: 'typescript' },
{ file: 'run.py', lang: 'python' },
{ file: 'index.js', lang: 'javascript' },
];
for (const c of implCandidates) {
const path = join(taskDir, c.file);
if (existsSync(path)) {
implementation = readFileSync(path, 'utf-8');
break;
}
}
const values = {
scope: 'native' as const,
userId: null,
dirName: entry.name,
name: (meta.name as string) || entry.name,
description: (meta.description as string) || null,
body: body || null,
version: (meta.version as number) || 1,
mode: (meta.mode as string) || 'agentic',
language: (meta.language as string) || null,
implementation,
args: (meta.args as string[]) || null,
trigger: (meta.trigger as TriggerConfig[]) || null,
inputs: (meta.inputs as Record<string, unknown>) || null,
config: (meta.config as Record<string, unknown>) || null,
};
// Upsert: check by dirName + scope since unique constraint doesn't work with NULL userId
const existing = await db
.select({ id: tasks.id })
.from(tasks)
.where(and(eq(tasks.dirName, values.dirName), eq(tasks.scope, 'native')))
.limit(1);
if (existing.length > 0) {
await db
.update(tasks)
.set({
name: values.name,
description: values.description,
body: values.body,
version: values.version,
mode: values.mode,
language: values.language,
implementation: values.implementation,
args: values.args,
trigger: values.trigger,
inputs: values.inputs,
config: values.config,
updatedAt: new Date(),
})
.where(eq(tasks.id, existing[0]!.id));
} else {
await db.insert(tasks).values(values);
}
console.log(`Seeded: ${entry.name} (${meta.mode}/${meta.language})`);
}
console.log('Done seeding tasks');
process.exit(0);
}
seedTasks().catch((err) => {
console.error('Seed failed:', err);
process.exit(1);
});
+1
View File
@@ -79,6 +79,7 @@ export {
createTask,
updateTask,
deleteTask,
upsertNativeTask,
} from './queries/tasks';
export {
@@ -72,3 +72,20 @@ export async function updateTask(id: number, data: Partial<TaskInsert>) {
export async function deleteTask(id: number) {
await db.delete(tasks).where(eq(tasks.id, id));
}
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 });
}
}