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.

Keep Assistant status stable during idle heartbeats

rcourtmancd6ee31981e2
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 b1ed3eb725..e367d28d21 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
@@ -1344,6 +1344,84 @@ describe('useChat', () => {
dispose();
});
+ it('keeps the provider wait visible when an idle heartbeat arrives', async () => {
+ const { getFireEvent } = setupWithEventCapture();
+ const { value: chat, dispose } = withRoot(() => useChat({ sessionId: 's' }));
+
+ await chat.sendMessage('hi');
+ const fire = getFireEvent();
+
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'provider_start',
+ message: 'Sent request to OpenRouter; waiting for the first token.',
+ },
+ });
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'stream_idle',
+ message: 'Assistant is still working; waiting for the next stream event.',
+ },
+ });
+
+ const assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.workflowStatus).toEqual(
+ expect.objectContaining({
+ phase: 'provider_start',
+ message: 'Sent request to OpenRouter; waiting for the first token.',
+ }),
+ );
+ expect(assistant.streamEvents).toHaveLength(1);
+ expect(assistant.streamEvents?.[0]).toEqual(
+ expect.objectContaining({
+ type: 'workflow_status',
+ workflowStatus: expect.objectContaining({
+ phase: 'provider_start',
+ message: 'Sent request to OpenRouter; waiting for the first token.',
+ }),
+ }),
+ );
+
+ 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' }));
+
+ await chat.sendMessage('hi');
+ const fire = getFireEvent();
+
+ fire({
+ type: 'workflow_state',
+ data: {
+ phase: 'stream_idle',
+ message: 'Assistant is still working; waiting for the next stream event.',
+ },
+ });
+
+ const assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.workflowStatus).toEqual(
+ expect.objectContaining({
+ phase: 'request_start',
+ message: 'Preparing Pulse context.',
+ }),
+ );
+ expect(assistant.streamEvents).toEqual([
+ expect.objectContaining({
+ type: 'workflow_status',
+ workflowStatus: expect.objectContaining({
+ phase: 'request_start',
+ message: 'Preparing Pulse context.',
+ }),
+ }),
+ ]);
+
+ dispose();
+ });
+
it('clears neutral workflow progress when answer content starts streaming', 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 6e4b4c3ddb..bf5d32ce6f 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -136,6 +136,23 @@ export function useChat(options: UseChatOptions = {}) {
(a.maxAttempts || 0) === (b.maxAttempts || 0) &&
(a.retryAfterMs || 0) === (b.retryAfterMs || 0);
+ const isStreamIdleWorkflowStatus = (status?: WorkflowStatus) =>
+ status?.phase === 'stream_idle';
+
+ // stream_idle is transport liveness, not a new Assistant step.
+ const visibleWorkflowStatusForHeartbeat = (
+ current: WorkflowStatus | undefined,
+ next: WorkflowStatus,
+ ): WorkflowStatus => {
+ if (!isStreamIdleWorkflowStatus(next)) return next;
+ if (!current || isStreamIdleWorkflowStatus(current)) return next;
+
+ return {
+ ...current,
+ startedAt: current.startedAt || next.startedAt,
+ };
+ };
+
const setAssistantWorkflowStatus = (
assistantId: string,
requestId: number,
@@ -146,8 +163,9 @@ export function useChat(options: UseChatOptions = {}) {
prev.map((msg) => {
if (msg.id !== assistantId) return msg;
if (msg.isStreaming === false) return msg;
- if (workflowStatusesMatch(msg.workflowStatus, workflowStatus)) return msg;
- return { ...msg, workflowStatus };
+ const visibleStatus = visibleWorkflowStatusForHeartbeat(msg.workflowStatus, workflowStatus);
+ if (workflowStatusesMatch(msg.workflowStatus, visibleStatus)) return msg;
+ return { ...msg, workflowStatus: visibleStatus };
}),
);
};
@@ -339,11 +357,12 @@ export function useChat(options: UseChatOptions = {}) {
): ChatMessage => {
const events = msg.streamEvents || [];
const now = Date.now();
+ const visibleStatus = visibleWorkflowStatusForHeartbeat(msg.workflowStatus, workflowStatus);
const nextEvent = withStreamEventTiming(
{
type: 'workflow_status',
- workflowStatus,
- startedAt: workflowStatus.startedAt,
+ workflowStatus: visibleStatus,
+ startedAt: visibleStatus.startedAt,
updatedAt: workflowStatus.startedAt,
},
now,
@@ -351,7 +370,19 @@ export function useChat(options: UseChatOptions = {}) {
const last = events[events.length - 1];
if (last?.type === 'workflow_status') {
- if (workflowStatusesMatch(last.workflowStatus, workflowStatus)) {
+ if (workflowStatusesMatch(last.workflowStatus, visibleStatus)) {
+ if (isStreamIdleWorkflowStatus(workflowStatus)) {
+ return {
+ ...msg,
+ streamEvents: [
+ ...events.slice(0, -1),
+ {
+ ...last,
+ updatedAt: nextEvent.updatedAt || now,
+ },
+ ],
+ };
+ }
return msg;
}
return {