|
| 1 | +package bridge |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "ccgo/internal/commands" |
| 10 | + "ccgo/internal/contracts" |
| 11 | +) |
| 12 | + |
| 13 | +type DirectOptions struct { |
| 14 | + SessionID contracts.ID |
| 15 | + Manifest Manifest |
| 16 | + Registry commands.Registry |
| 17 | +} |
| 18 | + |
| 19 | +type DirectHandler struct { |
| 20 | + sessionID contracts.ID |
| 21 | + manifest Manifest |
| 22 | + registry commands.Registry |
| 23 | +} |
| 24 | + |
| 25 | +type DirectHealthResponse struct { |
| 26 | + OK bool `json:"ok"` |
| 27 | + SessionID contracts.ID `json:"session_id,omitempty"` |
| 28 | + Commands int `json:"commands"` |
| 29 | +} |
| 30 | + |
| 31 | +type DirectCommandRequest struct { |
| 32 | + Command string `json:"command"` |
| 33 | + UUID contracts.ID `json:"uuid,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +type DirectResolveResponse struct { |
| 37 | + Allowed bool `json:"allowed"` |
| 38 | + Command *Command `json:"command,omitempty"` |
| 39 | + Name string `json:"name,omitempty"` |
| 40 | + Args string `json:"args,omitempty"` |
| 41 | + Reason string `json:"reason,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +type DirectExecuteResponse struct { |
| 45 | + Allowed bool `json:"allowed"` |
| 46 | + Handled bool `json:"handled"` |
| 47 | + Command *Command `json:"command,omitempty"` |
| 48 | + Name string `json:"name,omitempty"` |
| 49 | + Args string `json:"args,omitempty"` |
| 50 | + ShouldQuery bool `json:"should_query"` |
| 51 | + Model string `json:"model,omitempty"` |
| 52 | + AllowedTools []string `json:"allowed_tools,omitempty"` |
| 53 | + LocalResult *DirectLocalResult `json:"local_result,omitempty"` |
| 54 | + ResultText string `json:"result_text,omitempty"` |
| 55 | + Messages int `json:"messages"` |
| 56 | + Unknown bool `json:"unknown,omitempty"` |
| 57 | + Unsupported bool `json:"unsupported,omitempty"` |
| 58 | + Error string `json:"error,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +type DirectLocalResult struct { |
| 62 | + Type commands.LocalCommandResultType `json:"type"` |
| 63 | + HasValue bool `json:"has_value"` |
| 64 | +} |
| 65 | + |
| 66 | +type DirectErrorResponse struct { |
| 67 | + Error string `json:"error"` |
| 68 | +} |
| 69 | + |
| 70 | +func NewDirectHandler(opts DirectOptions) *DirectHandler { |
| 71 | + sessionID := opts.SessionID |
| 72 | + if sessionID == "" { |
| 73 | + sessionID = opts.Manifest.SessionID |
| 74 | + } |
| 75 | + return &DirectHandler{ |
| 76 | + sessionID: sessionID, |
| 77 | + manifest: opts.Manifest, |
| 78 | + registry: opts.Registry, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func NewDirectHandlerFromSettings(sessionID contracts.ID, cwd string, settings contracts.Settings) *DirectHandler { |
| 83 | + registry := commands.Load(commands.Options{CWD: cwd, Settings: settings}) |
| 84 | + return NewDirectHandler(DirectOptions{ |
| 85 | + SessionID: sessionID, |
| 86 | + Manifest: BuildManifest(sessionID, cwd, registry), |
| 87 | + Registry: registry, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func (h *DirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 92 | + switch normalizeDirectPath(r.URL.Path) { |
| 93 | + case "/health": |
| 94 | + h.handleHealth(w, r) |
| 95 | + case "/manifest": |
| 96 | + h.handleManifest(w, r) |
| 97 | + case "/resolve": |
| 98 | + h.handleResolve(w, r) |
| 99 | + case "/execute": |
| 100 | + h.handleExecute(w, r) |
| 101 | + default: |
| 102 | + writeDirectError(w, http.StatusNotFound, "bridge endpoint not found") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (h *DirectHandler) handleHealth(w http.ResponseWriter, r *http.Request) { |
| 107 | + if r.Method != http.MethodGet { |
| 108 | + writeDirectError(w, http.StatusMethodNotAllowed, "method not allowed") |
| 109 | + return |
| 110 | + } |
| 111 | + writeDirectJSON(w, http.StatusOK, DirectHealthResponse{ |
| 112 | + OK: true, |
| 113 | + SessionID: h.sessionID, |
| 114 | + Commands: len(h.manifest.Commands), |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +func (h *DirectHandler) handleManifest(w http.ResponseWriter, r *http.Request) { |
| 119 | + if r.Method != http.MethodGet { |
| 120 | + writeDirectError(w, http.StatusMethodNotAllowed, "method not allowed") |
| 121 | + return |
| 122 | + } |
| 123 | + writeDirectJSON(w, http.StatusOK, h.manifest) |
| 124 | +} |
| 125 | + |
| 126 | +func (h *DirectHandler) handleResolve(w http.ResponseWriter, r *http.Request) { |
| 127 | + if r.Method != http.MethodPost { |
| 128 | + writeDirectError(w, http.StatusMethodNotAllowed, "method not allowed") |
| 129 | + return |
| 130 | + } |
| 131 | + req, ok := decodeDirectCommandRequest(w, r) |
| 132 | + if !ok { |
| 133 | + return |
| 134 | + } |
| 135 | + resolved := h.resolve(req.Command) |
| 136 | + writeDirectJSON(w, http.StatusOK, resolved) |
| 137 | +} |
| 138 | + |
| 139 | +func (h *DirectHandler) handleExecute(w http.ResponseWriter, r *http.Request) { |
| 140 | + if r.Method != http.MethodPost { |
| 141 | + writeDirectError(w, http.StatusMethodNotAllowed, "method not allowed") |
| 142 | + return |
| 143 | + } |
| 144 | + req, ok := decodeDirectCommandRequest(w, r) |
| 145 | + if !ok { |
| 146 | + return |
| 147 | + } |
| 148 | + resolved := h.resolve(req.Command) |
| 149 | + if !resolved.Allowed || resolved.Command == nil { |
| 150 | + writeDirectJSON(w, http.StatusForbidden, DirectExecuteResponse{ |
| 151 | + Allowed: false, |
| 152 | + Error: resolved.Reason, |
| 153 | + }) |
| 154 | + return |
| 155 | + } |
| 156 | + canonical := canonicalSlashCommand(*resolved.Command, resolved.Args) |
| 157 | + result, handled, err := commands.ExecuteSlashCommand(h.registry, canonical, commands.SlashOptions{ |
| 158 | + SessionID: h.sessionID, |
| 159 | + UUID: req.UUID, |
| 160 | + }) |
| 161 | + response := DirectExecuteResponse{ |
| 162 | + Allowed: true, |
| 163 | + Handled: handled, |
| 164 | + Command: resolved.Command, |
| 165 | + Name: resolved.Name, |
| 166 | + Args: resolved.Args, |
| 167 | + } |
| 168 | + if err != nil { |
| 169 | + response.Error = err.Error() |
| 170 | + writeDirectJSON(w, http.StatusInternalServerError, response) |
| 171 | + return |
| 172 | + } |
| 173 | + response.ShouldQuery = result.ShouldQuery |
| 174 | + response.Model = result.Model |
| 175 | + response.AllowedTools = append([]string(nil), result.AllowedTools...) |
| 176 | + response.Messages = len(result.Messages) |
| 177 | + response.Unknown = result.Unknown |
| 178 | + response.Unsupported = result.Unsupported |
| 179 | + response.ResultText = result.ResultText |
| 180 | + if result.LocalResult != nil { |
| 181 | + response.LocalResult = &DirectLocalResult{ |
| 182 | + Type: result.LocalResult.Type, |
| 183 | + HasValue: strings.TrimSpace(result.LocalResult.Value) != "", |
| 184 | + } |
| 185 | + } |
| 186 | + writeDirectJSON(w, http.StatusOK, response) |
| 187 | +} |
| 188 | + |
| 189 | +func (h *DirectHandler) resolve(raw string) DirectResolveResponse { |
| 190 | + raw = strings.TrimSpace(raw) |
| 191 | + if raw == "" { |
| 192 | + return DirectResolveResponse{Allowed: false, Reason: "command is required"} |
| 193 | + } |
| 194 | + command, ok := h.manifest.FindCommand(raw) |
| 195 | + if !ok { |
| 196 | + return DirectResolveResponse{Allowed: false, Reason: "command is not bridge-safe or is not registered"} |
| 197 | + } |
| 198 | + _, args := splitBridgeCommand(raw) |
| 199 | + return DirectResolveResponse{ |
| 200 | + Allowed: true, |
| 201 | + Command: &command, |
| 202 | + Name: command.Name, |
| 203 | + Args: args, |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func decodeDirectCommandRequest(w http.ResponseWriter, r *http.Request) (DirectCommandRequest, bool) { |
| 208 | + defer r.Body.Close() |
| 209 | + var req DirectCommandRequest |
| 210 | + decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64*1024)) |
| 211 | + decoder.DisallowUnknownFields() |
| 212 | + if err := decoder.Decode(&req); err != nil { |
| 213 | + writeDirectError(w, http.StatusBadRequest, "invalid JSON request: "+err.Error()) |
| 214 | + return DirectCommandRequest{}, false |
| 215 | + } |
| 216 | + if strings.TrimSpace(req.Command) == "" { |
| 217 | + writeDirectError(w, http.StatusBadRequest, "command is required") |
| 218 | + return DirectCommandRequest{}, false |
| 219 | + } |
| 220 | + return req, true |
| 221 | +} |
| 222 | + |
| 223 | +func canonicalSlashCommand(command Command, args string) string { |
| 224 | + if strings.TrimSpace(args) == "" { |
| 225 | + return "/" + command.Name |
| 226 | + } |
| 227 | + return "/" + command.Name + " " + strings.TrimSpace(args) |
| 228 | +} |
| 229 | + |
| 230 | +func splitBridgeCommand(raw string) (string, string) { |
| 231 | + raw = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(raw), "/")) |
| 232 | + if raw == "" { |
| 233 | + return "", "" |
| 234 | + } |
| 235 | + fields := strings.Fields(raw) |
| 236 | + if len(fields) == 0 { |
| 237 | + return "", "" |
| 238 | + } |
| 239 | + name := fields[0] |
| 240 | + offset := strings.Index(raw, name) + len(name) |
| 241 | + return name, strings.TrimSpace(raw[offset:]) |
| 242 | +} |
| 243 | + |
| 244 | +func normalizeDirectPath(path string) string { |
| 245 | + path = strings.TrimSpace(path) |
| 246 | + if path == "" { |
| 247 | + return "/" |
| 248 | + } |
| 249 | + path = strings.TrimRight(path, "/") |
| 250 | + if path == "" { |
| 251 | + path = "/" |
| 252 | + } |
| 253 | + if path == "/v1" { |
| 254 | + return "/" |
| 255 | + } |
| 256 | + if strings.HasPrefix(path, "/v1/") { |
| 257 | + path = strings.TrimPrefix(path, "/v1") |
| 258 | + } |
| 259 | + if path == "" { |
| 260 | + return "/" |
| 261 | + } |
| 262 | + if !strings.HasPrefix(path, "/") { |
| 263 | + return "/" + path |
| 264 | + } |
| 265 | + return path |
| 266 | +} |
| 267 | + |
| 268 | +func writeDirectJSON(w http.ResponseWriter, status int, value any) { |
| 269 | + w.Header().Set("Content-Type", "application/json") |
| 270 | + w.WriteHeader(status) |
| 271 | + if err := json.NewEncoder(w).Encode(value); err != nil { |
| 272 | + _, _ = fmt.Fprintf(w, `{"error":%q}`+"\n", err.Error()) |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +func writeDirectError(w http.ResponseWriter, status int, message string) { |
| 277 | + writeDirectJSON(w, status, DirectErrorResponse{Error: message}) |
| 278 | +} |
0 commit comments