| export type ChatMsg = { role: 'user' | 'agent'; text: string }
|
|
|
| export type ChatSession = {
|
| messages: ChatMsg[]
|
| input: string
|
| progress: number
|
|
|
| isFresh: boolean
|
| }
|
|
|
|
|
| const SESSION_VERSION = 2
|
|
|
| export const ADVISOR_CHAT_STORAGE_KEY = 'advisor_chat_v1'
|
| export const PERSONA_CHAT_STORAGE_KEY = 'persona_chat_v1'
|
|
|
| export const ADVISOR_CHAT_INITIAL_MESSAGE =
|
| "Let's design your advisor panel — we'll walk through the workshop form in order. First, what would you like to name this advisor panel?"
|
|
|
| export const PERSONA_CHAT_INITIAL_MESSAGE =
|
| "Hi! I'm here to help you describe an AI advisor persona for your CCAI project. We'll go through the form in order. First, what would you like to name this persona (the label shown in the app)?"
|
|
|
|
|
| export function loadChatSession(storageKey: string, initialAgentMessage: string): ChatSession {
|
| try {
|
| const raw = localStorage.getItem(storageKey)
|
| if (raw) {
|
| const j = JSON.parse(raw) as Partial<ChatSession> & { version?: number }
|
| if (
|
| j.version === SESSION_VERSION &&
|
| Array.isArray(j.messages) &&
|
| j.messages.length > 0
|
| ) {
|
| return {
|
| messages: j.messages as ChatMsg[],
|
| input: typeof j.input === 'string' ? j.input : '',
|
| progress: typeof j.progress === 'number' ? j.progress : 0,
|
| isFresh: false,
|
| }
|
| }
|
| }
|
| } catch {
|
|
|
| }
|
| return {
|
| messages: [{ role: 'agent', text: initialAgentMessage }],
|
| input: '',
|
| progress: 0,
|
| isFresh: true,
|
| }
|
| }
|
|
|
| export function saveChatSession(
|
| storageKey: string,
|
| session: { messages: ChatMsg[]; input: string; progress: number },
|
| ) {
|
| localStorage.setItem(
|
| storageKey,
|
| JSON.stringify({ version: SESSION_VERSION, ...session }),
|
| )
|
| }
|
|
|
| export function clearChatSession(storageKey: string) {
|
| localStorage.removeItem(storageKey)
|
| }
|
|
|