From fd2255c83a67e7f547d07a4383873090ed4ddfd6 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Fri, 13 Mar 2026 20:34:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20XCOpenCodeWeb.exe?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E5=92=8C=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 electron/config.ts 配置文件 - 支持 command(命令行) 和 exe 两种模式 - 更新 package.json 打包配置,添加 bin 目录 - 更新 .gitignore 忽略 bin/*.exe --- .gitignore | 1 + electron/config.ts | 9 ++++++ electron/services/opencodeService.ts | 48 ++++++++++++++++++++++------ package.json | 4 ++- 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 electron/config.ts diff --git a/.gitignore b/.gitignore index 03fcb72..f0dfd3b 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ dist-electron/ # XCOpenCodeWeb (来自独立仓库 https://github.com/anomalyco/XCOpenCodeWeb) remote/xcopencodeweb/XCOpenCodeWeb.exe service/xcopencodeweb/XCOpenCodeWeb.exe +bin/*.exe # Tools output tools/tongyi/ppt_output/ diff --git a/electron/config.ts b/electron/config.ts new file mode 100644 index 0000000..aaa158a --- /dev/null +++ b/electron/config.ts @@ -0,0 +1,9 @@ +export const appConfig = { + opencode: { + mode: 'command', // 'command' | 'exe' + exePath: 'bin/XCOpenCodeWeb.exe', + port: 4096, + healthCheckInterval: 10000, + maxRestartAttempts: 3, + }, +} diff --git a/electron/services/opencodeService.ts b/electron/services/opencodeService.ts index a46e06e..45b23a4 100644 --- a/electron/services/opencodeService.ts +++ b/electron/services/opencodeService.ts @@ -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,7 +13,15 @@ class OpenCodeService { private _isRunning = false; get port(): number { - return OPENCODE_PORT; + return appConfig.opencode.port; + } + + get healthCheckInterval(): number { + return appConfig.opencode.healthCheckInterval; + } + + get maxRestartAttempts(): number { + return appConfig.opencode.maxRestartAttempts; } isRunning(): boolean { @@ -28,6 +36,23 @@ 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 { const password = 'xc_opencode_password'; const encoded = Buffer.from(`:${password}`).toString('base64'); @@ -51,14 +76,14 @@ class OpenCodeService { } private async restart(): Promise { - if (this.restartAttempts >= MAX_RESTART_ATTEMPTS) { + if (this.restartAttempts >= this.maxRestartAttempts) { log.error('[OpenCodeService] Max restart attempts reached, giving up'); this._isRunning = false; return; } this.restartAttempts++; - log.info(`[OpenCodeService] Attempting restart (${this.restartAttempts}/${MAX_RESTART_ATTEMPTS})...`); + log.info(`[OpenCodeService] Attempting restart (${this.restartAttempts}/${this.maxRestartAttempts})...`); await this.stop(); await this.start(); @@ -75,7 +100,7 @@ class OpenCodeService { log.warn('[OpenCodeService] Health check failed, attempting restart...'); await this.restart(); } - }, HEALTH_CHECK_INTERVAL); + }, this.healthCheckInterval); } async start(): Promise<{ success: boolean; error?: string }> { @@ -85,9 +110,14 @@ class OpenCodeService { } try { - log.info('[OpenCodeService] Starting OpenCode server...'); + const opencodePath = this.getOpenCodePath(); + const opencodeArgs = this.getOpenCodeArgs(); + const mode = appConfig.opencode.mode; - this.process = spawn('opencode', ['serve'], { + log.info(`[OpenCodeService] Starting OpenCode server (mode: ${mode})...`); + log.info(`[OpenCodeService] Path: ${opencodePath}`); + + this.process = spawn(opencodePath, opencodeArgs, { stdio: 'pipe', shell: true, detached: false, diff --git a/package.json b/package.json index 2f60ff8..04d07db 100644 --- a/package.json +++ b/package.json @@ -122,10 +122,12 @@ "dist-api/**/*", "shared/**/*", "tools/**/*", + "bin/**/*", "package.json" ], "asarUnpack": [ - "tools/**/*" + "tools/**/*", + "bin/**/*" ], "win": { "target": "nsis"