feat(rpa): 实现库存源路由与 RPA Driver 方案

- 添加库存源解析逻辑,根据发货通知目的地解析 RPA 源
- 实现 DSV 360 和 Crane FootPrint 的库存源配置
- 增加自动登录功能,优化浏览器操作
- 更新本地启动脚本,支持跳过 Worker 启动
- 添加后端测试,验证库存源解析与任务取消逻辑
- 编写文档,描述库存源路由与 RPA Driver 方案
This commit is contained in:
hugo committed 2026-06-29 20:34:49 +08:00
commit 1921959fc1
61 files changed
+2347 -473

No files matched your search

@@ -5,7 +5,9 @@
* 逻辑:由 useChatShipping 调用本文件;本文件根据 action.id 决定触发后端接口、本地消息落库或前端 UI 行为。
*/
import {
confirmWorkspaceShippingInventorySource,
generateWorkspaceShippingExcel,
getWorkspaceShippingTask,
matchWorkspaceShippingInventory,
retryWorkspaceShippingUpload,
type ShippingDecisionResponse,
@@ -14,7 +16,7 @@ import {
import { SHIPPING_DECISION_ACTIONS } from '../constants/shipping';
import { downloadWorkspaceShippingExcelFile } from './shippingDownload';
import type { ChatMessage } from '../types/chat.types';
import type { ShippingMatchResultPayload } from '../types/shipping.types';
import type { ShippingActionButton, ShippingMatchResultPayload } from '../types/shipping.types';
import type { Ref } from 'vue';
export interface ShippingMenuHandlerContext {
@@ -42,9 +44,53 @@ export interface ShippingMenuHandlerContext {
* @returns 菜单动作与按钮点击处理函数
*/
export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
const normalizeInventorySourceText = (value: string) =>
String(value || '')
.toUpperCase()
.replace(/[^A-Z0-9]+/g, '');
const recentInventorySourceHint = () => {
const parts: string[] = [];
for (let i = ctx.messages.value.length - 1; i >= 0 && parts.length < 6; i -= 1) {
const content = String(ctx.messages.value[i]?.content || '').trim();
if (content) parts.push(content);
}
return parts.join('\n');
};
const resolveInventorySourceCodeFromContext = async (taskId: string): Promise<string> => {
const detail = await getWorkspaceShippingTask(taskId);
const resolution = detail.inventory_source_resolution;
const candidates = resolution && typeof resolution === 'object'
? (resolution as { candidates?: unknown }).candidates
: null;
if (!Array.isArray(candidates) || candidates.length === 0) return '';
if (candidates.length === 1) {
const only = candidates[0];
return only && typeof only === 'object'
? String((only as { source_code?: unknown }).source_code || '').trim().toUpperCase()
: '';
}
const hint = normalizeInventorySourceText(recentInventorySourceHint());
if (!hint) return '';
const matches = candidates.filter((item) => {
if (!item || typeof item !== 'object') return false;
const source = item as { source_code?: unknown; display_name?: unknown; warehouse_code?: unknown; program_name?: unknown };
const values = [
source.source_code,
source.display_name,
source.warehouse_code,
source.program_name,
].map((value) => normalizeInventorySourceText(String(value || ''))).filter(Boolean);
return values.some((value) => hint.includes(value) || value.includes(hint));
});
if (matches.length !== 1) return '';
return String((matches[0] as { source_code?: unknown }).source_code || '').trim().toUpperCase();
};
const buildMatchResultPayload = (
taskId: string,
result: ShippingDecisionResponse & { action_buttons?: Array<{ id: string; label: string; message: string; variant?: 'primary' | 'secondary' | 'danger' }> },
result: ShippingDecisionResponse & { action_buttons?: Array<{ id: string; label: string; message: string; variant?: 'primary' | 'secondary' | 'danger'; source_code?: string }> },
): ShippingMatchResultPayload => {
const data = (result?.data && typeof result.data === 'object') ? result.data as Record<string, unknown> : {};
const lineResults = Array.isArray(data.line_results)
@@ -68,7 +114,7 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
};
const handleShippingMenuAction = async (
action: { id?: string; message: string },
action: ShippingActionButton | { id?: string; message: string },
chatMsg?: ChatMessage & { shipping_actions_used?: boolean },
) => {
if (ctx.isThinking.value) return;
@@ -163,6 +209,57 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
}
return;
}
if (action.id === 'confirm_inventory_source') {
if (chatMsg?.shipping_actions_used) return;
if (chatMsg) chatMsg.shipping_actions_used = true;
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
return;
}
try {
let sourceCode = String((action as { source_code?: unknown }).source_code || '').trim().toUpperCase();
if (!sourceCode) {
sourceCode = await resolveInventorySourceCodeFromContext(taskId);
}
if (!sourceCode) {
ctx.msg.warning('请直接输入要使用的库存源线索,例如 AWSDU、Dublin 或 Singapore。');
if (chatMsg) chatMsg.shipping_actions_used = false;
return;
}
const result = await confirmWorkspaceShippingInventorySource(taskId, sourceCode);
const label = String(('label' in action ? action.label : '') || sourceCode).trim();
const userText = `确认库存查询来源:${label}`;
const userMsg: ChatMessage = {
role: 'user',
type: 'text',
content: `${userText}\n任务编号:${taskId}`,
local_id: ctx.newLocalMessageId('shipping-user'),
local_only: true,
};
const assistantMsg: ChatMessage = {
role: 'assistant',
type: 'text',
content: String(result.message || '').trim(),
local_id: ctx.newLocalMessageId('shipping-assistant'),
local_only: true,
};
ctx.messages.value.push(userMsg, assistantMsg);
await ctx.saveLocalMessageToBackend(userMsg, userText);
await ctx.saveLocalMessageToBackend(assistantMsg, userText);
void ctx.refreshShippingActions();
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '确认库存源失败';
ctx.msg.warning(errorMessage);
if (chatMsg) chatMsg.shipping_actions_used = false;
}
return;
}
if (action.id === 'retry_resolve_inventory_source') {
await ctx.refreshShippingActions();
ctx.msg.info('已重新刷新库存源状态');
return;
}
if (action.id === 'edit_inventory_mapping') {
const taskId = ctx.getActiveShippingTaskId();
if (!taskId) {
@@ -246,7 +343,7 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
const handleShippingActionClick = async (
chatMsg: ChatMessage & { shipping_actions_used?: boolean },
action: { id?: string; message: string },
action: ShippingActionButton,
) => {
await handleShippingMenuAction(action, chatMsg);
};