- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码 - 删除废弃的 AgentRuntimeBinding 模型及其依赖 - 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑 - 修复附件完整性检测、订单抽取状态映射问题 - 更新多语言错误文案与前端单元测试配置 - 清理平台配置中不再使用的暴露模式配置
144 lines
5.5 KiB
TypeScript
144 lines
5.5 KiB
TypeScript
/**
|
|
* features/chat/api/chat.api.ts
|
|
* 用途:封装聊天会话、消息、源文件与附件上传 HTTP API,并恢复结构化历史消息状态。
|
|
* 依赖:shared/api/client、鉴权工具、chat.types。
|
|
* 逻辑:页面 composable 调用本模块;历史消息响应在返回前将 ChatLog.payload 中的领域状态投影到 ChatMessage。
|
|
*/
|
|
import { getAuthHeaders } from '@/shared/utils/auth';
|
|
import { requestJson } from '@/shared/api/client';
|
|
import type {
|
|
AssetHistorySearchResult,
|
|
AssetImageResolveResult,
|
|
ChatCrossAgentHistoryItem,
|
|
ChatMessage,
|
|
ChatQueryPreviewPage,
|
|
ChatSessionItem,
|
|
ChatSourceFileResult,
|
|
ChatUploadResult,
|
|
OaEmployeesResponse,
|
|
ShippingOrderExtractionState,
|
|
} 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() });
|
|
|
|
/**
|
|
* 从 ChatLog.payload 恢复订单识别终态,保证刷新会话后与实时 SSE 消息形状一致。
|
|
* 核心逻辑:仅接受带合法 status 的对象;服务端仍是终态事实源,本函数不推导或改写计数。
|
|
* @param message - 历史接口返回的单条消息。
|
|
* @returns 原消息或已补充 shipping_order_extraction_state 的消息。
|
|
*/
|
|
export const hydrateShippingOrderExtractionState = (message: ChatMessage): ChatMessage => {
|
|
const payload = message.payload && typeof message.payload === 'object'
|
|
? message.payload as Record<string, unknown>
|
|
: {};
|
|
const state = payload.shipping_order_extraction_state;
|
|
if (!state || typeof state !== 'object') return message;
|
|
const status = String((state as Record<string, unknown>).status || '');
|
|
if (!['queued', 'processing', 'completed', 'partially_completed', 'review_required', 'failed', 'cancelled'].includes(status)) return message;
|
|
message.shipping_order_extraction_state = state as ShippingOrderExtractionState;
|
|
return message;
|
|
};
|
|
|
|
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')
|
|
.map(hydrateShippingOrderExtractionState)
|
|
: 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 const resolveAssetImagePreview = (
|
|
agentId: number,
|
|
query: { asset_id?: string; asset_name?: string },
|
|
) => {
|
|
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);
|
|
return requestJson<AssetImageResolveResult>(`/api/chat/asset-image/resolve?${qs.toString()}`, auth());
|
|
};
|
|
|
|
export type { ChatSessionItem, ChatCrossAgentHistoryItem };
|