|
| 1 | +import { |
| 2 | + JupyterFrontEnd, |
| 3 | + JupyterFrontEndPlugin |
| 4 | +} from '@jupyterlab/application'; |
| 5 | + |
| 6 | +import { IJaiCore } from '../tokens'; |
| 7 | +import { AiService } from '../handler'; |
| 8 | +import { Menu } from '@lumino/widgets'; |
| 9 | +import { CommandRegistry } from '@lumino/commands'; |
| 10 | + |
| 11 | +export namespace CommandIDs { |
| 12 | + export const explain = 'jupyter-ai:explain'; |
| 13 | + export const fix = 'jupyter-ai:fix'; |
| 14 | + export const optimize = 'jupyter-ai:optimize'; |
| 15 | + export const refactor = 'jupyter-ai:refactor'; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Optional plugin that adds a "Generative AI" submenu to the context menu. |
| 20 | + * These implement UI shortcuts that explain, fix, refactor, or optimize code in |
| 21 | + * a notebook or file. |
| 22 | + * |
| 23 | + * **This plugin is experimental and may be removed in a future release.** |
| 24 | + */ |
| 25 | +export const menuPlugin: JupyterFrontEndPlugin<void> = { |
| 26 | + id: '@jupyter-ai/core:menu-plugin', |
| 27 | + autoStart: true, |
| 28 | + requires: [IJaiCore], |
| 29 | + activate: (app: JupyterFrontEnd, jaiCore: IJaiCore) => { |
| 30 | + const { activeCellManager, chatHandler, chatWidget, selectionWatcher } = |
| 31 | + jaiCore; |
| 32 | + |
| 33 | + function activateChatSidebar() { |
| 34 | + app.shell.activateById(chatWidget.id); |
| 35 | + } |
| 36 | + |
| 37 | + function getSelection(): AiService.Selection | null { |
| 38 | + const textSelection = selectionWatcher.selection; |
| 39 | + const activeCell = activeCellManager.getContent(false); |
| 40 | + const selection: AiService.Selection | null = textSelection |
| 41 | + ? { type: 'text', source: textSelection.text } |
| 42 | + : activeCell |
| 43 | + ? { type: 'cell', source: activeCell.source } |
| 44 | + : null; |
| 45 | + |
| 46 | + return selection; |
| 47 | + } |
| 48 | + |
| 49 | + function buildLabelFactory(baseLabel: string): () => string { |
| 50 | + return () => { |
| 51 | + const textSelection = selectionWatcher.selection; |
| 52 | + const activeCell = activeCellManager.getContent(false); |
| 53 | + |
| 54 | + return textSelection |
| 55 | + ? `${baseLabel} (${textSelection.numLines} lines selected)` |
| 56 | + : activeCell |
| 57 | + ? `${baseLabel} (1 active cell)` |
| 58 | + : baseLabel; |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + // register commands |
| 63 | + const menuCommands = new CommandRegistry(); |
| 64 | + menuCommands.addCommand(CommandIDs.explain, { |
| 65 | + execute: () => { |
| 66 | + const selection = getSelection(); |
| 67 | + if (!selection) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + activateChatSidebar(); |
| 72 | + chatHandler.sendMessage({ |
| 73 | + prompt: 'Explain the code below.', |
| 74 | + selection |
| 75 | + }); |
| 76 | + }, |
| 77 | + label: buildLabelFactory('Explain code'), |
| 78 | + isEnabled: () => !!getSelection() |
| 79 | + }); |
| 80 | + menuCommands.addCommand(CommandIDs.fix, { |
| 81 | + execute: () => { |
| 82 | + const activeCellWithError = activeCellManager.getContent(true); |
| 83 | + if (!activeCellWithError) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + chatHandler.sendMessage({ |
| 88 | + prompt: '/fix', |
| 89 | + selection: { |
| 90 | + type: 'cell-with-error', |
| 91 | + error: activeCellWithError.error, |
| 92 | + source: activeCellWithError.source |
| 93 | + } |
| 94 | + }); |
| 95 | + }, |
| 96 | + label: () => { |
| 97 | + const activeCellWithError = activeCellManager.getContent(true); |
| 98 | + return activeCellWithError |
| 99 | + ? 'Fix code cell (1 error cell)' |
| 100 | + : 'Fix code cell (no error cell)'; |
| 101 | + }, |
| 102 | + isEnabled: () => { |
| 103 | + const activeCellWithError = activeCellManager.getContent(true); |
| 104 | + return !!activeCellWithError; |
| 105 | + } |
| 106 | + }); |
| 107 | + menuCommands.addCommand(CommandIDs.optimize, { |
| 108 | + execute: () => { |
| 109 | + const selection = getSelection(); |
| 110 | + if (!selection) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + activateChatSidebar(); |
| 115 | + chatHandler.sendMessage({ |
| 116 | + prompt: 'Optimize the code below.', |
| 117 | + selection |
| 118 | + }); |
| 119 | + }, |
| 120 | + label: buildLabelFactory('Optimize code'), |
| 121 | + isEnabled: () => !!getSelection() |
| 122 | + }); |
| 123 | + menuCommands.addCommand(CommandIDs.refactor, { |
| 124 | + execute: () => { |
| 125 | + const selection = getSelection(); |
| 126 | + if (!selection) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + activateChatSidebar(); |
| 131 | + chatHandler.sendMessage({ |
| 132 | + prompt: 'Refactor the code below.', |
| 133 | + selection |
| 134 | + }); |
| 135 | + }, |
| 136 | + label: buildLabelFactory('Refactor code'), |
| 137 | + isEnabled: () => !!getSelection() |
| 138 | + }); |
| 139 | + |
| 140 | + // add commands as a context menu item containing a "Generative AI" submenu |
| 141 | + const submenu = new Menu({ |
| 142 | + commands: menuCommands |
| 143 | + }); |
| 144 | + submenu.id = 'jupyter-ai:submenu'; |
| 145 | + submenu.title.label = 'Generative AI'; |
| 146 | + submenu.addItem({ command: CommandIDs.explain }); |
| 147 | + submenu.addItem({ command: CommandIDs.fix }); |
| 148 | + submenu.addItem({ command: CommandIDs.optimize }); |
| 149 | + submenu.addItem({ command: CommandIDs.refactor }); |
| 150 | + |
| 151 | + app.contextMenu.addItem({ |
| 152 | + type: 'submenu', |
| 153 | + selector: '.jp-Editor', |
| 154 | + rank: 1, |
| 155 | + submenu |
| 156 | + }); |
| 157 | + } |
| 158 | +}; |
0 commit comments