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