108 lines
3.7 KiB
TypeScript
108 lines
3.7 KiB
TypeScript
/**
|
|
* features/chat/api/chat.api.ts
|
|
* 聊天域 HTTP API:会话、消息、源文件、附件上传。
|
|
*/
|
|
import { getAuthHeaders } from '@/shared/utils/auth';
|
|
import { requestJson } from '@/shared/api/client';
|
|
import type {
|
|
AssetHistorySearchResult,
|
|
ChatCrossAgentHistoryItem,
|
|
ChatMessage,
|
|
ChatQueryPreviewPage,
|
|
ChatSessionItem,
|
|
ChatSourceFileResult,
|
|
ChatUploadResult,
|
|
OaEmployeesResponse,
|
|
} from '../types/chat.types';
|
|
|
|
const auth = () => ({ headers: getAuthHeaders() });
|
|
const jsonAuth = () => ({ headers: { 'Content-Type': 'application/json', ...getAuthHeaders() } });
|
|
|
|
export const listChatSessions = (agentId: number) =>
|
|
requestJson<ChatSessionItem[]>(`/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 = async (conversationId: string, agentId: number) => {
|
|
const messages = await requestJson<ChatMessage[]>(`/api/chat/messages?conversation_id=${conversationId}&agent_id=${agentId}`, auth());
|
|
return Array.isArray(messages)
|
|
? messages.filter((message) => message?.type !== 'shipping-parse-review')
|
|
: messages;
|
|
};
|
|
|
|
export const saveLocalChatMessage = (payload: Record<string, unknown>) =>
|
|
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<ChatSourceFileResult>(`/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<OaEmployeesResponse>(`/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<ChatUploadResult>(`/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<ChatQueryPreviewPage>(`/api/chat/query-preview/${chatLogId}?${qs.toString()}`, auth());
|
|
};
|
|
|
|
export const searchAssetHistoryFiles = (
|
|
agentId: number,
|
|
query: { asset_id?: string; asset_name?: string; limit?: number },
|
|
) => {
|
|
const qs = new URLSearchParams({
|
|
agent_id: String(agentId),
|
|
});
|
|
if (query.asset_id) qs.set('asset_id', query.asset_id);
|
|
if (query.asset_name) qs.set('asset_name', query.asset_name);
|
|
if (query.limit) qs.set('limit', String(query.limit));
|
|
return requestJson<AssetHistorySearchResult>(`/api/chat/asset-history/search?${qs.toString()}`, auth());
|
|
};
|
|
|
|
export type { ChatSessionItem, ChatCrossAgentHistoryItem };
|