revert: 回滚之前的错误修改,恢复为命令行模式

This commit is contained in:
2026-03-13 20:42:02 +08:00
parent fd2255c83a
commit 53c1045406
4 changed files with 10 additions and 52 deletions

View File

@@ -1,9 +0,0 @@
export const appConfig = {
opencode: {
mode: 'command', // 'command' | 'exe'
exePath: 'bin/XCOpenCodeWeb.exe',
port: 4096,
healthCheckInterval: 10000,
maxRestartAttempts: 3,
},
}

View File

@@ -1,9 +1,9 @@
import { spawn, ChildProcess } from 'child_process';
import { app } from 'electron';
import path from 'path';
import log from 'electron-log';
import { appConfig } from '../config';
const OPENCODE_PORT = 4096;
const HEALTH_CHECK_INTERVAL = 10000;
const MAX_RESTART_ATTEMPTS = 3;
const HEALTH_CHECK_TIMEOUT = 2000;
class OpenCodeService {
@@ -13,15 +13,7 @@ class OpenCodeService {
private _isRunning = false;
get port(): number {
return appConfig.opencode.port;
}
get healthCheckInterval(): number {
return appConfig.opencode.healthCheckInterval;
}
get maxRestartAttempts(): number {
return appConfig.opencode.maxRestartAttempts;
return OPENCODE_PORT;
}
isRunning(): boolean {
@@ -36,23 +28,6 @@ class OpenCodeService {
};
}
private getOpenCodePath(): string {
if (appConfig.opencode.mode === 'exe') {
const basePath = app.isPackaged
? path.dirname(app.getPath('exe'))
: path.join(process.cwd(), 'bin');
return path.join(basePath, appConfig.opencode.exePath);
}
return 'opencode';
}
private getOpenCodeArgs(): string[] {
if (appConfig.opencode.mode === 'exe') {
return ['serve'];
}
return ['serve'];
}
private getAuthHeaders(): Record<string, string> {
const password = 'xc_opencode_password';
const encoded = Buffer.from(`:${password}`).toString('base64');
@@ -76,14 +51,14 @@ class OpenCodeService {
}
private async restart(): Promise<void> {
if (this.restartAttempts >= this.maxRestartAttempts) {
if (this.restartAttempts >= MAX_RESTART_ATTEMPTS) {
log.error('[OpenCodeService] Max restart attempts reached, giving up');
this._isRunning = false;
return;
}
this.restartAttempts++;
log.info(`[OpenCodeService] Attempting restart (${this.restartAttempts}/${this.maxRestartAttempts})...`);
log.info(`[OpenCodeService] Attempting restart (${this.restartAttempts}/${MAX_RESTART_ATTEMPTS})...`);
await this.stop();
await this.start();
@@ -100,7 +75,7 @@ class OpenCodeService {
log.warn('[OpenCodeService] Health check failed, attempting restart...');
await this.restart();
}
}, this.healthCheckInterval);
}, HEALTH_CHECK_INTERVAL);
}
async start(): Promise<{ success: boolean; error?: string }> {
@@ -110,14 +85,9 @@ class OpenCodeService {
}
try {
const opencodePath = this.getOpenCodePath();
const opencodeArgs = this.getOpenCodeArgs();
const mode = appConfig.opencode.mode;
log.info('[OpenCodeService] Starting OpenCode server...');
log.info(`[OpenCodeService] Starting OpenCode server (mode: ${mode})...`);
log.info(`[OpenCodeService] Path: ${opencodePath}`);
this.process = spawn(opencodePath, opencodeArgs, {
this.process = spawn('opencode', ['serve'], {
stdio: 'pipe',
shell: true,
detached: false,