import { getAuthHeaders } from '../utils/auth'; import { requestJson } from './client'; const auth = () => ({ headers: getAuthHeaders() }); const jsonAuth = () => ({ headers: { 'Content-Type': 'application/json', ...getAuthHeaders() } }); export interface ChatSessionItem { id: number; conversation_id: string; title: string; created_at: string; } export interface LeaveAgentOption { uid: string; employeeId: string; name: string; jobtitle?: string; jobtitleName?: string; departmentid?: string; subcompanyid1?: string; departmentName?: string; subcompanyName?: string; } export interface OaEmployeesResponse { items: LeaveAgentOption[]; total: number; } export interface ChatSourceFileResult { source_download_url?: string; source_filename?: string; source_ext?: string; } export interface ChatUploadResult { id: string; name?: string; size?: number; extension?: string; mime_type?: string; type?: 'image' | 'document' | 'audio' | 'video' | 'custom'; } export interface ChatQueryPreviewPage { columns: string[]; rows: any[][]; page: number; page_size: number; total: number; has_more: boolean; query?: string; } export const listChatSessions = (agentId: number) => requestJson(`/api/chat/sessions?agent_id=${agentId}`, auth()); export const deleteAllChatSessions = (agentId: number) => requestJson(`/api/chat/sessions?agent_id=${agentId}`, { method: 'DELETE', ...auth() }); export const deleteChatSession = (conversationId: string, agentId: number) => requestJson(`/api/chat/sessions/${conversationId}?agent_id=${agentId}`, { method: 'DELETE', ...auth() }); export const listChatMessages = (conversationId: string, agentId: number) => requestJson(`/api/chat/messages?conversation_id=${conversationId}&agent_id=${agentId}`, auth()); export const saveLocalChatMessage = (payload: Record) => requestJson<{ conversation_id?: string; chat_log_id?: number }>('/api/chat/messages/local', { method: 'POST', ...jsonAuth(), body: JSON.stringify(payload), }); export const resolveChatSourceFile = (params: { agent_id: number; dataset_id: string; remote_document_id: string; }) => { const qs = new URLSearchParams({ agent_id: String(params.agent_id), dataset_id: params.dataset_id, remote_document_id: params.remote_document_id, }); return requestJson(`/api/chat/source-file?${qs.toString()}`, auth()); }; export const listOaEmployees = ( agentId: number, query: { keyword?: string; page?: number; page_size?: number }, ) => { const qs = new URLSearchParams({ agent_id: String(agentId), keyword: query.keyword || '', page: String(query.page ?? 1), page_size: String(query.page_size ?? 20), }); return requestJson(`/api/chat/oa/employees?${qs.toString()}`, auth()); }; export const uploadChatFile = (agentId: number, file: File) => { const qs = new URLSearchParams({ agent_id: String(agentId) }); const fd = new FormData(); fd.append('file', file); return requestJson(`/api/chat/files/upload?${qs.toString()}`, { method: 'POST', body: fd, headers: getAuthHeaders(), }); }; export const getChatQueryPreviewPage = ( agentId: number, chatLogId: number, query: { page?: number; page_size?: number } = {}, ) => { const qs = new URLSearchParams({ agent_id: String(agentId), page: String(query.page ?? 1), page_size: String(query.page_size ?? 100), }); return requestJson(`/api/chat/query-preview/${chatLogId}?${qs.toString()}`, auth()); };