Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
6bd5f9f
1
Parent(s):
d2f4c97
feat(utils): add branch storage utility
Browse files
src/lib/utils/branchStorage.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { browser } from "$app/environment";
|
| 2 |
+
|
| 3 |
+
export interface BranchStateSnapshot {
|
| 4 |
+
messageId: string;
|
| 5 |
+
personaId: string;
|
| 6 |
+
personaName: string;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export interface BranchTrackingSnapshot {
|
| 10 |
+
messageId: string | null;
|
| 11 |
+
personaId: string | null;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
const BRANCH_STATE_PREFIX = "branchState_";
|
| 15 |
+
const BRANCH_TRACKING_PREFIX = "branchTracking_";
|
| 16 |
+
|
| 17 |
+
function getBranchStateKey(conversationId: string): string {
|
| 18 |
+
return `${BRANCH_STATE_PREFIX}${conversationId}`;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
function getBranchTrackingKey(conversationId: string): string {
|
| 22 |
+
return `${BRANCH_TRACKING_PREFIX}${conversationId}`;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
export function loadBranchState(conversationId: string): BranchStateSnapshot | null {
|
| 26 |
+
if (!browser) return null;
|
| 27 |
+
const stored = localStorage.getItem(getBranchStateKey(conversationId));
|
| 28 |
+
if (!stored) return null;
|
| 29 |
+
try {
|
| 30 |
+
return JSON.parse(stored) as BranchStateSnapshot;
|
| 31 |
+
} catch {
|
| 32 |
+
localStorage.removeItem(getBranchStateKey(conversationId));
|
| 33 |
+
return null;
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
export function persistBranchState(
|
| 38 |
+
conversationId: string,
|
| 39 |
+
state: BranchStateSnapshot | null
|
| 40 |
+
): void {
|
| 41 |
+
if (!browser) return;
|
| 42 |
+
const key = getBranchStateKey(conversationId);
|
| 43 |
+
if (state) {
|
| 44 |
+
localStorage.setItem(key, JSON.stringify(state));
|
| 45 |
+
} else {
|
| 46 |
+
localStorage.removeItem(key);
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
export function loadBranchTracking(conversationId: string): BranchTrackingSnapshot {
|
| 51 |
+
if (!browser) return { messageId: null, personaId: null };
|
| 52 |
+
const stored = localStorage.getItem(getBranchTrackingKey(conversationId));
|
| 53 |
+
if (!stored) return { messageId: null, personaId: null };
|
| 54 |
+
try {
|
| 55 |
+
const parsed = JSON.parse(stored) as BranchTrackingSnapshot;
|
| 56 |
+
return {
|
| 57 |
+
messageId: parsed.messageId ?? null,
|
| 58 |
+
personaId: parsed.personaId ?? null,
|
| 59 |
+
};
|
| 60 |
+
} catch {
|
| 61 |
+
localStorage.removeItem(getBranchTrackingKey(conversationId));
|
| 62 |
+
return { messageId: null, personaId: null };
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
export function persistBranchTracking(
|
| 67 |
+
conversationId: string,
|
| 68 |
+
tracking: BranchTrackingSnapshot | null
|
| 69 |
+
): void {
|
| 70 |
+
if (!browser) return;
|
| 71 |
+
const key = getBranchTrackingKey(conversationId);
|
| 72 |
+
if (tracking && (tracking.messageId || tracking.personaId)) {
|
| 73 |
+
localStorage.setItem(key, JSON.stringify(tracking));
|
| 74 |
+
} else {
|
| 75 |
+
localStorage.removeItem(key);
|
| 76 |
+
}
|
| 77 |
+
}
|