| 1 | package responses |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | |
| 6 | "reasonix/internal/provider" |
| 7 | ) |
| 8 | |
| 9 | func encodeResponsesTools(c *client, req provider.Request) []map[string]any { |
| 10 | tools := make([]map[string]any, 0, len(req.Tools)+1) |
| 11 | if c.search.NativeEnabled && !c.search.ClientEnabled { |
| 12 | tools = append(tools, map[string]any{"type": "web_search"}) |
| 13 | } |
| 14 | for _, tool := range req.Tools { |
| 15 | parameters := tool.Parameters |
| 16 | if len(parameters) == 0 { |
| 17 | parameters = provider.CanonicalizeSchema(nil) |
| 18 | } |
| 19 | item := map[string]any{ |
| 20 | "type": "function", "name": tool.Name, "description": tool.Description, |
| 21 | "parameters": json.RawMessage(parameters), |
| 22 | } |
| 23 | if tool.Strict && provider.IsFirstPartyOpenAI(c.baseURL) { |
| 24 | item["strict"] = true |
| 25 | } |
| 26 | if tool.Deferred && provider.IsFirstPartyOpenAI(c.baseURL) { |
| 27 | item["defer_loading"] = true |
| 28 | } |
| 29 | tools = append(tools, item) |
| 30 | } |
| 31 | if req.ToolSearch != nil && req.ToolSearch.Enabled && provider.IsFirstPartyOpenAI(c.baseURL) { |
| 32 | tools = append([]map[string]any{{"type": "tool_search"}}, tools...) |
| 33 | } |
| 34 | return tools |
| 35 | } |
| 36 |