import type { Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core"; import type { LlmProvider } from "@uncaged/nerve-workflow-utils"; import { createCursorRole } 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; }; 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 runRole = createCursorRole({ cwd, mode: "ask", model: "auto", env: {}, timeoutMs: 300_000, prompt: async () => buildPlanPrompt({ threadId: (start.meta as { threadId?: string }).threadId ?? "unknown", nerveRoot, }), extract: { provider, schema: planMetaSchema }, }); try { return await runRole(start, messages); } catch (e) { const msg = e instanceof Error ? e.message : String(e); return { content: `plan failed: ${msg}`, meta: { ready: false }, }; } }; }