@@ -60,6 +60,8 @@ type Service interface {
6060 IsBusy() bool
6161 Summarize(ctx context.Context, sessionID string) error
6262 UpdateModel() error
63+ QueuedPrompts(sessionID string) int
64+ ClearQueue(sessionID string)
6365}
6466
6567type agent struct {
@@ -79,6 +81,8 @@ type agent struct {
7981 summarizeProviderID string
8082
8183 activeRequests *csync.Map[string, context.CancelFunc]
84+
85+ promptQueue *csync.Map[string, []string]
8286}
8387
8488var agentPromptMap = map[string]prompt.PromptID{
@@ -228,6 +232,7 @@ func NewAgent(
228232 summarizeProviderID: string(providerCfg.ID),
229233 activeRequests: csync.NewMap[string, context.CancelFunc](),
230234 tools: csync.NewLazySlice(toolFn),
235+ promptQueue: csync.NewMap[string, []string](),
231236 }, nil
232237}
233238
@@ -247,6 +252,11 @@ func (a *agent) Cancel(sessionID string) {
247252 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
248253 cancel()
249254 }
255+
256+ if a.QueuedPrompts(sessionID) > 0 {
257+ slog.Info("Clearing queued prompts", "session_id", sessionID)
258+ a.promptQueue.Del(sessionID)
259+ }
250260}
251261
252262func (a *agent) IsBusy() bool {
@@ -265,6 +275,14 @@ func (a *agent) IsSessionBusy(sessionID string) bool {
265275 return busy
266276}
267277
278+func (a *agent) QueuedPrompts(sessionID string) int {
279+ l, ok := a.promptQueue.Get(sessionID)
280+ if !ok {
281+ return 0
282+ }
283+ return len(l)
284+}
285+
268286func (a *agent) generateTitle(ctx context.Context, sessionID string, content string) error {
269287 if content == "" {
270288 return nil
@@ -327,7 +345,13 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string, attac
327345 }
328346 events := make(chan AgentEvent)
329347 if a.IsSessionBusy(sessionID) {
330− return nil, ErrSessionBusy
348+ existing, ok := a.promptQueue.Get(sessionID)
349+ if !ok {
350+ existing = []string{}
351+ }
352+ existing = append(existing, content)
353+ a.promptQueue.Set(sessionID, existing)
354+ return nil, nil
331355 }
332356
333357 genCtx, cancel := context.WithCancel(ctx)
@@ -422,7 +446,36 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
422446 if (agentMessage.FinishReason() == message.FinishReasonToolUse) && toolResults != nil {
423447 // We are not done, we need to respond with the tool response
424448 msgHistory = append(msgHistory, agentMessage, *toolResults)
449+ // If there are queued prompts, process the next one
450+ nextPrompt, ok := a.promptQueue.Take(sessionID)
451+ if ok {
452+ for _, prompt := range nextPrompt {
453+ // Create a new user message for the queued prompt
454+ userMsg, err := a.createUserMessage(ctx, sessionID, prompt, nil)
455+ if err != nil {
456+ return a.err(fmt.Errorf("failed to create user message for queued prompt: %w", err))
457+ }
458+ // Append the new user message to the conversation history
459+ msgHistory = append(msgHistory, userMsg)
460+ }
461+ }
462+
425463 continue
464+ } else if agentMessage.FinishReason() == message.FinishReasonEndTurn {
465+ queuePrompts, ok := a.promptQueue.Take(sessionID)
466+ if ok {
467+ for _, prompt := range queuePrompts {
468+ if prompt == "" {
469+ continue
470+ }
471+ userMsg, err := a.createUserMessage(ctx, sessionID, prompt, nil)
472+ if err != nil {
473+ return a.err(fmt.Errorf("failed to create user message for queued prompt: %w", err))
474+ }
475+ msgHistory = append(msgHistory, userMsg)
476+ }
477+ continue
478+ }
426479 }
427480 if agentMessage.FinishReason() == "" {
428481 // Kujtim: could not track down where this is happening but this means its cancelled
@@ -852,6 +905,13 @@ func (a *agent) Summarize(ctx context.Context, sessionID string) error {
852905 return nil
853906}
854907
908+func (a *agent) ClearQueue(sessionID string) {
909+ if a.QueuedPrompts(sessionID) > 0 {
910+ slog.Info("Clearing queued prompts", "session_id", sessionID)
911+ a.promptQueue.Del(sessionID)
912+ }
913+}
914+
855915func (a *agent) CancelAll() {
856916 if !a.IsBusy() {
857917 return