feat(shipping-notice): 新增发货批次和任务管理功能,支持批次创建与任务处理

This commit is contained in:
hugo committed 2026-06-17 20:45:19 +08:00
1 parent 457f0f4e5e
commit 634dee4b81
81 files changed
+9088 -3625

No files matched your search

@@ -1,4 +1,4 @@
/**
/**
* features/chat/composables/useChatPage.ts
* 对话页主编排:组合 SSE、Agent、附件、请假/出货、路由同步与 page/* 子模块。
*/
@@ -221,6 +221,7 @@ export function useChatPage() {
onConversationLoadFailed: shipping.onConversationLoadFailed,
isShippingNoticeAgent: shipping.isShippingNoticeAgent,
shippingTaskIdFromQuery: shipping.shippingTaskIdFromQuery,
shippingBatchNoFromQuery: shipping.shippingBatchNoFromQuery,
clearHistory,
clearAttachments: attachmentApi.clearAttachments,
input,
@@ -262,6 +263,7 @@ export function useChatPage() {
getSourceMeta,
getSourceBrief,
getShippingInventoryMappingPayload: shipping.getShippingInventoryMappingPayload,
getShippingMatchResultPayload: shipping.getShippingMatchResultPayload,
canOpenQueryPreview: queryPreview.canOpen,
getQueryPreviewSummary: queryPreview.getSummary,
openQueryPreviewDialog: queryPreview.openDialog,
@@ -306,6 +308,7 @@ export function useChatPage() {
currentAgentId,
presetQuestion,
shippingTaskIdFromQuery: shipping.shippingTaskIdFromQuery,
shippingBatchNoFromQuery: shipping.shippingBatchNoFromQuery,
isShippingNoticeAgent: shipping.isShippingNoticeAgent,
resetChatStateForAgentChange,
cancelVoiceInput: voice.cancelVoiceInput,
@@ -405,6 +408,11 @@ export function useChatPage() {
handleDeleteConfirm: dialogs.handleDeleteConfirm,
showChatRating,
isShippingNoticeAgent: shipping.isShippingNoticeAgent,
shippingBatchSummary: shipping.shippingBatchSummary,
shippingBatchTasks: shipping.shippingBatchTasks,
activeShippingTaskDetail: shipping.activeShippingTaskDetail,
shippingBatchTasksExpanded: shipping.shippingBatchTasksExpanded,
switchShippingFocusTask: shipping.switchShippingFocusTask,
shippingSuggestedPrompt: shipping.shippingSuggestedPrompt,
handleShippingInventorySelected: shipping.handleShippingInventorySelected,
useShippingSuggestedPrompt: shipping.useShippingSuggestedPrompt,
@@ -13,6 +13,7 @@ export interface UseChatRouteSyncOptions {
currentAgentId: ComputedRef<number>;
presetQuestion: ComputedRef<string>;
shippingTaskIdFromQuery: ComputedRef<string | null>;
shippingBatchNoFromQuery?: ComputedRef<string | null>;
isShippingNoticeAgent: ComputedRef<boolean>;
resetChatStateForAgentChange: () => void;
cancelVoiceInput: () => void;
@@ -38,6 +39,7 @@ export function useChatRouteSync(options: UseChatRouteSyncOptions) {
currentAgentId,
presetQuestion,
shippingTaskIdFromQuery,
shippingBatchNoFromQuery,
resetChatStateForAgentChange,
cancelVoiceInput,
syncCurrentUserProfile,
@@ -99,7 +101,7 @@ export function useChatRouteSync(options: UseChatRouteSyncOptions) {
if (convId) {
await loadConversation(convId, { force: true });
if (syncToken !== routeSyncToken) return;
} else if (shippingTaskIdFromQuery.value) {
} else if (shippingTaskIdFromQuery.value || shippingBatchNoFromQuery?.value) {
await resumeShippingTaskFromQuery();
if (syncToken !== routeSyncToken) return;
}
@@ -120,13 +122,17 @@ export function useChatRouteSync(options: UseChatRouteSyncOptions) {
});
watch(
() => [route.params.agentId, route.query.conversation_id, route.query.preset, route.query.task_id],
async ([nextAgentId, nextConversationId, nextPreset, nextTaskId], [prevAgentId, prevConversationId, prevPreset, prevTaskId]) => {
() => [route.params.agentId, route.query.conversation_id, route.query.preset, route.query.task_id, route.query.batch_no],
async (
[nextAgentId, nextConversationId, nextPreset, nextTaskId, nextBatchNo],
[prevAgentId, prevConversationId, prevPreset, prevTaskId, prevBatchNo],
) => {
if (
nextAgentId === prevAgentId
&& nextConversationId === prevConversationId
&& nextPreset === prevPreset
&& nextTaskId === prevTaskId
&& nextBatchNo === prevBatchNo
) {
return;
}
@@ -135,6 +141,7 @@ export function useChatRouteSync(options: UseChatRouteSyncOptions) {
nextAgentId === prevAgentId
&& nextPreset === prevPreset
&& nextTaskId === prevTaskId
&& nextBatchNo === prevBatchNo
&& nextConversationId !== prevConversationId
) {
if (typeof nextConversationId === 'string' && nextConversationId) {
@@ -1,18 +1,8 @@
/**
/**
* features/chat/composables/useChatShipping.ts
* 发货通知(出货)编排层:组合 shipping/* 子模块,对外暴露 ChatView 所需 API。
*
* 子模块职责:
* shippingText / shippingMenu / shippingIntents … 纯函数
* useChatShippingRefresh 动作按钮刷新
* useChatShippingMappingForm 库存列映射表单
* useChatShippingDecision 人工决策与手工上传
* useChatShippingMenuHandler 气泡动作按钮点击
* useChatShippingUploadHandler 文件上传与建议提示
* useChatShippingSendHandler handleSend 前置拦截
*
* 消息类型约定(写入 messages 列表):
* - shipping-inventory-mapping:库存列映射表单
* 用途:发货通知聊天编排入口,组合 shipping/* 子模块并对 ChatView 暴露统一交互能力。
* 依赖:shipping.api、shipping/ 下各 handler 工厂、chat.deps、chat.types、Vue refs/computed/watch。
* 逻辑:上游由 ChatView/ChatPage 调用本 composable;本文件再把任务状态、动作刷新、文件上传、决策与发送拦截分发给 shipping 子模块。
*/
import {
@@ -31,18 +21,29 @@ import {
createShippingLocalMessages,
createShippingMappingFormHandlers,
createShippingMenuHandlers,
createShippingParseReviewHandlers,
createShippingSendHandlers,
createShippingUploadHandlers,
extractShippingActionsDirective,
findHistorySessionForShippingTask,
findLatestShippingTaskId,
getShippingInventoryMappingPayload,
getShippingMatchResultPayload,
getShippingParseReviewPayload,
getShippingInventorySampleRowText,
hasShippingInventorySampleRows,
normalizeShippingHeaderForDisplay,
sanitizeShippingConversationText,
shippingActionButtonClass,
} from '../shipping';
import {
getWorkspaceShippingBatch,
getWorkspaceShippingTask,
switchWorkspaceShippingBatchFocusTask,
type ShippingBatchSummary,
type ShippingTaskDetail,
type ShippingTaskSummary,
} from '@/features/chat/api/shipping.api';
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
import type { ChatUploadFileSpec, PendingAttachment } from '../types/shipping.types';
@@ -78,6 +79,13 @@ export interface UseChatShippingOptions {
createNewChatView: () => void;
}
/**
* 组装发货通知聊天所需的状态、动作处理器与发送拦截逻辑。
*
* 核心逻辑:统一管理当前任务/批次上下文,组合上传、映射、决策、动作刷新等子模块,并对 ChatView 暴露稳定 API。
* @param options - 聊天页依赖与响应式状态集合
* @returns 发货通知聊天场景所需的状态、事件处理器和工具函数
*/
export function useChatShipping(options: UseChatShippingOptions) {
const {
deps: { t, msg, router, route },
@@ -128,17 +136,90 @@ export function useChatShipping(options: UseChatShippingOptions) {
const value = route.query.task_id;
return typeof value === 'string' ? value.trim() : '';
});
const shippingBatchNoFromQuery = computed(() => {
const value = route.query.batch_no;
return typeof value === 'string' ? value.trim() : '';
});
const resolvedShippingTaskId = ref('');
const resolvedShippingBatchNo = ref('');
const shippingBatchSummary = ref<ShippingBatchSummary | null>(null);
const shippingBatchTasks = ref<ShippingTaskSummary[]>([]);
const shippingBatchTasksExpanded = ref(false);
const activeShippingTaskDetail = ref<ShippingTaskDetail | null>(null);
const shippingConversationTaskIdCache = ref<Record<string, string>>({});
const shippingInventorySchemaPreviewCache = ref<Record<string, unknown>>({});
const autoResumedShippingTasks = ref<Record<string, boolean>>({});
const syncShippingBatchSummary = async (batchNo: string) => {
const normalized = String(batchNo || '').trim();
if (!normalized) {
shippingBatchSummary.value = null;
shippingBatchTasks.value = [];
return;
}
try {
const detail = await getWorkspaceShippingBatch(normalized);
resolvedShippingBatchNo.value = detail.batch_no || normalized;
shippingBatchTasks.value = Array.isArray(detail.tasks) ? detail.tasks : [];
shippingBatchSummary.value = {
id: detail.id,
batch_no: detail.batch_no,
conversation_id: detail.conversation_id,
current_focus_task_no: detail.focus_task?.task_no || detail.current_focus_task_no || null,
file_count: detail.file_count,
process_summary: detail.process_summary || null,
created_at: detail.created_at,
updated_at: detail.updated_at,
};
if (detail.focus_task?.task_no) {
resolvedShippingTaskId.value = String(detail.focus_task.task_no).trim();
}
} catch {
// ignore summary refresh failure in chat flow
}
};
const switchShippingFocusTask = async (taskNo: string) => {
const batchNo = getActiveShippingBatchNo();
const normalizedTaskNo = String(taskNo || '').trim().toUpperCase();
if (!batchNo || !normalizedTaskNo) return false;
await switchWorkspaceShippingBatchFocusTask(batchNo, normalizedTaskNo);
resolvedShippingTaskId.value = normalizedTaskNo;
if (shippingBatchSummary.value) {
shippingBatchSummary.value = {
...shippingBatchSummary.value,
current_focus_task_no: normalizedTaskNo,
};
}
await syncShippingBatchSummary(batchNo);
return true;
};
const getActiveShippingTaskId = () =>
findLatestShippingTaskId(messages.value)
|| resolvedShippingTaskId.value
|| shippingTaskIdFromQuery.value
|| '';
const getActiveShippingBatchNo = () =>
resolvedShippingBatchNo.value
|| shippingBatchNoFromQuery.value
|| String(shippingBatchSummary.value?.batch_no || '').trim()
|| '';
const syncActiveShippingTaskDetail = async (taskId?: string) => {
const normalized = String(taskId || getActiveShippingTaskId() || '').trim().toUpperCase();
if (!normalized) {
activeShippingTaskDetail.value = null;
return;
}
try {
activeShippingTaskDetail.value = await getWorkspaceShippingTask(normalized);
} catch {
activeShippingTaskDetail.value = null;
}
};
const lastAssistantMessageContent = () => {
for (let i = messages.value.length - 1; i >= 0; i -= 1) {
const item = messages.value[i];
@@ -208,8 +289,24 @@ export function useChatShipping(options: UseChatShippingOptions) {
t,
});
const {
syncShippingParseReviewCard,
updateShippingParseReviewHeader,
updateShippingParseReviewLine,
addShippingParseReviewLine,
removeShippingParseReviewLine,
} = createShippingParseReviewHandlers({
messages,
resolvedShippingTaskId,
newLocalMessageId,
saveLocalMessageToBackend,
refreshShippingActions,
msg,
});
const {
handleShippingAutoUploadFile,
handleShippingAutoUploadNoticeBatch,
openShippingInventoryPicker,
handleShippingInventorySelected,
shippingSuggestedPrompt,
@@ -222,6 +319,8 @@ export function useChatShipping(options: UseChatShippingOptions) {
shippingInventoryPickerRef,
pendingShippingUploadTaskId,
resolvedShippingTaskId,
resolvedShippingBatchNo,
shippingBatchSummary,
shippingInventorySchemaPreviewCache,
getActiveShippingTaskId,
sendShippingMenuMessage,
@@ -258,6 +357,7 @@ export function useChatShipping(options: UseChatShippingOptions) {
attachments,
shippingAutoFlowRequested,
resolvedShippingTaskId,
getActiveShippingBatchNo,
currentAgentId,
selectedKb,
attachmentPolicy,
@@ -273,6 +373,7 @@ export function useChatShipping(options: UseChatShippingOptions) {
executeShippingDecision,
executeShippingManualUpload,
handleShippingAutoUploadFile,
handleShippingAutoUploadNoticeBatch,
refreshShippingActions,
shippingSuggestedPrompt,
msg,
@@ -281,6 +382,13 @@ export function useChatShipping(options: UseChatShippingOptions) {
// ── 路由 / 会话生命周期 ───────────────────────────────────
const resumeShippingTaskFromQuery = async () => {
const batchNo = shippingBatchNoFromQuery.value;
if (batchNo && isShippingNoticeAgent.value) {
await syncShippingBatchSummary(batchNo);
if (!shippingTaskIdFromQuery.value && resolvedShippingTaskId.value) {
return;
}
}
const taskId = shippingTaskIdFromQuery.value;
if (!taskId || !isShippingNoticeAgent.value) return;
if (route.query.conversation_id) return;
@@ -304,13 +412,17 @@ export function useChatShipping(options: UseChatShippingOptions) {
const onConversationLoadedEmpty = async () => {
if (!isShippingNoticeAgent.value || !shippingTaskIdFromQuery.value || isThinking.value) return;
await router.replace({ path: route.path, query: { task_id: shippingTaskIdFromQuery.value } });
const nextQuery: Record<string, string> = { task_id: shippingTaskIdFromQuery.value };
if (shippingBatchNoFromQuery.value) nextQuery.batch_no = shippingBatchNoFromQuery.value;
await router.replace({ path: route.path, query: nextQuery });
};
const onConversationLoadFailed = async () => {
if (!isShippingNoticeAgent.value || !shippingTaskIdFromQuery.value || isThinking.value) return;
createNewChatView();
void router.replace({ path: route.path, query: { task_id: shippingTaskIdFromQuery.value } });
const nextQuery: Record<string, string> = { task_id: shippingTaskIdFromQuery.value };
if (shippingBatchNoFromQuery.value) nextQuery.batch_no = shippingBatchNoFromQuery.value;
void router.replace({ path: route.path, query: nextQuery });
input.value = t('chat.shippingNoticeContinue', { taskId: shippingTaskIdFromQuery.value });
await invokeHandleSend?.();
};
@@ -327,8 +439,22 @@ export function useChatShipping(options: UseChatShippingOptions) {
].join('|');
});
watch([shippingActionsWatchKey, isThinking, isShippingNoticeAgent, () => currentAgent.value?.name], () => {
void refreshShippingActions();
void refreshShippingActions().then(async () => {
const taskId = getActiveShippingTaskId();
await Promise.all([
syncShippingParseReviewCard(taskId),
syncActiveShippingTaskDetail(taskId),
]);
});
});
watch([resolvedShippingBatchNo, shippingBatchNoFromQuery], ([batchNo, queryBatchNo]) => {
const nextBatchNo = String(batchNo || queryBatchNo || '').trim();
if (!nextBatchNo) {
shippingBatchSummary.value = null;
return;
}
void syncShippingBatchSummary(nextBatchNo);
}, { immediate: true });
return {
shippingBindSend,
@@ -339,13 +465,27 @@ export function useChatShipping(options: UseChatShippingOptions) {
pendingShippingUploadTaskId,
allowsSendWithAttachmentsOnly,
shippingTaskIdFromQuery,
shippingBatchNoFromQuery,
resolvedShippingBatchNo,
shippingBatchSummary,
shippingBatchTasks,
shippingBatchTasksExpanded,
activeShippingTaskDetail,
getActiveShippingBatchNo,
switchShippingFocusTask,
resumeShippingTaskFromQuery,
normalizeShippingHeaderForDisplay,
getShippingInventoryMappingPayload,
getShippingMatchResultPayload,
getShippingParseReviewPayload,
hasShippingInventorySampleRows,
getShippingInventorySampleRowText,
applyShippingInventoryMappingForm,
reuploadShippingInventoryFromMappingForm,
updateShippingParseReviewHeader,
updateShippingParseReviewLine,
addShippingParseReviewLine,
removeShippingParseReviewLine,
handleShippingActionClick,
handleShippingInventorySelected,
shippingActionButtonClass,