Skip to content

Improve prompt templates#14

Merged
ScriptSmith merged 4 commits intomainfrom
prompt-templates
Mar 18, 2026
Merged

Improve prompt templates#14
ScriptSmith merged 4 commits intomainfrom
prompt-templates

Conversation

@ScriptSmith
Copy link
Owner

No description provided.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 18, 2026

Greptile Summary

This PR renames the prompts concept to templates across the entire stack — database schema, Rust backend (models, repos, services, routes), OpenAPI spec, and React frontend — and adds meaningful new functionality: a list_by_org query that returns templates from all ownership scopes within an org (org, team, project, and user-owned), a template variable system ({{var}} syntax with metadata-driven fields), delete-from-popover in the chat input, and Templates management tabs in the Org/Team/Project admin detail pages.

Key changes:

  • Database: prompts table and prompt_owner_type enum renamed to templates/template_owner_type. The change is applied by modifying the existing initial migration rather than adding a new one, which will cause sqlx checksum-mismatch errors on any deployment that has already run the migration.
  • Backend: New list_by_org endpoint replaces the old list_by_owner(Organization, ...) for the org-level list route; it now returns all templates visible within the org hierarchy via subquery joins.
  • Frontend TemplatesButton: Adds delete-without-confirmation — unlike every other destructive action in the codebase, the trash icon fires immediately with no confirmation dialog.
  • Frontend cache invalidation: The new AdminPromptFormModal correctly invalidates all four cache keys on create/update; the user-facing PromptFormModal only invalidates templateListByOrg, which is correct since it always creates org-owned templates.
  • src/config/limits.rs: max_prompts_per_owner config key renamed to max_templates_per_owner — a breaking change for any deployment using a custom limits config.

Confidence Score: 2/5

  • Not safe to merge — the in-place modification of the initial migration will break all existing deployments on startup.
  • The rename is thorough and the new functionality (list_by_org cross-scope query, template variables, admin UI tabs) is well-implemented. However, the migration strategy is fundamentally broken for any existing deployment: modifying the initial migration file instead of adding a new one causes sqlx to detect a checksum mismatch and refuse to start. Additionally, the max_prompts_per_ownermax_templates_per_owner config rename is a silent breaking change. The delete-without-confirmation in TemplatesButton is a UX regression. These issues block a safe merge.
  • Both migrations_sqlx/postgres/20250101000000_initial.sql and migrations_sqlx/sqlite/20250101000000_initial.sql need a new additive migration file instead of in-place edits.

Important Files Changed

Filename Overview
migrations_sqlx/postgres/20250101000000_initial.sql Initial migration modified in-place to rename promptstemplates and prompt_owner_typetemplate_owner_type; will cause sqlx checksum mismatch failures on any existing deployment.
migrations_sqlx/sqlite/20250101000000_initial.sql Same in-place migration modification as the Postgres file; existing SQLite deployments will fail to start due to sqlx checksum mismatch.
src/db/repos/templates.rs New TemplateRepo trait replacing PromptRepo; adds list_by_org method that returns templates across all ownership scopes (org, team, project, user) within an organization.
src/db/postgres/templates.rs Postgres implementation of TemplateRepo; list_by_org correctly uses named $1 parameter for org_id across the filter subqueries with proper cursor pagination bindings.
src/db/sqlite/templates.rs SQLite implementation of TemplateRepo; list_by_org uses ORG_SCOPE_FILTER const with 4 positional ? parameters; binding order is correct. Comprehensive tests updated.
src/routes/admin/templates.rs list_by_org route now calls services.templates.list_by_org(org.id, params) instead of filtering by organization owner type — the new cross-scope query is wired up correctly. All CRUD routes updated.
ui/src/components/PromptsButton/PromptsButton.tsx Renamed to TemplatesButton; adds variable substitution modal, delete button, and inline description rendering. Delete fires immediately without confirmation, unlike all other delete flows in the codebase.
ui/src/components/Admin/PromptFormModal/PromptFormModal.tsx New admin-scoped template form modal; correctly invalidates all four cache keys (templateListByOrg, templateListByTeam, templateListByProject, templateListByUser) on both create and update.
ui/src/hooks/useUserPrompts.ts New useUserTemplates hook fetches user-owned templates and org-scoped templates separately, deduplicates by ID, and exposes hasMore. Backwards-compat alias useUserPrompts preserved.
ui/src/lib/templateVariables.ts New utility library for template variable parsing, {{var}} substitution, and validation. Clean, well-typed implementation.

Sequence Diagram

sequenceDiagram
    participant UI as UI (TemplatesButton / AdminFormModal)
    participant API as Admin API (/admin/v1/templates)
    participant SVC as TemplateService
    participant DB as TemplateRepo (Postgres/SQLite)

    Note over UI,DB: Create Template
    UI->>API: POST /admin/v1/templates {owner, name, content, metadata}
    API->>SVC: create(CreateTemplate)
    SVC->>DB: create(input)
    DB-->>SVC: Template
    SVC-->>API: Template
    API-->>UI: 201 Template

    Note over UI,DB: List by Org (new cross-scope query)
    UI->>API: GET /admin/v1/organizations/{slug}/templates
    API->>SVC: list_by_org(org_id, params)
    SVC->>DB: list_by_org(org_id, params)
    Note over DB: Joins org_memberships, teams, projects<br/>to return org + team + project + user templates
    DB-->>SVC: ListResult[Template]
    SVC-->>API: ListResult[Template]
    API-->>UI: TemplateListResponse {data, pagination}

    Note over UI,DB: Apply Template with Variables
    UI->>UI: User selects template
    alt Has variables in metadata
        UI->>UI: Show TemplateVariableForm modal
        UI->>UI: substituteVariables(content, values)
    end
    UI->>UI: setPendingPrompt(content)

    Note over UI,DB: Delete Template
    UI->>API: DELETE /admin/v1/templates/{id}
    API->>SVC: delete(id)
    SVC->>DB: soft-delete (set deleted_at)
    DB-->>SVC: ()
    SVC-->>API: ()
    API-->>UI: 200
    UI->>UI: invalidate templateListBy* cache keys
Loading

Comments Outside Diff (1)

  1. migrations_sqlx/postgres/20250101000000_initial.sql, line 1123-1170 (link)

    P0 Modified initial migration will break existing deployments

    This PR modifies the existing initial migration file rather than adding a new migration. sqlx tracks applied migrations by checksum — any deployment that has already run this migration will see a checksum mismatch and fail to start with an error like "migration 20250101000000 was previously applied but has been modified".

    The rename from promptstemplates and prompt_owner_typetemplate_owner_type requires a new migration file (e.g., 20250115000000_rename_prompts_to_templates.sql) containing:

    -- Postgres
    ALTER TABLE prompts RENAME TO templates;
    ALTER TYPE prompt_owner_type RENAME TO template_owner_type;
    ALTER INDEX idx_prompts_owner RENAME TO idx_templates_owner;
    ALTER INDEX idx_prompts_owner_active RENAME TO idx_templates_owner_active;
    ALTER INDEX idx_prompts_name RENAME TO idx_templates_name;
    ALTER TRIGGER update_prompts_updated_at ON templates RENAME TO update_templates_updated_at;

    The same applies to migrations_sqlx/sqlite/20250101000000_initial.sql (SQLite uses CREATE TABLE templates AS SELECT ... FROM prompts + DROP TABLE prompts pattern since it lacks RENAME TABLE in older versions, or ALTER TABLE prompts RENAME TO templates in SQLite 3.25+).

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: migrations_sqlx/postgres/20250101000000_initial.sql
    Line: 1123-1170
    
    Comment:
    **Modified initial migration will break existing deployments**
    
    This PR modifies the existing initial migration file rather than adding a new migration. `sqlx` tracks applied migrations by checksum — any deployment that has already run this migration will see a checksum mismatch and fail to start with an error like `"migration 20250101000000 was previously applied but has been modified"`.
    
    The rename from `prompts``templates` and `prompt_owner_type``template_owner_type` requires a new migration file (e.g., `20250115000000_rename_prompts_to_templates.sql`) containing:
    
    ```sql
    -- Postgres
    ALTER TABLE prompts RENAME TO templates;
    ALTER TYPE prompt_owner_type RENAME TO template_owner_type;
    ALTER INDEX idx_prompts_owner RENAME TO idx_templates_owner;
    ALTER INDEX idx_prompts_owner_active RENAME TO idx_templates_owner_active;
    ALTER INDEX idx_prompts_name RENAME TO idx_templates_name;
    ALTER TRIGGER update_prompts_updated_at ON templates RENAME TO update_templates_updated_at;
    ```
    
    The same applies to `migrations_sqlx/sqlite/20250101000000_initial.sql` (SQLite uses `CREATE TABLE templates AS SELECT ... FROM prompts` + `DROP TABLE prompts` pattern since it lacks `RENAME TABLE` in older versions, or `ALTER TABLE prompts RENAME TO templates` in SQLite 3.25+).
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: migrations_sqlx/postgres/20250101000000_initial.sql
Line: 1123-1170

Comment:
**Modified initial migration will break existing deployments**

This PR modifies the existing initial migration file rather than adding a new migration. `sqlx` tracks applied migrations by checksum — any deployment that has already run this migration will see a checksum mismatch and fail to start with an error like `"migration 20250101000000 was previously applied but has been modified"`.

The rename from `prompts``templates` and `prompt_owner_type``template_owner_type` requires a new migration file (e.g., `20250115000000_rename_prompts_to_templates.sql`) containing:

```sql
-- Postgres
ALTER TABLE prompts RENAME TO templates;
ALTER TYPE prompt_owner_type RENAME TO template_owner_type;
ALTER INDEX idx_prompts_owner RENAME TO idx_templates_owner;
ALTER INDEX idx_prompts_owner_active RENAME TO idx_templates_owner_active;
ALTER INDEX idx_prompts_name RENAME TO idx_templates_name;
ALTER TRIGGER update_prompts_updated_at ON templates RENAME TO update_templates_updated_at;
```

The same applies to `migrations_sqlx/sqlite/20250101000000_initial.sql` (SQLite uses `CREATE TABLE templates AS SELECT ... FROM prompts` + `DROP TABLE prompts` pattern since it lacks `RENAME TABLE` in older versions, or `ALTER TABLE prompts RENAME TO templates` in SQLite 3.25+).

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: ui/src/components/PromptsButton/PromptsButton.tsx
Line: 6347-6350

Comment:
**No confirmation before template deletion**

The delete button immediately fires the mutation with no confirmation dialog, unlike every other delete action in this codebase (admin pages all use `useConfirm()`). A single misclick on the small trash icon in the popover will permanently delete a template without any warning.

```typescript
const handleDelete = (e: React.MouseEvent, template: Template) => {
  e.stopPropagation();
  deleteMutation.mutate(template.id); // no confirmation
};
```

Consider adding a confirmation step (using `useConfirm()` or an inline `window.confirm`) before calling `deleteMutation.mutate`.

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: "Fix stories"

@ScriptSmith ScriptSmith changed the title Prompt templates Improve prompt templates Mar 18, 2026
@ScriptSmith
Copy link
Owner Author

@greptile-apps

@ScriptSmith ScriptSmith merged commit e6b6a3a into main Mar 18, 2026
21 checks passed
@ScriptSmith ScriptSmith deleted the prompt-templates branch March 19, 2026 13:30
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.

1 participant