diff --git a/vite.config.ts b/vite.config.ts index 68c9481..592408e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -118,7 +118,9 @@ function apiSavePlugin() { let resolvedSessionId = sessionId let aiText = '' - for (const line of stdout.trim().split('\n')) { + for (const rawLine of stdout.split(/\r?\n/)) { + const line = rawLine.trim() + if (!line) continue try { const event = JSON.parse(line) if (!resolvedSessionId && event.sessionID) resolvedSessionId = event.sessionID @@ -129,16 +131,16 @@ function apiSavePlugin() { } if (mode === 'json') { - const jsonMatch = aiText.match(/```json\n?([\s\S]*?)\n?```/) - const jsonStr = jsonMatch ? jsonMatch[1] : aiText + const codeBlock = aiText.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/) + let jsonStr = codeBlock ? codeBlock[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 })) + const bareMatch = aiText.match(/(\{[\s\S]*\}|\[[\s\S]*\])/) + jsonStr = bareMatch ? bareMatch[0] : aiText } + 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: aiText || 'done', sessionId: resolvedSessionId || '' }))