Files
zn-ai/electron/api/routes/providers.ts
duanshuwen e9f3a29886 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.
2026-04-17 21:32:06 +08:00

75 lines
3.3 KiB
TypeScript

import type { HostApiContext } from '../context';
import type { NormalizedHostApiRequest } from '../route-utils';
import { ok } from '../route-utils';
export async function handleProviderRoutes(
request: NormalizedHostApiRequest,
ctx: HostApiContext,
) {
const { pathname, method } = request;
const parsedBody = request.body && typeof request.body === 'string'
? JSON.parse(request.body)
: request.body;
if (pathname === '/api/provider-vendors' && method === 'GET') {
return ok(ctx.providerApiService.getVendors());
}
if (pathname === '/api/provider-accounts' && method === 'GET') {
return ok(ctx.providerApiService.getAccounts());
}
if (pathname === '/api/providers' && method === 'GET') {
return ok(ctx.providerApiService.getProviders());
}
if (pathname === '/api/provider-accounts/default' && method === 'GET') {
return ok(ctx.providerApiService.getDefault());
}
if (pathname === '/api/provider-accounts' && method === 'POST') {
return ok(ctx.providerApiService.createAccount(parsedBody || {}));
}
if (pathname === '/api/provider-accounts/default' && method === 'PUT') {
const result = ctx.providerApiService.setDefault(parsedBody || {});
return ok(result, result.success ? 200 : 400);
}
if (pathname.startsWith('/api/provider-accounts/') && method === 'PUT') {
const id = decodeURIComponent(pathname.replace('/api/provider-accounts/', ''));
const result = ctx.providerApiService.updateAccount(id, parsedBody || {});
return ok(result, result.success ? 200 : 404);
}
if (pathname.startsWith('/api/provider-accounts/') && method === 'DELETE') {
const id = decodeURIComponent(pathname.replace('/api/provider-accounts/', ''));
return ok(ctx.providerApiService.deleteAccount(id));
}
if (pathname === '/api/providers/default' && method === 'PUT') {
const result = ctx.providerApiService.setDefault({ accountId: (parsedBody as any)?.providerId });
return ok(result, result.success ? 200 : 400);
}
if (pathname.startsWith('/api/providers/') && pathname.endsWith('/api-key') && method === 'GET') {
const id = decodeURIComponent(pathname.replace('/api/providers/', '').replace('/api-key', ''));
return ok(ctx.providerApiService.getApiKey(id));
}
if (pathname.startsWith('/api/providers/') && method === 'PUT') {
const id = decodeURIComponent(pathname.replace('/api/providers/', ''));
const result = ctx.providerApiService.updateAccount(id, parsedBody || {});
return ok(result, result.success ? 200 : 404);
}
if (pathname.startsWith('/api/providers/') && method === 'DELETE') {
const rawPath = request.path.replace('/api/providers/', '');
const [rawId, query] = rawPath.split('?');
const id = decodeURIComponent(rawId);
if (query && query.includes('apiKeyOnly=1')) {
return ok(ctx.providerApiService.deleteApiKey(id));
}
return ok(ctx.providerApiService.deleteAccount(id));
}
if (pathname === '/api/providers/validate' && method === 'POST') {
return ok(await ctx.providerApiService.validateApiKey(parsedBody || {}));
}
if (pathname === '/api/usage/recent-token-history' && method === 'GET') {
const limitRaw = request.url.searchParams.get('limit');
const limit = limitRaw ? Number(limitRaw) : undefined;
return ok(await ctx.providerApiService.getUsageHistory(limit));
}
return null;
}