Spaces:
Running
Running
Download src/services/persistence.ts from aut4rk/private-voice-agent: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/aut4rk/private-voice-agent/resolve/4e3468620e7861b7df10fffa5abe4552da16ba09/src/services/persistence.ts
- Command line
-
hf download hf://spaces/aut4rk/private-voice-agent@4e3468620e7861b7df10fffa5abe4552da16ba09/src/services/persistence.ts
-
curl -L -o persistence.ts https://huggingface.co/spaces/aut4rk/private-voice-agent/resolve/4e3468620e7861b7df10fffa5abe4552da16ba09/src/services/persistence.ts
1.22 kB
| import type { PersistedSession, PersistenceService } from "../app/types"; | |
| import { PERSISTENCE_VERSION } from "../app/types"; | |
| const STORAGE_KEY = "private-voice-agent/session"; | |
| interface StorageLike { | |
| getItem(key: string): string | null; | |
| setItem(key: string, value: string): void; | |
| removeItem(key: string): void; | |
| } | |
| export class LocalPersistenceService implements PersistenceService { | |
| #storage: StorageLike | null; | |
| constructor(storage: StorageLike | null = typeof localStorage !== "undefined" ? localStorage : null) { | |
| this.#storage = storage; | |
| } | |
| load(): PersistedSession | null { | |
| if (!this.#storage) { | |
| return null; | |
| } | |
| const raw = this.#storage.getItem(STORAGE_KEY); | |
| if (!raw) { | |
| return null; | |
| } | |
| try { | |
| const parsed = JSON.parse(raw) as PersistedSession; | |
| if (parsed.version !== PERSISTENCE_VERSION) { | |
| this.clear(); | |
| return null; | |
| } | |
| return parsed; | |
| } catch { | |
| this.clear(); | |
| return null; | |
| } | |
| } | |
| save(value: PersistedSession): void { | |
| if (!this.#storage) { | |
| return; | |
| } | |
| this.#storage.setItem(STORAGE_KEY, JSON.stringify(value)); | |
| } | |
| clear(): void { | |
| this.#storage?.removeItem(STORAGE_KEY); | |
| } | |
| } | |