import { BrowserWindow } from 'electron'; import { gatewayManager } from '@electron/gateway/manager'; import { providerApiService } from '@electron/service/provider-api-service'; import { ClawHubService } from '@electron/gateway/clawhub'; import type { HostApiContext } from './context'; import type { HostApiRequest } from './route-utils'; import { normalizeRequest } from './route-utils'; import { handleAgentRoutes } from './routes/agents'; import { handleChannelRoutes } from './routes/channels'; import { handleCronRoutes } from './routes/cron'; import { handleFileRoutes } from './routes/files'; import { handleGatewayRoutes } from './routes/gateway'; import { handleKnowledgeRoutes } from './routes/knowledge'; import { handleLogRoutes } from './routes/logs'; import { handleProviderRoutes } from './routes/providers'; import { handleSettingsRoutes } from './routes/settings'; import { handleSessionRoutes } from './routes/sessions'; import { handleSkillRoutes } from './routes/skills'; type RouteHandler = ( request: ReturnType, ctx: HostApiContext, ) => Promise; const routeHandlers: RouteHandler[] = [ handleProviderRoutes, handleChannelRoutes, handleAgentRoutes, handleCronRoutes, handleGatewayRoutes, handleKnowledgeRoutes, handleLogRoutes, handleFileRoutes, handleSessionRoutes, handleSettingsRoutes, handleSkillRoutes, ]; function createContext(): HostApiContext { return { gatewayManager, providerApiService, mainWindow: BrowserWindow.getAllWindows()[0] ?? null, clawHubService: new ClawHubService(), }; } export async function dispatchLocalHostApi(request: HostApiRequest) { const normalized = normalizeRequest(request); const ctx = createContext(); for (const handler of routeHandlers) { const result = await handler(normalized, ctx); if (result) { return result; } } return null; }