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,23 +2,24 @@
* features/chat/shipping/useChatShippingUploadHandler.ts
* 用途:处理发货通知与库存文件上传、批量建单和附件建议提示。
* 依赖:shipping.api、shippingUploadPrompt、shipping 常量、chat.types。
* 逻辑:由 useChatShipping 调用本文件;本文件识别附件类型后决定是创建任务/批次还是上传库存,并把结果同步到聊天上下文。
* 逻辑:发货通知建批/库存上传/单票覆盖均经 startChatTurn + multipart SSE。
*/
import { computed } from 'vue';
import {
createWorkspaceShippingBatchFromNotices,
createWorkspaceShippingTask,
replaceWorkspaceShippingTaskNotice,
uploadWorkspaceShippingInventory,
type ShippingTaskSummary,
} from '@/features/chat/api/shipping.api';
import { extractExt } from '../constants/shipping';
import { buildShippingInventoryUploadPrompt, classifyShippingUploadFile } from './shippingUploadPrompt';
import { buildShippingCreateBatchTurn, buildShippingReplaceNoticeTurn, buildShippingUploadInventoryTurn } from './shippingTurnActions';
import { classifyShippingUploadFile } from './shippingUploadPrompt';
import type { StartChatTurnOptions } from '../types/chat-turn.types';
import type { ChatMessage } from '../types/chat.types';
import type { PendingAttachment } from '../types/shipping.types';
import type { Ref } from 'vue';
import type { ComputedRef, Ref } from 'vue';
export interface ShippingUploadHandlerContext {
conversationId: Ref<string | null>;
currentAgentId: ComputedRef<number>;
attachments: Ref<PendingAttachment[]>;
input: Ref<string>;
shippingAutoFlowRequested: Ref<boolean>;
@@ -35,9 +36,7 @@ export interface ShippingUploadHandlerContext {
shippingInventorySelectionTaskId: Ref<string>;
shippingInventorySchemaPreviewCache: Ref<Record<string, unknown>>;
getActiveShippingTaskId: () => string;
sendShippingMenuMessage: (message: string) => Promise<void>;
pushShippingLocalUserMessage: (text: string) => Promise<void>;
pushShippingLocalAssistantMessage: (text: string) => Promise<void>;
startChatTurn: (options: StartChatTurnOptions) => Promise<boolean>;
refreshShippingActions: () => Promise<void>;
msg: { warning: (text: string) => void };
t: (key: string, params?: Record<string, unknown>) => string;
@@ -56,35 +55,41 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
ctx.shippingInventorySelectionTaskId.value = '';
};
const continueShippingInventoryUploadWithDify = async (
taskId: string,
result: Record<string, unknown> | null | undefined,
fileName?: string,
) => {
const key = String(taskId || '').trim().toUpperCase();
const schemaPreview = (result?.data as { schema_preview?: unknown } | undefined)?.schema_preview;
if (key && schemaPreview && typeof schemaPreview === 'object') {
ctx.shippingInventorySchemaPreviewCache.value = {
...ctx.shippingInventorySchemaPreviewCache.value,
[key]: schemaPreview,
};
}
ctx.resolvedShippingTaskId.value = taskId;
await ctx.sendShippingMenuMessage(buildShippingInventoryUploadPrompt(result, fileName));
const runInventoryUploadChatTurn = async (taskId: string, file: File) => {
const turn = buildShippingUploadInventoryTurn(taskId, file);
const ok = await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'upload_followup',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
files: turn.files,
refs: turn.refs,
hooks: {
onAssistantMessageFinished: async (assistantMsg) => {
const payload = assistantMsg.payload && typeof assistantMsg.payload === 'object'
? assistantMsg.payload as { schema_preview?: unknown; task_no?: string }
: {};
const key = String(payload.task_no || taskId || '').trim().toUpperCase();
const preview = payload.schema_preview;
if (key && preview && typeof preview === 'object') {
ctx.shippingInventorySchemaPreviewCache.value = {
...ctx.shippingInventorySchemaPreviewCache.value,
[key]: preview,
};
}
syncBatchContextFromTurnMessage(assistantMsg);
void ctx.refreshShippingActions();
},
},
});
if (!ok) ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return ok;
};
const uploadInventoryForTask = async (taskId: string, file: File) => {
ctx.resolvedShippingTaskId.value = taskId;
await ctx.pushShippingLocalUserMessage(ctx.t('chat.shippingNoticeInventoryUploadStarted', { fileName: file.name }));
const result = await uploadWorkspaceShippingInventory(taskId, file);
const batchNo = String(
((result as unknown as { data?: { batch_no?: string } }).data?.batch_no)
|| ((result as unknown as { data?: { batch?: { batch_no?: string } } }).data?.batch?.batch_no)
|| '',
).trim();
if (batchNo) ctx.resolvedShippingBatchNo.value = batchNo;
await continueShippingInventoryUploadWithDify(taskId, result as unknown as Record<string, unknown>, file.name);
void ctx.refreshShippingActions();
await runInventoryUploadChatTurn(taskId, file);
};
const selectShippingInventoryUploadTask = (taskId: string): boolean => {
@@ -119,6 +124,38 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
resetInventoryTaskSelection();
};
const syncBatchContextFromTurnMessage = (assistantMsg: ChatMessage) => {
const payload = assistantMsg.payload && typeof assistantMsg.payload === 'object'
? assistantMsg.payload as { batch_no?: string; task_no?: string; orchestrator_started?: boolean }
: {};
const batchNo = String(payload.batch_no || assistantMsg.shipping_ui_state?.batch_no || '').trim();
const taskId = String(payload.task_no || assistantMsg.shipping_ui_state?.task_no || '').trim();
if (batchNo) ctx.resolvedShippingBatchNo.value = batchNo;
if (taskId) ctx.resolvedShippingTaskId.value = taskId;
};
const runNoticeBatchChatTurn = async (files: File[]) => {
const turn = buildShippingCreateBatchTurn(files, ctx.conversationId.value);
const ok = await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'upload_followup',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
files: turn.files,
hooks: {
onAssistantMessageFinished: async (assistantMsg) => {
syncBatchContextFromTurnMessage(assistantMsg);
void ctx.refreshShippingActions();
},
},
});
if (!ok) {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
}
return ok;
};
const syncBatchContextFromNoticeResponse = (result: Record<string, unknown> | null | undefined) => {
const batchNo = String(
(result?.batch_no as string | undefined)
@@ -138,24 +175,30 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
}
};
const runReplaceNoticeChatTurn = async (taskId: string, file: File) => {
const turn = buildShippingReplaceNoticeTurn(taskId, file);
const ok = await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'upload_followup',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
files: turn.files,
refs: turn.refs,
hooks: {
onAssistantMessageFinished: async (assistantMsg) => {
syncBatchContextFromTurnMessage(assistantMsg);
void ctx.refreshShippingActions();
},
},
});
if (!ok) ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return ok;
};
const handleReplaceNoticeForCurrentTask = async (taskId: string, file: File) => {
const result = await replaceWorkspaceShippingTaskNotice(taskId, file);
syncBatchContextFromNoticeResponse(result as unknown as Record<string, unknown>);
ctx.resolvedShippingTaskId.value = taskId;
const hint = String(result.message || '').trim();
const status = String((result as { status?: string }).status || '').trim();
const orchestratorStarted = Boolean(
(result.data as { orchestrator_started?: boolean } | undefined)?.orchestrator_started,
);
await ctx.pushShippingLocalAssistantMessage(
[
hint || `已重新上传发货通知并覆盖当前任务 ${taskId}`,
`任务编号:${taskId}`,
orchestratorStarted ? '后台已开始重新解析当前任务,请稍后查看进度自动刷新。' : '',
status === 'needs_human' ? '请根据提示重新上传清晰文件,或粘贴通知正文。' : '',
].filter(Boolean).join('\n'),
);
void ctx.refreshShippingActions();
await runReplaceNoticeChatTurn(taskId, file);
};
const handleShippingAutoUploadFile = async (file: File) => {
@@ -184,37 +227,7 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
await handleReplaceNoticeForCurrentTask(targetTaskId, file);
return;
}
const result = await createWorkspaceShippingBatchFromNotices(
[file],
String(ctx.conversationId.value || '').trim() || undefined,
);
syncBatchContextFromNoticeResponse(result as unknown as Record<string, unknown>);
const taskId = String(
((result.data as { focus_task?: { task_no?: string } } | undefined)?.focus_task?.task_no)
|| result.task_id
|| '',
).trim();
const batchNo = String(
result.batch_no
|| ((result.data as { batch?: { batch_no?: string } } | undefined)?.batch?.batch_no)
|| '',
).trim();
const hint = String(result.message || '').trim();
const status = String((result as { status?: string }).status || '').trim();
const orchestratorStarted = Boolean(
(result.data as { orchestrator_started?: boolean } | undefined)?.orchestrator_started,
);
const tail = status === 'needs_human' ? '请根据提示进行确认/驳回。' : '';
await ctx.pushShippingLocalAssistantMessage(
[
hint || `已处理发货通知附件:${file.name}`,
batchNo ? `批次编号:${batchNo}` : '',
taskId ? `任务编号:${taskId}` : '',
orchestratorStarted ? '后台已开始按票处理,请稍后查看进度自动刷新。' : '',
tail,
].filter(Boolean).join('\n'),
);
void ctx.refreshShippingActions();
await runNoticeBatchChatTurn([file]);
};
const handleShippingAutoUploadNoticeBatch = async (files: File[]) => {
@@ -228,50 +241,7 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
await handleReplaceNoticeForCurrentTask(activeTaskId, singleNoticeFile);
return;
}
const result = await createWorkspaceShippingBatchFromNotices(
noticeFiles,
String(ctx.conversationId.value || '').trim() || undefined,
);
syncBatchContextFromNoticeResponse(result as unknown as Record<string, unknown>);
const taskId = String(
((result.data as { focus_task?: { task_no?: string } } | undefined)?.focus_task?.task_no)
|| result.task_id
|| '',
).trim();
const batchNo = String(
result.batch_no
|| ((result.data as { batch?: { batch_no?: string } } | undefined)?.batch?.batch_no)
|| '',
).trim();
const batch = (result.data as {
batch?: { file_count?: number };
tasks?: Array<{ batch_index?: number; batch_total?: number }>;
} | undefined)?.batch;
const tasks = Array.isArray((result.data as { tasks?: unknown[] } | undefined)?.tasks)
? (result.data as { tasks: Array<{ batch_index?: number; batch_total?: number }> }).tasks
: [];
const focusTask = tasks[0];
const focusProgress = focusTask?.batch_index && focusTask?.batch_total
? `当前第 ${focusTask.batch_index}/${focusTask.batch_total} 票`
: '';
const fileCount = Number(batch?.file_count || noticeFiles.length || 0);
const hint = String(result.message || '').trim();
const status = String((result as { status?: string }).status || '').trim();
const orchestratorStarted = Boolean(
(result.data as { orchestrator_started?: boolean } | undefined)?.orchestrator_started,
);
const tail = status === 'needs_human' ? '请根据提示进行确认/驳回。' : '';
await ctx.pushShippingLocalAssistantMessage(
[
hint || `已处理 ${fileCount} 份发货通知附件`,
batchNo ? `批次编号:${batchNo}` : '',
taskId ? `当前任务:${taskId}` : '',
focusProgress,
orchestratorStarted ? `后台已开始按票拆分处理,本批 ${fileCount} 票会逐步更新状态。` : '',
tail,
].filter(Boolean).join('\n'),
);
void ctx.refreshShippingActions();
await runNoticeBatchChatTurn(noticeFiles);
};
const openShippingInventoryPicker = (taskId?: string): boolean => {