1
1
import { HttpClient } from '@angular/common/http' ;
2
- import { Component , input , output } from '@angular/core' ;
2
+ import { Component , inject , input , output , signal } from '@angular/core' ;
3
3
import { FormsModule } from '@angular/forms' ;
4
4
import { firstValueFrom } from 'rxjs' ;
5
5
import {
@@ -20,17 +20,19 @@ interface Model {
20
20
styleUrl : './ai-assistant.scss' ,
21
21
imports : [ FormsModule , MessageSpinner ] ,
22
22
host : {
23
- '[class.expanded]' : 'isExpanded' ,
23
+ '[class.expanded]' : 'isExpanded() ' ,
24
24
} ,
25
25
} )
26
26
export class AiAssistant {
27
27
readonly reportGroupId = input . required < string > ( ) ;
28
28
readonly close = output ( ) ;
29
29
30
30
protected messages : AiChatMessage [ ] = [ ] ;
31
- protected userInput = '' ;
32
- protected isLoading = false ;
33
- protected isExpanded = false ;
31
+ protected userInput = signal ( '' ) ;
32
+ protected isLoading = signal ( false ) ;
33
+ protected isExpanded = signal ( false ) ;
34
+
35
+ private readonly http = inject ( HttpClient ) ;
34
36
35
37
protected readonly models : Model [ ] = [
36
38
{ id : 'gemini-2.5-flash' , name : 'Gemini 2.5 Flash' } ,
@@ -39,23 +41,21 @@ export class AiAssistant {
39
41
] ;
40
42
protected selectedModel = this . models [ 0 ] . id ;
41
43
42
- constructor ( private readonly http : HttpClient ) { }
43
-
44
44
protected toggleExpanded ( ) : void {
45
- this . isExpanded = ! this . isExpanded ;
45
+ this . isExpanded . set ( ! this . isExpanded ( ) ) ;
46
46
}
47
47
48
48
async send ( ) : Promise < void > {
49
- if ( ! this . userInput . trim ( ) || this . isLoading ) {
49
+ if ( ! this . userInput ( ) . trim ( ) || this . isLoading ( ) ) {
50
50
return ;
51
51
}
52
52
53
53
const pastMessages = this . messages . slice ( ) ;
54
54
55
- this . messages . push ( { role : 'user' , text : this . userInput } ) ;
56
- const prompt = this . userInput ;
57
- this . userInput = '' ;
58
- this . isLoading = true ;
55
+ this . messages . push ( { role : 'user' , text : this . userInput ( ) } ) ;
56
+ const prompt = this . userInput ( ) ;
57
+ this . userInput . set ( '' ) ;
58
+ this . isLoading . set ( true ) ;
59
59
60
60
const payload : AiChatRequest = {
61
61
prompt,
@@ -75,7 +75,7 @@ export class AiAssistant {
75
75
text : 'Sorry, I failed to get a response. Please try again.' ,
76
76
} ) ;
77
77
} finally {
78
- this . isLoading = false ;
78
+ this . isLoading . set ( false ) ;
79
79
}
80
80
}
81
81
}
0 commit comments