105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
/**
|
|
* 对话页:加载历史会话、新建对话、切换 Agent 时重置状态。
|
|
*/
|
|
import type { RouteLocationNormalizedLoaded, Router } from 'vue-router';
|
|
import { listChatMessages } from '../api/chat.api';
|
|
import type { ChatMessage } from '../types/chat.types';
|
|
import type { ComputedRef, Ref } from 'vue';
|
|
|
|
export interface ChatPageConversationContext {
|
|
deps: { t: (key: string) => string; msg: { warning: (text: string) => void } };
|
|
route: RouteLocationNormalizedLoaded;
|
|
router: Router;
|
|
currentAgentId: ComputedRef<number>;
|
|
conversationId: Ref<string | null>;
|
|
activeRunId: Ref<number | null>;
|
|
messages: Ref<ChatMessage[]>;
|
|
isThinking: Ref<boolean>;
|
|
loadConversationState: (id: string, loaded: ChatMessage[]) => void;
|
|
createNewChatView: () => void;
|
|
clearPendingNoticeBatchRetry: () => void;
|
|
onConversationIdChanging: (nextId: string, prevId: string | null | undefined) => void;
|
|
onConversationLoadedEmpty: () => Promise<void>;
|
|
onConversationLoadFailed: () => Promise<void>;
|
|
isShippingNoticeAgent: ComputedRef<boolean>;
|
|
shippingTaskIdFromQuery: ComputedRef<string>;
|
|
shippingBatchNoFromQuery?: ComputedRef<string>;
|
|
clearHistory: () => void;
|
|
clearAttachments: () => void;
|
|
input: Ref<string>;
|
|
resetCurrentAgent: () => void;
|
|
copiedIndex: Ref<number | null>;
|
|
resetPeripheralState: () => void;
|
|
filePreviewVisible: Ref<boolean>;
|
|
previewFile: Ref<unknown>;
|
|
previewFiles: Ref<unknown[]>;
|
|
currentPreviewIndex: Ref<number>;
|
|
}
|
|
|
|
export function createChatPageConversationApi(ctx: ChatPageConversationContext) {
|
|
const loadConversation = async (id: string, options?: { force?: boolean }) => {
|
|
const force = options?.force ?? false;
|
|
if (!force && ctx.conversationId.value === id && !ctx.activeRunId.value && ctx.messages.value.length > 0) {
|
|
return;
|
|
}
|
|
|
|
const prevConversationId = ctx.conversationId.value;
|
|
try {
|
|
/**
|
|
* 切换会话前先清空输入框草稿,避免 textarea DOM 里残留上一会话的可见文本,
|
|
* 但父级 `input` 已经是空字符串,最终出现“看起来输入了内容,发送时却被当成空串”的错觉。
|
|
*/
|
|
ctx.input.value = '';
|
|
ctx.onConversationIdChanging(id, prevConversationId);
|
|
const loaded = await listChatMessages(id, ctx.currentAgentId.value);
|
|
ctx.loadConversationState(id, Array.isArray(loaded) ? loaded : []);
|
|
if (!loaded?.length) {
|
|
await ctx.onConversationLoadedEmpty();
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to load conversation', e);
|
|
await ctx.onConversationLoadFailed();
|
|
if (
|
|
ctx.isShippingNoticeAgent.value
|
|
&& (ctx.shippingTaskIdFromQuery.value || ctx.shippingBatchNoFromQuery?.value)
|
|
&& !ctx.isThinking.value
|
|
) {
|
|
return;
|
|
}
|
|
ctx.deps.msg.warning(ctx.deps.t('chat.historyLoadFailed'));
|
|
}
|
|
};
|
|
|
|
const startNewChat = () => {
|
|
ctx.clearPendingNoticeBatchRetry();
|
|
ctx.createNewChatView();
|
|
ctx.clearAttachments();
|
|
ctx.input.value = '';
|
|
ctx.copiedIndex.value = null;
|
|
ctx.resetPeripheralState();
|
|
ctx.filePreviewVisible.value = false;
|
|
ctx.previewFile.value = null;
|
|
ctx.previewFiles.value = [];
|
|
ctx.currentPreviewIndex.value = 0;
|
|
void ctx.router.replace({ path: ctx.route.path, query: {} });
|
|
};
|
|
|
|
const resetChatStateForAgentChange = () => {
|
|
ctx.clearPendingNoticeBatchRetry();
|
|
ctx.createNewChatView();
|
|
ctx.clearHistory();
|
|
ctx.clearAttachments();
|
|
ctx.input.value = '';
|
|
ctx.resetCurrentAgent();
|
|
ctx.copiedIndex.value = null;
|
|
ctx.resetPeripheralState();
|
|
ctx.filePreviewVisible.value = false;
|
|
ctx.previewFile.value = null;
|
|
ctx.previewFiles.value = [];
|
|
ctx.currentPreviewIndex.value = 0;
|
|
};
|
|
|
|
return { loadConversation, startNewChat, resetChatStateForAgentChange };
|
|
}
|