import type { Role, RoleResult, Schema, StartStep, WorkflowMessage } from "@uncaged/nerve-core"; import { END } from "@uncaged/nerve-core"; import { compileWorkflowSpec } from "@uncaged/nerve-daemon"; import { createCursorAdapter } from "@uncaged/nerve-adapter-cursor"; import type { LlmProvider } from "@uncaged/nerve-workflow-utils"; import { createLlmExtractFn, zodMeta } from "@uncaged/nerve-workflow-utils"; import { z } from "zod"; import { resolveRepoCwd } from "../../lib/repo-context.js"; import { buildPlanPrompt } from "./prompt.js"; export const planMetaSchema = z.object({ ready: z.boolean().describe("true if plan is clear and actionable"), }); export type PlanMeta = z.infer; export type BuildPlanDeps = { provider: LlmProvider; nerveRoot: string; }; const CURSOR_TIMEOUT_MS = 300_000; export function buildPlanRole({ provider, nerveRoot }: BuildPlanDeps): Role { return async (start: StartStep, messages: WorkflowMessage[]): Promise> => { const cwd = resolveRepoCwd(messages); if (cwd === null) { return { content: "plan cannot run: missing ---SOLVE_ISSUE_REPO--- or ---SOLVE_ISSUE_PARSE--- in thread", meta: { ready: false }, }; } const innerSpec = { main: { adapter: createCursorAdapter({ type: "cursor", model: "auto", timeout: CURSOR_TIMEOUT_MS, mode: "ask", }), prompt: async (start: StartStep) => buildPlanPrompt({ threadId: start.meta.threadId, nerveRoot, }), meta: zodMeta(planMetaSchema), }, }; const compiled = compileWorkflowSpec( { name: "_plan-inner", roles: innerSpec, moderator: () => END, }, { extractFn: async (raw: string, schema: Schema, dryRun: boolean) => createLlmExtractFn({ provider, dryRun })(raw, schema), createContext: (s, m) => ({ start: s, messages: m, workdir: cwd, signal: new AbortController().signal, }), }, ); try { return await compiled.roles.main(start, messages); } catch (e) { const msg = e instanceof Error ? e.message : String(e); return { content: `plan failed: ${msg}`, meta: { ready: false }, }; } }; }