# Prompt Organizer Tools

This document describes the currently active built-in tools.

## Render Markdown

### Current Runtime Behavior

- Parser: `markdown-it` (local bundle)
- Plugins: footnote, task lists, emoji
- Syntax highlighting: highlight.js
- **Mermaid diagrams:** Fenced blocks tagged `mermaid` render as SVG in the live preview. The vendored `@mermaid-js/tiny` build (`temp_libs/mermaid.tiny.min.js`, ~1.7 MB) lazy-loads on first use (works offline and on `file://` when the bundle is present). Supports common types (flowchart, sequence, class, state, ER, gantt, pie, etc.). **Not supported:** mindmap, architecture diagrams, and KaTeX math in diagram labels. Unsupported types show an inline message before the library loads. Word export rasterizes rendered SVG to PNG when possible; otherwise the diagram source is exported as a monospace code block.
- **PlantUML:** Not supported in Render Markdown. In-browser PlantUML (CheerpJ) requires `http://` or `https://`, multi-megabyte runtime assets, and separate licensing; it is deferred unless those constraints are explicitly accepted for a future tool.
- Word export: UI path is corporate-only flow (`downloadWord()` wiring), with `docx.js` lazy-loaded on mode entry/first export
- Word export converts the same rendered Markdown preview HTML into `.docx` structures where practical, including headings, inline styles, links, lists/task lists, tables, code blocks, blockquotes/callouts, horizontal rules, and a rendered footnotes section
- Images are embedded when they are data URLs or fetchable same-origin resources; otherwise export keeps the image alt text and source hyperlink instead of literal Markdown image syntax
- Fallback behavior exists when docx is unavailable (HTML export path)

### Actions

- Load example markdown
- Import markdown/text file
- Copy rendered HTML
- Export as Word
- Clear input

## Token Estimator

Estimates token usage for AI models from pasted text, with live recalculation. All processing is local.

### Model catalog

The in-app reference-model selector and multi-model comparison table use the catalog defined in `js/tool-tokens.js` (`MODEL_CATALOG` and the Pro `MODEL_CATALOG_EXTENDED` entries). Each row includes provider grouping, context window, and per-1K USD list prices used for cost cards. Catalog names and pricing metadata are intentional product copy (see unit tests in `test/unit/token-tokenizer.test.js` and entitlements coverage); they are not fetched from the network at runtime. The “Pricing as of” label in Token Estimator reflects `_PRICING_AS_OF` in the same module. Pro tier unlocks the extended catalog via the `tokens.modelCatalogExtended` capability.

### Current Runtime Behavior

- **Heuristic tokenizer:** Text is split on real pre-token boundaries (words, contractions, digit groups, punctuation runs, CJK characters, whitespace) and each chunk's cost is estimated; it does not fabricate sub-word splits. The representative sentence "The quick brown fox jumps over the lazy dog." pins to 10 tokens (a regression guard in `test/unit/token-tokenizer.test.js`).
- **Context-window meter:** A reference-model selector (grouped by provider) drives a progress bar showing tokens used vs. the model's context window. Near-limit (≥90%) and over-limit (≥100%) states are shown with both color and text ("near limit" / "over limit") so the cue is not color-only.
- **Multi-model comparison table:** Lists each model with its calibrated token count and context fit; rows are click/keyboard-selectable to set the reference model. Over-budget rows carry an "Over" text badge. On narrow screens the table collapses to a stacked card layout.

### Pro Tier

- **Model analysis** (`tokens.analysis.advanced`): Model-specific token estimate plus input/output/total cost cards. Costs use a shared formatter — `$0.00`, two significant figures for sub-cent values (e.g. `$0.0012`), and `<$0.0001` for vanishing amounts (never a flat `$0.000000`). Pricing is list-price metadata ("USD per 1K tokens", with an "as of" date) and a per-segment breakdown (splits on blank lines or list markers).
- **Extended model catalog** (`tokens.modelCatalogExtended`): Adds specialized model variants to the selector and comparison table; the table also gains an input-cost column.

### Actions

- Paste/type text for live estimation
- Pick a reference model or click a comparison row to set it
- Clear input (undoable)

## Text to Image

Converts pasted SVG markup into a local PNG or JPEG output without network dependencies.

### Actions

- Load sample SVG input
- Validate SVG safety and structure before conversion
- Configure output format, background, width, and padding
- Render preview and download image output
- Generate and copy a full inline-data `<img>` tag

### Safety + Offline Behavior

- Strictly blocks unsafe SVG constructs (`<script>`, `on*` handlers, and external resource URLs)
- Supports `file://` and local HTTP usage with browser-native APIs only
- Keeps output entirely local (no remote fetches)

## File Concatenator

Processes supported files and emits XML-tagged merged content for AI context workflows.

Current supported extension set is derived from validator/runtime config (includes CSV, TXT, MD, DOCX, XLSX/XLS, PDF, PPTX, RTF, ODT/ODS/ODP, EML).

Heavy format parsers are lazy-loaded through `PO.modules.toolDependencies`:
- `pdf.js` for PDF
- `mammoth.js` for DOCX
- `xlsx` for XLSX/XLS
- `jszip` for PPTX and OpenDocument formats (ODT/ODS/ODP)

## Prompt Lab

### Current Runtime Behavior

BYOK (bring your own key) prompt testing and metaprompting. Two views, switched with a segmented toggle (persisted in `promptLabPrefs.view`):

- **Metaprompting (Prompt Improver)**: a model of your choice rewrites the prompt for clarity, structure, and constraints. The instructions template is user-editable (reset-to-default available). Results stream in with a word-level diff against the original, plus Copy / Replace input / Save to library actions.
- **Model Comparison**: runs one prompt against up to 4 models side by side with streaming output, latency, and estimated output tokens.

Shared prompt card: library dropdown, auto-detected system prompt box (appears only when the loaded prompt has one; manual Add/Remove otherwise), and `{{ name | default }}` variable fill inputs (values substituted at run time; Improve keeps placeholders intact).

Providers: Anthropic, OpenAI, Google Gemini, OpenRouter (cloud, Pro via `lab.cloudProviders`), plus Ollama and LM Studio on localhost (free). All calls are browser-direct (`js/byok.js`); no relay server. API keys are AES-GCM encrypted in a dedicated IndexedDB store, never logged, and never included in vault exports or transfers (see `docs/security.md`).

A browser-local history (capped at 20 entries, `promptLabHistory` preference) records runs and improvements with restore and clear actions; it is never written to the vault.

Module: `js/tool-prompt-lab.js` (UI) and `js/byok.js` (provider layer). Cloud calls require the network; local providers work offline-adjacent against localhost servers.
