@@ -60,6 +60,8 @@ type Service interface {
60 IsBusy() bool
60 IsBusy() bool
61 Summarize(ctx context.Context, sessionID string) error
61 Summarize(ctx context.Context, sessionID string) error
62 UpdateModel() error
62 UpdateModel() error
63+ QueuedPrompts(sessionID string) int
64+ ClearQueue(sessionID string)
65type agent struct {
67type agent struct {
@@ -79,6 +81,8 @@ type agent struct {
79 summarizeProviderID string
81 summarizeProviderID string
81 activeRequests *csync.Map[string, context.CancelFunc]
83 activeRequests *csync.Map[string, context.CancelFunc]
85+ promptQueue *csync.Map[string, []string]
84var agentPromptMap = map[string]prompt.PromptID{
88var agentPromptMap = map[string]prompt.PromptID{
@@ -228,6 +232,7 @@ func NewAgent(
228 summarizeProviderID: string(providerCfg.ID),
232 summarizeProviderID: string(providerCfg.ID),
229 activeRequests: csync.NewMap[string, context.CancelFunc](),
233 activeRequests: csync.NewMap[string, context.CancelFunc](),
230 tools: csync.NewLazySlice(toolFn),
234 tools: csync.NewLazySlice(toolFn),
235+ promptQueue: csync.NewMap[string, []string](),
@@ -247,6 +252,11 @@ func (a *agent) Cancel(sessionID string) {
247 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
252 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
256+ if a.QueuedPrompts(sessionID) > 0 {
257+ slog.Info("Clearing queued prompts", "session_id", sessionID)
258+ a.promptQueue.Del(sessionID)
252func (a *agent) IsBusy() bool {
262func (a *agent) IsBusy() bool {
@@ -265,6 +275,14 @@ func (a *agent) IsSessionBusy(sessionID string) bool {
265 return busy
275 return busy
278+func (a *agent) QueuedPrompts(sessionID string) int {
279+ l, ok := a.promptQueue.Get(sessionID)
268func (a *agent) generateTitle(ctx context.Context, sessionID string, content string) error {
286func (a *agent) generateTitle(ctx context.Context, sessionID string, content string) error {
269 if content == "" {
287 if content == "" {
270 return nil
288 return nil
@@ -327,7 +345,13 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string, attac
328 events := make(chan AgentEvent)
346 events := make(chan AgentEvent)
329 if a.IsSessionBusy(sessionID) {
347 if a.IsSessionBusy(sessionID) {
348+ existing, ok := a.promptQueue.Get(sessionID)
350+ existing = []string{}
352+ existing = append(existing, content)
353+ a.promptQueue.Set(sessionID, existing)
330− return nil, ErrSessionBusy
354+ return nil, nil
333 genCtx, cancel := context.WithCancel(ctx)
357 genCtx, cancel := context.WithCancel(ctx)
@@ -422,7 +446,36 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
422 if (agentMessage.FinishReason() == message.FinishReasonToolUse) && toolResults != nil {
446 if (agentMessage.FinishReason() == message.FinishReasonToolUse) && toolResults != nil {
423 // We are not done, we need to respond with the tool response
447 // We are not done, we need to respond with the tool response
424 msgHistory = append(msgHistory, agentMessage, *toolResults)
448 msgHistory = append(msgHistory, agentMessage, *toolResults)
449+ // If there are queued prompts, process the next one
450+ nextPrompt, ok := a.promptQueue.Take(sessionID)
452+ for _, prompt := range nextPrompt {
453+ // Create a new user message for the queued prompt
454+ userMsg, err := a.createUserMessage(ctx, sessionID, prompt, nil)
456+ return a.err(fmt.Errorf("failed to create user message for queued prompt: %w", err))
458+ // Append the new user message to the conversation history
459+ msgHistory = append(msgHistory, userMsg)
464+ } else if agentMessage.FinishReason() == message.FinishReasonEndTurn {
465+ queuePrompts, ok := a.promptQueue.Take(sessionID)
467+ for _, prompt := range queuePrompts {
471+ userMsg, err := a.createUserMessage(ctx, sessionID, prompt, nil)
473+ return a.err(fmt.Errorf("failed to create user message for queued prompt: %w", err))
475+ msgHistory = append(msgHistory, userMsg)
427 if agentMessage.FinishReason() == "" {
480 if agentMessage.FinishReason() == "" {
428 // Kujtim: could not track down where this is happening but this means its cancelled
481 // 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 {
852 return nil
905 return nil
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)
855func (a *agent) CancelAll() {
915func (a *agent) CancelAll() {
856 if !a.IsBusy() {
916 if !a.IsBusy() {