65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import type { Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core";
|
|
import { cursorAgent, isDryRun, llmExtract } from "@uncaged/nerve-workflow-utils";
|
|
import type { LlmProvider } from "@uncaged/nerve-workflow-utils";
|
|
import { z } from "zod";
|
|
import { resolveRepoCwd } from "../../lib/repo-context.js";
|
|
import { threadIdFromStart } from "../../lib/start-meta.js";
|
|
import { formatSpawnFailure } from "../../lib/spawn-utils.js";
|
|
import { buildImplementPrompt } from "./prompt.js";
|
|
|
|
export const implementMetaSchema = z.object({
|
|
done: z.boolean().describe("true when changes are complete and build passes this round"),
|
|
});
|
|
export type ImplementMeta = z.infer<typeof implementMetaSchema>;
|
|
|
|
export type BuildImplementDeps = {
|
|
provider: LlmProvider;
|
|
nerveRoot: string;
|
|
};
|
|
|
|
export function buildImplementRole({ provider, nerveRoot }: BuildImplementDeps): Role<ImplementMeta> {
|
|
return async (start: StartStep, messages: WorkflowMessage[]): Promise<RoleResult<ImplementMeta>> => {
|
|
const cwd = resolveRepoCwd(messages);
|
|
if (cwd === null) {
|
|
return {
|
|
content: "implement cannot run: missing repo path in thread markers",
|
|
meta: { done: false },
|
|
};
|
|
}
|
|
|
|
const dry = isDryRun(start);
|
|
const prompt = buildImplementPrompt({ threadId: threadIdFromStart(start), nerveRoot });
|
|
const run = await cursorAgent({
|
|
prompt,
|
|
mode: "default",
|
|
model: "auto",
|
|
cwd,
|
|
env: null,
|
|
timeoutMs: 300_000,
|
|
dryRun: dry,
|
|
});
|
|
|
|
if (!run.ok) {
|
|
return {
|
|
content: `implement cursor-agent failed: ${formatSpawnFailure(run.error)}`,
|
|
meta: { done: false },
|
|
};
|
|
}
|
|
|
|
const metaR = await llmExtract({
|
|
text: run.value,
|
|
schema: implementMetaSchema,
|
|
provider,
|
|
dryRun: dry,
|
|
});
|
|
if (!metaR.ok) {
|
|
return {
|
|
content: `${run.value}\n\n[meta extract failed]`,
|
|
meta: { done: false },
|
|
};
|
|
}
|
|
|
|
return { content: run.value, meta: metaR.value };
|
|
};
|
|
}
|