- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
473 lines
17 KiB
TypeScript
473 lines
17 KiB
TypeScript
/**
|
|
* features/chat/api/shipping.api.ts
|
|
* 用途:封装发货通知相关 HTTP API,包括任务/批次查询、库存上传、映射确认、人工决策与 Excel 导出。
|
|
* 依赖:统一 requestJson / BusinessError、shipping.api.types 类型定义。
|
|
* 逻辑:上游由 useChatShipping 与 shipping/* handler 调用本文件;本文件统一请求工作台后端 `/api/workspace/*` 路由并抛出标准错误。
|
|
*/
|
|
import { BusinessError, requestJson } from '@/shared/api/client';
|
|
import { reportShippingDebugEvent } from '../shipping/shippingDebugReporter';
|
|
import type {
|
|
ShippingBatchSummary,
|
|
ShippingBatchUiState,
|
|
ShippingConversationWorkspace,
|
|
ShippingDecisionResponse,
|
|
ShippingInventoryUploadResponse,
|
|
ShippingNoticeHeader,
|
|
ShippingNoticeLine,
|
|
ShippingNoticeUploadResponse,
|
|
ShippingTaskCreateResponse,
|
|
ShippingTaskDetail,
|
|
ShippingTaskSummary,
|
|
ShippingTaskUiState,
|
|
} from '../types/shipping.api.types';
|
|
|
|
type ShippingApiStatusPayload = {
|
|
status?: string;
|
|
error?: string;
|
|
message?: string;
|
|
code?: string;
|
|
details?: Record<string, unknown>;
|
|
request_id?: string;
|
|
};
|
|
|
|
export type ShippingChatAgentOption = {
|
|
id: number;
|
|
name: string;
|
|
};
|
|
|
|
function jsonPost(body?: unknown): RequestInit {
|
|
return {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body ?? {}),
|
|
};
|
|
}
|
|
|
|
function assertShippingOk<T extends ShippingApiStatusPayload>(body: T): T {
|
|
if (body.status === 'failed') {
|
|
throw new BusinessError({
|
|
message: body.error || body.message || 'Shipping request failed',
|
|
code: body.code || 'SHIPPING.REQUEST_FAILED',
|
|
details: body.details,
|
|
requestId: body.request_id,
|
|
});
|
|
}
|
|
return body;
|
|
}
|
|
|
|
// #region debug-point E:workspace-api
|
|
const reportRetryRpaDebug = (hypothesisId: string, msg: string, data: Record<string, unknown>) =>
|
|
reportShippingDebugEvent('shipping.api.ts', hypothesisId, msg, data);
|
|
// #endregion
|
|
|
|
export const listWorkspaceShippingTasks = (params?: { process_status?: string; limit?: number }) => {
|
|
/** 拉取当前用户可见的发货任务列表。 */
|
|
const sp = new URLSearchParams();
|
|
if (params?.process_status) sp.set('process_status', params.process_status);
|
|
if (params?.limit) sp.set('limit', String(params.limit));
|
|
const qs = sp.toString();
|
|
return requestJson<ShippingTaskSummary[]>(`/api/workspace/shipping-tasks${qs ? '?' + qs : ''}`);
|
|
};
|
|
|
|
export const listWorkspaceShippingBatches = (params?: { limit?: number }) => {
|
|
/** 拉取当前用户的发货批次摘要列表。 */
|
|
const sp = new URLSearchParams();
|
|
if (params?.limit) sp.set('limit', String(params.limit));
|
|
const qs = sp.toString();
|
|
return requestJson<ShippingBatchSummary[]>(`/api/workspace/shipping-batches${qs ? '?' + qs : ''}`);
|
|
};
|
|
|
|
/** 拉取用户可见 Agent 列表,供发货工作台跳转 Chat 时定位目标 Agent。 */
|
|
export const listShippingChatAgents = () => requestJson<ShippingChatAgentOption[]>('/api/agents');
|
|
|
|
/** 获取批次详情、批次任务列表与当前焦点任务。 */
|
|
export const getWorkspaceShippingBatch = (batchNo: string) =>
|
|
requestJson<ShippingBatchUiState>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}`,
|
|
);
|
|
|
|
/** 获取批次级 UI State,供工作台、步骤条和任务卡片统一消费。 */
|
|
export const getWorkspaceShippingBatchUiState = (batchNo: string) =>
|
|
requestJson<ShippingBatchUiState>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/ui-state`,
|
|
);
|
|
|
|
/** 获取一个 Agent 会话下的多批次执行工作台快照。 */
|
|
export const getWorkspaceShippingConversationBatches = (conversationId: string) =>
|
|
requestJson<ShippingConversationWorkspace>(
|
|
`/api/workspace/shipping-conversations/${encodeURIComponent(conversationId)}/batch-workspace`,
|
|
);
|
|
|
|
/** 获取指定批次当前焦点任务。 */
|
|
export const getWorkspaceShippingBatchFocusTask = (batchNo: string) =>
|
|
requestJson<ShippingTaskSummary>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/focus-task`,
|
|
);
|
|
|
|
/** 获取单个发货任务详情。 */
|
|
export const getWorkspaceShippingTask = (taskNo: string) =>
|
|
requestJson<ShippingTaskDetail>(`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}`);
|
|
|
|
/** 获取单票任务 UI State,后续 Chat 按钮与顶部步骤条统一读取此状态。 */
|
|
export const getWorkspaceShippingTaskUiState = (taskNo: string) =>
|
|
requestJson<ShippingTaskUiState>(`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/ui-state`);
|
|
|
|
/** 生成任务导出文件的前端下载 URL。 */
|
|
export const workspaceShippingExportUrl = (taskNo: string) =>
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/export`;
|
|
|
|
/**
|
|
* 上传库存 Excel,并由后端完成解析与 schema preview 生成。
|
|
* @param taskNo - 发货任务编号
|
|
* @param file - 用户选择的库存文件
|
|
* @returns 库存上传与解析结果
|
|
*/
|
|
export async function uploadWorkspaceShippingInventory(taskNo: string, file: File) {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
const body = await requestJson<ShippingInventoryUploadResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/upload-inventory`,
|
|
{ method: 'POST', body: form },
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 确认当前任务的库存 RPA 来源。
|
|
* @param taskNo - 发货任务编号
|
|
* @param sourceCode - 已配置的库存源编码
|
|
* @returns RPA 库存导出触发结果
|
|
*/
|
|
export async function confirmWorkspaceShippingInventorySource(taskNo: string, sourceCode: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/confirm-inventory-source`,
|
|
jsonPost({ source_code: sourceCode }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 提交库存字段映射。
|
|
* @param taskNo - 发货任务编号
|
|
* @param columnMappingJson - 前端表单确认后的 map_json
|
|
* @returns 映射应用与库存解析结果
|
|
*/
|
|
export async function applyWorkspaceShippingInventoryMapping(taskNo: string, columnMappingJson: Record<string, unknown>) {
|
|
const body = await requestJson<ShippingInventoryUploadResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/apply-inventory-mapping`,
|
|
jsonPost({ map_json: columnMappingJson }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 创建单个发货任务。
|
|
* @param params - 建单参数,可包含 source_type、conversation_id、raw_content
|
|
* @returns 建单结果
|
|
*/
|
|
export async function createWorkspaceShippingTask(params?: {
|
|
source_type?: string;
|
|
conversation_id?: string;
|
|
raw_content?: string;
|
|
}) {
|
|
const body = await requestJson<ShippingTaskCreateResponse & ShippingApiStatusPayload>(
|
|
'/api/workspace/shipping-tasks',
|
|
jsonPost(params || {}),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 上传单个发货通知附件并立即建单解析。
|
|
* @param file - 发货通知文件
|
|
* @param conversationId - 可选会话 ID,用于把任务绑定到聊天会话
|
|
* @returns 建单与解析结果
|
|
*/
|
|
export async function createWorkspaceShippingTaskFromNotice(file: File, conversationId?: string) {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
if (conversationId) form.append('conversation_id', conversationId);
|
|
const body = await requestJson<ShippingNoticeUploadResponse & ShippingApiStatusPayload>(
|
|
'/api/workspace/shipping-tasks/create-from-notice',
|
|
{ method: 'POST', body: form },
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 重新上传发货通知并覆盖当前任务。
|
|
* @param taskNo - 需要覆盖的发货任务编号
|
|
* @param file - 新的发货通知文件
|
|
* @returns 覆盖后的任务状态与后台编排结果
|
|
*/
|
|
export async function replaceWorkspaceShippingTaskNotice(taskNo: string, file: File) {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
const body = await requestJson<ShippingNoticeUploadResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/replace-notice`,
|
|
{ method: 'POST', body: form },
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 批量上传多份发货通知并创建批次。
|
|
* @param files - 发货通知附件列表
|
|
* @param conversationId - 可选会话 ID
|
|
* @returns 批次与任务创建结果
|
|
*/
|
|
export async function createWorkspaceShippingBatchFromNotices(files: File[], conversationId?: string) {
|
|
const form = new FormData();
|
|
files.forEach((file) => form.append('files', file));
|
|
if (conversationId) form.append('conversation_id', conversationId);
|
|
const body = await requestJson<ShippingNoticeUploadResponse & ShippingApiStatusPayload & {
|
|
error?: string;
|
|
batch_no?: string;
|
|
data?: unknown;
|
|
}>('/api/workspace/shipping-batches/create-from-notices', { method: 'POST', body: form });
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 切换批次当前焦点任务。
|
|
* @param batchNo - 批次编号
|
|
* @param taskNo - 目标任务编号
|
|
* @returns 更新后的批次摘要与焦点任务
|
|
*/
|
|
export async function switchWorkspaceShippingBatchFocusTask(batchNo: string, taskNo: string) {
|
|
const body = await requestJson<ShippingApiStatusPayload & {
|
|
batch?: ShippingBatchSummary;
|
|
focus_task?: ShippingTaskSummary;
|
|
}>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/focus-task`,
|
|
jsonPost({ task_no: taskNo }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 重试当前发货任务。
|
|
* @param taskNo - 发货任务编号
|
|
* @param comment - 可选重试备注
|
|
* @returns 重试结果
|
|
*/
|
|
export async function retryWorkspaceShippingTask(taskNo: string, comment?: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/retry`,
|
|
jsonPost(comment ? { comment } : {}),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 重试批次中仍可自动推进的任务。
|
|
* @param batchNo - 批次编号
|
|
* @returns 批次重试结果
|
|
*/
|
|
export async function retryWorkspaceShippingBatchPending(batchNo: string) {
|
|
const body = await requestJson<ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-batches/${encodeURIComponent(batchNo)}/retry-pending`,
|
|
jsonPost(),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 提交人工决策。
|
|
* @param taskNo - 发货任务编号
|
|
* @param decision - 决策 ID,如 confirm、reject、reparse
|
|
* @param comment - 可选备注
|
|
* @returns 决策执行结果
|
|
*/
|
|
export async function submitWorkspaceShippingDecision(taskNo: string, decision: string, comment?: string) {
|
|
// #region debug-point E:submit-decision-entry
|
|
void reportRetryRpaDebug('E', 'submitWorkspaceShippingDecision entry', {
|
|
taskNo,
|
|
decision,
|
|
hasComment: Boolean(comment),
|
|
});
|
|
// #endregion
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/submit-decision`,
|
|
jsonPost({ decision, ...(comment ? { comment } : {}) }),
|
|
);
|
|
// #region debug-point E:submit-decision-response
|
|
void reportRetryRpaDebug('E', 'submitWorkspaceShippingDecision response', {
|
|
taskNo,
|
|
decision,
|
|
status: String(body?.status || ''),
|
|
message: String(body?.message || ''),
|
|
code: String(body?.code || ''),
|
|
});
|
|
// #endregion
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 更新发货通知单头字段。
|
|
* @param taskNo - 发货任务编号
|
|
* @param fields - 需要更新的单头字段
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function updateWorkspaceShippingNoticeFields(
|
|
taskNo: string,
|
|
fields: Partial<ShippingNoticeHeader>,
|
|
comment?: string,
|
|
) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/update-notice-fields`,
|
|
jsonPost({ fields, ...(comment ? { comment } : {}) }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 更新指定订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lineIndex - 订单行下标
|
|
* @param patch - 行字段补丁
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function updateWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
lineIndex: number,
|
|
patch: Partial<ShippingNoticeLine> & { quantity?: number },
|
|
comment?: string,
|
|
) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/update-notice-line`,
|
|
jsonPost({ line_index: lineIndex, patch, ...(comment ? { comment } : {}) }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 为发货通知新增订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param line - 新增的订单行内容
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function addWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
line: Partial<ShippingNoticeLine> & { quantity?: number },
|
|
comment?: string,
|
|
) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/add-notice-line`,
|
|
jsonPost({ line, ...(comment ? { comment } : {}) }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 删除指定订单行。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lineIndex - 待删除的订单行下标
|
|
* @param comment - 可选操作备注
|
|
* @returns 更新结果
|
|
*/
|
|
export async function removeWorkspaceShippingNoticeLine(
|
|
taskNo: string,
|
|
lineIndex: number,
|
|
comment?: string,
|
|
) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/remove-notice-line`,
|
|
jsonPost({ line_index: lineIndex, ...(comment ? { comment } : {}) }),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 执行库存匹配。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 匹配结果与后续动作提示
|
|
*/
|
|
export async function matchWorkspaceShippingInventory(taskNo: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/match-inventory`,
|
|
jsonPost(),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 以用户态触发自动推进,作为聊天前端兜底入口。
|
|
* @param taskNo - 发货任务编号
|
|
* @param lang - 可选语言代码
|
|
* @returns 自动推进结果
|
|
*/
|
|
export async function autoProgressWorkspaceShippingTask(taskNo: string, lang?: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/auto-progress`,
|
|
jsonPost(lang ? { lang } : {}),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 标记货代系统已人工上传完成。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 上传确认结果
|
|
*/
|
|
export async function markWorkspaceShippingManualUploaded(taskNo: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/mark-manual-uploaded`,
|
|
jsonPost(),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 重试将发货 Excel 上传至货代系统。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns RPA 上传触发或等待结果
|
|
*/
|
|
export async function retryWorkspaceShippingUpload(taskNo: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/retry-upload`,
|
|
jsonPost(),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/**
|
|
* 生成发货 Excel。
|
|
* @param taskNo - 发货任务编号
|
|
* @returns 生成结果,成功时通常会包含下载链接
|
|
*/
|
|
export async function generateWorkspaceShippingExcel(taskNo: string) {
|
|
const body = await requestJson<ShippingDecisionResponse & ShippingApiStatusPayload>(
|
|
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/generate-excel`,
|
|
jsonPost(),
|
|
);
|
|
return assertShippingOk(body);
|
|
}
|
|
|
|
/** 货代发货 Agent 显示名称,用于聊天路由与 Agent 匹配。 */
|
|
export const SHIPPING_NOTICE_AGENT_NAME = '货代发货通知 Agent';
|
|
|
|
export type {
|
|
ShippingBatchSummary,
|
|
ShippingBatchUiState,
|
|
ShippingChatAction,
|
|
ShippingConversationBatch,
|
|
ShippingConversationWorkspace,
|
|
ShippingConversationWorkspaceSummary,
|
|
ShippingCurrentStepProgress,
|
|
ShippingDecisionResponse,
|
|
ShippingInventoryUploadResponse,
|
|
ShippingMatchLineResult,
|
|
ShippingNoticeHeader,
|
|
ShippingNoticeLine,
|
|
ShippingNoticeUploadResponse,
|
|
ShippingTaskCreateResponse,
|
|
ShippingTaskDetail,
|
|
ShippingTaskSummary,
|
|
ShippingTaskUiState,
|
|
ShippingWorkflowStep,
|
|
} from '../types/shipping.api.types';
|