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

21.51% Statements 34/158
100% Branches 0/0
0% Functions 0/5
21.51% Lines 34/158

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 1591x 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 * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
 
import { FramingSource, ModelFraming } from '../models/modelFraming';
import { FramingItem } from '../providers/framingProvider';
import { FramingService } from '../services/framingService';
 
/**
 * Manages the Model Framing Editor webview panel.
 */
export class FramingEditorPanel {
    public static panels: Map<string, FramingEditorPanel> = new Map();
    public static readonly viewType = 'ollamaFramingEditor';
 
    private readonly _panel: vscode.WebviewPanel;
    private readonly _extensionUri: vscode.Uri;
    private _disposables: vscode.Disposable[] = [];
    private readonly _framingService: FramingService;
    private _framing: ModelFraming;
 
    /**
     * Creates or shows a framing editor panel.
     */
    public static createOrShow(extensionUri: vscode.Uri, framing: ModelFraming, framingService: FramingService) {
        const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;

        // If we already have a panel for this framing, show it.
        const existingPanel = FramingEditorPanel.panels.get(framing.id);
        if (existingPanel) {
            existingPanel._panel.reveal(column);
            return;
        }

        // Otherwise, create a new panel.
        const panel = vscode.window.createWebviewPanel(
            FramingEditorPanel.viewType,
            `Framing: ${framing.name}`,
            column || 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 framingPanel = new FramingEditorPanel(panel, extensionUri, framing, framingService);
        FramingEditorPanel.panels.set(framing.id, framingPanel);
    }
 
    private constructor(
        panel: vscode.WebviewPanel,
        extensionUri: vscode.Uri,
        framing: ModelFraming,
        framingService: FramingService,
    ) {
        this._panel = panel;
        this._extensionUri = extensionUri;
        this._framing = framing;
        this._framingService = framingService;

        // Set the webview's initial html content
        this._update();

        // Handle messages from the webview
        this._panel.webview.onDidReceiveMessage(
            async (message) => {
                switch (message.command) {
                    case 'save': {
                        if (this._framing.source === FramingSource.BuiltIn) {
                            vscode.window.showErrorMessage(
                                'Built-in framings cannot be saved. Please duplicate it first.',
                            );
                            return;
                        }
                        const updated = await this._framingService.updateFraming(this._framing.id, message.framing);
                        if (updated) {
                            this._framing = updated;
                            vscode.window.showInformationMessage(`Model Framing '${this._framing.name}' saved.`);
                            // Refresh tree view to reflect changes
                            vscode.commands.executeCommand('ollamaView.refresh');
                        }
                        return;
                    }
                    case 'duplicate': {
                        const copy = await this._framingService.duplicateFraming(this._framing.id);
                        if (copy) {
                            vscode.window.showInformationMessage(`Model Framing duplicated as '${copy.name}'.`);
                            vscode.commands.executeCommand('ollamaView.refresh');
                            // Open the new copy
                            FramingEditorPanel.createOrShow(this._extensionUri, copy, this._framingService);
                        }
                        return;
                    }
                    case 'delete': {
                        if (this._framing.source === FramingSource.User) {
                            // Trigger the global delete command with a temporary FramingItem
                            vscode.commands.executeCommand('ollamaView.deleteFraming', new FramingItem(this._framing));
                        }
                        return;
                    }
                    case 'cancel': {
                        this.dispose();
                        return;
                    }
                }
            },
            null,
            this._disposables,
        );

        // Listen for when the panel is disposed
        this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
    }
 
    public dispose() {
        FramingEditorPanel.panels.delete(this._framing.id);
        this._panel.dispose();
        while (this._disposables.length) {
            const x = this._disposables.pop();
            if (x) {
                x.dispose();
            }
        }
    }
 
    private _update() {
        this._panel.webview.html = this._getHtmlForWebview();

        // Initial state sync
        setTimeout(() => {
            this._panel.webview.postMessage({
                command: 'init',
                framing: this._framing,
            });
        }, 100);
    }
 
    private _getHtmlForWebview() {
        const htmlPath = path.join(this._extensionUri.fsPath, 'media', 'framing.html');
        let html = fs.readFileSync(htmlPath, 'utf8');

        const scriptUri = this._panel.webview.asWebviewUri(
            vscode.Uri.file(path.join(this._extensionUri.fsPath, 'dist', 'webview', 'framing.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;
    }
}