Files
syops/frontend/src/features/chat/shipping/shippingConversation.ts
T

54 lines
2.1 KiB
TypeScript

/**
* features/chat/shipping/shippingConversation.ts
* 用途:发货聊天会话侧栏同步与带任务编号的菜单消息发送。
* 依赖:chat.types、shipping.types、Vue Ref/ComputedRef。
* 逻辑:useChatShipping 通过 createShippingConversationHelpers 发送菜单类消息并刷新历史。
*/
import type { ChatSessionItem } from '../types/chat.types';
import type { ChatUploadFileSpec } from '../types/shipping.types';
import type { ChatMessageSendResult } from '../types/chat-turn.types';
import type { ComputedRef, Ref } from 'vue';
export interface ShippingConversationContext {
conversationId: Ref<string | null>;
history: Ref<ChatSessionItem[]>;
loadHistory: () => Promise<void>;
getActiveShippingTaskId: () => string;
currentAgentId: ComputedRef<number>;
selectedKb: Ref<string>;
sendMessage: (
content: string,
agentId: number,
kb: string,
files: ChatUploadFileSpec[],
context?: { task_id?: string; display_query?: string },
) => Promise<ChatMessageSendResult>;
}
/** 发送菜单类消息后,若当前会话尚未出现在侧栏历史中则刷新列表 */
export async function ensureShippingHistorySynced(ctx: ShippingConversationContext) {
if (!ctx.conversationId.value && ctx.history.value.length === 0) {
await ctx.loadHistory();
} else if (ctx.conversationId.value) {
const exists = ctx.history.value.some((h) => h.conversation_id === ctx.conversationId.value);
if (!exists) await ctx.loadHistory();
}
}
/**
* 创建发货会话辅助函数(菜单消息发送 + 历史同步)。
*/
export function createShippingConversationHelpers(ctx: ShippingConversationContext) {
const sendShippingMenuMessage = async (message: string) => {
const taskId = ctx.getActiveShippingTaskId();
const text = taskId ? `${message}\n任务编号:${taskId}` : message;
await ctx.sendMessage(text, ctx.currentAgentId.value, ctx.selectedKb.value, [], {
task_id: taskId || undefined,
display_query: message,
});
await ensureShippingHistorySynced(ctx);
};
return { sendShippingMenuMessage, ensureShippingHistorySynced };
}