|
1 | | -# Copilot Instructions — Ghost Project |
| 1 | +# Ghost — Copilot Instructions |
2 | 2 |
|
3 | 3 | ## Project Overview |
4 | | -Ghost is a private, local-first **Agent OS** for desktop and mobile. It uses Tauri v2 (Rust backend) + React/TypeScript (frontend) + SQLite/sqlite-vec + Candle (native AI). |
5 | 4 |
|
6 | | -## Key Conventions |
7 | | -- **Privacy first**: NEVER add telemetry, analytics, or external network calls |
8 | | -- **Rust backend**: `src-tauri/` with `thiserror`, `anyhow`, `tokio`, `tracing` |
9 | | -- **Frontend**: React 18 + TypeScript strict + Tailwind CSS v4 in `src/` |
10 | | -- **Website**: Astro Starlight in `website/` — deployed to GitHub Pages |
11 | | -- **Commits**: conventional commits (`feat:`, `fix:`, `docs:`, `refactor:`) |
12 | | -- **Never use `--all-features`** in Rust — `metal`/`accelerate` are macOS-only |
| 5 | +Ghost is a privacy-first AI desktop assistant built with Tauri v2 (Rust backend) + React/TypeScript/Vite (frontend). All AI inference runs locally via Candle + GGUF models. No cloud dependencies for core functionality. |
13 | 6 |
|
14 | | -## Website Maintenance |
15 | | -The documentation website (`website/`) should stay in sync with source files: |
16 | | -- `README.md` → `website/src/content/docs/guides/introduction.md` |
17 | | -- `ROADMAP.md` → `website/src/content/docs/reference/roadmap.md` |
18 | | -- `CHANGELOG.md` → `website/src/content/docs/reference/changelog.md` |
19 | | -- `CONTRIBUTING.md` → `website/src/content/docs/reference/contributing.md` |
| 7 | +## Tech Stack |
20 | 8 |
|
21 | | -Use the `website-maintainer` custom agent for website tasks. |
| 9 | +- **Framework:** Tauri v2 |
| 10 | +- **Backend:** Rust (edition 2021) |
| 11 | +- **Frontend:** React + TypeScript + Vite |
| 12 | +- **Database:** SQLite via rusqlite (FTS5 for full-text search) |
| 13 | +- **ML:** Candle (Hugging Face) — all-MiniLM-L6-v2 for embeddings (384D), Qwen2.5-Instruct GGUF for generation |
| 14 | +- **Agent:** ReAct engine with grammar-constrained tool calling |
| 15 | +- **Protocols:** MCP (rmcp crate), AG-UI, A2UI, A2A |
| 16 | +- **Licensing:** ghost-pro crate (freemium feature gating) |
22 | 17 |
|
23 | | -## File Structure |
| 18 | +## Build & Test |
| 19 | + |
| 20 | +```bash |
| 21 | +# Build |
| 22 | +cargo build --release |
| 23 | + |
| 24 | +# Run in dev mode (Tauri) |
| 25 | +cargo tauri dev |
| 26 | + |
| 27 | +# Run tests |
| 28 | +cargo test |
| 29 | + |
| 30 | +# Run specific test |
| 31 | +cargo test test_name |
| 32 | + |
| 33 | +# Clippy lints |
| 34 | +cargo clippy -- -D warnings |
| 35 | + |
| 36 | +# Frontend dev |
| 37 | +cd frontend && pnpm dev |
| 38 | + |
| 39 | +# Frontend build |
| 40 | +cd frontend && pnpm build |
| 41 | +``` |
| 42 | + |
| 43 | +## Module Structure |
| 44 | + |
| 45 | +``` |
| 46 | +src-tauri/ |
| 47 | +├── src/ |
| 48 | +│ ├── main.rs # Tauri app entry, window setup |
| 49 | +│ ├── lib.rs # Core library exports |
| 50 | +│ ├── agent/ # ReAct agent engine |
| 51 | +│ │ ├── mod.rs # Agent orchestrator |
| 52 | +│ │ ├── react.rs # ReAct loop (think → act → observe) |
| 53 | +│ │ ├── grammar.rs # Grammar-constrained generation |
| 54 | +│ │ └── tools/ # Built-in tool implementations |
| 55 | +│ ├── search/ # Hybrid search |
| 56 | +│ │ ├── fts.rs # FTS5 full-text search (<5ms) |
| 57 | +│ │ ├── semantic.rs # Vector search via Candle embeddings |
| 58 | +│ │ └── hybrid.rs # Combined ranking |
| 59 | +│ ├── mcp/ # MCP Server + Client |
| 60 | +│ │ ├── server.rs # Ghost as MCP server |
| 61 | +│ │ ├── client.rs # Connect to external MCP servers |
| 62 | +│ │ └── catalog.rs # 30+ curated + registry (6000+) |
| 63 | +│ ├── extraction/ # File processing |
| 64 | +│ │ ├── watcher.rs # File system watcher |
| 65 | +│ │ └── extractors/ # PDF, DOCX, 50+ formats |
| 66 | +│ ├── models/ # Local AI model management |
| 67 | +│ │ ├── loader.rs # GGUF model loading via Candle |
| 68 | +│ │ └── inference.rs # Text generation pipeline |
| 69 | +│ ├── db/ # Database layer |
| 70 | +│ │ ├── mod.rs # Connection pool, migrations |
| 71 | +│ │ ├── schema.rs # Table definitions |
| 72 | +│ │ └── migrations/ # SQL migrations |
| 73 | +│ └── pro/ # ghost-pro crate integration |
| 74 | +│ ├── license.rs # License key validation |
| 75 | +│ └── features.rs # Feature gating logic |
| 76 | +├── Cargo.toml |
| 77 | +└── tauri.conf.json |
| 78 | +``` |
| 79 | + |
| 80 | +## Rust Conventions |
| 81 | + |
| 82 | +- **Error handling:** Use `anyhow::Result` for application errors, `thiserror` for library errors. Never `unwrap()` in production paths. |
| 83 | +- **Async:** Tokio runtime (Tauri v2 default). Use `#[tauri::command]` for frontend-callable functions. |
| 84 | +- **Tauri commands:** All Tauri commands go in dedicated command modules, registered in `main.rs` via `invoke_handler`. |
| 85 | +- **Database:** All SQL goes through rusqlite with parameterized queries. Never interpolate strings into SQL. |
| 86 | +- **Logging:** Use `tracing` crate with structured fields. `tracing::info!`, `tracing::error!`, etc. |
| 87 | +- **Testing:** Unit tests in the same file (`#[cfg(test)]` module). Integration tests in `tests/` directory. |
| 88 | + |
| 89 | +## How to Add a New Tool |
| 90 | + |
| 91 | +Tools are functions the ReAct agent can call during reasoning. |
| 92 | + |
| 93 | +1. Create the tool implementation in `src/agent/tools/`: |
| 94 | +```rust |
| 95 | +// src/agent/tools/my_tool.rs |
| 96 | +use serde::{Deserialize, Serialize}; |
| 97 | +use anyhow::Result; |
| 98 | + |
| 99 | +#[derive(Debug, Deserialize)] |
| 100 | +pub struct MyToolInput { |
| 101 | + pub query: String, |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Debug, Serialize)] |
| 105 | +pub struct MyToolOutput { |
| 106 | + pub result: String, |
| 107 | +} |
| 108 | + |
| 109 | +pub async fn execute(input: MyToolInput) -> Result<MyToolOutput> { |
| 110 | + // Implementation here |
| 111 | + Ok(MyToolOutput { result: "done".into() }) |
| 112 | +} |
24 | 113 | ``` |
25 | | -src-tauri/src/ # Rust backend (indexer, db, embeddings, chat, agent, protocols) |
26 | | -src/ # React frontend (components, hooks, lib) |
27 | | -website/ # Astro Starlight documentation site |
28 | | -scripts/ # Build and maintenance scripts |
29 | | -.github/agents/ # Custom Copilot agents (website-maintainer) |
| 114 | + |
| 115 | +2. Register in `src/agent/tools/mod.rs`: |
| 116 | +```rust |
| 117 | +pub mod my_tool; |
| 118 | + |
| 119 | +// Add to the tool registry |
| 120 | +registry.register("my_tool", ToolDef { |
| 121 | + name: "my_tool", |
| 122 | + description: "What this tool does", |
| 123 | + parameters: serde_json::json!({ |
| 124 | + "type": "object", |
| 125 | + "properties": { |
| 126 | + "query": { "type": "string", "description": "The search query" } |
| 127 | + }, |
| 128 | + "required": ["query"] |
| 129 | + }), |
| 130 | + handler: |input| Box::pin(my_tool::execute(serde_json::from_value(input)?)), |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +3. Add tests: |
| 135 | +```rust |
| 136 | +#[cfg(test)] |
| 137 | +mod tests { |
| 138 | + use super::*; |
| 139 | + |
| 140 | + #[tokio::test] |
| 141 | + async fn test_my_tool_basic() { |
| 142 | + let input = MyToolInput { query: "test".into() }; |
| 143 | + let output = execute(input).await.unwrap(); |
| 144 | + assert!(!output.result.is_empty()); |
| 145 | + } |
| 146 | +} |
30 | 147 | ``` |
31 | 148 |
|
32 | | -## Copilot Agents |
33 | | -- **website-maintainer**: Assign website/docs issues to `@copilot` — it uses `.github/agents/website-maintainer.agent.md` |
34 | | -- The deploy workflow (`website-deploy.yml`) auto-syncs CHANGELOG, ROADMAP, CONTRIBUTING, SECURITY on every push |
| 149 | +4. Run `cargo test` and `cargo clippy` before committing. |
| 150 | + |
| 151 | +## How to Add a New MCP Server |
| 152 | + |
| 153 | +MCP servers extend Ghost's capabilities via the Model Context Protocol. |
| 154 | + |
| 155 | +1. Add to curated catalog in `src/mcp/catalog.rs`: |
| 156 | +```rust |
| 157 | +CatalogEntry { |
| 158 | + id: "my-server", |
| 159 | + name: "My Server", |
| 160 | + description: "What it does", |
| 161 | + transport: Transport::Stdio { |
| 162 | + command: "npx", |
| 163 | + args: &["-y", "@my-org/mcp-server"], |
| 164 | + }, |
| 165 | + // OR for SSE: |
| 166 | + // transport: Transport::Sse { url: "https://my-server.example.com/sse" }, |
| 167 | + category: Category::Productivity, |
| 168 | + requires_config: vec!["API_KEY"], // env vars needed |
| 169 | + tier: Tier::Free, // or Tier::Pro |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +2. For programmatic MCP client connections: |
| 174 | +```rust |
| 175 | +use crate::mcp::client::McpClient; |
| 176 | + |
| 177 | +let client = McpClient::connect("my-server", transport_config).await?; |
| 178 | +let tools = client.list_tools().await?; |
| 179 | +let result = client.call_tool("tool_name", params).await?; |
| 180 | +``` |
| 181 | + |
| 182 | +3. Test with `cargo test mcp` and verify the server appears in the UI catalog. |
| 183 | + |
| 184 | +## Deploy & Release |
| 185 | + |
| 186 | +- **CI/CD:** GitHub Actions builds for Windows (.msi), macOS (.dmg), Linux (.AppImage) |
| 187 | +- **Release:** Tag with `vX.Y.Z`, push triggers release workflow |
| 188 | +- **Signing:** macOS requires notarization credentials in GitHub Secrets |
| 189 | +- **Auto-update:** Tauri v2 updater plugin checks GitHub Releases |
| 190 | + |
| 191 | +## Key Dependencies |
| 192 | + |
| 193 | +| Crate | Purpose | |
| 194 | +|---|---| |
| 195 | +| `tauri` | Desktop framework (v2) | |
| 196 | +| `rusqlite` | SQLite + FTS5 | |
| 197 | +| `candle-core` / `candle-nn` | Local ML inference | |
| 198 | +| `candle-transformers` | Model architectures | |
| 199 | +| `rmcp` | MCP protocol client/server | |
| 200 | +| `tokenizers` | Tokenization for embeddings | |
| 201 | +| `serde` / `serde_json` | Serialization | |
| 202 | +| `tokio` | Async runtime | |
| 203 | +| `tracing` | Structured logging | |
| 204 | +| `anyhow` / `thiserror` | Error handling | |
| 205 | + |
| 206 | +## Common Pitfalls |
35 | 207 |
|
36 | | -## For Detailed Architecture |
37 | | -Read `CLAUDE.md` at the project root for full architecture, conventions, and decision log. |
| 208 | +- **Candle models:** Always load on a dedicated thread — model loading blocks and is heavy. Use `tokio::task::spawn_blocking`. |
| 209 | +- **FTS5 queries:** Escape special characters in user input before passing to FTS5 MATCH. |
| 210 | +- **Tauri commands:** Must return `Result<T, String>` (Tauri serialization constraint). Wrap errors with `.map_err(|e| e.to_string())`. |
| 211 | +- **Database migrations:** Always add new migrations, never modify existing ones. Run in order on first launch. |
| 212 | +- **Feature gating:** Check `ghost_pro::is_feature_enabled("feature_name")` before executing Pro-tier logic. Free tier must never crash when Pro features are unavailable. |
0 commit comments