File size: 3,347 Bytes
1423ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acceac1
 
 
 
 
 
 
1423ea9
 
 
 
 
 
 
 
 
35d0ce7
1423ea9
 
 
 
 
 
 
 
 
 
35d0ce7
1423ea9
 
 
35d0ce7
 
 
 
 
 
 
 
 
 
 
 
 
1423ea9
 
 
 
35d0ce7
1423ea9
 
 
 
 
 
 
35d0ce7
1423ea9
 
 
 
 
 
 
 
35d0ce7
1423ea9
 
 
 
 
acceac1
 
 
 
 
35d0ce7
1423ea9
 
 
 
 
 
 
 
 
 
bcebe14
1423ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import API from "./api"; // Your existing Axios instance
import { type AxiosResponse } from "axios";

// Interface matching the Backend Pydantic model "NoteInfo"
export interface Note {
  id: number;
  filename: string;
  created_at: string;
}

export interface ChatMessage {
  role: "user" | "assistant";
  content: string;
}

export interface Session {
  id: string;
  name: string;
  created_at: string;
  pdf_id: number;
}

// 1. Fetch the list of PDFs for the Sidebar
export const fetchNotes = async (): Promise<Note[]> => {
  const response: AxiosResponse<Note[]> = await API.get("/notes/");
  return response.data;
};

// 2. Upload a new PDF
export const uploadNote = async (file: File): Promise<Note> => {
  const formData = new FormData();
  formData.append("file", file);

  const response = await API.post("/notes/upload_notes", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });

  return {
    id: response.data.doc_id,
    filename: response.data.filename,
    created_at: new Date().toISOString(),
  };
};

// 3. NEW: Delete a note
export const deleteNote = async (noteId: number) => {
  const response = await API.delete(`/notes/${noteId}`);
  return response.data;
};

// 4. NEW: Rename a note
export const renameNote = async (noteId: number, newName: string) => {
  const response = await API.put(`/notes/${noteId}`, { new_filename: newName });
  return response.data;
};

// 5. Get the URL for the PDF content (for the viewer)
export const getNoteContentUrl = (noteId: number): string => {
  return `/api/v1/notes/${noteId}/content`;
};

// 6. Fetch the Blob directly
export const fetchNoteBlob = async (noteId: number): Promise<Blob> => {
  const response = await API.get(`/notes/${noteId}/content`, {
    responseType: "blob",
  });
  return response.data;
};

// 7. Create or Get Chat Session
export const createChatSession = async (
  pdfId: number,
  name: string = "New Chat"
) => {
  const response = await API.post("/notes/sessions", { pdf_id: pdfId, name });
  return response.data;
};

// 8. Get Chat History
export const fetchChatHistory = async (sessionId: string) => {
  const response = await API.get(`/notes/history/${sessionId}`);
  return response.data;
};

export const fetchSessions = async (pdfId: number): Promise<Session[]> => {
  const response = await API.get(`/notes/sessions/${pdfId}`);
  return response.data;
};

// 9. Stream Chat (Special Handling using fetch API)
export const streamChatRequest = async (
  sessionId: string,
  userMessage: string,
  onChunk: (chunk: string) => void,
  onError: (err: any) => void
) => {
  const token = localStorage.getItem("token");

  try {
    const response = await fetch(
      `/api/v1/notes/chat/${sessionId}?user_prompt=${encodeURIComponent(
        userMessage
      )}`,
      {
        method: "POST",
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
      }
    );

    if (!response.body) throw new Error("No response body");

    const reader = response.body.getReader();
    const decoder = new TextDecoder("utf-8");

    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      const chunk = decoder.decode(value, { stream: true });
      onChunk(chunk);
    }
  } catch (err) {
    onError(err);
  }
};