Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore: remove Copilot instructions
Elian Doran4fa469c12ecc
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.mddeleted file mode 100644index 2cebdf17bf..0000000000--- a/.github/copilot-instructions.md+++ /dev/null@@ -1,400 +0,0 @@-# Trilium Notes - AI Coding Agent Instructions--> **Note**: When updating this file, also update `CLAUDE.md` in the repository root to keep both AI coding assistants in sync.--## Project Overview--Trilium Notes is a hierarchical note-taking application with advanced features like synchronization, scripting, and rich text editing. Built as a TypeScript monorepo using pnpm, it implements a three-layer caching architecture (Becca/Froca/Shaca) with a widget-based UI system and supports extensive user scripting capabilities.--## Essential Architecture Patterns--### Three-Layer Cache System (Critical to Understand)-- **Becca** (`apps/server/src/becca/`): Server-side entity cache, primary data source-- **Froca** (`apps/client/src/services/froca.ts`): Client-side mirror synchronized via WebSocket-- **Shaca** (`apps/server/src/share/`): Optimized cache for public/shared notes--**Key insight**: Never bypass these caches with direct DB queries. Always use `becca.notes[noteId]`, `froca.getNote()`, or equivalent cache methods.--### Entity Relationship Model-Notes use a **multi-parent tree** via branches:-- `BNote` - The note content and metadata-- `BBranch` - Tree relationships (one note can have multiple parents via cloning)-- `BAttribute` - Key-value metadata attached to notes (labels and relations)--### Entity Change System & Sync-Every entity modification (notes, branches, attributes) creates an `EntityChange` record that drives synchronization:--```typescript-// Entity changes are automatically tracked-note.title = "New Title";-note.save(); // Creates EntityChange record with changeId--// Sync protocol via WebSocket-ws.sendMessage({ type: 'sync-pull-in-progress', ... });-```--**Critical**: This is why you must use Becca/Froca methods instead of direct DB writes - they create the change tracking records needed for sync.--### Entity Lifecycle & Events-The event system (`apps/server/src/services/events.ts`) broadcasts entity lifecycle events:--```typescript-// Subscribe to events in widgets or services-eventService.subscribe('noteChanged', ({ noteId }) => {- // React to note changes-});--// Common events: noteChanged, branchChanged, attributeChanged, noteDeleted-// Widget method: entitiesReloadedEvent({loadResults}) for handling reloads-```--**Becca loader priorities**: Events are emitted in order (notes → branches → attributes) during initial load to ensure referential integrity.--### TaskContext for Long Operations-Use `TaskContext` for operations with progress reporting (imports, exports, bulk operations):--```typescript-const taskContext = new TaskContext("task-id", "import", "Import Notes");-taskContext.increaseProgressCount();--// WebSocket messages: { type: 'taskProgressCount', taskId, taskType, data, progressCount }--**Pattern**: All long-running operations (delete note trees, export, import) use TaskContext to send WebSocket updates to the frontend.--### Protected Session Handling-Protected notes require an active encryption session:--```typescript-// Always check before accessing protected content-if (note.isContentAvailable()) {- const content = note.getContent(); // Safe-} else {- const title = note.getTitleOrProtected(); // Returns "[protected]"-}--// Protected session management-protectedSessionService.isProtectedSessionAvailable() // Check session-protectedSessionService.startProtectedSession() // After password entry-```--**Session timeout**: Protected sessions expire after inactivity. The encryption key is kept in memory only.--### Attribute Inheritance Patterns-Attributes can be inherited through three mechanisms:--```typescript-// 1. Standard inheritance (#hidePromotedAttributes ~hidePromotedAttributes)-note.getInheritableAttributes() // Walks up parent tree--// 2. Child prefix inheritance (child:label copies to children)-parentNote.setLabel("child:icon", "book") // All children inherit this--// 3. Template relation inheritance (#template=templateNoteId)-note.setRelation("template", templateNoteId)-note.getInheritedAttributes() // Includes template's inheritable attributes-```--**Cycle prevention**: Inheritance tracking prevents infinite loops when notes reference each other.--### Widget-Based UI Architecture-All UI components extend from widget base classes (`apps/client/src/widgets/`):--```typescript-// Right panel widget (sidebar)-class MyWidget extends RightPanelWidget {- get position() { return 100; } // Order in panel- get parentWidget() { return 'right-pane'; }- isEnabled() { return this.note && this.note.hasLabel('myLabel'); }- async refreshWithNote(note) { /* Update UI */ }-}--// Note-aware widget (responds to note changes)-class MyNoteWidget extends NoteContextAwareWidget {- async refreshWithNote(note) { /* Refresh when note changes */ }- async entitiesReloadedEvent({loadResults}) { /* Handle entity updates */ }-}-```--**Important**: Widgets use jQuery (`this.$widget`) for DOM manipulation. Don't mix React patterns here.--### Reusable Preact Components-Common UI components are available in `apps/client/src/widgets/react/` — **always** reuse these instead of writing raw HTML elements or custom implementations:-- `NoItems` - Empty state placeholder with icon and message (use for "no results", "too many items", error states)-- `ActionButton` - Consistent button styling with icon support-- `FormTextBox` - Text input with validation and controlled input handling; `FormTextBoxWithUnit` for inputs with a unit suffix (e.g. "mm", "px")-- `FormSelect` - Dropdown/combobox taking an object array as data-- `Slider` - Range slider with label-- `Checkbox`, `RadioButton` - Form controls-- `CollapsibleSection` - Expandable content sections-- `Badge` - Colored pill/label with optional icon, tooltip, and `onClick` (for counts, status flags). Set its color via the `--color` CSS variable on a wrapper class (not inline styles); pass `outline` for a colored-border/transparent-fill variant instead of a solid background. `BadgeWithDropdown` pairs a badge with a dropdown menu. Don't hand-roll pill/badge markup — reuse it--**Do not use Bootstrap utility classes** (e.g. `form-control-sm`, `form-select-sm`, `input-group`) on these components — they manage their own styling internally. If you need to adjust sizing or layout, use props provided by the component or CSS custom properties, not Bootstrap overrides.--#### Component Styling-- **Avoid inline styles** — do not use the `style` attribute/prop on JSX elements unless absolutely necessary (e.g. a truly dynamic, computed value that cannot be expressed in CSS). Static layout, sizing, spacing, and visual properties must go in CSS.-- **Per-component CSS files**: each component should have a matching `.css` file (e.g. `my_dialog.tsx` → `my_dialog.css`), imported at the top of the component file.-- **CSS nesting for scoping**: since CSS modules are not available, scope styles using a root class and native CSS nesting. For example, a dialog with `className="my-dialog"` should have its styles nested under `.modal.my-dialog { … }`.-- **Reuse existing components** instead of building custom markup — prefer `FormTextBox`, `FormTextBoxWithUnit`, `FormSelect`, `Slider`, `Button`, etc. over hand-rolled `<input>`, `<select>`, or `<button>` elements.--## Development Workflow--### Running & Testing-```bash-# From root directory-pnpm install # Install dependencies-corepack enable # Enable pnpm if not available-pnpm server:start # Dev server (http://localhost:8080)-pnpm server:start-prod # Production mode server-pnpm desktop:start # Desktop app development-pnpm server:test spec/etapi/search.spec.ts # Run specific test-pnpm test:parallel # Client tests (can run parallel)-pnpm test:sequential # Server tests (shared DB) + browser-mode editor tests-pnpm test:all # All tests (parallel + sequential)-pnpm coverage # Generate coverage reports-pnpm typecheck # Type check all projects-```--### Building-```bash-pnpm client:build # Build client application-pnpm server:build # Build server application-pnpm desktop:build # Build desktop application-```--### Test Organization-- **Server tests** (`apps/server/spec/`): Must run sequentially (shared database state)-- **Client tests** (`apps/client/src/`): Can run in parallel-- **E2E tests** (`packages/trilium-e2e/`): Shared Playwright tests, run via `pnpm --filter server e2e` or `pnpm --filter standalone e2e`-- **ETAPI tests** (`apps/server/spec/etapi/`): External API contract tests--**Pattern**: When adding new API endpoints, add tests in `spec/etapi/` following existing patterns (see `search.spec.ts`).--### Monorepo Navigation-```-apps/- client/ # Frontend (shared by server & desktop)- server/ # Node.js backend with REST API- desktop/ # Electron wrapper- web-clipper/ # Browser extension for saving web content- db-compare/ # Database comparison tool- dump-db/ # Database export utility- edit-docs/ # Documentation editing tools-packages/- commons/ # Shared types and utilities- ckeditor5/ # Custom rich text editor with Trilium-specific plugins- codemirror/ # Code editor integration- highlightjs/ # Syntax highlighting- share-theme/ # Theme for shared/published notes- ckeditor5-admonition/ # Admonition blocks plugin- ckeditor5-footnotes/ # Footnotes plugin- ckeditor5-math/ # Math equations plugin- ckeditor5-mermaid/ # Mermaid diagrams plugin-```--**Filter commands**: Use `pnpm --filter server test` to run commands in specific packages.--## Critical Code Patterns--### ETAPI Backwards Compatibility-When adding query parameters to ETAPI endpoints (`apps/server/src/etapi/`), maintain backwards compatibility by checking if new params exist before changing response format.--**Pattern**: ETAPI consumers expect specific response shapes. Always check for breaking changes.--### Frontend-Backend Communication-- **REST API**: `apps/server/src/routes/api/` - Internal endpoints (no auth required when `noAuthentication=true`)-- **ETAPI**: `apps/server/src/etapi/` - External API with authentication-- **WebSocket**: Real-time sync via `apps/server/src/services/ws.ts`--**Auth note**: ETAPI uses basic auth with tokens. Internal API endpoints trust the frontend.--### Adding New LLM Tools-Tools are defined using `defineTools()` in `apps/server/src/services/llm/tools/` and automatically registered for both the LLM chat and MCP server.--1. Add the tool definition in the appropriate module (`note_tools.ts`, `attribute_tools.ts`, `hierarchy_tools.ts`) or create a new module-2. Each tool needs: `description`, `inputSchema` (Zod), `execute` function, and optionally `mutates: true` for write operations or `needsContext: true` for tools that need the current note context-3. If creating a new module, wrap tools in `defineTools({...})` and add the registry to `allToolRegistries` in `tools/index.ts`-4. Add a client-side friendly name in `apps/client/src/translations/en/translation.json` under `llm.tools.<tool_name>` — use **imperative tense** (e.g. "Search notes", "Create note", "Get attributes"), not present continuous--### Database Migrations-- Add scripts in `apps/server/src/migrations/YYMMDD_HHMM__description.sql`-- Update schema in `apps/server/src/assets/db/schema.sql`-- Never bypass Becca cache after migrations--## Common Pitfalls--1. **Never bypass the cache layers** - Always use `becca.notes[noteId]`, `froca.getNote()`, or equivalent cache methods. Direct database queries will cause sync issues between Becca/Froca/Shaca and won't create EntityChange records needed for synchronization.--2. **Protected notes require session check** - Before accessing `note.title` or `note.getContent()` on protected notes, check `note.isContentAvailable()` or use `note.getTitleOrProtected()` which handles this automatically.--3. **Widget lifecycle matters** - Override `refreshWithNote()` for note changes, `doRenderBody()` for initial render, `entitiesReloadedEvent()` for entity updates. Widgets use jQuery (`this.$widget`) - don't mix React patterns.--4. **Tests run differently** - Server tests must run sequentially (shared database state), client tests can run in parallel. Use `pnpm test:sequential` for backend, `pnpm test:parallel` for frontend.--5. **ETAPI requires authentication** - ETAPI endpoints use basic auth with tokens. Internal API endpoints (`apps/server/src/routes/api/`) trust the frontend when `noAuthentication=true`.--6. **Search expressions are evaluated in memory** - The search service loads all matching notes, scores them in JavaScript, then sorts. You cannot add SQL-level LIMIT/OFFSET without losing scoring functionality.--7. **Documentation edits have rules** - The Script API reference is auto-generated by `apps/build-docs` into the gitignored `site/` (published to docs.triliumnotes.org); it is not committed and must not be hand-edited. `docs/User Guide/` should be edited via `pnpm edit-docs:edit-docs`, not manually. Only `docs/Developer Guide/` and `docs/Release Notes/` are safe for direct Markdown editing.--8. **pnpm workspace filtering** - Use `pnpm --filter server <command>` or shorthand `pnpm server:test` defined in root `package.json`. Note the `--filter` syntax, not `-F` or other shortcuts.--9. **Event subscription cleanup** - When subscribing to events in widgets, unsubscribe in `cleanup()` or `doDestroy()` to prevent memory leaks.--10. **Attribute inheritance can be complex** - When checking for labels/relations, use `note.getOwnedAttribute()` for direct attributes or `note.getAttribute()` for inherited ones. Don't assume attributes are directly on the note.--11. **`ELECTRON_RUN_AS_NODE` leak crashes Electron launches** - Shells spawned by Electron-based tools (the VS Code extension host, AI coding agents running inside it) often inherit `ELECTRON_RUN_AS_NODE=1`. With it set, launching the desktop app (`pnpm desktop:start`, `pnpm --filter desktop start-prod`) crashes with `TypeError: Not running in an Electron environment!` because `require("electron")` resolves to the npm stub's path string instead of the built-in module. Unset the variable before launching: `Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue` (PowerShell) or `unset ELECTRON_RUN_AS_NODE` (bash).--## MCP Server-- Trilium exposes an MCP (Model Context Protocol) server at `http://localhost:8080/mcp`, configured in `.mcp.json`-- The MCP server is **only available when the Trilium server is running** (`pnpm run server:start`)-- It provides tools for reading, searching, and modifying notes directly from the AI assistant-- Use it to interact with actual note data when developing or debugging note-related features--## TypeScript Configuration--- **Project references**: Monorepo uses TypeScript project references (`tsconfig.json`)-- **Path mapping**: Use relative imports, not path aliases-- **Build order**: `pnpm typecheck` builds all projects in dependency order-- **Build system**: Uses Vite for fast development, ESBuild for production optimization-- **Patches**: Custom patches in `patches/` directory for CKEditor and other dependencies-- **No non-null assertions**: Never use the TypeScript non-null assertion operator (postfix `!`), including in tests. Narrow instead — optional chaining (`?.`), a `?? fallback`, an explicit null check, or an `*OrThrow` accessor (e.g. `becca.getNoteOrThrow(id)` rather than `becca.getNote(id)!`).-- **Helper placement**: When extracting a standalone helper function from a component, widget, hook, or route, place it below the primary export it supports (or in a separate module), not wedged between the imports and the main definition. The file's primary export stays near the top; supporting helpers follow it.--## Key Files for Context--- `apps/server/src/becca/entities/bnote.ts` - Note entity methods-- `apps/client/src/services/froca.ts` - Frontend cache API-- `apps/server/src/services/search/services/search.ts` - Search implementation-- `apps/server/src/routes/routes.ts` - API route registration-- `apps/client/src/widgets/basic_widget.ts` - Widget base class-- `apps/server/src/main.ts` - Server startup entry point-- `apps/client/src/desktop.ts` - Client initialization-- `apps/server/src/services/backend_script_api.ts` - Scripting API-- `apps/server/src/assets/db/schema.sql` - Database schema--## Note Types and Features--Trilium supports multiple note types with specialized widgets in `apps/client/src/widgets/type_widgets/`:-- **Text**: Rich text with CKEditor5 (markdown import/export)-- **Code**: Syntax-highlighted code editing with CodeMirror-- **File**: Binary file attachments-- **Image**: Image display with editing capabilities-- **Canvas**: Drawing/diagramming with Excalidraw-- **Mermaid**: Diagram generation-- **Relation Map**: Visual note relationship mapping-- **Web View**: Embedded web pages-- **Doc/Book**: Hierarchical documentation structure--### Collections-Notes can be marked with the `#collection` label to enable collection view modes. Collections support multiple view types:-- **List**: Standard list view-- **Grid**: Card/grid layout-- **Calendar**: Calendar-based view-- **Table**: Tabular data view-- **GeoMap**: Geographic map view-- **Board**: Kanban-style board-- **Presentation**: Slideshow presentation mode--View types are configured via `#viewType` label (e.g., `#viewType=table`). Each view mode stores its configuration in a separate attachment (e.g., `table.json`). Collections are organized separately from regular note type templates in the note creation menu.--## Common Development Tasks--### Adding New Note Types-1. Create widget in `apps/client/src/widgets/type_widgets/`-2. Register in `apps/client/src/services/note_types.ts`-3. Add backend handling in `apps/server/src/services/notes.ts`--### Extending Search-- Search expressions handled in `apps/server/src/services/search/`-- Add new search operators in search context files-- Remember: scoring happens in-memory, not at database level--### Custom CKEditor Plugins-- Create new package in `packages/` following existing plugin structure-- Register in `packages/ckeditor5/src/plugins.ts`-- See `ckeditor5-admonition`, `ckeditor5-footnotes`, `ckeditor5-math`, `ckeditor5-mermaid` for examples--### Updating PDF.js-1. Update `pdfjs-dist` version in `packages/pdfjs-viewer/package.json`-2. Run `npx tsx scripts/update-viewer.ts` from that directory-3. Run `pnpm build` to verify success-4. Commit all changes including updated viewer files--### Database Migrations-- Add migration scripts in `apps/server/src/migrations/YYMMDD_HHMM__description.sql`-- Update schema in `apps/server/src/assets/db/schema.sql`-- Never bypass Becca cache after migrations--## Security & Features--### Security Considerations-- Per-note encryption with granular protected sessions-- CSRF protection for API endpoints-- OpenID and TOTP authentication support-- Sanitization of user-generated content--### Scripting System-Trilium provides powerful user scripting capabilities:-- **Frontend scripts**: Run in browser context with UI access-- **Backend scripts**: Run in Node.js context with full API access-- Script API documentation generated by `apps/build-docs` into `site/script-api/` (published to docs.triliumnotes.org)-- Backend API available via `api` object in script context--### Internationalization-- Translation files in `apps/client/src/translations/`-- Use translation system via `t()` function-- Automatic pluralization: Add `_other` suffix to translation keys (e.g., `item` and `item_other` for singular/plural)-- When a translated string contains **interpolated components** (e.g. links, note references) whose order may vary across languages, use `<Trans>` from `react-i18next` instead of `t()`. This lets translators reorder components freely (e.g. `"<Note/> in <Parent/>"` vs `"in <Parent/>, <Note/>"`)-- When adding a new locale, follow the step-by-step guide in `docs/Developer Guide/Developer Guide/Concepts/Internationalisation Translations/Adding a new locale.md`--#### Client vs Server Translation Usage-- **Client-side**: `import { t } from "../services/i18n"` with keys in `apps/client/src/translations/en/translation.json`-- **Server-side**: `import { t } from "i18next"` with keys in `apps/server/src/assets/translations/en/server.json`-- **Electron main process** (e.g. `apps/desktop/src/`): `import { t } from "i18next"` — uses server-side keys from `apps/server/src/assets/translations/en/server.json` (same as server-side). **Never hardcode user-facing strings** in Electron dialogs, tray menus, or IPC handlers — always use `t()`.-- **Interpolation**: Use `{{variable}}` for normal interpolation; use `{{- variable}}` (with hyphen) for **unescaped** interpolation when the value contains special characters like quotes that shouldn't be HTML-escaped--### Storing User Preferences-- **Do not use `localStorage`** for user preferences — Trilium has a synced options system that persists across devices-- To add a new user preference:- 1. Add the option type to `OptionDefinitions` in `packages/commons/src/lib/options_interface.ts`- 2. Add a default value in `apps/server/src/services/options_init.ts` in the `defaultOptions` array- 3. **Whitelist the option** in `apps/server/src/routes/api/options.ts` by adding it to `ALLOWED_OPTIONS` (required for client updates)- 4. Use `useTriliumOption("optionName")` hook in React components to read/write the option-- Available hooks: `useTriliumOption` (string), `useTriliumOptionBool`, `useTriliumOptionInt`, `useTriliumOptionJson`-- See `docs/Developer Guide/Developer Guide/Concepts/Options/Creating a new option.md` for detailed documentation--## Testing Conventions--- **Write concise tests**: Group related assertions together in a single test case rather than creating many one-shot tests-- **Extract and test business logic**: When adding pure business logic (e.g., data transformations, migrations, validations), extract it as a separate function and always write unit tests for it--```typescript-// ETAPI test pattern-describe("etapi/feature", () => {- beforeAll(async () => {- config.General.noAuthentication = false;- app = await buildApp();- token = await login(app);- });-- it("should test feature", async () => {- const response = await supertest(app)- .get("/etapi/notes?search=test")- .auth(USER, token, { type: "basic" })- .expect(200);-- expect(response.body.results).toBeDefined();- });-});-```--## Questions to Verify Understanding--Before implementing significant changes, confirm:-- Is this touching the cache layer? (Becca/Froca/Shaca must stay in sync via EntityChange records)-- Does this change API response shape? (Check backwards compatibility for ETAPI)-- Are you adding search features? (Understand expression-based architecture and in-memory scoring first)-- Is this a new widget? (Know which base class and lifecycle methods to use)-- Does this involve protected notes? (Check `isContentAvailable()` before accessing content)-- Is this a long-running operation? (Use TaskContext for progress reporting)-- Are you working with attributes? (Understand inheritance patterns: direct, child-prefix, template)diff --git a/CLAUDE.md b/CLAUDE.mdindex 20dbad921e..09fceb5716 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -2,8 +2,6 @@This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.-> **Note**: When updating this file, also update `.github/copilot-instructions.md` to keep both AI coding assistants in sync.-## OverviewTrilium Notes is a hierarchical note-taking application with synchronization, scripting, and rich text editing. TypeScript monorepo using pnpm with multiple apps and shared packages.