obsidian-copilot icon indicating copy to clipboard operation
obsidian-copilot copied to clipboard

feat: Add comprehensive system prompt management system.

Open Emt-lin opened this issue 1 month ago • 0 comments

Summary

This PR introduces a comprehensive file-based system prompt management system that replaces the legacy single system prompt setting with a flexible, multi-prompt architecture.

Key Features

  • File-Based Storage: System prompts are stored as .md files in a configurable folder
  • CRUD Operations: Full create, read, update, delete, and duplicate functionality
  • Clean Architecture: Repository → Manager → UIState → UI pattern with Jotai state management
  • Automatic Migration: Seamlessly migrates existing userSystemPrompt from settings to file system
  • Session & Global Prompts: Supports both session-level (temporary) and global default (persistent) prompt selection
  • Rich UI: New management modal and chat settings integration with proper error handling and user feedback

Architecture Highlights

Core Components

  1. SystemPromptManager (src/system-prompts/systemPromptManager.ts)

    • Singleton pattern for centralized CRUD operations
    • Manages file operations via Obsidian Vault API
    • Integrates with state management layer
  2. State Management (src/system-prompts/state.ts)

    • Jotai-based reactive state with independent store
    • Dual API: React hooks (reactive) + imperative functions (non-React)
    • Session-level and persistent settings support
  3. Migration System (src/system-prompts/migration.ts)

    • Automatic migration from legacy userSystemPrompt setting
    • User-friendly notification modal
    • Safe handling of existing files
  4. Utilities (src/system-prompts/systemPromptUtils.ts)

    • File operations and validation
    • Frontmatter management
    • Name generation and conflict resolution

UI Components

  • SystemPromptManagerModal: Full-featured management interface

    • Add new prompts
    • Edit existing prompts
    • Delete with confirmation
    • Duplicate prompts
    • Success/error notifications
  • ChatSettingsPopover: Enhanced chat settings

    • System prompt selector dropdown
    • Session-level prompt override
    • Model parameter customization
    • Clear warnings for disabling builtin prompts

Breaking Changes

None. This PR includes automatic migration from the legacy userSystemPrompt setting to the new file-based system.

Test Plan

Functionality Testing

  • [x] Create new system prompts via UI
  • [x] Edit existing system prompts (title and content)
  • [x] Delete system prompts with proper error handling
  • [x] Duplicate system prompts with automatic name generation
  • [x] Session-level prompt selection in chat settings
  • [x] Global default prompt configuration in settings
  • [x] Migration from legacy userSystemPrompt setting
  • [x] File system operations (create, read, update, delete)
  • [x] Frontmatter metadata handling
  • [x] Error handling and user notifications (Notice)

Unit Tests

  • [x] All existing tests pass
  • [x] New test coverage: 1,671 lines across 4 test files
  • [x] Migration scenarios tested
  • [x] State management tested
  • [x] CRUD operations tested
  • [x] Utility functions tested

Integration Testing

  • [x] Works with existing chat functionality
  • [x] Compatible with all LLM providers
  • [x] Session prompts reset correctly on new chat/load history
  • [x] Global defaults persist across sessions

Screenshots

https://github.com/user-attachments/assets/6f30f847-6899-482d-8ef1-2670eab7d286

Migration Notes

For users upgrading to this version:

  1. Existing custom system prompt (userSystemPrompt in settings) will be automatically migrated to a file named "Migrated Custom System Prompt" in the system prompts folder
  2. The migrated prompt will be set as the global default automatically
  3. A notification modal will inform users about the upgrade
  4. No manual intervention required

Emt-lin avatar Oct 27 '25 12:10 Emt-lin