forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatContext.tsx
More file actions
134 lines (116 loc) · 3.54 KB
/
Copy pathChatContext.tsx
File metadata and controls
134 lines (116 loc) · 3.54 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use client';
import {
createContext,
useContext,
useState,
useCallback,
useMemo,
ReactNode,
} from 'react';
import { useChat as useChatSession } from '@/hooks/useChat';
type SharedChat = ReturnType<typeof useChatSession>;
interface ChatContext {
// Panel state
isOpen: boolean;
openChat: () => void;
closeChat: () => void;
toggleChat: () => void;
// Context awareness
currentContext: ChatContextInfo | null;
setContext: (context: ChatContextInfo | null) => void;
/**
* The chat session being read, or `null` for the default shared thread.
* Owned here because both the panel and Home render the same transcript;
* a selection held by either one alone could not move the other.
*/
selectedChatSessionId: string | null;
selectChatSession: (sessionId: string | null) => void;
chat: SharedChat;
// App-specific chat (for notification routing)
selectedAppId: string | null;
selectApp: (appId: string | null) => void;
openChatWithApp: (appId: string) => void;
clearAppContext: () => void;
}
export interface ChatContextInfo {
type: 'conversation' | 'task' | 'memory' | 'recap' | 'general';
id?: string;
title?: string;
summary?: string;
/** ISO datetime with offset; hard-scopes chat retrieval (#4515). */
start_date?: string;
end_date?: string;
}
const ChatContext = createContext<ChatContext | null>(null);
export function ChatProvider({ children }: { children: ReactNode }) {
const [isOpen, setIsOpen] = useState(false);
const [currentContext, setCurrentContext] = useState<ChatContextInfo | null>(null);
const [selectedAppId, setSelectedAppId] = useState<string | null>(null);
const [selectedChatSessionId, setSelectedChatSessionId] = useState<string | null>(null);
const chat = useChatSession({
appId: selectedAppId ?? undefined,
chatSessionId: selectedChatSessionId,
});
const openChat = useCallback(() => setIsOpen(true), []);
const closeChat = useCallback(() => setIsOpen(false), []);
const toggleChat = useCallback(() => setIsOpen((prev) => !prev), []);
const setContext = useCallback((context: ChatContextInfo | null) => {
setCurrentContext(context);
}, []);
const selectChatSession = useCallback((sessionId: string | null) => {
setSelectedChatSessionId(sessionId);
}, []);
const selectApp = useCallback((appId: string | null) => {
setSelectedAppId(appId);
}, []);
// Open chat with a specific app context
const openChatWithApp = useCallback((appId: string) => {
setSelectedAppId(appId);
setIsOpen(true);
}, []);
// Clear app context and return to general Omi chat
const clearAppContext = useCallback(() => {
setSelectedAppId(null);
}, []);
// Memoize context value to prevent cascading re-renders of all consumers
const value = useMemo(
() => ({
isOpen,
openChat,
closeChat,
toggleChat,
currentContext,
setContext,
selectedChatSessionId,
selectChatSession,
chat,
selectedAppId,
selectApp,
openChatWithApp,
clearAppContext,
}),
[
isOpen,
openChat,
closeChat,
toggleChat,
currentContext,
setContext,
selectedChatSessionId,
selectChatSession,
chat,
selectedAppId,
selectApp,
openChatWithApp,
clearAppContext,
],
);
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
}
export function useChat() {
const context = useContext(ChatContext);
if (!context) {
throw new Error('useChat must be used within a ChatProvider');
}
return context;
}