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.

Hydrate Assistant loaded session tool events

rcourtman55590758642a
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index a626960061..3b6c1ab7f1 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -104,6 +104,10 @@ runtime cost control, and shared AI transport surfaces.
structured mentions, finding id, approval override, handoff resources,
handoff actions, and handoff metadata, but must not reconstruct scoped
context from prompt history or saved transcript prose.
+ Restored Assistant sessions must hydrate saved assistant content and
+ persisted tool calls into the same transcript event shape used by live
+ streams so switching sessions does not hide prior tool evidence or collapse
+ the resumed conversation into a text-only transcript.
Assistant output hygiene is part of the same boundary: provider reasoning
and raw serialized tool-call artifacts must never render as assistant
transcript prose. Reasoning/thinking deltas may update neutral progress
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 29dc641239..7559dcb2da 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
@@ -1431,10 +1431,57 @@ describe('useChat', () => {
expect(msgs[0].role).toBe('user');
expect(msgs[0].content).toBe('hello');
expect(msgs[1].toolCalls).toHaveLength(1);
+ expect(msgs[1].streamEvents).toEqual([
+ { type: 'content', content: 'hi there' },
+ {
+ type: 'tool',
+ tool: { name: 'test', input: '{}', output: 'ok', success: true },
+ },
+ ]);
expect(msgs[1].timestamp).toBeInstanceOf(Date);
dispose();
});
+ it('hydrates saved assistant tool calls into visible transcript events', async () => {
+ mockGetMessages.mockResolvedValue([
+ {
+ id: 'msg-1',
+ role: 'assistant',
+ content: 'I checked the resource.',
+ timestamp: '2024-01-01T00:00:01Z',
+ model: 'openai:gpt-4',
+ tool_calls: [
+ {
+ name: 'pulse_read',
+ input: '{"target":"vm-100"}',
+ output: 'Resource is running',
+ success: true,
+ },
+ ],
+ },
+ ]);
+
+ const { value: chat, dispose } = withRoot(() => useChat());
+ await chat.loadSession('sess-42');
+
+ const assistant = chat.messages()[0];
+ expect(assistant.model).toBe('openai:gpt-4');
+ expect(assistant.content).toBe('I checked the resource.');
+ expect(assistant.streamEvents).toEqual([
+ { type: 'content', content: 'I checked the resource.' },
+ {
+ type: 'tool',
+ tool: {
+ name: 'pulse_read',
+ input: '{"target":"vm-100"}',
+ output: 'Resource is running',
+ success: true,
+ },
+ },
+ ]);
+ dispose();
+ });
+
it('clears queued follow-ups before loading another session', async () => {
mockChat.mockImplementation(() => new Promise(() => {}));
mockGetMessages.mockResolvedValue([]);
diff --git a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
index 3f8c437b6c..8076bacfe2 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -332,6 +332,28 @@ export function useChat(options: UseChatOptions = {}) {
return Object.keys(sendOptions).length > 0 ? sendOptions : undefined;
};
+ const buildPersistedStreamEvents = (
+ content: string,
+ toolCalls?: ToolExecution[],
+ ): StreamDisplayEvent[] | undefined => {
+ const events: StreamDisplayEvent[] = [];
+ if (content.trim()) {
+ events.push({ type: 'content', content });
+ }
+ for (const tool of toolCalls || []) {
+ events.push({
+ type: 'tool',
+ tool: {
+ name: tool.name || 'unknown',
+ input: tool.input || '{}',
+ output: tool.output || '',
+ success: tool.success ?? true,
+ },
+ });
+ }
+ return events.length > 0 ? events : undefined;
+ };
+
const applyStreamSessionId = (streamSessionId: string) => {
if (!streamSessionId) return;
const previousSessionId = sessionId();
@@ -898,13 +920,20 @@ export function useChat(options: UseChatOptions = {}) {
try {
const msgs = await AIChatAPI.getMessages(id);
setMessages(
- msgs.map((m) => ({
- id: m.id,
- role: m.role,
- content: m.content,
- timestamp: new Date(m.timestamp),
- toolCalls: m.tool_calls,
- })),
+ msgs.map((m) => {
+ const toolCalls = m.tool_calls || [];
+ const streamEvents =
+ m.role === 'assistant' ? buildPersistedStreamEvents(m.content, toolCalls) : undefined;
+ return {
+ id: m.id,
+ role: m.role,
+ content: m.content,
+ timestamp: new Date(m.timestamp),
+ model: m.model,
+ toolCalls,
+ streamEvents,
+ };
+ }),
);
setSessionId(id);
return true;