All files / ollama-view/src extension.ts

76.71% Statements 145/189
85.71% Branches 18/21
76.92% Functions 10/13
76.71% Lines 145/189

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 1901x 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 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 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                     1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 1x  
import * as vscode from 'vscode';
 
import { ChatCommands } from './commands/chatCommands';
import { registerFramingCommands } from './commands/framingCommands';
import { ModelCommands } from './commands/modelCommands';
import { ProviderCommands } from './commands/providerCommands';
import { IOllamaClient } from './contracts/IOllamaClient';
import { Logger } from './logger';
import { OllamaApi } from './ollamaApi';
import { OllamaChatItem, OllamaInstanceItem, OllamaModelItem, OllamaProvider } from './ollamaProvider';
import { SetupPanel } from './panels/setupPanel';
import { FramingProvider } from './providers/framingProvider';
import { VscodeChatRepository } from './repositories/VscodeChatRepository';
import { VscodeModelSettingsRepository } from './repositories/VscodeModelSettingsRepository';
import { ChatOrchestrator } from './services/chatOrchestrator';
import { ChatService } from './services/chatService';
import { FramingService } from './services/framingService';
import { ModelService } from './services/modelService';
 
interface ModelAction extends vscode.QuickPickItem {
    id: string;
    command: string;
}
 
const getModelActions = (): ModelAction[] => [
    {
        label: '$(add) Create New Instance',
        id: 'createInstance',
        command: 'ollamaView.createInstance',
        description: 'Create a customized instance of this model',
    },
    {
        label: '$(settings-gear) Setup',
        id: 'setup',
        command: 'ollamaView.setup',
        description: 'Configure model settings',
    },
    { label: '$(trash) Delete', id: 'delete', command: 'ollamaView.delete', description: 'Permanently remove model' },
];
 
export function activate(context: vscode.ExtensionContext) {
    Logger.init();
 
    // Infrastructure
    const chatRepository = new VscodeChatRepository(context);
    const modelSettingsRepository = new VscodeModelSettingsRepository(context);
 
    // Dynamic Ollama Client to support immutable implementations on config changes
    class DynamicOllamaClient implements IOllamaClient {
        private currentClient: OllamaApi;
 
        constructor() {
            const config = vscode.workspace.getConfiguration('ollama-view');
            const apiUrl = config.get<string>('apiUrl') || 'http://127.0.0.1:11434';
            this.currentClient = new OllamaApi(apiUrl);
 
            context.subscriptions.push(
                vscode.workspace.onDidChangeConfiguration((e) => {
                    if (e.affectsConfiguration('ollama-view.apiUrl')) {
                        const newApiUrl = vscode.workspace.getConfiguration('ollama-view').get<string>('apiUrl') || 'http://127.0.0.1:11434';
                        this.currentClient = new OllamaApi(newApiUrl);
                    }
                })
            );
        }
 
        listModels() { return this.currentClient.listModels(); }
        listRunning() { return this.currentClient.listRunning(); }
        showModel(model: string) { return this.currentClient.showModel(model); }
        startModel(model: string) { return this.currentClient.startModel(model); }
        stopModel(model: string) { return this.currentClient.stopModel(model); }
        deleteModel(model: string) { return this.currentClient.deleteModel(model); }
        pullModel(model: string, onProgress: Parameters<IOllamaClient['pullModel']>[1]) { return this.currentClient.pullModel(model, onProgress); }
        createModel(options: Parameters<IOllamaClient['createModel']>[0]) { return this.currentClient.createModel(options); }
        chat(model: string, messages: Parameters<IOllamaClient['chat']>[1], onToken: Parameters<IOllamaClient['chat']>[2], options?: Parameters<IOllamaClient['chat']>[3]) {
            return this.currentClient.chat(model, messages, onToken, options);
        }
    }
 
    const dynamicOllamaApi = new DynamicOllamaClient();
 
    // Services
    const chatService = new ChatService(chatRepository);
    const modelService = new ModelService(modelSettingsRepository, dynamicOllamaApi as unknown as OllamaApi, chatService);
    const framingService = new FramingService(context);
    const chatOrchestrator = new ChatOrchestrator(chatService, modelService, framingService, dynamicOllamaApi as unknown as OllamaApi);
 
    // Providers
    const ollamaProvider = new OllamaProvider(chatService, modelService, dynamicOllamaApi);
    const framingProvider = new FramingProvider(framingService);
 
    // Command Handlers
    const modelCommands = new ModelCommands(modelService, chatService, ollamaProvider);
    const chatCommands = new ChatCommands(
        chatService,
        modelService,
        framingService,
        chatOrchestrator,
        ollamaProvider,
        context.extensionUri,
    );
    const providerCommands = new ProviderCommands(ollamaProvider, framingProvider);
 
    // Register TreeDataProvider
    vscode.window.registerTreeDataProvider('ollama-models-view', ollamaProvider);
    vscode.window.registerTreeDataProvider('ollama-framing-view', framingProvider);
 
    // Commands
    registerFramingCommands(context, framingService);
 
    context.subscriptions.push(
        vscode.commands.registerCommand('ollamaView.refresh', () => providerCommands.refresh()),
        vscode.commands.registerCommand('ollamaView.pull', () => providerCommands.pull()),
 
        vscode.commands.registerCommand('ollamaView.createChat', (node?: OllamaInstanceItem) =>
            chatCommands.createChat(node),
        ),
        vscode.commands.registerCommand('ollamaView.startChat', () => chatCommands.startChat()),
        vscode.commands.registerCommand('ollamaView.deleteChat', (node: OllamaChatItem) =>
            chatCommands.deleteChat(node),
        ),
        vscode.commands.registerCommand('ollamaView.openChat', (node: OllamaChatItem) => chatCommands.openChat(node)),
 
        vscode.commands.registerCommand('ollamaView.createInstance', (node: OllamaModelItem | OllamaInstanceItem) =>
            modelCommands.createInstance(node, context.extensionUri),
        ),
        vscode.commands.registerCommand('ollamaView.start', (node?: OllamaInstanceItem | OllamaModelItem) =>
            modelCommands.start(node),
        ),
        vscode.commands.registerCommand('ollamaView.stop', (node?: OllamaInstanceItem | OllamaModelItem) =>
            modelCommands.stop(node),
        ),
        vscode.commands.registerCommand('ollamaView.delete', (node?: OllamaInstanceItem | OllamaModelItem) =>
            modelCommands.delete(node),
        ),
 
        vscode.commands.registerCommand('ollamaView.setup', async (node?: OllamaInstanceItem | OllamaModelItem) => {
            if (!node) {
                return;
            }
            if (node instanceof OllamaInstanceItem) {
                const api = ollamaProvider.getApi();
                const models = await api.listModels();
                const model = models.find((m) => m.name === node.instance.modelName);
                if (model) {
                    SetupPanel.createOrShow(
                        context.extensionUri,
                        model,
                        modelService,
                        framingService,
                        ollamaProvider,
                        node.instance.id,
                        () => ollamaProvider.refresh(),
                    );
                }
            } else {
                SetupPanel.createOrShow(
                    context.extensionUri,
                    node.model,
                    modelService,
                    framingService,
                    ollamaProvider,
                    undefined,
                    () => ollamaProvider.refresh(),
                );
            }
        }),
 
        vscode.commands.registerCommand(
            'ollamaView.showMoreActions',
            async (node?: OllamaInstanceItem | OllamaModelItem) => {
                if (!node) {
                    return;
                }
                const name = node instanceof OllamaInstanceItem ? node.instance.name : node.model.name;
                const actions = getModelActions();
                const result = await vscode.window.showQuickPick(actions, {
                    placeHolder: `Actions for ${name}`,
                });

                if (result) {
                    vscode.commands.executeCommand(result.command, node);
                }
            },
        ),
    );
}
 
export function deactivate() { }