Files
AgentCompany/src/main/main-runtime.ts
T

102 lines
3.3 KiB
TypeScript

import { app } from "electron";
import { AgentApiServer, createApiServer } from "./api-server";
import { createAgentToken, initializeAgentTokenSigningKey } from "./agent-auth";
import { bootstrapApplication } from "./bootstrap-application";
import { AppDatabase } from "./database";
import { createMainRuntimeSupport } from "./main-runtime-support";
import type { ThreadIntegrationService } from "./thread-integration";
import type { createCheckpointEngine } from "./checkpoint-engine";
import type { DocumentLifecycleProcessor } from "./document-lifecycle-processor";
interface CreateMainRuntimeDependencies {
hasSingleInstanceLock: boolean;
isDev: boolean;
devServerOrigin: string | null;
profileDir: string;
windowStatePath: string;
preloadPath: string;
rendererDist: string;
iconPath: string;
workerPath: string;
devServerUrl?: string;
}
export function createMainRuntime({
hasSingleInstanceLock,
isDev,
devServerOrigin,
profileDir,
windowStatePath,
preloadPath,
rendererDist,
iconPath,
workerPath,
devServerUrl,
}: CreateMainRuntimeDependencies) {
const state: {
db: AppDatabase | null;
apiServer: AgentApiServer | null;
serviceGraph: Awaited<ReturnType<typeof bootstrapApplication>>["serviceGraph"] | null;
ipcSupport: Awaited<ReturnType<typeof bootstrapApplication>>["ipcSupport"] | null;
threadIntegration: ThreadIntegrationService | null;
checkpointEngine: ReturnType<typeof createCheckpointEngine> | null;
lifecycleProcessor: DocumentLifecycleProcessor | null;
isQuitting: boolean;
} = {
db: null,
apiServer: null,
serviceGraph: null,
ipcSupport: null,
threadIntegration: null,
checkpointEngine: null,
lifecycleProcessor: null,
isQuitting: false,
};
const support = createMainRuntimeSupport(state);
async function bootstrap() {
await bootstrapApplication({
hasSingleInstanceLock,
isDev,
devServerOrigin,
profileDir,
appDatabaseFactory: (targetProfileDir) => new AppDatabase(targetProfileDir, { isPackaged: app.isPackaged, appVersion: app.getVersion() }),
initSigningKey: initializeAgentTokenSigningKey,
windowStatePath,
preloadPath,
rendererDist,
iconPath,
devServerUrl,
workerPath,
createApiServer: (db, callbacks, allowedOrigins) =>
createApiServer(db, callbacks, allowedOrigins),
createRunToken: createAgentToken,
seedDefaultAutomationRules: support.seedDefaultAutomationRules,
isQuittingRef: {
get current() {
return state.isQuitting;
},
set current(value: boolean) {
state.isQuitting = value;
},
},
onRuntimeReady: ({ db, apiServer, serviceGraph, ipcSupport, threadIntegration, checkpointEngine, lifecycleProcessor }) => {
state.db = db;
state.apiServer = apiServer;
state.serviceGraph = serviceGraph;
state.ipcSupport = ipcSupport;
state.threadIntegration = threadIntegration;
state.checkpointEngine = checkpointEngine;
state.lifecycleProcessor = lifecycleProcessor;
// Register IPC handlers immediately so the renderer can call loadSnapshot()
// as soon as the window appears — before connector health checks finish.
support.registerHandlers();
},
});
}
return {
bootstrap,
};
}