dynamicToolsFromSDK converts codersdk.DynamicTool definitions into fantasy.AgentTool implementations for inclusion in the LLM tool list.
(logger slog.Logger, tools []codersdk.DynamicTool)
| 28 | // into fantasy.AgentTool implementations for inclusion in the LLM |
| 29 | // tool list. |
| 30 | func dynamicToolsFromSDK(logger slog.Logger, tools []codersdk.DynamicTool) []fantasy.AgentTool { |
| 31 | if len(tools) == 0 { |
| 32 | return nil |
| 33 | } |
| 34 | result := make([]fantasy.AgentTool, 0, len(tools)) |
| 35 | for _, t := range tools { |
| 36 | dt := &dynamicTool{ |
| 37 | name: t.Name, |
| 38 | description: t.Description, |
| 39 | } |
| 40 | // InputSchema is a full JSON Schema object stored as |
| 41 | // json.RawMessage. Extract the "properties" and |
| 42 | // "required" fields that fantasy.ToolInfo expects. |
| 43 | if len(t.InputSchema) > 0 { |
| 44 | var schema struct { |
| 45 | Properties map[string]any `json:"properties"` |
| 46 | Required []string `json:"required"` |
| 47 | } |
| 48 | if err := json.Unmarshal(t.InputSchema, &schema); err != nil { |
| 49 | // Defensive: present the tool with no parameter |
| 50 | // constraints rather than failing. The LLM may |
| 51 | // hallucinate argument shapes, but the tool will |
| 52 | // still appear in the tool list. |
| 53 | logger.Warn(context.Background(), "failed to parse dynamic tool input schema", |
| 54 | slog.F("tool_name", t.Name), |
| 55 | slog.Error(err)) |
| 56 | } else { |
| 57 | dt.parameters = schema.Properties |
| 58 | dt.required = schema.Required |
| 59 | } |
| 60 | } |
| 61 | result = append(result, dt) |
| 62 | } |
| 63 | return result |
| 64 | } |
| 65 | |
| 66 | func (t *dynamicTool) Info() fantasy.ToolInfo { |
| 67 | return fantasy.ToolInfo{ |