feat(chat): add Claude Code chat backend
This commit is contained in:
@@ -0,0 +1,358 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// maxTranscriptBytes bounds one history read. A Claude session file grows with
|
||||
// every turn; past this bound the caller is told the truth instead of being
|
||||
// served a silently truncated conversation.
|
||||
const maxTranscriptBytes = 64 << 20
|
||||
|
||||
type transcriptEntry struct {
|
||||
Type string `json:"type"`
|
||||
Subtype string `json:"subtype"`
|
||||
UUID string `json:"uuid"`
|
||||
IsSidechain bool `json:"isSidechain"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Cwd string `json:"cwd"`
|
||||
Message struct {
|
||||
ID string `json:"id"`
|
||||
Role string `json:"role"`
|
||||
Model string `json:"model"`
|
||||
Content json.RawMessage `json:"content"`
|
||||
} `json:"message"`
|
||||
ToolUseResult json.RawMessage `json:"toolUseResult"`
|
||||
Compact json.RawMessage `json:"compactMetadata"`
|
||||
}
|
||||
|
||||
func readTranscript(path string) ([]transcriptEntry, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if info.Size() > maxTranscriptBytes {
|
||||
return nil, fmt.Errorf("the Claude session transcript is larger than the %d MiB history limit", maxTranscriptBytes>>20)
|
||||
}
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 0, 64<<10), maxLineBytes)
|
||||
var entries []transcriptEntry
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
var entry transcriptEntry
|
||||
if json.Unmarshal(line, &entry) == nil {
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// transcriptSession describes a session from its retained file, so the manager
|
||||
// can resolve a registry row after a restart.
|
||||
func transcriptSession(id string) (protocol.ChatSession, bool) {
|
||||
path := findTranscriptFile(id)
|
||||
if path == "" {
|
||||
return protocol.ChatSession{}, false
|
||||
}
|
||||
session := protocol.ChatSession{ID: id, Backend: Backend, ModelProvider: Backend, Tag: protocol.ChatSessionTagOperator}
|
||||
entries, err := readTranscript(path)
|
||||
if err != nil {
|
||||
return session, true
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.Cwd != "" && session.Directory == "" {
|
||||
session.Directory = entry.Cwd
|
||||
}
|
||||
if stamp, ok := transcriptTime(entry.Timestamp); ok {
|
||||
if session.CreatedAt == 0 || stamp < session.CreatedAt {
|
||||
session.CreatedAt = stamp
|
||||
}
|
||||
if stamp > session.UpdatedAt {
|
||||
session.UpdatedAt = stamp
|
||||
}
|
||||
}
|
||||
if entry.Type == "assistant" && entry.Message.Model != "" && !entry.IsSidechain {
|
||||
session.Model = entry.Message.Model
|
||||
}
|
||||
}
|
||||
return session, true
|
||||
}
|
||||
|
||||
func transcriptTime(text string) (int64, bool) {
|
||||
if strings.TrimSpace(text) == "" {
|
||||
return 0, false
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339Nano, text)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return parsed.UnixMilli(), true
|
||||
}
|
||||
|
||||
// transcriptMessages maps the CLI's own session file into chat messages. It
|
||||
// merges the JSONL's one-entry-per-content-block shape by message id, attaches
|
||||
// tool results to the call that produced them, and never invents a turn.
|
||||
func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
||||
path := findTranscriptFile(s.id)
|
||||
if path == "" {
|
||||
return protocol.ChatHistoryPage{Messages: []protocol.ChatMessage{}}, nil
|
||||
}
|
||||
entries, err := readTranscript(path)
|
||||
if err != nil {
|
||||
return protocol.ChatHistoryPage{}, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
prompts := append([]promptRecord(nil), s.prompts...)
|
||||
s.mu.Unlock()
|
||||
promptIDs := map[string][]string{}
|
||||
for _, prompt := range prompts {
|
||||
if prompt.id == "" || prompt.text == "" {
|
||||
continue
|
||||
}
|
||||
promptIDs[prompt.text] = append(promptIDs[prompt.text], prompt.id)
|
||||
}
|
||||
usedPrompts := map[string]int{}
|
||||
takePromptID := func(text string) string {
|
||||
text = strings.TrimSpace(text)
|
||||
ids := promptIDs[text]
|
||||
if len(ids) == 0 {
|
||||
return ""
|
||||
}
|
||||
index := usedPrompts[text]
|
||||
if index >= len(ids) {
|
||||
return ""
|
||||
}
|
||||
usedPrompts[text] = index + 1
|
||||
return ids[index]
|
||||
}
|
||||
messages := []protocol.ChatMessage{}
|
||||
messageIndex := map[string]int{}
|
||||
toolIndex := map[string][2]int{} // tool id -> message/part index
|
||||
for _, entry := range entries {
|
||||
if entry.IsSidechain {
|
||||
continue
|
||||
}
|
||||
switch entry.Type {
|
||||
case "assistant":
|
||||
messageID := strings.TrimSpace(entry.Message.ID)
|
||||
if messageID == "" {
|
||||
messageID = entry.UUID
|
||||
}
|
||||
if messageID == "" {
|
||||
continue
|
||||
}
|
||||
created, _ := transcriptTime(entry.Timestamp)
|
||||
index, exists := messageIndex[messageID]
|
||||
if !exists {
|
||||
messages = append(messages, protocol.ChatMessage{ID: messageID, SessionID: s.id, Role: protocol.ChatRoleAssistant, Model: entry.Message.Model, CreatedAt: created, Parts: []protocol.ChatPart{}})
|
||||
index = len(messages) - 1
|
||||
messageIndex[messageID] = index
|
||||
}
|
||||
if entry.Message.Model != "" {
|
||||
messages[index].Model = entry.Message.Model
|
||||
}
|
||||
blocks := decodeBlocks(entry.Message.Content)
|
||||
for _, block := range blocks {
|
||||
switch block.Type {
|
||||
case "text":
|
||||
if strings.TrimSpace(block.Text) == "" {
|
||||
continue
|
||||
}
|
||||
ordinal := textOrdinal(messages[index].Parts, protocol.ChatPartText)
|
||||
part := protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartText, ordinal), Text: block.Text}
|
||||
messages[index].Parts = append(messages[index].Parts, part)
|
||||
case "thinking":
|
||||
if strings.TrimSpace(block.Thinking) == "" {
|
||||
continue
|
||||
}
|
||||
ordinal := textOrdinal(messages[index].Parts, protocol.ChatPartReasoning)
|
||||
part := protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, ordinal), Text: block.Thinking}
|
||||
messages[index].Parts = append(messages[index].Parts, part)
|
||||
case "tool_use":
|
||||
if block.ID == "" {
|
||||
continue
|
||||
}
|
||||
part := protocol.ChatPart{
|
||||
Type: protocol.ChatPartTool,
|
||||
ID: block.ID,
|
||||
Name: block.Name,
|
||||
State: &protocol.ChatToolState{
|
||||
Status: protocol.ChatToolRunning,
|
||||
Input: truncateJSON(block.Input, 64<<10),
|
||||
},
|
||||
}
|
||||
messages[index].Parts = append(messages[index].Parts, part)
|
||||
toolIndex[block.ID] = [2]int{index, len(messages[index].Parts) - 1}
|
||||
}
|
||||
}
|
||||
case "user":
|
||||
if len(entry.Message.Content) > 0 && entry.Message.Content[0] == '"' {
|
||||
// A string user entry is harness feedback (local command output or
|
||||
// a tool protocol message), never a turn the operator sent.
|
||||
continue
|
||||
}
|
||||
blocks := decodeBlocks(entry.Message.Content)
|
||||
var text string
|
||||
files := []protocol.ChatPart{}
|
||||
hasResult := false
|
||||
for _, block := range blocks {
|
||||
switch block.Type {
|
||||
case "tool_result":
|
||||
hasResult = true
|
||||
applyTranscriptToolResult(&messages, toolIndex, block)
|
||||
case "text":
|
||||
if strings.TrimSpace(block.Text) == "" {
|
||||
continue
|
||||
}
|
||||
if text != "" {
|
||||
text += "\n"
|
||||
}
|
||||
text += block.Text
|
||||
case "image":
|
||||
if block.Source != nil && block.Source.Data != "" && len(block.Source.Data) <= 512<<10 {
|
||||
files = append(files, protocol.ChatPart{Type: protocol.ChatPartFile, Mime: block.Source.MediaType, Name: "image", URI: "data:" + block.Source.MediaType + ";base64," + block.Source.Data})
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasResult || (text == "" && len(files) == 0) {
|
||||
continue
|
||||
}
|
||||
id := takePromptID(text)
|
||||
if id == "" {
|
||||
id = entry.UUID
|
||||
}
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
created, _ := transcriptTime(entry.Timestamp)
|
||||
parts := []protocol.ChatPart{}
|
||||
if text != "" {
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#text", Text: text})
|
||||
}
|
||||
parts = append(parts, files...)
|
||||
messages = append(messages, protocol.ChatMessage{ID: id, SessionID: s.id, Role: protocol.ChatRoleUser, CreatedAt: created, Parts: parts})
|
||||
}
|
||||
}
|
||||
// Drop assistant messages that ended with no renderable part (for example a
|
||||
// signature-only thinking entry).
|
||||
filtered := messages[:0]
|
||||
for _, message := range messages {
|
||||
if len(message.Parts) > 0 {
|
||||
filtered = append(filtered, message)
|
||||
}
|
||||
}
|
||||
messages = filtered
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
offset := 0
|
||||
if strings.TrimSpace(cursor) != "" {
|
||||
if parsed, err := strconv.Atoi(cursor); err == nil && parsed >= 0 {
|
||||
offset = parsed
|
||||
}
|
||||
}
|
||||
if offset > len(messages) {
|
||||
offset = len(messages)
|
||||
}
|
||||
end := len(messages) - offset
|
||||
start := end - limit
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
page := protocol.ChatHistoryPage{Messages: append([]protocol.ChatMessage{}, messages[start:end]...)}
|
||||
if start > 0 {
|
||||
page.HasMore = true
|
||||
page.NextCursor = strconv.Itoa(offset + len(page.Messages))
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func textOrdinal(parts []protocol.ChatPart, kind string) int {
|
||||
count := 0
|
||||
for _, part := range parts {
|
||||
if part.Type == kind {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func applyTranscriptToolResult(messages *[]protocol.ChatMessage, toolIndex map[string][2]int, block contentBlock) {
|
||||
position, ok := toolIndex[block.ToolUseID]
|
||||
if !ok || position[0] >= len(*messages) {
|
||||
return
|
||||
}
|
||||
message := &(*messages)[position[0]]
|
||||
if position[1] >= len(message.Parts) {
|
||||
return
|
||||
}
|
||||
part := &message.Parts[position[1]]
|
||||
if part.State == nil {
|
||||
part.State = &protocol.ChatToolState{}
|
||||
}
|
||||
text, images := toolResultContent(block.Content)
|
||||
status := protocol.ChatToolCompleted
|
||||
var toolErr *protocol.ChatError
|
||||
exit := exitCode(text)
|
||||
if block.IsError || (exit != nil && *exit != 0) {
|
||||
status = protocol.ChatToolError
|
||||
detail := strings.TrimSpace(text)
|
||||
if detail == "" {
|
||||
detail = "the tool returned an error"
|
||||
}
|
||||
toolErr = &protocol.ChatError{Type: "tool", Message: boundedText(detail, 2<<10)}
|
||||
}
|
||||
part.State.Status = status
|
||||
part.State.Error = toolErr
|
||||
if text != "" {
|
||||
part.State.Content = append(part.State.Content, protocol.ChatToolContent{Type: "text", Text: boundedText(text, 128<<10)})
|
||||
}
|
||||
part.State.Content = append(part.State.Content, images...)
|
||||
}
|
||||
|
||||
// Messages serves the authoritative transcript history.
|
||||
func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
||||
s := c.session(id)
|
||||
if s == nil {
|
||||
meta, ok := transcriptSession(id)
|
||||
if !ok {
|
||||
return protocol.ChatHistoryPage{}, errors.New("unknown Claude session " + id)
|
||||
}
|
||||
s = c.sessionFor(id, meta.Directory)
|
||||
}
|
||||
return s.transcriptMessages(limit, cursor)
|
||||
}
|
||||
|
||||
// sessionTranscriptPath is used by tests and diagnostics.
|
||||
func sessionTranscriptPath(id string) string {
|
||||
path := findTranscriptFile(id)
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
return filepath.Clean(path)
|
||||
}
|
||||
Reference in New Issue
Block a user