Skip to content

Feature/prompt templates and versioning#155

Merged
Sanjusha-tridz merged 11 commits into
developfrom
feature/prompt-templates-and-versioning
Feb 25, 2026
Merged

Feature/prompt templates and versioning#155
Sanjusha-tridz merged 11 commits into
developfrom
feature/prompt-templates-and-versioning

Conversation

@esafwan
Copy link
Copy Markdown
Contributor

@esafwan esafwan commented Feb 20, 2026

Summary

Introduces a comprehensive prompt template management system for agents, enabling reusable prompt templates with versioning, forking, and flexible prompt mode switching between local and template-based approaches.

Key Changes

New DocTypes

  • Agent Prompt: Immutable versioned prompt templates with support for:

    • Version history tracking via previous_version and prompt_group fields
    • Forking to create independent lineages
    • Visibility controls (Public/App/Private)
    • Category organization and tagging
    • System vs. user-managed prompts
  • Agent Prompt Category: Hierarchical categorization for prompt templates with icon and color support

Agent Enhancements

  • Added prompt_mode field to switch between "Local" (inline instructions) and "Template" (linked prompt) modes
  • Added template linking fields: agent_prompt, prompt_version_locked, template_version_at_attach, copied_from_prompt
  • Updated validation to support both prompt modes
  • Modified plan generation to use centralized prompt resolution

New API Module (huf/ai/prompt_api.py)

Whitelisted server-side operations for prompt management:

  • create_new_version(): Create immutable versions with automatic agent link updates for unlocked agents
  • fork_prompt(): Create independent prompt lineages
  • detach_from_template(): Convert template-mode agents to local mode with traceability
  • save_as_template(): Save agent instructions as reusable templates
  • attach_template(): Link agents to templates with optional version locking
  • get_version_history(): Retrieve full version lineage

Prompt Resolution (huf/ai/prompt_resolver.py)

Centralized resolve_prompt() function that:

  • Handles both Local and Template modes transparently
  • Supports version-locked agents (fixed to specific versions)
  • Automatically uses latest versions for unlocked agents
  • Provides consistent prompt resolution across sync runs, streaming, and orchestration

Integration Updates

  • Updated agent_integration.py, agent_scheduler.py, and orchestrator.py to use centralized prompt resolution instead of direct instructions field access

Implementation Details

  • Prompt versioning uses an immutable model where new versions create new documents rather than modifying existing ones
  • prompt_group field ties all versions of a prompt together for efficient history queries
  • Version locking allows agents to stay on specific prompt versions while others auto-upgrade
  • Backward compatible: agents default to "Local" mode using existing instructions field
  • System prompts are protected from user versioning

esafwan and others added 11 commits February 20, 2026 01:51
Introduce Agent Prompt Category as a standalone DocType for organizing
and grouping prompt templates. Supports hierarchical categories via
parent_category self-link, with icon and color fields for UI display.

This is the first step toward treating prompts as first-class reusable
artifacts in HUF, preparing for prompt marketplace and library UX.
Introduce Agent Prompt as a first-class reusable artifact in HUF.
Implements the immutable version model (Git-like) where each update
creates a new row linked via previous_version. Key design decisions:

- prompt_group: shared identifier across all versions of a lineage
- forked_from: tracks lineage when prompts are forked/copied
- is_latest: marks the current head version in each lineage
- visibility: Public/App/Private for future marketplace support
- is_system: protects HUF-managed prompts from user edits
- slug: URL-friendly identifier, auto-generated from title
- autoname: hash - supports immutable versioning without name conflicts

This model supports version pinning, prompt marketplace, cross-app
reuse, diffing, and audit trails.
Add prompt template support to Agent with backward-compatible defaults:

- prompt_mode: Select (Local/Template), defaults to "Local"
- agent_prompt: Link to Agent Prompt (visible in Template mode)
- prompt_version_locked: lock agent to a specific template version
- template_version_at_attach: records version at time of attachment
- copied_from_prompt: hidden traceability link for detach operations

Existing behavior is preserved: all current agents default to "Local"
mode and continue using the instructions field directly. The
instructions field gains depends_on for Local mode but remains
functional for all backend reads.

Validation updated to support both modes: Template mode requires
agent_prompt link, Local mode requires instructions text.
Add prompt_resolver.py as the single source of truth for resolving an
agent's effective prompt. All backend paths (sync runs, streaming,
triggers, orchestration) now go through resolve_prompt() instead of
reading agent_doc.instructions directly.

Resolution rules:
- Local mode: returns agent_doc.instructions (backward compatible)
- Template mode (unlocked): loads prompt_body from linked Agent Prompt
- Template mode (locked): walks the version lineage via prompt_group
  to find the exact version matching template_version_at_attach

Also updates AgentManager.create_agent() and Agent.generate_default_plan()
to use resolve_prompt() for consistent behavior.
Add prompt_api.py with server-side operations for the prompt template
lifecycle:

- create_new_version: immutable versioning — marks old as not-latest,
  creates new row, auto-updates unlocked agents
- fork_prompt: creates independent copy with forked_from lineage
- detach_from_template: copies resolved prompt to local, records
  copied_from_prompt for traceability
- save_as_template: creates Agent Prompt from agent's local instructions
- attach_template: switches agent to Template mode with optional
  version locking
- get_version_history: returns full version lineage via prompt_group

All methods are @frappe.whitelist() and follow Frappe patterns for
permissions and error handling.
Update remaining places that read agent_doc.instructions directly to
use resolve_prompt() instead:

- agent_scheduler.py: scheduled agent runs now resolve templates
- orchestrator.py: plan recreation uses resolved prompt
- agent.py on_update: detect prompt changes from template switches
- agent.py after_insert: pass resolved prompt to orchestration

This ensures agents in Template mode work correctly across all
execution paths: sync runs, streaming, scheduled triggers, and
orchestration planning.
- Add 'agent_prompt.js' with client script for the Agent Prompt DocType
- Expose 'Create New Version' action for saved, non-system, latest prompts
- Expose 'Fork Prompt' action allowing independent lineages
- Wire UI buttons to backend whitelisted API ('huf.ai.prompt_api')
- Add confirmation dialogs and success routing to newly generated records
- Use 'resolve_prompt(agent_doc)' in agent_hooks.py instead of accessing 'agent_doc.instructions' directly.
- Ensures DocEvent triggers now correctly resolve and pass Template mode instructions to the background worker, bringing it in line with 'agent_scheduler.py' and 'run_agent_sync'.
- Add 'prompt_template' link field to 'Agent Run' DocType to track which prompt template was used during the execution.
- Update 'run_agent_sync' in 'agent_integration.py' to populate the 'prompt_template' field only when the agent's 'prompt_mode' is set to 'Template'.
@Sanjusha-tridz Sanjusha-tridz marked this pull request as ready for review February 25, 2026 16:01
@Sanjusha-tridz Sanjusha-tridz merged commit c9d90f2 into develop Feb 25, 2026
@Sanjusha-tridz Sanjusha-tridz deleted the feature/prompt-templates-and-versioning branch February 26, 2026 08:33
esafwan pushed a commit that referenced this pull request Mar 10, 2026
…ersioning

Feature/prompt templates and versioning
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