RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(browser): use tenant model factory for all providers (#21478)
Sbaaoui Idriss3a05577c6b80
diff --git a/internal/agent/component/browser.go b/internal/agent/component/browser.goindex 6c8f023d1f..6f5dcd4086 100644--- a/internal/agent/component/browser.go+++ b/internal/agent/component/browser.go@@ -14,34 +14,14 @@// limitations under the License.//-// Package component — Browser (T3).-//-// Browser is an LLM-driven single-shot web extraction canvas node-// built on `github.com/browserbase/stagehand-go/v3` in local mode.-// It uses `RunExtract` (not the multi-step agent `RunTask`) to-// navigate to a page and extract structured content against a-// `{"type": "string"}` JSON schema.-//-// It mirrors the Python `agent/component/browser.py` param surface-// (`llm_id`, `prompts`, `max_steps`, `headless`, `persist_session`,-// `upload_sources`, etc.) so the v1 fixture-// (`internal/agent/dsl/testdata/browser.json`) loads without-// fixture-side changes.-//-// LLM dispatch is delegated to `StagehandInvoker` (see-// `stagehand_runtime.go`), which owns the stagehand-server child-// process and the session lifecycle. The component itself is a thin-// orchestrator: parse → resolve template → look up tenant model-// config → call runtime.RunExtract → emit Python-shaped outputs.-//-// File upload / download and persistent session management are-// not supported; see [`.claude/plans/tingly-weaving-orbit.md`]-// for the full deferral list.+// Browser runs natural-language browser tasks through Stagehand in local mode.+// StagehandInvoker.RunTask navigates and interacts with pages before returning+// the agent's final result. The runtime owns the server and session lifecycle.+// File upload/download and persistent sessions are not supported.package componentimport ("context"- "encoding/json""errors""fmt""net/url"@@ -49,49 +29,18 @@ import ("strings""ragflow/internal/agent/runtime"- "ragflow/internal/dao"- "ragflow/internal/entity"+ "ragflow/internal/entity/models""gorm.io/gorm")const componentNameBrowser = "Browser"-var stagehandNativeProviders = map[string]string{- "anthropic": "anthropic",- "google": "google",- "openai": "openai",-}+var browserModelFactory func(context.Context, string, string) (*models.ChatModel, error)-var browserFactoryDefaultBaseURL = map[string]string{- "302.ai": "https://api.302.ai/v1",- "anthropic": "https://api.anthropic.com/",- "astraflow": "https://api-us-ca.umodelverse.ai/v1",- "astraflow-cn": "https://api.modelverse.cn/v1",- "avian": "https://api.avian.io/v1",- "cometapi": "https://api.cometapi.com/v1",- "dashscope": "https://dashscope.aliyuncs.com/compatible-mode/v1",- "deepseek": "https://api.deepseek.com/v1",- "deerapi": "https://api.deerapi.com/v1",- "futurmix": "https://futurmix.ai/v1",- "giteeai": "https://ai.gitee.com/v1/",- "hunyuan": "https://api.hunyuan.cloud.tencent.com/v1",- "jiekouai": "https://api.jiekou.ai/openai",- "lingyi-ai": "https://api.lingyiwanwu.com/v1",- "longcat": "https://api.longcat.chat/openai",- "minimax": "https://api.minimaxi.com/v1",- "moonshot": "https://api.moonshot.cn/v1",- "n1n": "https://api.n1n.ai/v1",- "novitaai": "https://api.novita.ai/v3/openai",- "openai": "https://api.openai.com/v1",- "openrouter": "https://openrouter.ai/api/v1",- "perfxcloud": "https://cloud.perfxlab.cn/v1",- "ppio": "https://api.ppinfra.com/v3/openai",- "siliconflow": "https://api.siliconflow.cn/v1",- "stepfun": "https://api.stepfun.com/v1",- "tongyi-qianwen": "https://dashscope.aliyuncs.com/compatible-mode/v1",- "upstage": "https://api.upstage.ai/v1/solar",- "zhipu-ai": "https://open.bigmodel.cn/api/paas/v4",+// SetBrowserModelFactory binds Browser to the shared tenant-scoped model factory.+func SetBrowserModelFactory(factory func(context.Context, string, string) (*models.ChatModel, error)) {+ browserModelFactory = factory}// browserParam is the static DSL param surface for the Browser@@ -243,24 +192,8 @@ func NewBrowserComponent(params map[string]any) (Component, error) {// Name returns the registered component name.func (b *BrowserComponent) Name() string { return b.name }-// Invoke dispatches a single-shot extraction task via-// StagehandInvoker.RunExtract with a `{"type": "string"}` schema.-// The flow:-//-// 1. Pull tenant_id from `state.Sys["tenant_id"]`.-// 2. Resolve the `prompts` template via `runtime.ResolveTemplate`.-// 3. Resolve `llm_id` as a tenant_model id or legacy model@factory-// reference and look up the tenant LLM config (apiKey, baseURL).-// 4. Build `RunExtractRequest` with `ModelName = "openai/<model>"`,-// the resolved apiKey/baseURL/instruction, and-// `Schema = {"type": "string"}`.-// 5. Call `DefaultRuntime.RunExtract` → raw JSON string.-// 6. Unmarshal the JSON string to get the plain text content.-// 7. Emit the Python-shaped outputs (`content`,-// `downloaded_files`) plus Go-native compat keys.-//-// File upload/download and session persistence are not supported-// in this component; they are v1-deferred.+// Invoke runs the resolved browser instruction through Stagehand's agent so+// navigation and interaction happen before the final result is returned.func (b *BrowserComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map[string]any) (map[string]any, error) {state, err := runtime.GetStateFromContext(ctx)if err != nil {@@ -285,40 +218,45 @@ func (b *BrowserComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map[sreturn nil, fmt.Errorf("browser: resolve prompts template: %w", err)}- // 2. Look up tenant model config.- providerName, modelName, apiKey, baseURL, err := resolveBrowserLLM(ctx, db, tenantID, b.param.LLMID)- if err != nil {- return nil, fmt.Errorf("browser: tenant llm lookup (%q): %w", b.param.LLMID, err)+ if browserModelFactory == nil {+ return nil, errors.New("browser: model factory is not configured")}- baseURL = strings.TrimSpace(baseURL)-- // 3. Build RunExtractRequest with single-string schema.- req := RunExtractRequest{- TenantID: tenantID,- LLMID: b.param.LLMID,- ModelName: stagehandModelName(providerName, modelName),- BaseURL: baseURL,- APIKey: apiKey,- Headless: b.param.Headless,- Instruction: resolvedPrompts,- Schema: map[string]any{"type": "string"},+ chatModel, err := browserModelFactory(ctx, tenantID, b.param.LLMID)+ if err != nil {+ return nil, fmt.Errorf("browser: resolve model (%q): %w", b.param.LLMID, err)+ }+ if chatModel == nil {+ return nil, errors.New("browser: model factory returned nil model")+ }+ req := RunTaskRequest{+ TenantID: tenantID,+ LLMID: b.param.LLMID,+ ChatModel: chatModel,+ Headless: b.param.Headless,+ ChromiumSandbox: b.param.ChromiumSandbox != nil && *b.param.ChromiumSandbox,+ Instruction: resolvedPrompts,+ MaxSteps: b.param.MaxSteps,+ }+ if chatModel.ModelName != nil {+ req.ModelName = *chatModel.ModelName+ }+ if chatModel.APIConfig != nil {+ if chatModel.APIConfig.ApiKey != nil {+ req.APIKey = *chatModel.APIConfig.ApiKey+ }+ if chatModel.APIConfig.BaseURL != nil {+ req.BaseURL = *chatModel.APIConfig.BaseURL+ }}- // 4. Dispatch via the runtime's RunExtract.invoker := getDefaultStagehandInvoker()- rawJSON, err := invoker.RunExtract(ctx, req)+ content, err := invoker.RunTask(ctx, req)if err != nil {- return nil, fmt.Errorf("browser: stagehand extract (model=%q, base_url=%s): %w",+ return nil, fmt.Errorf("browser: stagehand task (model=%q, base_url=%s): %w",req.ModelName, browserBaseURLForLog(req.BaseURL), err)}- // 5. Unmarshal the JSON-string result to get the plain text.- var content string- if err = json.Unmarshal([]byte(rawJSON), &content); err != nil {- return nil, fmt.Errorf("browser: unmarshal extract result: %w", err)- }-- // 6. Build the output map.+ // 4. Build the output map.out := map[string]any{"content": content,"downloaded_files": []map[string]any{},@@ -344,26 +282,7 @@ func browserBaseURLForLog(raw string) string {return u.String()}-func stagehandModelName(providerName, modelName string) string {- modelName = strings.TrimSpace(modelName)- if modelName == "" {- return ""- }- if strings.Contains(modelName, "/") {- prefix, _, _ := strings.Cut(modelName, "/")- if stagehandNativeProviders[strings.ToLower(strings.TrimSpace(prefix))] != "" {- return modelName- }- return "openai/" + modelName- }- providerName = strings.ToLower(strings.TrimSpace(providerName))- if nativeProvider := stagehandNativeProviders[providerName]; nativeProvider != "" {- return nativeProvider + "/" + modelName- }- return "openai/" + modelName-}--// Stream mirrors Invoke; Browser is a single-shot generator.+// Stream emits the final result after the browser task completes.func (b *BrowserComponent) Stream(ctx context.Context, db *gorm.DB, inputs map[string]any) (<-chan map[string]any, error) {out, err := b.Invoke(ctx, db, inputs)if err != nil {@@ -379,11 +298,11 @@ func (b *BrowserComponent) Stream(ctx context.Context, db *gorm.DB, inputs map[sfunc (b *BrowserComponent) Inputs() map[string]string {return map[string]string{"llm_id": "Required: tenant model id, e.g. \"deepseek-v4-pro@DeepSeek\". model_id accepted as alias.",- "prompts": "Required: natural-language extraction task. {sys.query} and other canvas refs are resolved. prompt accepted as alias.",- "max_steps": "Accepted for fixture compat; ignored at Invoke.",+ "prompts": "Required: natural-language browser task. {sys.query} and other canvas refs are resolved. prompt accepted as alias.",+ "max_steps": "Maximum browser agent steps (default 30).","headless": "Browser launch mode (default true).","enable_default_extensions": "Accepted for fixture compat; ignored at Invoke.",- "chromium_sandbox": "Accepted for fixture compat; ignored at Invoke.",+ "chromium_sandbox": "Whether to enable Chromium's sandbox (default false).","persist_session": "Accepted for fixture compat; ignored at Invoke.","url": "Go-only; not used (kept for backward compat).","timeout": "Go-only; not used (kept for backward compat).",@@ -406,7 +325,7 @@ func (b *BrowserComponent) GetInputForm() map[string]any {// Outputs returns the response surface.func (b *BrowserComponent) Outputs() map[string]string {return map[string]string{- "content": "Extracted plain text (Sessions.Extract result with schema {\"type\":\"string\"}).",+ "content": "Browser agent final result.","downloaded_files": "Always [] (file download not supported).","url": "Go-native compat key; always \"\".","status": "Go-native compat key; always 0.",@@ -416,94 +335,6 @@ func (b *BrowserComponent) Outputs() map[string]string {}}-// resolveBrowserLLM resolves tenant model credentials exclusively through the-// model_provider series tables (tenant_model → tenant_model_provider →-// tenant_model_instance). It no longer falls back to the legacy tenant_llm path.-//-// Tests override the lookup via `browserLLMLookupForTest` (a package-level-// function variable) so they don't need a real DB. Production code leaves the-// variable unset.-func resolveBrowserLLM(ctx context.Context, db *gorm.DB, tenantID, llmID string) (providerName, modelName, apiKey, baseURL string, err error) {- if browserLLMLookupForTest != nil {- return browserLLMLookupForTest(ctx, db, tenantID, llmID)- }-- providerName, modelName, apiKey, baseURL, err = resolveTenantModelBrowserLLM(ctx, db, tenantID, llmID)- if err != nil {- return "", "", "", "", fmt.Errorf("tenant model lookup (%q): %w", llmID, err)- }- baseURL = browserOpenAICompatibleBaseURL(baseURL, providerName)- return providerName, modelName, apiKey, baseURL, nil-}--func resolveTenantModelBrowserLLM(ctx context.Context, db *gorm.DB, tenantID, modelID string) (providerName, modelName, apiKey, baseURL string, err error) {- modelRow, err := dao.NewTenantModelDAO().GetByID(ctx, db, modelID)- if err != nil {- if errors.Is(err, gorm.ErrRecordNotFound) {- return "", "", "", "", err- }- return "", "", "", "", fmt.Errorf("tenant model id=%s lookup failed: %w", modelID, err)- }- if modelRow.Status != "active" {- return "", "", "", "", fmt.Errorf("tenant model id=%s is disabled", modelID)- }- if !entity.ModelType(modelRow.ModelType).Has(entity.ModelTypeChat) {- return "", "", "", "", fmt.Errorf("tenant model id=%s cannot be used as %s model", modelID, entity.ModelTypeChat.String())- }-- provider, err := dao.NewTenantModelProviderDAO().GetByID(ctx, db, modelRow.ProviderID)- if err != nil {- if errors.Is(err, gorm.ErrRecordNotFound) {- return "", "", "", "", fmt.Errorf("provider id=%s not found for model id=%s", modelRow.ProviderID, modelID)- }- return "", "", "", "", err- }- if provider == nil {- return "", "", "", "", fmt.Errorf("provider id=%s not found for model id=%s", modelRow.ProviderID, modelID)- }- if provider.TenantID != tenantID {- return "", "", "", "", fmt.Errorf("tenant %s has no access to provider owned by tenant %s", tenantID, provider.TenantID)- }-- instance, err := dao.NewTenantModelInstanceDAO().GetByID(ctx, db, modelRow.InstanceID)- if err != nil {- if errors.Is(err, gorm.ErrRecordNotFound) {- return "", "", "", "", fmt.Errorf("instance id=%s not found for model id=%s", modelRow.InstanceID, modelID)- }- return "", "", "", "", err- }- if instance == nil {- return "", "", "", "", fmt.Errorf("instance id=%s not found for model id=%s", modelRow.InstanceID, modelID)- }-- apiKey = instance.APIKey- if strings.TrimSpace(instance.Extra) != "" {- var extra map[string]string- if err = json.Unmarshal([]byte(instance.Extra), &extra); err != nil {- return "", "", "", "", err- }- baseURL = extra["base_url"]- }- return provider.ProviderName, modelRow.ModelName, apiKey, baseURL, nil-}--func browserOpenAICompatibleBaseURL(baseURL, provider string) string {- baseURL = strings.TrimSpace(baseURL)- if baseURL != "" {- return baseURL- }- provider = strings.TrimSpace(provider)- if provider == "" {- return ""- }- return browserFactoryDefaultBaseURL[strings.ToLower(provider)]-}--// browserLLMLookupForTest is the test seam for `resolveBrowserLLM`.-// When non-nil, it's called instead of the real DAO lookup.-// Production leaves this nil; tests set it via `defer ... = nil`.-var browserLLMLookupForTest func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (providerName, modelName, apiKey, baseURL string, err error)-func init() {Register(componentNameBrowser, NewBrowserComponent)}diff --git a/internal/agent/component/browser_test.go b/internal/agent/component/browser_test.goindex 71ed080009..f89cc27182 100644--- a/internal/agent/component/browser_test.go+++ b/internal/agent/component/browser_test.go@@ -25,42 +25,40 @@ import ("ragflow/internal/agent/canvas""ragflow/internal/agent/runtime"- "ragflow/internal/entity"-- "gorm.io/gorm"+ "ragflow/internal/entity/models")-// mockStagehandInvoker captures RunExtract requests and returns a-// canned JSON string. Used by every test in this file; the Browser+// mockStagehandInvoker captures RunTask requests and returns a+// canned final message. Used by every test in this file; the Browser// component depends on `DefaultRuntime` which is swapped via// `SetDefaultStagehandInvoker` and restored at test cleanup.type mockStagehandInvoker struct {mu sync.Mutex- requests []RunExtractRequest- rawJSON string // canned JSON-string response (e.g. "\"hello\"")+ requests []RunTaskRequest+ message stringerr error}-func (m *mockStagehandInvoker) RunTask(_ context.Context, _ RunTaskRequest) (string, error) {- return "", errors.New("RunTask not used in browser tests")+func (m *mockStagehandInvoker) RunExtract(_ context.Context, _ RunExtractRequest) (string, error) {+ return "", errors.New("Browser must execute the task before extracting page content")}-func (m *mockStagehandInvoker) RunExtract(_ context.Context, req RunExtractRequest) (string, error) {+func (m *mockStagehandInvoker) RunTask(_ context.Context, req RunTaskRequest) (string, error) {m.mu.Lock()defer m.mu.Unlock()m.requests = append(m.requests, req)if m.err != nil {return "", m.err}- return m.rawJSON, nil+ return m.message, nil}-func (m *mockStagehandInvoker) lastRequest(t *testing.T) RunExtractRequest {+func (m *mockStagehandInvoker) lastRequest(t *testing.T) RunTaskRequest {t.Helper()m.mu.Lock()defer m.mu.Unlock()if len(m.requests) == 0 {- t.Fatal("no RunExtract call recorded")+ t.Fatal("no RunTask call recorded")}return m.requests[len(m.requests)-1]}@@ -140,7 +138,7 @@ func TestBrowser_LLMIDAndPromptsRequired(t *testing.T) {// TestBrowser_ResolvesSysQueryTemplate: {sys.query} in prompts is// resolved against canvas state before dispatch to the runtime.func TestBrowser_ResolvesSysQueryTemplate(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"ok"`}+ mock := &mockStagehandInvoker{message: "ok"}withMockRuntime(t, mock)c, _ := NewBrowserComponent(map[string]any{@@ -178,102 +176,46 @@ func mustState(t *testing.T, ctx context.Context) *runtime.CanvasState {return state}-// TestBrowser_DispatchesToRuntime: when a valid llm_id and tenant-// are provided, the resolved prompts and llm config are forwarded-// to the runtime.-//-// We override the tenant LLM lookup to return a hard error so the-// test doesn't need a seeded DB; the assertion is that the error-// surfaces with the expected wrapper AND the runtime is not called.+// A model resolution error must stop before the browser runtime is invoked.func TestBrowser_DispatchesToRuntime(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"agent result text"`}+ mock := &mockStagehandInvoker{message: "agent result text"}withMockRuntime(t, mock)-- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (string, string, string, string, error) {- return "", "", "", "", errors.New("fake: tenant LLM not found")- }- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })-- c, _ := NewBrowserComponent(map[string]any{- "llm_id": "deepseek-v4-pro@DeepSeek",- "prompts": "do something",+ factoryErr := errors.New("tenant model unavailable")+ withBrowserModelFactory(t, func(ctx context.Context, tenantID, modelRef string) (*models.ChatModel, error) {+ if tenantID != "tenant-1" || modelRef != "selected-model" {+ t.Errorf("factory scope: tenant=%q model=%q", tenantID, modelRef)+ }+ return nil, factoryErr})- ctx := stateWith(t, map[string]any{"tenant_id": "tenant-1"})-- _, err := c.Invoke(ctx, nil, nil)- if err == nil {- t.Fatal("expected tenant LLM lookup error, got nil")- }- if !strings.Contains(err.Error(), "tenant llm lookup") {- t.Errorf("error %q should mention tenant llm lookup", err.Error())- }- if !strings.Contains(err.Error(), "fake: tenant LLM not found") {- t.Errorf("error %q should include underlying message", err.Error())+ c, _ := NewBrowserComponent(map[string]any{"llm_id": "selected-model", "prompts": "do something"})+ _, err := c.Invoke(stateWith(t, map[string]any{"tenant_id": "tenant-1"}), nil, nil)+ if !errors.Is(err, factoryErr) {+ t.Fatalf("expected factory error, got %v", err)}if len(mock.requests) != 0 {- t.Errorf("runtime should not be called when tenant lookup fails; got %d calls", len(mock.requests))+ t.Fatal("runtime called after model factory failed")}}-func TestResolveBrowserLLM_ResolvesTenantModelID(t *testing.T) {- db := setupComponentTestDB(t)- pushComponentDB(t, db)- if err := db.Create(&entity.TenantModelProvider{- ID: "provider-1",- TenantID: "tenant-1",- ProviderName: "OpenAI",- }).Error; err != nil {- t.Fatalf("create provider: %v", err)- }- if err := db.Create(&entity.TenantModelInstance{- ID: "instance-1",- ProviderID: "provider-1",- InstanceName: "default",- APIKey: "sk-instance",- Status: "active",- Extra: `{"base_url":"https://instance.example/v1"}`,- }).Error; err != nil {- t.Fatalf("create instance: %v", err)- }- if err := db.Create(&entity.TenantModel{- ID: "tenant-model-1",- ProviderID: "provider-1",- InstanceID: "instance-1",- ModelName: "gpt-4o",- ModelType: int(entity.ModelTypeChat),- Status: "active",- }).Error; err != nil {- t.Fatalf("create model: %v", err)- }-- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = nil- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })-- ctx := t.Context()- provider, model, apiKey, baseURL, err := resolveBrowserLLM(ctx, db, "tenant-1", "tenant-model-1")- if err != nil {- t.Fatalf("resolveBrowserLLM: %v", err)- }- if provider != "OpenAI" {- t.Fatalf("provider=%q, want OpenAI", provider)- }- if model != "gpt-4o" {- t.Fatalf("model=%q, want gpt-4o", model)- }- if apiKey != "sk-instance" {- t.Fatalf("apiKey=%q, want sk-instance", apiKey)- }- if baseURL != "https://instance.example/v1" {- t.Fatalf("baseURL=%q, want https://instance.example/v1", baseURL)- }+func withBrowserModelFactory(t *testing.T, factory func(context.Context, string, string) (*models.ChatModel, error)) {+ t.Helper()+ previous := browserModelFactory+ SetBrowserModelFactory(factory)+ t.Cleanup(func() { SetBrowserModelFactory(previous) })+}++func withBrowserTestModel(t *testing.T) {+ t.Helper()+ model := models.NewChatModel(nil, new("gpt-4o"), &models.APIConfig{+ ApiKey: new("sk-test"), BaseURL: new("https://api.openai.com/v1"),+ })+ withBrowserModelFactory(t, func(context.Context, string, string) (*models.ChatModel, error) { return model, nil })}// TestBrowser_MissingTenant: state.Sys["tenant_id"] is required.// Missing tenant_id must error.func TestBrowser_MissingTenant(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"ok"`}+ mock := &mockStagehandInvoker{message: "ok"}withMockRuntime(t, mock)c, _ := NewBrowserComponent(map[string]any{@@ -290,25 +232,12 @@ func TestBrowser_MissingTenant(t *testing.T) {}}-// TestBrowser_PropagatesRuntimeError: a runtime error surfaces-// wrapped with model context.-//-// We can't easily seed the tenant DAO in a unit test, so this test-// verifies the error-wrapping contract by injecting a mock runtime-// and a no-op DAO bypass via the resolveTenantLLM indirection. For-// v1 we keep the indirection simple: we expose a package-level-// override that tests can swap.+// TestBrowser_PropagatesRuntimeError verifies runtime errors retain model context.func TestBrowser_PropagatesRuntimeError(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"x"`, err: errors.New("boom")}+ mock := &mockStagehandInvoker{message: "x", err: errors.New("boom")}withMockRuntime(t, mock)- // Override the tenant LLM lookup so the test doesn't need a- // real DB.- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (string, string, string, string, error) {- return "OpenAI", "gpt-4o", "sk-test", "https://api.openai.com/v1", nil- }- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })+ withBrowserTestModel(t)c, _ := NewBrowserComponent(map[string]any{"llm_id": "gpt-4o@OpenAI",@@ -320,10 +249,10 @@ func TestBrowser_PropagatesRuntimeError(t *testing.T) {if err == nil {t.Fatal("expected runtime error, got nil")}- if !strings.Contains(err.Error(), "stagehand extract") {- t.Errorf("error should mention stagehand extract; got %v", err)+ if !strings.Contains(err.Error(), "stagehand task") {+ t.Errorf("error should mention stagehand task; got %v", err)}- if !strings.Contains(err.Error(), `model="openai/gpt-4o"`) {+ if !strings.Contains(err.Error(), `model="gpt-4o"`) {t.Errorf("error should include model context; got %v", err)}if !strings.Contains(err.Error(), "base_url=https://api.openai.com/v1") {@@ -354,92 +283,28 @@ func TestBrowserBaseURLForLog(t *testing.T) {}}-func TestBrowserOpenAICompatibleBaseURL(t *testing.T) {- cases := []struct {- name string- baseURL string- provider string- want string- }{- {- name: "explicit_wins",- baseURL: " https://gateway.example/v1 ",- provider: "DeepSeek",- want: "https://gateway.example/v1",- },- {- name: "deepseek_default",- provider: "DeepSeek",- want: "https://api.deepseek.com/v1",- },- {- name: "siliconflow_default",- provider: "SILICONFLOW",- want: "https://api.siliconflow.cn/v1",- },- {- name: "unknown_provider",- provider: "unknown",- want: "",- },- }- for _, tc := range cases {- t.Run(tc.name, func(t *testing.T) {- if got := browserOpenAICompatibleBaseURL(tc.baseURL, tc.provider); got != tc.want {- t.Fatalf("browserOpenAICompatibleBaseURL(%q, %q) = %q, want %q", tc.baseURL, tc.provider, got, tc.want)- }- })- }-}--func TestStagehandModelName(t *testing.T) {- cases := []struct {- name string- provider string- model string- want string- }{- {name: "deepseek_uses_openai_compatible_provider", provider: "DeepSeek", model: "deepseek-v4-pro", want: "openai/deepseek-v4-pro"},- {name: "siliconflow_uses_openai_compatible_provider", provider: "SILICONFLOW", model: "Qwen/Qwen3-32B", want: "openai/Qwen/Qwen3-32B"},- {name: "openai_native", provider: "OpenAI", model: "gpt-4o", want: "openai/gpt-4o"},- {name: "anthropic_native", provider: "Anthropic", model: "claude-sonnet-4-5", want: "anthropic/claude-sonnet-4-5"},- {name: "google_native", provider: "Google", model: "gemini-3-flash-preview", want: "google/gemini-3-flash-preview"},- {name: "already_prefixed", provider: "DeepSeek", model: "openai/deepseek-v4-pro", want: "openai/deepseek-v4-pro"},- }- for _, tc := range cases {- t.Run(tc.name, func(t *testing.T) {- if got := stagehandModelName(tc.provider, tc.model); got != tc.want {- t.Fatalf("stagehandModelName(%q, %q) = %q, want %q", tc.provider, tc.model, got, tc.want)- }- })- }-}--// TestBrowser_RunExtractRequestShape verifies the RunExtractRequest-// fields forwarded to the runtime: ModelName, TenantID, APIKey,-// Schema, and the resolved Instruction.-func TestBrowser_RunExtractRequestShape(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"ok"`}+// TestBrowser_RunTaskRequestShape verifies model settings, task instructions,+// and the step limit are forwarded to the navigation-capable runtime.+func TestBrowser_RunTaskRequestShape(t *testing.T) {+ mock := &mockStagehandInvoker{message: "ok"}withMockRuntime(t, mock)- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (string, string, string, string, error) {- return "OpenAI", "gpt-4o", "sk-test", "https://api.openai.com/v1", nil- }- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })+ withBrowserTestModel(t)c, _ := NewBrowserComponent(map[string]any{- "llm_id": "gpt-4o@OpenAI",- "prompts": "extract the page title",+ "llm_id": "gpt-4o@OpenAI",+ "prompts": "{sys.query}",+ "max_steps": 17,})- ctx := stateWith(t, map[string]any{"tenant_id": "tenant-1"})+ instruction := "extract the title of the pr in https://github.com/infiniflow/ragflow/pull/21460"+ ctx := stateWith(t, map[string]any{"tenant_id": "tenant-1", "query": instruction})if _, err := c.Invoke(ctx, nil, nil); err != nil {t.Fatalf("Invoke: %v", err)}req := mock.lastRequest(t)- if req.ModelName != "openai/gpt-4o" {- t.Errorf("ModelName: got %q, want openai/gpt-4o", req.ModelName)+ if req.ModelName != "gpt-4o" {+ t.Errorf("ModelName: got %q, want gpt-4o", req.ModelName)}if req.TenantID != "tenant-1" {t.Errorf("TenantID: got %q, want tenant-1", req.TenantID)@@ -447,28 +312,27 @@ func TestBrowser_RunExtractRequestShape(t *testing.T) {if req.APIKey != "sk-test" {t.Errorf("APIKey: got %q, want sk-test", req.APIKey)}- if req.Instruction != "extract the page title" {- t.Errorf("Instruction: got %q, want 'extract the page title'", req.Instruction)+ if req.BaseURL != "https://api.openai.com/v1" {+ t.Errorf("BaseURL: got %q", req.BaseURL)+ }+ if req.ChatModel == nil {+ t.Fatal("shared chat model missing from runtime request")}- if req.Schema == nil {- t.Fatal("Schema should not be nil")+ if req.Instruction != instruction {+ t.Errorf("Instruction: got %q, want %q", req.Instruction, instruction)}- if typ, ok := req.Schema["type"]; !ok || typ != "string" {- t.Errorf("Schema.type: got %v, want 'string'", typ)+ if req.MaxSteps != 17 {+ t.Errorf("MaxSteps: got %d, want 17", req.MaxSteps)}}// TestBrowser_HeadlessPropagates: the param's headless bool is// forwarded to the runtime verbatim.func TestBrowser_HeadlessPropagates(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"ok"`}+ mock := &mockStagehandInvoker{message: "ok"}withMockRuntime(t, mock)- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (string, string, string, string, error) {- return "OpenAI", "gpt-4o", "sk-test", "", nil- }- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })+ withBrowserTestModel(t)c, _ := NewBrowserComponent(map[string]any{"llm_id": "gpt-4o@OpenAI",@@ -489,18 +353,49 @@ func TestBrowser_HeadlessPropagates(t *testing.T) {}}+func TestBrowser_ChromiumSandboxPropagates(t *testing.T) {+ for _, tc := range []struct {+ name string+ set *bool+ want bool+ }{+ {name: "default_disabled"},+ {name: "disabled", set: new(false)},+ {name: "enabled", set: new(true), want: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ mock := &mockStagehandInvoker{message: "ok"}+ withMockRuntime(t, mock)++ withBrowserTestModel(t)++ conf := map[string]any{"llm_id": "gpt-4o@OpenAI", "prompts": "x"}+ if tc.set != nil {+ conf["chromium_sandbox"] = *tc.set+ }+ c, err := NewBrowserComponent(conf)+ if err != nil {+ t.Fatalf("NewBrowserComponent: %v", err)+ }+ ctx := stateWith(t, map[string]any{"tenant_id": "tenant-1"})+ if _, err := c.Invoke(ctx, nil, nil); err != nil {+ t.Fatalf("Invoke: %v", err)+ }+ if got := mock.lastRequest(t).ChromiumSandbox; got != tc.want {+ t.Errorf("ChromiumSandbox = %v, want %v", got, tc.want)+ }+ })+ }+}+// TestBrowser_OutputsShape: the output map contains the Python keys// (content, downloaded_files) and the Go-native compat keys (url,// status, size, model_id, prompt).func TestBrowser_OutputsShape(t *testing.T) {- mock := &mockStagehandInvoker{rawJSON: `"the agent's final message"`}+ mock := &mockStagehandInvoker{message: "the agent's final message"}withMockRuntime(t, mock)- prevLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(ctx context.Context, db *gorm.DB, tenantID, llmID string) (string, string, string, string, error) {- return "OpenAI", "gpt-4o", "sk-test", "", nil- }- t.Cleanup(func() { browserLLMLookupForTest = prevLookup })+ withBrowserTestModel(t)c, _ := NewBrowserComponent(map[string]any{"llm_id": "gpt-4o@OpenAI",@@ -556,3 +451,44 @@ func TestBrowser_ParamCheck_NegativeMaxSteps(t *testing.T) {t.Errorf("expected max_steps error, got %v", err)}}++// Provider catalog coverage verifies delegation, without making provider calls.+func TestBrowser_ForwardsConfiguredProviderModels(t *testing.T) {+ if err := models.InitProviderManager("../../../conf/models"); err != nil {+ t.Fatal(err)+ }+ providers := models.GetProviderManager().Providers+ if len(providers) == 0 {+ t.Fatal("provider catalog is empty")+ }+ t.Logf("verifying Browser delegates %d configured provider drivers", len(providers))+ for _, provider := range providers {+ if provider.ModelDriver == nil {+ t.Fatalf("provider %q has no configured driver", provider.Name)+ }+ t.Run(provider.Name, func(t *testing.T) {+ mock := &mockStagehandInvoker{message: "ok"}+ withMockRuntime(t, mock)+ model := models.NewChatModel(provider.ModelDriver, new("configured-browser-model"), nil)+ withBrowserModelFactory(t, func(context.Context, string, string) (*models.ChatModel, error) { return model, nil })+ c, _ := NewBrowserComponent(map[string]any{"llm_id": "configured-model", "prompts": "browse"})+ if _, err := c.Invoke(stateWith(t, map[string]any{"tenant_id": "tenant-1"}), nil, nil); err != nil {+ t.Fatal(err)+ }+ req := mock.lastRequest(t)+ if req.ChatModel != model || req.ChatModel.ModelDriver != provider.ModelDriver || req.ModelName != *model.ModelName {+ t.Errorf("configured provider model was replaced: name=%q model=%p", req.ModelName, req.ChatModel)+ }+ })+ }+}++func TestBrowser_ModelFactoryUnavailable(t *testing.T) {+ for _, factory := range []func(context.Context, string, string) (*models.ChatModel, error){nil, func(context.Context, string, string) (*models.ChatModel, error) { return nil, nil }} {+ withBrowserModelFactory(t, factory)+ c, _ := NewBrowserComponent(map[string]any{"llm_id": "configured-model", "prompts": "browse"})+ if _, err := c.Invoke(stateWith(t, map[string]any{"tenant_id": "tenant-1"}), nil, nil); err == nil {+ t.Fatal("expected unavailable model error")+ }+ }+}diff --git a/internal/agent/component/stagehand_runtime_integration_test.go b/internal/agent/component/stagehand_runtime_integration_test.goindex 4e171eb60b..71b33c0fda 100644--- a/internal/agent/component/stagehand_runtime_integration_test.go+++ b/internal/agent/component/stagehand_runtime_integration_test.go@@ -52,14 +52,13 @@ import ("os""path/filepath""ragflow/internal/common"+ "ragflow/internal/entity/models""strings""testing""time""ragflow/internal/agent/canvas""ragflow/internal/agent/runtime"-- "gorm.io/gorm")// TestStagehandRuntime_Extract is the single happy-path integration@@ -183,8 +182,8 @@ func cacheDirGuess() string {return filepath.Join(home, ".cache", "stagehand", "lib")}-// TestBrowser_E2E_Extract exercises Browser's extraction against a local page.-// Requires OPENAI_* credentials and Chrome.+// TestBrowser_E2E_Extract exercises Browser's agent through the shared model+// driver 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)@@ -206,14 +205,16 @@ func TestBrowser_E2E_Extract(t *testing.T) {srv := httptest.NewServer(mux)t.Cleanup(srv.Close)- previousLookup := browserLLMLookupForTest- browserLLMLookupForTest = func(_ context.Context, _ *gorm.DB, tenantID, modelRef string) (string, string, string, string, error) {+ driver := models.NewOpenAIAPICompatibleModel(map[string]string{"default": strings.TrimRight(baseURL, "/")}, models.URLSuffix{Chat: "chat/completions"})+ chatModel := models.NewChatModel(driver, &model, &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL})+ previousFactory := browserModelFactory+ SetBrowserModelFactory(func(_ context.Context, tenantID, modelRef string) (*models.ChatModel, error) {if tenantID != "tenant-1" {- return "", "", "", "", fmt.Errorf("unexpected tenant %q", tenantID)+ return nil, fmt.Errorf("unexpected tenant %q", tenantID)}- return "OpenAI", model, apiKey, baseURL, nil- }- t.Cleanup(func() { browserLLMLookupForTest = previousLookup })+ return chatModel, nil+ })+ t.Cleanup(func() { SetBrowserModelFactory(previousFactory) })// --- use production stagehand runtime ---r := newStagehandRuntimeFromEnv()@@ -244,7 +245,7 @@ func TestBrowser_E2E_Extract(t *testing.T) {invokeCtx, cancel := context.WithTimeout(ctx, 3*time.Minute)defer cancel()- t.Logf("starting browser.Invoke (RunExtract) against %s (timeout 3m)", srv.URL)+ t.Logf("starting browser.Invoke (RunTask) against %s (timeout 3m)", srv.URL)out, err := c.Invoke(invokeCtx, nil, nil)if err != nil {t.Fatalf("Invoke: %v", err)diff --git a/internal/server/api/server.go b/internal/server/api/server.goindex a851b67e41..e102ca68e0 100644--- a/internal/server/api/server.go+++ b/internal/server/api/server.go@@ -126,6 +126,9 @@ func startServer(ctx context.Context, serverName string, arguments *bootstrap.SemcpService := service.NewMCPService()modelProviderService := service.NewModelProviderService()modelFactory := service.NewModelFactory()+ component.SetBrowserModelFactory(func(ctx context.Context, tenantID, modelRef string) (*modelModule.ChatModel, error) {+ return modelFactory.NewChatModel(ctx, service.ModelAccess{TenantID: tenantID}, modelRef)+ })modelCallService := service.NewModelCallServiceWithProviderService(modelProviderService, modelFactory)// Wire the real MemorySaver so the Message component can persist