-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontrol_parser.go
More file actions
279 lines (258 loc) · 9.09 KB
/
Copy pathcontrol_parser.go
File metadata and controls
279 lines (258 loc) · 9.09 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
package codebuddy
import (
"context"
"encoding/json"
"path/filepath"
"strings"
"time"
"github.com/agent-dance/agent-adaptor/driver"
"github.com/agent-dance/agent-adaptor/internal/clihelper"
)
const controlInitializeRequestID = "agent-adaptor-initialize"
type controlState struct {
ctx context.Context
sink driver.DecisionCapableSink
stdin clihelper.StdinController
runID string
policy driver.HumanDecisionPolicy
prompt string
configDir string
userStarted bool
plan string
}
func (p *parser) enableControl(ctx context.Context, sink driver.DecisionCapableSink, stdin clihelper.StdinController, runID string, policy driver.HumanDecisionPolicy, prompt string) {
p.control = &controlState{ctx: ctx, sink: sink, stdin: stdin, runID: runID, policy: policy, prompt: prompt}
}
func (p *parser) enablePersistentControl(ctx context.Context, sink driver.DecisionCapableSink, runID string, policy driver.HumanDecisionPolicy, prompt, configDir string) {
p.control = &controlState{ctx: ctx, sink: sink, runID: runID, policy: policy, prompt: prompt, configDir: configDir}
}
func (p *parser) handleControlPayload(payload map[string]any) bool {
if p.control == nil {
return false
}
switch topString(payload, "type") {
case "control_response":
response := topObject(payload, "response")
if topString(response, "request_id", "requestId") == controlInitializeRequestID && !p.control.userStarted {
p.control.userStarted = true
_ = p.control.stdin.Write(encodeControlUser(p.control.prompt))
}
return true
case "control_request":
p.handleControlRequest(payload)
return true
default:
return false
}
}
func (p *parser) handleControlRequest(payload map[string]any) {
requestID := topString(payload, "request_id", "requestId")
request := topObject(payload, "request")
if requestID == "" || request == nil || strings.ToLower(topString(request, "subtype")) != "can_use_tool" {
return
}
toolName := topString(request, "tool_name", "toolName", "name")
input := cloneMap(topObject(request, "input"))
toolUseID := topString(request, "tool_use_id", "toolUseId", "id")
kind := controlDecisionKind(toolName)
decision := p.control.sinkDecision(p, requestID, toolUseID, toolName, input, kind)
_ = p.control.stdin.Write(encodeControlResponse(requestID, toolUseID, input, decision, p.control.policy))
}
func (s *controlState) sinkDecision(p *parser, requestID, toolUseID, toolName string, input map[string]any, kind driver.HumanDecisionKind) driver.DecisionResponse {
req := driver.DecisionRequest{
RequestID: requestID,
RunID: s.runID,
ThreadID: p.sessionID,
Kind: kind,
Source: controlSource(kind),
ToolCallID: toolUseID,
Prompt: toolName,
Payload: map[string]any{"tool": toolName, "input": input},
CreatedAt: time.Now().UTC(),
}
if kind == driver.HumanDecisionPlanReview {
req.Prompt = "ExitPlanMode"
req.Payload["plan"] = s.plan
}
if kind == driver.HumanDecisionQuestion {
req.Prompt = questionPrompt(input)
req.Choices = questionChoices(input)
}
resp, err := s.sink.RequestDecision(s.ctx, req)
if err != nil && resp.Result == "" {
// The SDK may already classify a timeout or abort in resp. Preserve
// that classification; only an unclassified transport error becomes
// an aborted decision.
resp = driver.DecisionResponse{RequestID: requestID, Result: driver.DecisionAborted, Text: err.Error()}
}
if resp.RequestID == "" {
resp.RequestID = requestID
}
p.recordControlReject(req, resp, s.policy)
return resp
}
func (p *parser) captureControlPlan(toolName string, input map[string]any) {
if p.control == nil || !strings.EqualFold(toolName, "Write") {
return
}
path, _ := input["file_path"].(string)
content, _ := input["content"].(string)
if content == "" || !isCodeBuddyPlanFile(path, p.control.configDir) {
return
}
p.control.plan = content
}
// isCodeBuddyPlanFile 判断某次 Write 是否写入 CodeBuddy 的计划文件。计划位于
// <configDir>/plans/*.md;configDir 可被 CODEBUDDY_CONFIG_DIR 覆盖,因此已知时按
// 该前缀匹配,未知时回退到默认的 ~/.codebuddy/plans/ 布局。
func isCodeBuddyPlanFile(path, configDir string) bool {
clean := filepath.ToSlash(path)
if !strings.HasSuffix(strings.ToLower(clean), ".md") {
return false
}
if dir := strings.TrimSpace(configDir); dir != "" {
prefix := strings.TrimSuffix(filepath.ToSlash(dir), "/") + "/plans/"
if strings.Contains(clean, prefix) {
return true
}
}
return strings.Contains(clean, ".codebuddy/plans/")
}
func controlDecisionKind(toolName string) driver.HumanDecisionKind {
switch toolName {
case "AskUserQuestion":
return driver.HumanDecisionQuestion
case "ExitPlanMode":
return driver.HumanDecisionPlanReview
default:
return driver.HumanDecisionPermission
}
}
func controlSource(kind driver.HumanDecisionKind) string {
switch kind {
case driver.HumanDecisionQuestion:
return "AskUserQuestion"
case driver.HumanDecisionPlanReview:
return "ExitPlanMode"
default:
return "permission"
}
}
func encodeControlUser(prompt string) []byte {
frame, _ := json.Marshal(map[string]any{
"type": "user",
"session_id": "",
"message": map[string]any{
"role": "user",
"content": prompt,
},
"parent_tool_use_id": nil,
})
return append(frame, '\n')
}
func encodeControlResponse(requestID, toolUseID string, input map[string]any, decision driver.DecisionResponse,
policy driver.HumanDecisionPolicy) []byte {
allowed := decision.Result == driver.DecisionApproved || decision.Result == driver.DecisionAnswered
response := map[string]any{"allowed": allowed, "tool_use_id": toolUseID}
if allowed {
updated := cloneMap(input)
if decision.Result == driver.DecisionAnswered {
updated["answers"] = questionAnswers(input, decision)
if decision.Choice != "" {
updated["choice"] = decision.Choice
}
}
response["updatedInput"] = updated
} else {
response["reason"] = decision.Text
response["interrupt"] = controlDenyInterrupts(decision.Result, policy)
}
frame, _ := json.Marshal(map[string]any{
"type": "control_response",
"response": map[string]any{
"subtype": "success",
"request_id": requestID,
"response": response,
},
})
return append(frame, '\n')
}
// controlDenyInterrupts reports whether a denied control decision should carry
// interrupt=true, i.e. abort the CLI run. Rejected/Aborted honor OnReject and
// TimedOut honors OnTimeout; both default (Unset) to Abort. Approved/Answered
// never reach this path. 与 claude 的 interrupt 门控保持一致。
func controlDenyInterrupts(result driver.DecisionResult, policy driver.HumanDecisionPolicy) bool {
effective := driver.EffectiveHumanDecisionPolicy(policy)
switch result {
case driver.DecisionTimedOut:
return effective.OnTimeout == driver.FailureAbort
case driver.DecisionRejected, driver.DecisionAborted:
return effective.OnReject == driver.FailureAbort
default:
return false
}
}
func (p *parser) recordControlReject(req driver.DecisionRequest, resp driver.DecisionResponse, policy driver.HumanDecisionPolicy) {
if resp.Result != driver.DecisionRejected || p.pendingFailure != nil {
return
}
effective := driver.EffectiveHumanDecisionPolicy(policy)
if effective.OnReject != driver.FailureAbort && effective.OnReject != driver.FailureActionUnset {
return
}
snapshot := req
p.pendingFailure = &driver.RunFailure{
Code: driver.FailureReject,
Message: "CodeBuddy control request was rejected.",
HumanDecision: &driver.HumanDecisionFailure{
Kind: req.Kind, Source: req.Source, Decision: driver.DecisionRejected, Request: &snapshot, Attempts: 1,
},
}
}
func questionPrompt(input map[string]any) string {
questions, _ := input["questions"].([]any)
if len(questions) == 0 {
return "AskUserQuestion"
}
question, _ := questions[0].(map[string]any)
return topString(question, "question", "header")
}
// questionAnswers 将宿主的显式 Answer 原样写入 CodeBuddy 控制响应。
// 未提供 Answer 时,使用 SDK 的 Text 或 Choice 回填首题。
func questionAnswers(input map[string]any, decision driver.DecisionResponse) map[string]any {
if len(decision.Answer) > 0 {
return cloneMap(decision.Answer)
}
answer := strings.TrimSpace(decision.Text)
if answer == "" {
answer = strings.TrimSpace(decision.Choice)
}
question := firstQuestionText(input)
if answer == "" || question == "" {
return nil
}
return map[string]any{question: answer}
}
func firstQuestionText(input map[string]any) string {
questions, _ := input["questions"].([]any)
if len(questions) == 0 {
return ""
}
question, _ := questions[0].(map[string]any)
return topString(question, "question")
}
func questionChoices(input map[string]any) []driver.DecisionChoice {
questions, _ := input["questions"].([]any)
if len(questions) == 0 {
return nil
}
question, _ := questions[0].(map[string]any)
options, _ := question["options"].([]any)
choices := make([]driver.DecisionChoice, 0, len(options))
for _, option := range options {
entry, _ := option.(map[string]any)
label := topString(entry, "label")
choices = append(choices, driver.DecisionChoice{Key: label, Label: label, Description: topString(entry, "description")})
}
return choices
}