fix(remote): 添加 /api/files/upload 路由支持文件上传
This commit is contained in:
97
remote/src/services/opencode/OpenCodeService.js
Normal file
97
remote/src/services/opencode/OpenCodeService.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const { spawn } = require('child_process');
|
||||
const config = require('../../config');
|
||||
const logger = require('../../utils/logger');
|
||||
|
||||
class OpenCodeService {
|
||||
constructor() {
|
||||
this.process = null;
|
||||
this.isRunning = false;
|
||||
this.port = 3002;
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.isRunning) {
|
||||
logger.warn('OpenCode service is already running');
|
||||
return;
|
||||
}
|
||||
|
||||
const password = config.get('security.password');
|
||||
if (!password) {
|
||||
logger.error('OpenCode password not found in config');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info('Starting OpenCode service', { port: this.port });
|
||||
|
||||
const env = {
|
||||
...process.env,
|
||||
OPENCODE_SERVER_PASSWORD: password
|
||||
};
|
||||
|
||||
this.process = spawn('opencode', ['serve', '--port', this.port.toString()], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env,
|
||||
windowsHide: true
|
||||
});
|
||||
|
||||
this.isRunning = true;
|
||||
|
||||
this.process.stdout.on('data', (data) => {
|
||||
const output = data.toString().trim();
|
||||
if (output) {
|
||||
logger.info(`[OpenCode] ${output}`);
|
||||
}
|
||||
});
|
||||
|
||||
this.process.stderr.on('data', (data) => {
|
||||
const output = data.toString().trim();
|
||||
if (output) {
|
||||
logger.error(`[OpenCode] ${output}`);
|
||||
}
|
||||
});
|
||||
|
||||
this.process.on('error', (error) => {
|
||||
logger.error('OpenCode process error', { error: error.message });
|
||||
this.isRunning = false;
|
||||
});
|
||||
|
||||
this.process.on('close', (code) => {
|
||||
logger.info('OpenCode process closed', { code });
|
||||
this.isRunning = false;
|
||||
this.process = null;
|
||||
});
|
||||
|
||||
logger.info('OpenCode service started successfully');
|
||||
} catch (error) {
|
||||
logger.error('Failed to start OpenCode service', { error: error.message });
|
||||
this.isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.isRunning || !this.process) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Stopping OpenCode service');
|
||||
|
||||
try {
|
||||
this.process.kill();
|
||||
this.process = null;
|
||||
this.isRunning = false;
|
||||
logger.info('OpenCode service stopped');
|
||||
} catch (error) {
|
||||
logger.error('Failed to stop OpenCode service', { error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
getStatus() {
|
||||
return {
|
||||
running: this.isRunning,
|
||||
port: this.port
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OpenCodeService;
|
||||
Reference in New Issue
Block a user