Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(stagehand): route Browser through shared model drivers (#21477)

Sbaaoui Idriss4e1da1550986
diff --git a/internal/agent/component/stagehand_model.go b/internal/agent/component/stagehand_model.go
new file mode 100644
index 0000000000..4433938f45
--- /dev/null
+++ b/internal/agent/component/stagehand_model.go
@@ -0,0 +1,166 @@
+package component
+
+import (
+ "context"
+ "crypto/hmac"
+ "crypto/rand"
+ "encoding/json/v2"
+ "fmt"
+ "net"
+ "net/http"
+ "time"
+
+ "ragflow/internal/entity/models"
+)
+
+// Stagehand speaks OpenAI on this private connection; the selected Go driver
+// owns the actual provider protocol, credentials, endpoints and response parsing.
+func startStagehandModelServer(ctx context.Context, model *models.ChatModel) (baseURL, token string, closeServer func(), err error) {
+ if model == nil || model.ModelDriver == nil {
+ return "", "", nil, fmt.Errorf("stagehand model: resolved chat model is required")
+ }
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ return "", "", nil, fmt.Errorf("stagehand model: listen: %w", err)
+ }
+ token = rand.Text()
+ mux := http.NewServeMux()
+ mux.HandleFunc("POST /v1/chat/completions", func(w http.ResponseWriter, request *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ fail := func(status int, err error) {
+ w.WriteHeader(status)
+ _ = json.MarshalWrite(w, map[string]any{"error": map[string]any{"message": err.Error(), "type": "model_error"}})
+ }
+ if !hmac.Equal([]byte(request.Header.Get("Authorization")), []byte("Bearer "+token)) {
+ fail(http.StatusUnauthorized, fmt.Errorf("invalid Stagehand model token"))
+ return
+ }
+ var input struct {
+ Messages []models.Message `json:"messages"`
+ Stream bool `json:"stream"`
+ Temperature *float64 `json:"temperature"`
+ TopP *float64 `json:"top_p"`
+ MaxTokens *int `json:"max_tokens"`
+ MaxCompletionTokens *int `json:"max_completion_tokens"`
+ Stop any `json:"stop"`
+ Tools []map[string]any `json:"tools"`
+ ToolChoice any `json:"tool_choice"`
+ ResponseFormat *struct {
+ Type string `json:"type"`
+ JSONSchema struct {
+ Schema map[string]any `json:"schema"`
+ } `json:"json_schema"`
+ } `json:"response_format"`
+ }
+ if err := json.UnmarshalRead(http.MaxBytesReader(w, request.Body, 64<<20), &input); err != nil {
+ fail(http.StatusBadRequest, err)
+ return
+ }
+ if input.Stream || len(input.Messages) == 0 {
+ fail(http.StatusBadRequest, fmt.Errorf("Stagehand model requires non-streaming messages"))
+ return
+ }
+ config := &models.ChatConfig{Temperature: input.Temperature, TopP: input.TopP, MaxTokens: input.MaxTokens}
+ if config.MaxTokens == nil {
+ config.MaxTokens = input.MaxCompletionTokens
+ }
+ if len(input.Tools) > 0 {
+ config.Tools = input.Tools
+ }
+ if choice, ok := input.ToolChoice.(string); ok {
+ config.ToolChoice = &choice
+ } else if input.ToolChoice != nil {
+ config.ToolChoice = new("required")
+ config.ToolChoiceValue = input.ToolChoice
+ }
+ if input.Stop != nil {
+ var stop []string
+ switch value := input.Stop.(type) {
+ case string:
+ stop = []string{value}
+ case []any:
+ for _, item := range value {
+ text, ok := item.(string)
+ if !ok {
+ fail(http.StatusBadRequest, fmt.Errorf("invalid stop sequence"))
+ return
+ }
+ stop = append(stop, text)
+ }
+ default:
+ fail(http.StatusBadRequest, fmt.Errorf("invalid stop sequence"))
+ return
+ }
+ config.Stop = &stop
+ }
+ structured := false
+ if format := input.ResponseFormat; format != nil && format.Type != "text" {
+ if len(input.Tools) > 0 {
+ fail(http.StatusBadRequest, fmt.Errorf("combined structured output and tools are unsupported"))
+ return
+ }
+ schema := format.JSONSchema.Schema
+ switch format.Type {
+ case "json_object":
+ schema = map[string]any{"type": "object"}
+ case "json_schema":
+ if len(schema) == 0 {
+ fail(http.StatusBadRequest, fmt.Errorf("structured output schema is required"))
+ return
+ }
+ default:
+ fail(http.StatusBadRequest, fmt.Errorf("unsupported response format %q", format.Type))
+ return
+ }
+ // Function tools are the shared provider contract for structured output.
+ config.Tools = []map[string]any{{"type": "function", "function": map[string]any{"name": "ragflow_structured_output", "description": "Return the requested structured result.", "parameters": schema}}}
+ config.ToolChoice = new("required")
+ config.ToolChoiceValue = map[string]any{"type": "function", "function": map[string]any{"name": "ragflow_structured_output"}}
+ structured = true
+ }
+ result, err := model.ChatWithMessages(request.Context(), input.Messages, config, nil)
+ if err != nil {
+ fail(http.StatusBadGateway, err)
+ return
+ }
+ if result == nil {
+ fail(http.StatusBadGateway, fmt.Errorf("provider returned no response"))
+ return
+ }
+ content := result.Answer
+ calls := result.ToolCalls
+ if structured {
+ if len(calls) != 1 {
+ fail(http.StatusBadGateway, fmt.Errorf("provider did not return the structured output tool"))
+ return
+ }
+ function, _ := calls[0]["function"].(map[string]any)
+ arguments, ok := function["arguments"].(string)
+ var value any
+ if !ok || function["name"] != "ragflow_structured_output" || json.Unmarshal([]byte(arguments), &value) != nil {
+ fail(http.StatusBadGateway, fmt.Errorf("provider returned invalid structured output"))
+ return
+ }
+ content = &arguments
+ calls = nil
+ }
+ message := map[string]any{"role": "assistant", "content": content}
+ finishReason := "stop"
+ if len(calls) > 0 {
+ message["tool_calls"] = calls
+ finishReason = "tool_calls"
+ }
+ if result.ReasonContent != nil {
+ message["reasoning_content"] = *result.ReasonContent
+ }
+ response := map[string]any{"id": "chatcmpl-stagehand", "object": "chat.completion", "created": time.Now().Unix(), "model": "ragflow", "choices": []any{map[string]any{"index": 0, "message": message, "finish_reason": finishReason}}}
+ if result.Usage != nil {
+ response["usage"] = result.Usage
+ }
+ _ = json.MarshalWrite(w, response)
+ })
+ server := &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second, BaseContext: func(net.Listener) context.Context { return ctx }}
+ go func() { _ = server.Serve(listener) }()
+ stop := context.AfterFunc(ctx, func() { _ = server.Close() })
+ return "http://" + listener.Addr().String() + "/v1", token, func() { stop(); _ = server.Close() }, nil
+}
diff --git a/internal/agent/component/stagehand_runtime.go b/internal/agent/component/stagehand_runtime.go
index 4e6a69852e..fbc44a05d5 100644
--- a/internal/agent/component/stagehand_runtime.go
+++ b/internal/agent/component/stagehand_runtime.go
@@ -64,6 +64,7 @@ import (
"fmt"
"math"
"ragflow/internal/common"
+ "ragflow/internal/entity/models"
"sort"
"strconv"
"strings"
@@ -82,6 +83,7 @@ import (
// lifecycle; the caller is responsible for resolving these from
// the tenant model config and the canvas state.
type RunTaskRequest struct {
+ ChatModel *models.ChatModel
// TenantID is the RAGFlow tenant identifier (used for logging /
// error context; not currently passed to stagehand).
TenantID string
@@ -95,6 +97,8 @@ type RunTaskRequest struct {
// passed via the per-call Model.BaseURL field; when empty, the
// stagehand server's default (api.openai.com) is used.
BaseURL string
+ // OpenAIChatCompletions selects Chat Completions for compatible providers.
+ OpenAIChatCompletions bool
// APIKey is the LLM provider key. Required by the stagehand
// server in local mode (MODEL_API_KEY env).
APIKey string
@@ -106,7 +110,8 @@ type RunTaskRequest struct {
MaxSteps int
// Headless controls `Sessions.Start.Browser.LaunchOptions.Headless`.
// Nil = server default (true).
- Headless *bool
+ Headless *bool
+ ChromiumSandbox bool
// DownloadsPath is forwarded to
// `Sessions.Start.Browser.LaunchOptions.DownloadsPath`. When
@@ -388,6 +393,18 @@ func (r *stagehandRuntime) RunTask(ctx context.Context, req RunTaskRequest) (str
if req.Instruction == "" {
return "", errors.New("stagehand runtime: Instruction is required")
}
+ cacheReq := req
+ if req.ChatModel != nil {
+ baseURL, token, closeServer, err := startStagehandModelServer(ctx, req.ChatModel)
+ if err != nil {
+ return "", err
+ }
+ defer closeServer()
+ // Cache subprocesses by tenant/model, not by ephemeral bridge credentials.
+ cacheReq.ModelName, cacheReq.APIKey, cacheReq.BaseURL = "openai/ragflow", "ragflow", ""
+ req.ModelName, req.APIKey, req.BaseURL = "openai/ragflow", token, baseURL
+ req.OpenAIChatCompletions = true
+ }
if req.ModelName == "" {
return "", errors.New("stagehand runtime: ModelName is required")
}
@@ -395,7 +412,7 @@ func (r *stagehandRuntime) RunTask(ctx context.Context, req RunTaskRequest) (str
return "", errors.New("stagehand runtime: APIKey is required (stagehand local mode requires MODEL_API_KEY)")
}
- client, release, err := r.leaseClient(req)
+ client, release, err := r.leaseClient(cacheReq)
if err != nil {
return "", fmt.Errorf("stagehand runtime: client: %w", err)
}
@@ -418,6 +435,9 @@ func (r *stagehandRuntime) RunTask(ctx context.Context, req RunTaskRequest) (str
launchOpts := stagehand.SessionStartParamsBrowserLaunchOptions{
Headless: stagehand.Bool(headless),
}
+ if !req.ChromiumSandbox {
+ launchOpts.Args = []string{"--no-sandbox"}
+ }
if req.DownloadsPath != "" {
launchOpts.DownloadsPath = stagehand.String(req.DownloadsPath)
}
@@ -454,19 +474,23 @@ func (r *stagehandRuntime) RunTask(ctx context.Context, req RunTaskRequest) (str
maxSteps = 30
}
- execResp, err := client.Sessions.Execute(ctx, sessionID, stagehand.SessionExecuteParams{
- AgentConfig: stagehand.SessionExecuteParamsAgentConfig{
- // GenericModelConfigObject carries ModelName + APIKey +
- // BaseURL on a per-call basis; the "openai/" prefix in
- // ModelName tells the stagehand server to dispatch via
- // its OpenAI-compatible provider (CHANGELOG #1025 added
- // custom BaseURL support). The union's
- // OfSessionExecutesAgentConfigModelGenericModelConfigObject
- // variant is the only path that exposes BaseURL.
- Model: stagehand.SessionExecuteParamsAgentConfigModelUnion{
- OfSessionExecutesAgentConfigModelGenericModelConfigObject: stagehandExecuteModelConfig(req),
- },
+ agentConfig := stagehand.SessionExecuteParamsAgentConfig{
+ // GenericModelConfigObject carries ModelName + APIKey +
+ // BaseURL on a per-call basis; the "openai/" prefix in
+ // ModelName tells the stagehand server to dispatch via
+ // its OpenAI-compatible provider (CHANGELOG #1025 added
+ // custom BaseURL support). The union's
+ // OfSessionExecutesAgentConfigModelGenericModelConfigObject
+ // variant is the only path that exposes BaseURL.
+ Model: stagehand.SessionExecuteParamsAgentConfigModelUnion{
+ OfSessionExecutesAgentConfigModelGenericModelConfigObject: stagehandExecuteModelConfig(req),
},
+ }
+ if req.ChatModel != nil {
+ agentConfig.Mode = "dom"
+ }
+ execResp, err := client.Sessions.Execute(ctx, sessionID, stagehand.SessionExecuteParams{
+ AgentConfig: agentConfig,
ExecuteOptions: stagehand.SessionExecuteParamsExecuteOptions{
Instruction: req.Instruction,
MaxSteps: stagehand.Float(float64(maxSteps)),
@@ -488,10 +512,15 @@ func (r *stagehandRuntime) RunTask(ctx context.Context, req RunTaskRequest) (str
}
func stagehandExecuteModelConfig(req RunTaskRequest) *stagehand.SessionExecuteParamsAgentConfigModelGenericModelConfigObject {
+ provider, _, _ := strings.Cut(req.ModelName, "/")
model := &stagehand.SessionExecuteParamsAgentConfigModelGenericModelConfigObject{
ModelName: req.ModelName,
APIKey: stagehand.String(req.APIKey),
- Provider: "openai",
+ Provider: provider,
+ }
+ if provider == "openai" && req.OpenAIChatCompletions {
+ // Compatible providers implement Chat Completions, not necessarily Responses.
+ model.SetExtraFields(map[string]any{"openaiEndpointFormat": "chat"})
}
if baseURL := strings.TrimSpace(req.BaseURL); baseURL != "" {
model.BaseURL = stagehand.String(baseURL)
@@ -517,15 +546,19 @@ func stagehandExecuteModelConfig(req RunTaskRequest) *stagehand.SessionExecutePa
// Use RunTask (Sessions.Execute) instead when the agent must
// discover the URL, navigate multiple pages, or chain actions.
type RunExtractRequest struct {
- TenantID string
- LLMID string
- ModelName string // "openai/<model>"
- BaseURL string
- APIKey string
- Headless *bool
- URL string // optional: when set, Sessions.Navigate to this URL first
- Instruction string // natural-language extraction prompt
- Schema map[string]any // JSON schema; Data.Result is the structured data
+ TenantID string
+ LLMID string
+ ModelName string // "openai/<model>"
+ BaseURL string
+ OpenAIChatCompletions bool
+ APIKey string
+ Headless *bool
+ // ChromiumSandbox controls whether Chromium's sandbox is enabled.
+ // False (the zero value) adds --no-sandbox to support root containers.
+ ChromiumSandbox bool
+ URL string // optional: when set, Sessions.Navigate to this URL first
+ Instruction string // natural-language extraction prompt
+ Schema map[string]any // JSON schema; Data.Result is the structured data
}
// RunExtract implements the "structured single-shot" path against
@@ -576,6 +609,9 @@ func (r *stagehandRuntime) RunExtract(ctx context.Context, req RunExtractRequest
launchOpts := stagehand.SessionStartParamsBrowserLaunchOptions{
Headless: stagehand.Bool(headless),
}
+ if !req.ChromiumSandbox {
+ launchOpts.Args = []string{"--no-sandbox"}
+ }
if req.URL != "" {
// Note: BrowserLaunchOptions doesn't carry DownloadsPath /
// UserDataDir here; RunExtract is single-shot extraction
@@ -634,10 +670,15 @@ func (r *stagehandRuntime) RunExtract(ctx context.Context, req RunExtractRequest
}
func stagehandExtractModelConfig(req RunExtractRequest) stagehand.SessionExtractParamsOptionsModelUnion {
+ provider, _, _ := strings.Cut(req.ModelName, "/")
model := &stagehand.SessionExtractParamsOptionsModelGenericModelConfigObject{
ModelName: req.ModelName,
APIKey: stagehand.String(req.APIKey),
- Provider: "openai",
+ Provider: provider,
+ }
+ if provider == "openai" && req.OpenAIChatCompletions {
+ // The server supports this field; the pinned Go SDK has no typed field yet.
+ model.SetExtraFields(map[string]any{"openaiEndpointFormat": "chat"})
}
if baseURL := strings.TrimSpace(req.BaseURL); baseURL != "" {
model.BaseURL = stagehand.String(baseURL)
@@ -681,6 +722,9 @@ func (r *stagehandRuntime) leaseClient(req RunTaskRequest) (stagehand.Client, fu
return stagehand.Client{}, nil, errors.New("stagehand runtime: APIKey is required")
}
key := req.BaseURL + "|" + req.APIKey + "|" + req.ModelName
+ if req.ChatModel != nil {
+ key = fmt.Sprintf("resolved:%q:%q", req.TenantID, req.LLMID)
+ }
r.mu.Lock()
if r.closed {
r.mu.Unlock()
diff --git a/internal/agent/component/stagehand_runtime_integration_test.go b/internal/agent/component/stagehand_runtime_integration_test.go
index 5ce789cfcc..4e171eb60b 100644
--- a/internal/agent/component/stagehand_runtime_integration_test.go
+++ b/internal/agent/component/stagehand_runtime_integration_test.go
@@ -52,6 +52,7 @@ import (
"os"
"path/filepath"
"ragflow/internal/common"
+ "strings"
"testing"
"time"
@@ -182,13 +183,8 @@ func cacheDirGuess() string {
return filepath.Join(home, ".cache", "stagehand", "lib")
}
-// TestBrowser_E2E_Extract exercises browser.go's Invoke pipeline
-// end-to-end against a real stagehand-server-v3 subprocess, a real
-// OpenAI-compatible LLM, and a local httptest server. The component
-// navigates to a local page and extracts the page content via
-// Sessions.Extract with a {"type": "string"} schema.
-//
-// Skipped unless OPENAI_* env vars are configured.
+// TestBrowser_E2E_Extract exercises Browser's extraction against a local page.
+// Requires OPENAI_* credentials and Chrome.
func TestBrowser_E2E_Extract(t *testing.T) {
apiKey := common.GetEnv(common.EnvOpenAIAPIKey)
baseURL := common.GetEnv(common.EnvOpenAIBaseURL)
@@ -210,12 +206,14 @@ func TestBrowser_E2E_Extract(t *testing.T) {
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
- // Override tenant LLM lookup so the test doesn't need a real DB.
- prevLookup := browserLLMLookupForTest
- browserLLMLookupForTest = func(_ context.Context, _ *gorm.DB, _, _ string) (string, string, string, string, error) {
+ previousLookup := browserLLMLookupForTest
+ browserLLMLookupForTest = func(_ context.Context, _ *gorm.DB, tenantID, modelRef string) (string, string, string, string, error) {
+ if tenantID != "tenant-1" {
+ return "", "", "", "", fmt.Errorf("unexpected tenant %q", tenantID)
+ }
return "OpenAI", model, apiKey, baseURL, nil
}
- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })
+ t.Cleanup(func() { browserLLMLookupForTest = previousLookup })
// --- use production stagehand runtime ---
r := newStagehandRuntimeFromEnv()
@@ -242,24 +240,18 @@ func TestBrowser_E2E_Extract(t *testing.T) {
ctx := canvas.WithState(t.Context(), canvas.NewCanvasState("run-1", "task-1"))
state, _ := runtime.GetStateFromContext(ctx)
- state.Sys["user_id"] = "tenant-1"
+ state.Sys["tenant_id"] = "tenant-1"
invokeCtx, cancel := context.WithTimeout(ctx, 3*time.Minute)
defer cancel()
t.Logf("starting browser.Invoke (RunExtract) against %s (timeout 3m)", srv.URL)
out, err := c.Invoke(invokeCtx, nil, nil)
if err != nil {
- t.Logf("extraction failed (best-effort): %v", err)
- return
- }
- if out == nil {
- t.Logf("Invoke returned nil output (best-effort pass)")
- return
+ t.Fatalf("Invoke: %v", err)
}
-
- if content, ok := out["content"].(string); ok && content != "" {
- t.Logf("extracted content: %s", content)
- } else {
- t.Logf("extracted content is empty or missing (LLM-dependent)")
+ content, ok := out["content"].(string)
+ if !ok || strings.TrimSpace(content) == "" || content == "null" {
+ t.Fatalf("missing browser result: %#v", out)
}
+ t.Logf("extracted content: %s", content)
}
diff --git a/internal/entity/models/anthropic.go b/internal/entity/models/anthropic.go
index c1c6f3ba48..22ab46b7aa 100644
--- a/internal/entity/models/anthropic.go
+++ b/internal/entity/models/anthropic.go
@@ -95,7 +95,9 @@ func (a *AnthropicModel) ChatWithMessages(ctx context.Context, modelName string,
if systemPrompt != "" {
reqBody["system"] = systemPrompt
}
- applyAnthropicChatConfig(reqBody, chatModelConfig)
+ if err := applyAnthropicChatConfig(reqBody, chatModelConfig); err != nil {
+ return nil, err
+ }
ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)
defer cancel()
@@ -124,9 +126,10 @@ func (a *AnthropicModel) ChatWithMessages(ctx context.Context, modelName string,
return parseChatCompletionResponse(body, chatModelConfig, modelUsage, parseAnthropicChatResponse)
}
-func applyAnthropicChatConfig(reqBody map[string]interface{}, chatModelConfig *ChatConfig) {
+func applyAnthropicChatConfig(reqBody map[string]interface{}, chatModelConfig *ChatConfig) error {
+ reqBody["max_tokens"] = 1024
if chatModelConfig == nil {
- return
+ return nil
}
if chatModelConfig.MaxTokens != nil {
reqBody["max_tokens"] = *chatModelConfig.MaxTokens
@@ -142,6 +145,59 @@ func applyAnthropicChatConfig(reqBody map[string]interface{}, chatModelConfig *C
if chatModelConfig.Stop != nil {
reqBody["stop_sequences"] = *chatModelConfig.Stop
}
+ var tools []map[string]any
+ for _, raw := range normalizeToolList(chatModelConfig.Tools) {
+ tool, ok := raw.(map[string]any)
+ if !ok {
+ return fmt.Errorf("anthropic: invalid tool definition")
+ }
+ function, ok := tool["function"].(map[string]any)
+ if !ok {
+ function = tool
+ }
+ name, _ := function["name"].(string)
+ if name == "" {
+ return fmt.Errorf("anthropic: tool name is required")
+ }
+ inputSchema := map[string]any{"type": "object", "properties": map[string]any{}}
+ if parameters, present := function["parameters"]; present {
+ inputSchema, ok = parameters.(map[string]any)
+ if !ok || inputSchema == nil {
+ return fmt.Errorf("anthropic: tool %q parameters must be an object", name)
+ }
+ }
+ nativeTool := map[string]any{"name": name, "input_schema": inputSchema}
+ if description, ok := function["description"].(string); ok {
+ nativeTool["description"] = description
+ }
+ tools = append(tools, nativeTool)
+ }
+ if len(tools) > 0 {
+ reqBody["tools"] = tools
+ }
+ if choice := chatModelConfig.ToolChoiceValue; choice != nil {
+ value, ok := choice.(map[string]any)
+ if !ok {
+ return fmt.Errorf("anthropic: invalid tool choice")
+ }
+ function, _ := value["function"].(map[string]any)
+ name, _ := function["name"].(string)
+ if name == "" {
+ return fmt.Errorf("anthropic: tool choice name is required")
+ }
+ reqBody["tool_choice"] = map[string]any{"type": "tool", "name": name}
+ } else if choice := chatModelConfig.ToolChoice; choice != nil {
+ value := *choice
+ switch value {
+ case "required":
+ value = "any"
+ case "auto", "none":
+ default:
+ return fmt.Errorf("anthropic: unsupported tool choice %q", value)
+ }
+ reqBody["tool_choice"] = map[string]any{"type": value}
+ }
+ return nil
}
func setAnthropicHeaders(req *http.Request, apiKey string, streaming bool) {
@@ -160,6 +216,51 @@ func anthropicMessages(messages []Message) ([]map[string]interface{}, string, er
systemPrompts := make([]string, 0)
for _, msg := range messages {
role := strings.ToLower(strings.TrimSpace(msg.Role))
+ if role == "tool" {
+ if msg.ToolCallID == "" {
+ return nil, "", fmt.Errorf("anthropic: tool result requires tool_call_id")
+ }
+ content, err := anthropicContent(msg.Content)
+ if err != nil {
+ return nil, "", err
+ }
+ block := map[string]any{"type": "tool_result", "tool_use_id": msg.ToolCallID, "content": content}
+ if len(apiMessages) > 0 && apiMessages[len(apiMessages)-1]["role"] == "user" {
+ if blocks, ok := apiMessages[len(apiMessages)-1]["content"].([]map[string]any); ok {
+ apiMessages[len(apiMessages)-1]["content"] = append(blocks, block)
+ continue
+ }
+ }
+ apiMessages = append(apiMessages, map[string]any{"role": "user", "content": []map[string]any{block}})
+ continue
+ }
+ if role == "assistant" && len(msg.ToolCalls) > 0 {
+ var blocks []map[string]any
+ if msg.Content != nil && msg.Content != "" {
+ content, err := anthropicContent(msg.Content)
+ if err != nil {
+ return nil, "", err
+ }
+ if text, ok := content.(string); ok {
+ blocks = append(blocks, map[string]any{"type": "text", "text": text})
+ } else {
+ blocks = content.([]map[string]any)
+ }
+ }
+ for _, call := range msg.ToolCalls {
+ function, _ := call["function"].(map[string]any)
+ name, _ := function["name"].(string)
+ id, _ := call["id"].(string)
+ arguments, _ := function["arguments"].(string)
+ var input map[string]any
+ if id == "" || name == "" || json.Unmarshal([]byte(arguments), &input) != nil || input == nil {
+ return nil, "", fmt.Errorf("anthropic: invalid assistant tool call")
+ }
+ blocks = append(blocks, map[string]any{"type": "tool_use", "id": id, "name": name, "input": input})
+ }
+ apiMessages = append(apiMessages, map[string]any{"role": role, "content": blocks})
+ continue
+ }
content, err := anthropicContent(msg.Content)
if err != nil {
return nil, "", err
@@ -336,9 +437,12 @@ func parseAnthropicChatResponse(body []byte, _ *ChatConfig) (chatResponseParts,
var result struct {
ID string `json:"id"`
Content []struct {
- Type string `json:"type"`
- Text string `json:"text"`
- Thinking string `json:"thinking"`
+ Type string `json:"type"`
+ Text string `json:"text"`
+ Thinking string `json:"thinking"`
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Input map[string]any `json:"input"`
} `json:"content"`
Usage struct {
InputTokens int `json:"input_tokens"`
@@ -354,15 +458,25 @@ func parseAnthropicChatResponse(body []byte, _ *ChatConfig) (chatResponseParts,
var answer strings.Builder
var reasoning strings.Builder
+ var toolCalls []map[string]any
for _, block := range result.Content {
switch block.Type {
case "text":
answer.WriteString(block.Text)
case "thinking":
reasoning.WriteString(block.Thinking)
+ case "tool_use":
+ if block.ID == "" || block.Name == "" || block.Input == nil {
+ return chatResponseParts{}, fmt.Errorf("invalid tool_use in Anthropic response")
+ }
+ arguments, err := json.Marshal(block.Input)
+ if err != nil {
+ return chatResponseParts{}, fmt.Errorf("anthropic: marshal tool input: %w", err)
+ }
+ toolCalls = append(toolCalls, map[string]any{"id": block.ID, "type": "function", "function": map[string]any{"name": block.Name, "arguments": string(arguments)}})
}
}
- if answer.Len() == 0 {
+ if answer.Len() == 0 && len(toolCalls) == 0 {
return chatResponseParts{}, fmt.Errorf("no text content in Anthropic response")
}
@@ -381,6 +495,7 @@ func parseAnthropicChatResponse(body []byte, _ *ChatConfig) (chatResponseParts,
RequestID: result.ID,
Content: &ans,
ReasonContent: &reason,
+ ToolCalls: toolCalls,
Usage: usage,
}, nil
}
@@ -448,6 +563,9 @@ func (a *AnthropicModel) CheckConnection(ctx context.Context, apiConfig *APIConf
}
func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ if modelConfig != nil {
+ modelConfig.ToolCallsResult = nil
+ }
if err := a.baseModel.APIConfigCheck(apiConfig); err != nil {
return err
}
@@ -478,7 +596,9 @@ func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName s
if systemPrompt != "" {
reqBody["system"] = systemPrompt
}
- applyAnthropicChatConfig(reqBody, modelConfig)
+ if err := applyAnthropicChatConfig(reqBody, modelConfig); err != nil {
+ return err
+ }
jsonData, err := json.Marshal(reqBody)
if err != nil {
@@ -508,9 +628,29 @@ func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName s
sawTerminal := false
var streamUsage TokenUsage
sawUsage := false
+ type toolBlock struct {
+ id, name string
+ input map[string]any
+ partial strings.Builder
+ }
+ pendingTools := make(map[float64]*toolBlock)
+ toolCalls := make(map[int]map[string]any)
done, err := ParseSSEStream[map[string]interface{}](resp.Body, func(event map[string]interface{}) error {
eventType, _ := event["type"].(string)
switch eventType {
+ case "content_block_start":
+ block, _ := event["content_block"].(map[string]any)
+ if block["type"] != "tool_use" {
+ return nil
+ }
+ index, ok := event["index"].(float64)
+ id, _ := block["id"].(string)
+ name, _ := block["name"].(string)
+ input, _ := block["input"].(map[string]any)
+ if !ok || index < 0 || index != float64(int(index)) || id == "" || name == "" || input == nil {
+ return fmt.Errorf("anthropic: invalid streamed tool_use block")
+ }
+ pendingTools[index] = &toolBlock{id: id, name: name, input: input}
case "content_block_delta":
delta, ok := event["delta"].(map[string]interface{})
if !ok {
@@ -518,6 +658,14 @@ func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName s
}
deltaType, _ := delta["type"].(string)
switch deltaType {
+ case "input_json_delta":
+ index, ok := event["index"].(float64)
+ tool := pendingTools[index]
+ partial, valid := delta["partial_json"].(string)
+ if !ok || tool == nil || !valid {
+ return fmt.Errorf("anthropic: invalid streamed tool input delta")
+ }
+ tool.partial.WriteString(partial)
case "text_delta":
if text, ok := delta["text"].(string); ok && text != "" {
if err := sender(&text, nil); err != nil {
@@ -531,6 +679,27 @@ func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName s
}
}
}
+ case "content_block_stop":
+ index, ok := event["index"].(float64)
+ if tool := pendingTools[index]; ok && tool != nil {
+ if tool.partial.Len() > 0 {
+ if err := json.Unmarshal([]byte(tool.partial.String()), &tool.input); err != nil {
+ return fmt.Errorf("anthropic: invalid streamed tool input: %w", err)
+ }
+ }
+ if tool.input == nil {
+ return fmt.Errorf("anthropic: streamed tool input must be an object")
+ }
+ arguments, err := json.Marshal(tool.input)
+ if err != nil {
+ return fmt.Errorf("anthropic: marshal streamed tool input: %w", err)
+ }
+ toolCalls[int(index)] = map[string]any{
+ "id": tool.id, "type": "function",
+ "function": map[string]any{"name": tool.name, "arguments": string(arguments)},
+ }
+ delete(pendingTools, index)
+ }
case "message_start":
message, ok := event["message"].(map[string]interface{})
if !ok {
@@ -567,6 +736,10 @@ func (a *AnthropicModel) ChatStreamlyWithSender(ctx context.Context, modelName s
if !done && !sawTerminal {
return fmt.Errorf("anthropic: stream ended before message_stop")
}
+ if len(pendingTools) > 0 {
+ return fmt.Errorf("anthropic: stream ended before tool content_block_stop")
+ }
+ setSortedToolCallsResult(modelConfig, toolCalls)
if sawUsage {
streamUsage.TotalTokens = streamUsage.PromptTokens + streamUsage.CompletionTokens
diff --git a/internal/entity/models/anthropic_test.go b/internal/entity/models/anthropic_test.go
index ff1c420f2b..1ac2f89d8d 100644
--- a/internal/entity/models/anthropic_test.go
+++ b/internal/entity/models/anthropic_test.go
@@ -60,6 +60,47 @@ func TestAnthropicName(t *testing.T) {
}
}
+func TestAnthropicToolOptionalFields(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ function map[string]any
+ want string
+ wantErr bool
+ }{
+ {"omitted parameters and description", map[string]any{"name": "clock"}, `{"input_schema":{"properties":{},"type":"object"},"name":"clock"}`, false},
+ {"string description", map[string]any{"name": "clock", "parameters": map[string]any{"type": "object"}, "description": "Read clock"}, `{"description":"Read clock","input_schema":{"type":"object"},"name":"clock"}`, false},
+ {"non-string description", map[string]any{"name": "clock", "parameters": map[string]any{"type": "object"}, "description": 42}, `{"input_schema":{"type":"object"},"name":"clock"}`, false},
+ {"null description", map[string]any{"name": "clock", "parameters": map[string]any{"type": "object"}, "description": nil}, `{"input_schema":{"type":"object"},"name":"clock"}`, false},
+ {"null parameters", map[string]any{"name": "clock", "parameters": nil}, "", true},
+ {"non-object parameters", map[string]any{"name": "clock", "parameters": "invalid"}, "", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ body := make(map[string]any)
+ err := applyAnthropicChatConfig(body, &ChatConfig{Tools: []any{map[string]any{"type": "function", "function": tc.function}}})
+ if tc.wantErr {
+ if err == nil {
+ t.Fatal("expected invalid parameters to be rejected")
+ }
+ return
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ tools, ok := body["tools"].([]map[string]any)
+ if !ok || len(tools) != 1 {
+ t.Fatalf("tools = %#v, want one tool", body["tools"])
+ }
+ got, err := json.Marshal(tools[0])
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != tc.want {
+ t.Errorf("tool = %s, want %s", got, tc.want)
+ }
+ })
+ }
+}
+
func TestAnthropicChatHappyPath(t *testing.T) {
withSSRFBypass(t)
srv := newAnthropicServer(t, "/v1/messages", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
@@ -252,7 +293,7 @@ func TestAnthropicChatValidationErrors(t *testing.T) {
if _, err := m.ChatWithMessages(ctx, "claude", nil, &APIConfig{ApiKey: &apiKey}, nil, nil); err == nil || !strings.Contains(err.Error(), "messages is empty") {
t.Errorf("empty messages: got %v", err)
}
- if _, err := m.ChatWithMessages(ctx, "claude", []Message{{Role: "tool", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil); err == nil || !strings.Contains(err.Error(), "unsupported message role") {
+ if _, err := m.ChatWithMessages(ctx, "claude", []Message{{Role: "developer", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil); err == nil || !strings.Contains(err.Error(), "unsupported message role") {
t.Errorf("bad role: got %v", err)
}
if _, err := m.ChatWithMessages(ctx, "claude", []Message{{Role: "user", Content: []interface{}{map[string]interface{}{"type": "video_url"}}}}, &APIConfig{ApiKey: &apiKey}, nil, nil); err == nil || !strings.Contains(err.Error(), "unsupported content block type") {
@@ -292,8 +333,8 @@ func TestAnthropicChatRejectsMalformedResponse(t *testing.T) {
apiKey := "test-key"
_, err := newAnthropicForTest(srv.URL).ChatWithMessages(ctx, "claude", []Message{{Role: "user", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil)
- if err == nil || !strings.Contains(err.Error(), "no text content") {
- t.Errorf("expected no-text error, got %v", err)
+ if err == nil || !strings.Contains(err.Error(), "invalid tool_use") {
+ t.Errorf("expected invalid tool_use error, got %v", err)
}
}
@@ -370,6 +411,113 @@ func TestAnthropicChatStreamlyWithSenderHappyPath(t *testing.T) {
}
}
+func TestAnthropicStreamToolCalls(t *testing.T) {
+ withSSRFBypass(t)
+ for _, tc := range []struct {
+ name string
+ events []string
+ want []map[string]interface{}
+ }{
+ {
+ name: "fragmented arguments and multiple tools",
+ events: []string{
+ `{"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"call_1","name":"weather","input":{}}}`,
+ `{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"city\":\"San "}}`,
+ `{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"Francisco\",\"days\":2}"}}`,
+ `{"type":"content_block_stop","index":1}`,
+ `{"type":"content_block_start","index":2,"content_block":{"type":"tool_use","id":"call_2","name":"clock","input":{}}}`,
+ `{"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"{\"zone\":\"UTC\"}"}}`,
+ `{"type":"content_block_stop","index":2}`,
+ },
+ want: []map[string]interface{}{
+ {"id": "call_1", "type": "function", "function": map[string]interface{}{"name": "weather", "arguments": `{"city":"San Francisco","days":2}`}},
+ {"id": "call_2", "type": "function", "function": map[string]interface{}{"name": "clock", "arguments": `{"zone":"UTC"}`}},
+ },
+ },
+ {
+ name: "empty arguments",
+ events: []string{
+ `{"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"call_1","name":"clock","input":{}}}`,
+ `{"type":"content_block_stop","index":1}`,
+ },
+ want: []map[string]interface{}{
+ {"id": "call_1", "type": "function", "function": map[string]interface{}{"name": "clock", "arguments": `{}`}},
+ },
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ srv := newAnthropicServer(t, "/v1/messages", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ for _, event := range append(tc.events, `{"type":"message_delta","delta":{"stop_reason":"tool_use"}}`, `{"type":"message_stop"}`) {
+ _, _ = io.WriteString(w, "data: "+event+"\n\n")
+ }
+ })
+ defer srv.Close()
+ apiKey := "test-key"
+ config := &ChatConfig{}
+ sawDone := false
+ err := newAnthropicForTest(srv.URL).ChatStreamlyWithSender(t.Context(), "claude", []Message{{Role: "user", Content: "use tools"}}, &APIConfig{ApiKey: &apiKey}, config, nil, func(text, _ *string) error {
+ if text != nil && *text == "[DONE]" {
+ sawDone = true
+ if config.ToolCallsResult == nil {
+ t.Error("tool calls missing when stream completes")
+ }
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !sawDone || config.ToolCallsResult == nil {
+ t.Fatal("stream did not complete with tool calls")
+ }
+ got, _ := json.Marshal(*config.ToolCallsResult)
+ want, _ := json.Marshal(tc.want)
+ if string(got) != string(want) {
+ t.Errorf("tool calls = %s, want %s", got, want)
+ }
+ })
+ }
+}
+
+func TestAnthropicStreamRejectsInvalidToolInput(t *testing.T) {
+ withSSRFBypass(t)
+ for _, tc := range []struct {
+ name string
+ partial string
+ stop bool
+ }{
+ {"malformed JSON", `{`, true},
+ {"non-object JSON", `[]`, true},
+ {"null JSON", `null`, true},
+ {"unfinished tool block", `{}`, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ srv := newAnthropicServer(t, "/v1/messages", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ _, _ = io.WriteString(w, "data: "+`{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"call_1","name":"weather","input":{}}}`+"\n\n")
+ fragment, _ := json.Marshal(map[string]interface{}{"type": "content_block_delta", "index": 0, "delta": map[string]interface{}{"type": "input_json_delta", "partial_json": tc.partial}})
+ _, _ = io.WriteString(w, "data: "+string(fragment)+"\n\n")
+ if tc.stop {
+ _, _ = io.WriteString(w, "data: "+`{"type":"content_block_stop","index":0}`+"\n\n")
+ }
+ _, _ = io.WriteString(w, "data: "+`{"type":"message_stop"}`+"\n\n")
+ })
+ defer srv.Close()
+ apiKey := "test-key"
+ config := &ChatConfig{}
+ sawDone := false
+ err := newAnthropicForTest(srv.URL).ChatStreamlyWithSender(t.Context(), "claude", []Message{{Role: "user", Content: "use tools"}}, &APIConfig{ApiKey: &apiKey}, config, nil, func(text, _ *string) error {
+ sawDone = text != nil && *text == "[DONE]"
+ return nil
+ })
+ if err == nil || sawDone || config.ToolCallsResult != nil {
+ t.Fatalf("invalid tool input: err=%v, done=%v, calls=%v", err, sawDone, config.ToolCallsResult)
+ }
+ })
+ }
+}
+
func TestAnthropicChatStreamlyWithSenderRejectsHTTPError(t *testing.T) {
withSSRFBypass(t)
srv := newAnthropicServer(t, "/v1/messages", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {