-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.go
More file actions
45 lines (42 loc) · 886 Bytes
/
Copy pathtranscript.go
File metadata and controls
45 lines (42 loc) · 886 Bytes
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
)
// renderTranscript returns a plain-text view of the session for the preview pane.
func renderTranscript(path string, maxBytes int) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
var b strings.Builder
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 1024*1024), 16*1024*1024)
for sc.Scan() {
var m rawMsg
if err := json.Unmarshal(sc.Bytes(), &m); err != nil {
continue
}
if m.Type != "user" && m.Type != "assistant" {
continue
}
if m.IsMeta {
continue
}
text := firstText(m.Message)
if text == "" {
continue
}
role := strings.ToUpper(m.Type)
fmt.Fprintf(&b, "── %s ──\n%s\n\n", role, text)
if maxBytes > 0 && b.Len() > maxBytes {
b.WriteString("…(truncated)\n")
break
}
}
return b.String(), nil
}