- Add OpenClawProcessOwner class to manage the lifecycle of the OpenClaw process. - Introduce utility functions for managing OpenClaw runtime paths. - Update session store to normalize agent session keys and migrate existing keys. - Refactor main process to handle local provider API routing through a new dispatch function. - Enhance token usage writer to utilize a new session key parsing function. - Create agents management store to handle agent data and interactions. - Update chat store to integrate agent selection and session management. - Introduce AgentsSection component for displaying agent information in the UI. - Refactor HomePage to support agent selection and display current agent. - Update routing to reflect new agents page structure.
108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
import { BrowserWindow } from 'electron';
|
|
import { windowManager } from '@electron/service/window-service';
|
|
import logManager from '@electron/service/logger';
|
|
import type { GatewayEvent } from './types';
|
|
import * as chatHandlers from './handlers/chat';
|
|
import * as providerHandlers from './handlers/provider';
|
|
|
|
class GatewayManager {
|
|
private initialized = false;
|
|
private status: 'connected' | 'disconnected' | 'reconnecting' = 'disconnected';
|
|
|
|
private setStatus(status: 'connected' | 'disconnected' | 'reconnecting'): void {
|
|
this.status = status;
|
|
this.broadcast({ type: 'gateway:status', status });
|
|
}
|
|
|
|
async init(): Promise<void> {
|
|
if (this.initialized) return;
|
|
this.initialized = true;
|
|
this.status = 'connected';
|
|
logManager.info('GatewayManager initialized');
|
|
this.broadcast({ type: 'gateway:status', status: 'connected' });
|
|
}
|
|
|
|
async start(): Promise<void> {
|
|
await this.init();
|
|
}
|
|
|
|
async stop(): Promise<void> {
|
|
this.initialized = false;
|
|
this.setStatus('disconnected');
|
|
}
|
|
|
|
async restart(): Promise<void> {
|
|
this.initialized = false;
|
|
this.setStatus('reconnecting');
|
|
await this.init();
|
|
}
|
|
|
|
getStatus(): {
|
|
status: 'connected' | 'disconnected' | 'reconnecting';
|
|
initialized: boolean;
|
|
mode: 'in-process';
|
|
} {
|
|
return {
|
|
status: this.status,
|
|
initialized: this.initialized,
|
|
mode: 'in-process',
|
|
};
|
|
}
|
|
|
|
async checkHealth(): Promise<{
|
|
ok: boolean;
|
|
status: 'connected' | 'disconnected' | 'reconnecting';
|
|
initialized: boolean;
|
|
mode: 'in-process';
|
|
}> {
|
|
return {
|
|
ok: this.initialized && this.status === 'connected',
|
|
...this.getStatus(),
|
|
};
|
|
}
|
|
|
|
async rpc(method: string, params: any): Promise<any> {
|
|
if (!this.initialized) {
|
|
await this.init();
|
|
}
|
|
|
|
logManager.info(`Gateway RPC: ${method}`, params);
|
|
|
|
switch (method) {
|
|
case 'chat.send':
|
|
return chatHandlers.handleChatSend(params, (event) => this.broadcast(event));
|
|
case 'chat.history':
|
|
return chatHandlers.handleChatHistory(params);
|
|
case 'chat.abort':
|
|
return chatHandlers.handleChatAbort(params, (event) => this.broadcast(event));
|
|
case 'session.list':
|
|
return chatHandlers.handleSessionList();
|
|
case 'session.delete':
|
|
return chatHandlers.handleSessionDelete(params);
|
|
case 'provider.list':
|
|
return providerHandlers.handleProviderList();
|
|
case 'provider.getDefault':
|
|
return providerHandlers.handleProviderGetDefault();
|
|
default:
|
|
throw new Error(`Unknown gateway RPC method: ${method}`);
|
|
}
|
|
}
|
|
|
|
broadcast(event: GatewayEvent): void {
|
|
const mainWindow = BrowserWindow.getAllWindows().find(
|
|
(win) => windowManager.getName(win) === 'main'
|
|
);
|
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
|
mainWindow.webContents.send('gateway:event', event);
|
|
}
|
|
}
|
|
|
|
reloadProviders(): void {
|
|
logManager.info('GatewayManager reloading providers');
|
|
// For now, providers are resolved on each chat.send call,
|
|
// so no in-memory cache to invalidate. Future: notify active sessions.
|
|
}
|
|
}
|
|
|
|
export const gatewayManager = new GatewayManager();
|