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)