Download src/lib/api/documents.ts from asnannp/DocDoeAI: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/lib/api/documents.ts
- Command line
-
hf download hf://spaces/asnannp/DocDoeAI/src/lib/api/documents.ts
-
curl -L -o documents.ts https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/lib/api/documents.ts
3.82 kB
| import { apiDelete, apiGet, apiPatch, apiPost, apiUpload } from "@/lib/api/client"; | |
| import { withDemoFallback } from "@/lib/api/mode"; | |
| import type { | |
| ApiDocument, | |
| DocumentChunksListResponse, | |
| DocumentPreview, | |
| DocumentUploadInput, | |
| GenerationResponse, | |
| RetrievalRequest, | |
| RetrievalResponse, | |
| StudyMapOutput, | |
| } from "@/lib/api/types"; | |
| import { getPublicDemoMode } from "@/lib/env"; | |
| import { | |
| getLocalDocument, | |
| getLocalDocumentChunks, | |
| getLocalDocumentPreview, | |
| getLocalDocuments, | |
| retrieveLocalDocumentChunks, | |
| } from "@/lib/frontend-local-store"; | |
| export async function listDocuments() { | |
| if (getPublicDemoMode() === "demo") { | |
| return getLocalDocuments(); | |
| } | |
| return withDemoFallback( | |
| () => apiGet<ApiDocument[]>("/documents"), | |
| () => getLocalDocuments(), | |
| ); | |
| } | |
| export async function getDocument(documentId: string) { | |
| if (getPublicDemoMode() === "demo") { | |
| const localDocument = getLocalDocument(documentId); | |
| if (localDocument) return localDocument; | |
| } | |
| return withDemoFallback( | |
| () => apiGet<ApiDocument>(`/documents/${documentId}`), | |
| () => { | |
| const localDocument = getLocalDocument(documentId); | |
| if (!localDocument) throw new Error("Source was not found."); | |
| return localDocument; | |
| }, | |
| ); | |
| } | |
| export async function getDocumentPreview(documentId: string) { | |
| if (getPublicDemoMode() === "demo") { | |
| return getLocalDocumentPreview(documentId); | |
| } | |
| return withDemoFallback( | |
| () => apiGet<DocumentPreview>(`/documents/${documentId}/preview`), | |
| () => getLocalDocumentPreview(documentId), | |
| ); | |
| } | |
| export async function listDocumentChunks(documentId: string) { | |
| if (getPublicDemoMode() === "demo") { | |
| return getLocalDocumentChunks(documentId); | |
| } | |
| return withDemoFallback( | |
| () => apiGet<DocumentChunksListResponse>(`/documents/${documentId}/chunks`), | |
| () => getLocalDocumentChunks(documentId), | |
| ); | |
| } | |
| export async function retrieveDocumentChunks( | |
| documentId: string, | |
| payload: RetrievalRequest, | |
| ) { | |
| if (getPublicDemoMode() === "demo") { | |
| return retrieveLocalDocumentChunks(documentId, payload.query, payload.limit); | |
| } | |
| return withDemoFallback( | |
| () => apiPost<RetrievalResponse, RetrievalRequest>( | |
| `/documents/${documentId}/retrieve`, | |
| payload, | |
| ), | |
| () => retrieveLocalDocumentChunks(documentId, payload.query, payload.limit), | |
| ); | |
| } | |
| export async function createStudyMap(documentId: string, language = "English") { | |
| return apiPost<GenerationResponse<StudyMapOutput>, Record<string, never>>( | |
| `/documents/${documentId}/study-map?language=${encodeURIComponent(language)}`, | |
| {}, | |
| { timeoutMs: 60000 }, | |
| ); | |
| } | |
| export async function reclassifyDocument(documentId: string, materialType: string) { | |
| return apiPatch<ApiDocument, { material_type: string }>( | |
| `/documents/${documentId}/material-type`, | |
| { material_type: materialType }, | |
| ); | |
| } | |
| export async function deleteDocument(documentId: string) { | |
| // /documents/{id} (not /sources/{id}) — it also cleans up derived | |
| // generations/quizzes/flashcards and detaches video/profile references. | |
| return apiDelete<void>(`/documents/${documentId}`); | |
| } | |
| export async function retryDocumentProcessing(documentId: string) { | |
| return apiPost<ApiDocument, Record<string, never>>( | |
| `/documents/${documentId}/retry-processing`, | |
| {}, | |
| { timeoutMs: 60000 }, | |
| ); | |
| } | |
| export async function uploadDocument(input: DocumentUploadInput) { | |
| const formData = new FormData(); | |
| formData.append("file", input.file); | |
| if (input.title) formData.append("title", input.title); | |
| if (input.subject) formData.append("subject", input.subject); | |
| if (input.chapter) formData.append("chapter", input.chapter); | |
| if (input.syllabus) formData.append("syllabus", input.syllabus); | |
| return apiUpload<ApiDocument>("/documents/upload", formData, { timeoutMs: 60000 }); | |
| } | |