Spaces:
Sleeping
Sleeping
File size: 4,946 Bytes
3286713 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import type { EmotionState } from "@/lib/emily-api";
export interface JournalRecord {
id: string;
text: string;
mood: string | null;
responseText: string;
emotionState: EmotionState | null;
createdAt: string;
triggers: string[];
}
export interface DraftRecord {
id: string;
text: string;
mood: string | null;
createdAt: string;
}
export interface UserSnapshot {
draft: string;
mood: string | null;
entries: JournalRecord[];
drafts: DraftRecord[];
}
export const ACTIVE_USER_KEY = "echo-active-user";
const MAX_ENTRIES = 80;
const MAX_DRAFTS = 40;
export const EMPTY_SNAPSHOT: UserSnapshot = {
draft: "",
mood: null,
entries: [],
drafts: [],
};
const TRIGGER_RULES: Array<{ name: string; keywords: string[] }> = [
{ name: "Work Stress", keywords: ["work", "job", "office", "deadline", "boss"] },
{ name: "Social Media", keywords: ["instagram", "social", "online", "scroll", "twitter"] },
{ name: "Late Nights", keywords: ["late", "night", "sleep", "insomnia", "tired"] },
{ name: "Unresolved Talks", keywords: ["fight", "argument", "talk", "family", "friend"] },
{ name: "Financial Worry", keywords: ["money", "rent", "debt", "finance", "bills"] },
];
function hasWindow(): boolean {
return typeof window !== "undefined";
}
export function getUserStorageKey(userId: string): string {
return `echo-user-${userId}`;
}
export function getActiveUser(): string {
if (!hasWindow()) return "";
return localStorage.getItem(ACTIVE_USER_KEY) ?? "";
}
export function setActiveUser(userId: string): void {
if (!hasWindow()) return;
localStorage.setItem(ACTIVE_USER_KEY, userId);
}
export function clearActiveUser(): void {
if (!hasWindow()) return;
localStorage.removeItem(ACTIVE_USER_KEY);
}
export function getSnapshot(userId: string): UserSnapshot {
if (!hasWindow() || !userId) return { ...EMPTY_SNAPSHOT };
const raw = localStorage.getItem(getUserStorageKey(userId));
if (!raw) return { ...EMPTY_SNAPSHOT };
try {
const parsed = JSON.parse(raw) as UserSnapshot;
return {
draft: parsed.draft ?? "",
mood: parsed.mood ?? null,
entries: parsed.entries ?? [],
drafts: parsed.drafts ?? [],
};
} catch {
return { ...EMPTY_SNAPSHOT };
}
}
export function saveSnapshot(userId: string, snapshot: UserSnapshot): void {
if (!hasWindow() || !userId) return;
localStorage.setItem(getUserStorageKey(userId), JSON.stringify(snapshot));
}
export function appendEntry(snapshot: UserSnapshot, next: JournalRecord): UserSnapshot {
return {
...snapshot,
entries: [next, ...snapshot.entries].slice(0, MAX_ENTRIES),
};
}
export function appendDraft(snapshot: UserSnapshot, text: string): UserSnapshot {
if (!text.trim()) return snapshot;
const nextDraft: DraftRecord = {
id: `draft-${Date.now()}`,
text: text.trim(),
mood: snapshot.mood,
createdAt: new Date().toISOString(),
};
return {
...snapshot,
drafts: [nextDraft, ...snapshot.drafts].slice(0, MAX_DRAFTS),
};
}
export function deleteDraft(snapshot: UserSnapshot, draftId: string): UserSnapshot {
return {
...snapshot,
drafts: snapshot.drafts.filter((d) => d.id !== draftId),
};
}
export function extractTriggers(text: string): string[] {
const normalized = text.toLowerCase();
return TRIGGER_RULES.filter((rule) => rule.keywords.some((k) => normalized.includes(k))).map((rule) => rule.name);
}
export function toMoodLabel(entries: JournalRecord[]): string {
if (!entries.length) return "No data";
const scoreByMood: Record<string, number> = { Anxious: -2, Sad: -1, Neutral: 0, Good: 1, Great: 2 };
const scored = entries.map((entry) => scoreByMood[entry.mood ?? "Neutral"] ?? 0);
const avg = scored.reduce((sum, v) => sum + v, 0) / scored.length;
if (avg >= 1.5) return "Great";
if (avg >= 0.5) return "Good";
if (avg > -0.5) return "Neutral";
if (avg > -1.5) return "Sad";
return "Anxious";
}
export function buildInsights(entries: JournalRecord[]): string[] {
if (!entries.length) return ["No insights yet. Add journal entries to unlock pattern insights."];
const averageValence =
entries.reduce((sum, e) => sum + (e.emotionState?.emotional_valence ?? 0), 0) / entries.length;
const triggerCounts = new Map<string, number>();
for (const entry of entries) {
for (const trigger of entry.triggers) {
triggerCounts.set(trigger, (triggerCounts.get(trigger) ?? 0) + 1);
}
}
const topTrigger = Array.from(triggerCounts.entries()).sort((a, b) => b[1] - a[1])[0]?.[0];
const results = [
averageValence < -0.2
? "Trend: recent valence looks low. Try smaller recovery routines."
: "Trend: recent valence is stable or improving.",
`Mood trend: ${toMoodLabel(entries)}.`,
];
if (topTrigger) results.push(`Top trigger: ${topTrigger}.`);
return results;
}
export function formatLocalDate(iso: string): string {
return new Date(iso).toLocaleString();
}
|