- 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.7 KiB
TypeScript
62 lines
1.7 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 { 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 CreatePlanRoleDeps = {
|
|
extract: LlmExtractorConfig;
|
|
nerveRoot: string;
|
|
};
|
|
|
|
export function createPlanRole(
|
|
adapter: AgentFn,
|
|
{ extract, nerveRoot }: CreatePlanRoleDeps,
|
|
): Role<PlanMeta> {
|
|
return async (ctx: ThreadContext): Promise<RoleResult<PlanMeta>> => {
|
|
const messages = ctx.steps as unknown as WorkflowMessage[];
|
|
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 innerRole = createRole(
|
|
adapter,
|
|
async (innerCtx: ThreadContext) =>
|
|
buildPlanPrompt({
|
|
threadId: innerCtx.start.meta.threadId,
|
|
nerveRoot,
|
|
}),
|
|
planMetaSchema,
|
|
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: `plan failed: ${msg}`,
|
|
meta: { ready: false },
|
|
};
|
|
}
|
|
};
|
|
}
|