All files / ollama-view/src/panels setupPanel.ts

54.34% Statements 169/311
52.63% Branches 10/19
70% Functions 7/10
54.34% Lines 169/311

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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 3121x 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                           2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x                                                                                 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x                                               2x 2x 2x 4x     2x 2x 2x 2x 2x 1x 1x                                                     1x 1x                   1x 1x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x  
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
 
import { SetupExtensionToWebviewCommand, SetupWebviewToExtensionCommand } from '../contracts/IWebviewMessages';
import { Logger } from '../logger';
import { ModelInstance } from '../models/modelInstance';
import { OllamaModel, OllamaShowResponse } from '../ollamaApi';
import { OllamaProvider } from '../ollamaProvider';
import { FramingService } from '../services/framingService';
import { ModelService } from '../services/modelService';
import { assertNever } from '../utils';
 
export class SetupPanel {
    public static panels: Map<string, SetupPanel> = new Map();
    public static readonly viewType = 'ollamaModelSetup';
 
    private readonly _panel: vscode.WebviewPanel;
    private readonly _extensionUri: vscode.Uri;
    private _disposables: vscode.Disposable[] = [];
    private readonly _model: OllamaModel;
    private readonly _instanceId: string;
    private readonly _modelService: ModelService;
    private readonly _framingService: FramingService;
    private readonly _ollamaProvider: OllamaProvider;
    private readonly _onStateChange?: () => void;
    private _originalValues: OllamaShowResponse | null = null;
 
    public static createOrShow(
        extensionUri: vscode.Uri,
        model: OllamaModel,
        modelService: ModelService,
        framingService: FramingService,
        ollamaProvider: OllamaProvider,
        instanceId?: string,
        onStateChange?: () => void,
    ) {
        const id = instanceId || model.name;
        const existingPanel = SetupPanel.panels.get(id);
        if (existingPanel) {
            existingPanel._panel.reveal();
            return;
        }
 
        const instance = modelService.getSettings(id);
        const title = `${instance.name} - ${model.name}`;
 
        const panel = vscode.window.createWebviewPanel(SetupPanel.viewType, `Setup: ${title}`, vscode.ViewColumn.One, {
            enableScripts: true,
            retainContextWhenHidden: true,
            localResourceRoots: [
                vscode.Uri.file(path.join(extensionUri.fsPath, 'media')),
                vscode.Uri.file(path.join(extensionUri.fsPath, 'dist')),
            ],
        });
 
        const setupPanel = new SetupPanel(
            panel,
            extensionUri,
            model,
            modelService,
            framingService,
            ollamaProvider,
            id,
            onStateChange,
        );
        SetupPanel.panels.set(id, setupPanel);
    }
 
    private constructor(
        panel: vscode.WebviewPanel,
        extensionUri: vscode.Uri,
        model: OllamaModel,
        modelService: ModelService,
        framingService: FramingService,
        ollamaProvider: OllamaProvider,
        instanceId: string,
        onStateChange?: () => void,
    ) {
        this._panel = panel;
        this._extensionUri = extensionUri;
        this._model = model;
        this._instanceId = instanceId;
        this._modelService = modelService;
        this._framingService = framingService;
        this._ollamaProvider = ollamaProvider;
        this._onStateChange = onStateChange;
 
        this._update();
 
        this._panel.webview.onDidReceiveMessage(
            async (message: SetupWebviewToExtensionCommand) => {
                switch (message.command) {
                    case 'save': {
                        const settings = this._modelService.getSettings(this._instanceId);
                        const ollamaName = settings.ollamaModelName || settings.modelName;

                        // If model is running, we should stop it so it restarts with new parameters
                        if (this._ollamaProvider.isModelRunning(ollamaName)) {
                            try {
                                await this._ollamaProvider.stopModel(ollamaName);
                            } catch (e: unknown) {
                                Logger.error(`Failed to stop model ${ollamaName} during settings update`, e);
                            }
                        }

                        await this._modelService.setSettings(
                            this._instanceId,
                            message.instance as Partial<ModelInstance>,
                        );
                        vscode.window.showInformationMessage(`Settings saved for ${message.instance.name}`);
                        if (this._onStateChange) {
                            this._onStateChange();
                        }
                        this.dispose();
                        return;
                    }
                    case 'applyFraming':
                        await this._handleApplyFraming();
                        return;
                    case 'resetGroup':
                        await this._handleResetGroup(message.group);
                        return;
                    case 'cancel':
                        this.dispose();
                        return;
                    default:
                        assertNever(message);
                }
            },
            null,
            this._disposables,
        );
 
        this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
    }
 
    private async _postMessage(message: SetupExtensionToWebviewCommand) {
        await this._panel.webview.postMessage(message);
    }
 
    private async _handleResetGroup(group: 'hardware' | 'inference') {
        if (!this._originalValues) {
            this._originalValues = await this._ollamaProvider.getApi().showModel(this._model.name);
        }

        if (!this._originalValues) {
            return;
        }

        const params = this._extractParams(this._originalValues);
        const instance = this._modelService.getSettings(this._instanceId);
        if (group === 'hardware') {
            instance.config.num_gpu = params.num_gpu as number;
            instance.config.num_thread = params.num_thread as number;
            instance.config.use_mmap = params.use_mmap as boolean;
            instance.config.use_mlock = params.use_mlock as boolean;
        } else {
            instance.config.num_ctx = params.num_ctx as number;
            instance.config.num_predict = params.num_predict as number;
            instance.config.temperature = params.temperature as number;
            instance.config.top_p = params.top_p as number;
            instance.config.top_k = params.top_k as number;
            instance.config.repeat_penalty = params.repeat_penalty as number;
            instance.config.seed = params.seed as number;
            instance.config.stop = params.stop
                ? Array.isArray(params.stop)
                    ? (params.stop as string[])
                    : [params.stop as string]
                : undefined;
        }

        this._postMessage({
            command: 'init',
            model: this._model,
            settings: instance,
            originalValues: this._originalValues,
            originalParams: params,
            defaultMessage: ModelService.DEFAULT_SYSTEM_MESSAGE,
            isRunning: this._ollamaProvider.isModelRunning(instance.ollamaModelName || instance.modelName),
        });
    }
 
    private _extractParams(showData: OllamaShowResponse): Record<string, string | number | boolean | string[]> {
        const params: Record<string, string | number | boolean | string[]> = {};
        const modelfile = showData.modelfile || '';
        const parameters = showData.parameters || '';
 
        // Helper to parse a single line
        const parseLine = (line: string) => {
            const match = line.match(/^\s*(?:PARAMETER\s+)?(\w+)\s+(.+)$/i);
            if (match) {
                const key = match[1].toLowerCase();
                const rawVal = match[2].trim();
                let val: string | number | boolean = rawVal;

                if (rawVal === 'true') {
                    val = true;
                } else if (rawVal === 'false') {
                    val = false;
                } else if (!isNaN(Number(rawVal))) {
                    val = Number(rawVal);
                } else {
                    val = rawVal.replace(/^"|"$/g, '');
                }

                if (key === 'stop') {
                    if (!params.stop) {
                        params.stop = [];
                    }
                    (params.stop as string[]).push(val as string);
                } else {
                    params[key] = val;
                }
            }
        };
 
        modelfile.split('\n').forEach((l: string) => {
            if (l.toUpperCase().startsWith('PARAMETER ')) {
                parseLine(l);
            }
        });
        parameters.split('\n').forEach((l: string) => parseLine(l));
 
        return params;
    }
 
    private async _handleApplyFraming() {
        const framings = this._framingService.getAllFramings();
        const items = framings.map((f) => ({
            label: f.name,
            description: f.description,
            framing: f,
        }));

        const selected = await vscode.window.showQuickPick(items, {
            placeHolder: 'Select a model framing to apply',
        });

        if (selected) {
            const f = selected.framing;
            this._postMessage({
                command: 'updateFields',
                settings: {
                    systemMessage: f.systemMessage,
                    userMessagePrefix: f.userMessagePrefix,
                    userMessageSuffix: f.userMessageSuffix,
                    systemTurnPrefix: f.systemTurnPrefix,
                    systemTurnSuffix: f.systemTurnSuffix,
                },
            });
            vscode.window.showInformationMessage(`Applied framing: ${f.name}`);
        }
    }
 
    public dispose() {
        SetupPanel.panels.delete(this._instanceId);
        this._panel.dispose();
        while (this._disposables.length) {
            const x = this._disposables.pop();
            if (x) {
                x.dispose();
            }
        }
    }
 
    private async _update() {
        this._panel.webview.html = this._getHtmlForWebview();
 
        // Lazy load original values if needed
        if (!this._originalValues) {
            try {
                this._originalValues = await this._ollamaProvider.getApi().showModel(this._model.name);
            } catch (e: unknown) {
                Logger.error('Failed to fetch original model values', e);
            }
        }
 
        setTimeout(() => {
            const instance = this._modelService.getSettings(this._instanceId);
            const originalParams = this._originalValues ? this._extractParams(this._originalValues) : {};
 
            this._postMessage({
                command: 'init',
                model: this._model,
                settings: instance,
                originalValues: this._originalValues || undefined,
                originalParams: originalParams,
                defaultMessage: ModelService.DEFAULT_SYSTEM_MESSAGE,
                isRunning: this._ollamaProvider.isModelRunning(instance.ollamaModelName || instance.modelName),
            });
        }, 100);
    }
 
    private _getHtmlForWebview() {
        const htmlPath = path.join(this._extensionUri.fsPath, 'media', 'setup.html');
        let html = fs.readFileSync(htmlPath, 'utf8');
 
        const scriptUri = this._panel.webview.asWebviewUri(
            vscode.Uri.file(path.join(this._extensionUri.fsPath, 'dist', 'webview', 'setup.js')),
        );
        const styleUri = this._panel.webview.asWebviewUri(
            vscode.Uri.file(path.join(this._extensionUri.fsPath, 'media', 'common-webview.css')),
        );
 
        html = html.replace('{{scriptUri}}', scriptUri.toString());
        html = html.replace('{{styleUri}}', styleUri.toString());
 
        return html;
    }
}