Skip to content
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,7 @@
"tar": "7.5.24",
"vscode-jsonrpc": "10.0.0",
"vscode-languageclient": "^11.0.0",
"vscode-languageserver-textdocument": "1.0.16",
"webr": "0.6.1",
"winreg": "^1.2.5",
"ws": "^8.23.0"
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/interactive/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class SessionAgent {
ended: this.manifest.ended,
rPath: this.manifest.rPath,
libraryPaths: this.manifest.libraryPaths,
webRPackageRevision: this.manifest.webRPackageRevision,
runtimeSessionId: this.manifest.runtimeSessionId,
capabilities: this.manifest.capabilities,
});
Expand Down
1 change: 1 addition & 0 deletions src/interactive/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface RuntimeMetadata {
runtimeSessionId?: string;
rPath?: string;
libraryPaths?: string[];
webRPackageRevision?: number;
}
export const INSPECTION_METHODS = [
'workspace',
Expand Down
47 changes: 44 additions & 3 deletions src/interactive/backends/webr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { webcrypto } from 'node:crypto';
import { DesktopWebRPackageStore } from '../webRPackageStore';
import { WebRConsole, WebRConsoleEvent } from '../../webr/console';
import { BackendEvent, DispatchNotSentError, InspectionRequest, SessionBackend } from '../backend';
import type { AgentSettings, Submission } from '../protocol';
Expand All @@ -11,6 +13,7 @@ export class WebRBackend implements SessionBackend {
debugger: false,
tables: true,
viewerInspection: true,
webRPackageContext: true,
html: true,
inspection: false,
inspectionDeadline: false,
Expand All @@ -24,11 +27,16 @@ export class WebRBackend implements SessionBackend {
private current?: string;
private inputId = 0;
private stopped = false;
private readonly packages: DesktopWebRPackageStore;
private webRPackageRevision?: number;
private packageWarning?: string;

constructor(
private readonly settings: AgentSettings,
private readonly baseUrl: string,
) {}
) {
this.packages = new DesktopWebRPackageStore(settings.storage, settings.generation);
}

onEvent(listener: (event: BackendEvent) => void): () => void {
this.listeners.add(listener);
Expand Down Expand Up @@ -58,6 +66,7 @@ export class WebRBackend implements SessionBackend {
}
this.exit();
},
webcrypto as Pick<Crypto, 'randomUUID' | 'subtle'>,
);
this.console = console;
try {
Expand All @@ -66,11 +75,12 @@ export class WebRBackend implements SessionBackend {
await runtime.FS.mount('NODEFS', { root: this.settings.directory }, '/project');
await runtime.evalRVoid('base::setwd("/project")');
const rVersion = await runtime.evalRString('R.version.string');
await this.publishPackages();
if (!this.stopped) {
this.emit({
type: 'ready',
capabilities: this.capabilities,
metadata: { rVersion },
metadata: { rVersion, webRPackageRevision: this.webRPackageRevision },
});
}
} catch (error) {
Expand All @@ -79,12 +89,40 @@ export class WebRBackend implements SessionBackend {
}
}

private async publishPackages(): Promise<void> {
try {
const revision = await this.packages.publish();
if (revision !== undefined && !this.stopped) {
this.webRPackageRevision = revision;
this.emit({ type: 'metadata', metadata: { webRPackageRevision: revision } });
}
this.packageWarning = undefined;
} catch (error) {
this.reportPackageWarning(error);
}
}

private reportPackageWarning(error: unknown): void {
const message = 'Could not publish webR package information: ' + String(error);
if (!this.stopped && message !== this.packageWarning) {
this.packageWarning = message;
this.emit({ type: 'warning', message });
}
}

private receive(event: WebRConsoleEvent): void {
if (this.stopped) {
return;
}
const executionId = this.current;
switch (event.type) {
case 'packageContext':
try {
this.packages.stage(event.context, event.archives);
} catch (error) {
this.reportPackageWarning(error);
}
break;
case 'started':
case 'stream':
this.emit({ ...event, executionId });
Expand Down Expand Up @@ -136,7 +174,10 @@ export class WebRBackend implements SessionBackend {
resolve();
})
.then(
(state) => {
async (state) => {
// The next editor request can see packages installed by this cell
// as soon as its finished event is visible, including after reconnect.
await this.publishPackages();
this.current = undefined;
if (!this.stopped) {
this.emit({ type: 'finished', executionId: submission.id, state });
Expand Down
3 changes: 3 additions & 0 deletions src/interactive/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export class AgentClient extends EventEmitter {
if (typeof data.runtimeSessionId === 'string') {
this.manifest.runtimeSessionId = data.runtimeSessionId;
}
if (typeof data.webRPackageRevision === 'number') {
this.manifest.webRPackageRevision = data.webRPackageRevision;
}
if (Array.isArray(data.libraryPaths)) {
this.manifest.libraryPaths = data.libraryPaths.map(String);
}
Expand Down
33 changes: 17 additions & 16 deletions src/interactive/manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { DesktopWebRPackageSource } from './webRPackageStore';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { randomUUID } from 'crypto';
import { AgentClient } from './client';
import {
identifier,
AgentConfig,
AgentSnapshot,
DEFAULT_MAX_ASSET_BYTES,
Expand Down Expand Up @@ -1796,6 +1798,17 @@ export class InteractiveManager
}
}

private updateLanguageEnvironment(target: session.Session, manifest: SessionManifest): void {
if (manifest.provider === 'webr' && manifest.capabilities.webRPackageContext) {
target.webRPackageSource ??= new DesktopWebRPackageSource(
path.join(this.root, identifier(manifest.id)),
manifest.generation,
);
target.webRPackageSource.update(manifest.webRPackageRevision);
}
session.updateSessionLanguageEnvironment(target, manifest.rPath, manifest.libraryPaths);
}

private registerTarget(client: AgentClient): session.Session {
const manifest = client.manifest;
const target = session.registerSessionTransport(
Expand All @@ -1813,7 +1826,7 @@ export class InteractiveManager
target.pid = String(manifest.rPid ?? '');
target.rVer = manifest.rVersion ?? '';
target.processExited = manifest.status === 'exited';
session.updateSessionLanguageEnvironment(target, manifest.rPath, manifest.libraryPaths);
this.updateLanguageEnvironment(target, manifest);
target.label = manifest.label;
target.execute = async (code) => {
const view = [...this.views.values()].find((item) => item.target === target);
Expand Down Expand Up @@ -1923,11 +1936,7 @@ export class InteractiveManager
view.target.pid = String(snapshot.manifest.rPid ?? '');
view.target.rVer = snapshot.manifest.rVersion ?? '';
view.target.processExited = snapshot.manifest.status === 'exited';
session.updateSessionLanguageEnvironment(
view.target,
snapshot.manifest.rPath,
snapshot.manifest.libraryPaths,
);
this.updateLanguageEnvironment(view.target, snapshot.manifest);
view.base = snapshot.manifest.assetBase
? (
await vscode.env.asExternalUri(vscode.Uri.parse(snapshot.manifest.assetBase))
Expand Down Expand Up @@ -2184,11 +2193,7 @@ export class InteractiveManager
if (Array.isArray(event.data.libraryPaths)) {
view.client.manifest.libraryPaths = event.data.libraryPaths.map(String);
}
session.updateSessionLanguageEnvironment(
view.target,
view.client.manifest.rPath,
view.client.manifest.libraryPaths,
);
this.updateLanguageEnvironment(view.target, view.client.manifest);
if (typeof event.data.runtimeSessionId === 'string') {
view.client.manifest.runtimeSessionId = event.data.runtimeSessionId;
}
Expand Down Expand Up @@ -3569,11 +3574,7 @@ export class InteractiveManager
target.pid = String(snapshot.manifest.rPid ?? '');
target.rVer = snapshot.manifest.rVersion ?? '';
target.processExited = snapshot.manifest.status === 'exited';
session.updateSessionLanguageEnvironment(
target,
snapshot.manifest.rPath,
snapshot.manifest.libraryPaths,
);
this.updateLanguageEnvironment(target, snapshot.manifest);
view.prompts.clear();
if (snapshot.workspace) {
target.workspaceData = snapshot.workspace as unknown as session.WorkspaceData;
Expand Down
2 changes: 2 additions & 0 deletions src/interactive/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface SessionManifest extends SessionIdentity {
runtimeSessionId?: string;
rPath?: string;
libraryPaths?: string[];
/** Revision of the package snapshot stored by this webR generation. */
webRPackageRevision?: number;
}

export interface SourceLocation {
Expand Down
Loading
Loading