Repository navigation
Expand file tree
/
Copy pathcodec_kimi.go
More file actions
154 lines (148 loc) · 5.15 KB
/
Copy pathcodec_kimi.go
File metadata and controls
154 lines (148 loc) · 5.15 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
package agenthooks
import (
"encoding/json"
"time"
)
// Kimi Code dialect: Claude-shaped snake_case JSON on stdin, but a much
// narrower response surface. Only PreToolUse understands JSON output
// (hookSpecificOutput.permissionDecision, deny|allow only — no ask, no
// updatedInput, quirk #22). UserPromptSubmit and Stop block via exit code 2
// with the reason on stderr, and exit-0 stdout is appended to the model
// context (quirk #23), so the no-op form is empty stdout. The runtime itself
// is hard fail-open: crash, timeout, and any non-2 exit all allow (quirk #21).
var kimiKinds = map[string]EventKind{
"SessionStart": KindSessionStart,
"SessionEnd": KindSessionEnd,
"UserPromptSubmit": KindPromptSubmitted,
"PreToolUse": KindToolPre,
"PostToolUse": KindToolPost,
"PostToolUseFailure": KindToolError,
"PermissionRequest": KindPermission, // observation-only on Kimi (§4.1)
"Stop": KindStop,
"SubagentStart": KindSubagentStart,
"SubagentStop": KindSubagentStop,
"PreCompact": KindCompactPre,
"PostCompact": KindCompactPost,
"Notification": KindNotification,
// PermissionResult, StopFailure, Interrupt intentionally unmapped:
// delivered as KindOther with the native name and raw payload intact.
}
// kimiIn is the union of Kimi's per-event stdin fields. Field names diverge
// from Claude where noted (tool_call_id vs tool_use_id, tool_output vs
// tool_response, error vs tool_error).
type kimiIn struct {
SessionID string `json:"session_id"`
CWD string `json:"cwd"`
HookEventName string `json:"hook_event_name"`
ToolName string `json:"tool_name"`
ToolInput json.RawMessage `json:"tool_input"`
ToolCallID string `json:"tool_call_id"`
ToolOutput json.RawMessage `json:"tool_output"`
Error string `json:"error"`
Prompt string `json:"prompt"`
StopHookActive bool `json:"stop_hook_active"`
Source string `json:"source"`
Reason string `json:"reason"`
AgentName string `json:"agent_name"`
Trigger string `json:"trigger"`
Sink string `json:"sink"`
NotificationType string `json:"notification_type"`
Title string `json:"title"`
Body string `json:"body"`
}
func decodeKimi(v Variant, conf DetectionConfidence, now time.Time, payload []byte) (any, error) {
var in kimiIn
if err := json.Unmarshal(payload, &in); err != nil {
return nil, err
}
kind, ok := kimiKinds[in.HookEventName]
if !ok {
kind = KindOther
}
base := Event{
Provider: ProviderKimi,
Variant: v,
NativeName: in.HookEventName,
Kind: kind,
Time: now,
DetectionConfidence: conf,
Session: SessionInfo{
ID: in.SessionID,
CWD: in.CWD,
WorkspaceRoots: rootsFor(in.CWD),
},
Raw: json.RawMessage(payload),
}
if in.AgentName != "" {
base.Agent = &AgentInfo{Type: in.AgentName}
}
message := in.Body
if message == "" {
message = in.Title
}
// Kimi ships Claude's shapes under renamed keys: project onto claudeIn
// and reuse the shared builder.
shaped := claudeIn{
SessionID: in.SessionID,
CWD: in.CWD,
HookEventName: in.HookEventName,
ToolName: in.ToolName,
ToolInput: in.ToolInput,
ToolUseID: in.ToolCallID,
ToolResponse: in.ToolOutput,
ToolError: in.Error,
Prompt: in.Prompt,
Message: message,
Source: in.Source,
Reason: in.Reason,
StopHookActive: in.StopHookActive,
Trigger: in.Trigger,
}
return buildClaudeShaped(base, &shaped), nil
}
func encodeKimi(base *Event, d decisionCore) (wireResponse, error) {
if d.kind == DecisionAsk {
// Must have been degraded by policy before reaching the codec.
return wireResponse{}, ErrUnsupportedDecision
}
switch base.Kind {
case KindToolPre:
var decision string
switch d.kind {
case DecisionAllow:
decision = "allow"
case DecisionDeny:
decision = "deny"
default:
return wireResponse{}, nil
}
hso := map[string]any{
"hookEventName": base.NativeName,
"permissionDecision": decision,
}
if d.reason != "" || d.kind == DecisionDeny {
hso["permissionDecisionReason"] = d.reason
}
b, err := json.Marshal(map[string]any{"hookSpecificOutput": hso})
if err != nil {
return wireResponse{}, err
}
return wireResponse{Stdout: b}, nil
case KindPromptSubmitted:
if d.kind == DecisionBlockPrompt {
return wireResponse{Stderr: []byte(d.reason), ExitCode: 2}, nil
}
// Exit-0 stdout is appended to the model context on UserPromptSubmit
// — the only context mechanism Kimi has, and it is plain text.
if ctx := joinContext(d.context); ctx != "" {
return wireResponse{Stdout: []byte(ctx)}, nil
}
case KindStop:
if d.kind == DecisionContinue {
return wireResponse{Stderr: []byte(d.instruction), ExitCode: 2}, nil
}
}
// Everything else on Kimi is observation-only; empty stdout keeps the
// context clean (quirk #23).
return wireResponse{}, nil
}