@@ -11,18 +11,22 @@ import {
11
11
JupyterFrontEnd ,
12
12
JupyterFrontEndPlugin
13
13
} from '@jupyterlab/application' ;
14
- import { ReactWidget , IThemeManager } from '@jupyterlab/apputils' ;
14
+ import { IThemeManager , ReactWidget } from '@jupyterlab/apputils' ;
15
15
import { ICompletionProviderManager } from '@jupyterlab/completer' ;
16
16
import { INotebookTracker } from '@jupyterlab/notebook' ;
17
17
import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
18
18
import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
19
19
20
+ import { PartialJSONValue } from '@lumino/coreutils' ;
20
21
import { ChatHandler } from './chat-handler' ;
21
22
import { getSettings } from './llm-models' ;
22
23
import { AIProvider } from './provider' ;
23
24
import { renderSlashCommandOption } from './slash-commands' ;
24
25
import { IAIProvider } from './token' ;
25
26
27
+ import { HumanMessage , SystemMessage } from '@langchain/core/messages' ;
28
+ import { IMagicProvider } from 'jupyterlab_magic_wand' ;
29
+
26
30
const autocompletionRegistryPlugin : JupyterFrontEndPlugin < IAutocompletionRegistry > =
27
31
{
28
32
id : '@jupyterlite/ai:autocompletion-registry' ,
@@ -189,4 +193,74 @@ const aiProviderPlugin: JupyterFrontEndPlugin<IAIProvider> = {
189
193
}
190
194
} ;
191
195
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
+ ] ;
0 commit comments