W2: async job infra + researcher sub-agent
- src/store/jobs.ts: per-job JSON store in data/jobs (create/get/update/list, dedup by toolCallId) - src/agent/tools/jobs.ts: research tool — client-side (no execute), turn ends at the call; input per contracts ResearchToolInput - src/agent/tools/web.ts: server-side web_search (Google CSE) + web_fetch (Readability/Turndown/linkedom, Jina fallback) — errors return as strings, never thrown into the agent loop. Deps: @mozilla/readability, linkedom, turndown (+@types/turndown dev) - src/agent/subagents/researcher.ts: isolated ToolLoopAgent (web tools only, 12-step cap), researcher prompt ported from pi - src/agent/subagents/runner.ts: spawnPendingJobs hook + runResearchJob — fire-and-forget, activity line per tool call, fulfills the part in the LATEST chat record (output-available) before flipping job status; failure still fulfills the part so the teacher learns the outcome - src/server/chat-handler.ts: onFinish spawn hook after saveMessages - app/api/jobs/route.ts: GET ?chatId= polling endpoint - components/artifacts/job-card.tsx + registry entry: polling activity card (2s) → syncFromServer on finish; research-brief card (markdown + sources) - src/agent/prompts/20-jobs.md: research doctrine for the teacher - tests/jobs.test.ts: store CRUD, spawn hook via scripted mock teacher, runner completion/failure with fake model + stubbed fetch
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import type { JobKind, JobRecord } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Job store — one JSON file per background sub-agent job in data/jobs/
|
||||
* (contracts.ts "THE ASYNC PROTOCOL"). Keyed by toolCallId for dedup: the
|
||||
* chat-handler spawn hook never creates two jobs for the same tool part.
|
||||
*
|
||||
* Follows src/store/chats.ts idioms: sync fs, id-guarded filenames, JSON
|
||||
* with tab indentation. Writes are small and rare (activity line updates);
|
||||
* the read-modify-write sections in the runner contain no awaits, so they
|
||||
* are atomic within the single-threaded event loop.
|
||||
*/
|
||||
|
||||
/** Background jobs (data/jobs). Not in config.ts PATHS (orchestrator-owned). */
|
||||
const JOBS_DIR = path.join(PATHS.data, "jobs");
|
||||
|
||||
function jobFile(id: string): string {
|
||||
if (!/^[A-Za-z0-9_-]+$/.test(id)) throw new Error(`Invalid job id: ${id}`);
|
||||
return path.join(JOBS_DIR, `${id}.json`);
|
||||
}
|
||||
|
||||
export interface CreateJobInit {
|
||||
kind: JobKind;
|
||||
chatId: string;
|
||||
toolCallId: string;
|
||||
/** First activity line (defaults to "starting"). */
|
||||
activity?: string;
|
||||
}
|
||||
|
||||
export function createJob(init: CreateJobInit): JobRecord {
|
||||
const now = new Date().toISOString();
|
||||
const record: JobRecord = {
|
||||
id: `job_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
||||
kind: init.kind,
|
||||
chatId: init.chatId,
|
||||
toolCallId: init.toolCallId,
|
||||
status: "running",
|
||||
activity: init.activity ?? "starting",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
||||
fs.writeFileSync(jobFile(record.id), JSON.stringify(record, null, "\t"), "utf-8");
|
||||
return record;
|
||||
}
|
||||
|
||||
export function getJob(id: string): JobRecord | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(jobFile(id), "utf-8")) as JobRecord;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Merge a partial patch into the record (identity fields are immutable). */
|
||||
export function updateJob(
|
||||
id: string,
|
||||
patch: Partial<Omit<JobRecord, "id" | "kind" | "chatId" | "toolCallId" | "createdAt">>,
|
||||
): JobRecord | null {
|
||||
const record = getJob(id);
|
||||
if (!record) return null;
|
||||
const next: JobRecord = { ...record, ...patch, updatedAt: new Date().toISOString() };
|
||||
fs.writeFileSync(jobFile(id), JSON.stringify(next, null, "\t"), "utf-8");
|
||||
return next;
|
||||
}
|
||||
|
||||
/** All jobs for a chat, newest activity first. */
|
||||
export function listJobs(chatId: string): JobRecord[] {
|
||||
try {
|
||||
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
||||
return fs
|
||||
.readdirSync(JOBS_DIR)
|
||||
.filter((f) => f.endsWith(".json"))
|
||||
.map((f) => getJob(f.slice(0, -5)))
|
||||
.filter((j): j is JobRecord => j !== null && j.chatId === chatId)
|
||||
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
||||
export function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
||||
return listJobs(chatId).find((j) => j.toolCallId === toolCallId) ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user