feat: enhance theme management and image caching functionality

This commit is contained in:
DEV_DSW
2026-04-16 15:43:35 +08:00
parent 411f4f3421
commit b1f589a674
13 changed files with 74 additions and 183 deletions

View File

@@ -9,6 +9,7 @@ import { extractText, isToolOnlyMessage, isToolResultRole, isInternalMessage } f
import { hostApiFetch } from '@lib/host-api'
import { gatewayRpc, onGatewayEvent } from '@lib/gateway-client'
import { useProviderStore } from '@stores/providers'
import { IPC_EVENTS, CONFIG_KEYS } from '@lib/constants'
// ── Constants ───────────────────────────────────────────────────
const DEFAULT_SESSION_KEY = 'agent:main:main'
@@ -32,7 +33,6 @@ let _lastLoadSessionsAt = 0
const _historyLoadInFlight = new Map<string, Promise<void>>()
const _lastHistoryLoadAtBySession = new Map<string, number>()
const _chatEventDedupe = new Map<string, number>()
const IMAGE_CACHE_KEY = 'zn-ai:image-cache'
const IMAGE_CACHE_MAX = 100
// ── Helpers: Timers ─────────────────────────────────────────────
@@ -90,29 +90,35 @@ function isDuplicateChatEvent(eventState: string, event: Record<string, unknown>
}
// ── Helpers: Image Cache ────────────────────────────────────────
function loadImageCache(): Map<string, AttachedFileMeta> {
let _imageCache: Map<string, AttachedFileMeta> = new Map()
let _imageCacheInitialized = false
async function initImageCache(): Promise<void> {
if (_imageCacheInitialized) return
_imageCache = await loadImageCache()
_imageCacheInitialized = true
}
async function loadImageCache(): Promise<Map<string, AttachedFileMeta>> {
try {
const raw = localStorage.getItem(IMAGE_CACHE_KEY)
if (raw) {
const entries = JSON.parse(raw) as Array<[string, AttachedFileMeta]>
return new Map(entries)
const raw = await window.api.invoke(IPC_EVENTS.GET_CONFIG, CONFIG_KEYS.IMAGE_CACHE)
if (Array.isArray(raw)) {
return new Map(raw as Array<[string, AttachedFileMeta]>)
}
} catch { /* ignore */ }
return new Map()
}
function saveImageCache(cache: Map<string, AttachedFileMeta>): void {
async function saveImageCache(cache: Map<string, AttachedFileMeta>): Promise<void> {
try {
const entries = Array.from(cache.entries())
const trimmed = entries.length > IMAGE_CACHE_MAX
? entries.slice(entries.length - IMAGE_CACHE_MAX)
: entries
localStorage.setItem(IMAGE_CACHE_KEY, JSON.stringify(trimmed))
await window.api.invoke(IPC_EVENTS.SET_CONFIG, CONFIG_KEYS.IMAGE_CACHE, trimmed)
} catch { /* ignore */ }
}
const _imageCache = loadImageCache()
// ── Helpers: Timestamp ──────────────────────────────────────────
function toMs(ts: number): number {
return ts < 1e12 ? ts * 1000 : ts
@@ -918,6 +924,7 @@ export const useChatStore = defineStore('chat', {
try {
// Cache image attachments
if (attachments && attachments.length > 0) {
await initImageCache()
for (const a of attachments) {
_imageCache.set(a.stagedPath, {
fileName: a.fileName,
@@ -926,7 +933,7 @@ export const useChatStore = defineStore('chat', {
preview: a.preview,
})
}
saveImageCache(_imageCache)
await saveImageCache(_imageCache)
}
let messageContent = trimmed