"use client"; /** * Hand-off from a study/homework result into the chat: the compact result * summary and the deck it belongs to travel in sessionStorage and the chat * picks them up on mount, pre-scoped. One-shot, like the deck seed. */ export type ChatSeed = { /** Deck to scope the chat to (when the result belongs to one). */ deckId?: string; /** Human label for the context banner ("Study results"). */ label: string; /** Compact text sent as an extra context chunk with the first request. */ text: string; }; const KEY = "freezmo.chat.seed.v1"; export function setChatSeed(seed: ChatSeed): void { try { window.sessionStorage.setItem(KEY, JSON.stringify(seed)); } catch { /* private mode */ } } export function takeChatSeed(): ChatSeed | null { try { const raw = window.sessionStorage.getItem(KEY); if (!raw) return null; window.sessionStorage.removeItem(KEY); const parsed = JSON.parse(raw) as ChatSeed; if (!parsed || typeof parsed.text !== "string" || !parsed.text.trim()) return null; return { deckId: typeof parsed.deckId === "string" ? parsed.deckId : undefined, label: typeof parsed.label === "string" && parsed.label.trim() ? parsed.label : "Study result", text: parsed.text.slice(0, 6000), }; } catch { return null; } }