feat(chat): 优化发货相关消息处理逻辑
- 在列出聊天消息时,过滤掉类型为'shipping-parse-review'的消息 - 移除不再使用的shippingConversationTaskIdCache - 更新发货决策处理逻辑,简化保存消息的参数 - 添加发货动作按钮刷新时的任务定位优先级逻辑 - 移除解析复核消息的展示逻辑,统一处理到轻量工作台 - 增加单元测试,验证发货动作按钮刷新时的任务定位优先级
This commit is contained in:
1 parent
9f5493185d
commit
6a0a51313f
18 files changed
+1230
-199
No files matched your search
@@ -4,14 +4,12 @@
|
||||
* 依赖:shipping.api、shippingMenu、shippingTaskId、shippingNoticeActions 常量、chat.types。
|
||||
* 逻辑:由 useChatShipping 调用本文件;本文件根据当前 taskId 拉取动作配置并回写到 messages[].shipping_actions。
|
||||
*/
|
||||
import {
|
||||
getShippingChatActions,
|
||||
listWorkspaceShippingTasks,
|
||||
} from '@/features/chat/api/shipping.api';
|
||||
import { getShippingChatActions } from '@/features/chat/api/shipping.api';
|
||||
import { SHIPPING_TERMINAL_CHECKPOINTS } from '@/constants/shippingNoticeActions';
|
||||
import { SHIPPING_TASK_ID_RE } from '../constants/shipping';
|
||||
import { findLatestShippingTaskId } from './shippingTaskId';
|
||||
import { resolveShippingActionButtons } from './shippingMenu';
|
||||
import { stripShippingPlainActionMenu } from './shippingText';
|
||||
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
|
||||
import type { ShippingActionButton } from '../types/shipping.types';
|
||||
import type { ComputedRef, Ref } from 'vue';
|
||||
@@ -24,7 +22,32 @@ export interface ShippingRefreshContext {
|
||||
history: Ref<ChatSessionItem[]>;
|
||||
resolvedShippingTaskId: Ref<string>;
|
||||
shippingTaskIdFromQuery: ComputedRef<string>;
|
||||
shippingConversationTaskIdCache: Ref<Record<string, string>>;
|
||||
getActiveBatchTaskId: () => string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前聊天页最可靠的发货任务编号。
|
||||
*
|
||||
* 核心逻辑:优先使用批次当前焦点任务,其次才回退到消息内嵌任务号、已解析状态和路由参数;
|
||||
* 禁止再按 conversation 扫任务列表“猜”一票,避免多票会话把动作按钮挂错任务。
|
||||
* @param params - 当前聊天页中可用的任务定位线索
|
||||
* @returns 可信的任务编号;无法明确定位时返回空字符串
|
||||
*/
|
||||
export function resolvePreferredShippingTaskId(params: {
|
||||
batchTaskId?: string | null;
|
||||
messageTaskId?: string | null;
|
||||
resolvedTaskId?: string | null;
|
||||
queryTaskId?: string | null;
|
||||
historyTaskId?: string | null;
|
||||
}) {
|
||||
return String(
|
||||
params.batchTaskId
|
||||
|| params.messageTaskId
|
||||
|| params.resolvedTaskId
|
||||
|| params.queryTaskId
|
||||
|| params.historyTaskId
|
||||
|| '',
|
||||
).trim().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,28 +82,13 @@ export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
||||
const match = title.match(SHIPPING_TASK_ID_RE);
|
||||
return match?.[0] ? match[0].toUpperCase() : '';
|
||||
})();
|
||||
let taskId = findLatestShippingTaskId(msgs)
|
||||
|| ctx.resolvedShippingTaskId.value
|
||||
|| ctx.shippingTaskIdFromQuery.value
|
||||
|| historyTaskId;
|
||||
if (!taskId && cid && !ctx.isThinking.value) {
|
||||
const cached = ctx.shippingConversationTaskIdCache.value[cid];
|
||||
if (cached) {
|
||||
taskId = cached;
|
||||
} else {
|
||||
try {
|
||||
const rows = await listWorkspaceShippingTasks({ limit: 500 });
|
||||
const hit = rows.find((row) => String(row.conversation_id || '') === cid);
|
||||
const taskNo = hit?.task_no ? String(hit.task_no).trim().toUpperCase() : '';
|
||||
if (taskNo) {
|
||||
ctx.shippingConversationTaskIdCache.value = { ...ctx.shippingConversationTaskIdCache.value, [cid]: taskNo };
|
||||
taskId = taskNo;
|
||||
}
|
||||
} catch {
|
||||
// fall back to structured directive buttons embedded in the message
|
||||
}
|
||||
}
|
||||
}
|
||||
const taskId = resolvePreferredShippingTaskId({
|
||||
batchTaskId: ctx.getActiveBatchTaskId(),
|
||||
messageTaskId: findLatestShippingTaskId(msgs),
|
||||
resolvedTaskId: ctx.resolvedShippingTaskId.value,
|
||||
queryTaskId: ctx.shippingTaskIdFromQuery.value,
|
||||
historyTaskId,
|
||||
});
|
||||
if (taskId) ctx.resolvedShippingTaskId.value = taskId;
|
||||
let lastOpenAssistantIndex = -1;
|
||||
for (let i = msgs.length - 1; i >= 0; i -= 1) {
|
||||
@@ -102,11 +110,9 @@ export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
||||
if (targetAssistantIndex >= 0) {
|
||||
const target = msgs[targetAssistantIndex] as ChatMessage & { shipping_actions?: ShippingActionButton[] };
|
||||
const resolved = resolveShippingActionButtons(String(target.content || ''), null);
|
||||
if (resolved.cleanedText !== String(target.content || '').trim()) {
|
||||
target.content = resolved.cleanedText;
|
||||
}
|
||||
if (resolved.buttons.length && !target.shipping_actions?.length) {
|
||||
target.shipping_actions = resolved.buttons;
|
||||
const cleanedText = stripShippingPlainActionMenu(resolved.cleanedText);
|
||||
if (cleanedText !== String(target.content || '').trim()) {
|
||||
target.content = cleanedText;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,16 +164,20 @@ export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
||||
apiResolved ? (apiButtons ?? []) : null,
|
||||
{ apiResolved },
|
||||
);
|
||||
if (resolved.fromDirective || resolved.cleanedText !== String(chatMsg.content || '').trim()) {
|
||||
chatMsg.content = resolved.cleanedText;
|
||||
const cleanedText = stripShippingPlainActionMenu(resolved.cleanedText);
|
||||
if (resolved.fromDirective || cleanedText !== String(chatMsg.content || '').trim()) {
|
||||
chatMsg.content = cleanedText;
|
||||
}
|
||||
buttons = resolved.buttons;
|
||||
} else if (!apiResolved) {
|
||||
const resolved = resolveShippingActionButtons(String(chatMsg.content || ''), null);
|
||||
if (resolved.fromDirective) {
|
||||
chatMsg.content = resolved.cleanedText;
|
||||
const cleanedText = stripShippingPlainActionMenu(resolved.cleanedText);
|
||||
if (resolved.fromDirective || cleanedText !== String(chatMsg.content || '').trim()) {
|
||||
chatMsg.content = cleanedText;
|
||||
}
|
||||
buttons = resolved.buttons;
|
||||
} else {
|
||||
chatMsg.content = stripShippingPlainActionMenu(String(chatMsg.content || ''));
|
||||
}
|
||||
if (chatMsg.type === 'shipping-match-result' && chatMsg.payload && typeof chatMsg.payload === 'object') {
|
||||
(chatMsg.payload as { action_buttons?: ShippingActionButton[] }).action_buttons = buttons;
|
||||
|
||||
Reference in new issue
Block a user