Preferences

Overview

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.

Implementation

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

CREATE TABLE model_preferences (
  agent_id TEXT NOT NULL,
  chat_id INTEGER NOT NULL,
  model_type TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (agent_id, chat_id)
);

CREATE INDEX idx_model_preferences_agent ON model_preferences(agent_id);
CREATE INDEX idx_model_preferences_chat ON model_preferences(chat_id);

Usage

Getting Model Preference

const modelType = await ModelPreferencesService.getModelPreference(
  'agent-123',
  456
);

if (modelType) {
  console.log(`Using preferred model: ${modelType}`);
} else {
  console.log('Using default model');
}

Setting Model Preference

const success = await ModelPreferencesService.setModelPreference(
  'agent-123',
  456,
  'GEMINI'
);

if (!success) {
  console.error('Failed to set model preference');
}

Removing Model Preference

await ModelPreferencesService.deleteModelPreference(
  'agent-123',
  456
);

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