From 880782b1dd9e93e9166f11094c1047eff38d1c56 Mon Sep 17 00:00:00 2001 From: DoSun Date: Fri, 8 May 2026 18:10:02 +0800 Subject: [PATCH] fix: remove debug logs, add model fallback, fix prompt_async flow - Remove process.stderr.write debug logs from sessionHandle and prompt_async - Add model fallback in assistant message (use this._model if msg.model missing) - Add model field to user message emit for adapter consumption - Wait for session ready before setModel/prompt in prompt_async - Return 200 JSON instead of 204 No Content from prompt_async - Set Bun.serve idleTimeout to 255s to prevent connection drops --- src/server/routes/session.ts | 28 ++++++++++++++++++++-------- src/server/server.ts | 1 + src/server/sessionHandle.ts | 4 ++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/server/routes/session.ts b/src/server/routes/session.ts index e2de66a41..1c2fe74ef 100644 --- a/src/server/routes/session.ts +++ b/src/server/routes/session.ts @@ -365,8 +365,11 @@ export function createSessionRoutes( }) .post('/session/:sessionID/prompt_async', async c => { const id = c.req.param('sessionID') + const handle = sessionManager.getSession(id) - if (!handle) throw notFound('session not found') + if (!handle) { + throw notFound('session not found') + } const body = await c.req.json<{ content?: string @@ -380,17 +383,26 @@ export function createSessionRoutes( ?.filter((p) => p.type === 'text' && p.text) .map((p) => p.text) .join('\n') ?? '' - if (!content) throw badRequest('content is required') - if (handle.prompting) throw conflict('session is already processing a prompt') + if (!content) { + throw badRequest('content is required') + } + if (handle.prompting) { + throw conflict('session is already processing a prompt') + } void (async () => { - if (body.model?.modelID) { - try { await handle.setModel(body.model.modelID) } catch {} - } - handle.prompt(content).catch(() => {}) + try { + if (handle.status !== 'running') { + await handle.waitReady() + } + if (body.model?.modelID) { + try { await handle.setModel(body.model.modelID) } catch {} + } + handle.prompt(content).catch(() => {}) + } catch {} })() - return new Response(null, { status: 204 }) + return c.json({ ok: true }, 200) }) .post('/session/:sessionID/abort', async c => { const id = c.req.param('sessionID') diff --git a/src/server/server.ts b/src/server/server.ts index 395836075..7599c965d 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -58,6 +58,7 @@ export function startServer( hostname: config.host, port: config.port, fetch: app.fetch, + idleTimeout: 255, }) return server diff --git a/src/server/sessionHandle.ts b/src/server/sessionHandle.ts index 5fb77901c..687067a34 100644 --- a/src/server/sessionHandle.ts +++ b/src/server/sessionHandle.ts @@ -451,6 +451,9 @@ export class SessionHandle { if (msg.uuid && typeof msg.uuid === 'string') { this._lastMessageUuid = msg.uuid } + if (!msg.model && this._model) { + msg = { ...msg, model: this._model } + } this.emitEvent('message', msg) break } @@ -602,6 +605,7 @@ export class SessionHandle { type: 'user', content, session_id: this.sessionId, + model: this._model ?? '', }) this.writeStdin(userMsg)