37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import api from '../../api';
|
|
import type { AiApiResponse, AiConversation, AiMessagePage } from './types';
|
|
|
|
const basePath = '/ai/chat/conversations';
|
|
|
|
export const aiChatApi = {
|
|
listConversations: async () => (await api.get<AiApiResponse<AiConversation[]>>(basePath)).data,
|
|
createConversation: async (title?: string) =>
|
|
(await api.post<AiApiResponse<AiConversation>>(basePath, title ? { title } : {})).data,
|
|
renameConversation: async (id: number, title: string) =>
|
|
(await api.patch<AiApiResponse<AiConversation>>(`${basePath}/${id}`, { title })).data,
|
|
deleteConversation: (id: number) => api.delete<void>(`${basePath}/${id}`),
|
|
listMessages: async (id: number): Promise<AiMessagePage> => {
|
|
const first = (
|
|
await api.get<AiApiResponse<AiMessagePage>>(`${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<AiApiResponse<AiMessagePage>>(`${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`;
|
|
}
|