nanobot/nanobot/utils/prompt_templates.py
Jack Lu d436a1d678 feat: integrate Jinja2 templating for agent responses and memory consolidation
- Added Jinja2 template support for various agent responses, including identity, skills, and memory consolidation.
- Introduced new templates for evaluating notifications, handling subagent announcements, and managing platform policies.
- Updated the agent context and memory modules to utilize the new templating system for improved readability and maintainability.
- Added a new dependency on Jinja2 in pyproject.toml.
2026-04-04 14:18:22 +08:00

36 lines
1.2 KiB
Python

"""Load and render agent system prompt templates (Jinja2) under nanobot/templates/.
Agent prompts live in ``templates/agent/`` (pass names like ``agent/identity.md``).
Shared copy lives under ``agent/_snippets/`` and is included via
``{% include 'agent/_snippets/....md' %}``.
"""
from functools import lru_cache
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader
_TEMPLATES_ROOT = Path(__file__).resolve().parent.parent / "templates"
@lru_cache
def _environment() -> Environment:
# Plain-text prompts: do not HTML-escape variable values.
return Environment(
loader=FileSystemLoader(str(_TEMPLATES_ROOT)),
autoescape=False,
trim_blocks=True,
lstrip_blocks=True,
)
def render_template(name: str, *, strip: bool = False, **kwargs: Any) -> str:
"""Render ``name`` (e.g. ``agent/identity.md``, ``agent/platform_policy.md``) under ``templates/``.
Use ``strip=True`` for single-line user-facing strings when the file ends
with a trailing newline you do not want preserved.
"""
text = _environment().get_template(name).render(**kwargs)
return text.rstrip() if strip else text