Preferences

The Model Preferences system allows per-chat model selection for agents with persistent storage in PostgreSQL. This enables dynamic model switching and preference management at runtime.

Model Types

export type ModelType = 'OPENAI' | 'GEMINI' | 'GROK' | 'DEEPSEEK';

Service Interface

class ModelPreferencesService {
  static async getModelPreference(
    agentId: string, 
    chatId: number
  ): Promise<ModelType | null>;

  static async setModelPreference(
    agentId: string, 
    chatId: number, 
    modelType: ModelType
  ): Promise<boolean>;

  static async deleteModelPreference(
    agentId: string, 
    chatId: number
  ): Promise<boolean>;
}

Database Schema

Usage

Getting Model Preference

Setting Model Preference

Removing Model Preference

Error Handling

The service implements graceful error handling:

  • Returns null when no preference exists

  • Returns false for failed operations

  • Throws typed errors for database failures

  • Validates input parameters

Best Practices

Data Access

  • Use prepared statements

  • Implement connection pooling

  • Handle concurrent updates

  • Validate input parameters

Performance

  • Leverage database indexes

  • Cache frequent lookups

  • Batch operations when possible

  • Monitor query performance

Last updated