aboutsummaryrefslogtreecommitdiff
path: root/internal/claude/preview.go
blob: 01ab906bcbb5122d143589134d01955a6297dc92 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package claude

import (
	"bufio"
	"encoding/json"
	"os"
	"strings"
)

type RawMessage struct {
	Type    string          `json:"type"`
	Message json.RawMessage `json:"message"`
}

type UserMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type AssistantMessage struct {
	Role    string         `json:"role"`
	Content []ContentBlock `json:"content"`
}

type ContentBlock struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	Thinking string `json:"thinking,omitempty"`
	Name     string `json:"name,omitempty"`
	Input    any    `json:"input,omitempty"`
}

type PreviewContent struct {
	Messages []PreviewMessage
	Error    string
}

type PreviewMessage struct {
	Role    string
	Content string
}

func LoadSessionPreview(session *Session, maxMessages int) PreviewContent {
	if session == nil || session.FullPath == "" {
		return PreviewContent{Error: "No session selected"}
	}

	file, openError := os.Open(session.FullPath)

	if openError != nil {
		return PreviewContent{Error: "Could not open session file"}
	}

	defer func() { _ = file.Close() }()

	var messages []PreviewMessage

	scanner := bufio.NewScanner(file)
	scanBuffer := make([]byte, 0, 64*1024)

	scanner.Buffer(scanBuffer, 10*1024*1024)

	for scanner.Scan() {
		line := scanner.Text()

		if line == "" {
			continue
		}

		var rawMessage RawMessage

		if unmarshalError := json.Unmarshal([]byte(line), &rawMessage); unmarshalError != nil {
			continue
		}

		parsedMessages := parseRawMessage(rawMessage)
		messages = append(messages, parsedMessages...)
	}

	if len(messages) > maxMessages {
		messages = messages[len(messages)-maxMessages:]
	}

	if len(messages) == 0 {
		return PreviewContent{Error: "No messages in session"}
	}

	return PreviewContent{Messages: messages}
}

func parseRawMessage(rawMessage RawMessage) []PreviewMessage {
	var result []PreviewMessage

	switch rawMessage.Type {
	case "user":
		var userMessage UserMessage

		if unmarshalError := json.Unmarshal(rawMessage.Message, &userMessage); unmarshalError != nil {
			return nil
		}

		content := userMessage.Content

		if len(content) > 500 {
			content = content[:500] + " …"
		}

		if content != "" {
			result = append(result, PreviewMessage{Role: "user", Content: content})
		}
	case "assistant":
		var assistantMessage AssistantMessage

		if unmarshalError := json.Unmarshal(rawMessage.Message, &assistantMessage); unmarshalError != nil {
			return nil
		}

		for _, contentBlock := range assistantMessage.Content {
			switch contentBlock.Type {
			case "text":
				text := contentBlock.Text

				if len(text) > 500 {
					text = text[:500] + " …"
				}

				if text != "" {
					result = append(result, PreviewMessage{Role: "assistant", Content: text})
				}
			case "tool_use":
				toolInfo := contentBlock.Name

				if toolInfo != "" {
					if inputMap, isMap := contentBlock.Input.(map[string]interface{}); isMap {
						if command, exists := inputMap["command"]; exists {
							if commandString, isString := command.(string); isString {
								if len(commandString) > 60 {
									commandString = commandString[:60] + " …"
								}

								toolInfo += ": " + commandString
							}
						} else if pattern, exists := inputMap["pattern"]; exists {
							if patternString, isString := pattern.(string); isString {
								toolInfo += ": " + patternString
							}
						} else if filePath, exists := inputMap["file_path"]; exists {
							if filePathString, isString := filePath.(string); isString {
								pathParts := strings.Split(filePathString, "/")
								toolInfo += ": " + pathParts[len(pathParts)-1]
							}
						}
					}

					result = append(result, PreviewMessage{Role: "tool", Content: toolInfo})
				}
			case "thinking":
				thinking := contentBlock.Thinking

				if len(thinking) > 200 {
					thinking = thinking[:200] + " …"
				}

				if thinking != "" {
					result = append(result, PreviewMessage{Role: "thinking", Content: thinking})
				}
			}
		}
	}

	return result
}