| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 1 | package dodo_tools |
| 2 | |
| 3 | import ( |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 4 | "bytes" |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 5 | "context" |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | |
| 11 | "sketch.dev/llm" |
| 12 | ) |
| 13 | |
| 14 | type GetProjectConfigTool struct { |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 15 | apiBaseAddress string |
| 16 | projectId string |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 19 | func NewGetProjectConfigTool(apiBaseAddress string, projectId string) *llm.Tool { |
| 20 | tool := &GetProjectConfigTool{ |
| 21 | apiBaseAddress: apiBaseAddress, |
| 22 | projectId: projectId, |
| 23 | } |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 24 | return &llm.Tool{ |
| 25 | Name: "dodo_get_project_config", |
| 26 | Description: "A tool for getting current state of the infrastructure configuration of a dodo project", |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 27 | InputSchema: llm.EmptySchema(), |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 28 | Run: tool.Run, |
| 29 | EndsTurn: true, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | type GetProjectConfigInput struct { |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type GetProjectConfigOutput struct { |
| 37 | Config string `json:"config"` |
| 38 | } |
| 39 | |
| 40 | func (d *GetProjectConfigTool) Run(ctx context.Context, m json.RawMessage) ([]llm.Content, error) { |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 41 | resp, err := http.Get(fmt.Sprintf("%s/api/project/%s/config", d.apiBaseAddress, d.projectId)) |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | defer resp.Body.Close() |
| 46 | body, err := io.ReadAll(resp.Body) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if resp.StatusCode != http.StatusOK { |
| 51 | return nil, fmt.Errorf("failed to get project config: %s", string(body)) |
| 52 | } |
| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame] | 53 | output := GetProjectConfigOutput{ |
| 54 | Config: string(body), |
| 55 | } |
| 56 | jsonOutput, err := json.Marshal(output) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | return llm.TextContent(string(jsonOutput)), nil |
| 61 | } |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 62 | |
| 63 | type ValidateConfigTool struct { |
| 64 | apiBaseAddress string |
| 65 | } |
| 66 | |
| 67 | const ( |
| 68 | validateConfigSchemaInputSchema = ` |
| 69 | { |
| 70 | "type": "object", |
| 71 | "properties": { |
| 72 | "config": { |
| 73 | "type": "string", |
| 74 | "description": "The dodo-app configuration to validate" |
| 75 | } |
| 76 | }, |
| 77 | "required": ["config"] |
| 78 | } |
| 79 | ` |
| 80 | ) |
| 81 | |
| 82 | func NewValidateConfigTool(apiBaseAddress string) *llm.Tool { |
| 83 | tool := &ValidateConfigTool{ |
| 84 | apiBaseAddress: apiBaseAddress, |
| 85 | } |
| 86 | return &llm.Tool{ |
| 87 | Name: "dodo_validate_config", |
| 88 | Description: "A tool for validating the dodo-app configuration", |
| 89 | InputSchema: llm.MustSchema(validateConfigSchemaInputSchema), |
| 90 | Run: tool.Run, |
| 91 | EndsTurn: true, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | type ValidateConfigInput struct { |
| 96 | Config string `json:"config"` |
| 97 | } |
| 98 | |
| 99 | type ValidateConfigOutput struct { |
| 100 | Success bool `json:"success"` |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 101 | Errors any `json:"errors,omitempty"` |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func (d *ValidateConfigTool) Run(ctx context.Context, m json.RawMessage) ([]llm.Content, error) { |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 105 | var input ValidateConfigInput |
| 106 | if err := json.Unmarshal(m, &input); err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | resp, err := http.Post(fmt.Sprintf("%s/api/validate-config", d.apiBaseAddress), "application/json", bytes.NewBuffer([]byte(input.Config))) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | defer resp.Body.Close() |
| 114 | body, err := io.ReadAll(resp.Body) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | if resp.StatusCode != http.StatusOK { |
| 119 | return nil, fmt.Errorf("failed to validate config: %s", string(body)) |
| 120 | } |
| 121 | var output ValidateConfigOutput |
| 122 | if err := json.Unmarshal(body, &output); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | jsonOutput, err := json.Marshal(output) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return llm.TextContent(string(jsonOutput)), nil |
| 130 | } |
| 131 | |
| 132 | type DeployProjectTool struct { |
| 133 | apiBaseAddress string |
| 134 | projectId string |
| 135 | } |
| 136 | |
| 137 | const ( |
| 138 | deployProjectSchemaInputSchema = ` |
| 139 | { |
| 140 | "type": "object", |
| 141 | "properties": { |
| 142 | "config": { |
| 143 | "type": "string", |
| 144 | "description": "Serialized dodo-app configuration to deploy" |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | ` |
| 149 | ) |
| 150 | |
| 151 | func NewDeployProjectTool(apiBaseAddress string, projectId string) *llm.Tool { |
| 152 | tool := &DeployProjectTool{ |
| 153 | apiBaseAddress: apiBaseAddress, |
| 154 | projectId: projectId, |
| 155 | } |
| 156 | |
| 157 | return &llm.Tool{ |
| 158 | Name: "dodo_deploy_project", |
| 159 | Description: "A tool for deploying the dodo-app configuration", |
| 160 | InputSchema: llm.MustSchema(deployProjectSchemaInputSchema), |
| 161 | Run: tool.Run, |
| 162 | EndsTurn: true, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | type DeployProjectInput struct { |
| 167 | Config string `json:"config"` |
| 168 | } |
| 169 | |
| 170 | type DeployProjectOutput struct { |
| 171 | Success bool `json:"success"` |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 172 | Errors any `json:"errors,omitempty"` |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | type deployProjectReq struct { |
| 176 | Config map[string]any `json:"config"` |
| 177 | } |
| 178 | |
| 179 | func (d *DeployProjectTool) Run(ctx context.Context, m json.RawMessage) ([]llm.Content, error) { |
| 180 | var input DeployProjectInput |
| 181 | if err := json.Unmarshal(m, &input); err != nil { |
| 182 | return nil, err |
| 183 | } |
| 184 | req := deployProjectReq{} |
| 185 | if err := json.Unmarshal([]byte(input.Config), &req.Config); err != nil { |
| 186 | return nil, err |
| 187 | } |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 188 | jsonReq, err := json.Marshal(req) |
| 189 | if err != nil { |
| 190 | return nil, err |
| 191 | } |
| 192 | resp, err := http.Post(fmt.Sprintf("%s/api/project/%s/deploy", d.apiBaseAddress, d.projectId), "application/json", bytes.NewBuffer(jsonReq)) |
| 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | defer resp.Body.Close() |
| 197 | body, err := io.ReadAll(resp.Body) |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | if resp.StatusCode != http.StatusOK { |
| 202 | return nil, fmt.Errorf("failed to deploy project: %s", string(body)) |
| 203 | } |
| 204 | return llm.TextContent(string("Project deployed successfully")), nil |
| 205 | } |
| 206 | |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 207 | // Save |
| 208 | |
| 209 | type SaveProjectTool struct { |
| 210 | apiBaseAddress string |
| 211 | projectId string |
| 212 | } |
| 213 | |
| 214 | const ( |
| 215 | saveProjectSchemaInputSchema = ` |
| 216 | { |
| 217 | "type": "object", |
| 218 | "properties": { |
| 219 | "config": { |
| 220 | "type": "string", |
| 221 | "description": "Serialized dodo-app configuration to save" |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | ` |
| 226 | ) |
| 227 | |
| 228 | func NewSaveProjectTool(apiBaseAddress string, projectId string) *llm.Tool { |
| 229 | tool := &SaveProjectTool{ |
| 230 | apiBaseAddress: apiBaseAddress, |
| 231 | projectId: projectId, |
| 232 | } |
| 233 | |
| 234 | return &llm.Tool{ |
| 235 | Name: "dodo_save_config", |
| 236 | Description: "A tool for saving the dodo-app configuration", |
| 237 | InputSchema: llm.MustSchema(saveProjectSchemaInputSchema), |
| 238 | Run: tool.Run, |
| 239 | EndsTurn: true, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | type SaveProjectInput struct { |
| 244 | Config string `json:"config"` |
| 245 | } |
| 246 | |
| 247 | type SaveProjectOutput struct { |
| 248 | Success bool `json:"success"` |
| 249 | Errors any `json:"errors,omitempty"` |
| 250 | } |
| 251 | |
| 252 | type saveProjectReq struct { |
| 253 | Type string `json:"type"` |
| 254 | Config map[string]any `json:"config"` |
| 255 | } |
| 256 | |
| 257 | func (d *SaveProjectTool) Run(ctx context.Context, m json.RawMessage) ([]llm.Content, error) { |
| 258 | var input SaveProjectInput |
| 259 | if err := json.Unmarshal(m, &input); err != nil { |
| 260 | return nil, err |
| 261 | } |
| 262 | req := saveProjectReq{ |
| 263 | Type: "config", |
| 264 | } |
| 265 | if err := json.Unmarshal([]byte(input.Config), &req.Config); err != nil { |
| 266 | return nil, err |
| 267 | } |
| 268 | jsonReq, err := json.Marshal(req) |
| 269 | if err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | resp, err := http.Post(fmt.Sprintf("%s/api/project/%s/saved", d.apiBaseAddress, d.projectId), "application/json", bytes.NewBuffer(jsonReq)) |
| 273 | if err != nil { |
| 274 | return nil, err |
| 275 | } |
| 276 | defer resp.Body.Close() |
| 277 | body, err := io.ReadAll(resp.Body) |
| 278 | if err != nil { |
| 279 | return nil, err |
| 280 | } |
| 281 | if resp.StatusCode != http.StatusOK { |
| 282 | return nil, fmt.Errorf("failed to save project: %s", string(body)) |
| 283 | } |
| 284 | return llm.TextContent(string("Project saved successfully")), nil |
| 285 | } |
| 286 | |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 287 | func NewDodoTools(apiBaseAddress string, projectId string) []*llm.Tool { |
| 288 | return []*llm.Tool{ |
| 289 | NewGetProjectConfigTool(apiBaseAddress, projectId), |
| 290 | NewValidateConfigTool(apiBaseAddress), |
| 291 | NewDeployProjectTool(apiBaseAddress, projectId), |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 292 | NewSaveProjectTool(apiBaseAddress, projectId), |
| gio | fe6e714 | 2025-06-18 08:51:23 +0000 | [diff] [blame] | 293 | } |
| Giorgi Lekveishvili | 0556983 | 2025-07-06 09:16:29 +0400 | [diff] [blame^] | 294 | } |