Files
zn-ai/electron/api/route-utils.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

64 lines
1.3 KiB
TypeScript

import type { HostApiResult } from '@src/types/runtime';
export interface HostApiRequest {
path: string;
method?: string;
headers?: Record<string, string>;
body?: unknown;
}
export interface NormalizedHostApiRequest {
path: string;
pathname: string;
method: string;
headers: Record<string, string>;
body: unknown;
url: URL;
}
export function normalizeRequest(request: HostApiRequest): NormalizedHostApiRequest {
const path = String(request.path || '/').trim() || '/';
return {
path,
pathname: new URL(path, 'http://127.0.0.1').pathname,
method: String(request.method || 'GET').trim().toUpperCase(),
headers: request.headers || {},
body: request.body ?? null,
url: new URL(path, 'http://127.0.0.1'),
};
}
export function parseJsonBody<T>(body: unknown): T {
if (body == null || body === '') {
return {} as T;
}
if (typeof body === 'string') {
return JSON.parse(body) as T;
}
return body as T;
}
export function ok<T>(data: T, status = 200): HostApiResult<T> {
return {
success: true,
ok: true,
status,
json: data,
data,
};
}
export function fail<T = unknown>(status: number, error: string, data?: T): HostApiResult<T> {
return {
success: false,
ok: false,
status,
error,
text: error,
data,
};
}