-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec_cursor.go
More file actions
288 lines (276 loc) · 8.77 KB
/
Copy pathcodec_cursor.go
File metadata and controls
288 lines (276 loc) · 8.77 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package agenthooks
import (
"encoding/json"
"os"
"time"
)
// Cursor dialect: camelCase event names, snake_case fields, per-event output
// schemas (permission/user_message/agent_message, followup_message). The
// codec emits snake_case output plus a harmless legacy `continue` field
// (quirk #4) and un-stringifies MCP tool_input (quirk #5).
var cursorKinds = map[string]EventKind{
"sessionStart": KindSessionStart,
"sessionEnd": KindSessionEnd,
"beforeSubmitPrompt": KindPromptSubmitted,
"preToolUse": KindToolPre,
"beforeShellExecution": KindToolPre,
"beforeMCPExecution": KindToolPre,
"beforeReadFile": KindToolPre,
"postToolUse": KindToolPost,
"afterShellExecution": KindToolPost,
"afterMCPExecution": KindToolPost,
"postToolUseFailure": KindToolError,
"stop": KindStop,
"subagentStart": KindSubagentStart,
"subagentStop": KindSubagentStop,
"preCompact": KindCompactPre,
"afterFileEdit": KindFileEdited,
"afterTabFileEdit": KindFileEdited,
"afterAgentResponse": KindModelResponse,
"afterAgentThought": KindModelResponse,
}
type cursorIn struct {
ConversationID string `json:"conversation_id"`
GenerationID string `json:"generation_id"`
HookEventName string `json:"hook_event_name"`
WorkspaceRoots []string `json:"workspace_roots"`
UserEmail string `json:"user_email"`
Model string `json:"model"`
CWD string `json:"cwd"`
TranscriptPath string `json:"transcript_path"`
ToolName string `json:"tool_name"`
ToolInput json.RawMessage `json:"tool_input"`
ToolUseID string `json:"tool_use_id"`
Command string `json:"command"`
URL string `json:"url"`
MCPServerName string `json:"mcp_server_name"`
Prompt string `json:"prompt"`
FilePath string `json:"file_path"`
Output json.RawMessage `json:"output"`
ToolResponse json.RawMessage `json:"tool_response"`
ExitCode *int `json:"exit_code"`
Status string `json:"status"`
LoopCount int `json:"loop_count"`
SubagentType string `json:"subagent_type"`
SubagentID string `json:"subagent_id"`
Error string `json:"error"`
Duration *float64 `json:"duration"`
DurationMS *float64 `json:"duration_ms"`
Text string `json:"text"`
InputTokens *int `json:"input_tokens"`
OutputTokens *int `json:"output_tokens"`
CacheReadTokens *int `json:"cache_read_tokens"`
CacheWriteTokens *int `json:"cache_write_tokens"`
Cost *float64 `json:"cost"`
}
func decodeCursor(v Variant, conf DetectionConfidence, now time.Time, payload []byte) (any, error) {
var in cursorIn
if err := json.Unmarshal(payload, &in); err != nil {
return nil, err
}
kind, ok := cursorKinds[in.HookEventName]
if !ok {
kind = KindOther
}
cwd := in.CWD
if cwd == "" && len(in.WorkspaceRoots) > 0 {
cwd = in.WorkspaceRoots[0]
}
// Cursor ships the transcript location as an env var on hook processes;
// only some payloads repeat it inline.
if in.TranscriptPath == "" {
in.TranscriptPath = os.Getenv("CURSOR_TRANSCRIPT_PATH")
}
base := Event{
Provider: ProviderCursor,
Variant: v,
NativeName: in.HookEventName,
Kind: kind,
Time: now,
DetectionConfidence: conf,
Session: SessionInfo{
ID: in.ConversationID,
TurnID: in.GenerationID,
CWD: cwd,
WorkspaceRoots: in.WorkspaceRoots,
TranscriptPath: in.TranscriptPath,
Model: in.Model,
UserEmail: in.UserEmail,
},
Raw: json.RawMessage(payload),
}
if in.SubagentID != "" || in.SubagentType != "" {
base.Agent = &AgentInfo{ID: in.SubagentID, Type: in.SubagentType}
}
switch kind {
case KindToolPre:
return &ToolPreEvent{Event: base, Tool: cursorToolCall(base.Session, &in)}, nil
case KindToolPost, KindToolError:
out := in.Output
if len(out) == 0 {
out = in.ToolResponse
}
failed := kind == KindToolError || (in.ExitCode != nil && *in.ExitCode != 0)
duration := in.DurationMS
if duration == nil {
duration = in.Duration
}
return &ToolPostEvent{
Event: base,
Tool: cursorToolCall(base.Session, &in),
Output: out,
Failed: failed,
Error: in.Error,
DurationMS: duration,
}, nil
case KindPromptSubmitted:
return &PromptEvent{Event: base, Prompt: in.Prompt}, nil
case KindStop, KindSubagentStop:
return &StopEvent{
Event: base,
PreviouslyContinued: in.LoopCount > 0,
LoopCount: in.LoopCount,
FinalMessage: in.Text,
Usage: cursorUsage(&in),
}, nil
case KindSubagentStart:
return &SubagentStartEvent{Event: base}, nil
case KindSessionStart:
return &SessionStartEvent{Event: base}, nil
case KindSessionEnd:
return &SessionEndEvent{Event: base, Reason: in.Status}, nil
case KindCompactPre:
return &CompactEvent{Event: base}, nil
case KindFileEdited:
return &FileEditedEvent{Event: base, Path: in.FilePath}, nil
case KindModelRequest, KindModelResponse:
return &ModelEvent{Event: base}, nil
}
ev := base
return &ev, nil
}
// cursorToolCall normalizes the three shapes Cursor uses for the same
// concept: preToolUse (tool_name + object tool_input), shell events (bare
// command field), and MCP events (MCP:-prefixed name + stringified input).
func cursorToolCall(s SessionInfo, in *cursorIn) ToolCall {
name := in.ToolName
input := in.ToolInput
switch in.HookEventName {
case "beforeShellExecution", "afterShellExecution":
if name == "" {
name = "Shell"
}
if len(input) == 0 && in.Command != "" {
b, err := json.Marshal(map[string]string{"command": in.Command})
if err == nil {
input = b
}
}
case "beforeReadFile":
if name == "" {
name = "ReadFile"
}
if len(input) == 0 && in.FilePath != "" {
b, err := json.Marshal(map[string]string{"file_path": in.FilePath})
if err == nil {
input = b
}
}
}
tc := makeToolCall(s, name, in.ToolUseID, input, in.ToolInput)
if in.HookEventName == "beforeMCPExecution" || in.HookEventName == "afterMCPExecution" {
if tc.MCP == nil {
tc.MCP = &MCPCall{Tool: name}
tc.Canonical = ToolMCP
}
tc.MCP.URL = in.URL
tc.MCP.Command = in.Command
tc.MCP.Server = in.MCPServerName
}
return tc
}
func encodeCursor(base *Event, d decisionCore) (wireResponse, error) {
out := map[string]any{}
ctx := joinContext(d.context)
switch base.Kind {
case KindToolPre, KindPermission:
switch d.kind {
case DecisionAllow:
out["permission"] = "allow"
case DecisionDeny:
out["permission"] = "deny"
case DecisionAsk:
out["permission"] = "ask"
}
if d.kind != DecisionNoDecision {
// Harmless legacy field for the pre-2.0.64 camelCase era (quirk #4).
out["continue"] = true
}
if d.reason != "" || ctx != "" {
out["agent_message"] = joinNonEmpty(d.reason, ctx)
}
if d.systemMessage != "" {
out["user_message"] = d.systemMessage
}
if d.hasUpdatedInput && base.NativeName == "preToolUse" {
out["updated_input"] = d.updatedInput
}
case KindPromptSubmitted:
if d.kind == DecisionBlockPrompt {
out["continue"] = false
if d.reason != "" {
out["user_message"] = d.reason
}
} else if d.kind != DecisionNoDecision {
out["continue"] = true
}
case KindStop, KindSubagentStop:
if d.kind == DecisionContinue {
out["followup_message"] = d.instruction
}
case KindToolPost, KindToolError:
if d.hasReplacedOutput && base.NativeName == "afterMCPExecution" {
out["updated_mcp_tool_output"] = d.replacedOutput
}
}
b, err := json.Marshal(out)
if err != nil {
return wireResponse{}, err
}
return wireResponse{Stdout: b}, nil
}
// cursorUsage collects the end-of-turn token/cost totals Cursor reports on
// stop, returning nil when none are present so telemetry omits an empty block.
func cursorUsage(in *cursorIn) *Usage {
if in.InputTokens == nil && in.OutputTokens == nil && in.CacheReadTokens == nil &&
in.CacheWriteTokens == nil && in.Cost == nil && in.LoopCount == 0 && in.Status == "" {
return nil
}
u := &Usage{
InputTokens: in.InputTokens,
OutputTokens: in.OutputTokens,
CacheReadTokens: in.CacheReadTokens,
CacheWriteTokens: in.CacheWriteTokens,
Cost: in.Cost,
LoopCount: nil,
Status: in.Status,
}
if in.LoopCount != 0 {
lc := in.LoopCount
u.LoopCount = &lc
}
return u
}
func joinNonEmpty(parts ...string) string {
out := ""
for _, p := range parts {
if p == "" {
continue
}
if out != "" {
out += "\n"
}
out += p
}
return out
}