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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import { KnowledgeSource, RetrievedChunk, Citation, Language, LegalQueryResponse, ImageJob } from './types';

const DEFAULT_API_BASE = 'http://127.0.0.1:8000';
const API_BASE = (import.meta as any).env?.VITE_API_URL || DEFAULT_API_BASE;

// ── Types ────────────────────────────────────────────────────────────────────

type BackendSource = {
  id: string;
  type: 'pdf' | 'url' | 'youtube' | 'statute' | 'judgment' | 'constitution';
  title: string;
  origin?: string;
  language?: string;
  status?: KnowledgeSource['status'];
  chunkCount?: number;
  createdAt?: string;
  dateAdded?: string;
  doc_type?: string;   // For legal sources
  court?: string;      // For legal sources
  judgment_date?: string; // For legal sources
  progress_percentage?: number;
  error_message?: string;
};

export interface QueryResponse {
  answer: string;
  sources: Citation[];
  retrievedChunks: RetrievedChunk[];
  conversationId?: string; // Added for Phase 1
}

export interface Conversation {
  id: string;
  title: string;
  conv_type?: 'general' | 'legal';
  created_at: string;
  updated_at: string;
  message_count: number;
}

export interface ConversationMessage {
  id: string;
  question: string;
  answer: string;
  sourcesUsed: string[];
  createdAt: string;
}

// ── Helper: Normalization ───────────────────────────────────────────────────

function normalizeSource(raw: BackendSource): KnowledgeSource {
  // Map "url" β†’ "web" for frontend type consistency
  let type: KnowledgeSource['type'] =
    raw.type === 'url' ? 'web' :
      (raw.type as KnowledgeSource['type']);

  // If it's a legal type from Prompt 4, we might want to map it to 'pdf' 
  // or keep it if types.ts supports it. Assuming pdf for now if not 'web'/'youtube'.
  if (['statute', 'judgment', 'constitution'].includes(raw.type)) {
    type = 'pdf';
  }

  const language = ((raw.language || 'en').toUpperCase()) as Language;
  const dateRaw = raw.createdAt || raw.dateAdded;
  const dateAdded = dateRaw ? new Date(dateRaw) : new Date();

  const metadata: KnowledgeSource['metadata'] = {};
  if (raw.origin) {
    if (type === 'web' || type === 'youtube') metadata.url = raw.origin;
    if (raw.origin.match(/[?&]v=([^&]+)/)) {
      const match = raw.origin.match(/[?&]v=([^&]+)/);
      if (match) metadata.videoId = match[1];
    }
  }

  // Add legal metadata if present
  if (raw.doc_type) metadata.docType = raw.doc_type;
  if (raw.court) metadata.court = raw.court;
  if (raw.judgment_date) metadata.judgmentDate = raw.judgment_date;

  return {
    id: String(raw.id),
    type: type || 'pdf',
    title: raw.title,
    language,
    status: raw.status || 'completed',
    chunkCount: raw.chunkCount ?? 0,
    dateAdded,
    progress_percentage: raw.progress_percentage ?? 0,
    error_message: raw.error_message,
    metadata,
  };
}

// ── Sources API ──────────────────────────────────────────────────────────────

export async function fetchSources(): Promise<KnowledgeSource[]> {
  const res = await fetch(`${API_BASE}/sources`);
  if (!res.ok) throw new Error(`Failed to fetch sources: ${res.statusText}`);
  const data = await res.json();
  const raw: BackendSource[] = Array.isArray(data) ? data : (data.sources ?? []);
  return raw.map(normalizeSource);
}

export async function fetchLegalSources(): Promise<KnowledgeSource[]> {
  const res = await fetch(`${API_BASE}/legal/legal-sources`);
  if (!res.ok) throw new Error(`Failed to fetch legal sources: ${res.statusText}`);
  const data = await res.json();
  const raw: BackendSource[] = data.sources ?? [];
  return raw.map(normalizeSource);
}

export async function deleteSource(id: string): Promise<void> {
  const res = await fetch(`${API_BASE}/sources/${encodeURIComponent(id)}`, {
    method: 'DELETE',
  });
  if (!res.ok) {
    const detail = await res.text().catch(() => '');
    throw new Error(detail || `Delete failed with status ${res.status}`);
  }
}

// ── Ingestion API ────────────────────────────────────────────────────────────

export async function uploadPdf(file: File): Promise<KnowledgeSource> {
  const formData = new FormData();
  formData.append('file', file);

  const res = await fetch(`${API_BASE}/upload-pdf`, {
    method: 'POST',
    body: formData,
  });

  if (!res.ok) throw new Error(await res.text() || 'PDF Upload failed');
  const data = await res.json();
  return normalizeSource({
    id: data.source_id,
    type: 'pdf',
    title: data.title,
    chunkCount: data.chunk_count,
    status: data.status || 'UPLOADING',
    createdAt: new Date().toISOString(),
  });
}

export async function fetchSourceProgress(sourceId: string): Promise<KnowledgeSource & { progress_percentage: number; error_message?: string }> {
  const res = await fetch(`${API_BASE}/sources/${encodeURIComponent(sourceId)}`);
  if (!res.ok) throw new Error(`Failed to fetch source progress: ${res.statusText}`);
  const data = await res.json();
  return {
    ...normalizeSource(data),
    progress_percentage: data.progress_percentage ?? 0,
    error_message: data.error_message,
  };
}

export async function uploadLegal(file: File, docType: string = "judgment"): Promise<KnowledgeSource> {
  const formData = new FormData();
  formData.append('file', file);

  const res = await fetch(`${API_BASE}/legal/upload-legal?doc_type=${docType}`, {
    method: 'POST',
    body: formData,
  });

  if (!res.ok) throw new Error(await res.text() || 'Legal Upload failed');
  const data = await res.json();
  return normalizeSource({
    id: data.source_id,
    type: data.doc_type as any || 'pdf',
    title: data.title,
    chunkCount: data.chunk_count,
    status: 'completed',
    createdAt: new Date().toISOString(),
    doc_type: data.doc_type
  });
}

export async function addWebsite(url: string, language: Language = 'EN'): Promise<KnowledgeSource> {
  const res = await fetch(`${API_BASE}/add-url`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ url, language: language.toLowerCase() }),
  });
  if (!res.ok) throw new Error(await res.text() || 'URL Ingestion failed');
  const data = await res.json();
  return normalizeSource({
    id: data.source_id,
    type: 'url',
    title: data.title,
    chunkCount: data.chunk_count,
    status: data.status || 'EXTRACTING',
    origin: url,
  });
}

export async function addYouTube(url: string, language: Language = 'EN'): Promise<KnowledgeSource> {
  const res = await fetch(`${API_BASE}/add-youtube`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ url, language: language.toLowerCase() }),
  });
  if (!res.ok) throw new Error(await res.text() || 'YouTube Ingestion failed');
  const data = await res.json();
  return normalizeSource({
    id: data.source_id,
    type: 'youtube',
    title: data.title,
    chunkCount: data.chunk_count,
    status: data.status || 'EXTRACTING',
    origin: url,
  });
}

// ── Image API ────────────────────────────────────────────────────────────────

export async function uploadImage(file: File, context: string = ""): Promise<{ image_id: string, status: string }> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('context', context);

  const res = await fetch(`${API_BASE}/images/upload-image`, {
    method: 'POST',
    body: formData,
  });

  if (!res.ok) throw new Error(await res.text() || 'Image Upload failed');
  return await res.json();
}

export async function fetchImageJobs(): Promise<ImageJob[]> {
  const res = await fetch(`${API_BASE}/images/image-jobs`);
  if (!res.ok) throw new Error(`Failed to fetch image jobs: ${res.statusText}`);
  const data = await res.json();
  return data.jobs ?? [];
}

export async function fetchPendingImageCount(): Promise<number> {
  const res = await fetch(`${API_BASE}/images/image-jobs/pending-count`);
  if (!res.ok) throw new Error(`Failed to fetch pending count: ${res.statusText}`);
  const data = await res.json();
  return data.count ?? 0;
}

// ── Query API ───────────────────────────────────────────────────────────────

export async function queryRag(
  question: string, 
  sourceIds?: string[], 
  imageId?: string, 
  includeImages?: boolean,
  llmProvider: string = "groq"
): Promise<QueryResponse> {
  const res = await fetch(`${API_BASE}/query`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      question,
      source_ids: sourceIds && sourceIds.length > 0 ? sourceIds : null,
      conversation_id: (window as any).currentConversationId || null,
      image_id: imageId || null,
      include_images: includeImages || false,
      llm_provider: llmProvider,
    }),
  });

  if (!res.ok) throw new Error(await res.text() || 'Query failed');
  const data = await res.json();

  return {
    answer: data.answer,
    conversationId: data.conversationId, // Added for Phase 1
    sources: (data.citations || []).map((c: any) => ({
      sourceTitle: c.sourceTitle || c.source_title || '',
      sourceType: c.sourceType === 'url' ? 'web' : (c.sourceType || 'pdf'),
      reference: c.reference || '',
      snippet: c.snippet || '',
    })),
    retrievedChunks: (data.retrievedChunks || []).map((c: any) => ({
      id: c.chunkId || c.id || '',
      sourceId: c.sourceId || '',
      sourceName: c.sourceTitle || c.sourceName || '',
      sourceType: c.sourceType === 'url' ? 'web' : (c.sourceType || 'pdf'),
      language: ((c.language || 'en').toUpperCase()) as Language,
      text: c.text || '',
      similarityScore: c.score ?? c.similarityScore ?? 0,
      metadata: {
        page: c.pageNumber ?? c.metadata?.page,
        timestamp: c.timestampS != null
          ? `${Math.floor(c.timestampS / 60)}:${String(c.timestampS % 60).padStart(2, '0')}`
          : c.metadata?.timestamp,
        url: c.urlRef || c.metadata?.url,
        ipcSections: c.metadata?.ipc_sections || c.ipc_sections || [],
      },
    })),
  };
}

// ── Streaming API ───────────────────────────────────────────────────────────

export async function streamQueryRag(
  question: string,
  sourceIds: string[] | undefined,
  history: Array<{ role: string; content: string }>,
  onToken: (token: string) => void,
  onMeta: (meta: { chatId: string; conversationId?: string; citations: Citation[]; retrievedChunks: RetrievedChunk[] }) => void,
  onError: (err: Error) => void,
  imageId?: string,
  includeImages?: boolean,
  llmProvider: string = "groq",
  isLegalMode: boolean = false,
  legalFilter: string | null = null,
  agenticMode: boolean = false,
  onAgentStatus?: (status: { stage: number; message: string }) => void
): Promise<void> {
  let response: Response;
  try {
    response = await fetch(`${API_BASE}/query-stream`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        question,
        source_ids: sourceIds && sourceIds.length > 0 ? sourceIds : null,
        history,
        conversation_id: (window as any).currentConversationId || null,
        image_id: imageId || null,
        include_images: includeImages || false,
        llm_provider: llmProvider,
        is_legal_mode: isLegalMode,
        legal_filter: legalFilter,
        agentic_mode: agenticMode,
      }),
    });
  } catch (err) {
    if (err instanceof TypeError && err.message.includes('fetch')) {
      onError(new Error("Cannot connect to server. Is the backend running on port 8000?"));
    } else {
      onError(err instanceof Error ? err : new Error(String(err)));
    }
    return;
  }

  if (!response.ok) {
    onError(new Error(`Server returned ${response.status}`));
    return;
  }
  if (!response.body) {
    onError(new Error('No response body from server'));
    return;
  }

  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  let buffer = '';

  try {
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;

      buffer += decoder.decode(value, { stream: true });
      const lines = buffer.split('\n');
      buffer = lines.pop() || '';

      for (const line of lines) {
        if (!line.startsWith('data: ')) continue;
        const jsonStr = line.slice(6).trim();
        if (!jsonStr) continue;

        try {
          const parsed = JSON.parse(jsonStr);

          if (parsed.type === 'error') {
            onError(new Error(parsed.message || "Unknown streaming error"));
            return;
          }

          if (parsed.type === 'agent_status') {
            if (onAgentStatus) {
              onAgentStatus({ stage: parsed.stage, message: parsed.message });
            }
          }

          if (parsed.type === 'meta') {
            const citations: Citation[] = (parsed.citations || []).map((c: any) => ({
              sourceTitle: c.sourceTitle || c.source_title || '',
              sourceType: (c.sourceType === 'url' ? 'web' : c.sourceType || 'pdf') as any,
              reference: c.reference || '',
              snippet: c.snippet || '',
            }));

            const retrievedChunks: RetrievedChunk[] = (parsed.retrievedChunks || []).map((c: any, i: number) => ({
              id: c.chunkId || String(i),
              sourceId: c.sourceId || '',
              sourceName: c.sourceTitle || '',
              sourceType: (c.sourceType === 'url' ? 'web' : c.sourceType || 'pdf') as any,
              text: c.text || '',
              similarityScore: c.score || 0,
              metadata: { page: c.pageNumber, timestamp: c.timestampS, url: c.urlRef }
            }));

            onMeta({ 
              chatId: parsed.chatId || '', 
              conversationId: parsed.conversationId, 
              citations, 
              retrievedChunks 
            });
          }

          if (parsed.type === 'token' && parsed.content) {
            onToken(parsed.content);
          }

          if (parsed.type === 'done') {
            return;
          }
        } catch (e) {
          // Partial JSON or parse error β€” skip
        }
      }
    }
  } catch (err) {
    onError(err instanceof Error ? err : new Error(String(err)));
  }
}

// ── Evaluation API ───────────────────────────────────────────────────────────

export async function startEvaluation(nQuestions: number = 15, agenticMode: boolean = false): Promise<string> {
  const response = await fetch(`${API_BASE}/eval/start`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ 
      n_questions: nQuestions,
      agentic_mode: agenticMode
    }),
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.detail || 'Failed to start evaluation');
  }
  const data = await response.json();
  return data.job_id;
}

export async function getEvaluationStatus(jobId: string): Promise<any> {
  const response = await fetch(`${API_BASE}/eval/status/${jobId}`);
  if (!response.ok) {
    throw new Error('Failed to fetch evaluation status');
  }
  return response.json();
}

// ── History API ─────────────────────────────────────────────────────────────

export interface BackendChatEntry {
  id: string;
  question: string;
  answer: string;
  sourcesUsed: string[];
  createdAt: string;
}

export async function fetchHistory(): Promise<BackendChatEntry[]> {
  const res = await fetch(`${API_BASE}/history`);
  if (!res.ok) throw new Error(`Failed to fetch history: ${res.statusText}`);
  const data = await res.json();
  return data.history ?? [];
}

export async function clearHistory(): Promise<void> {
  const res = await fetch(`${API_BASE}/history`, { method: 'DELETE' });
  if (!res.ok) throw new Error('Failed to clear history');
}

// ── Events ──────────────────────────────────────────────────────────────────

export function notifySidebarRefresh() {
  window.dispatchEvent(new CustomEvent('sources-updated'));
}

export async function queryLegal(question: string, sourceFilter?: string, modelType: "finetuned" | "base" = "finetuned"): Promise<LegalQueryResponse> {
  const res = await fetch(`${API_BASE}/legal-query`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ 
      question, 
      source_filter: sourceFilter || null, 
      language: "en",
      model_type: modelType 
    })
  });
  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Failed to query legal search: ${text}`);
  }
  return res.json();
}

// ── Conversations API ────────────────────────────────────────────────────────

export async function createConversation(title = 'New Chat'): Promise<Conversation> {
  const res = await fetch(`${API_BASE}/conversations`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title }),
  });
  if (!res.ok) throw new Error('Failed to create conversation');
  return res.json();
}

export async function fetchConversations(): Promise<Conversation[]> {
  const res = await fetch(`${API_BASE}/conversations`);
  if (!res.ok) throw new Error('Failed to fetch conversations');
  const data = await res.json();
  return data.conversations ?? [];
}

export async function fetchConversationMessages(convId: string): Promise<{
  conversation: { id: string; title: string };
  messages: ConversationMessage[];
}> {
  const res = await fetch(`${API_BASE}/conversations/${convId}/messages`);
  if (!res.ok) throw new Error('Failed to fetch messages');
  return res.json();
}

export async function renameConversation(convId: string, title: string): Promise<void> {
  await fetch(`${API_BASE}/conversations/${convId}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title }),
  });
}

export async function deleteConversation(convId: string): Promise<void> {
  await fetch(`${API_BASE}/conversations/${convId}`, { method: 'DELETE' });
}

// ── Export API ──────────────────────────────────────────────────────────────

export async function exportToPDF(title: string, content: string, citations: any[]): Promise<void> {
  const response = await fetch(`${API_BASE}/export/pdf`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title, content, citations }),
  });

  if (!response.ok) throw new Error('Failed to export PDF');

  const blob = await response.blob();
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = `InteleX_Research_${new Date().getTime()}.pdf`;
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  document.body.removeChild(a);
}