refactor: parse opencode JSON stream output directly, remove extra session list call, increase timeout to 60s

This commit is contained in:
2026-06-15 12:02:40 +08:00
parent 525fa5ef8f
commit 78208cd4b1
2 changed files with 235 additions and 54 deletions

190
package-lock.json generated
View File

@@ -20,6 +20,7 @@
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue": "^5.0.0", "@vitejs/plugin-vue": "^5.0.0",
"adm-zip": "^0.5.17", "adm-zip": "^0.5.17",
"opencode-ai": "^1.17.6",
"typescript": "~5.6.0", "typescript": "~5.6.0",
"vite": "^5.4.0", "vite": "^5.4.0",
"vue-tsc": "^2.1.0" "vue-tsc": "^2.1.0"
@@ -1304,6 +1305,195 @@
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
} }
}, },
"node_modules/opencode-ai": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-ai/-/opencode-ai-1.17.7.tgz",
"integrity": "sha512-5oMjuqlVL78JhvXshwp2NCXCI+CHr24wWi7/5aI0CZoHnI44qTqssWOBUW59dPTpRGSOQmXTDTOOsPVYW38JPg==",
"cpu": [
"arm64",
"x64"
],
"dev": true,
"hasInstallScript": true,
"os": [
"darwin",
"linux",
"win32"
],
"bin": {
"opencode": "bin/opencode.exe"
},
"optionalDependencies": {
"opencode-darwin-arm64": "1.17.7",
"opencode-darwin-x64": "1.17.7",
"opencode-darwin-x64-baseline": "1.17.7",
"opencode-linux-arm64": "1.17.7",
"opencode-linux-arm64-musl": "1.17.7",
"opencode-linux-x64": "1.17.7",
"opencode-linux-x64-baseline": "1.17.7",
"opencode-linux-x64-baseline-musl": "1.17.7",
"opencode-linux-x64-musl": "1.17.7",
"opencode-windows-arm64": "1.17.7",
"opencode-windows-x64": "1.17.7",
"opencode-windows-x64-baseline": "1.17.7"
}
},
"node_modules/opencode-darwin-arm64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-darwin-arm64/-/opencode-darwin-arm64-1.17.7.tgz",
"integrity": "sha512-/uoZpJvnxY1jtRXAASQTIn0goya61M1RJhX0Zx2RwO+sdnrfvYYX6p7iL82Rl+Sp+TAS8y2NBvN+p/OLAnxsqg==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
]
},
"node_modules/opencode-darwin-x64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-darwin-x64/-/opencode-darwin-x64-1.17.7.tgz",
"integrity": "sha512-v60XhJae1eKn/Kjhy2PLOY+ss7peSox8ILZFz7fwBzRgz4q61gIo1vM9WzXQ6Vt+5Oj8etYbPl3xmt1XDLZtEA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
]
},
"node_modules/opencode-darwin-x64-baseline": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-darwin-x64-baseline/-/opencode-darwin-x64-baseline-1.17.7.tgz",
"integrity": "sha512-nvaY4qQgS9ZSkCvw9+DOrQQeycbW8/AEcD/Q0suleMuUgFIqfrsVa4cdsmYrUh3BH+y2NRaVgMSIfU9gPqXAKA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
]
},
"node_modules/opencode-linux-arm64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-arm64/-/opencode-linux-arm64-1.17.7.tgz",
"integrity": "sha512-HTH5Z5V7xiAD+/nYn9wQYwM/LDokBZu8Ig+npwJrhGWzZGM+lChbv2+griYyRoaNEZ+zRsCvwyv96TTfb6nWDQ==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-linux-arm64-musl": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-arm64-musl/-/opencode-linux-arm64-musl-1.17.7.tgz",
"integrity": "sha512-bIySXi+XNLHL5m8lS1ljL5XZzQ0iMdf/X6KKaqHbDZQ3E0Qu7ERIHZjohx8S7htvCdPztuzeSr2udS0emumf6g==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-linux-x64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-x64/-/opencode-linux-x64-1.17.7.tgz",
"integrity": "sha512-UIxkdA/8281EHbHYVr5PSD+eVoMdlyfkmXiZp3u9duttsMHdf1F6lw0XjYmDRBCPp8zQM1D1RLCABuRA/kUX+Q==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-linux-x64-baseline": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-x64-baseline/-/opencode-linux-x64-baseline-1.17.7.tgz",
"integrity": "sha512-fj62eWDQSygxS3Q5S3Gm4VYOsHrlTnje46bXoWc9IXfFNwFHAyL+izKzpU1SePCpCCEdsjy//nCKOnqN4PPK5g==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-linux-x64-baseline-musl": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-x64-baseline-musl/-/opencode-linux-x64-baseline-musl-1.17.7.tgz",
"integrity": "sha512-kTuZpRxMOzKt+ztp6yb+cSN8L69UYWN1x7Na4egX2d26IU0xK+RlXE9HjHzF/EjsmSBMc3DR8MvKUVldQ2XdbA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-linux-x64-musl": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-linux-x64-musl/-/opencode-linux-x64-musl-1.17.7.tgz",
"integrity": "sha512-iKUBKzVD1ybMmAy3KW6cjfst18+glp3Fgtd7POGEAaQO7cWzwSmOnFHN3uCAi/wYrfrvYd4zXxHsFP0yMJRUmA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
]
},
"node_modules/opencode-windows-arm64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-windows-arm64/-/opencode-windows-arm64-1.17.7.tgz",
"integrity": "sha512-BVlfloqHrjPhpDvbm3u1vQuEn063lbT3lcT7HBLmHpvwJd6FJutjPpm5/3xYxSusmXRGL7bmMZ3v1KPOeY0tBQ==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"win32"
]
},
"node_modules/opencode-windows-x64": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-windows-x64/-/opencode-windows-x64-1.17.7.tgz",
"integrity": "sha512-MAykQj6ouoZ2rMt+q8ujBTnc4sD86WfLnPom28CTD+KgmI1s2D2qka7J6DjpK6A3j8PpkC0bEBIqVBciVgY6EA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"win32"
]
},
"node_modules/opencode-windows-x64-baseline": {
"version": "1.17.7",
"resolved": "https://registry.npmmirror.com/opencode-windows-x64-baseline/-/opencode-windows-x64-baseline-1.17.7.tgz",
"integrity": "sha512-Gui/cezrLsLEZb1rUwNoKGXIiuZA0FsaGN3L/qR3/Qpce3e+hhqfqLHQXqlh0PFU1dSRKVRF8ukwWVx+2G5CPQ==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"win32"
]
},
"node_modules/path-browserify": { "node_modules/path-browserify": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",

View File

@@ -33,6 +33,31 @@ function apiSavePlugin() {
const dedupMap = new Map<string, number>() 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) => { server.middlewares.use('/api/ai', (req: any, res: any) => {
if (req.method !== 'POST') { res.writeHead(405); res.end(); return } if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
let body = '' let body = ''
@@ -62,8 +87,8 @@ function apiSavePlugin() {
const child = spawn('npx', args, { const child = spawn('npx', args, {
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' }, env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
timeout: 15000, timeout: 60000,
shell: true, stdio: ['ignore', 'pipe', 'pipe'],
}) })
let stdout = '' let stdout = ''
@@ -84,46 +109,37 @@ function apiSavePlugin() {
responded = true responded = true
if (code !== 0) { if (code !== 0) {
res.writeHead(500) 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 return
} }
let resolvedSessionId = sessionId let resolvedSessionId = sessionId
if (!resolvedSessionId) { let aiText = ''
for (const line of stdout.trim().split('\n')) {
try { try {
const listChild = spawn('npx', ['opencode', 'session', 'list', '--format', 'json', '--max-count', '1'], { const event = JSON.parse(line)
timeout: 5000, if (!resolvedSessionId && event.sessionID) resolvedSessionId = event.sessionID
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' }, if (event.type === 'text' && event.part?.text) {
shell: true, aiText = event.part.text
}) }
let listOut = '' } catch { continue }
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 {}
} }
if (mode === 'json') { if (mode === 'json') {
const match = stdout.match(/```json\n?([\s\S]*?)\n?```|\{[\s\S]*\}/) const jsonMatch = aiText.match(/```json\n?([\s\S]*?)\n?```/)
const jsonStr = match ? (match[1] || match[0]) : stdout const jsonStr = jsonMatch ? jsonMatch[1] : aiText
try { JSON.parse(jsonStr) } catch { try {
JSON.parse(jsonStr)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ result: jsonStr, sessionId: resolvedSessionId || '' }))
} catch {
res.writeHead(500) res.writeHead(500)
res.end(JSON.stringify({ error: 'invalid JSON returned', raw: stdout })) 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 { } else {
res.writeHead(200, { 'Content-Type': 'application/json' }) 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) { } 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('[]')
}
})
}, },
} }
} }