07d4740d0c
Claude Code records subagent/peer reports, task notifications, reminders and skill bodies as user-role entries. Classify them server-side (history and live stream) by origin/isMeta/queued_command mode: reports become kind agent_report, task notifications kind notice, harness meta is hidden, and the mid-turn wrapper is unwrapped to the operator's text. The app draws a collapsed Agent report card and a one-line system notice on the agent's side.
448 lines
14 KiB
Go
448 lines
14 KiB
Go
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"`
|
|
// Attachment is set on "attachment" entries; a queued_command with
|
|
// commandMode "prompt" is a user message the CLI read mid-turn. Prompt is
|
|
// either a string or an array of content blocks.
|
|
Attachment struct {
|
|
Type string `json:"type"`
|
|
Prompt json.RawMessage `json:"prompt"`
|
|
CommandMode string `json:"commandMode"`
|
|
IsMeta bool `json:"isMeta"`
|
|
Origin json.RawMessage `json:"origin"`
|
|
} `json:"attachment"`
|
|
// IsMeta marks a user entry the CLI wrote itself (a skill body, an image
|
|
// placeholder, a reminder). Origin says where injected input came from: a
|
|
// peer or subagent report, or a task notification. An operator prompt has
|
|
// neither.
|
|
IsMeta bool `json:"isMeta"`
|
|
Origin json.RawMessage `json:"origin"`
|
|
}
|
|
|
|
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
|
|
addUser := func(entry transcriptEntry, text string, files []protocol.ChatPart) {
|
|
id := takePromptID(text)
|
|
if id == "" {
|
|
id = entry.UUID
|
|
}
|
|
if id == "" {
|
|
return
|
|
}
|
|
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})
|
|
}
|
|
injectedSeen := map[string]bool{}
|
|
addInjected := func(entry transcriptEntry, item injected) {
|
|
if item.id == "" || injectedSeen[item.id] {
|
|
return
|
|
}
|
|
injectedSeen[item.id] = true
|
|
created, _ := transcriptTime(entry.Timestamp)
|
|
messages = append(messages, item.message(s.id, created))
|
|
}
|
|
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":
|
|
origin := decodeOrigin(entry.Origin)
|
|
if len(entry.Message.Content) > 0 && entry.Message.Content[0] == '"' {
|
|
// A string user entry is harness input (local command output, a
|
|
// reminder, a task notification or an agent's report), never a
|
|
// turn the operator sent. Reports and notices are shown as such.
|
|
var raw string
|
|
if json.Unmarshal(entry.Message.Content, &raw) == nil {
|
|
if item := classifyInjected(raw, origin, entry.IsMeta); item.class == classAgentReport || item.class == classNotice {
|
|
addInjected(entry, item)
|
|
}
|
|
}
|
|
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
|
|
}
|
|
if text != "" {
|
|
switch item := classifyInjected(text, origin, entry.IsMeta); item.class {
|
|
case classAgentReport, classNotice:
|
|
addInjected(entry, item)
|
|
continue
|
|
case classHidden:
|
|
continue
|
|
default:
|
|
text = item.text
|
|
}
|
|
} else if entry.IsMeta {
|
|
continue
|
|
}
|
|
addUser(entry, text, files)
|
|
case "attachment":
|
|
// A message the operator sent while the turn was running is recorded
|
|
// as a queued_command attachment where the CLI surfaced it to the
|
|
// model, so it is the user's turn at that point of the transcript.
|
|
// Other queued commands (task notifications, and agent reports the
|
|
// CLI also queues as "prompt" with an origin) are harness input:
|
|
// they are shown as notices and report cards, never as the operator's.
|
|
if entry.Attachment.Type != "queued_command" {
|
|
continue
|
|
}
|
|
mode := entry.Attachment.CommandMode
|
|
if mode != "prompt" && mode != "task-notification" {
|
|
continue
|
|
}
|
|
var text string
|
|
for _, block := range decodeBlocks(entry.Attachment.Prompt) {
|
|
if block.Type == "text" && strings.TrimSpace(block.Text) != "" {
|
|
if text != "" {
|
|
text += "\n"
|
|
}
|
|
text += block.Text
|
|
}
|
|
}
|
|
if strings.TrimSpace(text) == "" {
|
|
continue
|
|
}
|
|
origin := decodeOrigin(entry.Attachment.Origin)
|
|
if mode == "task-notification" && origin.Kind == "" {
|
|
origin.Kind = "task-notification"
|
|
}
|
|
switch item := classifyInjected(text, origin, entry.Attachment.IsMeta); item.class {
|
|
case classAgentReport, classNotice:
|
|
addInjected(entry, item)
|
|
continue
|
|
case classHidden:
|
|
continue
|
|
default:
|
|
text = item.text
|
|
}
|
|
addUser(entry, text, []protocol.ChatPart{})
|
|
}
|
|
}
|
|
// 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)
|
|
}
|