- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
186 lines
7.3 KiB
TypeScript
186 lines
7.3 KiB
TypeScript
/**
|
|
* features/chat/shipping/useChatShippingRefresh.ts
|
|
* 用途:刷新聊天区发货通知 UI State,把后端状态机 action_buttons 同步到消息气泡。
|
|
* 依赖:shipping.api、shippingTaskId、shippingNoticeActions 常量、chat.types。
|
|
* 逻辑:由 useChatShipping 调用本文件;本文件只读取后端 /ui-state,不再从 Dify 正文解析按钮。
|
|
*/
|
|
import { getWorkspaceShippingTaskUiState } 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 { stripShippingActionListByButtons, stripShippingPlainActionMenu } from './shippingText';
|
|
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
|
|
import type { ShippingActionButton } from '../types/shipping.types';
|
|
import type { ShippingTaskUiState } from '../types/shipping.api.types';
|
|
import type { ComputedRef, Ref } from 'vue';
|
|
|
|
export interface ShippingRefreshContext {
|
|
isShippingNoticeAgent: ComputedRef<boolean>;
|
|
messages: Ref<ChatMessage[]>;
|
|
conversationId: Ref<string | null>;
|
|
isThinking: Ref<boolean>;
|
|
history: Ref<ChatSessionItem[]>;
|
|
resolvedShippingTaskId: Ref<string>;
|
|
shippingTaskIdFromQuery: ComputedRef<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();
|
|
}
|
|
|
|
/**
|
|
* 判断某条助手消息后面是否已经出现新的用户回复。
|
|
* @param msgs - 当前消息列表
|
|
* @param index - 目标助手消息索引
|
|
* @returns `true` 表示该消息已被后续用户输入“盖住”
|
|
*/
|
|
export function assistantMessageHasLaterUserReply(msgs: Array<{ role?: string }>, index: number): boolean {
|
|
return msgs.slice(index + 1).some((item) => item.role === 'user');
|
|
}
|
|
|
|
/**
|
|
* 创建发货通知动作按钮刷新器。
|
|
*
|
|
* 核心逻辑:按 taskId 拉取后端 ui_state/action_buttons,并在终态 checkpoint 清空旧按钮。
|
|
* @param ctx - 发货通知聊天上下文
|
|
* @returns 动作刷新函数
|
|
*/
|
|
export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
|
let shippingActionsRequestSeq = 0;
|
|
|
|
const refreshShippingActions = async () => {
|
|
if (!ctx.isShippingNoticeAgent.value) return;
|
|
const msgs = ctx.messages.value;
|
|
if (!msgs.length) return;
|
|
const cid = String(ctx.conversationId.value || '').trim();
|
|
const historyTaskId = (() => {
|
|
if (!cid) return '';
|
|
const session = ctx.history.value.find((item) => String(item.conversation_id || '') === cid);
|
|
const title = String(session?.title || '');
|
|
const match = title.match(SHIPPING_TASK_ID_RE);
|
|
return match?.[0] ? match[0].toUpperCase() : '';
|
|
})();
|
|
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) {
|
|
const item = msgs[i];
|
|
if (item?.role !== 'assistant') continue;
|
|
if (assistantMessageHasLaterUserReply(msgs, i)) continue;
|
|
lastOpenAssistantIndex = i;
|
|
break;
|
|
}
|
|
let lastAssistantIndex = -1;
|
|
for (let i = msgs.length - 1; i >= 0; i -= 1) {
|
|
const item = msgs[i];
|
|
if (item?.role === 'assistant') {
|
|
lastAssistantIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
const targetAssistantIndex = lastOpenAssistantIndex >= 0 ? lastOpenAssistantIndex : lastAssistantIndex;
|
|
if (targetAssistantIndex >= 0) {
|
|
const target = msgs[targetAssistantIndex] as ChatMessage;
|
|
const cleanedText = stripShippingPlainActionMenu(String(target.content || ''));
|
|
if (cleanedText !== String(target.content || '').trim()) {
|
|
target.content = cleanedText;
|
|
}
|
|
}
|
|
|
|
let apiState: ShippingTaskUiState | null = null;
|
|
let apiResolved = false;
|
|
if (taskId) {
|
|
const seq = ++shippingActionsRequestSeq;
|
|
try {
|
|
const data = await getWorkspaceShippingTaskUiState(taskId);
|
|
if (seq === shippingActionsRequestSeq) {
|
|
apiResolved = true;
|
|
apiState = data;
|
|
}
|
|
} catch {
|
|
// keep existing SSE-delivered ui_state if present
|
|
}
|
|
}
|
|
const apiCheckpoint = apiState?.checkpoint ? String(apiState.checkpoint) : null;
|
|
if (apiResolved && apiCheckpoint && SHIPPING_TERMINAL_CHECKPOINTS.has(apiCheckpoint)) {
|
|
msgs.forEach((rawMsg) => {
|
|
const m = rawMsg as ChatMessage;
|
|
if (m.role === 'assistant') {
|
|
m.shipping_actions = undefined;
|
|
m.shipping_ui_state = apiState || undefined;
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
msgs.forEach((rawMsg, index) => {
|
|
const chatMsg = rawMsg as ChatMessage & {
|
|
shipping_actions_used?: boolean;
|
|
shipping_actions?: ShippingActionButton[];
|
|
};
|
|
if (chatMsg.role !== 'assistant' || chatMsg.shipping_actions_used) {
|
|
return;
|
|
}
|
|
if (chatMsg.type && !['text', undefined].includes(chatMsg.type)) {
|
|
chatMsg.shipping_actions = undefined;
|
|
return;
|
|
}
|
|
if (assistantMessageHasLaterUserReply(msgs, index) && index !== targetAssistantIndex) {
|
|
chatMsg.shipping_actions = undefined;
|
|
return;
|
|
}
|
|
let buttons: ShippingActionButton[] = [];
|
|
let cleanedText = stripShippingPlainActionMenu(String(chatMsg.content || ''));
|
|
if (index === targetAssistantIndex) {
|
|
if (apiState?.action_buttons?.length) {
|
|
buttons = apiState.action_buttons;
|
|
chatMsg.shipping_ui_state = apiState;
|
|
} else if (!apiResolved && chatMsg.shipping_ui_state?.action_buttons?.length) {
|
|
buttons = chatMsg.shipping_ui_state.action_buttons;
|
|
}
|
|
if (buttons.length) {
|
|
cleanedText = stripShippingActionListByButtons(cleanedText, buttons);
|
|
}
|
|
} else {
|
|
cleanedText = stripShippingPlainActionMenu(String(chatMsg.content || ''));
|
|
}
|
|
if (cleanedText !== String(chatMsg.content || '').trim()) {
|
|
chatMsg.content = cleanedText;
|
|
}
|
|
if (chatMsg.type === 'shipping-match-result' && chatMsg.payload && typeof chatMsg.payload === 'object') {
|
|
(chatMsg.payload as { action_buttons?: ShippingActionButton[] }).action_buttons = buttons;
|
|
}
|
|
chatMsg.shipping_actions = buttons.length ? buttons : undefined;
|
|
});
|
|
};
|
|
|
|
return { refreshShippingActions };
|
|
}
|