| 9 | } |
| 10 | |
| 11 | export const sendChat = async ( |
| 12 | optsOrFileId: ChatOptions | string | null, |
| 13 | question?: string |
| 14 | ) => { |
| 15 | // Support both (fileId, question) and (ChatOptions) signatures |
| 16 | let payload: any; |
| 17 | if ( |
| 18 | optsOrFileId && |
| 19 | typeof optsOrFileId === 'object' && |
| 20 | ('question' in optsOrFileId || 'fileId' in optsOrFileId) |
| 21 | ) { |
| 22 | // New hybrid API usage |
| 23 | const response = await axios.post('/api/chat', { |
| 24 | question: optsOrFileId.question, |
| 25 | file_id: optsOrFileId.fileId, |
| 26 | keywords: optsOrFileId.keywords, |
| 27 | metadata_filter: optsOrFileId.metadataFilter, |
| 28 | k: optsOrFileId.k, |
| 29 | }); |
| 30 | return response.data; |
| 31 | } |
| 32 | // Legacy usage |
| 33 | payload = { question, file_id: optsOrFileId }; |
| 34 | try { |
| 35 | const response = await axios.post('/api/chat', payload); |
| 36 | return response.data; |
| 37 | } catch (error: any) { |
| 38 | throw error?.response?.data?.detail || 'Chat request failed'; |
| 39 | } |
| 40 | }; |