refactor: extract wrapAgentAsAdapter to util-agent, support childThread in RoleFn (#222)
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
"test": "bun test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/workflow-runtime": "workspace:*"
|
||||
"@uncaged/workflow-runtime": "workspace:*",
|
||||
"@uncaged/workflow-cas": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export { buildAgentPrompt, buildThreadInput } from "./build-agent-prompt.js";
|
||||
export type { SpawnCliConfig, SpawnCliError, SpawnCliResult } from "./spawn-cli.js";
|
||||
export { spawnCli } from "./spawn-cli.js";
|
||||
export { wrapAgentAsAdapter } from "./wrap-agent-as-adapter.js";
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { putContentNodeWithRefs } from "@uncaged/workflow-cas";
|
||||
import type {
|
||||
AdapterFn,
|
||||
AgentContext,
|
||||
AgentFnResult,
|
||||
RoleResult,
|
||||
ThreadContext,
|
||||
WorkflowRuntime,
|
||||
} from "@uncaged/workflow-runtime";
|
||||
import type * as z from "zod/v4";
|
||||
|
||||
/**
|
||||
* Wraps a legacy AgentFn into an AdapterFn.
|
||||
* The agent produces a string (or { output, childThread }); the adapter
|
||||
* stores the output in CAS, runs extract, and returns typed meta + childThread.
|
||||
*/
|
||||
export function wrapAgentAsAdapter(
|
||||
agentFn: (ctx: AgentContext) => Promise<AgentFnResult>,
|
||||
): AdapterFn {
|
||||
return <T>(prompt: string, schema: z.ZodType<T>) => {
|
||||
return async (ctx: ThreadContext, runtime: WorkflowRuntime): Promise<RoleResult<T>> => {
|
||||
const agentCtx: AgentContext = { ...ctx, currentRole: { name: "agent", systemPrompt: prompt } };
|
||||
const result = await agentFn(agentCtx);
|
||||
const output = typeof result === "string" ? result : result.output;
|
||||
const childThread = typeof result === "string" ? null : result.childThread;
|
||||
const contentHash = await putContentNodeWithRefs(runtime.cas, output, []);
|
||||
const extracted = await runtime.extract(schema as z.ZodType<Record<string, unknown>>, contentHash);
|
||||
return { meta: extracted.meta as T, childThread };
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -6,5 +6,5 @@
|
||||
"composite": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"references": [{ "path": "../workflow-runtime" }]
|
||||
"references": [{ "path": "../workflow-runtime" }, { "path": "../workflow-cas" }]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user