-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec_claudecode.go
More file actions
220 lines (210 loc) · 6.89 KB
/
Copy pathcodec_claudecode.go
File metadata and controls
220 lines (210 loc) · 6.89 KB
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
package agenthooks
import (
"encoding/json"
"time"
)
// Claude Code dialect: snake_case JSON on stdin, camelCase JSON out,
// hookSpecificOutput envelope, exit 0 with an explicit body (the library
// never relies on exit-2 semantics because every blocking intent is
// expressible in the body).
var claudeKinds = map[string]EventKind{
"SessionStart": KindSessionStart,
"SessionEnd": KindSessionEnd,
"UserPromptSubmit": KindPromptSubmitted,
"PreToolUse": KindToolPre,
"PostToolUse": KindToolPost,
"PostToolUseFailure": KindToolError,
"PermissionRequest": KindPermission,
"Stop": KindStop,
"SubagentStart": KindSubagentStart,
"SubagentStop": KindSubagentStop,
"PreCompact": KindCompactPre,
"PostCompact": KindCompactPost,
"Notification": KindNotification,
"FileChanged": KindFileEdited,
"MessageDisplay": KindModelResponse, // approximate mapping (§3.4)
}
type claudeIn struct {
SessionID string `json:"session_id"`
TranscriptPath string `json:"transcript_path"`
CWD string `json:"cwd"`
HookEventName string `json:"hook_event_name"`
PermissionMode string `json:"permission_mode"`
Model string `json:"model"`
PromptID string `json:"prompt_id"`
ToolName string `json:"tool_name"`
ToolInput json.RawMessage `json:"tool_input"`
ToolUseID string `json:"tool_use_id"`
ToolResponse json.RawMessage `json:"tool_response"`
ToolError string `json:"tool_error"`
Prompt string `json:"prompt"`
Message string `json:"message"`
LastAssistantMessage string `json:"last_assistant_message"`
DurationMS *float64 `json:"duration_ms"`
Source string `json:"source"`
Reason string `json:"reason"`
StopHookActive bool `json:"stop_hook_active"`
Trigger string `json:"trigger"`
CustomInstructions string `json:"custom_instructions"`
AgentID string `json:"agent_id"`
AgentType string `json:"agent_type"`
FilePath string `json:"file_path"`
}
func decodeClaude(v Variant, conf DetectionConfidence, now time.Time, payload []byte) (any, error) {
var in claudeIn
if err := json.Unmarshal(payload, &in); err != nil {
return nil, err
}
kind, ok := claudeKinds[in.HookEventName]
if !ok {
kind = KindOther
}
base := Event{
Provider: ProviderClaudeCode,
Variant: v,
NativeName: in.HookEventName,
Kind: kind,
Time: now,
DetectionConfidence: conf,
Session: SessionInfo{
ID: in.SessionID,
TurnID: in.PromptID,
CWD: in.CWD,
WorkspaceRoots: rootsFor(in.CWD),
TranscriptPath: in.TranscriptPath,
Model: in.Model,
PermissionMode: in.PermissionMode,
},
Raw: json.RawMessage(payload),
}
if in.AgentID != "" || in.AgentType != "" {
base.Agent = &AgentInfo{ID: in.AgentID, Type: in.AgentType}
}
typed := buildClaudeShaped(base, &in)
if event, ok := typed.(*ToolPostEvent); ok {
backfillClaudeSkillOutput(event)
}
return typed, nil
}
// buildClaudeShaped constructs typed events from the Claude-shaped wire form.
// Codex deliberately ships the same shapes, so its decoder reuses this.
func buildClaudeShaped(base Event, in *claudeIn) any {
switch base.Kind {
case KindToolPre:
return &ToolPreEvent{Event: base, Tool: makeToolCall(base.Session, in.ToolName, in.ToolUseID, in.ToolInput, in.ToolInput)}
case KindPermission:
return &PermissionEvent{Event: base, Tool: makeToolCall(base.Session, in.ToolName, in.ToolUseID, in.ToolInput, in.ToolInput)}
case KindToolPost, KindToolError:
return &ToolPostEvent{
Event: base,
Tool: makeToolCall(base.Session, in.ToolName, in.ToolUseID, in.ToolInput, in.ToolInput),
Output: in.ToolResponse,
Failed: base.Kind == KindToolError,
Error: in.ToolError,
DurationMS: in.DurationMS,
}
case KindPromptSubmitted:
return &PromptEvent{Event: base, Prompt: in.Prompt}
case KindStop, KindSubagentStop:
lc := 0
if in.StopHookActive {
lc = 1
}
return &StopEvent{Event: base, PreviouslyContinued: in.StopHookActive, LoopCount: lc, FinalMessage: in.LastAssistantMessage, Usage: nil}
case KindSubagentStart:
return &SubagentStartEvent{Event: base}
case KindSessionStart:
return &SessionStartEvent{Event: base, Source: in.Source}
case KindSessionEnd:
return &SessionEndEvent{Event: base, Reason: in.Reason}
case KindNotification:
return &NotificationEvent{Event: base, Message: in.Message}
case KindCompactPre, KindCompactPost:
return &CompactEvent{Event: base, Trigger: in.Trigger, Instructions: in.CustomInstructions}
case KindFileEdited:
return &FileEditedEvent{Event: base, Path: in.FilePath}
case KindModelRequest, KindModelResponse:
return &ModelEvent{Event: base}
}
ev := base
return &ev
}
func rootsFor(cwd string) []string {
if cwd == "" {
return nil
}
return []string{cwd}
}
func encodeClaude(base *Event, d decisionCore) (wireResponse, error) {
out := map[string]any{}
hso := map[string]any{}
ctx := joinContext(d.context)
switch base.Kind {
case KindToolPre, KindPermission:
switch d.kind {
case DecisionAllow:
hso["permissionDecision"] = "allow"
if d.reason != "" {
hso["permissionDecisionReason"] = d.reason
}
case DecisionDeny:
hso["permissionDecision"] = "deny"
hso["permissionDecisionReason"] = d.reason
case DecisionAsk:
hso["permissionDecision"] = "ask"
hso["permissionDecisionReason"] = d.reason
}
if d.hasUpdatedInput {
hso["updatedInput"] = d.updatedInput
}
if ctx != "" {
hso["additionalContext"] = ctx
}
case KindPromptSubmitted:
if d.kind == DecisionBlockPrompt {
out["decision"] = "block"
out["reason"] = d.reason
}
if ctx != "" {
hso["additionalContext"] = ctx
}
case KindSessionStart:
if ctx != "" {
hso["additionalContext"] = ctx
}
case KindStop, KindSubagentStop:
if d.kind == DecisionContinue {
out["decision"] = "block"
out["reason"] = d.instruction
}
case KindToolPost, KindToolError:
if d.kind == DecisionFlagOutput {
out["decision"] = "block"
out["reason"] = d.reason
}
if d.hasReplacedOutput {
hso["updatedToolOutput"] = d.replacedOutput
}
if ctx != "" {
hso["additionalContext"] = ctx
}
}
if d.systemMessage != "" {
out["systemMessage"] = d.systemMessage
}
if d.stopAgent {
out["continue"] = false
if d.stopReason != "" {
out["stopReason"] = d.stopReason
}
}
if len(hso) > 0 {
hso["hookEventName"] = base.NativeName
out["hookSpecificOutput"] = hso
}
b, err := json.Marshal(out)
if err != nil {
return wireResponse{}, err
}
return wireResponse{Stdout: b}, nil
}