fix(agent/tools): port AkShare to ToolBase so it works as an Agent tool (#16417)
### What problem does this PR solve?
Closes #16416.
The **AkShare** agent tool (`agent/tools/akshare.py`) was never ported
to the modern `ToolBase`/`_invoke` interface during the agent module
redesign and was still written against the removed legacy
`_run`/`be_output` API, so it was non-functional:
1. **Adding it to an Agent raised `AttributeError`.** `AkShare` extended
`ComponentBase` (not `ToolBase`) and `AkShareParam` defined no `meta`,
so it had no `get_meta()`. `agent/component/agent_with_tools.py` builds
each tool's function descriptor via `cpn.get_meta()`, so constructing an
Agent that includes the AkShare tool raised `AttributeError: 'AkShare'
object has no attribute 'get_meta'`.
2. **It could never run.** `invoke()` dispatches to `self._invoke`, but
`AkShare` only implemented the legacy `_run`, so `_invoke` fell through
to `ComponentBase._invoke` → `NotImplementedError`. `_run` also called
`be_output(...)`, which no longer exists on the base classes.
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
### Changes
- Port `AkShareParam` to `ToolParamBase` with a `ToolMeta` (defined
before `super().__init__()`, matching `ArXivParam`/`TavilyExtractParam`)
exposing a required `query` parameter — the stock symbol to look up,
default `{sys.query}`. `query` matches the `{sys.query}` convention
shared by the other tools.
- Rewrite the component with `_invoke`/`set_output("formalized_content",
...)` (errors surfaced via `_ERROR`), keeping `top_n` and importing
`akshare` lazily.
- Add regression tests
(`test/unit_test/agent/component/test_akshare.py`) covering param
construction, validation, and the tool descriptor.
Same class of defect as #16329 (DeepL) and #16414 (Crawler).
Backend-only; no frontend changes.
---------
Co-authored-by: Zhichang Yu <yuzhichang@gmail.com>
2026-07-03 08:39:26 +05:00
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from agent.tools.akshare import AkShare, AkShareParam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_tool(top_n=10):
|
|
|
|
|
# Bypass the canvas-bound __init__ (mirrors test_pubmed_unit.py) and stub the
|
|
|
|
|
# canvas-touching helpers so we can exercise _invoke's execution path.
|
|
|
|
|
tool = AkShare.__new__(AkShare)
|
|
|
|
|
param = AkShareParam()
|
|
|
|
|
param.top_n = top_n
|
|
|
|
|
tool._param = param
|
|
|
|
|
tool.check_if_canceled = lambda *a, **k: False
|
|
|
|
|
out = {}
|
|
|
|
|
tool.set_output = lambda k, v: out.__setitem__(k, v)
|
2026-07-03 12:53:39 +08:00
|
|
|
tool.output = lambda k=None: out.get(k) if k else out
|
fix(agent/tools): port AkShare to ToolBase so it works as an Agent tool (#16417)
### What problem does this PR solve?
Closes #16416.
The **AkShare** agent tool (`agent/tools/akshare.py`) was never ported
to the modern `ToolBase`/`_invoke` interface during the agent module
redesign and was still written against the removed legacy
`_run`/`be_output` API, so it was non-functional:
1. **Adding it to an Agent raised `AttributeError`.** `AkShare` extended
`ComponentBase` (not `ToolBase`) and `AkShareParam` defined no `meta`,
so it had no `get_meta()`. `agent/component/agent_with_tools.py` builds
each tool's function descriptor via `cpn.get_meta()`, so constructing an
Agent that includes the AkShare tool raised `AttributeError: 'AkShare'
object has no attribute 'get_meta'`.
2. **It could never run.** `invoke()` dispatches to `self._invoke`, but
`AkShare` only implemented the legacy `_run`, so `_invoke` fell through
to `ComponentBase._invoke` → `NotImplementedError`. `_run` also called
`be_output(...)`, which no longer exists on the base classes.
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
### Changes
- Port `AkShareParam` to `ToolParamBase` with a `ToolMeta` (defined
before `super().__init__()`, matching `ArXivParam`/`TavilyExtractParam`)
exposing a required `query` parameter — the stock symbol to look up,
default `{sys.query}`. `query` matches the `{sys.query}` convention
shared by the other tools.
- Rewrite the component with `_invoke`/`set_output("formalized_content",
...)` (errors surfaced via `_ERROR`), keeping `top_n` and importing
`akshare` lazily.
- Add regression tests
(`test/unit_test/agent/component/test_akshare.py`) covering param
construction, validation, and the tool descriptor.
Same class of defect as #16329 (DeepL) and #16414 (Crawler).
Backend-only; no frontend changes.
---------
Co-authored-by: Zhichang Yu <yuzhichang@gmail.com>
2026-07-03 08:39:26 +05:00
|
|
|
return tool, out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fake_news_df(n):
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
return pd.DataFrame(
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"新闻链接": f"https://u{i}",
|
|
|
|
|
"新闻标题": f"title{i}",
|
|
|
|
|
"新闻内容": f"content{i}",
|
|
|
|
|
"发布时间": "2026-01-01",
|
|
|
|
|
"文章来源": "src",
|
|
|
|
|
}
|
|
|
|
|
for i in range(n)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_param_instantiates():
|
|
|
|
|
AkShareParam()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_passes_with_defaults():
|
|
|
|
|
AkShareParam().check()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_meta_exposes_query_parameter():
|
|
|
|
|
# Regression: AkShare extended ComponentBase and defined no `meta`, so it
|
|
|
|
|
# had no get_meta() and crashed agent_with_tools when added to an Agent.
|
|
|
|
|
meta = AkShareParam().get_meta()
|
|
|
|
|
params = meta["function"]["parameters"]
|
|
|
|
|
assert "query" in params["properties"]
|
|
|
|
|
assert "query" in params["required"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_rejects_non_positive_top_n():
|
|
|
|
|
param = AkShareParam()
|
|
|
|
|
param.top_n = 0
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
param.check()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_invoke_returns_content_and_sets_formalized_content(monkeypatch):
|
|
|
|
|
# Regression for the restored runtime path: _invoke(query=...) must fetch
|
|
|
|
|
# news, return the formatted content, write it to formalized_content, and
|
|
|
|
|
# respect top_n.
|
|
|
|
|
pytest.importorskip("akshare")
|
|
|
|
|
import akshare
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(akshare, "stock_news_em", lambda symbol: _fake_news_df(5))
|
|
|
|
|
|
|
|
|
|
tool, out = _make_tool(top_n=2)
|
|
|
|
|
res = tool._invoke(query="600519")
|
|
|
|
|
|
|
|
|
|
assert "title0" in res and "https://u0" in res
|
|
|
|
|
assert out["formalized_content"] == res
|
|
|
|
|
# top_n is applied via .head(top_n): only 2 articles formatted.
|
|
|
|
|
assert res.count("新闻内容:") == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_invoke_empty_query_returns_empty():
|
|
|
|
|
# Empty query short-circuits without calling akshare.
|
|
|
|
|
tool, out = _make_tool()
|
|
|
|
|
assert tool._invoke(query="") == ""
|
|
|
|
|
assert out.get("formalized_content") == ""
|