Skip to content

Commit 2e603ea

Browse files
committed
Extract drainQueuedMessages helper to deduplicate queue logic
1 parent 9b44457 commit 2e603ea

File tree

1 file changed

+25
-31
lines changed
  • apps/code/src/renderer/features/sessions/service

1 file changed

+25
-31
lines changed

apps/code/src/renderer/features/sessions/service/service.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -767,30 +767,14 @@ export class SessionService {
767767
"stopReason" in msg.result
768768
) {
769769
const stopReason = (msg.result as { stopReason?: string }).stopReason;
770-
const freshSession = sessionStoreSetters.getSessions()[taskRunId];
771-
const hasQueuedMessages =
772-
freshSession &&
773-
freshSession.messageQueue.length > 0 &&
774-
freshSession.status === "connected";
770+
const hasQueuedMessages = this.drainQueuedMessages(taskRunId, session);
775771

776772
// Only notify when queue is empty - queued messages will start a new turn
777773
if (stopReason && !hasQueuedMessages) {
778774
notifyPromptComplete(session.taskTitle, stopReason, session.taskId);
779775
}
780776

781777
taskViewedApi.markActivity(session.taskId);
782-
783-
// Process queued messages after turn completes - send all as one prompt
784-
if (hasQueuedMessages) {
785-
setTimeout(() => {
786-
this.sendQueuedMessages(session.taskId).catch((err) => {
787-
log.error("Failed to send queued messages", {
788-
taskId: session.taskId,
789-
error: err,
790-
});
791-
});
792-
}, 0);
793-
}
794778
}
795779

796780
if ("method" in msg && msg.method === "session/update" && "params" in msg) {
@@ -875,22 +859,32 @@ export class SessionService {
875859
isCompacting: false,
876860
});
877861

878-
const freshSession = sessionStoreSetters.getSessions()[taskRunId];
879-
const hasQueuedMessages =
880-
freshSession &&
881-
freshSession.messageQueue.length > 0 &&
882-
freshSession.status === "connected";
883-
if (hasQueuedMessages) {
884-
setTimeout(() => {
885-
this.sendQueuedMessages(session.taskId).catch((err) => {
886-
log.error("Failed to send queued messages after compaction", {
887-
taskId: session.taskId,
888-
error: err,
889-
});
862+
this.drainQueuedMessages(taskRunId, session);
863+
}
864+
}
865+
866+
private drainQueuedMessages(
867+
taskRunId: string,
868+
session: AgentSession,
869+
): boolean {
870+
const freshSession = sessionStoreSetters.getSessions()[taskRunId];
871+
const hasQueuedMessages =
872+
freshSession &&
873+
freshSession.messageQueue.length > 0 &&
874+
freshSession.status === "connected";
875+
876+
if (hasQueuedMessages) {
877+
setTimeout(() => {
878+
this.sendQueuedMessages(session.taskId).catch((err) => {
879+
log.error("Failed to send queued messages", {
880+
taskId: session.taskId,
881+
error: err,
890882
});
891-
}, 0);
892-
}
883+
});
884+
}, 0);
893885
}
886+
887+
return hasQueuedMessages;
894888
}
895889

896890
private handlePermissionRequest(

0 commit comments

Comments
 (0)