forked from wangziqi/gongxue-base
feat: 集成 AI 对话与只读查询工具
This commit is contained in:
36
apps/admin/src/components/AiChat/api.ts
Normal file
36
apps/admin/src/components/AiChat/api.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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`;
|
||||
}
|
||||
Reference in New Issue
Block a user