fix(go-agent): align Go DuckDuckGo component with canvas input form (#16775)

## Summary

- register the Go `DuckDuckGo` canvas component and restore its dynamic
input form metadata
- align the Go component input/output surface with the current canvas
usage for `query`, `channel`, and `top_n`
- fix DuckDuckGo news search in Go by fetching the required `vqd` token
before calling `news.js`, and add targeted regression tests

## Testing

Passed:
- `bash build.sh --test ./internal/agent/tool/... -run 'DuckDuckGo'`
- `bash build.sh --test ./internal/agent/component/... -run
'DuckDuckGo|TestVerifyRegistration_P1'`
- `bash build.sh --test ./internal/agent/component/... -run
'DuckDuckGo'`

Not run:
- frontend tests
- frontend build
- full Go test suite

<img width="1776" height="1092" alt="image"
src="https://github.com/user-attachments/assets/9f3f8e4b-f6b4-4915-b96c-3c5b8c7b8b30"
/>
This commit is contained in:
Hz_
2026-07-10 10:11:13 +08:00
committed by GitHub
parent 8236f2cabb
commit d48a5622df
6 changed files with 760 additions and 190 deletions

View File

@@ -0,0 +1,132 @@
//
// 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"
"encoding/json"
"strings"
"testing"
einotool "github.com/cloudwego/eino/components/tool"
)
type fakeDuckDuckGoInvoker struct {
args map[string]any
}
func (f *fakeDuckDuckGoInvoker) InvokableRun(_ context.Context, argsJSON string, _ ...einotool.Option) (string, error) {
if err := json.Unmarshal([]byte(argsJSON), &f.args); err != nil {
return "", err
}
return `{"results":[{"title":"RAGFlow","url":"https://ragflow.io","body":"Open source RAG engine"}]}`, nil
}
func TestDuckDuckGo_RegisteredFactory(t *testing.T) {
t.Parallel()
c, err := New("DuckDuckGo", nil)
if err != nil {
t.Fatalf("New(DuckDuckGo) errored: %v", err)
}
if got := c.Name(); got != "DuckDuckGo" {
t.Fatalf("Name() = %q, want DuckDuckGo", got)
}
formGetter, ok := c.(interface{ GetInputForm() map[string]any })
if !ok {
t.Fatal("DuckDuckGo component does not expose GetInputForm")
}
form := formGetter.GetInputForm()
query, ok := form["query"].(map[string]any)
if !ok {
t.Fatalf("GetInputForm()[query] has type %T, want map", form["query"])
}
if query["type"] != "line" {
t.Fatalf("GetInputForm()[query][type] = %v, want line", query["type"])
}
channel, ok := form["channel"].(map[string]any)
if !ok {
t.Fatalf("GetInputForm()[channel] has type %T, want map", form["channel"])
}
if channel["value"] != "general" {
t.Fatalf("GetInputForm()[channel][value] = %v, want general", channel["value"])
}
if _, ok := c.Outputs()["formalized_content"]; !ok {
t.Fatal("Outputs() missing formalized_content")
}
if _, ok := c.Outputs()["json"]; !ok {
t.Fatal("Outputs() missing json")
}
}
func TestDuckDuckGo_InvokeAdaptsCanvasInputsAndOutputs(t *testing.T) {
t.Parallel()
fake := &fakeDuckDuckGoInvoker{}
c := newDuckDuckGoComponentWithInvoker(fake)
out, err := c.Invoke(context.Background(), map[string]any{
"query": " privacy search ",
"channel": "news",
"top_n": float64(3),
})
if err != nil {
t.Fatalf("Invoke errored: %v", err)
}
if got := fake.args["query"]; got != "privacy search" {
t.Errorf("query arg = %v, want trimmed query", got)
}
if got := fake.args["channel"]; got != "news" {
t.Errorf("channel arg = %v, want news", got)
}
if got := fake.args["top_n"]; got != float64(3) {
t.Errorf("top_n arg = %v, want 3", got)
}
formalized, _ := out["formalized_content"].(string)
for _, want := range []string{"RAGFlow", "https://ragflow.io", "Open source RAG engine"} {
if !strings.Contains(formalized, want) {
t.Errorf("formalized_content missing %q: %s", want, formalized)
}
}
results, ok := out["json"].([]any)
if !ok {
t.Fatalf("json output has type %T, want []any", out["json"])
}
if len(results) != 1 {
t.Fatalf("json output length = %d, want 1", len(results))
}
}
func TestDuckDuckGo_InvokeEmptyQueryReturnsEmptyPayload(t *testing.T) {
t.Parallel()
c := newDuckDuckGoComponentWithInvoker(&fakeDuckDuckGoInvoker{})
out, err := c.Invoke(context.Background(), map[string]any{"query": " "})
if err != nil {
t.Fatalf("Invoke errored: %v", err)
}
if got := out["formalized_content"]; got != "" {
t.Errorf("formalized_content = %v, want empty string", got)
}
results, ok := out["json"].([]any)
if !ok || len(results) != 0 {
t.Fatalf("json output = %#v, want empty []any", out["json"])
}
}

View File

@@ -518,6 +518,7 @@ func init() {
Register(componentNameIteration, NewIterationStub)
Register(componentNameIterationItem, NewIterationItemStub)
Register("BGPT", newBGPTComponent)
Register("DuckDuckGo", newDuckDuckGoComponent)
Register("Google", newGoogleComponent)
Register("YahooFinance", newYahooFinanceComponent)
}

View File

@@ -313,6 +313,10 @@ type bgptInvoker interface {
InvokableRun(ctx context.Context, argsJSON string, opts ...einotool.Option) (string, error)
}
type duckDuckGoInvoker interface {
InvokableRun(ctx context.Context, argsJSON string, opts ...einotool.Option) (string, error)
}
// bgptComponent delegates to internal/agent/tool/BGPTTool and adapts
// the tool envelope to the BGPT canvas output contract.
type bgptComponent struct {
@@ -406,6 +410,121 @@ func (c *bgptComponent) Stream(_ context.Context, _ map[string]any) (<-chan map[
return nil, nil
}
// duckDuckGoComponent delegates to internal/agent/tool/DuckDuckGoTool.
type duckDuckGoComponent struct {
inner duckDuckGoInvoker
}
func newDuckDuckGoComponent(_ map[string]any) (Component, error) {
return newDuckDuckGoComponentWithInvoker(agenttool.NewDuckDuckGoTool()), nil
}
func newDuckDuckGoComponentWithInvoker(inner duckDuckGoInvoker) Component {
return &duckDuckGoComponent{inner: inner}
}
func (c *duckDuckGoComponent) Name() string { return "DuckDuckGo" }
func (c *duckDuckGoComponent) Inputs() map[string]string {
return map[string]string{
"query": "Search query.",
"channel": "Search channel: general or news.",
"top_n": "Maximum number of results.",
}
}
func (c *duckDuckGoComponent) GetInputForm() map[string]any {
return map[string]any{
"query": map[string]any{
"name": "Query",
"type": "line",
},
"channel": map[string]any{
"name": "Channel",
"type": "options",
"value": "general",
"options": []string{"general", "news"},
},
}
}
func (c *duckDuckGoComponent) Outputs() map[string]string {
return map[string]string{
"formalized_content": "Rendered search results for downstream LLM prompts.",
"json": "Raw result list.",
}
}
func (c *duckDuckGoComponent) Invoke(ctx context.Context, inputs map[string]any) (map[string]any, error) {
query := strings.TrimSpace(stringParam(inputs["query"]))
if query == "" {
return map[string]any{"formalized_content": "", "json": []any{}}, nil
}
args := map[string]any{
"query": query,
}
if channel := strings.TrimSpace(stringParam(inputs["channel"])); channel != "" {
args["channel"] = channel
}
if topN := toIntParam(inputs["top_n"]); topN > 0 {
args["top_n"] = topN
}
argsJSON, _ := json.Marshal(args)
out, err := c.inner.InvokableRun(ctx, string(argsJSON))
decoded := parseToolEnvelope(out)
if err != nil {
if len(decoded) > 0 {
return map[string]any{
"formalized_content": "",
"json": []any{},
"_ERROR": decoded["_ERROR"],
}, nil
}
return nil, fmt.Errorf("canvas: DuckDuckGo: %w", err)
}
results := anySlice(decoded["results"])
return map[string]any{
"formalized_content": renderDuckDuckGoResults(results),
"json": results,
}, nil
}
func (c *duckDuckGoComponent) Stream(_ context.Context, _ map[string]any) (<-chan map[string]any, error) {
return nil, nil
}
func renderDuckDuckGoResults(results []any) string {
if len(results) == 0 {
return ""
}
blocks := make([]string, 0, len(results))
for _, item := range results {
m, ok := item.(map[string]any)
if !ok {
continue
}
field := func(key string) string {
v, ok := m[key]
if !ok || v == nil {
return "-"
}
text := strings.TrimSpace(fmt.Sprintf("%v", v))
if text == "" {
return "-"
}
return text
}
blocks = append(blocks, strings.Join([]string{
fmt.Sprintf("Title: %s", field("title")),
fmt.Sprintf("URL: %s", field("url")),
fmt.Sprintf("Body: %s", field("body")),
}, "\n"))
}
return strings.Join(blocks, "\n\n")
}
func stringParam(v any) string {
if s, ok := v.(string); ok {
return s
@@ -1309,6 +1428,7 @@ var (
_ Component = (*tavilySearchComponent)(nil)
_ Component = (*googleComponent)(nil)
_ Component = (*tavilyExtractComponent)(nil)
_ Component = (*duckDuckGoComponent)(nil)
_ Component = (*exesqlComponent)(nil)
_ Component = (*codeExecComponent)(nil)
_ Component = (*yahooFinanceComponent)(nil)
@@ -1319,4 +1439,5 @@ var (
var _ einotool.InvokableTool = (*agenttool.TavilyTool)(nil)
var _ einotool.InvokableTool = (*agenttool.GoogleTool)(nil)
var _ einotool.InvokableTool = (*agenttool.TavilyExtractTool)(nil)
var _ einotool.InvokableTool = (*agenttool.DuckDuckGoTool)(nil)
var _ einotool.InvokableTool = (*agenttool.YahooFinanceTool)(nil)

View File

@@ -45,8 +45,8 @@ func TestVerifyRegistration_P1(t *testing.T) {
if len(missing) > 0 {
t.Fatalf("missing P0/P1 components: %v (have %d: %v)", missing, len(names), names)
}
if got := len(names); got < 12 || got > 34 {
t.Errorf("expected 12-34 registered (current plan scope + v1 wrappers/stubs), got %d: %v", got, names)
if got := len(names); got < 12 || got > 35 {
t.Errorf("expected 12-35 registered (current plan scope + v1 wrappers/stubs), got %d: %v", got, names)
}
// ExitLoop must NOT be in the registry (legacy compat lives at