| 1 | package transcript |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | |
| 13 | "reasonix/internal/store" |
| 14 | ) |
| 15 | |
| 16 | // LegacyDisplayTurn is read-only compatibility for pre-projection desktops. |
| 17 | // New events and snapshots use MessageID and TurnID, never text matching. |
| 18 | type LegacyDisplayTurn struct { |
| 19 | TurnID string `json:"turnId,omitempty"` |
| 20 | UserMessageID string `json:"userMessageId,omitempty"` |
| 21 | UserHash string `json:"userHash"` |
| 22 | Messages []Message `json:"messages"` |
| 23 | } |
| 24 | |
| 25 | type LegacyDisplays struct { |
| 26 | Users map[string]string |
| 27 | Turns []LegacyDisplayTurn |
| 28 | } |
| 29 | |
| 30 | func LegacyDisplayKey(content string) string { |
| 31 | return fmt.Sprintf("%x", sha256.Sum256([]byte(content))) |
| 32 | } |
| 33 | |
| 34 | func LoadLegacyDisplays(dir, sessionPath string) (LegacyDisplays, error) { |
| 35 | var users map[string]map[string]string |
| 36 | var turns map[string][]LegacyDisplayTurn |
| 37 | if err := readLegacyJSON(store.SessionLegacyDisplays(dir), &users); err != nil { |
| 38 | return LegacyDisplays{}, err |
| 39 | } |
| 40 | if err := readLegacyJSON(store.SessionLegacyPlannerDisplays(dir), &turns); err != nil { |
| 41 | return LegacyDisplays{}, err |
| 42 | } |
| 43 | key := filepath.Base(sessionPath) |
| 44 | return LegacyDisplays{Users: users[key], Turns: turns[key]}, nil |
| 45 | } |
| 46 | |
| 47 | func readLegacyJSON(path string, dst any) error { |
| 48 | if path == "" { |
| 49 | return nil |
| 50 | } |
| 51 | f, err := os.Open(path) |
| 52 | if errors.Is(err, os.ErrNotExist) { |
| 53 | return nil |
| 54 | } |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | defer f.Close() |
| 59 | const maxLegacyBytes = 64 << 20 |
| 60 | b, err := io.ReadAll(io.LimitReader(f, maxLegacyBytes+1)) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | if len(b) > maxLegacyBytes { |
| 65 | return errors.New("legacy transcript display exceeds import limit") |
| 66 | } |
| 67 | return json.Unmarshal(bytes.TrimPrefix(b, []byte{0xef, 0xbb, 0xbf}), dst) |
| 68 | } |
| 69 |