package claude import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "regexp" "strconv" "strings" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // ---- CLI line envelope ----------------------------------------------------- type lineEnvelope struct { Type string `json:"type"` Subtype string `json:"subtype"` UUID string `json:"uuid"` SessionID string `json:"session_id"` ParentToolUseID string `json:"parent_tool_use_id"` Message json.RawMessage `json:"message"` Event json.RawMessage `json:"event"` RequestID string `json:"request_id"` Request json.RawMessage `json:"request"` Response json.RawMessage `json:"response"` Status string `json:"status"` CompactMetadata json.RawMessage `json:"compactMetadata"` Result string `json:"result"` IsError bool `json:"is_error"` TotalCostUSD float64 `json:"total_cost_usd"` Usage json.RawMessage `json:"usage"` } type contentBlock struct { Type string `json:"type"` Text string `json:"text"` Thinking string `json:"thinking"` ID string `json:"id"` Name string `json:"name"` Input json.RawMessage `json:"input"` ToolUseID string `json:"tool_use_id"` Content json.RawMessage `json:"content"` IsError bool `json:"is_error"` Source *struct { Type string `json:"type"` MediaType string `json:"media_type"` Data string `json:"data"` } `json:"source"` } type messageBody struct { ID string `json:"id"` Role string `json:"role"` Model string `json:"model"` Content json.RawMessage `json:"content"` } type toolRequest struct { Subtype string `json:"subtype"` ToolName string `json:"tool_name"` DisplayName string `json:"display_name"` Description string `json:"description"` Input json.RawMessage `json:"input"` PermissionSuggestions json.RawMessage `json:"permission_suggestions"` ToolUseID string `json:"tool_use_id"` DecisionReason string `json:"decision_reason"` } type streamEvent struct { Type string `json:"type"` Index int `json:"index"` Message *messageBody `json:"message"` ContentBlock *contentBlock `json:"content_block"` Delta *struct { Type string `json:"type"` Text string `json:"text"` Thinking string `json:"thinking"` PartialJSON string `json:"partial_json"` } `json:"delta"` } // ---- Dispatch -------------------------------------------------------------- func (s *session) handleLine(p *process, line []byte) { var envelope lineEnvelope if err := json.Unmarshal(line, &envelope); err != nil { return } // Subagent traffic is a sidechain: its text and tools are not the operator's // main turn, and history excludes the same entries (isSidechain). if envelope.ParentToolUseID != "" { return } switch envelope.Type { case "control_request": s.handleControlRequest(p, envelope) case "control_response": s.handleControlResponse(p, envelope) case "stream_event": s.handleStreamEvent(envelope) case "assistant": s.handleAssistant(envelope) case "user": s.handleUser(envelope) case "result": s.handleResult(envelope) case "system": s.handleSystem(envelope) } } func (s *session) handleControlResponse(p *process, envelope lineEnvelope) { var reply struct { Subtype string `json:"subtype"` RequestID string `json:"request_id"` Response json.RawMessage `json:"response"` Error string `json:"error"` } if json.Unmarshal(envelope.Response, &reply) != nil || reply.RequestID == "" { return } if p == nil { return } p.mu.Lock() ch := p.pending[reply.RequestID] delete(p.pending, reply.RequestID) p.mu.Unlock() if ch == nil { return } if reply.Subtype == "error" { select { case ch <- controlReply{err: reply.Error}: default: } return } select { case ch <- controlReply{response: reply.Response}: default: } } func (s *session) handleControlRequest(p *process, envelope lineEnvelope) { var request toolRequest if json.Unmarshal(envelope.Request, &request) != nil { return } switch request.Subtype { case "can_use_tool": s.receivePermission(p, envelope.RequestID, request) case "hook_callback", "mcp_message": s.respondUnsupported(p, envelope.RequestID) default: s.respondUnsupported(p, envelope.RequestID) } } func (s *session) respondUnsupported(p *process, requestID string) { if p == nil || requestID == "" { return } _ = p.write(map[string]any{ "type": "control_response", "response": map[string]any{ "subtype": "error", "request_id": requestID, "error": "This Pipelines server does not support that Claude Code interaction", }, }) } // ---- Streaming text -------------------------------------------------------- func (s *session) handleStreamEvent(envelope lineEnvelope) { var event streamEvent if json.Unmarshal(envelope.Event, &event) != nil { return } s.mu.Lock() defer s.mu.Unlock() switch event.Type { case "message_start": s.streamID = "" if event.Message != nil { s.streamID = event.Message.ID } if s.streamID == "" { s.streamID = envelope.UUID } s.streamBlocks = map[int]blockState{} s.streamText = map[int]string{} case "content_block_start": if event.ContentBlock == nil { return } kind := streamKind(event.ContentBlock.Type) if kind == "" { return } ordinal := s.streamOrdinals[kind] s.streamOrdinals[kind] = ordinal + 1 s.streamBlocks[event.Index] = blockState{kind: kind, ordinal: ordinal} case "content_block_delta": if event.Delta == nil { return } block, ok := s.streamBlocks[event.Index] kind := block.kind if !ok { kind = streamKind(deltaKind(event.Delta.Type)) if kind == "" { return } block = blockState{kind: kind, ordinal: s.streamOrdinals[kind]} s.streamOrdinals[kind]++ s.streamBlocks[event.Index] = block } var delta string switch event.Delta.Type { case "text_delta": if block.kind != protocol.ChatPartText { return } delta = event.Delta.Text case "thinking_delta": if block.kind != protocol.ChatPartReasoning { return } delta = event.Delta.Thinking default: return } if delta == "" { return } s.streamText[event.Index] += delta messageID := s.streamID if messageID == "" { return } partID := protocol.ChatDeltaPartID(messageID, block.kind, block.ordinal) s.broadcastLocked(protocol.ChatEventFrame{ SessionID: s.id, NativeTurnID: s.turnID, MessageID: messageID, Kind: block.kind, Delta: delta, Part: &protocol.ChatPart{ID: partID, Type: block.kind}, }) case "message_stop": // Keep the assembled blocks: the CLI sends the completed assistant // message after message_stop, and that message must not repeat text // already delivered as deltas. message_start resets them for the next one. } } func streamKind(blockType string) string { switch blockType { case "text": return protocol.ChatPartText case "thinking", "reasoning": return protocol.ChatPartReasoning default: return "" } } func deltaKind(deltaType string) string { switch deltaType { case "text_delta": return "text" case "thinking_delta": return "thinking" default: return "" } } // broadcastLocked emits while the session lock is already held: the caller is a // stream-delta path that owns state it just updated. func (s *session) broadcastLocked(frame protocol.ChatEventFrame) { targets := make([]chan protocol.ChatEventFrame, 0, len(s.subs)) for ch := range s.subs { targets = append(targets, ch) } for _, ch := range targets { select { case ch <- frame: default: } } } // ---- Completed messages ---------------------------------------------------- func (s *session) handleAssistant(envelope lineEnvelope) { var body messageBody if json.Unmarshal(envelope.Message, &body) != nil { return } messageID := strings.TrimSpace(body.ID) if messageID == "" { messageID = envelope.UUID } if messageID == "" { return } blocks := decodeBlocks(body.Content) s.mu.Lock() if body.Model != "" { s.modelActual = body.Model } // Align the completed message with what was streamed, then emit only the // remainder: the CLI sends deltas first and the full block afterwards. var frames []protocol.ChatEventFrame for index, block := range blocks { switch block.Type { case "text", "thinking": kind := protocol.ChatPartText full := block.Text if block.Type == "thinking" { kind = protocol.ChatPartReasoning full = block.Thinking } if strings.TrimSpace(full) == "" { continue } state, ok := s.streamBlocks[index] if !ok || state.kind != kind { state = blockState{kind: kind, ordinal: s.streamOrdinals[kind]} s.streamOrdinals[kind]++ } streamed := s.streamText[index] delta := full if strings.HasPrefix(full, streamed) { delta = full[len(streamed):] } else if streamed != "" { // The stream and the final block disagree; never duplicate the // whole answer. The authoritative transcript carries the full text. delta = "" } if delta == "" { continue } frames = append(frames, protocol.ChatEventFrame{ SessionID: s.id, NativeTurnID: s.turnID, MessageID: messageID, Kind: kind, Delta: delta, Part: &protocol.ChatPart{ID: protocol.ChatDeltaPartID(messageID, kind, state.ordinal), Type: kind}, }) case "tool_use": if block.ID == "" { continue } s.toolMessages[block.ID] = messageID input := truncateJSON(block.Input, 64<<10) frames = append(frames, protocol.ChatEventFrame{ SessionID: s.id, NativeTurnID: s.turnID, MessageID: messageID, Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{ Type: protocol.ChatPartTool, ID: block.ID, Name: block.Name, State: &protocol.ChatToolState{ Status: protocol.ChatToolRunning, Input: input, }, }, }) if block.Name == "Bash" { if shell := s.recordShellLocked(block.ID, bashCommand(input)); shell != nil { frames = append(frames, protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: shell}) } } } } s.streamBlocks = map[int]blockState{} s.streamText = map[int]string{} s.streamOrdinals = map[string]int{} framesCopy := frames s.mu.Unlock() for _, frame := range framesCopy { s.emit(frame) } } func (s *session) handleUser(envelope lineEnvelope) { var body messageBody if json.Unmarshal(envelope.Message, &body) != nil { return } blocks := decodeBlocks(body.Content) for _, block := range blocks { if block.Type != "tool_result" || block.ToolUseID == "" { continue } 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 message := "the tool returned an error" if exit != nil && *exit != 0 { message = fmt.Sprintf("Exit code %d", *exit) } if strings.TrimSpace(text) != "" { message = boundedText(strings.TrimSpace(text), 2<<10) } toolErr = &protocol.ChatError{Type: "tool", Message: message} } content := []protocol.ChatToolContent{} if text != "" { content = append(content, protocol.ChatToolContent{Type: "text", Text: boundedText(text, 128<<10)}) } content = append(content, images...) s.mu.Lock() messageID := s.toolMessages[block.ToolUseID] shell := s.shells[block.ToolUseID] s.mu.Unlock() part := protocol.ChatPart{ Type: protocol.ChatPartTool, ID: block.ToolUseID, State: &protocol.ChatToolState{ Status: status, Content: content, Error: toolErr, }, } s.emit(protocol.ChatEventFrame{ SessionID: s.id, NativeTurnID: s.turnID, MessageID: messageID, Kind: protocol.ChatEventTool, Part: &part, }) if shell != nil { var frame *protocol.ChatShell s.mu.Lock() if snapshot, ok := s.shells[block.ToolUseID]; ok { snapshot.shell.Status = protocol.ChatShellExited snapshot.shell.Exit = exit if text != "" && len(text) >= len(snapshot.output) { snapshot.output = text snapshot.size = int64(len(text)) if len(snapshot.output) > commandOutputCap { snapshot.output = snapshot.output[:commandOutputCap] } } frame = &protocol.ChatShell{ID: snapshot.shell.ID, Status: snapshot.shell.Status, Command: snapshot.shell.Command, Exit: snapshot.shell.Exit} } s.mu.Unlock() if frame != nil { s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: frame}) } } } } func (s *session) handleResult(envelope lineEnvelope) { s.mu.Lock() turn := s.turnID running := s.running proc := s.proc s.running = false s.turnID = "" s.approvals = map[string]*pendingApproval{} s.forms = map[string]*pendingForm{} s.streamBlocks = map[int]blockState{} s.streamText = map[int]string{} s.streamOrdinals = map[string]int{} s.mu.Unlock() if envelope.Subtype != "success" && envelope.Subtype != "error_during_execution" && envelope.Subtype != "" { message := strings.TrimSpace(envelope.Result) if message == "" { message = "Claude Code ended the turn: " + envelope.Subtype } s.emit(protocol.ChatEventFrame{ SessionID: s.id, NativeTurnID: turn, MessageID: "claude-result-" + turn, Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{ Type: protocol.ChatPartTool, ID: "claude-result-" + turn, Name: "Claude Code", State: &protocol.ChatToolState{Status: protocol.ChatToolError, Error: &protocol.ChatError{Type: envelope.Subtype, Message: boundedText(message, 2<<10)}}, }, }) } if running { s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: turn, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: turn}}) } if proc != nil { go s.refreshUsage(proc) } } func (s *session) handleSystem(envelope lineEnvelope) { switch envelope.Subtype { case "status": if envelope.Status == "compacting" { s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: s.currentTurn(), Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running"}}) } case "compact_boundary": s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: s.currentTurn(), Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "completed"}}) case "init": // The CLI re-emits init at each turn; keep only the authoritative identity. if envelope.SessionID != "" && envelope.SessionID != s.id { s.mu.Lock() s.lastErr = "Claude Code opened unexpected session " + envelope.SessionID s.mu.Unlock() } } } func (s *session) currentTurn() string { s.mu.Lock() defer s.mu.Unlock() return s.turnID } // ---- Tool helpers ---------------------------------------------------------- func decodeBlocks(raw json.RawMessage) []contentBlock { if len(raw) == 0 { return nil } var blocks []contentBlock if json.Unmarshal(raw, &blocks) == nil { return blocks } var text string if json.Unmarshal(raw, &text) == nil && text != "" { return []contentBlock{{Type: "text", Text: text}} } return nil } func toolResultContent(raw json.RawMessage) (string, []protocol.ChatToolContent) { var text string if json.Unmarshal(raw, &text) == nil { return text, nil } blocks := decodeBlocks(raw) var builder strings.Builder images := []protocol.ChatToolContent{} for _, block := range blocks { switch block.Type { case "text": if builder.Len() > 0 { builder.WriteString("\n") } builder.WriteString(block.Text) case "image": if block.Source != nil && block.Source.Data != "" && len(block.Source.Data) <= 512<<10 { images = append(images, protocol.ChatToolContent{ Type: "file", Mime: block.Source.MediaType, Name: "image", URI: "data:" + block.Source.MediaType + ";base64," + block.Source.Data, }) } } } return builder.String(), images } var exitLine = regexp.MustCompile(`(?m)^Exit code (\d+)\s*$`) // exitCode reads the CLI's own "Exit code N" prefix. A successful command with // no prefix is 0; an error without a numeric code stays unknown. func exitCode(text string) *int { if match := exitLine.FindStringSubmatch(text); len(match) == 2 { if value, err := strconv.Atoi(match[1]); err == nil { return &value } } if strings.TrimSpace(text) != "" && !strings.HasPrefix(strings.TrimSpace(text), "Exit code") { zero := 0 return &zero } return nil } func bashCommand(input json.RawMessage) string { var parsed struct { Command string `json:"command"` } if json.Unmarshal(input, &parsed) == nil { return parsed.Command } return "" } func truncateJSON(raw json.RawMessage, limit int) json.RawMessage { if len(raw) == 0 { return json.RawMessage(`{}`) } if len(raw) > limit { wrapped, _ := json.Marshal(map[string]any{"truncated": true, "preview": string(raw[:limit])}) return wrapped } return raw } func boundedText(text string, limit int) string { if len(text) <= limit { return text } return text[:limit] + "\n… (truncated)" } // ---- Shells ---------------------------------------------------------------- const commandOutputCap = 128 << 10 type shellSnapshot struct { shell protocol.ChatShell output string size int64 order uint64 } func shellID(toolID string) string { sum := sha256.Sum256([]byte("claude\x00" + toolID)) return "sh_claude_" + hex.EncodeToString(sum[:16]) } // recordShellLocked retains a running Bash command. Called with s.mu held; the // caller emits the returned frame after the tool part, in that order. func (s *session) recordShellLocked(toolID, command string) *protocol.ChatShell { if toolID == "" { return nil } id := shellID(toolID) s.shellOrder = append(s.shellOrder, id) shell := protocol.ChatShell{ID: id, Status: protocol.ChatShellRunning, Command: command} s.shells[toolID] = &shellSnapshot{shell: shell, order: uint64(len(s.shellOrder))} return &shell } // ---- Usage ----------------------------------------------------------------- type usageModel struct { InputTokens int64 `json:"inputTokens"` OutputTokens int64 `json:"outputTokens"` CacheReadInputTokens int64 `json:"cacheReadInputTokens"` CacheCreationInputTokens int64 `json:"cacheCreationInputTokens"` CostUSD float64 `json:"costUSD"` ContextWindow int64 `json:"contextWindow"` } type sessionUsage struct { Session struct { TotalCostUSD float64 `json:"total_cost_usd"` ModelUsage map[string]usageModel `json:"model_usage"` } `json:"session"` } type contextUsage struct { TotalTokens int64 `json:"totalTokens"` MaxTokens int64 `json:"maxTokens"` } // refreshUsage asks the CLI for its own cumulative session totals. It runs off // the reader goroutine because a control request waits for a response that only // the reader can deliver. func (s *session) refreshUsage(p *process) { ctx, cancel := contextWithTimeout(controlRequestTimeout) defer cancel() raw, err := p.request(ctx, "get_usage", map[string]any{"skip_behaviors": true}) if err != nil { return } var totals sessionUsage if json.Unmarshal(raw, &totals) != nil { return } var input, output, cacheRead, cacheCreate int64 for _, model := range totals.Session.ModelUsage { input += model.InputTokens output += model.OutputTokens cacheRead += model.CacheReadInputTokens cacheCreate += model.CacheCreationInputTokens } total := input + output + cacheRead + cacheCreate cost := totals.Session.TotalCostUSD usage := &protocol.ChatUsage{ Scope: "session", InputTokens: int(input), OutputTokens: int(output), TotalTokens: &total, CostUSD: &cost, } ctx2, cancel2 := contextWithTimeout(controlRequestTimeout) defer cancel2() if rawContext, err := p.request(ctx2, "get_context_usage", map[string]any{}); err == nil { var context contextUsage if json.Unmarshal(rawContext, &context) == nil { if context.MaxTokens > 0 { usage.ContextWindow = &context.MaxTokens usage.ContextTokens = &context.TotalTokens } } } s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventUsage, Usage: usage}) } func contextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), timeout) }