feat(session): persist last request state for UI restoration
Sessions now record the input-bar state used for the most recent QA request (agent, model, KB scope, web search). The chat UI hydrates those settings on session reopen so users see the same configuration they used last time, instead of the global default. The state is stored in the existing sessions.agent_config JSONB column to avoid a new migration. Frontend snapshots the user's global defaults on session enter and restores them on session leave, so opening an old session does not pollute new-chat defaults.
This commit is contained in:
@@ -126,6 +126,28 @@ const route = useRoute();
|
||||
const router = useRouter();
|
||||
const session_id = ref(props.session_id || route.params.chatid);
|
||||
const sessionData = ref(null);
|
||||
|
||||
// 拉 session 详情,并按其 last_request_state 把输入栏状态恢复到当时的发起态。
|
||||
// 嵌入式(embeddedMode)由宿主页面注入 agent/KB,所以跳过整套恢复逻辑,
|
||||
// 避免污染宿主的 settings store。
|
||||
const loadSessionAndHydrate = async (sid) => {
|
||||
if (!sid || props.embeddedMode) return;
|
||||
try {
|
||||
const sessionRes = await getSession(sid);
|
||||
if (sessionRes?.data) {
|
||||
sessionData.value = sessionRes.data;
|
||||
const lastState = sessionRes.data.last_request_state;
|
||||
if (lastState) {
|
||||
// 先把当前的"全局默认"快照下来,再用 session 状态覆盖;
|
||||
// 离开会话时会从快照还原,避免本会话的状态污染新建对话。
|
||||
useSettingsStoreInstance.snapshotAsDefaultsIfNeeded();
|
||||
useSettingsStoreInstance.applyLastRequestState(lastState);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load session data:', error);
|
||||
}
|
||||
};
|
||||
const inputFieldRef = ref();
|
||||
const created_at = ref('');
|
||||
const limit = ref(20);
|
||||
@@ -252,8 +274,13 @@ watch([() => route.params], (newvalue) => {
|
||||
isReplying.value = false;
|
||||
currentAssistantMessageId.value = '';
|
||||
userHasScrolledUp.value = false;
|
||||
|
||||
|
||||
// 跨会话切换:先把旧会话覆盖前的全局默认还原,再让新会话重新拍快照
|
||||
// 并应用自己的 last_request_state(在 loadSessionAndHydrate 内部完成)。
|
||||
useSettingsStoreInstance.restoreDefaultsIfSnapshotted();
|
||||
|
||||
checkmenuTitle(session_id.value)
|
||||
loadSessionAndHydrate(session_id.value);
|
||||
let data = {
|
||||
session_id: session_id.value,
|
||||
created_at: '',
|
||||
@@ -1229,16 +1256,9 @@ onMounted(async () => {
|
||||
loading.value = false;
|
||||
isReplying.value = false;
|
||||
|
||||
// Load session data to get agent_config
|
||||
try {
|
||||
const sessionRes = await getSession(session_id.value);
|
||||
if (sessionRes?.data) {
|
||||
sessionData.value = sessionRes.data;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load session data:', error);
|
||||
}
|
||||
|
||||
// 拉会话详情;若服务端记录了 last_request_state,则按其恢复输入栏状态。
|
||||
await loadSessionAndHydrate(session_id.value);
|
||||
|
||||
checkmenuTitle(session_id.value)
|
||||
if (firstQuery.value) {
|
||||
scrollLock.value = true;
|
||||
@@ -1270,10 +1290,14 @@ onUnmounted(() => {
|
||||
});
|
||||
onBeforeRouteLeave((to, from, next) => {
|
||||
clearData()
|
||||
// 离开聊天会话 → 还原"用户全局默认",避免旧会话的请求态泄漏到新建对话。
|
||||
useSettingsStoreInstance.restoreDefaultsIfSnapshotted();
|
||||
next()
|
||||
})
|
||||
onBeforeRouteUpdate((to, from, next) => {
|
||||
clearData()
|
||||
// 仅"会话 → 会话"会落到这里;跨会话覆盖的还原放到 route.params 的 watch 里,
|
||||
// 因为新会话的 getSession 也在那边触发,便于保证 restore→snapshot→apply 顺序。
|
||||
next()
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user