mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-28 23:15:33 +08:00
fix: preserve opencode session creation events
OpenCode emits the real session id asynchronously on its first JSON output. The runner registered that id from a helper that could not see the spawned process because the process reference was scoped inside the model-resolution callback. That ReferenceError was swallowed by the generic JSON parse fallback, so the client never received session_created. Without that event, a new OpenCode chat stayed on / and the assistant stream was not attached to the new session view. Keep the process reference in the outer spawn scope so registration can update the active-process map and websocket writer as soon as OpenCode announces the session id. Split JSON parsing from event processing so malformed non-JSON output can still stream as raw text, while registration or adapter failures are surfaced as real errors instead of being hidden as assistant content. Add a fake opencode executable regression test to lock in the expected lifecycle ordering: session_created must be sent before live assistant messages, and the same session id must carry through stream_end and complete.
This commit is contained in:
@@ -29,6 +29,7 @@ async function spawnOpenCode(command, options = {}, ws) {
|
||||
let sessionCreatedSent = false;
|
||||
let stdoutLineBuffer = '';
|
||||
let terminalNotificationSent = false;
|
||||
let opencodeProcess = null;
|
||||
|
||||
const notifyTerminalState = ({ code = null, error = null } = {}) => {
|
||||
if (terminalNotificationSent) {
|
||||
@@ -63,11 +64,13 @@ async function spawnOpenCode(command, options = {}, ws) {
|
||||
}
|
||||
|
||||
capturedSessionId = nextSessionId;
|
||||
if (processKey !== capturedSessionId) {
|
||||
if (processKey !== capturedSessionId && opencodeProcess) {
|
||||
activeOpenCodeProcesses.delete(processKey);
|
||||
activeOpenCodeProcesses.set(capturedSessionId, opencodeProcess);
|
||||
}
|
||||
opencodeProcess.sessionId = capturedSessionId;
|
||||
if (opencodeProcess) {
|
||||
opencodeProcess.sessionId = capturedSessionId;
|
||||
}
|
||||
|
||||
if (ws.setSessionId && typeof ws.setSessionId === 'function') {
|
||||
ws.setSessionId(capturedSessionId);
|
||||
@@ -89,8 +92,20 @@ async function spawnOpenCode(command, options = {}, ws) {
|
||||
return;
|
||||
}
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = JSON.parse(line);
|
||||
} catch {
|
||||
ws.send(createNormalizedMessage({
|
||||
kind: 'stream_delta',
|
||||
content: line,
|
||||
sessionId: capturedSessionId || sessionId || null,
|
||||
provider: 'opencode',
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = JSON.parse(line);
|
||||
registerSession(readOpenCodeSessionId(response));
|
||||
const normalized = sessionsService.normalizeMessage(
|
||||
'opencode',
|
||||
@@ -100,10 +115,12 @@ async function spawnOpenCode(command, options = {}, ws) {
|
||||
for (const msg of normalized) {
|
||||
ws.send(msg);
|
||||
}
|
||||
} catch {
|
||||
} catch (error) {
|
||||
const errorContent = error instanceof Error ? error.message : String(error);
|
||||
console.error('[OpenCode] Failed to process JSON output:', errorContent);
|
||||
ws.send(createNormalizedMessage({
|
||||
kind: 'stream_delta',
|
||||
content: line,
|
||||
kind: 'error',
|
||||
content: errorContent,
|
||||
sessionId: capturedSessionId || sessionId || null,
|
||||
provider: 'opencode',
|
||||
}));
|
||||
@@ -122,7 +139,7 @@ async function spawnOpenCode(command, options = {}, ws) {
|
||||
args.push(command.trim());
|
||||
}
|
||||
|
||||
const opencodeProcess = spawnFunction('opencode', args, {
|
||||
opencodeProcess = spawnFunction('opencode', args, {
|
||||
cwd: workingDir,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
env: { ...process.env },
|
||||
|
||||
Reference in New Issue
Block a user