50 lines
1.6 KiB
TypeScript

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 { threadIdFromStart } from "../../lib/start-meta.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<typeof planMetaSchema>;
export type BuildPlanDeps = {
provider: LlmProvider;
nerveRoot: string;
};
export function buildPlanRole({ provider, nerveRoot }: BuildPlanDeps): Role<PlanMeta> {
return async (start: StartStep, messages: WorkflowMessage[]): Promise<RoleResult<PlanMeta>> => {
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<PlanMeta>({
cwd,
mode: "ask",
model: "auto",
env: {},
timeoutMs: 300_000,
prompt: async () => buildPlanPrompt({ threadId: threadIdFromStart(start), 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 },
};
}
};
}