import type { AgentFn, Role, RoleResult, ThreadContext, WorkflowMessage } from "@uncaged/nerve-core"; import type { LlmExtractorConfig } from "@uncaged/nerve-workflow-utils"; import { createRole } from "@uncaged/nerve-workflow-utils"; import { z } from "zod"; import { resolveRepoCwd } from "../lib/repo-context.js"; function buildImplementPrompt({ threadId, nerveRoot }: { threadId: string; nerveRoot: string }): string { return `You are the **implement** agent. You apply code changes for the issue. Read workflow context (plan, reviewer/test feedback): \`nerve thread show ${threadId}\` Read Nerve workspace conventions: \`cat ${nerveRoot}/CONVENTIONS.md\` Your cwd is the target repository. ## Requirements 1. Implement the planned changes; address reviewer/tester feedback from the thread if any. 2. Run the project **build** (\`pnpm build\`, \`npm run build\`, etc.) and fix issues until build passes. 3. Multi-step: if you cannot finish this round, explain why and set **done** to false. Do **not** run \`git checkout -b\`, \`git add\`, \`git commit\`, or \`git push\`. **Never** create commits on any branch — branching and commits are handled by the **committer** step after you finish. Then close with JSON: \`\`\`json { "done": true } \`\`\` or \`{ "done": false }\` matching whether implementation is complete. **done=true** only when changes are complete **and** build passes in this round.`; } export const implementMetaSchema = z.object({ done: z.boolean().describe("true when changes are complete and build passes this round"), }); export type ImplementMeta = z.infer; export type CreateImplementRoleDeps = { extract: LlmExtractorConfig; nerveRoot: string; }; export function createImplementRole( adapter: AgentFn, { extract, nerveRoot }: CreateImplementRoleDeps, ): Role { return async (ctx: ThreadContext): Promise> => { const messages = ctx.steps as unknown as WorkflowMessage[]; const cwd = resolveRepoCwd(messages); if (cwd === null) { return { content: "implement cannot run: missing repo path in thread markers", meta: { done: false }, }; } const innerRole = createRole( adapter, async (innerCtx: ThreadContext) => buildImplementPrompt({ threadId: innerCtx.start.meta.threadId, nerveRoot, }), implementMetaSchema, extract, ); const innerCtx: ThreadContext = { ...ctx, start: { ...ctx.start, meta: { ...ctx.start.meta, workdir: cwd }, }, }; try { return await innerRole(innerCtx); } catch (e) { const msg = e instanceof Error ? e.message : String(e); return { content: `implement failed: ${msg}`, meta: { done: false }, }; } }; }