feat: implement OpenClaw process owner and runtime path utilities

- 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.
This commit is contained in:
duanshuwen
2026-04-17 21:32:06 +08:00
parent eca70425cf
commit e9f3a29886
33 changed files with 1526 additions and 2428 deletions

View File

@@ -10,8 +10,9 @@ import log from 'electron-log';
import 'bytenode'; // Ensure bytenode is bundled/externalized correctly
import { appUpdater } from '@electron/service/updater';
import axios from 'axios';
import { providerApiService, onProviderChange } from '@electron/service/provider-api-service';
import { onProviderChange } from '@electron/service/provider-api-service';
import { gatewayManager } from '@electron/gateway/manager';
import { dispatchLocalHostApi } from '@electron/api/router';
// 初始化 updater确保在 app ready 之前或者之中注册好 IPC
appUpdater.init();
@@ -20,78 +21,14 @@ appUpdater.init();
// 模型管理相关接口在本地处理(对齐 ClawX其余接口代理到远端后端
const HOST_API_BASE_URL = process.env.VITE_SERVICE_URL || 'http://8.138.234.141/ingress';
async function handleLocalProviderApi(path: string, method: string, body: any) {
const parsedBody = typeof body === 'string' && body ? JSON.parse(body) : body;
if (path === '/api/provider-vendors' && method === 'GET') {
return { success: true, ok: true, json: providerApiService.getVendors(), data: providerApiService.getVendors() };
}
if (path === '/api/provider-accounts' && method === 'GET') {
return { success: true, ok: true, json: providerApiService.getAccounts(), data: providerApiService.getAccounts() };
}
if (path === '/api/providers' && method === 'GET') {
return { success: true, ok: true, json: providerApiService.getProviders(), data: providerApiService.getProviders() };
}
if (path === '/api/provider-accounts/default' && method === 'GET') {
return { success: true, ok: true, json: providerApiService.getDefault(), data: providerApiService.getDefault() };
}
if (path === '/api/provider-accounts' && method === 'POST') {
const result = providerApiService.createAccount(parsedBody || {});
return { success: true, ok: true, json: result, data: result };
}
if (path === '/api/provider-accounts/default' && method === 'PUT') {
const result = providerApiService.setDefault(parsedBody || {});
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path.startsWith('/api/provider-accounts/') && method === 'PUT') {
const id = decodeURIComponent(path.replace('/api/provider-accounts/', ''));
const result = providerApiService.updateAccount(id, parsedBody || {});
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path.startsWith('/api/provider-accounts/') && method === 'DELETE') {
const id = decodeURIComponent(path.replace('/api/provider-accounts/', ''));
const result = providerApiService.deleteAccount(id);
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path === '/api/providers/default' && method === 'PUT') {
const result = providerApiService.setDefault({ accountId: parsedBody?.providerId });
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path.startsWith('/api/providers/') && path.endsWith('/api-key') && method === 'GET') {
const id = decodeURIComponent(path.replace('/api/providers/', '').replace('/api-key', ''));
const result = providerApiService.getApiKey(id);
return { success: true, ok: true, json: result, data: result };
}
if (path.startsWith('/api/providers/') && method === 'PUT') {
// Provider updates are mapped to account updates for local storage
const id = decodeURIComponent(path.replace('/api/providers/', ''));
const result = providerApiService.updateAccount(id, parsedBody || {});
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path.startsWith('/api/providers/') && method === 'DELETE') {
const [rawId, query] = path.replace('/api/providers/', '').split('?');
const id = decodeURIComponent(rawId);
if (query && query.includes('apiKeyOnly=1')) {
const result = providerApiService.deleteApiKey(id);
return { success: result.success, ok: result.success, json: result, data: result };
}
const result = providerApiService.deleteAccount(id);
return { success: result.success, ok: result.success, json: result, data: result };
}
if (path === '/api/providers/validate' && method === 'POST') {
const result = await providerApiService.validateApiKey(parsedBody || {});
return { success: true, ok: true, json: result, data: result };
}
if (path === '/api/usage/recent-token-history' && method === 'GET') {
const usageHistory = await providerApiService.getUsageHistory();
return { success: true, ok: true, json: usageHistory, data: usageHistory };
}
return null;
}
ipcMain.handle('hostapi:fetch', async (_event, { path, method, headers, body }) => {
// 1. 优先本地处理模型管理接口
const localResult = await handleLocalProviderApi(path, method || 'GET', body);
// 1. 优先本地处理 Host API 路由(逐步对齐 ClawX
const localResult = await dispatchLocalHostApi({
path,
method: method || 'GET',
headers,
body,
});
if (localResult) return localResult;
// 2. 其余接口代理到远端后端