Skip to content

Latest commit

 

History

History
280 lines (232 loc) · 8.77 KB

File metadata and controls

280 lines (232 loc) · 8.77 KB

Prompt Library Implementation Summary

Overview

A new reusable package called prompt-library has been created to manage custom prompts with localStorage persistence. This package is now integrated into the ByteChat browser extension's Agent Chat feature.

Package Structure

New Package: prompt-library

Location: /Users/sauravverma/programs/bytebellpackages/prompt-library/

prompt-library/
├── src/
│   ├── index.ts                    # Main exports
│   ├── types.ts                    # TypeScript interfaces
│   ├── PromptLibrary.ts            # Core storage & CRUD operations
│   ├── components/
│   │   ├── PromptSelector.tsx      # Dropdown UI component
│   │   └── PromptManager.tsx       # Create/Edit modal component
│   └── hooks/
│       └── usePromptLibrary.ts     # React hook for state management
├── package.json
├── tsconfig.json
└── README.md

Core Features

1. PromptLibrary Class (Core Logic)

Static class that handles all CRUD operations with localStorage:

  • loadAll() - Load all saved prompts
  • create(input) - Create a new prompt
  • getById(id) - Get a specific prompt
  • update(id, input) - Update an existing prompt
  • delete(id) - Delete a prompt
  • search(query) - Search prompts by name or content
  • count() - Get total number of prompts
  • clearAll() - Delete all prompts

2. Data Types

interface CustomPrompt {
  id: string;              // Unique identifier
  name: string;            // Display name
  prompt: string;          // The actual prompt text
  createdAt: number;       // Timestamp
  updatedAt: number;       // Timestamp
}

interface PromptInput {
  name: string;            // Prompt name
  prompt: string;          // Prompt text
}

3. React Components

PromptSelector

A dropdown component that shows:

  • "None (Default behavior)" option
  • List of all saved prompts
  • "+ Create New Prompt" option
  • Edit button (appears when a prompt is selected)

PromptManager

A modal dialog for creating/editing prompts with:

  • Two simple fields: Name and Prompt Text
  • Preview section showing how the prompt will look
  • Save, Cancel, and Delete buttons
  • Input validation

4. React Hook

usePromptLibrary() - Provides state management:

const {
  prompts,        // Array of all prompts
  loading,        // Loading state
  error,          // Error message
  createPrompt,   // Function to create
  updatePrompt,   // Function to update
  deletePrompt,   // Function to delete
  getPrompt,      // Function to get by ID
  searchPrompts,  // Function to search
  refresh         // Function to reload
} = usePromptLibrary();

Integration with ByteChat

1. ConversationalAgent Updates

File: bytechat-browser-agent/src/conversational/ConversationalAgent.ts

Changes made:

  • Added customPrompt?: string to AgentConfig interface
  • Modified sendMessage() to prepend custom prompt before processing
  • Added setCustomPrompt(prompt) method to update prompt dynamically
  • Added getCustomPrompt() method to retrieve current prompt

How it works:

// When a custom prompt is set:
const processedInput = this.config.customPrompt
  ? `${this.config.customPrompt}\n\nUser: ${userInput}`
  : userInput;

// This processed input is sent to the LLM

2. AgentChat Component Updates

File: ByteChat/src/components/AgentChat.tsx

Changes made:

  1. Added prompt library imports
  2. Added state for prompt management:
    • selectedPromptId - Currently selected prompt
    • isPromptModalOpen - Modal visibility
    • editingPrompt - Prompt being edited
  3. Added usePromptLibrary() hook
  4. Added effect to update agent when prompt selection changes
  5. Added handler functions for prompt CRUD operations
  6. Added UI components:
    • <PromptSelector /> above the message input
    • <PromptManager /> modal for creating/editing

User Workflow

Creating a Custom Prompt

  1. Click the "Custom Prompt" dropdown in Agent Chat
  2. Select "+ Create New Prompt"
  3. Enter a name (e.g., "Code Review Assistant")
  4. Enter the detailed prompt text
  5. Preview shows how it will be formatted
  6. Click "Create Prompt"

Using a Custom Prompt

  1. Select a prompt from the "Custom Prompt" dropdown
  2. The prompt is automatically applied to the agent
  3. All subsequent messages will be prepended with this prompt
  4. Format: {custom_prompt}\n\nUser: {user_message}

Editing a Custom Prompt

  1. Select a prompt from the dropdown
  2. Click the edit (pencil) icon that appears
  3. Modify the name or prompt text
  4. Click "Update Prompt"

Deleting a Custom Prompt

  1. Edit a prompt (as above)
  2. Click "Delete Prompt" button
  3. Confirm the deletion

Storage

Location: Browser's localStorage Key: prompt-library-v1 Format: JSON array of CustomPrompt objects

Example:

[
  {
    "id": "prompt_1729692000000_abc123",
    "name": "Code Review Assistant",
    "prompt": "You are an expert code reviewer. Focus on security, performance, and best practices.",
    "createdAt": 1729692000000,
    "updatedAt": 1729692000000
  }
]

Technical Details

Dependencies Added

  • ByteChat package.json:
    "prompt-library": "file:../prompt-library"

Build Process

  1. Build prompt-library: npm run build (in prompt-library directory)
  2. Build bytechat-browser-agent: npm run build (in bytechat-browser-agent directory)
  3. Install in ByteChat: npm install (in ByteChat directory)
  4. Build ByteChat: npm run build

Package Exports

The prompt-library package exports:

  • PromptLibrary - Core class
  • CustomPrompt, PromptInput, PromptOperationResult - Types
  • PromptSelector - React component
  • PromptManager - React component
  • usePromptLibrary - React hook
  • STORAGE_KEY - Constant

Benefits

  1. Reusability: The prompt-library package can be used in any React application
  2. Type Safety: Full TypeScript support with interfaces
  3. Persistence: Prompts are saved in localStorage and survive browser restarts
  4. User-Friendly: Simple two-field interface (name + prompt)
  5. Validation: Input validation prevents empty or duplicate names
  6. Preview: Users can see how their prompt will be formatted
  7. Flexibility: Prompts can be created, edited, deleted, and toggled on/off

Example Use Cases

1. Code Review Assistant

Name: Code Review Assistant
Prompt: You are an expert code reviewer. Please focus on:
- Security vulnerabilities
- Performance issues
- Code quality and best practices
- Potential bugs
Provide constructive feedback with specific suggestions.

2. Technical Writer

Name: Technical Documentation
Prompt: You are a technical writer. When explaining concepts:
- Use clear, concise language
- Include practical examples
- Break down complex topics
- Focus on clarity over brevity

3. Friendly Assistant

Name: Friendly Helper
Prompt: You are a friendly and encouraging assistant. Be:
- Patient and understanding
- Enthusiastic about helping
- Clear in your explanations
- Supportive when users struggle

Future Enhancements (Optional)

  1. Import/Export: Allow users to export/import prompt collections
  2. Categories/Tags: Organize prompts by category
  3. Sharing: Share prompts between team members
  4. Variables: Support template variables in prompts (e.g., {{username}})
  5. History: Track prompt usage statistics
  6. Cloud Sync: Sync prompts across devices
  7. Marketplace: Community-shared prompt library

Testing

To test the implementation:

  1. Navigate to the Agent Chat tab in ByteChat
  2. Look for the "Custom Prompt" dropdown above the message input
  3. Click "+ Create New Prompt"
  4. Create a test prompt (e.g., "Be concise and direct")
  5. Select the prompt from the dropdown
  6. Send a message - it will be prepended with your custom prompt
  7. Check the console logs to verify the prompt is being applied

Files Modified/Created

Created:

  • prompt-library/src/index.ts
  • prompt-library/src/types.ts
  • prompt-library/src/PromptLibrary.ts
  • prompt-library/src/components/PromptSelector.tsx
  • prompt-library/src/components/PromptManager.tsx
  • prompt-library/src/hooks/usePromptLibrary.ts
  • prompt-library/package.json
  • prompt-library/tsconfig.json
  • prompt-library/README.md

Modified:

  • bytechat-browser-agent/src/conversational/ConversationalAgent.ts
  • ByteChat/src/components/AgentChat.tsx
  • ByteChat/package.json

Conclusion

The prompt library package is now fully functional and integrated into the ByteChat extension. Users can create, edit, and manage custom prompts that will be automatically prepended to their messages when sending to the OpenRouter LLM. The implementation is clean, reusable, and follows React best practices with TypeScript for type safety.