-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompts.go
More file actions
228 lines (200 loc) · 5.44 KB
/
Copy pathprompts.go
File metadata and controls
228 lines (200 loc) · 5.44 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bytes"
"encoding/json"
"fmt"
"hash/fnv"
"io"
"os"
"regexp"
"strings"
"time"
)
// ─── Prompts ───────────────────────────────────────────────────────────────
const transcriptTailBytes = 262144 // 256KB — tool_result entries can push real prompts far back
var (
promptCreateRe = regexp.MustCompile(`create|write|add|implement`)
promptFixRe = regexp.MustCompile(`fix|debug|error`)
)
// fetchPrompts reads the transcript file and returns up to 3 recent user prompts.
func fetchPrompts(transcriptPath string) []string {
if transcriptPath == "" {
return nil
}
f, err := os.Open(transcriptPath)
if err != nil {
return nil
}
defer func() { _ = f.Close() }()
fi, err := f.Stat()
if err != nil {
return nil
}
size := fi.Size()
if size > transcriptTailBytes {
if _, err := f.Seek(size-transcriptTailBytes, io.SeekStart); err != nil {
return nil
}
}
tail, err := io.ReadAll(f)
if err != nil {
return nil
}
lines := bytes.Split(tail, []byte("\n"))
var prompts []string
for i := len(lines) - 1; i >= 0 && len(prompts) < 3; i-- {
line := bytes.TrimSpace(lines[i])
if len(line) == 0 {
continue
}
text := extractUserPromptText(line)
if text != "" {
prompts = append([]string{text}, prompts...)
}
}
return prompts
}
type promptCache struct {
Prompts []string `json:"prompts"`
}
// fetchPromptsWithCache wraps fetchPrompts with a file-based TTL cache.
// If ttl <= 0, caching is disabled and fetchPrompts is called directly.
func fetchPromptsWithCache(transcriptPath string, ttl int) []string {
if ttl <= 0 || transcriptPath == "" {
return fetchPrompts(transcriptPath)
}
h := fnv.New32a()
_, _ = h.Write([]byte(transcriptPath))
cachePath := fmt.Sprintf("/tmp/statusline-prompts-%x.json", h.Sum32())
if info, err := os.Stat(cachePath); err == nil {
if time.Since(info.ModTime()) < time.Duration(ttl)*time.Second {
if data, err := os.ReadFile(cachePath); err == nil {
var cached promptCache
if json.Unmarshal(data, &cached) == nil {
return cached.Prompts
}
}
}
}
prompts := fetchPrompts(transcriptPath)
if data, err := json.Marshal(promptCache{Prompts: prompts}); err == nil {
_ = os.WriteFile(cachePath, data, 0644)
}
return prompts
}
// extractUserPromptText parses a JSONL line and extracts the user prompt text,
// applying all filters from the TS implementation.
func extractUserPromptText(line []byte) string {
// Fast pre-check: must contain "type":"user"
if !bytes.Contains(line, []byte(`"type":"user"`)) {
return ""
}
var entry struct {
Type string `json:"type"`
Message json.RawMessage `json:"message"`
}
if err := json.Unmarshal(line, &entry); err != nil {
return ""
}
if entry.Type != "user" {
return ""
}
// Parse message content
var msg struct {
Content json.RawMessage `json:"content"`
}
if err := json.Unmarshal(entry.Message, &msg); err != nil {
return ""
}
var text string
// Try string first
if err := json.Unmarshal(msg.Content, &text); err != nil {
// Try array of content blocks
var blocks []struct {
Type string `json:"type"`
Text string `json:"text"`
}
if err2 := json.Unmarshal(msg.Content, &blocks); err2 != nil {
return ""
}
for _, b := range blocks {
if b.Type == "text" {
text = b.Text
break
}
}
}
if text == "" {
return ""
}
// Extract slash commands from transcript XML wrapper.
// Claude Code records "/commit --push" as:
// <command-name>/commit</command-name>\n<command-message>commit --push</command-message>\n...
// Pull the command-message content and use it as the prompt text.
slashExtracted := false
if strings.Contains(text, "<command-message>") {
if start := strings.Index(text, "<command-message>"); start >= 0 {
start += len("<command-message>")
if end := strings.Index(text[start:], "</command-message>"); end >= 0 {
cmdText := strings.TrimSpace(text[start : start+end])
if cmdText != "" {
if !strings.HasPrefix(cmdText, "/") {
cmdText = "/" + cmdText
}
text = cmdText
slashExtracted = true
// Fall through to remaining filters.
}
}
}
}
// Apply filters
// Filter messages that look like XML/system tags (not real user prompts)
if strings.HasPrefix(text, "<") {
return ""
}
if strings.HasPrefix(text, "You are a") {
return ""
}
if strings.HasPrefix(text, `{"`) {
return ""
}
if len(text) > 500 {
return ""
}
if strings.Contains(text, "<system-reminder>") {
return ""
}
if !slashExtracted && (strings.Contains(text, "<command-name>") || strings.Contains(text, "<command-message>")) {
return ""
}
if strings.Contains(text, "<task-notification>") {
return ""
}
return strings.TrimSpace(strings.ReplaceAll(text, "\n", " "))
}
// truncateWords truncates text to at most max words, appending "..." if needed.
func truncateWords(text string, max int) string {
words := strings.Fields(text)
if len(words) > max {
return strings.Join(words[:max], " ") + "..."
}
return strings.Join(words, " ")
}
// getPromptIcon returns an icon character based on the prompt content.
func getPromptIcon(prompt string) string {
if strings.HasPrefix(prompt, "/") {
return "»"
}
if strings.Contains(prompt, "?") {
return "?"
}
lower := strings.ToLower(prompt)
if promptCreateRe.MatchString(lower) {
return "+"
}
if promptFixRe.MatchString(lower) {
return "×"
}
return "›"
}