Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/vscode 0325 #168

Merged
merged 9 commits into from
Jul 31, 2024
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
14 changes: 13 additions & 1 deletion src/inspect_ai/_view/www/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ body {
background-color: var(--bs-light);
}

.navbar-title-grid {
display: grid;
grid-template-columns: 1fr;
width: 100%;
}

@media (min-width: 575px) {
.navbar-title-grid {
grid-template-columns: 1fr auto;
}
}

[data-bs-theme="dark"] .navbar {
background-color: unset;
}
Expand Down Expand Up @@ -634,7 +646,7 @@ table.table.table-sm td {
margin-left: 0;
}
to {
margin-left: 100%;
margin-left: 95%;
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/inspect_ai/_view/www/src/navbar/Navbar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,7 @@ export const Navbar = ({
borderBottom: "solid var(--bs-border-color) 1px",
}}
>
<div
style=${{
display: "grid",
gridTemplateColumns: "1fr auto",
width: "100%",
}}
>
${navbarContents}
</div>
<div class="navbar-title-grid">${navbarContents}</div>
</nav>
`;
};
Expand Down
1 change: 1 addition & 0 deletions tools/vscode/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{
"version": "0.2.0",
"configurations": [

{
"name": "Run Extension",
"type": "extensionHost",
Expand Down
5 changes: 5 additions & 0 deletions tools/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.3.25

- Poll more frequently to show new log files
- Add support for showing the log viewer in the Positron Viewer

## 0.3.24

- Properly deal with URI encoded characters in log directories when opening a log file
Expand Down
2 changes: 1 addition & 1 deletion tools/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": {
"name": "UK AI Safety Institute"
},
"version": "0.3.24",
"version": "0.3.25",
"license": "MIT",
"homepage": "https://inspect.ai-safety-institute.org.uk/",
"repository": {
Expand Down
77 changes: 77 additions & 0 deletions tools/vscode/src/@types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@



declare module 'positron' {

import * as vscode from 'vscode';

export interface PositronApi {
version: string;
runtime: PositronRuntime;
languages: PositronLanguages;
window: PositronWindow;
}

export interface PositronRuntime {
executeCode(
languageId: string,
code: string,
focus: boolean
): Thenable<boolean>;
}

export interface PositronLanguages {
registerStatementRangeProvider(
selector: vscode.DocumentSelector,
provider: StatementRangeProvider
): vscode.Disposable;
}

export interface StatementRangeProvider {
provideStatementRange(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.ProviderResult<StatementRange>;
}

export interface StatementRange {
readonly range: vscode.Range;
readonly code?: string;
}

export interface PositronWindow {
createPreviewPanel(
viewType: string,
title: string,
preserveFocus?: boolean,
options?: PreviewOptions
): PreviewPanel;
}

export interface PreviewOptions {
readonly enableScripts?: boolean;
readonly enableForms?: boolean;
readonly localResourceRoots?: readonly vscode.Uri[];
readonly portMapping?: readonly vscode.WebviewPortMapping[];
}

export interface PreviewPanel {
readonly viewType: string;
title: string;
readonly webview: vscode.Webview;
readonly active: boolean;
readonly visible: boolean;
readonly viewColumn: vscode.ViewColumn;
readonly onDidChangeViewState: vscode.Event<PreviewPanelOnDidChangeViewStateEvent>;
readonly onDidDispose: vscode.Event<void>;
reveal(preserveFocus?: boolean): void;
dispose(): any;
}

export interface PreviewPanelOnDidChangeViewStateEvent {
readonly previewPanel: PreviewPanel;
}
}


49 changes: 20 additions & 29 deletions tools/vscode/src/components/webview.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
Uri,
WebviewPanel,
window,
ViewColumn,
EventEmitter,
ExtensionContext,
} from "vscode";
import { Uri, ViewColumn, EventEmitter, ExtensionContext, window } from "vscode";

import { Disposable } from "../core/dispose";
import { getNonce } from "../core/nonce";
import { ExtensionHost, HostWebviewPanel } from "../hooks";

export interface ShowOptions {
readonly preserveFocus?: boolean;
Expand All @@ -24,15 +18,16 @@ export class InspectWebviewManager<T extends InspectWebview<S>, S> {
private webviewType_: new (
context: ExtensionContext,
state: S,
webviewPanel: WebviewPanel
) => T
webviewPanel: HostWebviewPanel
) => T,
private host_: ExtensionHost
) {
this.extensionUri_ = context.extensionUri;

context.subscriptions.push(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens now to Inspect View in VS Code when you do a full reload? Does it restore itself (seems like it might be able to, even if it just goes back to the default view). I'm concerned that by removing this code we end up with a dead Inspect View panel, but LMK what you observe here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I observe that it doesn't orphan a window, but I agree with you that this seems sketchy. I am reverting that change.

window.registerWebviewPanelSerializer(this.viewType_, {
deserializeWebviewPanel: (panel, state: S) => {
this.restoreWebview(panel, state);
deserializeWebviewPanel: (panel) => {
//this.restoreWebview(panel as HostWebviewPanel, state);
setTimeout(() => {
panel.dispose();
}, 200);
Expand Down Expand Up @@ -95,42 +90,37 @@ export class InspectWebviewManager<T extends InspectWebview<S>, S> {
}

private preserveEditorFocus() {
// No need to take action here - we are already setting preserveFocus
// No need to take action here - we are already setting preserveFocus
// and ensuring that focus ends up in the correct places
}

private restoreWebview(panel: WebviewPanel, state: S): void {
private restoreWebview(panel: HostWebviewPanel, state: S): void {
const view = new this.webviewType_(this.context, state, panel);
this.registerWebviewListeners(view);
this.activeView_ = view;
}


private createWebview(
context: ExtensionContext,
state: S,
showOptions?: ShowOptions
): T {

const webview = window.createWebviewPanel(
const previewPanel = this.host_.createPreviewPanel(
this.viewType_,
this.title_,
{
viewColumn: showOptions?.viewColumn || ViewColumn.Beside,
preserveFocus: showOptions?.preserveFocus,
},
showOptions?.preserveFocus,
{
enableScripts: true,
enableForms: true,
retainContextWhenHidden: true,
localResourceRoots: [
...this.localResourceRoots,
Uri.joinPath(context.extensionUri, "assets", "www")
Uri.joinPath(context.extensionUri, "assets", "www"),
],
}
);

const inspectWebView = new this.webviewType_(context, state, webview);
const inspectWebView = new this.webviewType_(context, state, previewPanel);
return inspectWebView;
}

Expand Down Expand Up @@ -169,15 +159,15 @@ export class InspectWebviewManager<T extends InspectWebview<S>, S> {
}

export abstract class InspectWebview<T> extends Disposable {
protected readonly _webviewPanel: WebviewPanel;
protected readonly _webviewPanel: HostWebviewPanel;

private readonly _onDidDispose = this._register(new EventEmitter<void>());
public readonly onDispose = this._onDidDispose.event;

public constructor(
private readonly context: ExtensionContext,
state: T,
webviewPanel: WebviewPanel
webviewPanel: HostWebviewPanel
) {
super();

Expand Down Expand Up @@ -212,7 +202,8 @@ export abstract class InspectWebview<T> extends Disposable {
protected abstract getHtml(state: T): string;

protected getExtensionVersion(): string {
return (this.context.extension.packageJSON as Record<string, unknown>).version as string;
return (this.context.extension.packageJSON as Record<string, unknown>)
.version as string;
}

protected webviewHTML(
Expand Down Expand Up @@ -261,7 +252,8 @@ export abstract class InspectWebview<T> extends Disposable {
font-src ${this._webviewPanel.webview.cspSource};
style-src ${this._webviewPanel.webview.cspSource} ${allowUnsafe ? "'unsafe-inline'" : ""
};
script-src 'nonce-${nonce}' ${allowUnsafe ? "'unsafe-eval'" : ""};
script-src 'nonce-${nonce}' ${allowUnsafe ? "'unsafe-eval'" : ""
};
connect-src ${this._webviewPanel.webview.cspSource} ;
frame-src *;
">
Expand Down Expand Up @@ -294,5 +286,4 @@ export abstract class InspectWebview<T> extends Disposable {
protected escapeAttribute(value: string | Uri): string {
return value.toString().replace(/"/g, "&quot;");
}

}
}
34 changes: 24 additions & 10 deletions tools/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@ import { activateEvalManager } from "./providers/inspect/inspect-eval";
import { activateActivityBar } from "./providers/activity-bar/activity-bar-provider";
import { activateActiveTaskProvider } from "./providers/active-task/active-task-provider";
import { activateWorkspaceTaskProvider } from "./providers/workspace/workspace-task-provider";
import { WorkspaceStateManager, activateWorkspaceState } from "./providers/workspace/workspace-state-provider";
import {
WorkspaceStateManager,
activateWorkspaceState,
} from "./providers/workspace/workspace-state-provider";
import { initializeWorkspace } from "./providers/workspace/workspace-init";
import { activateWorkspaceEnv } from "./providers/workspace/workspace-env-provider";
import { initPythonInterpreter } from "./core/python";
import { initInspectProps } from "./inspect";
import { activateInspectManager } from "./providers/inspect/inspect-manager";
import { checkActiveWorkspaceFolder } from "./core/workspace";
import { inspectBinPath, inspectVersion } from "./inspect/props";
import { extensionHost } from "./hooks";

const kInspectMinimumVersion = "0.3.8";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export async function activate(context: ExtensionContext) {

// we don't activate anything if there is no workspace
if (!checkActiveWorkspaceFolder()) {
return;
}

// Get the host
const host = extensionHost();

const commandManager = new CommandManager();

// init python interpreter
Expand Down Expand Up @@ -62,14 +68,23 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(inspectManager);

// Eval Manager
const [inspectEvalCommands, inspectEvalMgr] = await activateEvalManager(stateManager, context);
const [inspectEvalCommands, inspectEvalMgr] = await activateEvalManager(
stateManager,
context
);

// Activate a watcher which inspects the active document and determines
// the active task (if any)
const [taskCommands, activeTaskManager] = activateActiveTaskProvider(inspectEvalMgr, context);
const [taskCommands, activeTaskManager] = activateActiveTaskProvider(
inspectEvalMgr,
context
);

// Active the workspace manager to watch for tasks
const workspaceTaskMgr = activateWorkspaceTaskProvider(inspectManager, context);
const workspaceTaskMgr = activateWorkspaceTaskProvider(
inspectManager,
context
);

// Read the extension configuration
const settingsMgr = new InspectSettingsManager(() => {
Expand All @@ -90,7 +105,8 @@ export async function activate(context: ExtensionContext) {
inspectManager,
settingsMgr,
workspaceEnvManager,
context
context,
host
);
const inspectLogviewManager = logviewWebviewManager;

Expand All @@ -106,7 +122,6 @@ export async function activate(context: ExtensionContext) {
context
);


// Register the log view link provider
window.registerTerminalLinkProvider(
logviewTerminalLinkProvider(logviewWebviewManager)
Expand All @@ -127,7 +142,7 @@ export async function activate(context: ExtensionContext) {
...taskBarCommands,
...stateCommands,
...envComands,
...taskCommands
...taskCommands,
].forEach((cmd) => commandManager.register(cmd));
context.subscriptions.push(commandManager);

Expand Down Expand Up @@ -164,7 +179,6 @@ const checkInspectVersion = async () => {
if (inspectBinPath()) {
const version = inspectVersion();
if (version && version.compare(kInspectMinimumVersion) === -1) {

const close: MessageItem = { title: "Close" };
await window.showInformationMessage<MessageItem>(
"The VS Code extension requires a newer version of Inspect. Please update " +
Expand All @@ -173,4 +187,4 @@ const checkInspectVersion = async () => {
);
}
}
};
};
Loading
Loading