Spaces:
Paused
Paused
Download console/src/api/modules/chat.ts from mysticaldragon/HF: direct link, hf CLI and curl.
- Browser
- Download file 6.38 kB
-
https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src/api/modules/chat.ts
- Command line
-
hf download hf://spaces/mysticaldragon/HF/console/src/api/modules/chat.ts
-
curl -L -o chat.ts https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src/api/modules/chat.ts
6.38 kB
| import { request } from "../request"; | |
| import { getApiUrl, getApiToken } from "../config"; | |
| import { buildAuthHeaders } from "../authHeaders"; | |
| import type { | |
| ChatSpec, | |
| ChatHistory, | |
| ChatDeleteResponse, | |
| ChatUpdateRequest, | |
| ChatGroup, | |
| BatchArchiveResult, | |
| Session, | |
| } from "../types"; | |
| /** Response from POST /console/upload. url = filename only; agent_id from header. */ | |
| export interface ChatUploadResponse { | |
| url: string; | |
| file_name: string; | |
| stored_name?: string; | |
| } | |
| const FILES_PREVIEW = "/files/preview"; | |
| export const chatApi = { | |
| /** Upload a file for chat attachment. Returns URL path for content. */ | |
| uploadFile: async (file: File): Promise<ChatUploadResponse> => { | |
| const formData = new FormData(); | |
| formData.append("file", file); | |
| const response = await fetch(getApiUrl("/console/upload"), { | |
| method: "POST", | |
| headers: buildAuthHeaders(), | |
| body: formData, | |
| }); | |
| if (!response.ok) { | |
| const text = await response.text().catch(() => ""); | |
| throw new Error( | |
| `Upload failed: ${response.status} ${response.statusText}${ | |
| text ? ` - ${text}` : "" | |
| }`, | |
| ); | |
| } | |
| return response.json(); | |
| }, | |
| filePreviewUrl: (filename: string): string => { | |
| if (!filename) return ""; | |
| if (filename.startsWith("http://") || filename.startsWith("https://")) | |
| return filename; | |
| let cleaned = filename.replace(/^\/+/, ""); | |
| const path = `${FILES_PREVIEW}/${cleaned}`; | |
| const url = getApiUrl(path); | |
| const token = getApiToken(); | |
| if (token) { | |
| return `${url}?token=${encodeURIComponent(token)}`; | |
| } | |
| return url; | |
| }, | |
| listChats: (params?: { | |
| user_id?: string; | |
| channel?: string; | |
| archived?: boolean; | |
| include_app_owned?: boolean; | |
| }) => { | |
| const searchParams = new URLSearchParams(); | |
| if (params?.user_id) searchParams.append("user_id", params.user_id); | |
| if (params?.channel) searchParams.append("channel", params.channel); | |
| if (params?.archived !== undefined) | |
| searchParams.append("archived", String(params.archived)); | |
| if (params?.include_app_owned !== undefined) | |
| searchParams.append( | |
| "include_app_owned", | |
| String(params.include_app_owned), | |
| ); | |
| const query = searchParams.toString(); | |
| return request<ChatSpec[]>(`/chats${query ? `?${query}` : ""}`); | |
| }, | |
| createChat: (chat: Partial<ChatSpec>) => | |
| request<ChatSpec>("/chats", { | |
| method: "POST", | |
| body: JSON.stringify(chat), | |
| }), | |
| getChat: ( | |
| chatId: string, | |
| options?: { signal?: AbortSignal; include_app_owned?: boolean }, | |
| ) => { | |
| const searchParams = new URLSearchParams(); | |
| if (options?.include_app_owned !== undefined) | |
| searchParams.append( | |
| "include_app_owned", | |
| String(options.include_app_owned), | |
| ); | |
| const query = searchParams.toString(); | |
| return request<ChatHistory>( | |
| `/chats/${encodeURIComponent(chatId)}${query ? `?${query}` : ""}`, | |
| { | |
| signal: options?.signal, | |
| }, | |
| ); | |
| }, | |
| updateChat: (chatId: string, chat: ChatUpdateRequest) => | |
| request<ChatSpec>(`/chats/${encodeURIComponent(chatId)}`, { | |
| method: "PUT", | |
| body: JSON.stringify(chat), | |
| }), | |
| deleteChat: (chatId: string) => | |
| request<ChatDeleteResponse>(`/chats/${encodeURIComponent(chatId)}`, { | |
| method: "DELETE", | |
| }), | |
| batchDeleteChats: (chatIds: string[]) => | |
| request<{ success: boolean; deleted_count: number }>( | |
| "/chats/batch-delete", | |
| { | |
| method: "POST", | |
| body: JSON.stringify(chatIds), | |
| }, | |
| ), | |
| archiveChat: (chatId: string) => | |
| request<ChatSpec>(`/chats/${encodeURIComponent(chatId)}/archive`, { | |
| method: "POST", | |
| }), | |
| unarchiveChat: (chatId: string) => | |
| request<ChatSpec>(`/chats/${encodeURIComponent(chatId)}/unarchive`, { | |
| method: "POST", | |
| }), | |
| batchArchiveChats: (chatIds: string[]) => | |
| request<BatchArchiveResult>("/chats/actions/batch-archive", { | |
| method: "POST", | |
| body: JSON.stringify({ chat_ids: chatIds }), | |
| }), | |
| batchUnarchiveChats: (chatIds: string[]) => | |
| request<BatchArchiveResult>("/chats/actions/batch-unarchive", { | |
| method: "POST", | |
| body: JSON.stringify({ chat_ids: chatIds }), | |
| }), | |
| listGroups: () => request<ChatGroup[]>("/chats/groups"), | |
| createGroup: (name: string) => | |
| request<ChatGroup>("/chats/groups", { | |
| method: "POST", | |
| body: JSON.stringify({ name }), | |
| }), | |
| updateGroup: (groupId: string, update: { name?: string; pinned?: boolean }) => | |
| request<ChatGroup>(`/chats/groups/${encodeURIComponent(groupId)}`, { | |
| method: "PUT", | |
| body: JSON.stringify(update), | |
| }), | |
| reorderGroups: (groupIds: string[]) => | |
| request<ChatGroup[]>("/chats/groups/order", { | |
| method: "PUT", | |
| body: JSON.stringify({ group_ids: groupIds }), | |
| }), | |
| deleteGroup: (groupId: string) => | |
| request<{ success: boolean; group_id: string }>( | |
| `/chats/groups/${encodeURIComponent(groupId)}`, | |
| { method: "DELETE" }, | |
| ), | |
| stopChat: (chatId: string) => | |
| request<void>(`/console/chat/stop?chat_id=${encodeURIComponent(chatId)}`, { | |
| method: "POST", | |
| }), | |
| }; | |
| export const sessionApi = { | |
| listSessions: (params?: { user_id?: string; channel?: string }) => { | |
| const searchParams = new URLSearchParams(); | |
| if (params?.user_id) searchParams.append("user_id", params.user_id); | |
| if (params?.channel) searchParams.append("channel", params.channel); | |
| const query = searchParams.toString(); | |
| return request<Session[]>(`/chats${query ? `?${query}` : ""}`); | |
| }, | |
| getSession: (sessionId: string) => | |
| request<ChatHistory>(`/chats/${encodeURIComponent(sessionId)}`), | |
| deleteSession: (sessionId: string) => | |
| request<ChatDeleteResponse>(`/chats/${encodeURIComponent(sessionId)}`, { | |
| method: "DELETE", | |
| }), | |
| createSession: (session: Partial<Session>) => | |
| request<Session>("/chats", { | |
| method: "POST", | |
| body: JSON.stringify(session), | |
| }), | |
| updateSession: (sessionId: string, session: ChatUpdateRequest) => | |
| request<Session>(`/chats/${encodeURIComponent(sessionId)}`, { | |
| method: "PUT", | |
| body: JSON.stringify(session), | |
| }), | |
| batchDeleteSessions: (sessionIds: string[]) => | |
| request<{ success: boolean; deleted_count: number }>( | |
| "/chats/batch-delete", | |
| { | |
| method: "POST", | |
| body: JSON.stringify(sessionIds), | |
| }, | |
| ), | |
| }; | |