@@ -19,6 +19,10 @@ import { getSettings } from './llm-models';
1919import { AIProvider } from './provider' ;
2020import { 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+
2226const 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 ] ;
0 commit comments