refactor(worker): use pi-mono Agent directly

Rewrites the utility-process worker to drive pi-agent-core's Agent class
against pi-ai models and pi-coding-agent tools. The entry file keeps the
queue and cancellation state, delegates runs to WorkerRunner, and routes
RPC responses through WorkerRpcClient so beforeToolCall/afterToolCall
approvals and prepareRun credential resolution travel back to main's
RuntimeBridge. Worker message schema gains `agent-event` + `rpc-request`
envelopes, and run-finished no longer depends on the deleted sessionCodec.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-17 02:21:12 -04:00
1 parent 892476bd36
commit 4de9cd0339
8 files changed
+404 -257

No files matched your search

+36 -2
View File
@@ -1,3 +1,4 @@
import type { RuntimeBridge } from "../agent-runtime";
import type { workerMessageSchema } from "./worker-types";
import { handleRunLog } from "./handlers/run-log-handler";
import { handleRunStatus } from "./handlers/run-status-handler";
@@ -6,8 +7,8 @@ import type { HandlerContext, HandlerDeps } from "./handlers/handler-context";
export async function handleWorkerMessage(
message: unknown,
_worker: ReturnType<typeof import("electron").utilityProcess.fork> | null,
deps: HandlerDeps,
worker: ReturnType<typeof import("electron").utilityProcess.fork> | null,
deps: HandlerDeps & { getRuntimeBridge?: () => RuntimeBridge | null },
workerMessageSchemaRef: typeof workerMessageSchema,
destroyed: boolean,
MAX_RETRY_ATTEMPTS: number,
@@ -44,5 +45,38 @@ export async function handleWorkerMessage(
case "run-finished":
handleRunFinished(event, deps, context);
return;
case "agent-event": {
const bridge = deps.getRuntimeBridge?.();
if (bridge) {
bridge.handleAgentEvent({
runId: event.runId,
agentId: event.agentId,
companyId: event.companyId,
taskId: event.taskId,
emittedAt: event.emittedAt,
event: event.event,
});
}
return;
}
case "rpc-request": {
const bridge = deps.getRuntimeBridge?.();
const response = bridge
? await bridge.handleRpc({
rpcId: event.rpcId,
runId: event.runId,
method: event.method,
payload: event.payload,
})
: { ok: false as const, error: "RuntimeBridge not initialised." };
try {
worker?.postMessage({ type: "rpc-response", rpcId: event.rpcId, response });
} catch (error) {
deps.logger.error(
`[worker] rpc-response postMessage failed: ${error instanceof Error ? error.message : String(error)}`,
);
}
return;
}
}
}