mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-03 17:11:24 +00:00
Rewrite the default/creative/strict presets (classic + agentic) into
structured sections: grounding and cite-by-title guidance, insufficient-
context behavior, current date, respond-in-user-language, scoped mermaid
usage, an untrusted-content guardrail, and a conditional XML-tagged
document context block. A memory directory listing is injected at render
time via the template prefetch mechanism so the model starts oriented
without burning a tool call.
Fixes along the way:
- Agentic preset swap was dead code: _get_prompt_content cached the
classic preset before create_agent's swap check ran, so agentic and
research agents always got the classic preset. The swap now happens
inside _get_prompt_content.
- Jinja autoescape corrupted document content in custom prompts
(< -> <); prompts are not HTML, autoescape is now off.
- Literal {summaries} leaked into the prompt when no docs were
retrieved; the placeholder is now stripped.
- Agentic/research prompts referenced tool names from a dropped naming
scheme (search_internal, reason_think); they now reference the real
names (search, reason).
- The strict preset told the model to "be very creative and use your
imagination" right after "never make up information".
- extract_tool_usages recorded intermediate attribute chains as
bare-tool usages, which meant "run all actions" at prefetch; only
maximal chains are recorded now.
- Headless runs retrieved docs but never rendered them into the
prompt; the prompt is now rendered like the streaming path.
- Default tools were unreachable by name in prompt templates
(prefetch results were keyed by synthetic id only); defaults now
claim the name key unless an explicit row shadows it.
Tool layer: memory/notes/todo actions are namespaced (memory_view,
note_overwrite, todo_create, ...) with legacy unprefixed names still
accepted via prefix stripping; duplicate action names across tools are
disambiguated with the owning tool's name instead of numeric suffixes;
thin tool descriptions rewritten (brave, duckduckgo, telegram, ntfy,
cryptoprice, read_webpage, internal_search, think).
Docs are now wrapped per chunk in <document index>/<source>/<content>
tags for citation-by-title support.
202 lines
7.7 KiB
Python
202 lines
7.7 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from application.templates.template_engine import TemplateEngine, TemplateRenderError
|
|
|
|
|
|
@pytest.fixture
|
|
def engine():
|
|
return TemplateEngine()
|
|
|
|
|
|
# ── render ─────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestRender:
|
|
def test_simple_variable(self, engine):
|
|
result = engine.render("Hello {{ name }}", {"name": "World"})
|
|
assert result == "Hello World"
|
|
|
|
def test_empty_template_returns_empty(self, engine):
|
|
assert engine.render("", {"x": 1}) == ""
|
|
|
|
def test_none_template_returns_empty(self, engine):
|
|
assert engine.render(None, {"x": 1}) == ""
|
|
|
|
def test_no_variables(self, engine):
|
|
assert engine.render("plain text", {}) == "plain text"
|
|
|
|
def test_multiple_variables(self, engine):
|
|
tpl = "{{ a }} and {{ b }}"
|
|
assert engine.render(tpl, {"a": "X", "b": "Y"}) == "X and Y"
|
|
|
|
def test_nested_dict_access(self, engine):
|
|
tpl = "{{ data.key }}"
|
|
assert engine.render(tpl, {"data": {"key": "value"}}) == "value"
|
|
|
|
def test_loop(self, engine):
|
|
tpl = "{% for i in items %}{{ i }} {% endfor %}"
|
|
result = engine.render(tpl, {"items": ["a", "b", "c"]})
|
|
assert result.strip() == "a b c"
|
|
|
|
def test_conditional(self, engine):
|
|
tpl = "{% if show %}yes{% else %}no{% endif %}"
|
|
assert engine.render(tpl, {"show": True}) == "yes"
|
|
assert engine.render(tpl, {"show": False}) == "no"
|
|
|
|
def test_syntax_error_raises_template_render_error(self, engine):
|
|
with pytest.raises(TemplateRenderError, match="syntax error"):
|
|
engine.render("{% if %}", {})
|
|
|
|
def test_undefined_variable_chainable(self, engine):
|
|
# ChainableUndefined should NOT raise; it silently returns empty
|
|
result = engine.render("{{ missing }}", {})
|
|
assert result == ""
|
|
|
|
def test_no_autoescape_content_preserved(self, engine):
|
|
# Output is an LLM prompt, not HTML: injected values (e.g. document
|
|
# content with markup or code) must pass through verbatim.
|
|
result = engine.render(
|
|
"{{ content }}", {"content": "<code>a < b && c == 'd'</code>"}
|
|
)
|
|
assert result == "<code>a < b && c == 'd'</code>"
|
|
|
|
def test_trim_blocks(self, engine):
|
|
tpl = "{% if True %}\nyes\n{% endif %}"
|
|
result = engine.render(tpl, {})
|
|
assert result.strip() == "yes"
|
|
|
|
def test_general_exception_raises_template_render_error(self, engine):
|
|
with patch.object(engine._env, "from_string", side_effect=RuntimeError("boom")):
|
|
with pytest.raises(TemplateRenderError, match="rendering failed"):
|
|
engine.render("{{ x }}", {"x": 1})
|
|
|
|
|
|
# ── validate_template ──────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestValidateTemplate:
|
|
def test_valid_template(self, engine):
|
|
assert engine.validate_template("{{ name }}") is True
|
|
|
|
def test_empty_template_valid(self, engine):
|
|
assert engine.validate_template("") is True
|
|
|
|
def test_none_template_valid(self, engine):
|
|
assert engine.validate_template(None) is True
|
|
|
|
def test_invalid_syntax(self, engine):
|
|
assert engine.validate_template("{% if %}") is False
|
|
|
|
def test_plain_text_valid(self, engine):
|
|
assert engine.validate_template("just plain text") is True
|
|
|
|
def test_complex_valid_template(self, engine):
|
|
tpl = "{% for x in items %}{{ x.name }}{% endfor %}"
|
|
assert engine.validate_template(tpl) is True
|
|
|
|
def test_general_exception_returns_false(self, engine):
|
|
with patch.object(engine._env, "from_string", side_effect=RuntimeError("boom")):
|
|
assert engine.validate_template("{{ x }}") is False
|
|
|
|
|
|
# ── extract_variables ──────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestExtractVariables:
|
|
def test_empty_template(self, engine):
|
|
assert engine.extract_variables("") == set()
|
|
|
|
def test_none_template(self, engine):
|
|
assert engine.extract_variables(None) == set()
|
|
|
|
def test_syntax_error_returns_empty(self, engine):
|
|
assert engine.extract_variables("{% if %}") == set()
|
|
|
|
def test_general_exception_returns_empty(self, engine):
|
|
with patch.object(engine._env, "parse", side_effect=RuntimeError("boom")):
|
|
assert engine.extract_variables("{{ x }}") == set()
|
|
|
|
|
|
# ── extract_tool_usages ───────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestExtractToolUsages:
|
|
def test_empty_template(self, engine):
|
|
assert engine.extract_tool_usages("") == {}
|
|
|
|
def test_none_template(self, engine):
|
|
assert engine.extract_tool_usages(None) == {}
|
|
|
|
def test_syntax_error_returns_empty(self, engine):
|
|
assert engine.extract_tool_usages("{% if %}") == {}
|
|
|
|
def test_getattr_single_tool(self, engine):
|
|
tpl = "{{ tools.memory.notes }}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "memory" in result
|
|
assert "notes" in result["memory"]
|
|
|
|
def test_getattr_tool_without_action(self, engine):
|
|
tpl = "{{ tools.search }}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "search" in result
|
|
assert None in result["search"]
|
|
|
|
def test_getitem_bracket_notation(self, engine):
|
|
tpl = '{{ tools["calendar"]["events"] }}'
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "calendar" in result
|
|
assert "events" in result["calendar"]
|
|
|
|
def test_multiple_tools(self, engine):
|
|
tpl = "{{ tools.memory.notes }} {{ tools.search.query }}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "memory" in result
|
|
assert "search" in result
|
|
|
|
def test_same_tool_multiple_actions(self, engine):
|
|
tpl = "{{ tools.memory.notes }} {{ tools.memory.tasks }}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "memory" in result
|
|
assert "notes" in result["memory"]
|
|
assert "tasks" in result["memory"]
|
|
|
|
def test_non_tools_getattr_ignored(self, engine):
|
|
tpl = "{{ data.something }}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert result == {}
|
|
|
|
def test_general_parse_error_returns_empty(self, engine):
|
|
with patch.object(engine._env, "parse", side_effect=RuntimeError("boom")):
|
|
assert engine.extract_tool_usages("{{ tools.x }}") == {}
|
|
|
|
def test_tools_in_loop(self, engine):
|
|
tpl = "{% for item in tools.memory.notes %}{{ item }}{% endfor %}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "memory" in result
|
|
assert "notes" in result["memory"]
|
|
|
|
def test_tools_in_conditional(self, engine):
|
|
tpl = "{% if tools.search.results %}found{% endif %}"
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert "search" in result
|
|
assert "results" in result["search"]
|
|
|
|
def test_full_chain_does_not_record_bare_tool(self, engine):
|
|
# The intermediate ``tools.memory`` node must not register as a
|
|
# bare-tool usage (None = "run all actions") when the template
|
|
# only references one specific action.
|
|
tpl = (
|
|
"{% if tools.memory.memory_view %}"
|
|
"{{ tools.memory.memory_view }}"
|
|
"{% endif %}"
|
|
)
|
|
result = engine.extract_tool_usages(tpl)
|
|
assert result == {"memory": {"memory_view"}}
|