|
| 1 | +package builtin |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "time" |
| 12 | + |
| 13 | + latest "github.com/docker/cagent/pkg/config/v2" |
| 14 | + "github.com/docker/cagent/pkg/js" |
| 15 | + "github.com/docker/cagent/pkg/tools" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + ToolNameAPI = "api" |
| 20 | +) |
| 21 | + |
| 22 | +type APITool struct { |
| 23 | + tools.ElicitationTool |
| 24 | + handler *apiHandler |
| 25 | + config latest.APIToolConfig |
| 26 | +} |
| 27 | + |
| 28 | +var _ tools.ToolSet = (*APITool)(nil) |
| 29 | + |
| 30 | +type apiHandler struct { |
| 31 | + config latest.APIToolConfig |
| 32 | +} |
| 33 | + |
| 34 | +func (h *apiHandler) CallTool(ctx context.Context, toolCall tools.ToolCall) (*tools.ToolCallResult, error) { |
| 35 | + client := &http.Client{ |
| 36 | + Timeout: 30 * time.Second, |
| 37 | + } |
| 38 | + |
| 39 | + endpoint := h.config.Endpoint |
| 40 | + var reqBody io.Reader = http.NoBody |
| 41 | + switch h.config.Method { |
| 42 | + case http.MethodGet: |
| 43 | + if toolCall.Function.Arguments != "" { |
| 44 | + var params map[string]string |
| 45 | + |
| 46 | + if err := json.Unmarshal([]byte(toolCall.Function.Arguments), ¶ms); err != nil { |
| 47 | + return nil, fmt.Errorf("invalid arguments: %w", err) |
| 48 | + } |
| 49 | + expanded, err := js.ExpandString(ctx, endpoint, params) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to expand endpoint: %w", err) |
| 52 | + } |
| 53 | + endpoint = expanded |
| 54 | + } |
| 55 | + case http.MethodPost: |
| 56 | + var params map[string]any |
| 57 | + |
| 58 | + if err := json.Unmarshal([]byte(toolCall.Function.Arguments), ¶ms); err != nil { |
| 59 | + return nil, fmt.Errorf("invalid arguments: %w", err) |
| 60 | + } |
| 61 | + jsonData, err := json.Marshal(params) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to marshal request body: %v", err) |
| 64 | + } |
| 65 | + reqBody = bytes.NewReader(jsonData) |
| 66 | + } |
| 67 | + |
| 68 | + req, err := http.NewRequestWithContext(ctx, h.config.Method, endpoint, reqBody) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to create request: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + req.Header.Set("User-Agent", userAgent) |
| 74 | + if h.config.Method == http.MethodPost { |
| 75 | + req.Header.Set("Content-Type", "application/json") |
| 76 | + } |
| 77 | + |
| 78 | + for key, value := range h.config.Headers { |
| 79 | + req.Header.Set(key, value) |
| 80 | + } |
| 81 | + |
| 82 | + resp, err := client.Do(req) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("request failed: %v", err) |
| 85 | + } |
| 86 | + defer resp.Body.Close() |
| 87 | + |
| 88 | + maxSize := int64(1 << 20) |
| 89 | + body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize)) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("failed to read response body: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + return &tools.ToolCallResult{Output: string(body)}, nil |
| 95 | +} |
| 96 | + |
| 97 | +type APIToolOption func(*APITool) |
| 98 | + |
| 99 | +func NewAPITool(config latest.APIToolConfig) *APITool { |
| 100 | + return &APITool{ |
| 101 | + config: config, |
| 102 | + handler: &apiHandler{ |
| 103 | + config: config, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (t *APITool) Instructions() string { |
| 109 | + return t.config.Instruction |
| 110 | +} |
| 111 | + |
| 112 | +func (t *APITool) Tools(context.Context) ([]tools.Tool, error) { |
| 113 | + inputSchema, err := tools.SchemaToMap(map[string]any{ |
| 114 | + "type": "object", |
| 115 | + "properties": t.config.Args, |
| 116 | + "required": t.config.Required, |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("invalid schema: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + parsedURL, err := url.Parse(t.config.Endpoint) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("invalid URL: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + if parsedURL.Scheme == "" || parsedURL.Host == "" { |
| 128 | + return nil, fmt.Errorf("invalid URL: missing scheme or host") |
| 129 | + } |
| 130 | + |
| 131 | + if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" { |
| 132 | + return nil, fmt.Errorf("only HTTP and HTTPS URLs are supported") |
| 133 | + } |
| 134 | + |
| 135 | + return []tools.Tool{ |
| 136 | + { |
| 137 | + Name: t.config.Name, |
| 138 | + Category: "api", |
| 139 | + Description: t.config.Instruction, |
| 140 | + Parameters: inputSchema, |
| 141 | + OutputSchema: tools.MustSchemaFor[string](), |
| 142 | + Handler: t.handler.CallTool, |
| 143 | + Annotations: tools.ToolAnnotations{ |
| 144 | + ReadOnlyHint: true, |
| 145 | + Title: "API URLs", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (t *APITool) Start(context.Context) error { |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func (t *APITool) Stop(context.Context) error { |
| 156 | + return nil |
| 157 | +} |
0 commit comments