refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 出货聊天:助手消息动作按钮刷新(有状态 composable 工厂)。
|
||||
*
|
||||
* 按 taskId 拉取 getShippingChatActions,写入 messages[].shipping_actions;
|
||||
* 终态 checkpoint 时清除所有按钮。
|
||||
*/
|
||||
import {
|
||||
getShippingChatActions,
|
||||
listWorkspaceShippingTasks,
|
||||
} 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 type { ChatMessage, ChatSessionItem } from '../types/chat.types';
|
||||
import type { ShippingActionButton } from '../types/shipping.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>;
|
||||
shippingConversationTaskIdCache: Ref<Record<string, string>>;
|
||||
}
|
||||
|
||||
export function assistantMessageHasLaterUserReply(msgs: Array<{ role?: string }>, index: number): boolean {
|
||||
return msgs.slice(index + 1).some((item) => item.role === 'user');
|
||||
}
|
||||
|
||||
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() : '';
|
||||
})();
|
||||
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 content-based buttons
|
||||
}
|
||||
}
|
||||
}
|
||||
if (taskId) ctx.resolvedShippingTaskId.value = taskId;
|
||||
let apiButtons: Array<{ id: string; label: string; message: string; variant?: string }> | null = null;
|
||||
let apiCheckpoint: string | null = null;
|
||||
let apiResolved = false;
|
||||
if (taskId && !ctx.isThinking.value) {
|
||||
const seq = ++shippingActionsRequestSeq;
|
||||
try {
|
||||
const data = await getShippingChatActions(taskId);
|
||||
if (seq === shippingActionsRequestSeq) {
|
||||
apiResolved = true;
|
||||
apiCheckpoint = data.checkpoint ? String(data.checkpoint) : null;
|
||||
apiButtons = data.action_buttons?.length ? data.action_buttons : null;
|
||||
}
|
||||
} catch {
|
||||
// fall back to content-based buttons
|
||||
}
|
||||
}
|
||||
if (apiResolved && apiCheckpoint && SHIPPING_TERMINAL_CHECKPOINTS.has(apiCheckpoint)) {
|
||||
msgs.forEach((rawMsg) => {
|
||||
const m = rawMsg as ChatMessage & { shipping_actions?: unknown };
|
||||
if (m.role === 'assistant') {
|
||||
m.shipping_actions = undefined;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
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;
|
||||
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[] = [];
|
||||
if (index === targetAssistantIndex) {
|
||||
const resolved = resolveShippingActionButtons(
|
||||
String(chatMsg.content || ''),
|
||||
apiResolved ? (apiButtons ?? []) : null,
|
||||
{ apiResolved },
|
||||
);
|
||||
if (resolved.fromDirective) {
|
||||
chatMsg.content = resolved.cleanedText;
|
||||
}
|
||||
buttons = resolved.buttons;
|
||||
} else if (!apiResolved) {
|
||||
const resolved = resolveShippingActionButtons(String(chatMsg.content || ''), null);
|
||||
if (resolved.fromDirective) {
|
||||
chatMsg.content = resolved.cleanedText;
|
||||
}
|
||||
buttons = resolved.buttons;
|
||||
}
|
||||
chatMsg.shipping_actions = buttons.length ? buttons : undefined;
|
||||
});
|
||||
};
|
||||
|
||||
return { refreshShippingActions };
|
||||
}
|
||||
Reference in new issue
Block a user