Jelajahi Sumber

Make the chat per-document.

ian-r-rose 8 tahun lalu
induk
melakukan
8b39929149

+ 1 - 1
packages/chatbox-extension/package.json

@@ -17,7 +17,7 @@
     "@jupyterlab/codeeditor": "^0.3.1",
     "@jupyterlab/chatbox": "^0.1.0",
     "@jupyterlab/coreutils": "^0.3.1",
-    "@jupyterlab/docregistry": "^0.3.1",
+    "@jupyterlab/docmanager": "^0.3.1",
     "@jupyterlab/filebrowser": "^0.3.1",
     "@jupyterlab/launcher": "^0.3.1",
     "@jupyterlab/rendermime": "^0.3.1",

+ 14 - 2
packages/chatbox-extension/src/index.ts

@@ -9,6 +9,10 @@ import {
   ICommandPalette, ILayoutRestorer
 } from '@jupyterlab/apputils';
 
+import {
+  IDocumentManager
+} from '@jupyterlab/docmanager';
+
 import {
   IEditorServices
 } from '@jupyterlab/codeeditor';
@@ -45,7 +49,7 @@ namespace CommandIDs {
 export
 const chatboxPlugin: JupyterLabPlugin<void> = {
   id: 'jupyter.extensions.chatbox',
-  requires: [IRenderMime, ICommandPalette, IEditorServices, ILayoutRestorer],
+  requires: [IRenderMime, ICommandPalette, IEditorServices, IDocumentManager, ILayoutRestorer],
   autoStart: true,
   activate: activateChatbox
 }
@@ -60,7 +64,7 @@ export default chatboxPlugin;
 /**
  * Activate the chatbox extension.
  */
-function activateChatbox(app: JupyterLab, rendermime: IRenderMime, palette: ICommandPalette, editorServices: IEditorServices, restorer: ILayoutRestorer): void {
+function activateChatbox(app: JupyterLab, rendermime: IRenderMime, palette: ICommandPalette, editorServices: IEditorServices, docManager: IDocumentManager, restorer: ILayoutRestorer): void {
   let { commands, shell } = app;
   let category = 'Chatbox';
   let command: string;
@@ -99,4 +103,12 @@ function activateChatbox(app: JupyterLab, rendermime: IRenderMime, palette: ICom
     }
   });
   palette.addItem({ command, category });
+
+  let updateDocumentModel = function (): void {
+    let context = docManager.contextForWidget(shell.currentWidget);
+    if (context && context.model !== panel.chatbox.model) {
+      panel.chatbox.model = context.model;
+    }
+  }
+  shell.currentChanged.connect(updateDocumentModel);
 }

+ 1 - 0
packages/chatbox/package.json

@@ -17,6 +17,7 @@
     "@jupyterlab/cells": "^0.3.1",
     "@jupyterlab/codeeditor": "^0.3.1",
     "@jupyterlab/coreutils": "^0.3.1",
+    "@jupyterlab/docregistry": "^0.3.1",
     "@jupyterlab/outputarea": "^0.3.1",
     "@jupyterlab/rendermime": "^0.3.1",
     "@jupyterlab/services": "^0.42.1",

+ 40 - 2
packages/chatbox/src/widget.ts

@@ -6,7 +6,7 @@ import {
 } from '@phosphor/coreutils';
 
 import {
-  map, toArray
+  map, toArray, each
 } from '@phosphor/algorithm';
 
 import {
@@ -20,6 +20,10 @@ import {
 import {
 } from '@phosphor/widgets';
 
+import {
+  DocumentRegistry
+} from '@jupyterlab/docregistry';
+
 import {
   IEditorMimeTypeService, CodeEditor
 } from '@jupyterlab/codeeditor';
@@ -121,6 +125,38 @@ class Chatbox extends Widget {
     return inputLayout.widgets[0] as MarkdownCellWidget || null;
   }
 
+  /**
+   * The document model associated with the chatbox.
+   */
+  get model(): DocumentRegistry.IModel {
+    return this._model;
+  }
+  set model(model: DocumentRegistry.IModel) {
+    this._model = model;
+    let modelDB: any = (this._model as any).modelDB;
+    if (modelDB) {
+      modelDB.connected.then(() => {
+        // Update the chatlog vector.
+        if (modelDB.has('internal:chat')) {
+          this._log = modelDB.get('internal:chat') as IObservableVector<Chatbox.IChatEntry>;
+        } else {
+          this._log = modelDB.createVector('internal:chat');
+        }
+        // Remove any existing widgets.
+        while (this._content.widgets.length) {
+          (this._content.layout as PanelLayout).removeWidgetAt(0);
+        }
+        each(this._log, entry => {
+          let options = this._createMarkdownCellOptions(entry.text);
+          let cellWidget = this.contentFactory.createPrompt(options, this);
+          cellWidget.readOnly = true;
+          cellWidget.rendered = true;
+          this.addCell(cellWidget);
+        });
+      });
+    }
+  }
+
   /**
    * Add a new cell to the content panel.
    *
@@ -305,11 +341,12 @@ class Chatbox extends Widget {
   /**
    * Create the options used to initialize markdown cell widget.
    */
-  private _createMarkdownCellOptions(): MarkdownCellWidget.IOptions {
+  private _createMarkdownCellOptions(text: string = ''): MarkdownCellWidget.IOptions {
     let contentFactory = this.contentFactory.markdownCellContentFactory;
     let modelFactory = this.modelFactory;
     let model = modelFactory.createMarkdownCell({ });
     let rendermime = this.rendermime;
+    model.value.text = text || '';
     return { model, rendermime, contentFactory };
   }
 
@@ -318,6 +355,7 @@ class Chatbox extends Widget {
   private _log: IObservableVector<Chatbox.IChatEntry> = null;
   private _input: Panel = null;
   private _mimetype = 'text/x-ipython';
+  private _model: DocumentRegistry.IModel = null;
 }