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
35 changes: 33 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICodeCellModel } from '@jupyterlab/cells';
import { Cell, ICodeCellModel } from '@jupyterlab/cells';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
Expand Down Expand Up @@ -129,7 +129,37 @@ const plugin: JupyterFrontEndPlugin<void> = {
chatWidget.title.closable = true;
app.shell.add(chatWidget, 'right');

// Keep the enabled state in sync when the active cell changes.
chatWidget.node.addEventListener('click', (event: MouseEvent) => {
const target = event.target;
if (target instanceof HTMLAnchorElement) {
const href = target.getAttribute('href');
if (href && href.startsWith('#tutor-cell-')) {
event.preventDefault();
const cellId = href.slice('#tutor-cell-'.length);
if (!notebookTracker) {
return;
}
const notebookPanel = notebookTracker.find((panel: NotebookPanel) =>
panel.content.widgets.some(
(widget: Cell) => widget.model.id === cellId
)
);
if (notebookPanel) {
app.shell.activateById(notebookPanel.id);
const index = notebookPanel.content.widgets.findIndex(
(widget: Cell) => widget.model.id === cellId
);
if (index !== -1) {
notebookPanel.content.activeCellIndex = index;
notebookPanel.content.widgets[index].node.scrollIntoView({
block: 'nearest'
});
}
}
}
}
});

notebookTracker?.activeCellChanged.connect(() => {
commands.notifyCommandChanged(CommandIDs.explainCode);
});
Expand Down Expand Up @@ -315,6 +345,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
body: bodyContent,
formattedBody: formattedBody,
notebookPath,
cellId: cell.model.id,
attachments: attachment ? [attachment] : undefined
});
},
Expand Down
5 changes: 5 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AI_AVATAR } from './icons';

interface ITutorNewMessage extends INewMessage {
attachments?: IAttachment[];
cellId?: string;
notebookPath?: string;
formattedBody?: string;
}
Expand Down Expand Up @@ -96,6 +97,10 @@ export class TutorChatModel extends AbstractChatModel {
accumulated += chunk;
streamingMsg.update({ body: accumulated });
}
if (message.cellId) {
accumulated += `\n\n[Go to Cell](#tutor-cell-${message.cellId})`;
streamingMsg.update({ body: accumulated });
}
} catch (err) {
if (err instanceof Error && err.name === 'AbortError') return;
const errorText = err instanceof Error ? err.message : String(err);
Expand Down
19 changes: 19 additions & 0 deletions style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
border-top: none;
}

/* Style Go to Cell links as standard buttons */
#jupyter-ai-tutor-panel a[href^="#tutor-cell-"] {
display: inline-block;
background-color: var(--jp-brand-color1, #2196f3);
color: var(--jp-layout-color0, #ffffff) !important;
text-decoration: none;
border-radius: 3px;
padding: 4px 8px;
font-weight: 500;
font-size: var(--jp-ui-font-size0, 11px);
white-space: nowrap;
margin-top: 8px;
}

#jupyter-ai-tutor-panel a[href^="#tutor-cell-"]:hover {
background-color: var(--jp-brand-color0, #1976d2);
text-decoration: none;
}

/* Override toolbar visibility on cell overlap */
/* stylelint-disable-next-line selector-class-pattern */
.jp-toolbar-overlap .jp-cell-toolbar {
Expand Down
Loading