// Package transcript extracts context from Claude Code transcript JSONL files. // It finds the last assistant text message, strips markdown, and formats it as // language-appropriate comment lines suitable for prepending to a diff display pane. package transcript import ( "bufio" "encoding/json" "os" "strings" "unicode" ) // BeginMarker and EndMarker bracket the context block written into the file // under review. They are deliberately distinctive so diff.StripContextBlock // can locate and remove the block after the user's edit session without // colliding with natural file content. const ( BeginMarker = "" EndMarker = "" ) type transcriptLine struct { Type string `json:"type"` Message struct { Content []struct { Type string `json:"type"` Text string `json:"text"` } `json:"content"` } `json:"message"` } // CommentPrefix returns the line-comment prefix for a file extension. func CommentPrefix(ext string) string { switch ext { case ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".js", ".jsx", ".ts", ".tsx", ".rs", ".swift", ".kt", ".kts", ".cs", ".scala", ".groovy", ".m": return "//" case ".lua", ".hs", ".sql": return "--" case ".vim", ".el", ".clj", ".cljs", ".lisp", ".scm": return ";" default: return "#" } } // Extract reads the JSONL transcript at path, finds the last assistant text // message, strips markdown formatting, and returns it formatted as comment // lines appropriate for the given file extension, wrapped to width columns. // Returns an empty string when nothing useful can be extracted. func Extract(path, ext string, width int) string { if path == "" { return "" } text := lastAssistantText(path) if text == "" { return "" } summary := stripMarkdown(text) if summary == "" { return "" } return format(summary, CommentPrefix(ext), width) } // lastAssistantText scans the JSONL file and returns the text from the last // assistant message that contains at least one non-empty text block. func lastAssistantText(path string) string { f, err := os.Open(path) //nolint:gosec // G304: path comes from hook input, trusted if err != nil { return "" } defer f.Close() //nolint:errcheck var last string sc := bufio.NewScanner(f) sc.Buffer(make([]byte, 1<<20), 1<<20) // 1 MiB — handles large tool outputs for sc.Scan() { var line transcriptLine if err := json.Unmarshal(sc.Bytes(), &line); err != nil { continue } if line.Type != "assistant" { continue } for _, c := range line.Message.Content { if c.Type == "text" && c.Text != "" { last = c.Text break } } } return last } // stripMarkdown removes formatting tokens and returns the first meaningful line. func stripMarkdown(text string) string { for raw := range strings.SplitSeq(text, "\n") { raw = strings.ReplaceAll(raw, "**", "") raw = strings.ReplaceAll(raw, "`", "") // Skip code fences and insight decorator lines. if strings.HasPrefix(raw, "```") || strings.HasPrefix(raw, "★") || strings.HasPrefix(raw, "─") { continue } line := strings.TrimLeftFunc(raw, unicode.IsSpace) if line != "" { return line } } return "" } // wordWrap breaks text into lines of at most width characters on word boundaries. func wordWrap(text string, width int) []string { words := strings.Fields(text) if len(words) == 0 { return nil } var lines []string cur := words[0] for _, w := range words[1:] { if len(cur)+1+len(w) > width { lines = append(lines, cur) cur = w } else { cur += " " + w } } return append(lines, cur) } // format wraps each line of summary in a comment prefix and brackets the // whole block in BeginMarker / EndMarker sentinel lines so a downstream // strip step can locate and remove it from the file under review. // The first body line carries a "[context]" tag for visual distinguishability. func format(summary, prefix string, width int) string { wrapped := wordWrap(summary, width) if len(wrapped) == 0 { return "" } var sb strings.Builder sb.WriteString(prefix + " " + BeginMarker) for i, line := range wrapped { if i == 0 { sb.WriteString("\n" + prefix + " [context] " + line) } else { sb.WriteString("\n" + prefix + " " + line) } } sb.WriteString("\n" + prefix + " " + EndMarker) return sb.String() }