Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,7 @@ custom tool 为 `{name,description,input_schema}`,可选 `type:"custom"`。ser
- `web_search_*` 的 `allowed_domains`、`blocked_domains` 与 `user_location` 写入搜索偏好,域名限制使用 `site:` 与 `-site:` 查询
- `web_fetch_*` 的 `allowed_domains` 与 `blocked_domains` 作为读取 URL 的域名范围(含子域名)写入系统指令
- 调用次数由上游决定,其余选项保持上游请求不变
- named `tool` 的 `tool_choice` 指定上表的 server tool 时为 `required`,本次只允许该工具
- `tool_search_tool_*`、`advisor_*` 与类型名含 `toolset` 的工具不声明到上游

Anthropic 定义的客户端工具以请求中的 `name` 声明为函数,描述与参数由服务固定,模型调用以 `tool_use` 返回由客户端执行:
Expand Down
35 changes: 26 additions & 9 deletions internal/api/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,19 @@ func anthropicParts(raw json.RawMessage) ([]aistudio.Part, error) {
}

func mapAnthropicTools(tools []anthropicTool, choice json.RawMessage) (aistudio.Tools, error) {
config, err := anthropicToolChoice(choice)
if err != nil {
return aistudio.Tools{}, err
}
var mapped aistudio.Tools
// chosen 是 tool_choice 指定的 server tool 对应的 Google 工具
chosen := ""
server := func(tool anthropicTool, google string) {
mapped.Google = appendUnique(mapped.Google, google)
if len(config.AllowedFunctionNames) == 1 && config.AllowedFunctionNames[0] == tool.Name {
chosen = google
}
}
for _, tool := range tools {
typeName := strings.ToLower(tool.Type)
switch {
Expand All @@ -748,12 +760,12 @@ func mapAnthropicTools(tools []anthropicTool, choice json.RawMessage) (aistudio.
return aistudio.Tools{}, fmt.Errorf("web_search blocked_domains: %w", err)
}
mapped.GoogleSearch = search
mapped.Google = appendUnique(mapped.Google, "google_search")
server(tool, "google_search")
case typeName == "image_search":
if err := validateAnthropicServerTool(tool, "image_search"); err != nil {
return aistudio.Tools{}, err
}
mapped.Google = appendUnique(mapped.Google, "image_search")
server(tool, "image_search")
case strings.HasPrefix(typeName, "web_fetch_"):
if err := validateAnthropicServerTool(tool, "web_fetch"); err != nil {
return aistudio.Tools{}, err
Expand All @@ -769,12 +781,12 @@ func mapAnthropicTools(tools []anthropicTool, choice json.RawMessage) (aistudio.
if len(fetch.AllowedDomains) > 0 || len(fetch.BlockedDomains) > 0 {
mapped.URLContext = &fetch
}
mapped.Google = appendUnique(mapped.Google, "url_context")
server(tool, "url_context")
case strings.HasPrefix(typeName, "code_execution_"):
if err := validateAnthropicServerTool(tool, "code_execution"); err != nil {
return aistudio.Tools{}, err
}
mapped.Google = appendUnique(mapped.Google, "code_execution")
server(tool, "code_execution")
case strings.HasPrefix(typeName, "tool_search_tool_"), strings.HasPrefix(typeName, "advisor_"), strings.Contains(typeName, "toolset"):
case strings.HasPrefix(typeName, "bash_"), strings.HasPrefix(typeName, "text_editor_"), strings.HasPrefix(typeName, "memory_"), strings.HasPrefix(typeName, "computer_"):
declaration, err := anthropicClientTool(tool, typeName)
Expand All @@ -786,12 +798,12 @@ func mapAnthropicTools(tools []anthropicTool, choice json.RawMessage) (aistudio.
if err := validateAnthropicServerTool(tool, "url_context"); err != nil {
return aistudio.Tools{}, err
}
mapped.Google = appendUnique(mapped.Google, "url_context")
server(tool, "url_context")
case typeName == "google_maps":
if err := validateAnthropicServerTool(tool, "google_maps"); err != nil {
return aistudio.Tools{}, err
}
mapped.Google = appendUnique(mapped.Google, "google_maps")
server(tool, "google_maps")
case typeName == "", typeName == "custom":
if tool.Name == "" {
return aistudio.Tools{}, fmt.Errorf("tool name is required")
Expand All @@ -813,9 +825,14 @@ func mapAnthropicTools(tools []anthropicTool, choice json.RawMessage) (aistudio.
return aistudio.Tools{}, fmt.Errorf("unsupported tool type %q", tool.Type)
}
}
config, err := anthropicToolChoice(choice)
if err != nil {
return aistudio.Tools{}, err
if chosen != "" {
config.AllowedFunctionNames, mapped.Functions, mapped.Google = nil, nil, []string{chosen}
if chosen != "google_search" {
mapped.GoogleSearch = nil
}
if chosen != "url_context" {
mapped.URLContext = nil
}
}
mapped.ToolConfig = config
return mapped, nil
Expand Down
45 changes: 45 additions & 0 deletions internal/api/anthropic_tools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package api

import (
"encoding/json"
"slices"
"testing"
)

// TestAnthropicServerToolChoice 核对 tool_choice 指定 server tool 时只开启该工具对应的 Google 工具与它的选项,指定客户端函数时仍只允许该函数
func TestAnthropicServerToolChoice(t *testing.T) {
const search = `{"type":"web_search_20250305","name":"web_search","max_uses":1}`
const tools = search + `,{"type":"web_fetch_20250910","name":"web_fetch","allowed_domains":["example.com"]},{"name":"Read","input_schema":{"type":"object"}}`
for _, test := range []struct {
name, tools, choice string
google, functions []string
allowed []string
search, fetch bool
}{
{"web_search", search, "web_search", []string{"google_search"}, nil, nil, true, false},
{"web_search with functions", tools, "web_search", []string{"google_search"}, nil, nil, true, false},
{"web_fetch with functions", tools, "web_fetch", []string{"url_context"}, nil, nil, false, true},
{"function", tools, "Read", []string{"google_search", "url_context"}, []string{"Read"}, []string{"Read"}, true, true},
} {
t.Run(test.name, func(t *testing.T) {
var request anthropicRequest
body := `{"model":"m","max_tokens":400,"messages":[{"role":"user","content":"x"}],"tools":[` + test.tools + `],"tool_choice":{"type":"tool","name":"` + test.choice + `"}}`
if err := json.Unmarshal([]byte(body), &request); err != nil {
t.Fatal(err)
}
got, err := request.toGenerateRequest("id")
if err != nil {
t.Fatal(err)
}
var functions []string
for _, declaration := range got.Tools.Functions {
functions = append(functions, declaration.Name)
}
config := got.Tools.ToolConfig
if config.Mode != "required" || !slices.Equal(config.AllowedFunctionNames, test.allowed) || !slices.Equal(got.Tools.Google, test.google) || !slices.Equal(functions, test.functions) ||
(got.Tools.GoogleSearch != nil) != test.search || (got.Tools.URLContext != nil) != test.fetch {
t.Fatalf("tools=%+v functions=%v", got.Tools, functions)
}
})
}
}