Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Replace provisional Assistant model route rows

rcourtman2a59f44dc079
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 4f314b25df..38131260ea 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -216,6 +216,11 @@ runtime cost control, and shared AI transport surfaces.
known at turn creation, Pulse must seed a selected-model stream event before
backend/provider activity arrives so the transcript and active-turn footer
show concrete route motion instead of a generic request-start wait.
+ If backend/provider startup immediately confirms a different selected route
+ before any durable assistant content, thinking, tool, approval, or question
+ evidence arrives, that confirmed route replaces the provisional local
+ selected-model row rather than stacking multiple "Using ..." rows. Once a
+ durable boundary exists, later route events are preserved chronologically.
Placeholder `request_start` workflow statuses remain fallback activity only:
once selected-model, provider, tool, thinking, approval, question, or content
activity is visible, the placeholder must not outrank it in the footer or
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
index 4940e07afc..71e460fec5 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
@@ -1515,6 +1515,108 @@ describe('useChat', () => {
dispose();
});
+ it('replaces a provisional selected-model row when provider startup confirms a different route', async () => {
+ const { getFireEvent } = setupWithEventCapture();
+ const { value: chat, dispose } = withRoot(() =>
+ useChat({
+ sessionId: 's',
+ defaultModel: () => 'openrouter:qwen/qwen3.7-plus',
+ }),
+ );
+
+ await chat.sendMessage('hi');
+ let assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.streamEvents).toEqual([
+ expect.objectContaining({
+ type: 'model_switch',
+ model: 'openrouter:qwen/qwen3.7-plus',
+ modelEvent: 'selected',
+ }),
+ ]);
+
+ const fire = getFireEvent();
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'prepare',
+ message: 'Preparing Pulse context.',
+ },
+ });
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'provider_start',
+ message: 'Sent request to DeepSeek; waiting for the first token.',
+ provider: 'deepseek',
+ model: 'deepseek:deepseek-chat',
+ },
+ });
+
+ assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.model).toBe('deepseek:deepseek-chat');
+ expect(assistant.streamEvents?.map((event) => event.type)).toEqual([
+ 'model_switch',
+ 'workflow_status',
+ ]);
+ expect(assistant.streamEvents?.filter((event) => event.type === 'model_switch')).toEqual([
+ expect.objectContaining({
+ type: 'model_switch',
+ model: 'deepseek:deepseek-chat',
+ modelEvent: 'selected',
+ }),
+ ]);
+ expect(
+ assistant.streamEvents?.some((event) => event.model === 'openrouter:qwen/qwen3.7-plus'),
+ ).toBe(false);
+
+ dispose();
+ });
+
+ it('keeps prior selected-model evidence after durable assistant content starts', async () => {
+ const { getFireEvent } = setupWithEventCapture();
+ const { value: chat, dispose } = withRoot(() =>
+ useChat({
+ sessionId: 's',
+ defaultModel: () => 'openrouter:qwen/qwen3.7-plus',
+ }),
+ );
+
+ await chat.sendMessage('hi');
+ const fire = getFireEvent();
+ fire({ type: 'content', data: 'Partial answer.' });
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'provider_start',
+ message: 'Sent request to DeepSeek; waiting for the first token.',
+ provider: 'deepseek',
+ model: 'deepseek:deepseek-chat',
+ },
+ });
+
+ const assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.streamEvents?.map((event) => event.type)).toEqual([
+ 'model_switch',
+ 'content',
+ 'model_switch',
+ 'workflow_status',
+ ]);
+ expect(assistant.streamEvents?.filter((event) => event.type === 'model_switch')).toEqual([
+ expect.objectContaining({
+ type: 'model_switch',
+ model: 'openrouter:qwen/qwen3.7-plus',
+ modelEvent: 'selected',
+ }),
+ expect.objectContaining({
+ type: 'model_switch',
+ model: 'deepseek:deepseek-chat',
+ modelEvent: 'selected',
+ }),
+ ]);
+
+ dispose();
+ });
+
it('keeps the local request-start status when an early idle heartbeat arrives', async () => {
const { getFireEvent } = setupWithEventCapture();
const { value: chat, dispose } = withRoot(() => useChat({ sessionId: 's' }));
diff --git a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
index 3f44e536b0..795dd24b87 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -428,6 +428,24 @@ export function useChat(options: UseChatOptions = {}) {
return model && failed && model !== failed ? 'fallback' : 'switch';
};
+ const isDurableAssistantStreamBoundary = (event: StreamDisplayEvent): boolean =>
+ event.type !== 'workflow_status' && event.type !== 'model_switch';
+
+ const replaceableSelectedModelEventIndex = (events: StreamDisplayEvent[]): number => {
+ for (let index = events.length - 1; index >= 0; index -= 1) {
+ const event = events[index];
+ if (isDurableAssistantStreamBoundary(event)) return -1;
+ if (
+ event.type === 'model_switch' &&
+ streamModelEventKind(event) === 'selected' &&
+ !event.failedModel?.trim()
+ ) {
+ return index;
+ }
+ }
+ return -1;
+ };
+
const withModelRouteEvent = (
msg: ChatMessage,
route: string,
@@ -446,6 +464,26 @@ export function useChat(options: UseChatOptions = {}) {
if (duplicate) {
return { ...msg, model };
}
+ const events = msg.streamEvents || [];
+ if (options.modelEvent === 'selected' && !failedModel) {
+ const replaceableIndex = replaceableSelectedModelEventIndex(events);
+ if (replaceableIndex >= 0) {
+ const replacement = withStreamEventTiming({
+ type: 'model_switch',
+ model,
+ modelEvent: 'selected',
+ });
+ return {
+ ...msg,
+ model,
+ streamEvents: [
+ ...events.slice(0, replaceableIndex),
+ replacement,
+ ...events.slice(replaceableIndex + 1),
+ ],
+ };
+ }
+ }
const updated = addStreamEvent(msg, {
type: 'model_switch',
model,