@@ -13,55 +13,20 @@ import { ICompletionProviderManager } from '@jupyterlab/completer';
1313import { INotebookTracker } from '@jupyterlab/notebook' ;
1414import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
1515import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
16- import { ChatMistralAI , MistralAI } from '@langchain/mistralai' ;
1716
18- import { CodestralHandler } from './handler' ;
19- import { CodestralProvider } from './provider' ;
20-
21- const inlineProviderPlugin : JupyterFrontEndPlugin < void > = {
22- id : 'jupyterlab-codestral:inline-provider' ,
23- autoStart : true ,
24- requires : [ ICompletionProviderManager , ISettingRegistry ] ,
25- activate : (
26- app : JupyterFrontEnd ,
27- manager : ICompletionProviderManager ,
28- settingRegistry : ISettingRegistry
29- ) : void => {
30- const mistralClient = new MistralAI ( {
31- model : 'codestral-latest' ,
32- apiKey : 'TMP'
33- } ) ;
34- const provider = new CodestralProvider ( { mistralClient } ) ;
35- manager . registerInlineProvider ( provider ) ;
36-
37- settingRegistry
38- . load ( inlineProviderPlugin . id )
39- . then ( settings => {
40- const updateKey = ( ) => {
41- const apiKey = settings . get ( 'apiKey' ) . composite as string ;
42- mistralClient . apiKey = apiKey ;
43- } ;
44-
45- settings . changed . connect ( ( ) => updateKey ( ) ) ;
46- updateKey ( ) ;
47- } )
48- . catch ( reason => {
49- console . error (
50- `Failed to load settings for ${ inlineProviderPlugin . id } ` ,
51- reason
52- ) ;
53- } ) ;
54- }
55- } ;
17+ import { ChatHandler } from './chat-handler' ;
18+ import { AIProvider } from './provider' ;
19+ import { IAIProvider } from './token' ;
5620
5721const chatPlugin : JupyterFrontEndPlugin < void > = {
5822 id : 'jupyterlab-codestral:chat' ,
59- description : 'Codestral chat extension' ,
23+ description : 'LLM chat extension' ,
6024 autoStart : true ,
6125 optional : [ INotebookTracker , ISettingRegistry , IThemeManager ] ,
62- requires : [ IRenderMimeRegistry ] ,
26+ requires : [ IAIProvider , IRenderMimeRegistry ] ,
6327 activate : async (
6428 app : JupyterFrontEnd ,
29+ aiProvider : IAIProvider ,
6530 rmRegistry : IRenderMimeRegistry ,
6631 notebookTracker : INotebookTracker | null ,
6732 settingsRegistry : ISettingRegistry | null ,
@@ -75,15 +40,15 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
7540 } ) ;
7641 }
7742
78- const mistralClient = new ChatMistralAI ( {
79- model : 'codestral-latest' ,
80- apiKey : 'TMP'
81- } ) ;
82- const chatHandler = new CodestralHandler ( {
83- mistralClient,
43+ const chatHandler = new ChatHandler ( {
44+ provider : aiProvider . chatModel ,
8445 activeCellManager : activeCellManager
8546 } ) ;
8647
48+ aiProvider . modelChange . connect ( ( ) => {
49+ chatHandler . provider = aiProvider . chatModel ;
50+ } ) ;
51+
8752 let sendWithShiftEnter = false ;
8853 let enableCodeToolbar = true ;
8954
@@ -94,25 +59,6 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
9459 chatHandler . config = { sendWithShiftEnter, enableCodeToolbar } ;
9560 }
9661
97- // TODO: handle the apiKey better
98- settingsRegistry
99- ?. load ( inlineProviderPlugin . id )
100- . then ( settings => {
101- const updateKey = ( ) => {
102- const apiKey = settings . get ( 'apiKey' ) . composite as string ;
103- mistralClient . apiKey = apiKey ;
104- } ;
105-
106- settings . changed . connect ( ( ) => updateKey ( ) ) ;
107- updateKey ( ) ;
108- } )
109- . catch ( reason => {
110- console . error (
111- `Failed to load settings for ${ inlineProviderPlugin . id } ` ,
112- reason
113- ) ;
114- } ) ;
115-
11662 Promise . all ( [ app . restored , settingsRegistry ?. load ( chatPlugin . id ) ] )
11763 . then ( ( [ , settings ] ) => {
11864 if ( ! settings ) {
@@ -148,4 +94,38 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
14894 }
14995} ;
15096
151- export default [ inlineProviderPlugin , chatPlugin ] ;
97+ const aiProviderPlugin : JupyterFrontEndPlugin < IAIProvider > = {
98+ id : 'jupyterlab-codestral:ai-provider' ,
99+ autoStart : true ,
100+ requires : [ ICompletionProviderManager , ISettingRegistry ] ,
101+ provides : IAIProvider ,
102+ activate : (
103+ app : JupyterFrontEnd ,
104+ manager : ICompletionProviderManager ,
105+ settingRegistry : ISettingRegistry
106+ ) : IAIProvider => {
107+ const aiProvider = new AIProvider ( { completionProviderManager : manager } ) ;
108+
109+ settingRegistry
110+ . load ( aiProviderPlugin . id )
111+ . then ( settings => {
112+ const updateProvider = ( ) => {
113+ const provider = settings . get ( 'provider' ) . composite as string ;
114+ aiProvider . setModels ( provider , settings . composite ) ;
115+ } ;
116+
117+ settings . changed . connect ( ( ) => updateProvider ( ) ) ;
118+ updateProvider ( ) ;
119+ } )
120+ . catch ( reason => {
121+ console . error (
122+ `Failed to load settings for ${ aiProviderPlugin . id } ` ,
123+ reason
124+ ) ;
125+ } ) ;
126+
127+ return aiProvider ;
128+ }
129+ } ;
130+
131+ export default [ chatPlugin , aiProviderPlugin ] ;
0 commit comments