fix: use npx opencode instead of direct bin path, add spawn error handling
This commit is contained in:
@@ -56,12 +56,11 @@ function apiSavePlugin() {
|
||||
: 'JSON模式:只返回修改后的 JSON 文本,不要写任何文件。需求:'
|
||||
const fullMessage = modePrefix + userMessage
|
||||
|
||||
const args = ['run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']
|
||||
if (sessionId) args.push('--session', sessionId)
|
||||
const args = ['opencode', 'run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']
|
||||
if (sessionId) args.splice(2, 0, '--session', sessionId)
|
||||
args.push(fullMessage)
|
||||
|
||||
const opencodeBin = resolve(__dirname, 'node_modules', '.bin', 'opencode')
|
||||
const child = spawn(opencodeBin, args, {
|
||||
const child = spawn('npx', args, {
|
||||
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
|
||||
timeout: 15000,
|
||||
})
|
||||
@@ -71,6 +70,11 @@ function apiSavePlugin() {
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.stderr.on('data', (d: Buffer) => stderr += d.toString())
|
||||
|
||||
child.on('error', (err) => {
|
||||
res.writeHead(503)
|
||||
res.end(JSON.stringify({ error: 'opencode 未安装,请运行 npm install' }))
|
||||
})
|
||||
|
||||
child.on('close', async (code) => {
|
||||
if (code !== 0) {
|
||||
res.writeHead(500)
|
||||
@@ -81,12 +85,13 @@ function apiSavePlugin() {
|
||||
let resolvedSessionId = sessionId
|
||||
if (!resolvedSessionId) {
|
||||
try {
|
||||
const listChild = spawn(opencodeBin, ['session', 'list', '--format', 'json', '--max-count', '1'], {
|
||||
const listChild = spawn('npx', ['opencode', 'session', 'list', '--format', 'json', '--max-count', '1'], {
|
||||
timeout: 5000,
|
||||
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
|
||||
})
|
||||
let listOut = ''
|
||||
listChild.stdout.on('data', (d: Buffer) => listOut += d.toString())
|
||||
listChild.on('error', () => {})
|
||||
await new Promise<void>((resolveList) => listChild.on('close', () => {
|
||||
try {
|
||||
const sessions = JSON.parse(listOut)
|
||||
@@ -122,11 +127,15 @@ function apiSavePlugin() {
|
||||
})
|
||||
|
||||
server.middlewares.use('/api/ai/sessions', (req: any, res: any) => {
|
||||
if (req.method !== 'GET') { res.writeHead(405); res.end(); return }
|
||||
try {
|
||||
const opencodeBin = resolve(__dirname, 'node_modules', '.bin', 'opencode')
|
||||
const child = spawn(opencodeBin, ['session', 'list', '--format', 'json'], { timeout: 5000 })
|
||||
const child = spawn('npx', ['opencode', 'session', 'list', '--format', 'json'], { timeout: 5000 })
|
||||
let stdout = ''
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.on('error', () => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
})
|
||||
child.on('close', () => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(stdout || '[]')
|
||||
|
||||
Reference in New Issue
Block a user