feat(chat): render the live turn's thinking in the dashboard (#1907)
Reasoning frames accumulate into streamingTurn.reasoning and the live row shows them in a collapsible Thinking trace built from the components/ui trace pattern, with a spinning icon and shimmering title while no reasoning has arrived yet. A turn that produces no reasoning renders no trace, and reasoning never leaks into the reply text.
This commit is contained in:
5 files changed
+197
-4
No files matched your search
@@ -7,6 +7,7 @@ import { CodeBlockCopyButton } from "@/components/ui/code-block";
|
|||||||
import { Message, MessageContent, MessageFooter } from "@/components/ui/message";
|
import { Message, MessageContent, MessageFooter } from "@/components/ui/message";
|
||||||
import ChatAttachment from "./ChatAttachment.vue";
|
import ChatAttachment from "./ChatAttachment.vue";
|
||||||
import ChatMarkdown from "./ChatMarkdown.vue";
|
import ChatMarkdown from "./ChatMarkdown.vue";
|
||||||
|
import ChatThinking from "./ChatThinking.vue";
|
||||||
import ChatToolCall from "./ChatToolCall.vue";
|
import ChatToolCall from "./ChatToolCall.vue";
|
||||||
import ChatToolGroup from "./ChatToolGroup.vue";
|
import ChatToolGroup from "./ChatToolGroup.vue";
|
||||||
import type { ChatMedia, ChatToolCall as ToolCall } from "./state";
|
import type { ChatMedia, ChatToolCall as ToolCall } from "./state";
|
||||||
@@ -27,8 +28,12 @@ const props = withDefaults(
|
|||||||
assistant?: boolean;
|
assistant?: boolean;
|
||||||
/** The turn is still arriving: a tool call with no outcome is running. */
|
/** The turn is still arriving: a tool call with no outcome is running. */
|
||||||
streaming?: boolean;
|
streaming?: boolean;
|
||||||
|
/** The live turn's thinking so far, rendered as a collapsible trace. */
|
||||||
|
reasoning?: string;
|
||||||
|
/** The live turn is still arriving and has not reasoned yet. */
|
||||||
|
thinkingPending?: boolean;
|
||||||
}>(),
|
}>(),
|
||||||
{ tools: () => [], media: () => [], assistant: false, streaming: false },
|
{ tools: () => [], media: () => [], assistant: false, streaming: false, reasoning: "", thinkingPending: false },
|
||||||
);
|
);
|
||||||
|
|
||||||
// The reply in the order it happened: text up to each call's offset, then the
|
// The reply in the order it happened: text up to each call's offset, then the
|
||||||
@@ -73,6 +78,11 @@ const segments = computed(() => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<template v-if="assistant">
|
<template v-if="assistant">
|
||||||
|
<ChatThinking
|
||||||
|
v-if="reasoning || thinkingPending"
|
||||||
|
:text="reasoning"
|
||||||
|
:pending="thinkingPending"
|
||||||
|
/>
|
||||||
<template v-for="(segment, i) in segments" :key="i">
|
<template v-for="(segment, i) in segments" :key="i">
|
||||||
<ChatMarkdown v-if="'text' in segment" :text="segment.text" />
|
<ChatMarkdown v-if="'text' in segment" :text="segment.text" />
|
||||||
<Trace v-else class="my-2">
|
<Trace v-else class="my-2">
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { BrainIcon } from "@lucide/vue";
|
||||||
|
|
||||||
|
import { Spinner } from "@/components/ui/spinner";
|
||||||
|
import {
|
||||||
|
Trace,
|
||||||
|
TraceCollapsible,
|
||||||
|
TraceCollapsibleContent,
|
||||||
|
TraceCollapsibleTrigger,
|
||||||
|
TraceIcon,
|
||||||
|
TraceLine,
|
||||||
|
TraceTitle,
|
||||||
|
} from "@/components/ui/trace";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The live turn's thinking: the model's reasoning as it streams, in the same
|
||||||
|
* collapsible trace a tool call uses. While the turn has not produced any
|
||||||
|
* reasoning yet, the icon spins and the title shimmers; once text arrives it
|
||||||
|
* streams into the open trace. A turn that never reasons renders nothing,
|
||||||
|
* which the caller decides by not mounting this at all.
|
||||||
|
*/
|
||||||
|
withDefaults(
|
||||||
|
defineProps<{
|
||||||
|
text?: string;
|
||||||
|
/** The turn is still arriving and has not reasoned yet. */
|
||||||
|
pending?: boolean;
|
||||||
|
}>(),
|
||||||
|
{ text: "", pending: false },
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Trace class="my-2">
|
||||||
|
<TraceCollapsible default-open>
|
||||||
|
<TraceCollapsibleTrigger>
|
||||||
|
<TraceLine :status="pending && !text ? 'running' : undefined">
|
||||||
|
<TraceIcon>
|
||||||
|
<Spinner v-if="pending && !text" />
|
||||||
|
<BrainIcon v-else />
|
||||||
|
</TraceIcon>
|
||||||
|
<TraceTitle>Thinking</TraceTitle>
|
||||||
|
</TraceLine>
|
||||||
|
</TraceCollapsibleTrigger>
|
||||||
|
<TraceCollapsibleContent v-if="text" class="whitespace-pre-wrap [overflow-wrap:anywhere]">{{ text }}</TraceCollapsibleContent>
|
||||||
|
</TraceCollapsible>
|
||||||
|
</Trace>
|
||||||
|
</template>
|
||||||
@@ -63,9 +63,21 @@ const bubbles = computed(() => messages.value.map(parts));
|
|||||||
// the scroller sees one row grow and settle, not one leave and another arrive.
|
// the scroller sees one row grow and settle, not one leave and another arrive.
|
||||||
const rows = computed(() => {
|
const rows = computed(() => {
|
||||||
const turn = streamingTurn.value;
|
const turn = streamingTurn.value;
|
||||||
if (!turn) return bubbles.value.map((bubble) => ({ ...bubble, live: false }));
|
if (!turn) {
|
||||||
|
return bubbles.value.map((bubble) => ({
|
||||||
|
...bubble,
|
||||||
|
live: false,
|
||||||
|
reasoning: "",
|
||||||
|
thinkingPending: false,
|
||||||
|
}));
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
...bubbles.value.map((bubble) => ({ ...bubble, live: false })),
|
...bubbles.value.map((bubble) => ({
|
||||||
|
...bubble,
|
||||||
|
live: false,
|
||||||
|
reasoning: "",
|
||||||
|
thinkingPending: false,
|
||||||
|
})),
|
||||||
{
|
{
|
||||||
id: `m${bubbles.value.length}`,
|
id: `m${bubbles.value.length}`,
|
||||||
text: turn.text || "…",
|
text: turn.text || "…",
|
||||||
@@ -75,6 +87,11 @@ const rows = computed(() => {
|
|||||||
update: undefined,
|
update: undefined,
|
||||||
choose: undefined,
|
choose: undefined,
|
||||||
live: true,
|
live: true,
|
||||||
|
reasoning: turn.reasoning ?? "",
|
||||||
|
// Nothing said and nothing reasoned yet: the turn may still be thinking,
|
||||||
|
// so the row shows the pending thinking trace. Once the answer's text
|
||||||
|
// arrives without any reasoning, the trace is not rendered at all.
|
||||||
|
thinkingPending: !turn.text && !(turn.reasoning ?? ""),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
@@ -120,6 +137,8 @@ useLiveResource("chat", () => void reloadTranscript());
|
|||||||
:media="row.media"
|
:media="row.media"
|
||||||
:assistant="row.assistant"
|
:assistant="row.assistant"
|
||||||
:streaming="row.live"
|
:streaming="row.live"
|
||||||
|
:reasoning="row.reasoning"
|
||||||
|
:thinking-pending="row.thinkingPending"
|
||||||
>
|
>
|
||||||
<template v-if="row.live && streamingTurn">
|
<template v-if="row.live && streamingTurn">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
+15
-1
@@ -124,6 +124,8 @@ export interface StreamingTurn {
|
|||||||
isError: boolean;
|
isError: boolean;
|
||||||
turn?: ChatTurn;
|
turn?: ChatTurn;
|
||||||
isRetry: boolean;
|
isRetry: boolean;
|
||||||
|
/** The model's thinking so far, streamed on `reasoning` frames. */
|
||||||
|
reasoning?: string;
|
||||||
/** A question the turn waits on until the operator answers it. */
|
/** A question the turn waits on until the operator answers it. */
|
||||||
ask?: ChatAsk;
|
ask?: ChatAsk;
|
||||||
}
|
}
|
||||||
@@ -731,10 +733,11 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let activeTools: ChatToolCall[] = [];
|
let activeTools: ChatToolCall[] = [];
|
||||||
streamingTurn.value = { text: "", tools: [], isError: false, turn, isRetry };
|
streamingTurn.value = { text: "", tools: [], isError: false, turn, isRetry, reasoning: "" };
|
||||||
askError.value = "";
|
askError.value = "";
|
||||||
|
|
||||||
let streamedText = "";
|
let streamedText = "";
|
||||||
|
let streamedReasoning = "";
|
||||||
let finished = false;
|
let finished = false;
|
||||||
let timedOut = false;
|
let timedOut = false;
|
||||||
let detached = false;
|
let detached = false;
|
||||||
@@ -806,6 +809,15 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
? { ...streamingTurn.value, text: streamedText }
|
? { ...streamingTurn.value, text: streamedText }
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
// The model's thinking streams ahead of the answer on its own frame
|
||||||
|
// kind; it accumulates like the text, but renders as the live turn's
|
||||||
|
// thinking trace rather than as reply text.
|
||||||
|
if (event.type === "reasoning") {
|
||||||
|
streamedReasoning += event.text;
|
||||||
|
streamingTurn.value = streamingTurn.value
|
||||||
|
? { ...streamingTurn.value, reasoning: streamedReasoning }
|
||||||
|
: null;
|
||||||
|
}
|
||||||
// A navigate result is appended to the same list as the tool calls: it
|
// A navigate result is appended to the same list as the tool calls: it
|
||||||
// is one more thing the turn did, and it renders as a chip rather than
|
// is one more thing the turn did, and it renders as a chip rather than
|
||||||
// a tool line.
|
// a tool line.
|
||||||
@@ -873,6 +885,7 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
isError: false,
|
isError: false,
|
||||||
turn,
|
turn,
|
||||||
isRetry,
|
isRetry,
|
||||||
|
reasoning: streamedReasoning,
|
||||||
};
|
};
|
||||||
statusText.value = "Stopped";
|
statusText.value = "Stopped";
|
||||||
} else {
|
} else {
|
||||||
@@ -886,6 +899,7 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
isError: true,
|
isError: true,
|
||||||
turn: turn ? retryChatTurn(turn) : undefined,
|
turn: turn ? retryChatTurn(turn) : undefined,
|
||||||
isRetry,
|
isRetry,
|
||||||
|
reasoning: streamedReasoning,
|
||||||
};
|
};
|
||||||
statusText.value = "Error. Retry available.";
|
statusText.value = "Error. Retry available.";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import test from "node:test";
|
||||||
|
import { register } from "node:module";
|
||||||
|
import { pathToFileURL } from "node:url";
|
||||||
|
import { resolve as resolvePath } from "node:path";
|
||||||
|
|
||||||
|
// state.ts reads the dashboard through "@/lib/api" and "@/router", which node
|
||||||
|
// cannot resolve on its own (and which pull in the whole SPA). A resolve hook
|
||||||
|
// maps the alias into src/ for the pure modules and stubs the two browser-only
|
||||||
|
// ones, so the real frame handling in state.ts runs under node.
|
||||||
|
const srcRoot = resolvePath(import.meta.dirname, "../src");
|
||||||
|
const apiStub =
|
||||||
|
"data:text/javascript," +
|
||||||
|
encodeURIComponent(
|
||||||
|
"export const api = new Proxy({}, { get: (_, p) => globalThis.__archieChatTestApi[p] });",
|
||||||
|
);
|
||||||
|
const routerStub =
|
||||||
|
"data:text/javascript," +
|
||||||
|
encodeURIComponent(
|
||||||
|
"export const router = { currentRoute: { value: { fullPath: '/', name: 'home', meta: {}, params: {} } } };",
|
||||||
|
);
|
||||||
|
register(
|
||||||
|
"data:text/javascript," +
|
||||||
|
encodeURIComponent(`
|
||||||
|
const SRC = ${JSON.stringify(pathToFileURL(srcRoot + "/").href)};
|
||||||
|
export async function resolve(specifier, context, next) {
|
||||||
|
if (specifier === "@/lib/api") return { url: ${JSON.stringify(apiStub)}, shortCircuit: true };
|
||||||
|
if (specifier === "@/router") return { url: ${JSON.stringify(routerStub)}, shortCircuit: true };
|
||||||
|
if (specifier.startsWith("@/")) return { url: SRC + specifier.slice(2) + ".ts", shortCircuit: true };
|
||||||
|
if ((specifier.startsWith("./") || specifier.startsWith("../")) && !/\\.[a-z0-9]+$/i.test(specifier)) {
|
||||||
|
return { url: new URL(specifier + ".ts", context.parentURL).href, shortCircuit: true };
|
||||||
|
}
|
||||||
|
return next(specifier, context);
|
||||||
|
}
|
||||||
|
`),
|
||||||
|
);
|
||||||
|
|
||||||
|
interface Frame {
|
||||||
|
type: string;
|
||||||
|
text?: string;
|
||||||
|
session_id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One SSE body: each frame as the server writes it, `data: {...}\n\n`. */
|
||||||
|
function sseBody(frames: Frame[]): ReadableStream<Uint8Array> {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const payload = frames.map((frame) => `data: ${JSON.stringify(frame)}\n\n`).join("");
|
||||||
|
return new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(encoder.encode(payload));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const started = { type: "started", session_id: "s1" };
|
||||||
|
|
||||||
|
// The stream stub: chatStream answers with the given frames, sessions reads
|
||||||
|
// back an empty list so the finished turn does not trigger a reload.
|
||||||
|
function stubApi(frames: Frame[]): void {
|
||||||
|
(globalThis as Record<string, unknown>).__archieChatTestApi = {
|
||||||
|
chatStream: () => ({ body: sseBody(frames) }),
|
||||||
|
chatSessions: () => ({ sessions: [], commands: [] }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = await import("../src/chat/state.ts");
|
||||||
|
|
||||||
|
test("reasoning frames accumulate on the live turn as its thinking", async () => {
|
||||||
|
stubApi([
|
||||||
|
started,
|
||||||
|
{ type: "reasoning", text: "considering " },
|
||||||
|
{ type: "reasoning", text: "the request" },
|
||||||
|
{ type: "error", text: "provider exploded" },
|
||||||
|
]);
|
||||||
|
await state.sendMessage({ textOverride: "hello" });
|
||||||
|
const turn = state.streamingTurn.value;
|
||||||
|
assert.ok(turn, "the failed turn stays on the panel");
|
||||||
|
assert.equal(turn.isError, true);
|
||||||
|
assert.equal(turn.reasoning, "considering the request");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("reasoning never leaks into the reply text", async () => {
|
||||||
|
stubApi([
|
||||||
|
started,
|
||||||
|
{ type: "reasoning", text: "secret chain of thought" },
|
||||||
|
{ type: "delta", text: "Hello" },
|
||||||
|
{ type: "done", text: "Hello", session_id: "s1" },
|
||||||
|
]);
|
||||||
|
await state.sendMessage({ textOverride: "hi" });
|
||||||
|
assert.equal(state.streamingTurn.value, null);
|
||||||
|
const last = state.messages.value.at(-1);
|
||||||
|
assert.equal(last?.from, "assistant");
|
||||||
|
assert.equal(last?.text, "Hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("a turn without reasoning frames completes normally", async () => {
|
||||||
|
stubApi([started, { type: "delta", text: "Hi" }, { type: "done", text: "Hi", session_id: "s1" }]);
|
||||||
|
await state.sendMessage({ textOverride: "hey" });
|
||||||
|
assert.equal(state.streamingTurn.value, null);
|
||||||
|
assert.equal(state.messages.value.at(-1)?.text, "Hi");
|
||||||
|
assert.equal(state.statusText.value, "Ready");
|
||||||
|
});
|
||||||
Reference in new issue
Block a user