All files / ollama-view/src ollamaProvider.ts

92.16% Statements 200/217
80.76% Branches 42/52
93.33% Functions 14/15
92.16% Lines 200/217

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 2181x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x 1x 1x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 54x 54x 54x 69x 69x 69x 69x 69x 15x 69x   54x   54x 54x 54x 54x 54x 54x 69x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 90x 90x 1x 1x 2x 2x 1x 1x 4x 4x 2x 2x 2x 2x 4x 4x 1x 1x 2x 2x 1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 90x 90x 1x 1x 61x 61x 61x     61x 61x 20x 20x 20x 20x 41x 61x 23x 23x 23x 46x 23x 23x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 67x 67x 44x 44x 23x 23x 18x 18x 18x 18x 61x 44x 44x 21x 21x 44x 23x 23x 23x 44x 18x 18x 18x 1x 1x 67x 67x 67x 67x 67x 67x 67x 1x 1x 5x 5x 1x  
import * as path from 'path';
import * as vscode from 'vscode';
 
import { IOllamaClient } from './contracts/IOllamaClient';
import { ModelInstance } from './models/modelInstance';
import { OllamaModel } from './ollamaApi';
import { Chat, ChatService } from './services/chatService';
import { ModelService } from './services/modelService';
 
export class OllamaChatItem extends vscode.TreeItem {
    constructor(public readonly chat: Chat) {
        super(chat.name, vscode.TreeItemCollapsibleState.None);
        this.tooltip = `Created: ${new Date(chat.createdAt).toLocaleString()}`;
        this.description = ''; // Maybe last message snippet?
        this.contextValue = 'ollama-chat';
        this.iconPath = new vscode.ThemeIcon('comment-discussion');

        // Command to open chat
        this.command = {
            command: 'ollamaView.openChat',
            title: 'Open Chat',
            arguments: [this],
        };
    }
}
 
export class OllamaInstanceItem extends vscode.TreeItem {
    constructor(
        public readonly instance: ModelInstance,
        public readonly isRunning: boolean,
        public readonly isStarting: boolean = false,
        public readonly isStopping: boolean = false,
        public readonly isRoot: boolean = false,
    ) {
        super(instance.name, vscode.TreeItemCollapsibleState.Collapsed);
 
        const statusText = isRunning ? 'Running' : isStarting ? 'Starting' : isStopping ? 'Stopping' : 'Stopped';
        this.tooltip = `${instance.name} (${instance.modelName})\nStatus: ${statusText}`;
        this.description = isRunning ? 'Running' : isStarting ? 'Starting...' : isStopping ? 'Stopping...' : 'Stopped';
 
        // Context value for menus
        const baseContext = isRunning
            ? 'variable-running'
            : isStarting
              ? 'variable-starting'
              : isStopping
                ? 'variable-stopping'
                : 'variable-stopped';
 
        this.contextValue = isRoot ? `${baseContext}-root` : baseContext;
 
        if (isRunning) {
            this.iconPath = vscode.Uri.file(path.join(__dirname, '..', 'media', 'model-icon-running.svg'));
        } else if (isStarting) {
            this.iconPath = vscode.Uri.file(path.join(__dirname, '..', 'media', 'model-icon-starting.svg'));
        } else if (isStopping) {
            this.iconPath = vscode.Uri.file(path.join(__dirname, '..', 'media', 'model-icon-stopping.svg'));
        } else {
            this.iconPath = {
                light: vscode.Uri.file(path.join(__dirname, '..', 'media', 'model-icon-light.svg')),
                dark: vscode.Uri.file(path.join(__dirname, '..', 'media', 'model-icon-dark.svg')),
            };
        }
    }
}
 
export class OllamaModelItem extends vscode.TreeItem {
    constructor(public readonly model: OllamaModel) {
        // Collapsible to show instances
        super(model.name, vscode.TreeItemCollapsibleState.Collapsed);
 
        this.tooltip = `${model.name}\nSize: ${(model.size / 1024 / 1024 / 1024).toFixed(2)} GB`;
        this.contextValue = 'ollama-model';
        this.iconPath = new vscode.ThemeIcon('library');
    }
}
 
export class OllamaProvider implements vscode.TreeDataProvider<OllamaModelItem | OllamaInstanceItem | OllamaChatItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<
        OllamaModelItem | OllamaInstanceItem | OllamaChatItem | undefined | null | void
    > = new vscode.EventEmitter<OllamaModelItem | OllamaInstanceItem | OllamaChatItem | undefined | null | void>();
    readonly onDidChangeTreeData: vscode.Event<
        OllamaModelItem | OllamaInstanceItem | OllamaChatItem | undefined | null | void
    > = this._onDidChangeTreeData.event;
 
    private runningModels: Set<string> = new Set();
    private startingModels: Set<string> = new Set();
    private stoppingModels: Set<string> = new Set();
 
    constructor(
        private chatService: ChatService,
        private modelService: ModelService,
        private api: IOllamaClient,
    ) {}
 
    refresh(): void {
        this._onDidChangeTreeData.fire();
    }
 
    private normalizeName(name: string): string {
        return name.replace(/:latest$/, '');
    }
 
    isModelRunning(modelName: string): boolean {
        return this.runningModels.has(this.normalizeName(modelName));
    }
 
    setStarting(modelName: string, starting: boolean) {
        const name = this.normalizeName(modelName);
        if (starting) {
            this.startingModels.add(name);
        } else {
            this.startingModels.delete(name);
        }
        this.refresh();
    }
 
    setStopping(modelName: string, stopping: boolean) {
        const name = this.normalizeName(modelName);
        if (stopping) {
            this.stoppingModels.add(name);
        } else {
            this.stoppingModels.delete(name);
        }
        this.refresh();
    }
 
    async startModel(modelName: string): Promise<void> {
        this.setStarting(modelName, true);
        try {
            await this.api.startModel(modelName);
        } finally {
            this.setStarting(modelName, false);
        }
    }
 
    async stopModel(modelName: string): Promise<void> {
        this.setStopping(modelName, true);
        try {
            await this.api.stopModel(modelName);
        } finally {
            this.setStopping(modelName, false);
        }
    }
 
    getTreeItem(element: OllamaModelItem | OllamaChatItem): vscode.TreeItem {
        return element;
    }
 
    async getChildren(
        element?: OllamaModelItem | OllamaInstanceItem | OllamaChatItem,
    ): Promise<(OllamaModelItem | OllamaInstanceItem | OllamaChatItem)[]> {
        if (element instanceof OllamaChatItem) {
            return [];
        }
 
        if (element instanceof OllamaInstanceItem) {
            // Return chats for this instance
            const chats = this.chatService.getChatsForModel(element.instance.id);
            return chats.map((c) => new OllamaChatItem(c));
        }
 
        if (element instanceof OllamaModelItem) {
            // Return instances for this model
            const instances = this.modelService.getInstancesForModel(element.model.name);
            return instances.map((inst) => {
                return this.createInstanceItem(inst);
            });
        }
 
        // Root elements: Models
        const [models, running] = await Promise.all([this.api.listModels(), this.api.listRunning()]);
 
        this.runningModels = new Set(running.map((r) => this.normalizeName(r.model)));
 
        // Cleanup settings for models that no longer exist
        this.modelService.cleanupOrphanedSettings(models.map((m) => m.name));
 
        // Filter out models that are actually managed instances of other models
        const rootModels = models.filter((m) => {
            const instance = this.modelService.getInstanceByOllamaName(m.name);
            if (!instance) {
                return true; // External model, show at root
            }
            // If it's ours, only show at root if it's NOT a managed custom instance
            return !instance.isManaged;
        });
 
        const rootItems: (OllamaModelItem | OllamaInstanceItem)[] = [];
 
        for (const model of rootModels) {
            const instances = this.modelService.getInstancesForModel(model.name);
            if (instances.length === 1) {
                // Flatten: Show the single instance directly at root
                rootItems.push(this.createInstanceItem(instances[0], true));
            } else {
                // Grouped: Show the model group
                rootItems.push(new OllamaModelItem(model));
            }
        }
 
        return rootItems;
    }
 
    private createInstanceItem(inst: ModelInstance, isRoot: boolean = false): OllamaInstanceItem {
        const actualName = inst.ollamaModelName || inst.modelName;
        const normName = this.normalizeName(actualName);
        const isStart = this.startingModels.has(normName);
        const isStop = this.stoppingModels.has(normName);
        const isRun = this.runningModels.has(normName) && !isStart && !isStop;
        return new OllamaInstanceItem(inst, isRun, isStart, isStop, isRoot);
    }
 
    getApi(): IOllamaClient {
        return this.api;
    }
}