refactor: parse opencode JSON stream output directly, remove extra session list call, increase timeout to 60s
This commit is contained in:
@@ -33,6 +33,31 @@ function apiSavePlugin() {
|
||||
|
||||
const dedupMap = new Map<string, number>()
|
||||
|
||||
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'] })
|
||||
let stdout = ''
|
||||
let responded = false
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.on('error', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
})
|
||||
child.on('close', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(stdout || '[]')
|
||||
})
|
||||
} catch {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
}
|
||||
})
|
||||
|
||||
server.middlewares.use('/api/ai', (req: any, res: any) => {
|
||||
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
||||
let body = ''
|
||||
@@ -62,8 +87,8 @@ function apiSavePlugin() {
|
||||
|
||||
const child = spawn('npx', args, {
|
||||
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
|
||||
timeout: 15000,
|
||||
shell: true,
|
||||
timeout: 60000,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
})
|
||||
|
||||
let stdout = ''
|
||||
@@ -84,46 +109,37 @@ function apiSavePlugin() {
|
||||
responded = true
|
||||
if (code !== 0) {
|
||||
res.writeHead(500)
|
||||
res.end(JSON.stringify({ error: 'opencode exited with code ' + code, stderr }))
|
||||
res.end(JSON.stringify({ error: code === null ? 'opencode 超时,请重试或简化需求' : 'opencode exited with code ' + code, stderr }))
|
||||
return
|
||||
}
|
||||
|
||||
let resolvedSessionId = sessionId
|
||||
if (!resolvedSessionId) {
|
||||
let aiText = ''
|
||||
|
||||
for (const line of stdout.trim().split('\n')) {
|
||||
try {
|
||||
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 || '' },
|
||||
shell: true,
|
||||
})
|
||||
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)
|
||||
if (Array.isArray(sessions) && sessions.length > 0) {
|
||||
resolvedSessionId = sessions[0].id
|
||||
}
|
||||
} catch {}
|
||||
resolveList()
|
||||
}))
|
||||
} catch {}
|
||||
const event = JSON.parse(line)
|
||||
if (!resolvedSessionId && event.sessionID) resolvedSessionId = event.sessionID
|
||||
if (event.type === 'text' && event.part?.text) {
|
||||
aiText = event.part.text
|
||||
}
|
||||
} catch { continue }
|
||||
}
|
||||
|
||||
if (mode === 'json') {
|
||||
const match = stdout.match(/```json\n?([\s\S]*?)\n?```|\{[\s\S]*\}/)
|
||||
const jsonStr = match ? (match[1] || match[0]) : stdout
|
||||
try { JSON.parse(jsonStr) } catch {
|
||||
const jsonMatch = aiText.match(/```json\n?([\s\S]*?)\n?```/)
|
||||
const jsonStr = jsonMatch ? jsonMatch[1] : aiText
|
||||
try {
|
||||
JSON.parse(jsonStr)
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ result: jsonStr, sessionId: resolvedSessionId || '' }))
|
||||
} catch {
|
||||
res.writeHead(500)
|
||||
res.end(JSON.stringify({ error: 'invalid JSON returned', raw: stdout }))
|
||||
return
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ result: jsonStr, sessionId: resolvedSessionId || '' }))
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ result: stdout || 'done', sessionId: resolvedSessionId || '' }))
|
||||
res.end(JSON.stringify({ result: aiText || 'done', sessionId: resolvedSessionId || '' }))
|
||||
}
|
||||
})
|
||||
} catch (e: any) {
|
||||
@@ -132,31 +148,6 @@ 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, shell: true })
|
||||
let stdout = ''
|
||||
let responded = false
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.on('error', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
})
|
||||
child.on('close', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(stdout || '[]')
|
||||
})
|
||||
} catch {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
}
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user