Skip to content

fix(templates): surface local templates in /api/templates + reject unprefixed (#843) - #844

Merged
vybe merged 1 commit into
devfrom
feature/843-list-local-templates
May 14, 2026
Merged

vybe merged 1 commit into
devfrom
feature/843-list-local-templates

Conversation

@dolho

@dolho dolho commented May 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Two related defects: the UI never saw local templates, and the API silently accepted broken template names.

Before:

  • GET /api/templates returned only GitHub-configured templates. The 10+ bundled local templates under config/agent-templates/ were invisible to the frontend — the "Local templates" section in CreateAgentModal.vue rendered empty even on installs where local templates were obviously present on disk.
  • POST /api/agents with template: "dd-compliance" (missing local: prefix) silently fell through the elif chain in crud.py:129 and created a blank agent — 200 OK, no warning, no metadata applied.

After:

  • GET /api/templates returns local + GitHub (27 entries on this dev box; 21 local + 6 GitHub).
  • GET /api/templates/local:<name> resolves a single local template.
  • POST /api/agents with an unprefixed template returns 400 with a clear actionable message ("Template '' must start with 'local:' … Example: 'local:'"). Empty template still creates a blank agent (unchanged).

Files

  • services/template_service.py — new get_local_templates() + get_local_template(id). Entries shape-match _build_template (GitHub entries) so the frontend handles both sources identically. get_all_templates() merges local first.
  • routers/templates.py — single-template GET now resolves local: ids in addition to github:.
  • services/agent_service/crud.py:129 — explicit prefix validation before the elif chain. 400 on unrecognised prefix; empty template still allowed.

Live verification

GET /api/templates                         → 27 entries (21 local + 6 github), was 6
GET /api/templates/local:dd-compliance    → full metadata incl. capabilities, use_cases
POST /api/agents  template="dd-compliance" → 400 with actionable message
POST /api/agents  template=""              → 200 (blank agent, unchanged)
POST /api/agents  template="local:dd-..."  → 200 (no regression)

Tests

tests/unit/test_local_templates_listing.py — 11 unit tests:

  • scan happy-path
  • skip directories without template.yaml
  • skip directories with malformed YAML
  • empty / missing directory
  • non-directory entries ignored
  • deterministic alphabetical ordering
  • single-id GET (get_local_template)
  • wrong-prefix rejection (github: and unprefixed)
  • display_name fallback chain (yaml display_name → yaml name → dirname)
  • description fallback (description → tagline)

All 11 pass locally. Stdlib-only via the importlib loader pattern other tests use.

Test plan

  • Live smoke covers list, single-GET, all three create variants (unprefixed/empty/prefixed)
  • 11 unit tests pass
  • Lint clean (tests/lint_sys_modules.py — uses monkeypatch.setitem)
  • CI: full 6-seed pytest matrix + regression diff

Related to #843

🤖 Generated with Claude Code

…prefixed (#843)

Before this PR:

- `GET /api/templates` returned only GitHub-configured templates. The
  10+ local templates bundled under `config/agent-templates/` (dd-*,
  demo-analyst, etc.) were invisible to the frontend. The "Local
  templates" section in `CreateAgentModal.vue:106` rendered empty even
  though those templates work fine via the API when called with a
  `local:` prefix.
- `POST /api/agents` with an unprefixed template name (e.g.
  `"dd-compliance"` instead of `"local:dd-compliance"`) silently
  fell through the elif chain in `crud.py:129` and created a blank
  agent — 200 OK, no warning, but no template.yaml, no MCP servers,
  no metadata. The user only noticed when the agent didn't act like
  the template promised.

What changes

- `services/template_service.py`:
  - New `get_local_templates()` scans `/agent-configs/templates/`
    (or `./config/agent-templates/` outside the container), parses
    each `template.yaml`, and returns entries shaped identically to
    GitHub-template entries — same `id`/`display_name`/`description`/
    `source` keys so the existing frontend rendering at
    `CreateAgentModal.vue:117` handles both without a per-source
    branch. Each entry's id is `local:<dirname>`.
  - New `get_local_template(id)` resolves a single `local:<name>` id
    so the per-template GET endpoint works for both sources.
  - `get_all_templates()` returns local + GitHub (local first).
- `routers/templates.py`:
  - List endpoint unchanged at the call site — gets the merged list
    via `get_all_templates()`.
  - Single-template GET now also handles `local:` ids.
- `services/agent_service/crud.py:129`:
  - Added an explicit prefix validation before the elif chain. Empty
    `template` (blank agent) is still allowed. Unrecognised prefix
    returns 400 with a message that names both valid prefixes and
    suggests the most likely correction (`local:<input>`).

Live verification

- `GET /api/templates` now returns 27 entries on this dev box (21
  local + 6 GitHub). Pre-PR: 6 (GitHub only).
- `GET /api/templates/local:dd-compliance` returns the full
  template metadata including `capabilities` and `use_cases`.
- `POST /api/agents` with `template: "dd-compliance"` (no prefix)
  → 400 with: "Template 'dd-compliance' must start with 'local:'
  … Example: 'local:dd-compliance' or 'github:owner/repo'."
- `POST /api/agents` with empty `template` → 200 (blank agent)
- `POST /api/agents` with `template: "local:dd-compliance"` → 200
  (no regression on the working path)

Tests

`tests/unit/test_local_templates_listing.py` — 11 unit tests
covering scan happy-path, skip-no-yaml, skip-malformed-yaml,
empty-dir, files-not-dirs, deterministic ordering, single-id
lookup, wrong-prefix rejection, display-name + description
fallback chains. Run stdlib-only via the importlib pattern other
backend-internal tests use.

Related to #843

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean two-for-one fix. Shape-matched local entries, correct path resolution, actionable 400 message, lint-compliant tests. LGTM.

@vybe
vybe merged commit bdf3171 into dev May 14, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants