This commit is contained in:
2026-06-09 21:19:30 +08:00
parent 8b41ff9360
commit 0484e79978
66 changed files with 4043 additions and 533 deletions
+45 -2
View File
@@ -7,6 +7,8 @@
// FAKE_ACP_FS_READ=path prompt 时发起 fs/read_text_file 请求该路径
// FAKE_ACP_FS_WRITE=path:content prompt 时发起 fs/write_text_file
// FAKE_ACP_PROMPT_UPDATES=N 每 prompt 发 N 个 update(默认 3)
// FAKE_ACP_PERMISSION=toolName prompt 时发起 session/request_permission
// (options: allow_once/reject_once),阻塞等响应后再结束
package main
import (
@@ -66,14 +68,14 @@ func main() {
case "session/new":
respond(out, msg.ID, map[string]any{"sessionId": "fake-session-id"})
case "session/prompt":
handlePrompt(out, msg.ID)
handlePrompt(in, out, msg.ID)
case "session/cancel":
// ignore
}
}
}
func handlePrompt(out *bufio.Writer, id json.RawMessage) {
func handlePrompt(in *bufio.Reader, out *bufio.Writer, id json.RawMessage) {
updates := 3
if v := os.Getenv("FAKE_ACP_PROMPT_UPDATES"); v != "" {
if n, _ := strconv.Atoi(v); n > 0 {
@@ -102,6 +104,10 @@ func handlePrompt(out *bufio.Writer, id json.RawMessage) {
}
}
if tool := os.Getenv("FAKE_ACP_PERMISSION"); tool != "" {
doRequestPermission(in, out, tool)
}
respond(out, id, map[string]any{"stopReason": "end_turn"})
if maxStr := os.Getenv("FAKE_ACP_CRASH_AFTER_PROMPTS"); maxStr != "" {
@@ -139,6 +145,43 @@ func doFsWrite(out *bufio.Writer, path, content string) error {
return nil
}
// doRequestPermission 发起 session/request_permission 并阻塞读取 stdin 直到收到
// 匹配 id 的响应(期间忽略其它消息)。用于端到端验证人工审批/自动放行链路。
func doRequestPermission(in *bufio.Reader, out *bufio.Writer, tool string) {
id := nextServerID.Add(1)
idJSON, _ := json.Marshal(id)
body := mustJSON(Message{
JSONRPC: "2.0", ID: idJSON, Method: "session/request_permission",
Params: mustJSON(map[string]any{
"sessionId": "fake-session-id",
"toolCall": map[string]any{"name": tool},
"options": []map[string]any{
{"id": "allow_once", "name": "Allow"},
{"id": "reject_once", "name": "Reject"},
},
}),
})
_, _ = out.Write(body)
_, _ = out.Write([]byte{'\n'})
_ = out.Flush()
// 阻塞等待该 request 的响应(忽略中途其它帧)。
want := string(idJSON)
for {
line, err := in.ReadBytes('\n')
if err != nil {
return
}
var m Message
if err := json.Unmarshal(line, &m); err != nil {
continue
}
if len(m.ID) > 0 && string(m.ID) == want && (m.Result != nil || m.Error != nil) {
return
}
}
}
func respond(out *bufio.Writer, id json.RawMessage, result any) {
body := mustJSON(Message{JSONRPC: "2.0", ID: id, Result: mustJSON(result)})
_, _ = out.Write(body)