perf: use direct opencode binary path instead of npx

This commit is contained in:
2026-06-15 12:30:57 +08:00
parent c2a9fcdb2e
commit f346f8d568

View File

@@ -8,6 +8,8 @@ function apiSavePlugin() {
return {
name: 'api-save',
configureServer(server: any) {
const opencodeBin = resolve(__dirname, 'node_modules', 'opencode-ai', 'bin', 'opencode.exe')
server.middlewares.use('/api/save', (req: any, res: any) => {
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
let body = ''
@@ -36,7 +38,7 @@ function apiSavePlugin() {
server.middlewares.use('/api/ai/sessions', (req: any, res: any) => {
if (req.method !== 'GET') { res.writeHead(405); res.end(); return }
try {
const child = spawn('npx', ['opencode', 'session', 'list', '--format', 'json'], { timeout: 5000, stdio: ['ignore', 'pipe', 'pipe'], shell: process.platform === 'win32' })
const child = spawn(opencodeBin, ['session', 'list', '--format', 'json'], { timeout: 5000, stdio: ['ignore', 'pipe', 'pipe'] })
let stdout = ''
let responded = false
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
@@ -81,15 +83,14 @@ function apiSavePlugin() {
: 'JSON模式只返回修改后的 JSON 文本,不要写任何文件。需求:'
const fullMessage = modePrefix + userMessage
const args = ['opencode', 'run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']
if (sessionId) args.splice(2, 0, '--session', sessionId)
const args = ['run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']
if (sessionId) args.push('--session', sessionId)
args.push(fullMessage)
const child = spawn('npx', args, {
const child = spawn(opencodeBin, args, {
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
timeout: 60000,
stdio: ['ignore', 'pipe', 'pipe'],
shell: process.platform === 'win32',
})
let stdout = ''