This repository has been archived on 2026-05-07. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
ccrm/daemon/internal/claude/protocol.go

88 lines
2.9 KiB
Go

package claude
import "encoding/json"
// MessageHandler is called for each JSON message received from the Claude subprocess.
type MessageHandler func(msg *CLIMessage)
// CLIMessage is the base envelope for all messages from Claude's stdout.
type CLIMessage struct {
Type string `json:"type"`
Raw json.RawMessage `json:"-"` // full original JSON line
}
// --- Messages FROM Claude (stdout) ---
// SystemMessage is emitted once at session start.
type SystemMessage struct {
Type string `json:"type"` // "system"
Subtype string `json:"subtype,omitempty"`
SessionID string `json:"session_id"`
Tools []string `json:"tools,omitempty"`
Model string `json:"model,omitempty"`
CWD string `json:"cwd,omitempty"`
}
// AssistantMessage contains Claude's response content.
type AssistantMessage struct {
Type string `json:"type"` // "assistant"
Message AssistantContent `json:"message"`
}
// AssistantContent holds the content blocks of an assistant message.
type AssistantContent struct {
ID string `json:"id,omitempty"`
Role string `json:"role,omitempty"`
Content []ContentBlock `json:"content,omitempty"`
Model string `json:"model,omitempty"`
}
// ContentBlock is a single block within a message (text or tool_use).
type ContentBlock struct {
Type string `json:"type"` // "text" or "tool_use"
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
}
// ResultMessage is emitted when a turn completes.
type ResultMessage struct {
Type string `json:"type"` // "result"
Subtype string `json:"subtype,omitempty"`
SessionID string `json:"session_id"`
IsError bool `json:"is_error,omitempty"`
Duration int `json:"duration_ms,omitempty"`
NumTurns int `json:"num_turns,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
TotalCostUSD float64 `json:"total_cost_usd,omitempty"`
}
// ControlRequest is a permission or callback request from Claude.
type ControlRequest struct {
Type string `json:"type"` // "tool_use_permission"
Tool ToolUseRequest `json:"tool,omitempty"`
}
// ToolUseRequest describes a tool permission request.
type ToolUseRequest struct {
RequestID string `json:"request_id"`
ToolName string `json:"tool_name"`
Input json.RawMessage `json:"input,omitempty"`
}
// --- Messages TO Claude (stdin) ---
// UserMessage sends a prompt to Claude.
type UserMessage struct {
Type string `json:"type"` // "user_message"
Content string `json:"content"`
}
// ControlResponse responds to a permission request.
type ControlResponse struct {
Type string `json:"type"` // "tool_use_permission_response"
RequestID string `json:"request_id"`
Allow bool `json:"allow"`
Reason string `json:"reason,omitempty"`
}