Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/providers/anthropic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ func buildParams(
params.Tools = translateTools(tools)
}

if tcOpt, ok := options["tool_choice"].(map[string]any); ok {
tcType, _ := tcOpt["type"].(string)
switch tcType {
case "auto":
params.ToolChoice = anthropic.ToolChoiceUnionParam{
OfAuto: &anthropic.ToolChoiceAutoParam{},
}
case "any":
params.ToolChoice = anthropic.ToolChoiceUnionParam{
OfAny: &anthropic.ToolChoiceAnyParam{},
}
case "tool":
if name, ok := tcOpt["name"].(string); ok {
params.ToolChoice = anthropic.ToolChoiceParamOfTool(name)
}
case "none":
params.ToolChoice = anthropic.ToolChoiceUnionParam{
OfNone: &anthropic.ToolChoiceNoneParam{},
}
}
}

// Extended Thinking / Adaptive Thinking
// The thinking_level value directly determines the API parameter format:
// "adaptive" → {thinking: {type: "adaptive"}} + output_config.effort
Expand Down Expand Up @@ -326,6 +348,9 @@ func translateTools(tools []ToolDefinition) []anthropic.ToolUnionParam {
}
tool.InputSchema.Required = required
}
if t.CacheControl != nil && t.CacheControl.Type == "ephemeral" {
tool.CacheControl = anthropic.NewCacheControlEphemeralParam()
}
result = append(result, anthropic.ToolUnionParam{OfTool: &tool})
}
return result
Expand Down
22 changes: 21 additions & 1 deletion pkg/providers/openai_compat/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,27 @@ func (p *Provider) Chat(

if len(tools) > 0 {
requestBody["tools"] = tools
requestBody["tool_choice"] = "auto"

if tcOpt, ok := options["tool_choice"].(map[string]any); ok {
tcType, _ := tcOpt["type"].(string)
switch tcType {
case "none":
requestBody["tool_choice"] = "none"
case "auto", "any":
requestBody["tool_choice"] = "auto"
case "tool":
if name, ok := tcOpt["name"].(string); ok {
requestBody["tool_choice"] = map[string]any{
"type": "function",
"function": map[string]any{
"name": name,
},
}
}
}
} else {
requestBody["tool_choice"] = "auto"
}
}

if maxTokens, ok := asInt(options["max_tokens"]); ok {
Expand Down
5 changes: 3 additions & 2 deletions pkg/providers/protocoltypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ type Message struct {
}

type ToolDefinition struct {
Type string `json:"type"`
Function ToolFunctionDefinition `json:"function"`
Type string `json:"type"`
Function ToolFunctionDefinition `json:"function"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}

type ToolFunctionDefinition struct {
Expand Down