Files
ragflow/internal/agent/component/tool_dispatch_test.go
Hz_ a7da78d0d7 refactor(go-agent): unify tool-backed canvas components (#16912)
## Summary

This PR consolidates Eino-backed Agent tools behind the shared
ToolBackedComponent implementation and aligns their Canvas
configuration,
runtime inputs, output conversion, validation, and registration.

## What changed

- Migrated these Canvas components to the unified tool-backed path:
    - Tavily Search and Extract
    - Execute SQL
    - Google
    - Yahoo Finance
    - Email
    - DuckDuckGo
    - Wikipedia
    - Google Scholar
    - ArXiv
    - PubMed
    - BGPT
    - GitHub
    - WenCai
    - SearXNG
    - Keenable Search

- Removed superseded component wrappers and their duplicate tests.
- Added dedicated registry builders for node-level configuration and
validation.
- Kept model-emitted runtime inputs separate from Canvas node
configuration.
- Moved Email defaults, template resolution, recipient parsing, SMTP
execution, and output conversion into the owning tool.
- Added complete ToolComponent specifications for Canvas inputs,
outputs, and input forms.
- Preserved raw upstream fields where downstream workflows may depend on
them.
- Added workflow registration coverage for all migrated component names.
- Kept HTTP Request, Docs Generator, and Browser as standalone
components because they are not Eino-backed tools.

## Testing

Passed:

bash build.sh --test ./internal/agent/tool/...
bash build.sh --test ./internal/agent/component/...
bash build.sh --test ./internal/agent/runtime/...

<img width="2057" height="1111" alt="image"
src="https://github.com/user-attachments/assets/c728d7a3-9d15-4c5c-b0eb-6b77ad0e41ac"
/>
<img width="2057" height="1111" alt="image"
src="https://github.com/user-attachments/assets/ef13119a-ef92-4b43-9061-eee9511bf492"
/>
<img width="2057" height="1111" alt="image"
src="https://github.com/user-attachments/assets/f50f2c30-eab0-463c-b0b3-0a02523219f1"
/>
2026-07-15 21:42:08 +08:00

109 lines
3.7 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package component
import (
"context"
"testing"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
"ragflow/internal/agent/tool"
)
// TestPhase3_5_ToolDispatchViaEinoReact is a regression guard for
// the tool-dispatch path. The Python equivalent is an async-aware
// tool dispatcher that handles sync/async tool call routing and
// MCP fallback. The Go side uses eino's react.Agent, which has its
// own equivalent dispatcher built into the framework.
//
// This test pins the contract: the Go AgentComponent delegates tool
// dispatch to eino's react.Agent via compose.ToolsNodeConfig, and
// runEinoReActAgent constructs the agent with that config. The
// integration is verified at the type level — both compose.ToolsNodeConfig
// and react.NewAgent must be reachable from runEinoReActAgent — and
// the actual dispatch behaviour is owned by eino (not by us).
func TestPhase3_5_ToolDispatchViaEinoReact(t *testing.T) {
// This is a static check: runEinoReActAgent must reference
// compose.ToolsNodeConfig. If a future refactor removes the
// import or the field, this test fails to compile.
var _ compose.ToolsNodeConfig
var _ react.AgentConfig
_ = tool.NewRetrievalTool // touch the tool package so a missing symbol surfaces
}
// TestPhase3_6_ToolDSLLoading exercises the Python-equivalent of
// _load_tool_obj: buildAgentTools(p) maps AgentParam.Tools (a slice
// of registered tool names) to einotool.BaseTool instances via
// agenttool.BuildAll. The factory validates each name against the
// registry; an unknown name surfaces a build-time error (not a
// silent failure inside the ReAct loop).
func TestPhase3_6_ToolDSLLoading(t *testing.T) {
var captured AgentParam
withAgentRunner(t, func(_ context.Context, p AgentParam) (*schema.Message, error) {
captured = p
// Return a non-tool result so eino's react.Agent doesn't try
// to actually invoke any tool — we just need buildAgentTools
// to have wired the registry successfully.
return &schema.Message{Role: schema.Assistant, Content: "ok"}, nil
})
c := NewAgentComponent(AgentParam{
ModelID: "stub",
MaxRounds: 1,
Tools: []string{"retrieval"}, // known tool
})
_, err := c.Invoke(context.Background(), map[string]any{
"user_prompt": "test",
})
if err != nil {
t.Fatalf("Invoke with known tool: %v", err)
}
if len(captured.Tools) != 1 || captured.Tools[0] != "retrieval" {
t.Errorf("Tools not preserved: %v", captured.Tools)
}
}
func TestAgent_GoogleToolDSLParamsLoading(t *testing.T) {
c := NewAgentComponent(AgentParam{
ModelID: "stub",
MaxRounds: 1,
Tools: []string{"google"},
ToolParams: map[string]map[string]any{
"google": {
"api_key": "KEY",
"country": "us",
"language": "en",
},
},
})
form := c.GetInputForm()
googleForm, ok := form["google_search"].(map[string]any)
if !ok {
t.Fatalf("GetInputForm missing google tool form: %+v", form)
}
if _, ok := googleForm["q"]; !ok {
t.Fatalf("google tool form missing q: %+v", googleForm)
}
if _, err := buildAgentTools(c.param); err != nil {
t.Fatalf("buildAgentTools with google params: %v", err)
}
}