Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8f640e2
refactor(ai): modularize 13-domain subsystem facades and add AIFlow m…
oksbwn Jul 25, 2026
597bac0
feat(ui): refine AI Health Page telemetry timeline UX, session usage …
oksbwn Jul 25, 2026
f602711
docs(ai): update VitePress documentation for 13-domain facade archite…
oksbwn Jul 25, 2026
f78596a
docs(release): add 2026-07-25 release notes entry for AI 13-domain fa…
oksbwn Jul 25, 2026
31b3334
Docs updated
oksbwn Jul 25, 2026
6abf1c2
fix(grounding): resolve Windows file link verification and auto-forma…
oksbwn Jul 25, 2026
634418b
feat(prompts): format retrieved evidence headers with clickable file …
oksbwn Jul 25, 2026
527fecc
feat(telemetry): isolate flow telemetry into ai-telemetry.db
oksbwn Jul 25, 2026
7c9c7d9
feat(ipc): route FlowTracker telemetry queries and cascade delete to …
oksbwn Jul 25, 2026
6d7cf78
feat(ui): update AI Health page with Telemetry DB card, caller filter…
oksbwn Jul 25, 2026
265a591
test(ai): add telemetry DB and error trace unit tests
oksbwn Jul 25, 2026
475e0ed
fix(lint): resolve ESLint errors and warnings across telemetry and UI…
oksbwn Jul 25, 2026
d88a3e8
style(ui): normalize Lucide icon sizes to approved design system steps
oksbwn Jul 25, 2026
cc6d5c9
New audtiring telemnterry systrem Added
oksbwn Jul 26, 2026
6d4d71f
feat(ai): optimize task intent routing and pipeline telemetry
oksbwn Jul 26, 2026
e72bdaf
fix(ai): harden pipeline telemetry, intent routing, and retrieval qua…
oksbwn Jul 26, 2026
e5d2515
Fixed duplicate reponse!
oksbwn Jul 26, 2026
37d761d
Fixed link rendering
oksbwn Jul 26, 2026
1a71818
Fixed purging
oksbwn Jul 26, 2026
b61f7d9
Modularization propagated to otehr AI modules
oksbwn Jul 26, 2026
11f4bd8
Decomposed Tools
oksbwn Jul 26, 2026
b9cb908
Docs Updated
oksbwn Jul 26, 2026
08c8416
UX Improved
oksbwn Jul 26, 2026
261b567
Pilpeline Updated
oksbwn Jul 26, 2026
bbf08e1
AI Finally in workable state
oksbwn Jul 26, 2026
6a2db7d
Docs updated
oksbwn Jul 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 69 additions & 102 deletions ai/README.md
Original file line number Diff line number Diff line change
@@ -1,132 +1,99 @@
# Notely AI Platform — Comprehensive AI & Agent Subsystem Architecture
# Notely AI Platform — Master Architecture & Subsystem Reference

This directory contains the codebase for Notely's local-first, modular AI platform. Markdown notes remain the single source of truth, parsed and indexed into offline-first SQLite databases (`ai-embeddings.db`, `ai-graph.db`, `ai-memory.db`).
This directory contains the codebase for Notely's local-first, 13-domain modular AI platform. Markdown notes remain the single source of truth, parsed and indexed into offline-first SQLite databases (`ai-embeddings.db`, `ai-graph.db`, `ai-memory.db`).

---

## AI Platform Overview & Design Philosophy
## AI Platform Overview & Core Architecture

Notely's AI is engineered as an **intelligent knowledge companion** rather than a generic LLM chatbot wrapper.
Notely's AI is engineered as a **modular, local-first intelligent knowledge companion**.

### Core Guiding Principles:
1. **Human-like & Natural**: Speaks like a knowledgeable pair programmer and workspace teammate. Never exposes internal technical mechanics (`"search_notes"`, `"vector similarity"`, `"knowledge graph nodes"`).
2. **Context-Aware & Grounded**: Proactively retrieves workspace facts before generating answers. All claims are grounded in verified note file links (`[file.md](file:///path)`).
3. **Multi-Tool Planning & Orchestration**: Dynamically executes parallel retrievals, chains tool outputs, and evaluates evidence confidence before answer synthesis.
4. **Strict Note Immutability**: Existing notes are **100% read-only**. AI tools cannot update, edit, move, rename, or delete existing user notes under any circumstances.
5. **Local-First & Provider Agnostic**: Leverages local ONNX embeddings (`BGE-small-en-v1.5`) and background worker processes (`utilityProcess`), while supporting Gemini, Groq, OpenAI, and Local GGUF models.
All 13 sub-domains are decoupled into dedicated directories with a mandatory single entry point facade (`index.js`). All query execution is coordinated by the master orchestrator **`AIFlow.js`** through a 5-stage pipeline with structured telemetry logging to `LogDB` (`FlowTracker`) and zero-latency **Context Compaction** (`ai/compaction/`).

---

## Complete 4-Layer Decoupled AI Architecture

```mermaid
graph TD
%% Frontend & IPC
subgraph Client ["UI & IPC Bridge"]
UI["AIChatPanel / AIPalette"]
Diagnostics["AIHealthPage.jsx (Diagnostics & Traces)"]
IPC["Electron IPC Handlers (aiHandlers.cjs)"]
end

%% 4-Layer Decoupled Core
subgraph Planning ["4-Layer Decoupled Planning & Core Triad"]
Agent["Agent.js (Central Orchestrator)"]
IntentAnalyzer["IntentAnalyzer.js (Layer 1 Intent Detection)"]
CapabilityResolver["CapabilityResolver.js (Layer 2 Capability Resolution)"]
Planner["Planner.js (Layer 3 Execution DAG Planner)"]
ContextOrchestrator["ContextOrchestrator.js (Layer 4 Multi-Tool Engine)"]
ReasoningBrain["ReasoningBrain.js (Pure LLM Synthesis)"]
ActionBrain["ActionBrain.js (Permission Gatekeeper)"]
SelfCorrectionEngine["SelfCorrectionEngine.js (ReAct Validator)"]
end

%% Retrieval & Registry
subgraph Retrieval ["Context, Registry & Tools"]
Registry["ApplicationToolRegistry.cjs (Single Source of Truth Catalog)"]
ContextEngine["ContextEngine.js (Context Buffer Pipeline)"]
HybridRetriever["HybridRetriever.js (Reciprocal Rank Fusion)"]
SemanticRetriever["SemanticRetriever.js (Vector Cosine Search)"]
GraphRetriever["GraphRetriever.js (Recursive CTE Graph Walk)"]
SemanticTools["SemanticTools.js (Tool Execution Runner)"]
end

%% Storage
subgraph Storage ["SQLite Storage (WAL Mode)"]
EmbedDB["ai-embeddings.db (Chunk Vectors)"]
GraphDB["ai-graph.db (Entity Relations & Evidence)"]
MemoryDB["ai-memory.db (Chats & Traces)"]
end

%% Data Flow
UI & Diagnostics --> IPC --> Agent
Agent --> ContextOrchestrator & ReasoningBrain & ActionBrain
ContextOrchestrator --> IntentAnalyzer --> CapabilityResolver --> Planner
CapabilityResolver --> Registry
ContextOrchestrator --> SemanticTools & HybridRetriever
SemanticTools --> Registry
HybridRetriever --> SemanticRetriever & GraphRetriever
SemanticRetriever --> EmbedDB
GraphRetriever --> GraphDB
Agent --> MemoryDB
ReasoningBrain --> SelfCorrectionEngine
```
## Complete 14-Domain Module Directory Map

| Domain Directory | Entry Point Facade | Architectural Responsibilities |
|---|---|---|
| **`ai/compaction/`** | `index.js` | **`CompactionEngine` (0ms NLP intent/outcome extractor & 2-tier sliding window compactor)** |
| **`ai/planner/`** | `index.js` | `Planner`, `ContextOrchestrator`, `IntentAnalyzer`, `CapabilityResolver`, multi-tool RAG |
| **`ai/brains/`** | `index.js` | `WorkspaceBrain`, `ReasoningBrain`, `ActionBrain` (3-Brain Triad reasoning engine) |
| **`ai/personas/`** | `index.js` | `PersonaManager`, `PersonaStandard`, persona DB validation & prompt overlays |
| **`ai/prompts/`** | `index.js` | `PromptPipeline`, `PromptLoader`, `TemplateEngine`, `PromptLibrary` (system prompt assembly) |
| **`ai/context/`** | `index.js` | `ContextEngine`, `ContextManager`, `SemanticRetriever`, `GraphRetriever`, `HybridRetriever` |
| **`ai/graph/`** | `index.js` | `GraphDB`, `GraphService`, `GraphBuilder`, `MarkdownASTParser`, GLiNER/GLiREL neural models |
| **`ai/embeddings/`** | `index.js` | `EmbeddingDB`, `EmbeddingService`, ONNX Transformer embedder |
| **`ai/memory/`** | `index.js` | `ConversationStore`, `MemoryDB`, `PersonaDB`, `InteractionLog` |
| **`ai/executor/`** | `index.js` | `QueryExecutor`, `SelfCorrectionEngine` (Runtime Dynamic Strategies) |
| **`ai/tools/`** | `index.js` | `ToolRegistry`, `SemanticTools`, `DocumentReader`, Application Tool Registry |
| **`ai/grounding/`** | `index.js` | `GroundingEngine` (citation link validator, line links, note title claim linter) |
| **`ai/formatter/`** | `index.js` | Response Formatter (markdown clean-up, tool output formatting) |
| **`ai/testing/`** | `index.js` | `PromptTester` (Prompt Safety Harness, policy linter, test audit runner) |

---

## Subsystem Component Reference

### 1. 3-Brain Architectural Triad & Orchestrator
## Master Flow Orchestrator Pipeline (`ai/core/AIFlow.js`)

| Component | File Path | Architectural Responsibility | Key Safeguards & Capabilities |
|---|---|---|---|
| Component | File Path | Architectural Responsibility | Key Safeguards & Capabilities |
|---|---|---|---|
| **IntentAnalyzer** | [`ai/core/IntentAnalyzer.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/IntentAnalyzer.js) | Intent Detection & Goal Deconstruction | Deconstructs queries into Goal, Domain, Information Needs, and Sub-intents dynamically from `ApplicationToolRegistry` metadata. |
| **CapabilityResolver** | [`ai/core/CapabilityResolver.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/CapabilityResolver.js) | Capability Resolution & Endpoint Binding | Maps Information Needs to Abstract Capabilities (`notes:search`, `tasks:extract`, `graph:traverse`, `web:search`). Binds tool endpoints dynamically. |
| **Planner** | [`ai/core/Planner.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/Planner.js) | Execution Plan Generation (DAG) | Generates structured capability plans (`ExecutionPlan`). Uses Vercel AI SDK `generateObject` when online; local capability DAG when offline. |
| **ContextOrchestrator** | [`ai/core/ContextOrchestrator.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/ContextOrchestrator.js) | Multi-Tool Planning & Context Aggregation | Coordinates full 4-layer lifecycle (`IntentAnalyzer` -> `CapabilityResolver` -> `Planner` -> Tool Execution Engine -> Evidence Aggregation). |
| **ApplicationToolRegistry**| [`electron/tools/ApplicationToolRegistry.cjs`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/electron/tools/ApplicationToolRegistry.cjs) | Single Source of Truth Tool Catalog | Central registry for tool schemas, capabilities, permissions, and Vercel AI SDK / MCP output formats. Enforces note immutability (`notes.move` removed). |
```mermaid
graph TD
UserQuery["User Query + Session ID"] --> Stage1["Stage 1: Context & Persona Resolution (memory + personas + compaction)"]
Stage1 --> Stage2["Stage 2: Intent Planning & Hybrid Retrieval (planner + graph + embeddings)"]
Stage2 --> Stage3["Stage 3: System Prompt Assembly & Harness Audit (prompts + testing)"]
Stage3 --> Stage4["Stage 4: Dynamic Runtime Strategy Execution & Tools (executor + tools + grounding + formatter)"]
Stage4 --> Stage5["Stage 5: Memory Persistence & Telemetry Logging (memory + logs)"]
Stage5 --> Telemetry["LogDB FlowTracker & ConversationStore"]
```

### 2. Planning & Tool Ecosystem
### Stage Summary:
1. **Stage 1 (Context & Persona Resolution)**: Resolves conversation state, loads active persona, and applies 0ms context compaction (`ai/compaction/`).
2. **Stage 2 (Intent Planning & Hybrid Retrieval)**: `ContextOrchestrator` runs parallel vector/graph retrieval & confidence scoring.
3. **Stage 3 (System Prompt Assembly & Safety Audit)**: `PromptPipeline` assembles system prompt & runs safety invariant linter.
4. **Stage 4 (Runtime Dynamic Strategy Execution & Tools)**: `QueryExecutor` resolves runtime strategy (Streaming, Multi-step tool loop, Self-correction verification) and runs `GroundingEngine`.
5. **Stage 5 (Memory Persistence & Telemetry Logging)**: Persists turn to `ConversationStore` and logs 5-stage trace payload to `LogDB` (`FlowTracker`).

| Component | File Path | Responsibility | Capabilities |
|---|---|---|---|
| **SemanticTools** | [`ai/tools/SemanticTools.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/tools/SemanticTools.js) | High-Level Tool Execution Runner | Executes tools dynamically via `applicationToolRegistry.executeTool(toolName, args)` with local hybrid retriever fallbacks. |
---

### 3. Prompting, Persona & Grounding System
## Context Compaction Algorithm (`ai/compaction/`)

| Component | File Path | Responsibility | Features |
|---|---|---|---|
| **PromptLibrary** | [`ai/core/PromptLibrary.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/PromptLibrary.js) | Modular System Prompts | Assembles base policies, dynamic domain context inference, active persona instructions, and workspace context. |
| **GroundingEngine** | [`ai/core/GroundingEngine.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/GroundingEngine.js) | Citation Link Validator | Audits file link citations (`[label](file:///path)`) against disk and strips broken links before response output. |
| **SelfCorrectionEngine**| [`ai/core/SelfCorrectionEngine.js`](file:///c:/Users/oksbw/OneDrive/Desktop/Antigravity%20Workspace/Notely/ai/core/SelfCorrectionEngine.js) | ReAct Response Validation Pass | Intercepts draft responses, strips leaked technical tool narration jargon, and validates grounding. |
- **2-Tier Sliding Window Algorithm**:
- **Tier 1 (Verbatim Window)**: Recent 4 messages preserved verbatim for immediate context.
- **Tier 2 (Executive Memory Summary)**: Older turns programmatically compressed into structured bullet points using 0ms NLP intent & outcome extraction heuristics:
```markdown
[EXECUTIVE MEMORY SUMMARY OF PAST TURNS]
- Turn 1: User requested "explain auth" -> Referenced notes: Architecture Notes
- Turn 2: User requested "add telemetry" -> Generated code snippet/action
```
- **Benefits**: ~75-80% input token reduction, faster LLM latency, zero text redundancy.

---

## 4-Layer Decoupled Planning & Context Orchestration

`ContextOrchestrator.js` coordinates a 4-layer decoupled planning architecture:
## AI Health & Diagnostics UI (`AIHealthPage.jsx`)

1. **Layer 1 (Intent Detection)**: `IntentAnalyzer.js` deconstructs query into an `IntentManifest` (Goal, Domain, Information Needs, Sub-intents) dynamically from tool catalog metadata.
2. **Layer 2 (Capability Resolution)**: `CapabilityResolver.js` maps Information Needs to Abstract Semantic Capabilities (`notes:search`, `tasks:extract`, `graph:traverse`, `web:search`) and binds registered tool endpoints dynamically.
3. **Layer 3 (Execution Planning)**: `Planner.js` constructs an ordered execution DAG plan (`ExecutionPlan`). Uses active LLM provider when online; local dynamic capability DAG when offline.
4. **Layer 4 (Tool Orchestration)**: `ContextOrchestrator.js` executes capability steps in parallel/chained steps, evaluates confidence ($0.0 - 1.0$), and consolidates evidence payload for `ReasoningBrain.js`.
- **Messages Tab**: Clean conversation transcript (technical tool call boxes removed).
- **Flow Telemetry Tab**: Interactive 5-stage execution trace view displaying:
1. Timeline & duration per stage
2. Persona & active note context
3. Pre-retrieval trace steps & confidence score
4. System prompt viewer with Copy & Expand
5. Tool calls with input arguments & output payloads
6. Compaction stats (`compactedTurnsCount`, `isCompacted`)
7. Token consumption & latency breakdown

---

## Verification & Test Suite Execution

All AI subsystem components are covered by Vitest test suites under `tests/ai/`:
All AI subsystem modules are fully covered by unit & integration test suites under `tests/ai/`:

```bash
node node_modules/vitest/vitest.mjs run tests/ai
npm test
```

### Test Suite Map (27 Test Files / 72 Tests Passing 100%):
- `tests/ai/orchestrator.spec.js`: Multi-tool planning, parallel retrieval & evidence aggregation tests.
- `tests/ai/brainTriad.spec.js`: 3-Brain isolation & note immutability tests.
- `tests/ai/planner.spec.js`: Intent classification & semantic tools tests.
- `tests/ai/grounding.spec.js`: Citation link verification & prompt composition tests.
- `tests/ai/selfCorrection.spec.js`: ReAct validation pass & zero-jargon gate tests.
- `tests/ai/harness.spec.js`: Evaluation harness metrics tests.
- `tests/ai/knowledgeGraph.spec.js`: Recursive CTE graph traversal & UTC date matching tests.
### Test Suite Summary:
- **59 Test Files Passed (100% Pass Rate)**
- **249 Individual Tests Passed**
- Key Test Specs:
- `tests/ai/flow.spec.js`: Master `AIFlow` 5-stage orchestration & telemetry tests.
- `tests/ai/facades.spec.js`: Single entry point facade export integrity for all 13 modules.
- `tests/ai/compaction.spec.js`: Zero-latency NLP intent extraction & sliding window compaction tests.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions ai/brains/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Brains Module Facade (3-Brain Triad)
* Single entry point for WorkspaceBrain, ReasoningBrain, and ActionBrain.
*/

const WorkspaceBrain = require('./WorkspaceBrain');
const ReasoningBrain = require('./ReasoningBrain');
const ActionBrain = require('./ActionBrain');

module.exports = {
WorkspaceBrain,
ReasoningBrain,
ActionBrain,

createWorkspaceBrain: (db) => new WorkspaceBrain(db),
createReasoningBrain: (llmRegistry) => new ReasoningBrain(llmRegistry),
createActionBrain: (agent) => new ActionBrain(agent)
};
Loading
Loading