All files / src/services modelService.ts

21.67% Statements 62/286
100% Branches 1/1
8.33% Functions 1/12
21.67% Lines 62/286

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 2871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x                           1x 1x                           1x 1x                 1x 1x                                     1x 1x 1x 1x 1x                                       1x 1x 1x 1x 1x                                                 1x 1x                                                                                       1x 1x                                               1x 1x                                                               1x 1x                                   1x  
import { v4 as uuidv4 } from 'uuid';
 
import { IModelSettingsRepository } from '../contracts/IModelSettingsRepository';
import { IOllamaClient } from '../contracts/IOllamaClient';
import { Logger } from '../logger';
import { ModelInstance } from '../models/modelInstance';
import { ChatService } from './chatService';
 
export interface ModelSettings {
    systemMessage?: string;
    userMessagePrefix?: string;
    userMessageSuffix?: string;
    systemTurnPrefix?: string;
    systemTurnSuffix?: string;
    /** Version of the data structure */
    dataVersion?: number;
    /** Allow for unknown fields to be preserved */
    [key: string]: unknown;
}
 
export class ModelService {
    public static readonly DEFAULT_SYSTEM_MESSAGE = 'You are a helpful AI assistant.';
    public static readonly CURRENT_VERSION = 2;
 
    constructor(
        private repository: IModelSettingsRepository,
        private api: IOllamaClient,
        private chatService: ChatService,
    ) {}
 
    /**
     * Gets settings for a specific instance.
     * If instanceId matches a modelName, it returns the primary instance for that model.
     */
    getSettings(instanceIdOrModelName: string): ModelInstance {
        const allSettings = this.repository.getAll();
        let entry = allSettings[instanceIdOrModelName];

        if (!entry) {
            // Return a default instance for the model name
            return this.createDefaultInstance(instanceIdOrModelName);
        }

        // Migration from V1 (ModelSettings) to V2 (ModelInstance)
        if (!entry.id || !entry.modelName || entry.isManaged === undefined) {
            entry = this.migrateToInstance(instanceIdOrModelName, entry as unknown as ModelSettings);
        }

        return entry as ModelInstance;
    }
 
    private createDefaultInstance(modelName: string): ModelInstance {
        return {
            id: modelName,
            name: modelName,
            modelName: modelName,
            ollamaModelName: modelName, // Primary instance uses the base name
            config: {},
            systemMessage: ModelService.DEFAULT_SYSTEM_MESSAGE,
            isManaged: false, // Default instances are base models
            createdAt: Date.now(),
            updatedAt: Date.now(),
            dataVersion: ModelService.CURRENT_VERSION,
        };
    }
 
    private migrateToInstance(modelName: string, legacy: ModelSettings): ModelInstance {
        return {
            ...legacy,
            id: modelName,
            name: modelName,
            modelName: modelName,
            ollamaModelName: modelName,
            config: {},
            isManaged: false,
            createdAt: Date.now(),
            updatedAt: Date.now(),
            dataVersion: ModelService.CURRENT_VERSION,
        } as ModelInstance;
    }
 
    async setSystemMessage(instanceId: string, message: string): Promise<void> {
        const allSettings = this.repository.getAll();
        const instance = this.getSettings(instanceId);
        instance.systemMessage = message;
        instance.updatedAt = Date.now();

        allSettings[instanceId] = instance;
        await this.repository.save(allSettings);
    }
 
    async setSettings(instanceId: string, settings: Partial<ModelInstance>): Promise<void> {
        const allSettings = this.repository.getAll();
        const existing = this.getSettings(instanceId);

        const updated: ModelInstance = {
            ...existing,
            ...settings,
            updatedAt: Date.now(),
            dataVersion: ModelService.CURRENT_VERSION,
        };

        // If it's a managed instance (not the primary one), update the model in Ollama
        if (updated.ollamaModelName && updated.ollamaModelName !== updated.modelName) {
            await this.syncWithOllama(updated);
        }

        allSettings[instanceId] = updated;
        await this.repository.save(allSettings);
    }
 
    /**
     * Lists all instances for a specific base model.
     */
    getInstancesForModel(modelName: string): ModelInstance[] {
        const allSettings = this.repository.getAll();
        const instances: ModelInstance[] = [];

        // Always include the default instance
        const defaultInstance = this.getSettings(modelName);
        instances.push(defaultInstance);

        for (const key in allSettings) {
            if (key === modelName) {
                continue;
            }
            const entry = allSettings[key];
            if (entry.modelName === modelName) {
                instances.push(entry);
            }
        }

        return instances.sort((a, b) => a.createdAt - b.createdAt);
    }
 
    /**
     * Finds an instance (primary or custom) by its internal Ollama name.
     */
    getInstanceByOllamaName(ollamaModelName: string): ModelInstance | undefined {
        const allSettings = this.repository.getAll();
        const nameToMatch = ollamaModelName.replace(/:latest$/, '');

        // 1. Check primary instances (keys)
        if (allSettings[ollamaModelName]) {
            return this.getSettings(ollamaModelName);
        }
        if (allSettings[nameToMatch]) {
            return this.getSettings(nameToMatch);
        }

        // 2. Check all custom instances
        for (const key in allSettings) {
            const instance = allSettings[key];
            if (instance.ollamaModelName) {
                const managedName = instance.ollamaModelName.replace(/:latest$/, '');
                if (managedName === nameToMatch || instance.ollamaModelName === ollamaModelName) {
                    return instance;
                }
            }
        }

        return undefined;
    }
 
    async createInstance(modelName: string, name: string): Promise<ModelInstance> {
        const allSettings = this.repository.getAll();

        // Ensure name uniqueness within our UI
        const instances = this.getInstancesForModel(modelName);
        let finalName = name;
        let counter = 1;
        while (instances.some((i) => i.name === finalName)) {
            finalName = `${name} (${++counter})`;
        }

        // Generate a valid Ollama model name (slug)
        const slug = finalName
            .toLowerCase()
            .replace(/[^a-z0-9]/g, '-')
            .replace(/-+/g, '-')
            .replace(/^-|-$/g, '');
        let ollamaName = `${modelName.split(':')[0]}-${slug}`;

        // Ensure ollamaName uniqueness by checking with API or local state
        if (Object.values(allSettings).some((inst: ModelInstance) => inst.ollamaModelName === ollamaName)) {
            ollamaName = `${ollamaName}-${uuidv4().substring(0, 4)}`;
        }

        const newInstance: ModelInstance = {
            id: uuidv4(),
            name: finalName,
            modelName: modelName,
            ollamaModelName: ollamaName,
            config: {},
            systemMessage: ModelService.DEFAULT_SYSTEM_MESSAGE,
            isManaged: true, // Custom instances are managed
            createdAt: Date.now(),
            updatedAt: Date.now(),
            dataVersion: ModelService.CURRENT_VERSION,
        };

        // Create the model in Ollama
        await this.syncWithOllama(newInstance);

        allSettings[newInstance.id] = newInstance;
        await this.repository.save(allSettings);
        return newInstance;
    }
 
    async deleteSettings(instanceId: string): Promise<void> {
        const allSettings = this.repository.getAll();
        const instance = allSettings[instanceId];

        if (instance) {
            // Delete from Ollama if it's a managed instance
            if (instance.ollamaModelName && instance.ollamaModelName !== instance.modelName) {
                try {
                    await this.api.deleteModel(instance.ollamaModelName);
                } catch (e: unknown) {
                    Logger.error(`Failed to delete managed model ${instance.ollamaModelName} from Ollama`, e);
                }
            }

            // Cascading delete for chats
            await this.chatService.deleteChatsForModel(instanceId);

            delete allSettings[instanceId];
            await this.repository.save(allSettings);
        } else {
            // Even if no settings found, try to cleanup chats for this ID (might be primary instance ID)
            await this.chatService.deleteChatsForModel(instanceId);
        }
    }
 
    async cleanupOrphanedSettings(activeModelNames: string[]): Promise<void> {
        const allSettings = this.repository.getAll();
        const activeSet = new Set(activeModelNames);
        let changed = false;

        for (const key in allSettings) {
            const entry = allSettings[key];
            const baseModelName = entry.modelName || key;

            if (!activeSet.has(baseModelName)) {
                // If we delete the base model setting, we should probably delete all its instances too
                // and their Ollama models.
                if (entry.ollamaModelName && entry.ollamaModelName !== entry.modelName) {
                    try {
                        await this.api.deleteModel(entry.ollamaModelName);
                    } catch (e: unknown) {
                        Logger.error(`Failed to cleanup orphaned managed model ${entry.ollamaModelName}`, e);
                    }
                }

                // Cascading delete for chats of the orphaned instance
                await this.chatService.deleteChatsForModel(key);

                delete allSettings[key];
                changed = true;
            }
        }

        if (changed) {
            await this.repository.save(allSettings);
        }
    }
 
    private async syncWithOllama(instance: ModelInstance): Promise<void> {
        if (!instance.ollamaModelName || instance.ollamaModelName === instance.modelName) {
            return;
        }

        // Based on latest manual validation:
        // Use structured parameters object instead of raw Modelfile where possible
        const options = {
            model: instance.ollamaModelName,
            from: instance.modelName,
            system: instance.systemMessage,
            parameters: {
                ...instance.config,
            },
        };

        await this.api.createModel(options);
    }
}