# anysphere/priompt architecture

> Architecture diagram of the GitHub repository anysphere/priompt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anysphere/priompt
- Repository: https://github.com/anysphere/priompt

## Overview

##### Main Components
- Priompt Library: This is the core package (found under the “priompt” directory) that contains the primary logic for parsing JSX-based prompt components, calculating priorities (using absolute “p” and relative “prel” values), and ultimately rendering the prompt with appropriate token budgeting.
- Tokenizer (tiktoken-node): This module, which includes Rust components and multiple npm packages for different operating systems, deals with tokenization. It underpins features such as sourcemap generation and token counting in prompt rendering.
- Examples: The examples folder serves as usage demonstrations (with .yaml files and accompanying configuration) to show how to build prompts using the library.

##### Relationships and Interactions
- The Priompt Library (core engine) receives a JSX tree of prompt components and:
- Processes the component tree
- Evaluates each component’s priority (using scopes, first, empty, etc.)
- Applies the binary search logic (as described in the README) to determine which parts can be included within a given token limit (T)
- Optionally generates a sourcemap linking prompt text back to the source JSX nodes.
- It collaborates with the Tokenizer (tiktoken-node) module to measure the tokens in the rendered output.
- The Priompt Preview acts as an interactive layer on top of Priompt. It uses modern web build tools (Vite, Tailwind CSS) to offer a dashboard where users can see how their prompt is constructed and debug issues (latency, token usage, etc.).
- Examples illustrate typical usage patterns and demonstrate how developers are intended to integrate these components in their application.

##### Architectural Patterns and Technologies
- The design is inspired by React – using components, JSX, and a unidirectional data flow.
- There is a clearly defined separation of concerns:
- The rendering engine (Priompt) handling the transformation from JSX to final text prompt.
- The preview dashboard (Priompt Preview) offering a consumer-friendly debugging environment.
- The tokenization service (tiktoken-node) providing necessary low-level performance optimizations.
- Technologies include TypeScript (noted from .ts/.tsx files), Vite, PostCSS, Tailwind (for Priompt Preview), and Rust (for tiktoken-node).

##### Final Suggested Elements
- Priompt Core Engine (with sub-modules: JSX Renderer, Priority Evaluator, Binary Search Render, Sourcemap Generator)
- Priompt Preview Dashboard (UI components, configuration, Vite build pipeline)
- Tokenizer Service (tiktoken-node, Rust backend, OS-specific npm packages)
- Examples (sample usage and configuration files)
- Data flow from the UI (Preview) to the Core Engine.
- Dependency from the Core Engine to the Tokenizer for prompt sizing.
- Feedback from Example components into the Core Engine for demonstration.
- Additional annotations (notes on caching, performance constraints, future features like isolated token limits) as minor callouts if needed.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Layer: Examples
    subgraph "Examples"
        D1["Usage Demonstrations"]:::examples
        D2["Prompt_Configurations"]:::examples
    end

    %% Second Layer: Priompt Preview Dashboard
    subgraph "Priompt Preview Dashboard"
        B1["UI_Components"]:::ui
        B2["Vite_Build_Pipeline"]:::ui
    end

    %% Third Layer: Priompt Core Engine
    subgraph "Priompt Core Engine"
        A1["JSX_Parsing"]:::core
        A2["Priority_Evaluation"]:::core
        A3["Binary_Search_Render"]:::core
        A4["Sourcemap_Generation"]:::core
    end

    %% Fourth Layer: Tokenizer Service
    subgraph "Tokenizer Service (tiktoken-node)"
        C1["Rust_Backend"]:::tokenizer
        C2["OS-specific_Packages"]:::tokenizer
    end

    %% External Interactions
    D1 -- "feeds" --> A1
    D2 -- "feeds" --> A1
    B1 -- "renders" --> A1

    %% Internal Core Engine Flow
    A1 --> A2
    A2 --> A3
    A3 --> A4

    %% Core Engine Consults Tokenizer Service
    A2 -- "tokenizes" --> C1
    A4 -- "tokenizes" --> C1

    %% Click Events for Priompt Core Engine
    click A1 "https://github.com/anysphere/priompt/blob/main/priompt/src/index.ts"
    click A2 "https://github.com/anysphere/priompt/blob/main/priompt/src/components.tsx"
    click A3 "https://github.com/anysphere/priompt/blob/main/priompt/src/lib.ts"
    click A4 "https://github.com/anysphere/priompt/blob/main/priompt/src/preview.ts"

    %% Click Events for Priompt Preview Dashboard
    click B1 "https://github.com/anysphere/priompt/blob/main/priompt-preview/src/App.tsx"
    click B2 "https://github.com/anysphere/priompt/blob/main/priompt-preview/src/main.tsx"

    %% Click Events for Tokenizer Service
    click C1 "https://github.com/anysphere/priompt/blob/main/tiktoken-node/src/lib.rs"
    click C2 "https://github.com/anysphere/priompt/blob/main/tiktoken-node/index.js"

    %% Click Events for Examples
    click D1 "https://github.com/anysphere/priompt/blob/main/examples/priompt/ArvidStory/example01.yaml"
    click D2 "https://github.com/anysphere/priompt/blob/main/examples/src/prompt.tsx"

    %% Styles
    classDef core fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef ui fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef tokenizer fill:#f08080,stroke:#333,stroke-width:2px;
    classDef examples fill:#ffd700,stroke:#333,stroke-width:2px;
```
