@jataware/beaker-client 2.0.0-b.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/util.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import { JSONObject } from '@lumino/coreutils';
2
+ import { KernelFutureHandler } from '@jupyterlab/services/lib/kernel/future';
3
+ import { IBeakerShellMessage, BeakerCodeCell, IBeakerIOPubMessage, IBeakerCell } from './notebook';
4
+ import * as Kernel from '@jupyterlab/services/lib/kernel/kernel';
5
+ export declare enum KernelState {
6
+ unknown = "unknown",
7
+ starting = "starting",
8
+ idle = "idle",
9
+ busy = "busy",
10
+ terminating = "terminating",
11
+ restarting = "restarting",
12
+ autorestarting = "autorestarting",
13
+ dead = "dead"
14
+ }
15
+ export declare const createMessageId: (msgType: string) => string;
16
+ export interface IBeakerFuture<REQUEST extends IBeakerShellMessage = IBeakerShellMessage, REPLY extends IBeakerShellMessage = IBeakerShellMessage> extends Kernel.IShellFuture<REQUEST, REPLY> {
17
+ msgId?: string;
18
+ onResponse?: (msg: any) => void | PromiseLike<void>;
19
+ }
20
+ export interface IBeakerAvailableContexts {
21
+ [key: string]: {
22
+ languages: {
23
+ slug: string;
24
+ kernel: string;
25
+ };
26
+ defaultPayload: string;
27
+ };
28
+ }
29
+ export interface IActiveContextInfo {
30
+ slug: string;
31
+ class: string;
32
+ config: JSONObject;
33
+ language: {
34
+ slug: string;
35
+ subkernel: string;
36
+ };
37
+ kernelInfo?: any;
38
+ name?: string;
39
+ }
40
+ export declare class BeakerCellFuture extends KernelFutureHandler<IBeakerShellMessage, IBeakerShellMessage> {
41
+ /**
42
+ * Construct a new KernelFutureHandler.
43
+ */
44
+ constructor(msg: IBeakerShellMessage, expectReply: boolean, disposeOnDone: boolean, kernel?: Kernel.IKernelConnection | null, cell?: BeakerCodeCell);
45
+ private cell;
46
+ }
47
+ export declare namespace BeakerCellFutures {
48
+ const handleIOPub: (msg: IBeakerIOPubMessage, cell: IBeakerCell) => void;
49
+ const handleReply: (msg: IBeakerShellMessage, cell: IBeakerCell) => void;
50
+ }
package/dist/util.js ADDED
@@ -0,0 +1,112 @@
1
+ import { v4 } from 'uuid';
2
+ import { KernelFutureHandler } from '@jupyterlab/services/lib/kernel/future';
3
+
4
+ var KernelState = /* @__PURE__ */ ((KernelState2) => {
5
+ KernelState2["unknown"] = "unknown";
6
+ KernelState2["starting"] = "starting";
7
+ KernelState2["idle"] = "idle";
8
+ KernelState2["busy"] = "busy";
9
+ KernelState2["terminating"] = "terminating";
10
+ KernelState2["restarting"] = "restarting";
11
+ KernelState2["autorestarting"] = "autorestarting";
12
+ KernelState2["dead"] = "dead";
13
+ return KernelState2;
14
+ })(KernelState || {});
15
+ const createMessageId = (msgType) => {
16
+ const uuid = v4().replaceAll("-", "").slice(0, 16);
17
+ return `beaker-${uuid}-${msgType}`;
18
+ };
19
+ class BeakerCellFuture extends KernelFutureHandler {
20
+ /**
21
+ * Construct a new KernelFutureHandler.
22
+ */
23
+ constructor(msg, expectReply, disposeOnDone, kernel, cell) {
24
+ if (!kernel) {
25
+ throw Error("Unable to send message. Not connected to kernel.");
26
+ }
27
+ if (!cell) {
28
+ throw Error("Cannot execute in cell as cell is undefined or invalid.");
29
+ }
30
+ const msgId = msg.header.msg_id;
31
+ if (msg.channel === void 0) {
32
+ msg.channel = "shell";
33
+ }
34
+ const disposalCallback = () => {
35
+ kernel._futures.delete(msgId);
36
+ };
37
+ super(disposalCallback, msg, expectReply, disposeOnDone, kernel);
38
+ kernel._futures?.set(msgId, this);
39
+ this.cell = cell;
40
+ this.onIOPub = (msg2) => BeakerCellFutures.handleIOPub(msg2, this.cell);
41
+ this.onReply = (msg2) => BeakerCellFutures.handleReply(msg2, this.cell);
42
+ }
43
+ cell;
44
+ }
45
+ var BeakerCellFutures;
46
+ ((BeakerCellFutures2) => {
47
+ BeakerCellFutures2.handleIOPub = (msg, cell) => {
48
+ const msg_type = msg.header.msg_type;
49
+ const content = msg.content;
50
+ if (msg_type === "status") {
51
+ cell.status = content.execution_state;
52
+ } else if (msg_type === "execute_result") {
53
+ cell.busy = false;
54
+ if (content.execution_count) {
55
+ cell.execution_count = content.execution_count;
56
+ }
57
+ cell.outputs.push({
58
+ output_type: "execute_result",
59
+ ...content
60
+ });
61
+ } else if (msg_type === "stream") {
62
+ cell.outputs.push({
63
+ output_type: "stream",
64
+ ...content
65
+ });
66
+ } else if (msg_type === "display_data") {
67
+ cell.outputs.push({
68
+ output_type: "display_data",
69
+ ...content
70
+ });
71
+ }
72
+ };
73
+ BeakerCellFutures2.handleReply = (msg, cell) => {
74
+ cell.busy = false;
75
+ if (msg.content.status === "ok") {
76
+ cell.last_execution = { ...cell.last_execution, status: "ok" };
77
+ cell.execution_count = Number(msg.content.execution_count);
78
+ } else if (msg.content.status === "error") {
79
+ cell.execution_count = Number(msg.content.execution_count);
80
+ const error_details = {
81
+ ename: msg.content.ename,
82
+ evalue: msg.content.evalue,
83
+ traceback: msg.content.traceback
84
+ };
85
+ cell.last_execution = {
86
+ status: "error",
87
+ ...error_details
88
+ };
89
+ cell.outputs.push({
90
+ output_type: "error",
91
+ ...error_details
92
+ });
93
+ } else if (msg.content.status === "abort") {
94
+ cell.execution_count = msg.content.execution_count;
95
+ const error_details = {
96
+ ename: "Execution aborted",
97
+ evalue: "Execution aborted",
98
+ traceback: []
99
+ };
100
+ cell.last_execution = {
101
+ status: "error",
102
+ ...error_details
103
+ };
104
+ cell.outputs.push({
105
+ output_type: "error",
106
+ content: error_details
107
+ });
108
+ }
109
+ };
110
+ })(BeakerCellFutures || (BeakerCellFutures = {}));
111
+
112
+ export { BeakerCellFuture, BeakerCellFutures, KernelState, createMessageId };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@jataware/beaker-client",
3
+ "version": "2.0.0-b.1",
4
+ "type": "module",
5
+ "description": "Beaker Notebook client library",
6
+ "repository": {
7
+ "url": "git+https://github.com/jataware/beaker-kernel.git"
8
+ },
9
+ "scripts": {
10
+ "build": "run-s type-check build-lib",
11
+ "build-lib": "vite build",
12
+ "type-check": "tsc --noEmit"
13
+ },
14
+ "main": "./dist/index.js",
15
+ "module": "./dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "files": [
18
+ "dist/",
19
+ "src/"
20
+ ],
21
+ "dependencies": {
22
+ "@jupyterlab/apputils": "^4.5.1",
23
+ "@jupyterlab/nbformat": "^4.4.1",
24
+ "@jupyterlab/rendermime": "^4.4.1",
25
+ "@jupyterlab/services": "^7.4.1",
26
+ "json-schema": "^0.4.0",
27
+ "node-fetch": "^2.6.6",
28
+ "uuid": "^11.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/json-schema": "^7.0.15",
32
+ "@types/node-fetch": "^2.6.6",
33
+ "@types/uuid": "^9.0.7",
34
+ "npm-run-all2": "^7.0.2",
35
+ "typescript": "^5.8.0",
36
+ "vite": "^7.1.5",
37
+ "vite-plugin-dts": "^4.5.4"
38
+ }
39
+ }
@@ -0,0 +1,34 @@
1
+ import { JSONObject } from '@lumino/coreutils';
2
+ import { IBeakerShellMessage } from './notebook';
3
+ import { IBeakerFuture } from './util';
4
+ import * as messages from '@jupyterlab/services/lib/kernel/messages';
5
+ import * as nbformat from '@jupyterlab/nbformat';
6
+ import { PartialJSONObject } from '@lumino/coreutils';
7
+
8
+
9
+ declare module "@jupyterlab/services/lib/kernel" {
10
+ export interface KernelConnection {
11
+ sendBeakerMessage(
12
+ messageType: string,
13
+ content: JSONObject,
14
+ messageId: string,
15
+ expectReply: boolean,
16
+ disposeOnDone: boolean
17
+ ): IBeakerFuture;
18
+
19
+ }
20
+ }
21
+
22
+ declare module "@jupyterlab/services/lib/kernel/messages" {
23
+ export function createMessage<T extends IBeakerShellMessage>(options: messages.IOptions<T>): T;
24
+
25
+ }
26
+
27
+ declare module '@jupyterlab/nbformat' {
28
+ export interface IBaseOutput extends PartialJSONObject {
29
+ output_type: string;
30
+ metadata?: nbformat.OutputMetadata;
31
+ }
32
+ }
33
+
34
+ export default {};
package/src/history.ts ADDED
@@ -0,0 +1,61 @@
1
+ import * as nbformat from '@jupyterlab/nbformat';
2
+
3
+
4
+ export interface IBeakerHistoryEvent {
5
+ kernel_id: string;
6
+ kernel_slug: string;
7
+ context_slug: string;
8
+ creation_time: Date;
9
+ }
10
+
11
+ export interface IBeakerHistoryExecutionEvent extends IBeakerHistoryEvent {
12
+ cell_id: string;
13
+ execution_time: Date;
14
+ execution_duration: number;
15
+
16
+ silent: boolean;
17
+ execution_id: number;
18
+ source: nbformat.MultilineString;
19
+ result: any;
20
+ stdin: nbformat.MultilineString;
21
+ stderr: nbformat.MultilineString;
22
+ success: boolean;
23
+ }
24
+
25
+ export interface IBeakerHistoryQueryEvent extends IBeakerHistoryEvent {
26
+ cell_id: string;
27
+ execution_time: Date;
28
+ execution_duration: number;
29
+
30
+ source: nbformat.MultilineString;
31
+ thoughts: string[];
32
+ result: nbformat.MultilineString;
33
+ }
34
+
35
+ export type BeakerHistoryEvent = IBeakerHistoryExecutionEvent | IBeakerHistoryQueryEvent;
36
+
37
+ export interface IBeakerHistory {
38
+ sessionId: string;
39
+ events: IBeakerHistoryEvent[];
40
+ }
41
+
42
+
43
+ export class BeakerHistory implements IBeakerHistory {
44
+
45
+ constructor(sessionId: string) {
46
+ this.sessionId = sessionId;
47
+ this.events = [];
48
+ }
49
+
50
+ public addEvent(event: BeakerHistoryEvent) {
51
+ this.events.push(event);
52
+ };
53
+
54
+ public clear() {
55
+ // Clear with a splice to ensure reactivity
56
+ this.events.splice(0, this.events.splice.length);
57
+ }
58
+
59
+ sessionId: string;
60
+ events: IBeakerHistoryEvent[];
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ // @ts-ignore: Include extension code to extend typing from Jupyterlabs
2
+ import extension from './extension';
3
+ if (extension === undefined) {
4
+ throw new Error("Unable to extend Jupyterlab types");
5
+ }
6
+
7
+ export * from './session';
8
+ export * from './notebook';
9
+ export * from './render';
10
+ export * from './util';
11
+ export * from './history';
12
+