File size: 8,571 Bytes
808fe45
3f73990
 
 
 
 
 
 
 
 
 
808fe45
 
 
 
 
 
3f73990
 
 
 
808fe45
 
3f73990
 
808fe45
 
3f73990
 
808fe45
 
3f73990
808fe45
 
3f73990
 
 
 
 
 
 
 
 
 
 
 
808fe45
 
 
 
 
 
 
 
 
 
3f73990
 
 
 
808fe45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f73990
 
808fe45
 
3f73990
 
 
 
808fe45
 
 
3f73990
 
808fe45
 
 
3f73990
808fe45
 
 
3f73990
 
 
 
 
 
 
808fe45
 
 
 
 
 
 
3f73990
808fe45
 
 
 
 
 
3f73990
808fe45
 
 
 
 
3f73990
808fe45
 
 
 
 
 
 
 
 
 
3f73990
 
 
808fe45
 
3f73990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808fe45
 
 
 
 
 
3f73990
 
 
 
808fe45
 
 
 
 
 
 
 
 
 
 
 
 
3f73990
808fe45
 
 
3f73990
808fe45
 
 
 
 
3f73990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808fe45
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { useRef, useEffect, useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import { 
  Menu, 
  PanelLeftClose, 
  Cpu, 
  FileText, 
  Settings,
  ChevronLeft,
  ChevronRight 
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import { ChatMessage } from "@/components/ChatMessage";
import { ChatInput } from "@/components/ChatInput";
import { WelcomeScreen } from "@/components/WelcomeScreen";
import { ConversationSidebar } from "@/components/ConversationSidebar";
import { ModelStore } from "@/components/ModelStore";
import { PDFViewer } from "@/components/PDFViewer";
import { SettingsPanel } from "@/components/SettingsPanel";
import { ProviderBadge } from "@/components/ProviderBadge";
import { useChat } from "@/hooks/useChat";
import { useVoice } from "@/hooks/useVoice";
import { usePDF } from "@/hooks/usePDF";
import { useSettings } from "@/hooks/useSettings";
import { cn } from "@/lib/utils";

type RightPanelView = "models" | "documents" | "settings" | null;

export function ChatInterface() {
  const [sidebarOpen, setSidebarOpen] = useState(true);
  const [rightPanel, setRightPanel] = useState<RightPanelView>(null);
  const scrollRef = useRef<HTMLDivElement>(null);

  const { settings, updateSettings } = useSettings();
  
  const {
    documents,
    activeDocument,
    isLoading: pdfLoading,
    uploadPDF,
    removeDocument,
    setActiveDocument,
    getContextForRAG,
  } = usePDF();

  const {
    conversations,
    activeConversation,
    activeConversationId,
    messages,
    isLoading,
    createConversation,
    setActiveConversationId,
    deleteConversation,
    sendMessage,
  } = useChat({ 
    settings,
    pdfContext: activeDocument ? getContextForRAG("") : undefined,
  });

  const {
    isListening,
    transcript,
    startListening,
    stopListening,
  } = useVoice();

  // Auto-scroll to bottom when new messages arrive
  useEffect(() => {
    if (scrollRef.current) {
      scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
    }
  }, [messages]);

  const handleSendMessage = async (content: string) => {
    const context = activeDocument ? getContextForRAG(content) : undefined;
    sendMessage(content);
  };

  const handleFileUpload = async (file: File) => {
    if (file.type === "application/pdf") {
      await uploadPDF(file);
      setRightPanel("documents");
    }
  };

  const toggleRightPanel = (view: RightPanelView) => {
    setRightPanel((prev) => (prev === view ? null : view));
  };

  return (
    <div className="flex h-screen bg-background overflow-hidden">
      {/* Sidebar */}
      <AnimatePresence mode="wait">
        {sidebarOpen && (
          <motion.div 
            initial={{ width: 0, opacity: 0 }}
            animate={{ width: 280, opacity: 1 }}
            exit={{ width: 0, opacity: 0 }}
            transition={{ duration: 0.2 }}
            className="border-r border-border shrink-0 overflow-hidden"
          >
            <ConversationSidebar
              conversations={conversations}
              activeConversationId={activeConversationId}
              onSelectConversation={setActiveConversationId}
              onNewConversation={createConversation}
              onDeleteConversation={deleteConversation}
            />
          </motion.div>
        )}
      </AnimatePresence>

      {/* Main Chat Area */}
      <div className="flex-1 flex flex-col min-w-0">
        {/* Header */}
        <header className="h-14 px-4 flex items-center justify-between border-b border-border glass-subtle">
          <div className="flex items-center gap-3">
            <Button
              variant="ghost"
              size="icon"
              onClick={() => setSidebarOpen(!sidebarOpen)}
              className="text-muted-foreground hover:text-foreground rounded-xl"
            >
              {sidebarOpen ? (
                <PanelLeftClose className="h-5 w-5" />
              ) : (
                <Menu className="h-5 w-5" />
              )}
            </Button>
            <h1 className="font-semibold text-foreground">
              {activeConversation?.title || "AI Assistant"}
            </h1>
            {activeConversation && (
              <ProviderBadge provider={settings.provider} />
            )}
          </div>

          {/* Right Panel Toggles */}
          <div className="flex items-center gap-1">
            <Button
              variant="ghost"
              size="icon"
              onClick={() => toggleRightPanel("models")}
              className={cn(
                "rounded-xl transition-colors",
                rightPanel === "models" 
                  ? "bg-primary/20 text-primary" 
                  : "text-muted-foreground hover:text-foreground"
              )}
              title="Model Store"
            >
              <Cpu className="h-5 w-5" />
            </Button>
            <Button
              variant="ghost"
              size="icon"
              onClick={() => toggleRightPanel("documents")}
              className={cn(
                "rounded-xl transition-colors relative",
                rightPanel === "documents" 
                  ? "bg-primary/20 text-primary" 
                  : "text-muted-foreground hover:text-foreground"
              )}
              title="Documents"
            >
              <FileText className="h-5 w-5" />
              {documents.length > 0 && (
                <span className="absolute -top-1 -right-1 w-4 h-4 rounded-full bg-primary text-[10px] flex items-center justify-center text-primary-foreground">
                  {documents.length}
                </span>
              )}
            </Button>
            <Button
              variant="ghost"
              size="icon"
              onClick={() => toggleRightPanel("settings")}
              className={cn(
                "rounded-xl transition-colors",
                rightPanel === "settings" 
                  ? "bg-primary/20 text-primary" 
                  : "text-muted-foreground hover:text-foreground"
              )}
              title="Settings"
            >
              <Settings className="h-5 w-5" />
            </Button>
          </div>
        </header>

        {/* Messages Area */}
        <ScrollArea className="flex-1" ref={scrollRef}>
          <div className="max-w-3xl mx-auto px-4">
            {messages.length === 0 ? (
              <WelcomeScreen 
                onSuggestionClick={handleSendMessage} 
                hasDocuments={documents.length > 0}
              />
            ) : (
              <div className="py-4">
                {messages.map((message) => (
                  <ChatMessage key={message.id} message={message} />
                ))}
              </div>
            )}
          </div>
        </ScrollArea>

        {/* Input Area */}
        <ChatInput
          onSendMessage={handleSendMessage}
          onFileUpload={handleFileUpload}
          isLoading={isLoading}
          isListening={isListening}
          transcript={transcript}
          provider={settings.provider}
          onStartListening={startListening}
          onStopListening={stopListening}
        />
      </div>

      {/* Right Panel */}
      <AnimatePresence mode="wait">
        {rightPanel && (
          <motion.div
            initial={{ width: 0, opacity: 0 }}
            animate={{ width: 320, opacity: 1 }}
            exit={{ width: 0, opacity: 0 }}
            transition={{ duration: 0.2 }}
            className="border-l border-border shrink-0 overflow-hidden glass-sidebar"
          >
            {rightPanel === "models" && (
              <ModelStore
                ollamaUrl={settings.ollamaUrl}
                selectedModel={settings.ollamaModel}
                onModelSelect={(model) => updateSettings({ ollamaModel: model, provider: "ollama" })}
              />
            )}
            {rightPanel === "documents" && (
              <PDFViewer
                documents={documents}
                activeDocument={activeDocument}
                isLoading={pdfLoading}
                onUpload={uploadPDF}
                onRemove={removeDocument}
                onSelect={setActiveDocument}
              />
            )}
            {rightPanel === "settings" && (
              <SettingsPanel
                settings={settings}
                onUpdate={updateSettings}
              />
            )}
          </motion.div>
        )}
      </AnimatePresence>
    </div>
  );
}