mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-25 01:20:11 +08:00
fix(agent/tools): port DeepL to ToolBase so it works as an Agent tool (#18395)
### Summary Closes #18394.
This commit is contained in:
@@ -24,16 +24,20 @@ pytest.importorskip("deepl")
|
||||
from agent.tools.deepl import DeepL, DeepLParam # noqa: E402
|
||||
|
||||
|
||||
class _Canvas:
|
||||
def is_canceled(self):
|
||||
return False
|
||||
def _make_tool(param=None):
|
||||
# Bypass the canvas-bound __init__ (mirrors test_akshare.py) and stub the
|
||||
# canvas-touching helpers so we can exercise _invoke's execution path.
|
||||
tool = DeepL.__new__(DeepL)
|
||||
tool._param = param or DeepLParam()
|
||||
tool.check_if_canceled = lambda *a, **k: False
|
||||
out = {}
|
||||
tool.set_output = lambda k, v: out.__setitem__(k, v)
|
||||
tool.output = lambda k=None: out.get(k) if k else out
|
||||
return tool, out
|
||||
|
||||
|
||||
def _deepl(param=None):
|
||||
cpn = DeepL.__new__(DeepL)
|
||||
cpn._canvas = _Canvas()
|
||||
cpn._param = param or DeepLParam()
|
||||
return cpn
|
||||
def test_param_instantiates():
|
||||
DeepLParam()
|
||||
|
||||
|
||||
def test_check_passes_with_defaults():
|
||||
@@ -56,14 +60,58 @@ def test_check_rejects_invalid_target_lang():
|
||||
param.check()
|
||||
|
||||
|
||||
def test_meta_exposes_query_parameter():
|
||||
# Regression: DeepL extended ComponentBase and defined no `meta`, so it had
|
||||
# no get_meta() and crashed agent_with_tools when added to an Agent.
|
||||
meta = DeepLParam().get_meta()
|
||||
params = meta["function"]["parameters"]
|
||||
assert "query" in params["properties"]
|
||||
assert "query" in params["required"]
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_run_returns_error_on_translation_failure():
|
||||
cpn = _deepl()
|
||||
cpn._param.inputs = {"content": {"value": ["hello"]}}
|
||||
def test_invoke_returns_translation_and_sets_formalized_content():
|
||||
# Regression for the restored runtime path: DeepL only implemented the
|
||||
# legacy _run, so _invoke fell through to ComponentBase._invoke and raised
|
||||
# NotImplementedError.
|
||||
tool, out = _make_tool()
|
||||
|
||||
with patch.object(DeepL, "get_input", return_value={"content": ["hello"]}):
|
||||
with patch("agent.tools.deepl.deepl.Translator") as translator_cls:
|
||||
translator_cls.return_value.translate_text.side_effect = RuntimeError("boom")
|
||||
result = cpn._run([])
|
||||
with patch("agent.tools.deepl.deepl.Translator") as translator_cls:
|
||||
translator_cls.return_value.translate_text.return_value.text = "hello"
|
||||
res = tool._invoke(query="你好")
|
||||
|
||||
assert "**Error**:boom" in result.iloc[0]["content"]
|
||||
assert res == "hello"
|
||||
assert out["formalized_content"] == "hello"
|
||||
|
||||
|
||||
def test_invoke_passes_configured_languages():
|
||||
param = DeepLParam()
|
||||
param.source_lang = "ZH"
|
||||
param.target_lang = "EN-US"
|
||||
tool, _ = _make_tool(param)
|
||||
|
||||
with patch("agent.tools.deepl.deepl.Translator") as translator_cls:
|
||||
translate_text = translator_cls.return_value.translate_text
|
||||
translate_text.return_value.text = "hello"
|
||||
tool._invoke(query="你好")
|
||||
|
||||
translate_text.assert_called_once_with("你好", source_lang="ZH", target_lang="EN-US")
|
||||
|
||||
|
||||
def test_invoke_empty_query_returns_empty():
|
||||
# Empty query short-circuits without calling the DeepL SDK.
|
||||
tool, out = _make_tool()
|
||||
assert tool._invoke(query="") == ""
|
||||
assert out.get("formalized_content") == ""
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_invoke_surfaces_error_on_translation_failure():
|
||||
tool, out = _make_tool()
|
||||
|
||||
with patch("agent.tools.deepl.deepl.Translator") as translator_cls:
|
||||
translator_cls.return_value.translate_text.side_effect = RuntimeError("boom")
|
||||
res = tool._invoke(query="你好")
|
||||
|
||||
assert "boom" in res
|
||||
assert "boom" in out["_ERROR"]
|
||||
|
||||
Reference in New Issue
Block a user