Przeglądaj źródła

WIP: 建立快速模式与 Agent 显示偏好

[gstack-context]
Decisions: fast mode 按 account/profile/session 持久化并支持 draft 迁移;show thinking 按 account/profile 持久化
Remaining: fast 9119 同步、Agent 显示设置、闪电 UI、完整回归
Skill: /gitnexus-exploring + /frontend-skill
[/gstack-context]
samien 2 miesięcy temu
rodzic
commit
32724f7e07

+ 31 - 0
src/client/utils/sessionPreferences.ts

@@ -0,0 +1,31 @@
+const FAST_MODE_PREFIX = 'hermes-yaoyao:fast-mode'
+const THINKING_PREFIX = 'hermes-yaoyao:show-thinking'
+
+function scopedKey(prefix: string, userId: string, profile: string, sessionId?: string): string {
+  return [prefix, userId || 'local', profile || 'default', sessionId || ''].map(encodeURIComponent).join('|')
+}
+
+export function readSessionFastMode(userId: string, profile: string, sessionId: string): boolean {
+  try { return localStorage.getItem(scopedKey(FAST_MODE_PREFIX, userId, profile, sessionId)) === '1' } catch { return false }
+}
+
+export function writeSessionFastMode(userId: string, profile: string, sessionId: string, enabled: boolean): void {
+  try {
+    const key = scopedKey(FAST_MODE_PREFIX, userId, profile, sessionId)
+    if (enabled) localStorage.setItem(key, '1')
+    else localStorage.removeItem(key)
+  } catch { /* optional local mirror */ }
+}
+
+export function moveSessionFastMode(userId: string, profile: string, fromSessionId: string, toSessionId: string, enabled: boolean): void {
+  writeSessionFastMode(userId, profile, fromSessionId, false)
+  writeSessionFastMode(userId, profile, toSessionId, enabled)
+}
+
+export function readAgentShowThinking(userId: string, profile: string): boolean {
+  try { return localStorage.getItem(scopedKey(THINKING_PREFIX, userId, profile)) !== '0' } catch { return true }
+}
+
+export function writeAgentShowThinking(userId: string, profile: string, visible: boolean): void {
+  try { localStorage.setItem(scopedKey(THINKING_PREFIX, userId, profile), visible ? '1' : '0') } catch { /* optional preference */ }
+}

+ 30 - 0
tests/client/sessionPreferences.test.ts

@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest'
+import {
+  moveSessionFastMode, readAgentShowThinking, readSessionFastMode, writeAgentShowThinking, writeSessionFastMode,
+} from '@/utils/sessionPreferences'
+
+describe('session and Agent display preferences', () => {
+  it('isolates fast mode by account, Agent, and session', () => {
+    writeSessionFastMode('u1', 'yaoer', 's1', true)
+    expect(readSessionFastMode('u1', 'yaoer', 's1')).toBe(true)
+    expect(readSessionFastMode('u1', 'default', 's1')).toBe(false)
+    expect(readSessionFastMode('u2', 'yaoer', 's1')).toBe(false)
+    expect(readSessionFastMode('u1', 'yaoer', 's2')).toBe(false)
+  })
+
+  it('moves a draft fast-mode selection to the stored session identity', () => {
+    writeSessionFastMode('u1', 'default', 'draft-1', true)
+    moveSessionFastMode('u1', 'default', 'draft-1', 'stored-1', true)
+    expect(readSessionFastMode('u1', 'default', 'draft-1')).toBe(false)
+    expect(readSessionFastMode('u1', 'default', 'stored-1')).toBe(true)
+  })
+
+  it('stores show-thinking once per account and Agent', () => {
+    expect(readAgentShowThinking('u1', 'yaoer')).toBe(true)
+    writeAgentShowThinking('u1', 'yaoer', false)
+    expect(readAgentShowThinking('u1', 'yaoer')).toBe(false)
+    expect(readAgentShowThinking('u1', 'default')).toBe(true)
+    writeAgentShowThinking('u1', 'yaoer', true)
+    expect(readAgentShowThinking('u1', 'yaoer')).toBe(true)
+  })
+})