handleToolCall 处理工具调用请求
(w http.ResponseWriter, r *http.Request)
| 126 | |
| 127 | // handleToolCall 处理工具调用请求 |
| 128 | func (s *HTTPBridgeServer) handleToolCall(w http.ResponseWriter, r *http.Request) { |
| 129 | // 仅支持 POST |
| 130 | if r.Method != http.MethodPost { |
| 131 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | // 解析请求 |
| 136 | var req ToolCallRequest |
| 137 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 138 | s.sendError(w, "Invalid JSON request", http.StatusBadRequest) |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | // 验证参数 |
| 143 | if req.Tool == "" { |
| 144 | s.sendError(w, "Tool name is required", http.StatusBadRequest) |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | // 获取工具上下文 |
| 149 | tc := s.getToolContext() |
| 150 | |
| 151 | // 调用工具 |
| 152 | ctx := r.Context() |
| 153 | result, _ := s.bridge.CallTool(ctx, req.Tool, req.Input, tc) |
| 154 | |
| 155 | // 返回响应 |
| 156 | s.sendJSON(w, &ToolCallResponse{ |
| 157 | Success: result.Success, |
| 158 | Result: result.Result, |
| 159 | Error: result.Error, |
| 160 | }) |
| 161 | } |
| 162 | |
| 163 | // handleToolList 处理工具列表请求 |
| 164 | func (s *HTTPBridgeServer) handleToolList(w http.ResponseWriter, r *http.Request) { |