feat(rpa): 实现库存源路由与 RPA Driver 方案
- 添加库存源解析逻辑,根据发货通知目的地解析 RPA 源 - 实现 DSV 360 和 Crane FootPrint 的库存源配置 - 增加自动登录功能,优化浏览器操作 - 更新本地启动脚本,支持跳过 Worker 启动 - 添加后端测试,验证库存源解析与任务取消逻辑 - 编写文档,描述库存源路由与 RPA Driver 方案
This commit is contained in:
commit
1921959fc1
61 files changed
+2347
-473
No files matched your search
@@ -99,6 +99,28 @@ export async function uploadWorkspaceShippingInventory(taskNo: string, file: Fil
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认当前任务的库存 RPA 来源。
|
||||
* @param taskNo - 发货任务编号
|
||||
* @param sourceCode - 已配置的库存源编码
|
||||
* @returns RPA 库存导出触发结果
|
||||
*/
|
||||
export async function confirmWorkspaceShippingInventorySource(taskNo: string, sourceCode: string) {
|
||||
const res = await fetch(
|
||||
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/confirm-inventory-source`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ source_code: sourceCode }),
|
||||
},
|
||||
);
|
||||
const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string };
|
||||
if (!res.ok || body.status === 'failed') {
|
||||
throw new Error(body.error || body.message || res.statusText);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交库存字段映射。
|
||||
* @param taskNo - 发货任务编号
|
||||
|
||||
@@ -103,45 +103,24 @@ const getMessageKey = (msg: ChatMessage, index: number) => (
|
||||
@regenerate="emit('regenerate')"
|
||||
@open-feedback="emit('open-feedback', index)"
|
||||
/>
|
||||
<ToolRunProgressCard
|
||||
v-if="shouldRenderMessage(msg) && shouldShowToolRunProgressAfterMessage(msg, index)"
|
||||
:run="getMessageRunProgress(msg)"
|
||||
:loading="isMessageRunProgressLoading(msg)"
|
||||
:waiting="false"
|
||||
:error="getMessageRunProgressError(msg)"
|
||||
:fallback-title="toolRunProgressTitle"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<template v-for="(msg, index) in messages" :key="index">
|
||||
<ChatMessageBubble
|
||||
v-if="shouldRenderMessage(msg)"
|
||||
:message="msg"
|
||||
:index="index"
|
||||
:message-count="messages.length"
|
||||
:is-thinking="isThinking"
|
||||
:show-chat-rating="showChatRating"
|
||||
:copied-index="copiedIndex"
|
||||
:is-shipping-notice-agent="isShippingNoticeAgent"
|
||||
:handlers="handlers"
|
||||
:leave-handlers="leaveHandlers"
|
||||
:shipping-handlers="shippingHandlers"
|
||||
@copy="emit('copy', { message: msg, index })"
|
||||
@edit="emit('edit', $event)"
|
||||
@resend="emit('resend', $event)"
|
||||
@regenerate="emit('regenerate')"
|
||||
@open-feedback="emit('open-feedback', index)"
|
||||
/>
|
||||
<ToolRunProgressCard
|
||||
v-if="shouldRenderMessage(msg) && shouldShowToolRunProgressAfterMessage(msg, index)"
|
||||
:run="getMessageRunProgress(msg)"
|
||||
:loading="isMessageRunProgressLoading(msg)"
|
||||
:waiting="false"
|
||||
:error="getMessageRunProgressError(msg)"
|
||||
:fallback-title="toolRunProgressTitle"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<ChatThinkingBubble v-if="showThinkingBubble" />
|
||||
<div v-if="messages.length > 0" class="h-2 md:h-3" />
|
||||
</div>
|
||||
<ChatThinkingBubble v-if="showThinkingBubble" />
|
||||
<div v-if="messages.length > 0" class="h-2 md:h-3" />
|
||||
</div>
|
||||
|
||||
<button
|
||||
v-if="showScrollToBottom"
|
||||
type="button"
|
||||
class="absolute bottom-4 left-1/2 z-10 flex h-10 w-10 -translate-x-1/2 items-center justify-center rounded-full border border-slate-100 bg-white text-[28px] font-light leading-none text-slate-500 shadow-[0_10px_28px_rgba(15,23,42,0.16)] transition hover:-translate-y-0.5 hover:border-slate-200 hover:text-slate-700 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-300 dark:shadow-black/30 md:bottom-5"
|
||||
class="sticky bottom-4 left-1/2 z-30 flex h-10 w-10 -translate-x-1/2 items-center justify-center rounded-full border border-slate-100 bg-white text-[28px] font-light leading-none text-slate-500 shadow-[0_10px_28px_rgba(15,23,42,0.16)] transition hover:-translate-y-0.5 hover:border-slate-200 hover:text-slate-700 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-300 dark:shadow-black/30 md:bottom-6"
|
||||
:title="$t('chat.scrollToLatest')"
|
||||
@click="emit('scroll-to-bottom')"
|
||||
>
|
||||
|
||||
@@ -290,14 +290,11 @@ export function useChat() {
|
||||
context?: { task_id?: string; batch_no?: string; display_query?: string },
|
||||
hooks?: SendMessageLifecycleHooks,
|
||||
) => {
|
||||
<<<<<<< HEAD
|
||||
const displayQuery = String(context?.display_query || query || '').trim();
|
||||
=======
|
||||
clearLikelyCompletedStreamTimer();
|
||||
cancelActiveStream();
|
||||
const controller = new AbortController();
|
||||
activeStreamController.value = controller;
|
||||
>>>>>>> dev
|
||||
const runViewId = viewId.value;
|
||||
const runMessages = messages.value;
|
||||
const run: ChatRun = {
|
||||
|
||||
@@ -66,7 +66,7 @@ export function resolveShippingNumericChoice(
|
||||
}
|
||||
|
||||
export function normalizeApiShippingButtons(
|
||||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string }> | null,
|
||||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string; source_code?: string }> | null,
|
||||
): ShippingActionButton[] {
|
||||
return (apiButtons || []).map((button) => ({
|
||||
...button,
|
||||
@@ -81,7 +81,7 @@ export function normalizeApiShippingButtons(
|
||||
*/
|
||||
export function resolveShippingActionButtons(
|
||||
content: string,
|
||||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string }> | null,
|
||||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string; source_code?: string }> | null,
|
||||
opts?: { apiResolved?: boolean },
|
||||
) {
|
||||
const directive = extractShippingActionsDirective(String(content || ''));
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
*/
|
||||
import {
|
||||
applyWorkspaceShippingInventoryMapping,
|
||||
confirmWorkspaceShippingInventorySource,
|
||||
generateWorkspaceShippingExcel,
|
||||
getWorkspaceShippingBatch,
|
||||
getWorkspaceShippingTask,
|
||||
switchWorkspaceShippingBatchFocusTask,
|
||||
} from '@/features/chat/api/shipping.api';
|
||||
import { SHIPPING_DECISION_ACTIONS, SHIPPING_MENU_ACTION_SPECS } from '../constants/shipping';
|
||||
@@ -79,6 +81,59 @@ export interface ShippingSendHandlerContext {
|
||||
* @returns 发送前文本与附件自动流程处理函数;返回 true 表示已消费本次发送
|
||||
*/
|
||||
export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
|
||||
const normalizeInventorySourceText = (value: string) =>
|
||||
String(value || '')
|
||||
.toUpperCase()
|
||||
.replace(/[^A-Z0-9]+/g, '');
|
||||
|
||||
const tryConfirmInventorySourceFromText = async (content: string): Promise<boolean> => {
|
||||
const taskId = ctx.getActiveShippingTaskId();
|
||||
if (!taskId) return false;
|
||||
const normalized = normalizeInventorySourceText(content);
|
||||
if (!normalized) return false;
|
||||
try {
|
||||
const detail = await getWorkspaceShippingTask(taskId);
|
||||
if (detail.pending_human_reason !== 'inventory_source_unresolved') return false;
|
||||
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 false;
|
||||
const hit = candidates.find((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) => normalized === value || normalized.includes(value) || value.includes(normalized));
|
||||
}) as { source_code?: unknown; display_name?: unknown } | undefined;
|
||||
const sourceCode = String(hit?.source_code || '').trim().toUpperCase();
|
||||
if (!sourceCode) return false;
|
||||
if (typeof content !== 'string') return false;
|
||||
ctx.input.value = '';
|
||||
ctx.attachments.value = [];
|
||||
const label = String(hit?.display_name || sourceCode).trim();
|
||||
await ctx.pushShippingLocalUserMessage(content.trim());
|
||||
const result = await confirmWorkspaceShippingInventorySource(taskId, sourceCode);
|
||||
await ctx.pushShippingLocalAssistantMessage(
|
||||
[
|
||||
String(result.message || '').trim(),
|
||||
`已确认库存查询来源:${label}`,
|
||||
`任务编号:${taskId}`,
|
||||
].filter(Boolean).join('\n'),
|
||||
);
|
||||
void ctx.refreshShippingActions();
|
||||
return true;
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : '确认库存源失败';
|
||||
ctx.msg.warning(errorMessage);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const trySwitchShippingTask = async (content: string): Promise<boolean> => {
|
||||
const intent = parseShippingTaskSwitchIntent(content);
|
||||
if (!intent) return false;
|
||||
@@ -187,6 +242,11 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
|
||||
ctx.attachments.value = [];
|
||||
return true;
|
||||
}
|
||||
if (await tryConfirmInventorySourceFromText(content)) {
|
||||
if (typeof textOrEvent !== 'string') ctx.input.value = '';
|
||||
ctx.attachments.value = [];
|
||||
return true;
|
||||
}
|
||||
const assistantCtx = ctx.lastAssistantMessageContent();
|
||||
const numericText = content.trim();
|
||||
if (/^[1-91-9](?:[\.\、\)::]?)$/.test(numericText)) {
|
||||
|
||||
@@ -31,6 +31,8 @@ export interface ShippingTaskSummary {
|
||||
step_payload?: Record<string, unknown> & {
|
||||
current_step_progress?: ShippingCurrentStepProgress;
|
||||
};
|
||||
rpa_inventory_source?: Record<string, unknown> | null;
|
||||
inventory_source_resolution?: Record<string, unknown> | null;
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
}
|
||||
@@ -104,6 +106,7 @@ export interface ShippingChatAction {
|
||||
label: string;
|
||||
message: string;
|
||||
variant: 'primary' | 'secondary' | 'danger';
|
||||
source_code?: string;
|
||||
}
|
||||
|
||||
export interface ShippingChatActionsResponse {
|
||||
|
||||
@@ -82,6 +82,7 @@ export interface ShippingMatchResultPayload {
|
||||
label: string;
|
||||
message: string;
|
||||
variant: 'primary' | 'secondary' | 'danger';
|
||||
source_code?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
@@ -90,4 +91,5 @@ export interface ShippingActionButton {
|
||||
label: string;
|
||||
message: string;
|
||||
variant?: ShippingActionVariant;
|
||||
source_code?: string;
|
||||
}
|
||||
Reference in new issue
Block a user