Files
zn-ai/electron/gateway/openclaw-process-owner.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

113 lines
2.5 KiB
TypeScript

import {
ensureOpenClawRuntimeLayout,
getOpenClawRuntimePaths,
type OpenClawRuntimePaths,
} from '@electron/utils/paths';
export type OpenClawProcessOwnerState =
| 'idle'
| 'preparing'
| 'running'
| 'stopping'
| 'stopped'
| 'failed';
export interface OpenClawProcessOwnerStatus {
state: OpenClawProcessOwnerState;
prepared: boolean;
runtimePaths: OpenClawRuntimePaths;
lastError?: string;
}
export interface OpenClawProcessOwnerOptions {
runtimePaths?: Partial<OpenClawRuntimePaths>;
}
export interface OpenClawProcessOwnerLike {
prepare(): Promise<void>;
start(): Promise<void>;
stop(): Promise<void>;
restart(): Promise<void>;
getStatus(): OpenClawProcessOwnerStatus;
getRuntimePaths(): OpenClawRuntimePaths;
}
function mergeRuntimePaths(
base: OpenClawRuntimePaths,
override?: Partial<OpenClawRuntimePaths>,
): OpenClawRuntimePaths {
if (!override) {
return base;
}
return {
configDir: override.configDir ?? base.configDir,
runtimeDir: override.runtimeDir ?? base.runtimeDir,
dir: override.dir ?? base.dir,
resolvedDir: override.resolvedDir ?? base.resolvedDir,
entryPath: override.entryPath ?? base.entryPath,
};
}
export class OpenClawProcessOwner implements OpenClawProcessOwnerLike {
private status: OpenClawProcessOwnerStatus;
constructor(options?: OpenClawProcessOwnerOptions) {
const runtimePaths = mergeRuntimePaths(
getOpenClawRuntimePaths(),
options?.runtimePaths,
);
this.status = {
state: 'idle',
prepared: false,
runtimePaths,
};
}
async prepare(): Promise<void> {
if (this.status.prepared) {
return;
}
this.status.state = 'preparing';
ensureOpenClawRuntimeLayout(this.status.runtimePaths);
this.status.prepared = true;
this.status.state = 'idle';
}
async start(): Promise<void> {
if (this.status.state === 'running') {
return;
}
await this.prepare();
this.status.state = 'running';
}
async stop(): Promise<void> {
if (this.status.state === 'idle' || this.status.state === 'stopped') {
return;
}
this.status.state = 'stopping';
this.status.state = 'stopped';
}
async restart(): Promise<void> {
await this.stop();
await this.start();
}
getStatus(): OpenClawProcessOwnerStatus {
return {
...this.status,
runtimePaths: { ...this.status.runtimePaths },
};
}
getRuntimePaths(): OpenClawRuntimePaths {
return { ...this.status.runtimePaths };
}
}