* feat(runtime): start and ship the local agent * refactor: gate local agent behind feature flag * chore: skip agent manager if flag is false * fix: hot reload agents * test: fix config tests
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import type { ChildProcess } from "node:child_process";
|
|
import type { AgentManagerRuntime } from "./controller/server";
|
|
import { spawnLocalAgentProcess, stopLocalAgentProcess } from "./local/process";
|
|
|
|
export type { AgentBackupEventHandlers } from "./controller/server";
|
|
|
|
type AgentRuntimeState = {
|
|
agentManager: AgentManagerRuntime | null;
|
|
localAgent: ChildProcess | null;
|
|
};
|
|
|
|
type ProcessWithAgentRuntime = NodeJS.Process & {
|
|
__zerobyteAgentRuntime?: AgentRuntimeState;
|
|
};
|
|
|
|
const getAgentRuntimeState = () => {
|
|
const runtimeProcess = process as ProcessWithAgentRuntime;
|
|
const existingRuntime = runtimeProcess.__zerobyteAgentRuntime;
|
|
|
|
if (existingRuntime) {
|
|
return existingRuntime;
|
|
}
|
|
|
|
const runtime = {
|
|
agentManager: null,
|
|
localAgent: null,
|
|
};
|
|
|
|
runtimeProcess.__zerobyteAgentRuntime = runtime;
|
|
return runtime;
|
|
};
|
|
|
|
const getAgentManagerRuntime = () => getAgentRuntimeState().agentManager;
|
|
|
|
export const startAgentRuntime = async () => {
|
|
const runtime = getAgentRuntimeState();
|
|
|
|
if (runtime.agentManager) {
|
|
await runtime.agentManager.stop();
|
|
}
|
|
|
|
const { createAgentManagerRuntime } = await import("./controller/server");
|
|
const agentManager = createAgentManagerRuntime();
|
|
|
|
await agentManager.start();
|
|
runtime.agentManager = agentManager;
|
|
};
|
|
|
|
export const spawnLocalAgent = async () => {
|
|
await spawnLocalAgentProcess(getAgentRuntimeState());
|
|
};
|
|
|
|
export const stopLocalAgent = async () => {
|
|
await stopLocalAgentProcess(getAgentRuntimeState());
|
|
};
|
|
|
|
export const stopAgentRuntime = async () => {
|
|
await getAgentManagerRuntime()?.stop();
|
|
await stopLocalAgent();
|
|
};
|