import api from '../../api'; import type { AiApiResponse, AiConversation, AiMessagePage } from './types'; const basePath = '/ai/chat/conversations'; export const aiChatApi = { listConversations: async () => (await api.get>(basePath)).data, createConversation: async (title?: string) => (await api.post>(basePath, title ? { title } : {})).data, renameConversation: async (id: number, title: string) => (await api.patch>(`${basePath}/${id}`, { title })).data, deleteConversation: (id: number) => api.delete(`${basePath}/${id}`), listMessages: async (id: number): Promise => { const first = ( await api.get>(`${basePath}/${id}/messages`, { params: { page: 1, limit: 100 }, }) ).data; const pageCount = Math.ceil(first.total / first.limit); if (pageCount <= 1) return first; const rest = await Promise.all( Array.from({ length: pageCount - 1 }, (_, index) => api .get>(`${basePath}/${id}/messages`, { params: { page: index + 2, limit: first.limit }, }) .then((response) => response.data), ), ); return { ...first, items: [first, ...rest].flatMap((page) => page.items) }; }, }; export function conversationStreamUrl(id: number): string { return `/api${basePath}/${id}/stream`; }