Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
"test": "vscode-test"
},
"devDependencies": {
"@types/vscode": "^1.120.0",
"@types/mocha": "^10.0.10",
"@types/node": "22.x",
"typescript-eslint": "^8.56.1",
"eslint": "^9.39.3",
"@types/vscode": "^1.120.0",
"@vscode/test-cli": "^0.0.12",
"@vscode/test-electron": "^2.5.2",
"esbuild": "^0.27.3",
"eslint": "^9.39.3",
"npm-run-all": "^4.1.5",
"typescript": "^5.9.3",
"@vscode/test-cli": "^0.0.12",
"@vscode/test-electron": "^2.5.2"
"typescript-eslint": "^8.56.1"
},
"dependencies": {
"ollama": "^0.6.3"
}
}
190 changes: 171 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,178 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { Ollama } from 'ollama';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
import {
createPullRecommendedModelCommand,
createShowOllamaModelsCommand,
createStartOllamaCommand,
OllamaCliService,
OllamaRuntimeService,
updateOllamaStatusBarItem,
} from './features/ollama';
import { createSelectOllamaModelCommand } from './features/settings';
import {
FRILINGO_OUTPUT_CHANNEL_NAME,
createTranslateHoverProvider,
createTranslateSelectionCommand,
createTranslateTextCommand,
OllamaService,
} from './features/translation';
import { createOllamaConfig } from './shared/config/ollama';

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "frilingo" is now active!');
export function activate(context: vscode.ExtensionContext) {
const getOllamaConfig = () =>
createOllamaConfig(vscode.workspace.getConfiguration('frilingo'));
const ollamaService = new OllamaService(undefined, getOllamaConfig);
const ollamaCliService = new OllamaCliService();
const ollamaRuntimeService = new OllamaRuntimeService(
ollamaCliService,
new Ollama({ host: getOllamaConfig().host }),
getOllamaConfig,
);
// Creates a shared output channel so every translation request writes to the same log.
const outputChannel = vscode.window.createOutputChannel(FRILINGO_OUTPUT_CHANNEL_NAME);
// Keeps the current Ollama readiness visible even when the output panel is closed.
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
updateOllamaStatusBarItem(statusBarItem, ollamaRuntimeService.getState());
const runtimeSubscription = ollamaRuntimeService.onDidChangeState((state) => {
updateOllamaStatusBarItem(statusBarItem, state);
});
const showOllamaModelsDisposable = vscode.commands.registerCommand(
'frilingo.showOllamaModels',
createShowOllamaModelsCommand(ollamaCliService, ollamaRuntimeService, getOllamaConfig, {
showInformationMessage: (message: string, ...items: string[]) =>
vscode.window.showInformationMessage(message, ...items),
showWarningMessage: (message: string, ...items: string[]) =>
vscode.window.showWarningMessage(message, ...items),
executeCommand: (command: string) => vscode.commands.executeCommand(command),
openExternal: (uri: vscode.Uri) => vscode.env.openExternal(uri),
outputChannel,
refreshRuntimeState: () => ollamaRuntimeService.refreshState(),
}),
);
const startOllamaDisposable = vscode.commands.registerCommand(
'frilingo.startOllama',
createStartOllamaCommand(ollamaCliService, ollamaRuntimeService, {
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showErrorMessage: (message: string) => vscode.window.showErrorMessage(message),
withProgress: <T>(task: () => Promise<T>) =>
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Frilingo: Starting Ollama...',
cancellable: false,
},
task,
),
outputChannel,
refreshRuntimeState: () => ollamaRuntimeService.refreshState(),
}),
);
const pullRecommendedModelDisposable = vscode.commands.registerCommand(
'frilingo.pullRecommendedModel',
createPullRecommendedModelCommand(ollamaCliService, ollamaRuntimeService, getOllamaConfig, {
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showErrorMessage: (message: string) => vscode.window.showErrorMessage(message),
withProgress: <T>(
title: string,
task: (progress: vscode.Progress<{ message?: string }>) => Promise<T>,
) =>
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title,
cancellable: false,
},
(progress) => task(progress),
),
outputChannel,
refreshRuntimeState: () => ollamaRuntimeService.refreshState(),
}),
);
const selectOllamaModelDisposable = vscode.commands.registerCommand(
'frilingo.selectOllamaModel',
createSelectOllamaModelCommand(ollamaCliService, ollamaRuntimeService, {
showQuickPick: (items, options) => vscode.window.showQuickPick(items, options),
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showWarningMessage: (message: string) => vscode.window.showWarningMessage(message),
executeCommand: (command: string) => vscode.commands.executeCommand(command),
updateConfiguration: (model: string) =>
vscode.workspace
.getConfiguration('frilingo')
.update('ollamaModel', model, vscode.ConfigurationTarget.Global),
refreshRuntimeState: () => ollamaRuntimeService.refreshState(),
}),
);
const translateSelectionDisposable = vscode.commands.registerCommand(
'frilingo.translateSelection',
createTranslateSelectionCommand(ollamaService, {
getActiveTextEditor: () => vscode.window.activeTextEditor,
getActiveModelName: () => getOllamaConfig().model,
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showErrorMessage: (message: string) => vscode.window.showErrorMessage(message),
withProgress: <T>(
title: string,
task: (cancellationToken: vscode.CancellationToken) => Promise<T>,
) =>
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title,
cancellable: true,
},
(_progress, cancellationToken) => task(cancellationToken),
),
outputChannel,
}),
);
const translateTextDisposable = vscode.commands.registerCommand(
'frilingo.translateText',
createTranslateTextCommand(ollamaService, {
getActiveModelName: () => getOllamaConfig().model,
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showErrorMessage: (message: string) => vscode.window.showErrorMessage(message),
withProgress: <T>(
title: string,
task: (cancellationToken: vscode.CancellationToken) => Promise<T>,
) =>
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title,
cancellable: true,
},
(_progress, cancellationToken) => task(cancellationToken),
),
outputChannel,
}),
);
const hoverProviderDisposable = vscode.languages.registerHoverProvider(
{ scheme: 'file' },
createTranslateHoverProvider(ollamaRuntimeService),
);

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
const disposable = vscode.commands.registerCommand('frilingo.helloWorld', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World from frilingo!');
});
// Runs one startup inventory pass so the output channel shows local model state immediately.
void vscode.commands.executeCommand('frilingo.showOllamaModels');

context.subscriptions.push(disposable);
context.subscriptions.push(
statusBarItem,
runtimeSubscription,
showOllamaModelsDisposable,
startOllamaDisposable,
pullRecommendedModelDisposable,
selectOllamaModelDisposable,
translateSelectionDisposable,
translateTextDisposable,
hoverProviderDisposable,
outputChannel,
);
}

// This method is called when your extension is deactivated
export function deactivate() {}
export function deactivate() {
// No cleanup is required for v0.1.
}
93 changes: 93 additions & 0 deletions src/features/ollama/commands/pullRecommendedModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as vscode from 'vscode';

import { DEFAULT_OLLAMA_MODEL, FrilingoOllamaConfig, OLLAMA_CONFIG } from '../../../shared/config/ollama';
import {
appendOllamaCliError,
appendOllamaPullResult,
extractLatestPullStatus,
} from '../services/ollamaOutput';
import { OllamaCliService } from '../services/ollamaCliService';
import { OllamaRuntimeService } from '../services/ollamaRuntimeService';

type PullRecommendedModelDependencies = {
showInformationMessage: (message: string) => Thenable<string | undefined>;
showErrorMessage: (message: string) => Thenable<string | undefined>;
withProgress: <T>(
title: string,
task: (progress: vscode.Progress<{ message?: string }>) => Promise<T>,
) => Thenable<T>;
outputChannel: vscode.OutputChannel;
refreshRuntimeState?: () => Promise<unknown>;
};

const defaultDependencies: PullRecommendedModelDependencies = {
showInformationMessage: (message: string) =>
vscode.window.showInformationMessage(message),
showErrorMessage: (message: string) => vscode.window.showErrorMessage(message),
withProgress: <T>(
title: string,
task: (progress: vscode.Progress<{ message?: string }>) => Promise<T>,
) =>
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title,
cancellable: false,
},
(progress) => task(progress),
),
outputChannel: vscode.window.createOutputChannel('Frilingo'),
};

// Pulls the configured recommendation so the user can bootstrap Frilingo quickly.
export function createPullRecommendedModelCommand(
ollamaCliService: OllamaCliService,
runtimeService?: Pick<OllamaRuntimeService, 'refreshState'>,
getConfig: () => FrilingoOllamaConfig = () => OLLAMA_CONFIG,
dependencies: PullRecommendedModelDependencies = defaultDependencies,
): () => Promise<void> {
return async () => {
const config = getConfig();
const model = config.model || DEFAULT_OLLAMA_MODEL;

try {
dependencies.outputChannel.appendLine(
`[Frilingo] Pulling model: ${model}`,
);
const output = await dependencies.withProgress(
`Frilingo: Pulling ${model}...`,
(progress) =>
ollamaCliService.pullModel(model, {
// Updates the VSCode notification with the latest sanitized CLI status instead of dumping raw ANSI output.
onOutput: (chunk: string) => {
const latestStatus = extractLatestPullStatus(chunk);

if (latestStatus) {
progress.report({ message: latestStatus });
}
},
}),
);

appendOllamaPullResult(
dependencies.outputChannel,
model,
output,
);
await (dependencies.refreshRuntimeState?.() ?? runtimeService?.refreshState());
dependencies.outputChannel.show(true);

await dependencies.showInformationMessage(
`Frilingo: Pulled recommended model ${model}.`,
);
} catch (error) {
const errorMessage = String(error);

appendOllamaCliError(dependencies.outputChannel, 'pull', errorMessage);
dependencies.outputChannel.show(true);
await dependencies.showErrorMessage(
`Frilingo: Failed to pull ${model}.`,
);
}
};
}
Loading
Loading