Skip to content

Commit 2cd0934

Browse files
committed
Integrate with the magic-wand extension
1 parent b5358b9 commit 2cd0934

File tree

4 files changed

+1230
-35
lines changed

4 files changed

+1230
-35
lines changed

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"@langchain/mistralai": "^0.1.1",
6767
"@lumino/coreutils": "^2.1.2",
6868
"@lumino/polling": "^2.1.2",
69-
"@lumino/signaling": "^2.1.2"
69+
"@lumino/signaling": "^2.1.2",
70+
"jupyterlab_magic_wand": "*"
7071
},
7172
"devDependencies": {
7273
"@jupyterlab/builder": "^4.0.0",
@@ -104,7 +105,16 @@
104105
"jupyterlab": {
105106
"extension": true,
106107
"outputDir": "jupyterlite_ai/labextension",
107-
"schemaDir": "schema"
108+
"schemaDir": "schema",
109+
"disabledExtensions": [
110+
"jupyterlab_magic_wand:magic-provider"
111+
],
112+
"sharedPackages": {
113+
"jupyterlab_magic_wand": {
114+
"bundled": false,
115+
"singleton": true
116+
}
117+
}
108118
},
109119
"eslintIgnore": [
110120
"node_modules",
@@ -196,5 +206,8 @@
196206
"selector-no-vendor-prefix": null,
197207
"value-no-vendor-prefix": null
198208
}
209+
},
210+
"resolutions": {
211+
"jupyterlab_magic_wand": "file:/Users/jtp/git/jtpio/jupyterlab-magic-wand"
199212
}
200213
}

src/index.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { getSettings } from './llm-models';
1919
import { AIProvider } from './provider';
2020
import { IAIProvider } from './token';
2121

22+
import { IMagicProvider } from 'jupyterlab_magic_wand';
23+
import { PartialJSONValue } from '@lumino/coreutils';
24+
import { HumanMessage, SystemMessage } from '@langchain/core/messages';
25+
2226
const chatPlugin: JupyterFrontEndPlugin<void> = {
2327
id: '@jupyterlite/ai:chat',
2428
description: 'LLM chat extension',
@@ -151,4 +155,69 @@ const aiProviderPlugin: JupyterFrontEndPlugin<IAIProvider> = {
151155
}
152156
};
153157

154-
export default [chatPlugin, aiProviderPlugin];
158+
const magicProviderPlugin: JupyterFrontEndPlugin<IMagicProvider> = {
159+
id: '@jupyterlite/ai:magic-provider',
160+
autoStart: true,
161+
requires: [IAIProvider],
162+
provides: IMagicProvider,
163+
activate: (app: JupyterFrontEnd, aiProvider: IAIProvider): IMagicProvider => {
164+
console.log('@jupyterlite/ai magic provider plugin activated');
165+
const events = app.serviceManager.events;
166+
167+
return {
168+
magic: async (
169+
cellId: string,
170+
codeInput: string,
171+
content: PartialJSONValue | undefined
172+
) => {
173+
const trimmedPrompt = codeInput.trim();
174+
175+
// TODO: taken from jupyterlab-magic-wand
176+
const PROMPT =
177+
'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. ';
178+
179+
const messages = [
180+
new SystemMessage(PROMPT),
181+
new HumanMessage(trimmedPrompt)
182+
];
183+
184+
const response = await aiProvider.chatModel?.invoke(messages);
185+
if (!response) {
186+
return;
187+
}
188+
189+
const source = response.content;
190+
191+
events.emit({
192+
schema_id: 'https://events.jupyter.org/jupyter_ai/magic_button/v1',
193+
version: '1',
194+
data: {
195+
agent: 'Magic Button Agent',
196+
input: codeInput,
197+
// @ts-expect-error: TODO
198+
context: {
199+
cell_id: cellId,
200+
content
201+
},
202+
messages: [source],
203+
commands: [
204+
{
205+
name: 'update-cell-source',
206+
args: {
207+
cell_id: cellId,
208+
cell_type: 'code',
209+
source: source
210+
}
211+
}
212+
]
213+
}
214+
});
215+
// TODO:
216+
console.log('MAGIC PROVIDER');
217+
console.log(cellId, codeInput, content);
218+
}
219+
};
220+
}
221+
};
222+
223+
export default [chatPlugin, aiProviderPlugin, magicProviderPlugin];

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)