- Role: (start, messages) → (ctx: ThreadContext) - AgentFn prompt callbacks: (start) → (ctx) - ModeratorContext → ThreadContext - 13 files updated across knowledge-extraction and solve-issue workflows 小橘 <xiaoju@shazhou.work>
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
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";
|
|
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 CreateImplementRoleDeps = {
|
|
extract: LlmExtractorConfig;
|
|
nerveRoot: string;
|
|
};
|
|
|
|
export function createImplementRole(
|
|
adapter: AgentFn,
|
|
{ extract, nerveRoot }: CreateImplementRoleDeps,
|
|
): Role<ImplementMeta> {
|
|
return async (ctx: ThreadContext): Promise<RoleResult<ImplementMeta>> => {
|
|
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 },
|
|
};
|
|
}
|
|
};
|
|
}
|