# jwangkun/prompt-tools architecture

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

- Interactive diagram: https://gitdiagram.com/jwangkun/prompt-tools
- Repository: https://github.com/jwangkun/prompt-tools

## Overview

##### 项目类型与目的（先定“画什么”）
从 README + 文件树可以明确：这是一个 **Tauri 桌面应用（跨平台）**，目标是 **本地管理 Prompt（创建/编辑/搜索/组织）**，核心强调 **本地优先 + 隐私安全**。技术栈是：
- **UI 前端**：TypeScript + Vite + React（运行在 Tauri WebView 中）
- **本地“后端/核心”**：Rust + Tauri（提供系统能力、数据库访问、窗口/更新等）
- **数据层**：SQLite（本地文件）
- **工程化/脚本**：Node 脚本用于初始化/导入/整理 prompts
- **发布流水线**：GitHub Actions + GitHub Releases（结合 Tauri updater 的可能性）

> 画系统设计图时，要把它当成“桌面端的前后端分层系统”：**Web UI（渲染层） ↔ IPC/命令调用 ↔ Rust Core（业务/系统层） ↔ SQLite（数据层）**，并补齐“更新/发布/初始化脚本”等工程侧组件。

##### 从文件结构拆主组件（按“能独立框起来的东西”尽量拆细）
你可以直接用目录作为第一层组件来源：

##### 前端（WebView 内）
- `src/`
- `main.ts / main.js`：前端入口（React 挂载、路由/状态等通常从这里开始）
- `advanced.ts`：可能是高级功能/设置相关（可在组件图里单独列为“高级配置模块”）
- `updater.ts`, `test-updater.ts`：更新相关 UI/逻辑（与 GitHub Releases/Updater 交互）
- `styles.css`, `src/styles/*`：UI 样式
- `assets/`：静态资源
- `index.html`：Vite/前端入口页面
- `vite.config.*`, `tsconfig.json`：构建配置（通常放在“构建/工具链”图层）

**在架构图中前端至少拆成：**

##### Rust / Tauri 核心（本地系统层）
- `src-tauri/src/`
- `main.rs`：Tauri 应用入口（注册命令、窗口、插件、updater、菜单等）
- `lib.rs`：通常承载命令处理/模块组织
- `database.rs`：数据库访问层（SQLite 的 CRUD、schema、迁移等）
- `src-tauri/tauri.conf.json`：Tauri 配置（权限、updater、bundle、窗口、capabilities）
- `src-tauri/capabilities/default.json`：Tauri v2 capability（权限模型），在图里非常关键：**它决定前端可调用哪些系统能力**
- `src-tauri/build.rs`：构建期逻辑

**在架构图中 Rust 侧建议拆成：**

##### 数据初始化与迁移/导入脚本（工程工具）
根目录这些脚本应该作为“离线工具链”组件单独画一块（很多桌面应用忽略，但这里脚本很明显）：
- `init-database.js`, `init-database-with-prompts.js`：初始化 SQLite、可能填充初始 prompts
- `import-prompts.js`：导入 prompts（从文件/JSON/其他格式）
- `organize-prompts.js/.cjs`：整理/归类 prompts
- `initial-prompts.json`：初始数据
- `verify-setup.cjs`, `test-update.cjs`：验证/测试更新或环境

**在图里把它们标成：**
- “开发者工具/数据维护脚本（Node.js）” → 直接读写 SQLite 或生成导入文件 → 被应用读取

##### CI/CD 与发布（外部系统）
- `.github/workflows/ci.yml`, `release.yml`, `manual-release.yml`
- Releases 页面（README 明确从 GitHub Releases 下载）
- （若启用 Tauri updater）应用内更新会从 Release 拉取元数据/包

**在架构图中把 GitHub 作为外部系统画出来：**
- GitHub Actions（build/sign/bundle）→ GitHub Releases（产物/更新源）→ 用户端应用（下载/更新）

##### 交互关系与数据流（箭头怎么画才“准”）
这是你图里最关键的“箭头层”，建议至少包含以下流向：

##### 返回：SQLite → Rust → IPC → UI 渲染

> 这里要明确：**前端不直接访问 SQLite**；SQLite 在 Rust 侧（更安全/符合 Tauri 模式）。

##### 更新路径（如果启用 updater）
- **UI / Rust Updater 模块** → **GitHub Releases**（检查版本/下载） → **安装替换/重启**
- 图中要标注“网络边界”：本应用主要本地运行，但 updater 会出网。

##### 初始化/导入路径（开发或高级用户）
- **Node scripts** → 生成/写入 **SQLite** 或生成导入 JSON
- **App** 启动 → 读取 SQLite → UI 展示

##### 启动/打包路径（开发构建）
- 开发模式：`pnpm tauri:dev`
- Vite dev server 提供前端资源
- Tauri Host 启动 WebView 指向 dev server
- 生产构建：`pnpm tauri:build`
- Vite build 静态资源
- Tauri bundle（macOS app 等）

把这两条用“虚线/灰色”画在“工程视图”里，和运行时路径区分开。

##### 建议你画的“最准确”的三层图（Principal 工程师视角）
不要试图一张图画完所有细节；建议分三张（或一张主图 + 两张补充）。

目的：把边界说清楚（本地优先、哪些外部依赖）。
包含组件：
- 用户
- Prompt Tools Desktop App
- 本地文件系统（SQLite DB 文件）
- GitHub Releases（下载/更新源）
- （可选）AI 平台：**注意** README 没说会直连 ChatGPT/MJ，因此除非代码里明确有网络调用，不要画“直接调用大模型 API”。

##### 图 2：Container / High-Level Architecture（容器级/子系统级）
把 Desktop App 展开为：
- Web UI（React/Vite）
- Tauri Bridge (IPC/invoke)
- Rust Core（commands + services）
- SQLite（local DB）
- Updater（可作为 Rust Core 的子模块或单独容器）
- Capabilities/Permissions（来自 `src-tauri/capabilities/default.json`，可画为“安全边界/权限控制层”）

关键箭头：
- UI ↔ IPC ↔ Rust Core ↔ SQLite
- Updater ↔ GitHub Releases

用目录/文件映射组件（这是“最落地”的部分）：
- Frontend
- `src/main.ts`（App bootstrap）
- `src/updater.ts`（更新流程）
- `src/advanced.ts`（高级设置/配置）
- `src/styles/*`（UI）
- Rust
- `src-tauri/src/main.rs`（注册命令/启动）
- `src-tauri/src/lib.rs`（模块组织/命令实现入口）
- `src-tauri/src/database.rs`（SQLite DAO/Repository）
- Tooling
- `init-database*.js`, `import-prompts.js`, `organize-prompts.*`, `initial-prompts.json`

并用注释标明：哪些是“运行时必须”，哪些是“开发/维护脚本”。

##### 图上必须标注的技术与模式（让架构“可信”）
结合该项目特点，你的图里建议显式写出：
- **Tauri WebView + Rust Core** 分层（桌面端“前后端分离”）
- **Local-first**：数据只在本地 SQLite（隐私/离线）
- **IPC/Command 模式**：前端通过 invoke 调用 Rust 命令（安全边界）
- **Capabilities 权限模型**（Tauri v2）：限制前端可访问的系统能力
- **Release/Updater 模式**：GitHub Actions → GitHub Releases → App Updater

##### 画图规范（你要求的“尽量拆组件”落地做法）
为了“细且不乱”，建议统一以下视觉规则：

##### 形状/颜色（示例规则）
- 蓝色矩形：运行时应用组件（UI、Rust Core、DB）
- 橙色六边形：外部系统（GitHub、OS）
- 灰色圆角矩形：工程化/脚本/CI（workflows、init scripts）
- 盾牌/边框：安全边界（capabilities/权限）

##### 箭头类型
- 实线箭头：运行时调用/数据流（UI → Rust → DB）
- 虚线箭头：构建/发布流（CI → Release → 下载）
- 双向箭头：读写（DB ↔ Rust）

##### 标签要写到“可实现”
箭头上直接写动词/协议，例如：
- “Tauri invoke(command)”
- “SQL query/transaction”
- “Check update manifest / download bundle”
- “Generate seed prompts / import JSON → SQLite”

##### 一张“主图”你可以怎么排版（推荐布局）
从左到右 / 从上到下分层很清晰：

**上层：外部**
- User
- GitHub Actions / GitHub Releases

**中层：App（大框）**
- React UI（含 updater UI）
- Tauri IPC/Bridge
- Rust Core（commands/services）
- Updater module（如有）
- Capabilities/Permissions（贴着 IPC 边界画）

**下层：本地资源**
- SQLite DB file
- OS services（Filesystem / App bundle / Keychain/Clipboard 若启用）

**侧边：Tooling**
- Node init/import/organize scripts → SQLite

##### 你还需要补齐的“未知点”（为准确性做的最后核对）
仅靠文件树/README，有几处建议你在画图前快速打开源码确认（但即便不确认，也可先抽象画）：
##### capabilities 允许哪些权限：`src-tauri/capabilities/default.json`（决定 OS 能力是否该出现在图里）

如果你按上述三层图（上下文 → 容器 → 组件）画，并且把 **UI↔IPC↔Rust↔SQLite**、**Updater↔GitHub Releases**、**脚本↔SQLite** 这三条主链路画全，这个项目的系统设计图就会既“准确”又“足够细”。

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% ========== External / Context ==========
  User["User"]:::external

  subgraph "External Services"
    GH["GitHub"]:::external
    GHA["GitHub Actions\n(CI/CD)"]:::cicd
    GHR["GitHub Releases\n(Artifacts + Update Source)"]:::external
  end

  subgraph "Local Machine"
    OS["OS Services\n(Filesystem / Windowing / Notifications)"]:::external
    FS[(Local Disk)]:::storage
    SQLite[(SQLite DB File)]:::db
  end

  %% ========== Desktop App Boundary ==========
  subgraph "Prompt Tools Desktop App (Tauri)"
    direction TB

    subgraph "Frontend (WebView)"
      direction TB
      FE_HTML["index.html\n(Vite entry page)"]:::frontend
      FE_MainTS["src/main.ts\n(React bootstrap)"]:::frontend
      FE_MainJS["src/main.js\n(alt/compat entry)"]:::frontend
      FE_UI["React UI\n(Prompt create/edit/search/organize)"]:::frontend
      FE_Advanced["Advanced / Settings\nModule"]:::frontend
      FE_StylesRoot["Global Styles"]:::frontend
      FE_StylesCSS["styles.css"]:::frontend
      FE_StylesDir["styles/ (module styles)"]:::frontend
      FE_UpdaterCSS["Updater styles"]:::frontend
      FE_Assets["assets/"]:::frontend

      FE_UpdaterUI["Updater UI / Flow"]:::frontend
      FE_TestUpdater["Updater Test Page"]:::frontend

      FE_Bridge["Tauri JS API Bridge\n(invoke / events)"]:::bridge
    end

    subgraph "Security Boundary"
      direction TB
      CAP["Tauri Capabilities / Permissions\n(allowlist boundary)"]:::security
    end

    subgraph "Rust Core (Tauri Host)"
      direction TB
      TAURI_CONF["tauri.conf.json\n(App window/bundle/updater config)"]:::config
      RUST_BUILD["build.rs\n(Build-time hooks)"]:::tooling
      ICONS["icons/ (bundle resources)"]:::asset

      R_MAIN["main.rs\n(Tauri entry: window/WebView,\nplugin + command registration)"]:::backend
      R_LIB["lib.rs\n(Command handlers + module wiring)"]:::backend

      R_CMD["Command / IPC Layer\n(Tauri commands)"]:::backend
      R_SVC["Domain Service Layer\n(PromptService: CRUD/search/tags/import)"]:::backend
      R_DAO["database.rs\n(SQLite DAO/Repository)"]:::backend
      R_UPDATER["Updater Module\n(Tauri updater integration)"]:::backend
    end

    subgraph "Local Data"
      direction TB
      DB_ACCESS["DB Access Boundary\n(Rust-only DB access)"]:::note
    end
  end

  %% ========== Tooling / Engineering ==========
  subgraph "Developer Tooling (Node.js scripts)"
    direction TB
    TOOL_VERIFY["verify-setup.cjs\n(Environment validation)"]:::tooling
    TOOL_INITDB["init-database.js\n(Create/initialize SQLite)"]:::tooling
    TOOL_INITDB_SEED["init-database-with-prompts.js\n(Init + seed prompts)"]:::tooling
    TOOL_IMPORT["import-prompts.js\n(Offline import)"]:::tooling
    TOOL_ORG_CJS["organize-prompts.cjs\n(Organize prompts)"]:::tooling
    TOOL_ORG_JS["organize-prompts.js\n(Organize prompts)"]:::tooling
    TOOL_SEED["initial-prompts.json\n(Seed data)"]:::tooling
    TOOL_TEST_UPDATE["test-update.cjs\n(Update flow test)"]:::tooling
  end

  subgraph "Build Toolchain"
    direction TB
    VITE_TS["vite.config.ts"]:::config
    VITE_JS["vite.config.js"]:::config
    TSCONFIG["tsconfig.json"]:::config
  end

  subgraph "Repository Workflows"
    direction TB
    WF_CI["ci.yml\n(Build/Test)"]:::cicd
    WF_REL["release.yml\n(Automated release)"]:::cicd
    WF_MANUAL["manual-release.yml\n(Manual release)"]:::cicd
  end

  %% ========== Runtime User Flow ==========
  User -->|"uses"| FE_UI
  FE_HTML -->|"loads"| FE_MainTS
  FE_HTML -->|"loads"| FE_MainJS
  FE_MainTS -->|"renders"| FE_UI
  FE_UI -->|"opens"| FE_Advanced

  FE_UI -->|"invoke(command)"| FE_Bridge
  FE_UpdaterUI -->|"invoke(check/download)"| FE_Bridge
  FE_TestUpdater -->|"invoke(test flows)"| FE_Bridge

  FE_Bridge -->|"guarded_by"| CAP
  CAP -->|"allows"| R_CMD

  R_MAIN -->|"registers"| R_CMD
  R_LIB -->|"implements"| R_CMD
  R_CMD -->|"calls"| R_SVC
  R_SVC -->|"queries/transactions"| R_DAO
  R_DAO -->|"read/write"| SQLite
  SQLite -->|"stored_on"| FS

  R_MAIN -->|"uses"| TAURI_CONF
  R_MAIN -->|"accesses"| OS
  R_UPDATER -->|"check_manifest/download_bundle"| GHR
  FE_UpdaterUI -->|"status/progress"| FE_UI

  %% Enforce note: frontend does not access SQLite directly
  DB_ACCESS -.->|"no_direct_access_from_frontend"| FE_UI
  DB_ACCESS -->|"DB_only_via_Rust"| R_DAO

  %% ========== Tooling / Init / Import ==========
  TOOL_VERIFY -->|"validates"| VITE_TS
  TOOL_VERIFY -->|"validates"| TSCONFIG

  TOOL_SEED -->|"input_to"| TOOL_INITDB_SEED
  TOOL_INITDB -->|"creates/writes"| SQLite
  TOOL_INITDB_SEED -->|"creates+seeds"| SQLite
  TOOL_IMPORT -->|"imports_into"| SQLite
  TOOL_ORG_CJS -->|"reorganizes"| SQLite
  TOOL_ORG_JS -->|"reorganizes"| SQLite

  %% ========== Build / Dev / Release Flow (Engineering) ==========
  VITE_TS -.->|"builds_frontend_assets"| FE_HTML
  VITE_JS -.->|"builds_frontend_assets"| FE_HTML
  TSCONFIG -.->|"typecheck"| FE_UI

  WF_CI -.->|"runs_on"| GHA
  WF_REL -.->|"runs_on"| GHA
  WF_MANUAL -.->|"runs_on"| GHA

  GHA -.->|"build/sign/bundle"| R_MAIN
  GHA -.->|"publish"| GHR
  GHR -.->|"download/install/update"| User

  %% ========== Styling Connections ==========
  FE_StylesCSS -->|"styles"| FE_UI
  FE_StylesDir -->|"styles"| FE_UI
  FE_UpdaterCSS -->|"styles"| FE_UpdaterUI
  FE_Assets -->|"static_assets"| FE_UI
  ICONS -->|"app_icons"| R_MAIN

  %% ========== Click Events (mapped paths) ==========
  click R_MAIN "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/src/main.rs"
  click R_LIB "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/src/lib.rs"
  click R_DAO "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/src/database.rs"
  click TAURI_CONF "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/tauri.conf.json"
  click CAP "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/capabilities/default.json"
  click RUST_BUILD "https://github.com/jwangkun/prompt-tools/blob/main/src-tauri/build.rs"
  click ICONS "https://github.com/jwangkun/prompt-tools/tree/main/src-tauri/icons/"
  click FE_MainTS "https://github.com/jwangkun/prompt-tools/blob/main/src/main.ts"
  click FE_MainJS "https://github.com/jwangkun/prompt-tools/blob/main/src/main.js"
  click FE_Advanced "https://github.com/jwangkun/prompt-tools/blob/main/src/advanced.ts"
  click FE_UpdaterUI "https://github.com/jwangkun/prompt-tools/blob/main/src/updater.ts"
  click FE_TestUpdater "https://github.com/jwangkun/prompt-tools/blob/main/src/test-updater.ts"
  click FE_StylesCSS "https://github.com/jwangkun/prompt-tools/blob/main/src/styles.css"
  click FE_UpdaterCSS "https://github.com/jwangkun/prompt-tools/blob/main/src/styles/updater.css"
  click FE_StylesDir "https://github.com/jwangkun/prompt-tools/tree/main/src/styles/"
  click FE_Assets "https://github.com/jwangkun/prompt-tools/tree/main/src/assets/"
  click FE_HTML "https://github.com/jwangkun/prompt-tools/blob/main/index.html"
  click VITE_TS "https://github.com/jwangkun/prompt-tools/blob/main/vite.config.ts"
  click VITE_JS "https://github.com/jwangkun/prompt-tools/blob/main/vite.config.js"
  click TSCONFIG "https://github.com/jwangkun/prompt-tools/blob/main/tsconfig.json"
  click TOOL_INITDB "https://github.com/jwangkun/prompt-tools/blob/main/init-database.js"
  click TOOL_INITDB_SEED "https://github.com/jwangkun/prompt-tools/blob/main/init-database-with-prompts.js"
  click TOOL_IMPORT "https://github.com/jwangkun/prompt-tools/blob/main/import-prompts.js"
  click TOOL_ORG_CJS "https://github.com/jwangkun/prompt-tools/blob/main/organize-prompts.cjs"
  click TOOL_ORG_JS "https://github.com/jwangkun/prompt-tools/blob/main/organize-prompts.js"
  click TOOL_SEED "https://github.com/jwangkun/prompt-tools/blob/main/initial-prompts.json"
  click TOOL_TEST_UPDATE "https://github.com/jwangkun/prompt-tools/blob/main/test-update.cjs"
  click TOOL_VERIFY "https://github.com/jwangkun/prompt-tools/blob/main/verify-setup.cjs"
  click WF_CI "https://github.com/jwangkun/prompt-tools/blob/main/.github/workflows/ci.yml"
  click WF_REL "https://github.com/jwangkun/prompt-tools/blob/main/.github/workflows/release.yml"
  click WF_MANUAL "https://github.com/jwangkun/prompt-tools/blob/main/.github/workflows/manual-release.yml"

  %% ========== Styles ==========
  classDef frontend fill:#1f77b4,stroke:#0b2d4d,color:#ffffff,stroke-width:1px
  classDef backend fill:#6f42c1,stroke:#2e1653,color:#ffffff,stroke-width:1px
  classDef bridge fill:#17a2b8,stroke:#0b4f59,color:#ffffff,stroke-width:1px
  classDef db fill:#2ca02c,stroke:#145214,color:#ffffff,stroke-width:1px
  classDef storage fill:#3cb371,stroke:#145214,color:#ffffff,stroke-width:1px
  classDef external fill:#ff7f0e,stroke:#7a3b00,color:#ffffff,stroke-width:1px
  classDef tooling fill:#6c757d,stroke:#2f3438,color:#ffffff,stroke-width:1px
  classDef cicd fill:#495057,stroke:#1f2326,color:#ffffff,stroke-width:1px
  classDef security fill:#d63384,stroke:#5a1636,color:#ffffff,stroke-width:2px
  classDef config fill:#0d6efd,stroke:#083a7a,color:#ffffff,stroke-width:1px
  classDef asset fill:#fd7e14,stroke:#7a3b00,color:#ffffff,stroke-width:1px
  classDef note fill:#fff3cd,stroke:#d39e00,color:#3b2f00,stroke-width:1px
```
