File size: 1,302 Bytes
26c925a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"
// Per-session notes — markdown notes attached to a session
// Stored as { [sessionId]: notesString } in localStorage

const KEY = "tans-agents:session-notes-v1"

function readAll(): Record<string, string> {
  if (typeof window === "undefined") return {}
  try {
    const raw = localStorage.getItem(KEY)
    return raw ? JSON.parse(raw) : {}
  } catch {
    return {}
  }
}

function writeAll(notes: Record<string, string>) {
  if (typeof window === "undefined") return
  try {
    localStorage.setItem(KEY, JSON.stringify(notes))
    window.dispatchEvent(new Event("tans:notes-changed"))
  } catch {}
}

export function getNotes(sessionId: string): string {
  return readAll()[sessionId] ?? ""
}

export function setNotes(sessionId: string, content: string) {
  const all = readAll()
  if (!content.trim()) {
    delete all[sessionId]
  } else {
    all[sessionId] = content.slice(0, 50_000)
  }
  writeAll(all)
}

export function hasNotes(sessionId: string): boolean {
  return Boolean(readAll()[sessionId])
}

export function listSessionsWithNotes(): { sessionId: string; preview: string; length: number }[] {
  const all = readAll()
  return Object.entries(all).map(([sessionId, content]) => ({
    sessionId,
    preview: content.slice(0, 100),
    length: content.length,
  }))
}