Skip to content

Commit 1cbbed3

Browse files
committed
feat: queue pending prompts when session is busy (anomalyco#16102)
1 parent d6fc5f4 commit 1cbbed3

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎packages/opencode/src/server/routes/session.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,22 @@ export const SessionRoutes = lazy(() =>
816816
return stream(c, async (stream) => {
817817
const sessionID = c.req.valid("param").sessionID
818818
const body = c.req.valid("json")
819-
const msg = await SessionPrompt.prompt({ ...body, sessionID })
820-
stream.write(JSON.stringify(msg))
819+
try {
820+
const msg = await SessionPrompt.prompt({ ...body, sessionID })
821+
stream.write(JSON.stringify(msg))
822+
} catch (err) {
823+
if (Session.BusyError.isInstance(err)) {
824+
SessionPrompt.queuePending(sessionID, {
825+
resolve: (msg) => stream.write(JSON.stringify(msg)),
826+
parts: body.parts,
827+
model: body.model,
828+
system: body.system,
829+
format: body.format,
830+
})
831+
} else {
832+
throw err
833+
}
834+
})
821835
})
822836
},
823837
)
@@ -848,13 +862,26 @@ export const SessionRoutes = lazy(() =>
848862
return stream(c, async () => {
849863
const sessionID = c.req.valid("param").sessionID
850864
const body = c.req.valid("json")
851-
SessionPrompt.prompt({ ...body, sessionID }).catch((err) => {
852-
log.error("prompt_async failed", { sessionID, error: err })
853-
Bus.publish(Session.Event.Error, {
854-
sessionID,
855-
error: new NamedError.Unknown({ message: err instanceof Error ? err.message : String(err) }).toObject(),
856-
})
857-
})
865+
try {
866+
await SessionPrompt.prompt({ ...body, sessionID })
867+
} catch (err) {
868+
if (Session.BusyError.isInstance(err)) {
869+
SessionPrompt.queuePending(sessionID, {
870+
resolve: () => {},
871+
parts: body.parts,
872+
model: body.model,
873+
system: body.system,
874+
format: body.format,
875+
})
876+
log.info("prompt_async queued", { sessionID })
877+
} else {
878+
log.error("prompt_async failed", { sessionID, error: err })
879+
Bus.publish(Session.Event.Error, {
880+
sessionID,
881+
error: new NamedError.Unknown({ message: err instanceof Error ? err.message : String(err) }).toObject(),
882+
})
883+
}
884+
}
858885
})
859886
},
860887
)

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export namespace SessionPrompt {
7676
resolve(input: MessageV2.WithParts): void
7777
reject(reason?: any): void
7878
}[]
79+
pending: {
80+
resolve(input: MessageV2.WithParts): void
81+
parts: MessageV2.Part[]
82+
model?: Provider.Model
83+
system?: string
84+
format?: MessageV2.Format
85+
}[]
7986
}
8087
> = {}
8188
return data
@@ -92,6 +99,23 @@ export namespace SessionPrompt {
9299
if (match) throw new Session.BusyError(sessionID)
93100
}
94101

102+
export function queuePending(
103+
sessionID: SessionID,
104+
input: {
105+
resolve(input: MessageV2.WithParts): void
106+
parts: MessageV2.Part[]
107+
model?: Provider.Model
108+
system?: string
109+
format?: MessageV2.Format
110+
},
111+
) {
112+
const s = state()
113+
if (!s[sessionID]) {
114+
s[sessionID] = { abort: new AbortController(), callbacks: [], pending: [] }
115+
}
116+
s[sessionID].pending.push(input)
117+
}
118+
95119
export const PromptInput = z.object({
96120
sessionID: SessionID.zod,
97121
messageID: MessageID.zod.optional(),
@@ -299,6 +323,37 @@ export namespace SessionPrompt {
299323
await SessionStatus.set(sessionID, { type: "busy" })
300324
log.info("loop", { step, sessionID })
301325
if (abort.aborted) break
326+
327+
// Drain pending messages and inject them as user messages BEFORE loading message history
328+
const pending = state()[sessionID]?.pending ?? []
329+
if (pending.length > 0) {
330+
state()[sessionID].pending = []
331+
for (const p of pending) {
332+
const agentName = await Agent.defaultAgent()
333+
const agent = await Agent.get(agentName)
334+
if (!agent) continue
335+
336+
const model = p.model ?? agent.model ?? (await lastModel(sessionID))
337+
const info: MessageV2.Info = {
338+
id: MessageID.ascending(),
339+
role: "user",
340+
sessionID,
341+
time: { created: Date.now() },
342+
agent: agent.name,
343+
model,
344+
system: p.system,
345+
format: p.format,
346+
}
347+
348+
await Session.updateMessage(info)
349+
for (const part of p.parts) {
350+
await Session.updatePart({ ...part, messageID: info.id, sessionID })
351+
}
352+
353+
p.resolve({ info, parts: p.parts })
354+
}
355+
}
356+
302357
let msgs = await MessageV2.filterCompacted(MessageV2.stream(sessionID))
303358

304359
let lastUser: MessageV2.User | undefined

0 commit comments

Comments
 (0)