@@ -19,7 +19,6 @@ import { OpenAiFile, GetThreadResponseDto } from '@boldare/openai-assistant';
1919import { Message } from 'openai/resources/beta/threads/messages' ;
2020import { TextContentBlock } from 'openai/resources/beta/threads/messages/messages' ;
2121
22-
2322@Injectable ( { providedIn : 'root' } )
2423export class ChatService {
2524 isLoading$ = new BehaviorSubject < boolean > ( false ) ;
@@ -35,11 +34,21 @@ export class ChatService {
3534 ) {
3635 document . body . classList . add ( 'ai-chat' ) ;
3736
37+ this . subscribeMessages ( ) ;
3838 this . setInitialValues ( ) ;
39- this . watchMessages ( ) ;
4039 this . watchVisibility ( ) ;
4140 }
4241
42+ subscribeMessages ( ) : void {
43+ if ( ! environment . isStreamingEnabled ) {
44+ this . watchMessages ( ) ;
45+ } else {
46+ this . watchTextCreated ( ) ;
47+ this . watchTextDelta ( ) ;
48+ this . watchTextDone ( ) ;
49+ }
50+ }
51+
4352 isMessageInvisible ( message : Message ) : boolean {
4453 const metadata = message . metadata as Record < string , unknown > ;
4554 return metadata ?. [ 'status' ] === ChatMessageStatus . Invisible ;
@@ -87,11 +96,13 @@ export class ChatService {
8796
8897 refresh ( ) : void {
8998 this . isLoading$ . next ( true ) ;
99+ this . isTyping$ . next ( false ) ;
90100 this . messages$ . next ( [ ] ) ;
91101 this . threadService . start ( ) . subscribe ( ) ;
92102 }
93103
94104 clear ( ) : void {
105+ this . isTyping$ . next ( false ) ;
95106 this . threadService . clear ( ) ;
96107 this . messages$ . next ( [ ] ) ;
97108 }
@@ -120,15 +131,42 @@ export class ChatService {
120131 const files = await this . chatFilesService . sendFiles ( ) ;
121132 this . addFileMessage ( files ) ;
122133
123- this . chatGatewayService . sendMessage ( {
134+ this . chatGatewayService . callStart ( {
124135 content,
125136 threadId : this . threadService . threadId$ . value ,
126137 file_ids : files . map ( file => file . id ) || [ ] ,
127138 } ) ;
128139 }
129140
141+ watchTextCreated ( ) : Subscription {
142+ return this . chatGatewayService . textCreated ( ) . subscribe ( data => {
143+ this . isTyping$ . next ( false ) ;
144+ this . addMessage ( { content : data . text . value , role : ChatRole . Assistant } ) ;
145+ } ) ;
146+ }
147+
148+ watchTextDelta ( ) : Subscription {
149+ return this . chatGatewayService . textDelta ( ) . subscribe ( data => {
150+ const length = this . messages$ . value . length ;
151+ this . messages$ . value [ length - 1 ] . content = data . text . value ;
152+ } ) ;
153+ }
154+
155+ watchTextDone ( ) : Subscription {
156+ return this . chatGatewayService . textDone ( ) . subscribe ( data => {
157+ this . isTyping$ . next ( false ) ;
158+ this . messages$ . next ( [
159+ ...this . messages$ . value . slice ( 0 , - 1 ) ,
160+ {
161+ content : data . text . value ,
162+ role : ChatRole . Assistant ,
163+ } ,
164+ ] ) ;
165+ } ) ;
166+ }
167+
130168 watchMessages ( ) : Subscription {
131- return this . chatGatewayService . getMessages ( ) . subscribe ( data => {
169+ return this . chatGatewayService . callDone ( ) . subscribe ( data => {
132170 this . addMessage ( {
133171 content : data . content ,
134172 role : ChatRole . Assistant ,
0 commit comments