forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseStreamSubscription.ts
More file actions
110 lines (103 loc) · 3.94 KB
/
useStreamSubscription.ts
File metadata and controls
110 lines (103 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useEffect } from 'react';
import type { Message, SessionStreamSnapshot } from '@/types';
import {
subscribe,
getSnapshot,
clearSnapshot,
} from '@/lib/stream-session-manager';
import { transferPendingToMessage } from '@/lib/image-ref-store';
interface UseStreamSubscriptionOpts {
sessionId: string;
setStreamSnapshot: React.Dispatch<React.SetStateAction<SessionStreamSnapshot | null>>;
setStreamingSessionId: (id: string) => void;
setPendingApprovalSessionId: (id: string) => void;
setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
}
export function useStreamSubscription({
sessionId,
setStreamSnapshot,
setStreamingSessionId,
setPendingApprovalSessionId,
setMessages,
}: UseStreamSubscriptionOpts): void {
useEffect(() => {
// Restore snapshot if stream is already active (e.g., user switched away and back)
const existing = getSnapshot(sessionId);
if (existing) {
setStreamSnapshot(existing);
if (existing.phase === 'active') {
setStreamingSessionId(sessionId);
}
if (existing.pendingPermission && !existing.permissionResolved) {
setPendingApprovalSessionId(sessionId);
}
// If stream completed while this ChatView was unmounted, consume finalMessageContent now.
// Re-fetch messages from DB to avoid duplicates (backend already persisted the reply).
if (existing.phase !== 'active' && existing.finalMessageContent) {
fetch(`/api/chat/sessions/${sessionId}/messages?limit=50`)
.then(res => res.ok ? res.json() : null)
.then(data => {
if (data?.messages) {
setMessages(data.messages);
}
})
.catch(() => {
// Fallback: append locally if DB fetch fails
const assistantMessage: Message = {
id: 'temp-assistant-' + Date.now(),
session_id: sessionId,
role: 'assistant',
content: existing.finalMessageContent!,
created_at: new Date().toISOString(),
token_usage: existing.tokenUsage ? JSON.stringify(existing.tokenUsage) : null,
};
transferPendingToMessage(assistantMessage.id);
setMessages((prev) => [...prev, assistantMessage]);
});
clearSnapshot(sessionId);
}
} else {
setStreamSnapshot(null);
}
const unsubscribe = subscribe(sessionId, (event) => {
setStreamSnapshot(event.snapshot);
// Sync panel state
if (event.type === 'phase-changed') {
if (event.snapshot.phase === 'active') {
setStreamingSessionId(sessionId);
} else {
setStreamingSessionId('');
setPendingApprovalSessionId('');
}
}
if (event.type === 'permission-request') {
setPendingApprovalSessionId(sessionId);
}
if (event.type === 'completed') {
setStreamingSessionId('');
setPendingApprovalSessionId('');
// Append the final assistant message to the messages list
const finalContent = event.snapshot.finalMessageContent;
if (finalContent) {
const assistantMessage: Message = {
id: 'temp-assistant-' + Date.now(),
session_id: sessionId,
role: 'assistant',
content: finalContent,
created_at: new Date().toISOString(),
token_usage: event.snapshot.tokenUsage ? JSON.stringify(event.snapshot.tokenUsage) : null,
};
// Transfer pending reference images to this message ID
transferPendingToMessage(assistantMessage.id);
setMessages((prev) => [...prev, assistantMessage]);
}
// Clear the snapshot from the manager since we've consumed it
clearSnapshot(sessionId);
}
});
return () => {
unsubscribe();
// Do NOT abort — stream continues in the manager
};
}, [sessionId, setStreamingSessionId, setPendingApprovalSessionId, setStreamSnapshot, setMessages]);
}