(ctx context.Context, input map[string]any, tc *tools.ToolContext)
| 136 | } |
| 137 | |
| 138 | func (t *AskUserQuestionTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 139 | // 验证必需参数 |
| 140 | if err := ValidateRequired(input, []string{"questions"}); err != nil { |
| 141 | return NewClaudeErrorResponse(err), nil |
| 142 | } |
| 143 | |
| 144 | // 解析问题列表 |
| 145 | questions, err := t.parseQuestions(input["questions"]) |
| 146 | if err != nil { |
| 147 | return NewClaudeErrorResponse(err), nil |
| 148 | } |
| 149 | |
| 150 | // 验证问题数量 |
| 151 | if len(questions) < 1 || len(questions) > 4 { |
| 152 | return NewClaudeErrorResponse(fmt.Errorf("questions count must be between 1 and 4, got %d", len(questions))), nil |
| 153 | } |
| 154 | |
| 155 | // 验证每个问题 |
| 156 | for i, q := range questions { |
| 157 | if err := t.validateQuestion(q, i); err != nil { |
| 158 | return NewClaudeErrorResponse(err), nil |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // 生成请求ID |
| 163 | requestID := uuid.New().String() |
| 164 | |
| 165 | // 创建响应通道(带缓冲,确保发送不会阻塞) |
| 166 | responseChan := make(chan map[string]any, 1) |
| 167 | t.pendingRequests[requestID] = responseChan |
| 168 | |
| 169 | // 创建请求对象并注册到全局表 |
| 170 | req := &askUserRequest{ |
| 171 | responseChan: responseChan, |
| 172 | answered: false, |
| 173 | } |
| 174 | globalAskUserRequestsMu.Lock() |
| 175 | globalAskUserRequests[requestID] = req |
| 176 | globalAskUserRequestsMu.Unlock() |
| 177 | |
| 178 | askUserLog.Info(context.Background(), "registered request, waiting for user response", map[string]any{"request_id": requestID}) |
| 179 | |
| 180 | // 创建响应回调函数(用于 Emit 事件) |
| 181 | respond := func(answers map[string]any) error { |
| 182 | return RespondToAskUser(requestID, answers) |
| 183 | } |
| 184 | |
| 185 | // 发送事件到 Control 通道 |
| 186 | if tc.Reporter != nil { |
| 187 | // 通过 Reporter 发送中间结果,包含事件信息 |
| 188 | tc.Reporter.Intermediate("ask_user_event", map[string]any{ |
| 189 | "request_id": requestID, |
| 190 | "questions": questions, |
| 191 | "event_type": "ask_user", |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | // 如果有 Emit 函数,使用它发送事件 |
nothing calls this directly
no test coverage detected