# Prompt Template Library

## Objective

Keep reusable prompts in one place instead of rewriting the same instructions each time.

A managed collection of named prompt templates, scoped by owner and role, that the app's AI features run from.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find every place in the app where prompt text is currently assembled inline, and move that text into named templates that the feature loads at run time. Leaving one copy behind guarantees the two drift apart.
2. Split each template into a system portion, which only administrators can edit and which users never see, and a task portion that authors may change. The system portion is where safety rules, output shape, and tone live, and a user editing it can disable them by accident.
3. Scope templates to a user, a workspace, or a role using the app's existing permission model, and default a new template to its author rather than to everyone.
4. Record who created and last changed each template, when, and which features use it, so an unused template can be retired and a broken one can be traced back to a change.
5. Placeholder syntax, typing, and validation of substituted values are owned by Prompt Variables; this feature owns storage, scoping, and discovery. Do not build a second substitution mechanism here.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- System instructions and user-editable task content must be stored and rendered separately. A single editable blob lets an author delete the safety and formatting rules without realising the feature depended on them.
- Templates must respect the app's scoping rules, so a workspace template never appears in another workspace and a role-restricted template never appears to users who cannot run it.
- A template with unfilled required variables must be blocked before any request is sent, because a prompt containing literal placeholder text produces confidently wrong output rather than an error.
- A template that references data or a tool the running user cannot access must fail with a clear explanation. Silently dropping the inaccessible part produces an answer built on a gap nobody was told about.
- Ownership and last-updated information must be recorded and displayed, or a library of a hundred templates becomes impossible to audit or prune.
- Deleting or renaming a template that a live feature depends on must be refused or must warn with the list of dependents, rather than breaking the feature at the next run.
- Templates must have a length ceiling, because an unbounded template multiplied by every request is a cost problem before it is a quality problem.
- If the template store is unreachable, the feature must fail with a clear message rather than falling back to an empty prompt.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] All prompt text used by the app lives in the template library, with no inline copies remaining.
- [ ] System instructions are separated from user-editable content and are not visible or editable to ordinary authors.
- [ ] Templates are scoped by user, workspace, and role, and new templates default to their author.
- [ ] Each template shows its owner, last editor, last update, and the features that use it.
- [ ] Deleting or renaming a template in use warns with its dependents rather than silently breaking them.
- [ ] Running a template with missing required values is blocked before any request is made.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
