~bigbes/lethe

ref: ad5ee6528aae4f5d70cee77f87bd9b275eb21928 lethe/internal/collector/parser/claudecode/parser.go -rw-r--r-- 11.1 KiB
ad5ee652 — Eugene Blikh collector: add ingest sender and outbox replay 24 days ago
                                                                                
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package claudecode

import (
	"bufio"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"sourcecraft.dev/bigbes/lethe/internal/collector/parser"
	"sourcecraft.dev/bigbes/lethe/internal/shared/wire"
)

const toolName = "claude-code"

// Parser maps Claude Code JSONL transcripts into lethe wire events.
type Parser struct {
	host string
}

// New builds a parser that stamps every emitted event with host.
func New(host string) *Parser {
	return &Parser{host: host}
}

// Tool returns the collector-facing tool name.
func (p *Parser) Tool() string {
	return toolName
}

// Discover walks root recursively and returns every JSONL transcript.
func (p *Parser) Discover(root string) ([]parser.SourceFile, error) {
	files := make([]parser.SourceFile, 0)
	err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		if filepath.Ext(path) != ".jsonl" {
			return nil
		}
		info, err := d.Info()
		if err != nil {
			return err
		}
		files = append(files, parser.SourceFile{Path: path, Size: info.Size()})
		return nil
	})
	if err != nil {
		return nil, err
	}
	sort.Slice(files, func(i, j int) bool {
		return files[i].Path < files[j].Path
	})
	return files, nil
}

// Parse reads complete newline-terminated records from path starting at since.
// A partial trailing line is left for the next poll so offsets never land in
// the middle of a JSON object that Claude is still writing.
func (p *Parser) Parse(path string, since int64) ([]wire.TurnEvent, int64, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, since, err
	}
	defer func() { _ = f.Close() }()

	info, err := f.Stat()
	if err != nil {
		return nil, since, err
	}
	if since < 0 || since > info.Size() {
		since = 0
	}
	if _, err := f.Seek(since, io.SeekStart); err != nil {
		return nil, since, err
	}

	r := bufio.NewReaderSize(f, 1<<20)
	offset := since
	events := make([]wire.TurnEvent, 0)
	for {
		lineStart := offset
		line, err := r.ReadBytes('\n')
		switch {
		case errors.Is(err, io.EOF) && len(line) == 0:
			return events, offset, nil
		case errors.Is(err, io.EOF):
			return events, offset, nil
		case err != nil:
			return events, offset, err
		}
		offset += int64(len(line))

		recordBytes := strings.TrimSpace(string(line))
		if recordBytes == "" {
			continue
		}
		event, ok := p.mapRecord(path, lineStart, []byte(recordBytes), info.ModTime())
		if ok {
			events = append(events, event)
		}
	}
}

type transcriptRecord struct {
	Type                    string          `json:"type"`
	UUID                    string          `json:"uuid"`
	Timestamp               string          `json:"timestamp"`
	CWD                     string          `json:"cwd"`
	SessionID               string          `json:"sessionId"`
	GitBranch               string          `json:"gitBranch"`
	Version                 string          `json:"version"`
	ParentUUID              *string         `json:"parentUuid"`
	SourceToolAssistantUUID string          `json:"sourceToolAssistantUUID"`
	IsSidechain             bool            `json:"isSidechain"`
	Message                 *messageRecord  `json:"message"`
	ToolUseResult           json.RawMessage `json:"toolUseResult"`
}

type messageRecord struct {
	Role    string          `json:"role"`
	Model   string          `json:"model"`
	Content json.RawMessage `json:"content"`
	Usage   *usageRecord    `json:"usage"`
}

type usageRecord struct {
	InputTokens  int64 `json:"input_tokens"`
	OutputTokens int64 `json:"output_tokens"`
}

type contentPart struct {
	Type        string          `json:"type"`
	Text        string          `json:"text"`
	Name        string          `json:"name"`
	Input       json.RawMessage `json:"input"`
	Content     string          `json:"content"`
	ToolUseID   string          `json:"tool_use_id"`
	IsError     bool            `json:"is_error"`
	Description string          `json:"description"`
}

func (p *Parser) mapRecord(path string, seq int64, raw []byte, fallbackTime time.Time) (wire.TurnEvent, bool) {
	var record transcriptRecord
	if err := json.Unmarshal(raw, &record); err != nil {
		return p.systemFallback(path, seq, raw, fallbackTime, fmt.Sprintf("invalid claude json: %v", err)), true
	}
	if record.Message == nil {
		return wire.TurnEvent{}, false
	}

	timestamp, err := parseTimestamp(record.Timestamp)
	if err != nil {
		return p.systemFallback(path, seq, raw, fallbackTime, fmt.Sprintf("invalid claude timestamp: %v", err)), true
	}
	base := wire.TurnEvent{
		Tool:      toolName,
		Host:      p.host,
		SessionID: sessionIDFor(path, record.SessionID),
		TurnID:    turnIDFor(record, seq, timestamp, raw),
		Seq:       seq,
		Timestamp: timestamp,
		SessionMeta: wire.SessionMeta{
			WorkingDir: stringPtrOrNil(record.CWD),
			SourceFile: path,
		},
		Metadata: cloneRaw(raw),
	}

	text, parts, err := parseContent(record.Message.Content)
	if err != nil {
		return p.systemFallback(path, seq, raw, fallbackTime, fmt.Sprintf("invalid claude content: %v", err)), true
	}
	toolParts := filterParts(parts, "tool_use")
	toolResultParts := filterParts(parts, "tool_result")

	switch record.Message.Role {
	case "user":
		if text != "" {
			base.Role = "user"
			base.Content = text
			return base, true
		}
		if len(toolResultParts) > 0 {
			base.Role = "tool"
			base.Content = renderToolResult(record.ToolUseResult, toolResultParts)
			base.ToolCalls = toolResultPayload(record.ToolUseResult, toolResultParts)
			return base, true
		}
	case "assistant":
		if text != "" {
			base.Role = "assistant"
			base.Content = text
			base.Model = stringPtrOrNil(record.Message.Model)
			base.TokensIn = int64PtrOrNil(record.Message.Usage, func(u *usageRecord) int64 { return u.InputTokens })
			base.TokensOut = int64PtrOrNil(record.Message.Usage, func(u *usageRecord) int64 { return u.OutputTokens })
			if len(toolParts) > 0 {
				base.ToolCalls = marshalParts(toolParts)
			}
			return base, true
		}
		if len(toolParts) > 0 {
			base.Role = "tool"
			base.Content = renderToolUse(toolParts)
			base.Model = stringPtrOrNil(record.Message.Model)
			base.TokensIn = int64PtrOrNil(record.Message.Usage, func(u *usageRecord) int64 { return u.InputTokens })
			base.TokensOut = int64PtrOrNil(record.Message.Usage, func(u *usageRecord) int64 { return u.OutputTokens })
			base.ToolCalls = marshalParts(toolParts)
			return base, true
		}
	case "system":
		if text != "" {
			base.Role = "system"
			base.Content = text
			return base, true
		}
	}

	return wire.TurnEvent{}, false
}

func (p *Parser) systemFallback(path string, seq int64, raw []byte, fallbackTime time.Time, content string) wire.TurnEvent {
	timestamp := fallbackTime.Unix()
	return wire.TurnEvent{
		Tool:      toolName,
		Host:      p.host,
		SessionID: sessionIDFor(path, ""),
		TurnID:    synthesizedTurnID(sessionIDFor(path, ""), seq, timestamp, raw),
		Seq:       seq,
		Role:      "system",
		Timestamp: timestamp,
		Content:   content,
		SessionMeta: wire.SessionMeta{
			SourceFile: path,
		},
		Metadata: cloneRaw(raw),
	}
}

func parseContent(raw json.RawMessage) (string, []contentPart, error) {
	trimmed := strings.TrimSpace(string(raw))
	if trimmed == "" || trimmed == "null" {
		return "", nil, nil
	}
	if trimmed[0] == '"' {
		var text string
		if err := json.Unmarshal(raw, &text); err != nil {
			return "", nil, err
		}
		return strings.TrimSpace(text), nil, nil
	}
	parts := make([]contentPart, 0)
	if err := json.Unmarshal(raw, &parts); err != nil {
		return "", nil, err
	}
	texts := make([]string, 0)
	for _, part := range parts {
		if part.Type == "text" && strings.TrimSpace(part.Text) != "" {
			texts = append(texts, strings.TrimSpace(part.Text))
		}
	}
	return strings.Join(texts, "\n\n"), parts, nil
}

func filterParts(parts []contentPart, want string) []contentPart {
	out := make([]contentPart, 0)
	for _, part := range parts {
		if part.Type == want {
			out = append(out, part)
		}
	}
	return out
}

func renderToolUse(parts []contentPart) string {
	if len(parts) == 0 {
		return "<tool_use>"
	}
	part := parts[0]
	label := part.Name
	if label == "" {
		label = "tool"
	}
	var input struct {
		Description string `json:"description"`
	}
	if err := json.Unmarshal(part.Input, &input); err == nil && strings.TrimSpace(input.Description) != "" {
		return fmt.Sprintf("<tool_use: %s - %s>", label, strings.TrimSpace(input.Description))
	}
	return fmt.Sprintf("<tool_use: %s>", label)
}

func renderToolResult(toolUseResult json.RawMessage, parts []contentPart) string {
	summary := firstToolResultSummary(toolUseResult, parts)
	if summary == "" {
		return "<tool_result>"
	}
	return fmt.Sprintf("<tool_result: %s>", summary)
}

func firstToolResultSummary(toolUseResult json.RawMessage, parts []contentPart) string {
	var payload struct {
		Stdout string `json:"stdout"`
		Stderr string `json:"stderr"`
	}
	if len(toolUseResult) > 0 && json.Unmarshal(toolUseResult, &payload) == nil {
		if summary := summarizeText(payload.Stdout); summary != "" {
			return summary
		}
		if summary := summarizeText(payload.Stderr); summary != "" {
			return summary
		}
	}
	for _, part := range parts {
		if summary := summarizeText(part.Content); summary != "" {
			return summary
		}
	}
	return ""
}

func summarizeText(text string) string {
	text = strings.TrimSpace(text)
	if text == "" {
		return ""
	}
	line := text
	if idx := strings.IndexByte(line, '\n'); idx >= 0 {
		line = line[:idx]
	}
	line = strings.TrimSpace(line)
	if len(line) > 120 {
		line = line[:117] + "..."
	}
	return line
}

func toolResultPayload(toolUseResult json.RawMessage, parts []contentPart) json.RawMessage {
	if len(toolUseResult) > 0 {
		return cloneRaw(toolUseResult)
	}
	return marshalParts(parts)
}

func marshalParts(parts []contentPart) json.RawMessage {
	if len(parts) == 0 {
		return nil
	}
	b, err := json.Marshal(parts)
	if err != nil {
		return nil
	}
	return b
}

func parseTimestamp(raw string) (int64, error) {
	if strings.TrimSpace(raw) == "" {
		return 0, errors.New("missing timestamp")
	}
	ts, err := time.Parse(time.RFC3339Nano, raw)
	if err != nil {
		return 0, err
	}
	return ts.Unix(), nil
}

func sessionIDFor(path, sessionID string) string {
	if strings.TrimSpace(sessionID) != "" {
		return sessionID
	}
	base := filepath.Base(path)
	return strings.TrimSuffix(base, filepath.Ext(base))
}

func turnIDFor(record transcriptRecord, seq, timestamp int64, raw []byte) string {
	if strings.TrimSpace(record.UUID) != "" {
		return record.UUID
	}
	return synthesizedTurnID(sessionIDFor("", record.SessionID), seq, timestamp, raw)
}

func synthesizedTurnID(sessionID string, seq, timestamp int64, raw []byte) string {
	sum := sha256.Sum256([]byte(fmt.Sprintf("%s|%d|%d|%s", sessionID, seq, timestamp, summarizeText(string(raw)))))
	return hex.EncodeToString(sum[:8])
}

func cloneRaw(raw json.RawMessage) json.RawMessage {
	if len(raw) == 0 {
		return nil
	}
	out := make([]byte, len(raw))
	copy(out, raw)
	return out
}

func stringPtrOrNil(value string) *string {
	value = strings.TrimSpace(value)
	if value == "" {
		return nil
	}
	return &value
}

func int64PtrOrNil[T any](value *T, get func(*T) int64) *int64 {
	if value == nil {
		return nil
	}
	v := get(value)
	if v == 0 {
		return nil
	}
	return &v
}