1- import { Component , OnInit } from '@angular/core' ;
2- import { UntypedFormBuilder , UntypedFormGroup } from '@angular/forms' ;
1+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
2+ import { FormBuilder , FormGroup , FormControl , AbstractControl } from '@angular/forms' ;
33import { Clipboard } from '@angular/cdk/clipboard' ;
44import { Router } from '@angular/router' ;
5+ import { Subject } from 'rxjs' ;
56import { finalize } from 'rxjs/operators' ;
6-
77import { ConfigurationService } from '../configuration/configuration.service' ;
88import { CouchService } from '../shared/couchdb.service' ;
99import { PlanetMessageService } from '../shared/planet-message.service' ;
1010import { StateService } from '../shared/state.service' ;
1111
12+ interface AIServiceConfigForm {
13+ streaming : FormControl < boolean > ;
14+ assistantName : FormControl < string > ;
15+ assistantInstructions : FormControl < string > ;
16+ [ key : string ] : AbstractControl < any , any > ;
17+ }
18+
1219@Component ( {
1320 templateUrl : './manager-aiservices.component.html' ,
1421 styleUrls : [ './manager-settings.shared.scss' ] ,
1522} )
16- export class ManagerAIServicesComponent implements OnInit {
23+ export class ManagerAIServicesComponent implements OnInit , OnDestroy {
1724 configuration : any = { } ;
18- configForm : UntypedFormGroup ;
25+ configForm : FormGroup < AIServiceConfigForm > ;
1926 hideKey : { [ key : string ] : boolean } = { } ;
2027 spinnerOn = true ;
28+ private unsubscribe$ = new Subject < void > ( ) ;
2129
2230 constructor (
23- private fb : UntypedFormBuilder ,
31+ private fb : FormBuilder ,
2432 private clipboard : Clipboard ,
2533 private configurationService : ConfigurationService ,
2634 private couchService : CouchService ,
@@ -29,10 +37,10 @@ export class ManagerAIServicesComponent implements OnInit {
2937 private stateService : StateService ,
3038 ) {
3139 this . configForm = this . fb . group ( {
32- streaming : [ false ] ,
33- assistantName : [ '' ] ,
34- assistantInstructions : [ '' ]
35- } ) ;
40+ streaming : new FormControl ( false , { nonNullable : true } ) ,
41+ assistantName : new FormControl ( '' , { nonNullable : true } ) ,
42+ assistantInstructions : new FormControl ( '' , { nonNullable : true } )
43+ } ) as FormGroup < AIServiceConfigForm > ;
3644 }
3745
3846 ngOnInit ( ) {
@@ -41,14 +49,19 @@ export class ManagerAIServicesComponent implements OnInit {
4149 this . initForm ( ) ;
4250 }
4351
52+ ngOnDestroy ( ) {
53+ this . unsubscribe$ . next ( ) ;
54+ this . unsubscribe$ . complete ( ) ;
55+ }
56+
4457 initForm ( ) {
4558 this . configForm = this . fb . group ( {
46- streaming : [ ! ! this . configuration . streaming ] ,
59+ streaming : new FormControl ( ! ! this . configuration . streaming , { nonNullable : true } ) ,
4760 ...this . mapConfigToFormGroup ( this . configuration . keys , 'keys_' ) ,
4861 ...this . mapConfigToFormGroup ( this . configuration . models , 'models_' ) ,
49- assistantName : [ this . configuration . assistant ?. name || '' ] ,
50- assistantInstructions : [ this . configuration . assistant ?. instructions || '' ]
51- } ) ;
62+ assistantName : new FormControl ( this . configuration . assistant ?. name || '' , { nonNullable : true } ) ,
63+ assistantInstructions : new FormControl ( this . configuration . assistant ?. instructions || '' , { nonNullable : true } )
64+ } ) as FormGroup < AIServiceConfigForm > ;
5265
5366 if ( this . configuration . keys ) {
5467 for ( const key of Object . keys ( this . configuration . keys ) ) {
@@ -58,19 +71,15 @@ export class ManagerAIServicesComponent implements OnInit {
5871 }
5972
6073 mapConfigToFormGroup ( configObject : any , prefix : string ) {
61- const formGroupObj = { } ;
74+ const formGroupObj : { [ key : string ] : FormControl < string > } = { } ;
6275 if ( configObject ) {
6376 for ( const key of Object . keys ( configObject ) ) {
64- formGroupObj [ prefix + key ] = [ configObject [ key ] || '' ] ;
77+ formGroupObj [ prefix + key ] = new FormControl ( configObject [ key ] || '' , { nonNullable : true } ) ;
6578 }
6679 }
6780 return formGroupObj ;
6881 }
6982
70- objectKeys ( obj : any ) : string [ ] {
71- return Object . keys ( obj ) ;
72- }
73-
7483 saveConfig ( ) {
7584 const spinnerOff = ( ) => this . spinnerOn = false ;
7685 if ( ! this . configForm . valid ) {
@@ -112,7 +121,7 @@ export class ManagerAIServicesComponent implements OnInit {
112121 }
113122
114123 copyKey ( key : string ) {
115- const value = this . configForm . get ( 'keys_' + key ) . value ;
124+ const value = this . configForm . get ( 'keys_' + key ) ? .value || '' ;
116125 this . clipboard . copy ( value ) ;
117126 }
118127
0 commit comments