refactor(frontend): 重构前端第二版

This commit is contained in:
sophie committed 2026-06-11 13:47:06 +08:00
1 parent 2631f78b00
commit 4b562cfa82
205 files changed
+15399 -12015

No files matched your search

@@ -0,0 +1,80 @@
/**
* 对话页:加载历史会话、新建对话、切换 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;
onConversationIdChanging: (nextId: string, prevId: string | null | undefined) => void;
onConversationLoadedEmpty: () => Promise<void>;
onConversationLoadFailed: () => Promise<void>;
isShippingNoticeAgent: ComputedRef<boolean>;
shippingTaskIdFromQuery: ComputedRef<string>;
clearHistory: () => void;
clearAttachments: () => void;
input: Ref<string>;
resetCurrentAgent: () => void;
copiedIndex: Ref<number | null>;
resetPeripheralState: () => void;
filePreviewVisible: Ref<boolean>;
previewFile: Ref<unknown>;
}
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 {
ctx.onConversationIdChanging(id, prevConversationId);
const loaded = await listChatMessages(id, ctx.currentAgentId.value);
ctx.loadConversationState(id, Array.isArray(loaded) ? loaded : []);
if (!loaded?.length) {
ctx.deps.msg.warning(ctx.deps.t('chat.historyEmpty'));
await ctx.onConversationLoadedEmpty();
return;
}
} catch (e) {
console.error('Failed to load conversation', e);
await ctx.onConversationLoadFailed();
if (ctx.isShippingNoticeAgent.value && ctx.shippingTaskIdFromQuery.value && !ctx.isThinking.value) {
return;
}
ctx.deps.msg.warning(ctx.deps.t('chat.historyLoadFailed'));
}
};
const startNewChat = () => {
ctx.createNewChatView();
void ctx.router.replace({ path: ctx.route.path, query: {} });
};
const resetChatStateForAgentChange = () => {
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;
};
return { loadConversation, startNewChat, resetChatStateForAgentChange };
}