feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持

This commit is contained in:
hugo committed 2026-07-07 11:10:32 +08:00
1 parent ac4262cd75
commit c8ce77199e
183 files changed
+11690 -2300

No files matched your search

@@ -2,18 +2,14 @@
* features/chat/shipping/useChatShippingSendHandler.ts
* 用途:在聊天发送前拦截发货通知专属意图,如切票、决策文本、列映射和附件自动流程。
* 依赖:shipping.api、shippingIntents、shippingMenu、shippingMapping、shippingDownload、chat.types。
* 逻辑:由 useChatShipping 调用本文件;本文件优先消费 shipping 领域意图,只有未命中时才放行到默认 Dify 对话链路。
* 逻辑:业务动作经 Chat Turn / handleShippingMenuAction;纯 UI(下载/选文件/映射卡片)保留本地或客户端行为。
*/
import {
applyWorkspaceShippingInventoryMapping,
confirmWorkspaceShippingInventorySource,
generateWorkspaceShippingExcel,
getWorkspaceShippingBatch,
getWorkspaceShippingTask,
switchWorkspaceShippingBatchFocusTask,
} from '@/features/chat/api/shipping.api';
import { SHIPPING_DECISION_ACTIONS, SHIPPING_MENU_ACTION_SPECS } from '../constants/shipping';
import { buildShippingFieldAlignmentText } from './shippingMapping';
import { buildShippingApplyMappingTurn, buildShippingSwitchFocusTurn } from './shippingTurnActions';
import { downloadWorkspaceShippingExcelFile } from './shippingDownload';
import {
isShippingDownloadExcelIntent,
@@ -26,8 +22,9 @@ import {
import { resolveShippingDecisionFromText, resolveShippingNumericChoice } from './shippingMenu';
import { classifyShippingUploadFile } from './shippingUploadPrompt';
import { extractExt } from '../constants/shipping';
import type { StartChatTurnOptions } from '../types/chat-turn.types';
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
import type { ChatUploadFileSpec, PendingAttachment } from '../types/shipping.types';
import type { ChatUploadFileSpec, PendingAttachment, ShippingActionButton } from '../types/shipping.types';
import type { ComputedRef, Ref } from 'vue';
export interface ShippingSendHandlerContext {
@@ -57,12 +54,11 @@ export interface ShippingSendHandlerContext {
buildFileSpecsForSend: () => Promise<ChatUploadFileSpec[]>;
loadHistory: () => Promise<void>;
sendShippingMenuMessage: (message: string) => Promise<void>;
pushShippingLocalUserMessage: (text: string) => Promise<void>;
pushShippingLocalAssistantMessage: (text: string) => Promise<void>;
startChatTurn: (options: StartChatTurnOptions) => Promise<boolean>;
openShippingInventoryPicker: () => boolean;
executeShippingDecision: (decision: string, displayText?: string) => Promise<boolean>;
executeShippingManualUpload: (taskId: string, displayText?: string) => Promise<boolean>;
handleShippingMenuAction: (action: { id?: string; message: string }) => Promise<void>;
handleShippingMenuAction: (action: ShippingActionButton | { id?: string; message: string; label?: string; source_code?: string }) => Promise<void>;
handleShippingAutoUploadFile: (file: File) => Promise<void>;
handleShippingAutoUploadNoticeBatch: (files: File[]) => Promise<void>;
refreshShippingActions: () => Promise<void>;
@@ -116,16 +112,12 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
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();
await ctx.handleShippingMenuAction({
id: 'confirm_inventory_source',
message: `确认库存查询来源:${label}`,
label,
source_code: sourceCode,
});
return true;
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '确认库存源失败';
@@ -167,11 +159,21 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
ctx.msg.info(intent.mode === 'next' ? '已经是最后一票' : intent.mode === 'prev' ? '已经是第一票' : '已在当前任务');
return true;
}
await switchWorkspaceShippingBatchFocusTask(batchNo, targetTaskNo);
ctx.resolvedShippingTaskId.value = targetTaskNo;
await ctx.pushShippingLocalUserMessage(content.trim());
await ctx.pushShippingLocalAssistantMessage(`已切换到任务 ${targetTaskNo},请继续当前票处理。`);
void ctx.refreshShippingActions();
const turn = buildShippingSwitchFocusTurn(batchNo, targetTaskNo, content.trim());
await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'action',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
refs: turn.refs,
hooks: {
onAssistantMessageFinished: async () => {
void ctx.refreshShippingActions();
},
},
});
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '切换任务失败';
ctx.msg.warning(errorMessage);
@@ -203,8 +205,6 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
ctx.msg.warning('一次仅支持处理一个文件,将自动处理第一个');
}
try {
const prompt = trimmed || ctx.shippingSuggestedPrompt.value || '上传库存并进入字段对齐';
await ctx.pushShippingLocalUserMessage(prompt);
await ctx.handleShippingAutoUploadFile(first.file);
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '文件处理失败';
@@ -295,23 +295,20 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
ctx.resolvedShippingTaskId.value = taskId;
if (typeof textOrEvent !== 'string') ctx.input.value = '';
ctx.attachments.value = [];
await ctx.pushShippingLocalUserMessage(content.trim());
try {
const result = await applyWorkspaceShippingInventoryMapping(taskId, mapping);
const alignmentText = buildShippingFieldAlignmentText(result);
const hint = alignmentText || String(result.message || '').trim();
await ctx.pushShippingLocalAssistantMessage(
[
`映射已应用:${Object.entries(mapping).map(([k, v]) => `${k}=${v}`).join(',')}`,
`任务编号:${taskId}`,
hint,
].filter(Boolean).join('\n'),
);
void ctx.refreshShippingActions();
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '应用映射失败';
ctx.msg.warning(errorMessage);
}
const turn = buildShippingApplyMappingTurn(taskId, mapping);
await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'form_submit',
displayText: `${content.trim()}\n任务编号:${taskId}`,
actionType: turn.actionType,
payload: turn.payload,
refs: turn.refs,
hooks: {
onAssistantMessageFinished: async () => {
void ctx.refreshShippingActions();
},
},
});
return true;
}
}
@@ -340,20 +337,7 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
ctx.resolvedShippingTaskId.value = taskId;
if (typeof textOrEvent !== 'string') ctx.input.value = '';
ctx.attachments.value = [];
await ctx.pushShippingLocalUserMessage(content.trim());
try {
const result = await generateWorkspaceShippingExcel(taskId);
await ctx.pushShippingLocalAssistantMessage(
[
String(result.message || '').trim() || '已生成发货 Excel',
`任务编号:${taskId}`,
].filter(Boolean).join('\n'),
);
void ctx.refreshShippingActions();
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : '生成发货 Excel 失败';
ctx.msg.warning(errorMessage);
}
await ctx.handleShippingMenuAction({ id: 'generate_excel', message: content.trim() });
return true;
}
if (isShippingDownloadExcelIntent(content)) {
@@ -365,7 +349,6 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
ctx.resolvedShippingTaskId.value = taskId;
if (typeof textOrEvent !== 'string') ctx.input.value = '';
ctx.attachments.value = [];
await ctx.pushShippingLocalUserMessage(content.trim());
try {
await downloadWorkspaceShippingExcelFile(taskId);
} catch (e: unknown) {