-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathcompat.go
More file actions
180 lines (159 loc) · 4.67 KB
/
compat.go
File metadata and controls
180 lines (159 loc) · 4.67 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
package copilotcli
import (
"encoding/json"
"errors"
"fmt"
"time"
)
// HookHost identifies which host format produced a copilot-compatible hook payload.
type HookHost string
const (
HostUnknown HookHost = "unknown"
HostCopilotCLI HookHost = "copilot-cli"
HostVSCode HookHost = "vscode"
)
// VS Code hookEventName values (from official VS Code docs).
// See: https://code.visualstudio.com/docs/copilot/customization/hooks
const (
VSCodeEventSessionStart = "SessionStart"
VSCodeEventUserPromptSubmit = "UserPromptSubmit"
VSCodeEventStop = "Stop"
VSCodeEventPreToolUse = "PreToolUse"
VSCodeEventPostToolUse = "PostToolUse"
VSCodeEventPreCompact = "PreCompact"
VSCodeEventSubagentStart = "SubagentStart"
VSCodeEventSubagentStop = "SubagentStop"
)
// vsCodeEventToHookNames maps each VS Code hookEventName to the CLI hook name(s)
// that are allowed to carry that event. "Stop" maps to both agent-stop and
// session-end because VS Code uses a single Stop event where Copilot CLI
// distinguishes the two.
var vsCodeEventToHookNames = map[string][]string{
VSCodeEventUserPromptSubmit: {HookNameUserPromptSubmitted},
VSCodeEventSessionStart: {HookNameSessionStart},
VSCodeEventStop: {HookNameAgentStop, HookNameSessionEnd},
VSCodeEventSubagentStop: {HookNameSubagentStop},
VSCodeEventPreToolUse: {HookNamePreToolUse},
VSCodeEventPostToolUse: {HookNamePostToolUse},
VSCodeEventPreCompact: {},
VSCodeEventSubagentStart: {},
}
type hookEnvelope struct {
Host HookHost
SessionID string
Prompt string
TranscriptPath string
HookEventName string
Source string
InitialPrompt string
StopReason string
Reason string
Timestamp time.Time
}
func parseHookEnvelope(data []byte) (*hookEnvelope, error) {
if len(data) == 0 {
return nil, errors.New("empty hook input")
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("failed to parse hook input: %w", err)
}
env := &hookEnvelope{
Host: detectHookHost(raw),
SessionID: firstString(raw, "sessionId"),
Prompt: firstString(raw, "prompt"),
TranscriptPath: firstString(raw, "transcriptPath", "transcript_path"),
HookEventName: firstString(raw, "hookEventName"),
Source: firstString(raw, "source"),
InitialPrompt: firstString(raw, "initialPrompt"),
StopReason: firstString(raw, "stopReason"),
Reason: firstString(raw, "reason"),
}
ts, err := parseTimestamp(raw["timestamp"])
if err != nil {
return nil, fmt.Errorf("failed to parse hook input: %w", err)
}
env.Timestamp = ts
if env.Timestamp.IsZero() {
env.Timestamp = time.Now()
}
return env, nil
}
func detectHookHost(raw map[string]json.RawMessage) HookHost {
if _, ok := raw["hookEventName"]; ok {
return HostVSCode
}
if _, ok := raw["transcript_path"]; ok {
return HostVSCode
}
if isJSONString(raw["timestamp"]) {
return HostVSCode
}
if _, ok := raw["transcriptPath"]; ok {
return HostCopilotCLI
}
if isJSONNumber(raw["timestamp"]) {
return HostCopilotCLI
}
return HostUnknown
}
func firstString(raw map[string]json.RawMessage, keys ...string) string {
for _, key := range keys {
value, ok := raw[key]
if !ok {
continue
}
var s string
if err := json.Unmarshal(value, &s); err == nil {
return s
}
}
return ""
}
func parseTimestamp(raw json.RawMessage) (time.Time, error) {
if len(raw) == 0 {
return time.Time{}, nil
}
var millis int64
if err := json.Unmarshal(raw, &millis); err == nil {
return time.UnixMilli(millis), nil
}
var ts string
if err := json.Unmarshal(raw, &ts); err != nil {
return time.Time{}, fmt.Errorf("unmarshal timestamp string: %w", err)
}
parsed, err := time.Parse(time.RFC3339Nano, ts)
if err != nil {
return time.Time{}, fmt.Errorf("parse timestamp %q: %w", ts, err)
}
return parsed, nil
}
func isJSONString(raw json.RawMessage) bool {
if len(raw) == 0 || raw[0] != '"' {
return false
}
var s string
return json.Unmarshal(raw, &s) == nil
}
func isJSONNumber(raw json.RawMessage) bool {
if len(raw) == 0 {
return false
}
var n int64
return json.Unmarshal(raw, &n) == nil
}
// validateVSCodeEvent checks whether the hookEventName is consistent with the
// CLI hook subcommand that was invoked. Returns true if the event should be
// processed, false if it should be silently skipped (mismatch or unknown event).
func validateVSCodeEvent(hookEventName, hookName string) bool {
allowedHooks, known := vsCodeEventToHookNames[hookEventName]
if !known {
return false
}
for _, allowed := range allowedHooks {
if allowed == hookName {
return true
}
}
return false
}