| gio | 3050307 | 2025-06-17 10:50:15 +0000 | [diff] [blame^] | 1 | package dodo_tools |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | |
| 10 | "sketch.dev/llm" |
| 11 | ) |
| 12 | |
| 13 | type GetProjectConfigTool struct { |
| 14 | } |
| 15 | |
| 16 | const ( |
| 17 | getProjectSchemaInputSchema = ` |
| 18 | { |
| 19 | "type": "object", |
| 20 | "properties": { |
| 21 | "apiBaseAddress": { |
| 22 | "type": "string", |
| 23 | "description": "The base address of the dodo API" |
| 24 | }, |
| 25 | "projectId": { |
| 26 | "type": "string", |
| 27 | "description": "The ID of the dodo project to get infrastructure configuration." |
| 28 | } |
| 29 | }, |
| 30 | "required": ["apiBaseAddress", "projectId"] |
| 31 | } |
| 32 | ` |
| 33 | ) |
| 34 | |
| 35 | func NewGetProjectConfigTool() *llm.Tool { |
| 36 | tool := &GetProjectConfigTool{} |
| 37 | return &llm.Tool{ |
| 38 | Name: "dodo_get_project_config", |
| 39 | Description: "A tool for getting current state of the infrastructure configuration of a dodo project", |
| 40 | InputSchema: llm.MustSchema(getProjectSchemaInputSchema), |
| 41 | Run: tool.Run, |
| 42 | EndsTurn: true, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | type GetProjectConfigInput struct { |
| 47 | ApiBaseAddress string `json:"apiBaseAddress"` |
| 48 | ProjectId string `json:"projectId"` |
| 49 | } |
| 50 | |
| 51 | type GetProjectConfigOutput struct { |
| 52 | Config string `json:"config"` |
| 53 | } |
| 54 | |
| 55 | func (d *GetProjectConfigTool) Run(ctx context.Context, m json.RawMessage) ([]llm.Content, error) { |
| 56 | var input GetProjectConfigInput |
| 57 | if err := json.Unmarshal(m, &input); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | resp, err := http.Get(fmt.Sprintf("%s/api/project/%s/config", input.ApiBaseAddress, input.ProjectId)) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | defer resp.Body.Close() |
| 65 | body, err := io.ReadAll(resp.Body) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | if resp.StatusCode != http.StatusOK { |
| 70 | return nil, fmt.Errorf("failed to get project config: %s", string(body)) |
| 71 | } |
| 72 | var config map[string]interface{} |
| 73 | if err := json.Unmarshal(body, &config); err != nil { |
| 74 | return nil, fmt.Errorf("got invalid project config: %s %s", err, string(body)) |
| 75 | } |
| 76 | output := GetProjectConfigOutput{ |
| 77 | Config: string(body), |
| 78 | } |
| 79 | jsonOutput, err := json.Marshal(output) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return llm.TextContent(string(jsonOutput)), nil |
| 84 | } |