@@ -16,6 +16,8 @@ import {
1616 mergeMessageRuns
1717} from '@langchain/core/messages' ;
1818import { UUID } from '@lumino/coreutils' ;
19+ import { getErrorMessage } from './llm-models' ;
20+ import { IAIProvider } from './token' ;
1921
2022export type ConnectionMessage = {
2123 type : 'connection' ;
@@ -25,14 +27,14 @@ export type ConnectionMessage = {
2527export class ChatHandler extends ChatModel {
2628 constructor ( options : ChatHandler . IOptions ) {
2729 super ( options ) ;
28- this . _provider = options . provider ;
30+ this . _aiProvider = options . aiProvider ;
31+ this . _aiProvider . modelChange . connect ( ( ) => {
32+ this . _errorMessage = this . _aiProvider . chatError ;
33+ } ) ;
2934 }
3035
3136 get provider ( ) : BaseChatModel | null {
32- return this . _provider ;
33- }
34- set provider ( provider : BaseChatModel | null ) {
35- this . _provider = provider ;
37+ return this . _aiProvider . chatModel ;
3638 }
3739
3840 async sendMessage ( message : INewMessage ) : Promise < boolean > {
@@ -46,15 +48,15 @@ export class ChatHandler extends ChatModel {
4648 } ;
4749 this . messageAdded ( msg ) ;
4850
49- if ( this . _provider === null ) {
50- const botMsg : IChatMessage = {
51+ if ( this . _aiProvider . chatModel === null ) {
52+ const errorMsg : IChatMessage = {
5153 id : UUID . uuid4 ( ) ,
52- body : '**AI provider not configured for the chat**' ,
54+ body : `** ${ this . _errorMessage ? this . _errorMessage : this . _defaultErrorMessage } **` ,
5355 sender : { username : 'ERROR' } ,
5456 time : Date . now ( ) ,
5557 type : 'msg'
5658 } ;
57- this . messageAdded ( botMsg ) ;
59+ this . messageAdded ( errorMsg ) ;
5860 return false ;
5961 }
6062
@@ -69,19 +71,37 @@ export class ChatHandler extends ChatModel {
6971 } )
7072 ) ;
7173
72- const response = await this . _provider . invoke ( messages ) ;
73- // TODO: fix deprecated response.text
74- const content = response . text ;
75- const botMsg : IChatMessage = {
76- id : UUID . uuid4 ( ) ,
77- body : content ,
78- sender : { username : 'Bot' } ,
79- time : Date . now ( ) ,
80- type : 'msg'
81- } ;
82- this . messageAdded ( botMsg ) ;
83- this . _history . messages . push ( botMsg ) ;
84- return true ;
74+ this . updateWriters ( [ { username : 'AI' } ] ) ;
75+ return this . _aiProvider . chatModel
76+ . invoke ( messages )
77+ . then ( response => {
78+ const content = response . content ;
79+ const botMsg : IChatMessage = {
80+ id : UUID . uuid4 ( ) ,
81+ body : content . toString ( ) ,
82+ sender : { username : 'AI' } ,
83+ time : Date . now ( ) ,
84+ type : 'msg'
85+ } ;
86+ this . messageAdded ( botMsg ) ;
87+ this . _history . messages . push ( botMsg ) ;
88+ return true ;
89+ } )
90+ . catch ( reason => {
91+ const error = getErrorMessage ( this . _aiProvider . name , reason ) ;
92+ const errorMsg : IChatMessage = {
93+ id : UUID . uuid4 ( ) ,
94+ body : `**${ error } **` ,
95+ sender : { username : 'ERROR' } ,
96+ time : Date . now ( ) ,
97+ type : 'msg'
98+ } ;
99+ this . messageAdded ( errorMsg ) ;
100+ return false ;
101+ } )
102+ . finally ( ( ) => {
103+ this . updateWriters ( [ ] ) ;
104+ } ) ;
85105 }
86106
87107 async getHistory ( ) : Promise < IChatHistory > {
@@ -96,12 +116,14 @@ export class ChatHandler extends ChatModel {
96116 super . messageAdded ( message ) ;
97117 }
98118
99- private _provider : BaseChatModel | null ;
119+ private _aiProvider : IAIProvider ;
120+ private _errorMessage : string = '' ;
100121 private _history : IChatHistory = { messages : [ ] } ;
122+ private _defaultErrorMessage = 'AI provider not configured' ;
101123}
102124
103125export namespace ChatHandler {
104126 export interface IOptions extends ChatModel . IOptions {
105- provider : BaseChatModel | null ;
127+ aiProvider : IAIProvider ;
106128 }
107129}
0 commit comments