Skip to content

Commit df96d85

Browse files
committed
Integrate with the magic-wand extension
1 parent f2259dc commit df96d85

File tree

4 files changed

+1237
-40
lines changed

4 files changed

+1237
-40
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@lumino/signaling": "^2.1.2",
7171
"@mui/icons-material": "^5.11.0",
7272
"@mui/material": "^5.11.0",
73+
"jupyterlab_magic_wand": "^0.4.0",
7374
"react": "^18.2.0",
7475
"react-dom": "^18.2.0"
7576
},
@@ -110,6 +111,15 @@
110111
"jupyterlab": {
111112
"extension": true,
112113
"outputDir": "jupyterlite_ai/labextension",
113-
"schemaDir": "schema"
114+
"schemaDir": "schema",
115+
"disabledExtensions": [
116+
"jupyterlab_magic_wand:magic-provider"
117+
],
118+
"sharedPackages": {
119+
"jupyterlab_magic_wand": {
120+
"bundled": false,
121+
"singleton": true
122+
}
123+
}
114124
}
115125
}

src/index.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ import {
1111
JupyterFrontEnd,
1212
JupyterFrontEndPlugin
1313
} from '@jupyterlab/application';
14-
import { ReactWidget, IThemeManager } from '@jupyterlab/apputils';
14+
import { IThemeManager, ReactWidget } from '@jupyterlab/apputils';
1515
import { ICompletionProviderManager } from '@jupyterlab/completer';
1616
import { INotebookTracker } from '@jupyterlab/notebook';
1717
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
1818
import { ISettingRegistry } from '@jupyterlab/settingregistry';
1919

20+
import { PartialJSONValue } from '@lumino/coreutils';
2021
import { ChatHandler } from './chat-handler';
2122
import { getSettings } from './llm-models';
2223
import { AIProvider } from './provider';
2324
import { renderSlashCommandOption } from './slash-commands';
2425
import { IAIProvider } from './token';
2526

27+
import { HumanMessage, SystemMessage } from '@langchain/core/messages';
28+
import { IMagicProvider } from 'jupyterlab_magic_wand';
29+
2630
const autocompletionRegistryPlugin: JupyterFrontEndPlugin<IAutocompletionRegistry> =
2731
{
2832
id: '@jupyterlite/ai:autocompletion-registry',
@@ -189,4 +193,74 @@ const aiProviderPlugin: JupyterFrontEndPlugin<IAIProvider> = {
189193
}
190194
};
191195

192-
export default [chatPlugin, autocompletionRegistryPlugin, aiProviderPlugin];
196+
const magicProviderPlugin: JupyterFrontEndPlugin<IMagicProvider> = {
197+
id: '@jupyterlite/ai:magic-provider',
198+
autoStart: true,
199+
requires: [IAIProvider],
200+
provides: IMagicProvider,
201+
activate: (app: JupyterFrontEnd, aiProvider: IAIProvider): IMagicProvider => {
202+
console.log('@jupyterlite/ai magic provider plugin activated');
203+
const events = app.serviceManager.events;
204+
205+
return {
206+
magic: async (
207+
cellId: string,
208+
codeInput: string,
209+
content: PartialJSONValue | undefined
210+
) => {
211+
const trimmedPrompt = codeInput.trim();
212+
213+
// TODO: taken from jupyterlab-magic-wand
214+
const PROMPT =
215+
'The input below came from a code cell in Jupyter. If the input does not look like code, but instead a prompt, write code based on the prompt. Then, update the code to make it more efficient, add code comments, and respond with only the code and comments. ';
216+
217+
const messages = [
218+
new SystemMessage(PROMPT),
219+
new HumanMessage(trimmedPrompt)
220+
];
221+
222+
const response = await aiProvider.chatModel?.invoke(messages);
223+
if (!response) {
224+
return;
225+
}
226+
227+
const source = response.content;
228+
229+
events.emit({
230+
schema_id: 'https://events.jupyter.org/jupyter_ai/magic_button/v1',
231+
version: '1',
232+
data: {
233+
agent: 'Magic Button Agent',
234+
input: codeInput,
235+
// @ts-expect-error: TODO
236+
context: {
237+
cell_id: cellId,
238+
content
239+
},
240+
messages: [source],
241+
commands: [
242+
{
243+
name: 'update-cell-source',
244+
args: {
245+
cell_id: cellId,
246+
cell_type: 'code',
247+
source: source
248+
}
249+
}
250+
]
251+
}
252+
});
253+
// TODO:
254+
console.log('MAGIC PROVIDER');
255+
console.log(cellId, codeInput, content);
256+
}
257+
};
258+
}
259+
};
260+
261+
export default [
262+
chatPlugin,
263+
aiProviderPlugin,
264+
autocompletionRegistryPlugin,
265+
magicProviderPlugin
266+
];

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"outDir": "lib",
1717
"rootDir": "src",
1818
"strict": true,
19+
"skipLibCheck": true,
1920
"strictNullChecks": true,
2021
"target": "ES2018"
2122
},

0 commit comments

Comments
 (0)