Prompt Template Library
Pro5 variants: grid card gallery, list view, template editor, picker modal and playground (two templates side by side on one set of variables) — with variable injection, token counter, usage stats, category filter and version history.
import { SvPromptTemplateLibraryComponent } from '@styloviz-pro/prompt-template-library';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
Editor & picker
When to use
Managing reusable prompt templates with variables, versions and usage stats. Teams standardizing prompts across an AI product. For a single inline prompt field, a Text Field or Mention input is enough.
Async states
- loading
- error
- retry
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| loading | boolean | false | True while the data is being fetched. |
| error | SvDataError | string | null | null | Load failure. Use `toSvDataError()` from `@styloviz/core` to normalize an `HttpErrorResponse`. |
| variant | PromptLibraryVariant | 'grid' | Presentation: gallery, list, editor, picker or side-by-side playground. |
| locale | string | svLocale() | BCP 47 locale for dates and numbers. |
| templates | readonly PromptTemplate[] | [] | The library's templates. Bind it to what the server returns; `DEMO_TEMPLATES` is exported for showcases. |
| compareAId | string (two-way) | '' | Left-hand template of the `playground` comparison. |
| compareBId | string (two-way) | '' | Right-hand template of the `playground` comparison. |
| pricing | SvPromptPricing | { inputPer1M: 3, outputTokens: 400, out… | Prices and expected completion length the comparison is costed against. |
| runsPerMonth | number | 100_000 | Calls a month, for the compounded figure. The per-run difference between two prompts is a fraction of a cent by construction, which is why nobody ever optimises one. This is the number that turns it into something worth acting on. |
| tokenizer | SvPromptTokenizer | null | null | The tokenizer behind every token figure here. A token count is a claim about a tokenizer. Four characters to the token is an English-prose approximation: Chinese runs closer to one character per token, and minified JSON lands somewhere else again, so a library whose authors write in any of those is handing them a budget wrong by a factor rather than a percentage. Pass your model's own counter and the figures become exact; leave it and they stay estimates that **say** so. |
| secretPatterns | readonly SvAiMaskPattern[] | SV_AI_COMMON_PATTERNS | Patterns that must not be saved into a template. A prompt library is where API keys get pasted into a system message and shared with a team. Somebody drops a working key in to see if the call goes through, it works, and the template is saved, named and handed to nine colleagues — with no commit and no code review anywhere in the path. Defaults to the pack's own `SV_AI_COMMON_PATTERNS`, so this is on out of the box rather than being a policy somebody has to remember to set. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| retry | void | The host should re-run the failed load. |
| templateSelect | string | Emitted with the id of the template the user selects/uses. |
| promptCopy | { id: string; prompt: string } | The resolved prompt reached the clipboard. Carries the template id and the exact text written, so a host can log usage or feed it straight to a model without re-resolving the variables itself. |
| copyFailed | string | The clipboard write was refused — an insecure context, or a denied permission. Emitted with the template id so a host can offer a fallback (a select-all, a toast). The component stays silent on its own: claiming a copy that did not happen is worse than saying nothing. |
| variablesReady | TemplateVariablesReadyEvent | Emitted the moment every required variable of the selected template has a value — distinct from `promptCopy`, which only fires once the user actually clicks copy. A host can use this to enable a "Run" button or log template completion without requiring a clipboard write at all. Fires again if the user clears a required field and refills it, and whenever a different template becomes ready. |
| secretScanChange | PromptSecretScanEvent | The secret scan of the selected template changed — including back to clean, so a host can unblock. This component has no save action, so it cannot block one. The host owns saving; this is what it blocks on. Emitted when a template is selected and whenever what the scan found changes, not per keystroke. Also readable at any time as `selectedSecretScan()`. |
| compareAIdChange | string | Emits the new `compareAId` whenever it changes — the output half of `[(compareAId)]`. |
| compareBIdChange | string | Emits the new `compareBId` whenever it changes — the output half of `[(compareBId)]`. |
Wire it to your API
This component is headless about transport: it renders what you give it and reports what the user asks for. Nothing below requires @styloviz/data — drive it from whatever you already use.
Template
Bound to the names this component actually exposes.
Host class
Drive the three async inputs from your own fetch.
This component does not emit a query, so it renders what you give it. Call `load()` on your own trigger and bind `templates`, `loading` and `error`. Normalize failures with `toSvDataError()` from `@styloviz/core`. It derives `retryable` from the status, so a 404 does not get a "Try again" button that can only fail again.
Full walkthrough: Connecting to a backend.
Customization & theming
Gallery, list, editor, picker and side-by-side playground presentations. Variable injection with live token count. Category filter and version history. Emits `templateSelect` with the chosen template id. Secret detection is **on out of the box**, using the pack’s own `SV_AI_COMMON_PATTERNS` rather than a second list that would drift. A prompt library is where API keys get pasted into a system message and shared with a team: somebody drops a working key in to see whether the call goes through, it works, and the template is saved, named and handed to nine colleagues — with no commit and no code review anywhere in the path. `[secretPatterns]` narrows or widens it. The warning renders in the `editor` variant, where the text is edited, as well as in the `grid` footer, and the scan runs on the prompt **as it stands** — a key pasted into a variable is caught as well as one typed into the template. **Decision: the component emits, the host blocks.** It has no save action of its own, so it cannot block one. `(secretScanChange)` emits `{ id, scan }` (`PromptSecretScanEvent`) when a template is selected and whenever what the scan found changes — including back to clean, so a host can unblock — but not per keystroke; `selectedSecretScan()` exposes the same result for a host that reads it at save time. Copy is deliberately not blocked: putting a key on your own clipboard to call your own model is ordinary use, sharing it through the library is not. The warning names the **blast radius, not the rule**: 'matches pattern provider-key' tells the author nothing they can act on. And it is a detector, not a redactor — silently rewriting somebody’s prompt is how a template stops working with no explanation. `[tokenizer]` drives every token figure — the prompt count in the grid footer, list rows, editor header, picker card and both playground sides — and the basis renders beside each one **as text**, not only as a tooltip. The basis travels with the number (`svPromptTokenCount` returns `{ tokens, basis, estimated }`), so a tokenizer that fails falls back to the estimate *and* to the estimate’s label, marked `~`, rather than naming a tokenizer over a figure it never produced. `tokensEst` is shown as the author’s estimate of **output** tokens and is never presented as the count. Copy confirms only once the clipboard write resolves (`promptCopy`). When the clipboard refuses — plain HTTP, where `navigator.clipboard` does not exist, or a denied permission — the library takes the same in-surface fallback as `sv-ai-chat`: the **resolved prompt is selected**, a line beside the button names ⌘C or Ctrl+C for the reader’s platform, the button’s accessible name says the same, and the line is announced once through `SvLiveAnnouncer`. `copyFailed` still fires with the template id, and `copyPrompt()` resolves `true` / `false`. Why the basis matters: a token count is a claim about a tokenizer — four characters per token is an **English-prose** approximation, Chinese runs closer to one character per token and minified JSON lands somewhere else again, so an unlabelled figure is a budget built on somebody else’s language. A tokenizer that throws or returns nonsense falls back to the estimate rather than taking the surface down — these come from a host’s own WASM build as often as not.
Best practices
Do: Show a live token count so authors keep prompts within model limits. Do: Version templates so changes are auditable and reversible. Tip: Use the picker variant to embed template selection inside another flow.
Accessibility
Editor fields are labelled; search inputs carry an aria-label. Category filter reports aria-pressed. Filters and cards are keyboard navigable.
Feedback on Prompt Template Library
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.