Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat(desktop): a chat waiting for another shows Waiting, not Thinking

Salman Qureshibbd09970c7c2
diff --git a/apps/desktop/src/api/mercury.ts b/apps/desktop/src/api/mercury.ts
index fa534073..e886c186 100644
--- a/apps/desktop/src/api/mercury.ts
+++ b/apps/desktop/src/api/mercury.ts
@@ -58,6 +58,8 @@ export const chat = {
save: (threadId: string, saved: boolean) => http.post<{ id: string; saved: boolean; savedAt: string | null }>(`/api/chat/threads/${encodeURIComponent(threadId)}/save`, { saved }),
undo: (threadId: string, content: string) => http.post<{ ok: boolean; content: string; removed: number }>(`/api/chat/threads/${encodeURIComponent(threadId)}/undo`, { content }),
answer: (promptId: string, action: string, targetId?: string) => http.post<{ resolved: boolean }>(`/api/chat/permission/${encodeURIComponent(promptId)}`, { action, targetId }),
+ /** Put this chat's waiting message first in line. */
+ queueFirst: (threadId: string) => http.post<{ moved: number }>(`/api/chat/threads/${encodeURIComponent(threadId)}/queue/first`, {}),
/** Every slash command with what it does (the "/" picker). */
commands: () => http.get<{ commands: CommandEntry[] }>("/api/chat/commands").then((r) => r.commands),
settings: () => http.get<ChatSettings>("/api/chat/settings"),
diff --git a/apps/desktop/src/app/conversation.ts b/apps/desktop/src/app/conversation.ts
index 90c398f2..14c05229 100644
--- a/apps/desktop/src/app/conversation.ts
+++ b/apps/desktop/src/app/conversation.ts
@@ -25,6 +25,12 @@ export interface ConversationState {
live: ThoughtTrace | null;
/** The stop button was pressed and the runtime hasn't confirmed yet. */
stopping: boolean;
+ /**
+ * This chat's message is waiting in line: Mercury runs one turn at a time
+ * and another chat's is running. Cleared when this chat's turn starts.
+ */
+ waiting: { position: number; busyWith?: string; busySessionId?: string } | null;
+ setWaiting(waiting: ConversationState["waiting"]): void;
setStopping(stopping: boolean): void;
/** Text to put in the box (an undone message); the composer takes it once. */
draft: string | null;
@@ -55,6 +61,8 @@ function makeConversation(surface: Surface): ConversationStore {
messages: [],
streaming: "",
loading: false,
+ waiting: null,
+ setWaiting: (waiting) => set({ waiting }),
agent: "idle",
step: null,
prompts: [],
diff --git a/apps/desktop/src/app/events.ts b/apps/desktop/src/app/events.ts
index 106546fb..44764a11 100644
--- a/apps/desktop/src/app/events.ts
+++ b/apps/desktop/src/app/events.ts
@@ -119,7 +119,13 @@ export function handleEvent(store: ConversationStore, event: string, raw: string
// in a terminal, another device) reaches every toggle on screen.
if (data.permission === "allow-all" || data.permission === "ask-me") useRuntimeState.getState().setAllowAll(data.permission === "allow-all");
break;
+ case "queued":
+ // Waiting behind another chat's turn: say so, instead of "Thinking".
+ conv.setWaiting({ position: Number(data.position) || 1, busyWith: typeof data.busyWith === "string" ? data.busyWith : undefined, busySessionId: typeof data.busySessionId === "string" ? data.busySessionId : undefined });
+ conv.setAgent("thinking");
+ break;
case "thinking":
+ conv.setWaiting(null);
conv.setAgent("thinking");
conv.setLive((l) => l);
break;
@@ -141,10 +147,12 @@ export function handleEvent(store: ConversationStore, event: string, raw: string
break;
}
case "reasoning":
+ conv.setWaiting(null);
if (data.done === true) break;
if (typeof data.text === "string" && data.text) conv.setLive((l) => ({ ...l, reasoning: (l.reasoning + data.text).slice(-20_000) }));
break;
case "step_start": {
+ conv.setWaiting(null);
const tool = String(data.tool ?? "");
conv.setStep({ total: Number(data.totalSteps ?? 0), current: Number(data.step ?? data.stepNumber ?? 0), tool });
conv.setAgent("working");
@@ -166,10 +174,12 @@ export function handleEvent(store: ConversationStore, event: string, raw: string
break;
}
case "text_delta":
+ conv.setWaiting(null);
conv.setAgent("streaming");
conv.appendStreaming(String(data.text ?? ""));
break;
case "text_done": {
+ conv.setWaiting(null);
const text = String(data.fullText ?? data.text ?? raw);
// The runtime's reply to a stop is its "⏹ Task stopped" note: end the turn as stopped.
if (isStopNote(text) || (store.getState().stopping && !text.trim())) { finishStopped(store); void refreshThreads(); break; }
@@ -199,6 +209,7 @@ export function handleEvent(store: ConversationStore, event: string, raw: string
if (typeof data.id === "string") conv.removePrompt(data.id);
break;
case "error": {
+ conv.setWaiting(null);
const message = typeof data.message === "string" ? data.message : raw || "Something went wrong";
conv.clearStreaming();
conv.clearLive();
@@ -294,6 +305,15 @@ export async function openThread(surface: Surface, id: string | null): Promise<v
}
})
.catch(() => { /* older Mercury */ });
+ // Still waiting in line behind another chat? (Opened after the message was sent.)
+ store.getState().setWaiting(null);
+ void http.get<{ state: string; position?: number; busyWith?: string; busySessionId?: string }>(`/api/chat/threads/${encodeURIComponent(id)}/queue`)
+ .then((q) => {
+ if (store.getState().activeId !== id || q.state !== "queued") return;
+ store.getState().setWaiting({ position: q.position ?? 1, busyWith: q.busyWith, busySessionId: q.busySessionId });
+ store.getState().setAgent("thinking");
+ })
+ .catch(() => { /* older Mercury */ });
// The plan Mercury was working through in this conversation, if any.
void http.get<{ plan: { steps: Array<{ label: string; status: "pending" | "active" | "done" }>; updatedAt: number } | null }>(`/api/chat/plan?sessionId=${encodeURIComponent(id)}`)
.then((r) => { if (store.getState().activeId === id && r.plan?.steps?.length) store.getState().setPlan(r.plan); })
diff --git a/apps/desktop/src/app/waiting.test.ts b/apps/desktop/src/app/waiting.test.ts
new file mode 100644
index 00000000..ce00f280
--- /dev/null
+++ b/apps/desktop/src/app/waiting.test.ts
@@ -0,0 +1,30 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+/**
+ * A chat waiting behind another chat's turn shows "Waiting" (with what's
+ * ahead), not "Thinking" — until its own turn starts.
+ */
+vi.mock("~/api/mercury", () => ({ chat: {} }));
+
+const { conversations } = await import("./conversation");
+const { handleEvent } = await import("./events");
+const store = conversations.chat;
+
+describe("waiting for another chat", () => {
+ beforeEach(() => store.setState({ activeId: "jokes", waiting: null, agent: "idle", messages: [], streaming: "", live: null }));
+
+ it("a queued event shows where it stands and what's ahead", () => {
+ handleEvent(store, "queued", JSON.stringify({ targetId: "jokes", position: 2, busyWith: "Research the top news", busySessionId: "research" }));
+ expect(store.getState().waiting).toEqual({ position: 2, busyWith: "Research the top news", busySessionId: "research" });
+ expect(store.getState().agent).toBe("thinking");
+ });
+
+ it("clears as soon as its own turn starts", () => {
+ handleEvent(store, "queued", JSON.stringify({ position: 1 }));
+ handleEvent(store, "thinking", JSON.stringify({}));
+ expect(store.getState().waiting).toBeNull();
+ handleEvent(store, "queued", JSON.stringify({ position: 1 }));
+ handleEvent(store, "text_delta", JSON.stringify({ text: "Why did" }));
+ expect(store.getState().waiting).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/views/chat/Composer.tsx b/apps/desktop/src/views/chat/Composer.tsx
index 1674e394..7d334626 100644
--- a/apps/desktop/src/views/chat/Composer.tsx
+++ b/apps/desktop/src/views/chat/Composer.tsx
@@ -213,7 +213,9 @@ export function Composer({ surface, placeholder, code, extra, blockedReason, mon
const busy = agent === "thinking" || agent === "streaming" || agent === "working";
// While Mercury works the send button is a stop button; typing a follow-up turns it back into send.
- const showStop = busy && text.trim().length === 0 && files.length === 0;
+ // A chat waiting behind another's turn has nothing of its own to stop: Stop would stop the other chat.
+ const waiting = useConversation((s) => s.waiting);
+ const showStop = busy && !waiting && text.trim().length === 0 && files.length === 0;
function stop() {
setError(null);
void stopTurn(convStore).catch((err) => setError(err instanceof Error ? `Couldn't stop: ${err.message}` : "Couldn't stop"));
diff --git a/apps/desktop/src/views/chat/Conversation.tsx b/apps/desktop/src/views/chat/Conversation.tsx
index be6f8e0e..2e184ef9 100644
--- a/apps/desktop/src/views/chat/Conversation.tsx
+++ b/apps/desktop/src/views/chat/Conversation.tsx
@@ -10,6 +10,7 @@ import { Approval } from "~/components/indicators/Approval";
import { ThinkingDots } from "~/components/indicators/Thinking";
import { Message, StreamingMessage } from "./Message";
import { Composer, type ComposerProps } from "./Composer";
+import { WaitingNotice } from "./Waiting";
const PAGE = 30;
@@ -29,6 +30,7 @@ export function Conversation({ header, empty, composer }: { header: ReactNode; e
const step = useConversation((s) => s.step);
const prompts = useConversation((s) => s.prompts);
const live = useConversation((s) => s.live);
+ const waiting = useConversation((s) => s.waiting);
const stopping = useConversation((s) => s.stopping);
const scroller = useRef<HTMLDivElement>(null);
const content = useRef<HTMLDivElement>(null);
@@ -103,7 +105,9 @@ export function Conversation({ header, empty, composer }: { header: ReactNode; e
{(streaming || (live && (agent === "thinking" || agent === "working" || agent === "streaming" || agent === "needs-you"))) && (
<StreamingMessage text={streaming} live={live} stopping={stopping} />
)}
- {!streaming && !live && (agent === "thinking" || agent === "working") && (
+ {waiting && !streaming ? (
+ <WaitingNotice threadId={activeId} waiting={waiting} />
+ ) : !streaming && !live && (agent === "thinking" || agent === "working") && (
<ThinkingDots label={stopping ? "Stopping" : agent === "working" && step?.tool ? `Running ${step.tool}` : "Thinking"} className="pl-10" />
)}
{prompts.map((p, i) => (
diff --git a/apps/desktop/src/views/chat/Waiting.tsx b/apps/desktop/src/views/chat/Waiting.tsx
new file mode 100644
index 00000000..6169bf90
--- /dev/null
+++ b/apps/desktop/src/views/chat/Waiting.tsx
@@ -0,0 +1,65 @@
+import { Clock } from "lucide-react";
+import { useState } from "react";
+import * as api from "~/api/mercury";
+import { useChat, useUi } from "~/app/store";
+import { openThread } from "~/app/events";
+import { surfaceOf } from "~/app/notify/notify";
+import { Button } from "~/components/ui/Button";
+import { ConfirmButton } from "~/components/ui/kit";
+
+/**
+ * In place of "Thinking" while this chat's message waits its turn: Mercury
+ * works on one thing at a time, and another chat is ahead. Says which one and
+ * what it's doing, and offers to open it or to go first (which stops it).
+ */
+export function WaitingNotice({ threadId, waiting }: { threadId: string | null; waiting: { position: number; busyWith?: string; busySessionId?: string } }) {
+ const threads = useChat((s) => s.threads);
+ const [busy, setBusy] = useState(false);
+ const [problem, setProblem] = useState<string | null>(null);
+ const other = waiting.busySessionId ? threads.find((t) => t.id === waiting.busySessionId) : undefined;
+ const otherName = other?.title || other?.alias || (waiting.busySessionId ? "another chat" : "another task");
+ const ahead = waiting.position > 1 ? ` ${waiting.position - 1} more ${waiting.position - 1 === 1 ? "message is" : "messages are"} ahead of yours.` : " Yours is next.";
+
+ async function goFirst() {
+ if (!threadId) return;
+ setBusy(true);
+ setProblem(null);
+ try {
+ await api.chat.queueFirst(threadId);
+ await api.chat.stop();
+ } catch (e) {
+ setProblem(e instanceof Error ? e.message : String(e));
+ } finally {
+ setBusy(false);
+ }
+ }
+
+ function openOther() {
+ const id = waiting.busySessionId;
+ if (!id) return;
+ const surface = surfaceOf(id);
+ useUi.getState().setView(surface);
+ void openThread(surface, id);
+ }
+
+ return (
+ <div className="ml-10 flex flex-col gap-2.5 rounded-xl border border-line bg-soft px-4 py-3" role="status">
+ <div className="flex items-start gap-2.5">
+ <Clock size={15} className="mt-0.5 shrink-0 text-ink3" aria-hidden="true" />
+ <p className="text-sm text-ink2">
+ <span className="font-medium text-ink">Waiting.</span> Mercury works on one thing at a time and is finishing{" "}
+ {waiting.busyWith ? <>“{waiting.busyWith}”</> : "something"} in <span className="text-ink">{otherName}</span>.{ahead}
+ </p>
+ </div>
+ <div className="flex flex-wrap items-center gap-2 pl-[25px]">
+ {waiting.busySessionId && <Button size="sm" variant="ghost" onClick={openOther}>Open that chat</Button>}
+ {threadId && (
+ <ConfirmButton size="sm" variant="ghost" confirmLabel="Stop it and start yours?" loading={busy} onConfirm={() => void goFirst()}>
+ Do mine first
+ </ConfirmButton>
+ )}
+ {problem && <span className="text-xs text-red">{problem}</span>}
+ </div>
+ </div>
+ );
+}