Skip to content

Commit

Permalink
Add interruptPrompt() method to Jupyter kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Jun 29, 2023
1 parent 7fdf62e commit 0c2145c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions extensions/jupyter-adapter/src/JupyterKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {
}
}

/**
* Interrupts the kernel and resets prompt state
*/
public async interruptPrompt(id: string): Promise<void> {
await this.interrupt();
this._inputRequests.delete(id);
}

/**
* Send a message to the kernel
*
Expand Down
10 changes: 10 additions & 0 deletions extensions/jupyter-adapter/src/LanguageRuntimeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ export class LanguageRuntimeAdapter
this._kernel.replyToPrompt(id, reply);
}

/**
* Interrupt an input prompt from the kernel.
*
* @param id The ID of the prompt to which user responded
*/
public async interruptPrompt(id: string): Promise<void> {
this._kernel.log(`Interrupting prompt ${id}`);
this._kernel.interruptPrompt(id);
}

/**
* Interrupts the kernel.
*/
Expand Down
4 changes: 4 additions & 0 deletions extensions/positron-r/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class RRuntime implements positron.LanguageRuntime, vscode.Disposable {
this._kernel.replyToPrompt(id, reply);
}

async interruptPrompt(id: string): Promise<void> {
await this._kernel.interruptPrompt(id);
}

start(): Thenable<positron.LanguageRuntimeInfo> {
return this._kernel.start();
}
Expand Down
3 changes: 3 additions & 0 deletions src/positron-dts/positron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ declare module 'positron' {
/** Reply to a prompt issued by the runtime */
replyToPrompt(id: string, reply: string): void;

/** Interrupt a prompt issued by the runtime */
interruptPrompt(id: string): Promise<void>;

/**
* Start the runtime; returns a Thenable that resolves with information about the runtime.
* If the runtime fails to start for any reason, the Thenable should reject with an error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ class ExtHostLanguageRuntimeAdapter implements ILanguageRuntime {
this._proxy.$replyToPrompt(this.handle, id, value);
}

async interruptPrompt(id: string): Promise<void> {
await this._proxy.$interruptPrompt(this.handle, id);
}

async interrupt(): Promise<void> {
this._stateEmitter.fire(RuntimeState.Interrupting);
return this._proxy.$interruptLanguageRuntime(this.handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ExtHostLanguageRuntimeShape {
$removeClient(handle: number, id: string): void;
$sendClientMessage(handle: number, client_id: string, message_id: string, message: any): void;
$replyToPrompt(handle: number, id: string, response: string): void;
$interruptPrompt(handle: number, id: string): Promise<void>;
$interruptLanguageRuntime(handle: number): Promise<void>;
$restartLanguageRuntime(handle: number): Promise<void>;
$shutdownLanguageRuntime(handle: number): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export class ExtHostLanguageRuntime implements extHostProtocol.ExtHostLanguageRu
this._runtimes[handle].replyToPrompt(id, response);
}

async $interruptPrompt(handle: number, id: string): Promise<void> {
if (handle >= this._runtimes.length) {
throw new Error(`Cannot interrupt prompt: language runtime handle '${handle}' not found or no longer valid.`);
}
this._runtimes[handle].interruptPrompt(id);
}

public registerClientHandler(handler: positron.RuntimeClientHandler): IDisposable {
this._clientHandlers.push(handler);
return new Disposable(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ export interface ILanguageRuntime {
*/
replyToPrompt(id: string, value: string): void;

/** Interrupt an input prompt issued by the runtime */
interruptPrompt(id: string): Promise<void>;

start(): Thenable<ILanguageRuntimeInfo>;

/** Interrupt the runtime */
Expand Down Expand Up @@ -490,4 +493,3 @@ export interface ILanguageRuntimeService {
startRuntime(runtimeId: string): void;
}
export { RuntimeClientType, IRuntimeClientInstance };

Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class PositronConsoleInstance extends Disposable implements IPositronConsoleInst
interruptPrompt(id: string) {
if (this._promptActive) {
this._promptActive = false;
this._runtime.interrupt();
this._runtime.interruptPrompt(id);
}
}

Expand Down

0 comments on commit 0c2145c

Please sign in to comment.