File size: 14,509 Bytes
1425cf0
 
 
 
 
62309aa
1425cf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e507e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425cf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e507e4c
1425cf0
 
 
 
 
 
e507e4c
 
 
 
1425cf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62309aa
1425cf0
 
 
62309aa
1425cf0
e507e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425cf0
 
62309aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425cf0
62309aa
 
 
1425cf0
 
 
 
 
 
 
62309aa
 
 
 
 
 
1425cf0
62309aa
 
 
 
 
1425cf0
62309aa
 
 
 
1425cf0
62309aa
 
 
 
 
 
 
 
1425cf0
 
 
62309aa
 
1425cf0
 
 
 
 
 
 
62309aa
1425cf0
 
 
 
 
62309aa
 
 
1425cf0
 
62309aa
1425cf0
 
 
 
62309aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425cf0
62309aa
1425cf0
62309aa
 
1425cf0
62309aa
 
 
 
 
 
 
 
1425cf0
 
 
 
 
 
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'
import { Slider } from '@/components/ui/slider'
import { Label } from '@/components/ui/label'
import { User, Upload, File, X, BookOpen } from 'lucide-react'

interface UploadedFile {
  id: string
  name: string
  size: number
  type: string
  uploadedAt: string
  status: string
  chunks?: number
  assistantId?: string | null
}

interface SelectedAssistant {
  id: string
  name: string
  type: 'user'|'template'|'new'
  originalTemplate?: string
}

interface DocumentsTabProps {
  isLoading: boolean
  ragEnabled: boolean
  setRagEnabled: (enabled: boolean) => void
  retrievalCount: number
  setRetrievalCount: (count: number) => void
  currentAssistant: SelectedAssistant | null
}

export function DocumentsTab({
  isLoading,
  ragEnabled,
  setRagEnabled,
  retrievalCount,
  setRetrievalCount,
  currentAssistant
}: DocumentsTabProps) {
  const [uploadedFiles, setUploadedFiles] = useState<UploadedFile[]>([])
  const [isUploading, setIsUploading] = useState(false)

  // Load assistant-specific documents when assistant changes
  useEffect(() => {
    const loadDocuments = async () => {
      if (!currentAssistant) {
        // Load all documents when no assistant is selected
        try {
          const response = await fetch('/rag/documents')
          if (response.ok) {
            const data = await response.json()
            const allFiles: UploadedFile[] = data.documents.map((doc: any) => ({
              id: doc.id,
              name: doc.filename,
              size: doc.size || 0,
              type: doc.content_type || 'application/octet-stream',
              uploadedAt: doc.upload_date,
              status: 'processed',
              chunks: doc.chunk_count,
              assistantId: doc.assistant_id
            }))
            setUploadedFiles(allFiles)
          }
        } catch (error) {
          console.error('Error loading documents:', error)
        }
        return
      }

      try {
        // For now, load all documents but filter by assistant in the future
        const response = await fetch('/rag/documents')
        if (response.ok) {
          const data = await response.json()
          if (data.documents) {
            const documentList = Object.entries(data.documents).map(([docId, docInfo]: [string, any]) => ({
              id: docId,
              name: docInfo.filename,
              size: 0,
              type: docInfo.file_type,
              uploadedAt: new Date().toISOString(),
              status: docInfo.status,
              chunks: docInfo.chunks,
              assistantId: docInfo.assistant_id || null // Future: filter by assistant
            })) as UploadedFile[]
            
            // TODO: Filter documents by currentAssistant.id when backend supports it
            setUploadedFiles(documentList)
          }
        }
      } catch (error) {
        console.error('Error loading documents:', error)
      }
    }

    loadDocuments()
  }, [currentAssistant])

  const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
    const files = event.target.files
    if (!files) return

    setIsUploading(true)
    
    try {
      const formData = new FormData()
      
      // Add assistant ID to the upload (if available)
      if (currentAssistant) {
        formData.append('assistant_id', currentAssistant.id)
      }
      
      for (const file of Array.from(files)) {
        formData.append('files', file)
      }

      const response = await fetch('/rag/upload', {
        method: 'POST',
        body: formData,
      })

      if (response.ok) {
        const result = await response.json()
        
        // Add successfully processed files to the list
        const newFiles = result.results
          .filter((r: any) => r.success)
          .map((r: any) => ({
            id: r.doc_id,
            name: r.filename,
            size: 0, // Server doesn't return size currently
            type: 'processed',
            uploadedAt: new Date().toISOString(),
            status: 'processed',
            chunks: r.chunks,
            assistantId: currentAssistant?.id
          })) as UploadedFile[]
        
        setUploadedFiles((prev: UploadedFile[]) => [...prev, ...newFiles])
        
        // Show errors for failed uploads
        const failedUploads = result.results.filter((r: any) => !r.success)
        if (failedUploads.length > 0) {
          console.error('Some files failed to upload:', failedUploads)
        }
      } else {
        console.error('Upload failed:', response.statusText)
      }
      
      // Reset input
      event.target.value = ''
    } catch (error) {
      console.error('File upload error:', error)
    } finally {
      setIsUploading(false)
    }
  }

  const removeFile = async (fileId: string) => {
    try {
      const response = await fetch(`/rag/documents/${fileId}`, {
        method: 'DELETE',
      })
      
      if (response.ok) {
        setUploadedFiles((prev: UploadedFile[]) => prev.filter((f: UploadedFile) => f.id !== fileId))
      } else {
        console.error('Failed to delete document:', response.statusText)
      }
    } catch (error) {
      console.error('Error deleting document:', error)
      // Remove from UI anyway
      setUploadedFiles((prev: UploadedFile[]) => prev.filter((f: UploadedFile) => f.id !== fileId))
    }
  }



  return (
    <div className="space-y-4 pb-6">
      {!currentAssistant ? (
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <BookOpen className="h-4 w-4 text-blue-600" />
              <span>Documents & Knowledge Base</span>
            </CardTitle>
          </CardHeader>
          <CardContent className="space-y-4">
            {/* Upload Area */}
            <div className="border-2 border-dashed border-gray-200 rounded-lg p-6 text-center hover:border-gray-300 transition-colors">
              <input
                type="file"
                id="file-upload"
                multiple
                accept=".pdf,.txt,.docx,.md"
                onChange={handleFileUpload}
                className="hidden"
                disabled={isUploading || isLoading}
              />
              <Label
                htmlFor="file-upload"
                className="cursor-pointer flex flex-col items-center space-y-2"
              >
                <Upload className="h-8 w-8 text-gray-400" />
                <span className="text-sm font-medium">
                  {isUploading ? 'Uploading...' : 'Drag or drop a file'}
                </span>
                <span className="text-xs text-muted-foreground">
                  PDF, TXT, DOCX, MD supported
                </span>
              </Label>
            </div>

            {/* Documents List */}
            {uploadedFiles.length > 0 && (
              <div className="space-y-2">
                <Label className="text-sm font-medium">Documents ({uploadedFiles.length})</Label>
                
                <div className="space-y-1">
                  {uploadedFiles.map((file) => (
                    <div
                      key={file.id}
                      className="flex items-center justify-between p-2 border rounded bg-gray-50/50 hover:bg-gray-50"
                    >
                      <div className="flex items-center space-x-2 flex-1 min-w-0">
                        <File className="h-3 w-3 text-blue-600 flex-shrink-0" />
                        <span className="text-sm truncate">{file.name}</span>
                        {file.chunks && (
                          <span className="text-xs text-muted-foreground flex-shrink-0">
                            {file.chunks} chunks
                          </span>
                        )}
                      </div>
                      <Button
                        size="sm"
                        variant="ghost"
                        onClick={() => removeFile(file.id)}
                        disabled={isLoading}
                        className="h-6 w-6 p-0 text-red-500 hover:text-red-600 hover:bg-red-50"
                      >
                        <X className="h-3 w-3" />
                      </Button>
                    </div>
                  ))}
                </div>
              </div>
            )}

            {/* Empty State */}
            {uploadedFiles.length === 0 && (
              <div className="text-center py-3">
                <p className="text-xs text-muted-foreground">
                  No documents uploaded yet
                </p>
              </div>
            )}
          </CardContent>
        </Card>
      ) : (
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <User className="h-4 w-4 text-blue-600" />
                <span>{currentAssistant.name}</span>
                <span className={`px-2 py-1 rounded text-xs font-medium ${
                  currentAssistant.type === 'user' ? 'bg-blue-100 text-blue-700' :
                  currentAssistant.type === 'template' ? 'bg-gray-100 text-gray-700' :
                  'bg-green-100 text-green-700'
                }`}>
                  {currentAssistant.type === 'user' ? 'My Assistant' :
                   currentAssistant.type === 'template' ? 'Template' : 'New Assistant'}
                </span>
              </div>
              
              <div className="flex items-center gap-2">
                <Label className="text-xs text-muted-foreground">RAG</Label>
                <input
                  type="checkbox"
                  checked={ragEnabled}
                  onChange={(e) => setRagEnabled(e.target.checked)}
                  className="rounded"
                  disabled={isLoading || uploadedFiles.length === 0}
                />
                {ragEnabled && uploadedFiles.length > 0 && (
                  <div className="flex items-center gap-1 text-green-600">
                    <div className="w-2 h-2 bg-green-500 rounded-full"></div>
                    <span className="text-xs font-medium">Active</span>
                  </div>
                )}
              </div>
            </CardTitle>
          </CardHeader>
          
          <CardContent className="space-y-4">
            {/* RAG Settings - Compact */}
            {ragEnabled && (
              <div className="border-l-2 border-blue-200 pl-3 space-y-2">
                <div className="flex items-center justify-between">
                  <Label className="text-xs font-medium">Retrieval Depth</Label>
                  <span className="text-xs text-muted-foreground">{retrievalCount} chunks</span>
                </div>
                <Slider
                  value={[retrievalCount]}
                  onValueChange={(value) => setRetrievalCount(value[0])}
                  max={10}
                  min={1}
                  step={1}
                  className="w-full"
                />
              </div>
            )}

            {/* Upload Area */}
            <div className="border-2 border-dashed border-gray-200 rounded-lg p-4 text-center hover:border-gray-300 transition-colors">
              <input
                type="file"
                id="file-upload"
                multiple
                accept=".pdf,.txt,.docx,.md"
                onChange={handleFileUpload}
                className="hidden"
                disabled={isUploading || isLoading}
              />
              <Label
                htmlFor="file-upload"
                className="cursor-pointer flex flex-col items-center space-y-2"
              >
                <Upload className="h-6 w-6 text-gray-400" />
                <span className="text-sm">
                  {isUploading ? 'Uploading...' : 'Drop files or click to upload'}
                </span>
                <span className="text-xs text-muted-foreground">
                  PDF, TXT, DOCX, MD supported
                </span>
              </Label>
            </div>

            {/* Documents List - Compact */}
            {uploadedFiles.length > 0 && (
              <div className="space-y-2">
                <div className="flex items-center justify-between">
                  <Label className="text-sm font-medium">Documents ({uploadedFiles.length})</Label>
                  {ragEnabled && (
                    <span className="text-xs text-green-600">
                      Using {Math.min(retrievalCount, uploadedFiles.length)} for context
                    </span>
                  )}
                </div>
                
                <div className="space-y-1">
                  {uploadedFiles.map((file) => (
                    <div
                      key={file.id}
                      className="flex items-center justify-between p-2 border rounded bg-gray-50/50 hover:bg-gray-50"
                    >
                      <div className="flex items-center space-x-2 flex-1 min-w-0">
                        <File className="h-3 w-3 text-blue-600 flex-shrink-0" />
                        <span className="text-sm truncate">{file.name}</span>
                        {file.chunks && (
                          <span className="text-xs text-muted-foreground flex-shrink-0">
                            {file.chunks} chunks
                          </span>
                        )}
                      </div>
                      <Button
                        size="sm"
                        variant="ghost"
                        onClick={() => removeFile(file.id)}
                        disabled={isLoading}
                        className="h-6 w-6 p-0 text-red-500 hover:text-red-600 hover:bg-red-50"
                      >
                        <X className="h-3 w-3" />
                      </Button>
                    </div>
                  ))}
                </div>
              </div>
            )}

            {/* Empty State */}
            {uploadedFiles.length === 0 && (
              <div className="text-center py-3">
                <p className="text-xs text-muted-foreground">
                  No documents uploaded yet
                </p>
              </div>
            )}
          </CardContent>
        </Card>
      )}
    </div>
  )
}