feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持
This commit is contained in:
1 parent
ac4262cd75
commit
c8ce77199e
183 files changed
+11690
-2300
No files matched your search
@@ -1,30 +1,24 @@
|
||||
/**
|
||||
* features/chat/shipping/useChatShippingMenuHandler.ts
|
||||
* 用途:分发聊天气泡动作按钮点击,连接 API、决策处理、下载和文件选择器。
|
||||
* 依赖:shipping.api、shippingDownload、shipping 常量、chat.types。
|
||||
* 逻辑:由 useChatShipping 调用本文件;本文件根据 action.id 决定触发后端接口、本地消息落库或前端 UI 行为。
|
||||
* 用途:分发聊天气泡动作按钮点击;业务动作统一走 Chat Turn SSE。
|
||||
* 依赖:shippingTurnActions、shipping.api(只读 task 详情)、shippingDownload、chat.types。
|
||||
* 逻辑:可映射为 action_type 的按钮走 startChatTurn;纯 UI(下载/选文件/表单)仍走本地行为。
|
||||
*/
|
||||
import {
|
||||
confirmWorkspaceShippingInventorySource,
|
||||
generateWorkspaceShippingExcel,
|
||||
getWorkspaceShippingTask,
|
||||
matchWorkspaceShippingInventory,
|
||||
retryWorkspaceShippingUpload,
|
||||
type ShippingDecisionResponse,
|
||||
type ShippingMatchLineResult,
|
||||
} from '@/features/chat/api/shipping.api';
|
||||
import { getWorkspaceShippingTask } from '@/features/chat/api/shipping.api';
|
||||
import { SHIPPING_DECISION_ACTIONS } from '../constants/shipping';
|
||||
import { downloadWorkspaceShippingExcelFile } from './shippingDownload';
|
||||
import { buildShippingTurnRefs, resolveShippingChatTurn } from './shippingTurnActions';
|
||||
import type { StartChatTurnOptions } from '../types/chat-turn.types';
|
||||
import type { ChatMessage } from '../types/chat.types';
|
||||
import type { ShippingActionButton, ShippingMatchResultPayload } from '../types/shipping.types';
|
||||
import type { Ref } from 'vue';
|
||||
import type { ShippingActionButton } from '../types/shipping.types';
|
||||
import type { ComputedRef, Ref } from 'vue';
|
||||
|
||||
export interface ShippingMenuHandlerContext {
|
||||
isThinking: Ref<boolean>;
|
||||
currentAgentId: ComputedRef<number>;
|
||||
getActiveShippingTaskId: () => string;
|
||||
getActiveShippingBatchNo: () => string;
|
||||
messages: Ref<ChatMessage[]>;
|
||||
newLocalMessageId: (prefix: string) => string;
|
||||
saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise<void>;
|
||||
sendShippingMenuMessage: (message: string) => Promise<void>;
|
||||
openShippingInventoryMappingFormForTask: (taskId: string) => Promise<boolean>;
|
||||
openShippingInventoryPicker: (taskId?: string) => boolean;
|
||||
@@ -36,6 +30,7 @@ export interface ShippingMenuHandlerContext {
|
||||
) => Promise<boolean>;
|
||||
executeShippingManualUpload: (taskId: string, displayText?: string) => Promise<boolean>;
|
||||
refreshShippingActions: () => Promise<void>;
|
||||
startChatTurn: (options: StartChatTurnOptions) => Promise<boolean>;
|
||||
msg: { warning: (text: string) => void; info: (text: string) => void };
|
||||
t: (key: string) => string;
|
||||
}
|
||||
@@ -46,10 +41,6 @@ export interface HandleShippingMenuActionOptions {
|
||||
|
||||
/**
|
||||
* 创建发货通知动作菜单处理器。
|
||||
*
|
||||
* 核心逻辑:按 action.id 分流到生成 Excel、库存匹配、人工决策、手工上传确认等前后端动作。
|
||||
* @param ctx - 动作菜单上下文
|
||||
* @returns 菜单动作与按钮点击处理函数
|
||||
*/
|
||||
export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
|
||||
const normalizeInventorySourceText = (value: string) =>
|
||||
@@ -96,29 +87,32 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
|
||||
return String((matches[0] as { source_code?: unknown }).source_code || '').trim().toUpperCase();
|
||||
};
|
||||
|
||||
const buildMatchResultPayload = (
|
||||
const runShippingActionTurn = async (
|
||||
action: ShippingActionButton | { id?: string; message: string; label?: string },
|
||||
taskId: string,
|
||||
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)
|
||||
? data.line_results as ShippingMatchLineResult[]
|
||||
: [];
|
||||
const shortageRaw = data.shortage && typeof data.shortage === 'object'
|
||||
? data.shortage as { lines?: ShippingMatchLineResult[] }
|
||||
: null;
|
||||
return {
|
||||
taskId,
|
||||
match_status: typeof data.match_status === 'string' ? data.match_status : null,
|
||||
line_results: lineResults,
|
||||
shortage_lines: Array.isArray(shortageRaw?.lines) ? shortageRaw.lines : [],
|
||||
action_buttons: Array.isArray(result.action_buttons)
|
||||
? result.action_buttons.map((action) => ({
|
||||
...action,
|
||||
variant: action.variant || 'secondary',
|
||||
}))
|
||||
: [],
|
||||
};
|
||||
extra?: { sourceCode?: string },
|
||||
) => {
|
||||
const actionId = String(action.id || '').trim();
|
||||
const turnSpec = resolveShippingChatTurn(actionId, taskId, extra);
|
||||
if (!turnSpec) return false;
|
||||
const displayBase = String(action.message || actionId || '操作').trim();
|
||||
const displayText = actionId === 'confirm_inventory_source'
|
||||
? `${displayBase}\n任务编号:${taskId}`
|
||||
: `${displayBase}(任务编号:${taskId})`;
|
||||
await ctx.startChatTurn({
|
||||
agentId: ctx.currentAgentId.value,
|
||||
inputType: 'action',
|
||||
displayText,
|
||||
actionType: turnSpec.actionType,
|
||||
payload: turnSpec.payload,
|
||||
refs: buildShippingTurnRefs(taskId, ctx.getActiveShippingBatchNo()),
|
||||
hooks: {
|
||||
onAssistantMessageFinished: async () => {
|
||||
void ctx.refreshShippingActions();
|
||||
},
|
||||
},
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleShippingMenuAction = async (
|
||||
@@ -129,6 +123,7 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
|
||||
const explicitTaskId = String(options?.taskId || '').trim().toUpperCase();
|
||||
const resolveTaskId = () => explicitTaskId || ctx.getActiveShippingTaskId();
|
||||
if (ctx.isThinking.value) return;
|
||||
|
||||
if (action.id === 'refresh') {
|
||||
await ctx.refreshShippingActions();
|
||||
ctx.msg.info(ctx.t('chat.shippingNoticeRefreshDone'));
|
||||
@@ -143,129 +138,61 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
|
||||
try {
|
||||
await downloadWorkspaceShippingExcelFile(taskId);
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : '下载 Excel 失败';
|
||||
ctx.msg.warning(errorMessage);
|
||||
ctx.msg.warning(e instanceof Error ? e.message : '下载 Excel 失败');
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (action.id === 'generate_excel') {
|
||||
|
||||
const chatTurnActionIds = new Set([
|
||||
'generate_excel',
|
||||
'retry_upload',
|
||||
'match_inventory',
|
||||
'mark_manual_uploaded',
|
||||
'confirm_inventory_source',
|
||||
]);
|
||||
|
||||
if (action.id && chatTurnActionIds.has(action.id)) {
|
||||
if (chatMsg?.shipping_actions_used) return;
|
||||
if (chatMsg) chatMsg.shipping_actions_used = true;
|
||||
const taskId = resolveTaskId();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
||||
if (chatMsg) chatMsg.shipping_actions_used = false;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await generateWorkspaceShippingExcel(taskId);
|
||||
const userMsg: ChatMessage = {
|
||||
role: 'user',
|
||||
type: 'text',
|
||||
content: `${action.message || '生成发货 Excel'}(任务编号:${taskId})`,
|
||||
local_id: ctx.newLocalMessageId('shipping-user'),
|
||||
local_only: true,
|
||||
};
|
||||
const assistantMsg: ChatMessage = {
|
||||
role: 'assistant',
|
||||
type: 'text',
|
||||
content: [
|
||||
String(result.message || '').trim(),
|
||||
`任务编号:${taskId}`,
|
||||
].filter(Boolean).join('\n'),
|
||||
local_id: ctx.newLocalMessageId('shipping-assistant'),
|
||||
local_only: true,
|
||||
};
|
||||
ctx.messages.value.push(userMsg, assistantMsg);
|
||||
await ctx.saveLocalMessageToBackend(userMsg, action.message || '生成发货 Excel');
|
||||
await ctx.saveLocalMessageToBackend(assistantMsg);
|
||||
void ctx.refreshShippingActions();
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : '生成发货 Excel 失败';
|
||||
ctx.msg.warning(errorMessage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (action.id === 'retry_upload') {
|
||||
if (chatMsg?.shipping_actions_used) return;
|
||||
if (chatMsg) chatMsg.shipping_actions_used = true;
|
||||
const taskId = resolveTaskId();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await retryWorkspaceShippingUpload(taskId);
|
||||
const userText = action.message || ctx.t('chat.shippingNoticeRetryUploadAction');
|
||||
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 || ctx.t('chat.shippingNoticeRetryUploadQueued')).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);
|
||||
void ctx.refreshShippingActions();
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : ctx.t('chat.shippingNoticeRetryUploadFailed');
|
||||
ctx.msg.warning(errorMessage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (action.id === 'confirm_inventory_source') {
|
||||
if (chatMsg?.shipping_actions_used) return;
|
||||
if (chatMsg) chatMsg.shipping_actions_used = true;
|
||||
const taskId = resolveTaskId();
|
||||
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);
|
||||
let sourceCode = '';
|
||||
if (action.id === 'confirm_inventory_source') {
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (!sourceCode) {
|
||||
ctx.msg.warning('请直接输入要使用的库存源线索,例如 AWSDU、Dublin 或 Singapore。');
|
||||
if (chatMsg) chatMsg.shipping_actions_used = false;
|
||||
if (action.id === 'mark_manual_uploaded') {
|
||||
await ctx.executeShippingManualUpload(taskId, '我已在货代系统完成上传');
|
||||
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);
|
||||
void ctx.refreshShippingActions();
|
||||
const displayMessage = action.id === 'confirm_inventory_source'
|
||||
? `确认库存查询来源:${label || sourceCode}`
|
||||
: action.message;
|
||||
await runShippingActionTurn(
|
||||
{ ...action, message: displayMessage },
|
||||
taskId,
|
||||
sourceCode ? { sourceCode } : undefined,
|
||||
);
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : '确认库存源失败';
|
||||
ctx.msg.warning(errorMessage);
|
||||
ctx.msg.warning(e instanceof Error ? e.message : '操作失败');
|
||||
if (chatMsg) chatMsg.shipping_actions_used = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (action.id === 'retry_resolve_inventory_source') {
|
||||
await ctx.refreshShippingActions();
|
||||
ctx.msg.info('已重新刷新库存源状态');
|
||||
@@ -288,55 +215,6 @@ export function createShippingMenuHandlers(ctx: ShippingMenuHandlerContext) {
|
||||
ctx.openShippingNoticePicker(resolveTaskId());
|
||||
return;
|
||||
}
|
||||
if (action.id === 'match_inventory') {
|
||||
if (chatMsg?.shipping_actions_used) return;
|
||||
if (chatMsg) chatMsg.shipping_actions_used = true;
|
||||
const taskId = resolveTaskId();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await matchWorkspaceShippingInventory(taskId);
|
||||
const userMsg: ChatMessage = {
|
||||
role: 'user',
|
||||
type: 'text',
|
||||
content: `${action.message || '匹配库存'}(任务编号:${taskId})`,
|
||||
local_id: ctx.newLocalMessageId('shipping-user'),
|
||||
local_only: true,
|
||||
};
|
||||
const assistantMsg: ChatMessage = {
|
||||
role: 'assistant',
|
||||
type: 'shipping-match-result',
|
||||
content: String(result.message || '').trim(),
|
||||
payload: buildMatchResultPayload(taskId, result),
|
||||
local_id: ctx.newLocalMessageId('shipping-assistant'),
|
||||
local_only: true,
|
||||
};
|
||||
ctx.messages.value.push(userMsg, assistantMsg);
|
||||
await ctx.saveLocalMessageToBackend(userMsg, action.message || '匹配库存');
|
||||
await ctx.saveLocalMessageToBackend(assistantMsg);
|
||||
void ctx.refreshShippingActions();
|
||||
} catch (e: unknown) {
|
||||
const errorMessage = e instanceof Error ? e.message : '匹配库存失败';
|
||||
ctx.msg.warning(errorMessage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (action.id === 'mark_manual_uploaded') {
|
||||
if (chatMsg?.shipping_actions_used) return;
|
||||
if (chatMsg) {
|
||||
chatMsg.shipping_actions_used = true;
|
||||
chatMsg.shipping_actions = undefined;
|
||||
}
|
||||
const taskId = resolveTaskId();
|
||||
if (!taskId) {
|
||||
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
||||
return;
|
||||
}
|
||||
await ctx.executeShippingManualUpload(taskId, '我已在货代系统完成上传');
|
||||
return;
|
||||
}
|
||||
if (action.id === 'paste_inventory_json') {
|
||||
ctx.msg.info(ctx.t('chat.shippingNoticePasteHint'));
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user