@@ -180,17 +180,14 @@ export class FreestylerPanel extends UI.Panel.Panel {
180180 #toggleSearchElementAction: UI . ActionRegistration . Action ;
181181 #contentContainer: HTMLElement ;
182182 #aidaClient: Host . AidaClient . AidaClient ;
183- #freestylerAgent: FreestylerAgent ;
184- #drJonesFileAgent: DrJonesFileAgent ;
185- #drJonesNetworkAgent: DrJonesNetworkAgent ;
186- #drJonesPerformanceAgent: DrJonesPerformanceAgent ;
187183 #viewProps: FreestylerChatUiProps ;
188184 #viewOutput: ViewOutput = { } ;
189185 #serverSideLoggingEnabled = isFreestylerServerSideLoggingEnabled ( ) ;
190186 #freestylerEnabledSetting: Common . Settings . Setting < boolean > | undefined ;
191187 #changeManager = new ChangeManager ( ) ;
192188
193189 #agents = new Set < AiAgent < unknown > > ( ) ;
190+ #currentAgent?: AiAgent < unknown > ;
194191
195192 constructor ( private view : View = defaultView , { aidaClient, aidaAvailability, syncInfo} : {
196193 aidaClient : Host . AidaClient . AidaClient ,
@@ -235,11 +232,6 @@ export class FreestylerPanel extends UI.Panel.Panel {
235232 selectedNetworkRequest : null ,
236233 selectedAiCallTree : null ,
237234 } ;
238-
239- this . #freestylerAgent = this . #createFreestylerAgent( ) ;
240- this . #drJonesFileAgent = this . #createDrJonesFileAgent( ) ;
241- this . #drJonesNetworkAgent = this . #createDrJonesNetworkAgent( ) ;
242- this . #drJonesPerformanceAgent = this . #createDrJonesPerformanceAgent( ) ;
243235 }
244236
245237 #getChatUiState( ) : FreestylerChatUiState {
@@ -257,6 +249,19 @@ export class FreestylerPanel extends UI.Panel.Panel {
257249 }
258250 }
259251
252+ #createAgent( agentType : AgentType ) : AiAgent < unknown > {
253+ switch ( agentType ) {
254+ case AgentType . FREESTYLER :
255+ return this . #createFreestylerAgent( ) ;
256+ case AgentType . DRJONES_FILE :
257+ return this . #createDrJonesFileAgent( ) ;
258+ case AgentType . DRJONES_NETWORK_REQUEST :
259+ return this . #createDrJonesNetworkAgent( ) ;
260+ case AgentType . DRJONES_PERFORMANCE :
261+ return this . #createDrJonesPerformanceAgent( ) ;
262+ }
263+ }
264+
260265 #createFreestylerAgent( ) : FreestylerAgent {
261266 const agent = new FreestylerAgent ( {
262267 aidaClient : this . #aidaClient,
@@ -469,71 +474,60 @@ export class FreestylerPanel extends UI.Panel.Panel {
469474 }
470475
471476 handleAction ( actionId : string ) : void {
477+ let targetAgentType : AgentType | undefined ;
472478 switch ( actionId ) {
473479 case 'freestyler.elements-floating-button' : {
474- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
475480 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . FreestylerOpenedFromElementsPanelFloatingButton ) ;
476- this . #viewProps. agentType = AgentType . FREESTYLER ;
477- this . #viewProps. messages = [ ] ;
478- this . doUpdate ( ) ;
479- void this . #doConversation( this . #freestylerAgent. runFromHistory ( ) ) ;
481+ targetAgentType = AgentType . FREESTYLER ;
480482 break ;
481483 }
482484 case 'freestyler.element-panel-context' : {
483- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
484485 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . FreestylerOpenedFromElementsPanel ) ;
485- this . #viewProps. agentType = AgentType . FREESTYLER ;
486- this . #viewProps. messages = [ ] ;
487- this . doUpdate ( ) ;
488- void this . #doConversation( this . #freestylerAgent. runFromHistory ( ) ) ;
486+ targetAgentType = AgentType . FREESTYLER ;
489487 break ;
490488 }
491489 case 'drjones.network-floating-button' : {
492- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
493490 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . DrJonesOpenedFromNetworkPanelFloatingButton ) ;
494- this . #viewProps. agentType = AgentType . DRJONES_NETWORK_REQUEST ;
495- this . #viewProps. messages = [ ] ;
496- this . doUpdate ( ) ;
497- void this . #doConversation( this . #drJonesNetworkAgent. runFromHistory ( ) ) ;
491+ targetAgentType = AgentType . DRJONES_NETWORK_REQUEST ;
498492 break ;
499493 }
500494 case 'drjones.network-panel-context' : {
501- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
502495 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . DrJonesOpenedFromNetworkPanel ) ;
503- this . #viewProps. agentType = AgentType . DRJONES_NETWORK_REQUEST ;
504- this . #viewProps. messages = [ ] ;
505- this . doUpdate ( ) ;
506- void this . #doConversation( this . #drJonesNetworkAgent. runFromHistory ( ) ) ;
496+ targetAgentType = AgentType . DRJONES_NETWORK_REQUEST ;
507497 break ;
508498 }
509499 case 'drjones.performance-panel-context' : {
510- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
511500 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . DrJonesOpenedFromPerformancePanel ) ;
512- this . #viewProps. agentType = AgentType . DRJONES_PERFORMANCE ;
513- this . #viewProps. messages = [ ] ;
514- this . doUpdate ( ) ;
515- void this . #doConversation( this . #drJonesPerformanceAgent. runFromHistory ( ) ) ;
501+ targetAgentType = AgentType . DRJONES_PERFORMANCE ;
516502 break ;
517503 }
518504 case 'drjones.sources-floating-button' : {
519- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
520505 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . DrJonesOpenedFromSourcesPanelFloatingButton ) ;
521- this . #viewProps. agentType = AgentType . DRJONES_FILE ;
522- this . #viewProps. messages = [ ] ;
523- this . doUpdate ( ) ;
524- void this . #doConversation( this . #drJonesFileAgent. runFromHistory ( ) ) ;
506+ targetAgentType = AgentType . DRJONES_FILE ;
525507 break ;
526508 }
527509 case 'drjones.sources-panel-context' : {
528- this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
529510 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . DrJonesOpenedFromSourcesPanel ) ;
530- this . #viewProps. agentType = AgentType . DRJONES_FILE ;
531- this . #viewProps. messages = [ ] ;
532- this . doUpdate ( ) ;
533- void this . #doConversation( this . #drJonesFileAgent. runFromHistory ( ) ) ;
511+ targetAgentType = AgentType . DRJONES_FILE ;
534512 break ;
535513 }
536514 }
515+
516+ if ( ! targetAgentType ) {
517+ return ;
518+ }
519+
520+ if ( ! this . #currentAgent) {
521+ this . #currentAgent = this . #createAgent( targetAgentType ) ;
522+ } else if ( this . #currentAgent. type !== targetAgentType ) {
523+ this . #currentAgent = this . #createAgent( targetAgentType ) ;
524+ }
525+ this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
526+ Host . userMetrics . actionTaken ( Host . UserMetrics . Action . FreestylerOpenedFromElementsPanelFloatingButton ) ;
527+ this . #viewProps. messages = [ ] ;
528+ this . #viewProps. agentType = this . #currentAgent. type ;
529+ this . doUpdate ( ) ;
530+ void this . #doConversation( this . #currentAgent. runFromHistory ( ) ) ;
537531 }
538532
539533 #onHistoryClicked( event : Event ) : void {
@@ -572,70 +566,25 @@ export class FreestylerPanel extends UI.Panel.Panel {
572566
573567 #clearHistory( ) : void {
574568 this . #agents = new Set ( ) ;
575- this . #freestylerAgent = this . #createFreestylerAgent( ) ;
576- this . #drJonesFileAgent = this . #createDrJonesFileAgent( ) ;
577- this . #drJonesNetworkAgent = this . #createDrJonesNetworkAgent( ) ;
578- this . #drJonesPerformanceAgent = this . #createDrJonesPerformanceAgent( ) ;
569+ this . #currentAgent = undefined ;
579570 this . #viewProps. messages = [ ] ;
580571 this . #viewProps. agentType = undefined ;
581572 this . doUpdate ( ) ;
582573 }
583574
584575 #onDeleteClicked( ) : void {
585- if ( ! this . #viewProps. agentType ) {
586- return ;
587- }
588-
589- switch ( this . #viewProps. agentType ) {
590- case AgentType . FREESTYLER :
591- this . #agents. delete ( this . #freestylerAgent) ;
592- this . #freestylerAgent = this . #createFreestylerAgent( ) ;
593- break ;
594- case AgentType . DRJONES_FILE :
595- this . #agents. delete ( this . #drJonesFileAgent) ;
596- this . #drJonesFileAgent = this . #createDrJonesFileAgent( ) ;
597- break ;
598- case AgentType . DRJONES_NETWORK_REQUEST :
599- this . #agents. delete ( this . #drJonesNetworkAgent) ;
600- this . #drJonesNetworkAgent = this . #createDrJonesNetworkAgent( ) ;
601- break ;
602- case AgentType . DRJONES_PERFORMANCE :
603- this . #agents. delete ( this . #drJonesPerformanceAgent) ;
604- this . #drJonesPerformanceAgent = this . #createDrJonesPerformanceAgent( ) ;
605- break ;
576+ if ( this . #currentAgent) {
577+ const agentType = this . #currentAgent. type ;
578+ this . #agents. delete ( this . #currentAgent) ;
579+ this . #currentAgent = this . #createAgent( agentType ) ;
606580 }
607581 this . #viewProps. messages = [ ] ;
608582 this . #viewProps. agentType = undefined ;
609583 this . doUpdate ( ) ;
610584 }
611585
612586 async #switchAgent( agent : AiAgent < unknown > ) : Promise < void > {
613- switch ( agent . type ) {
614- case AgentType . FREESTYLER :
615- if ( this . #freestylerAgent === agent && agent . type === this . #viewProps. agentType ) {
616- return ;
617- }
618- this . #freestylerAgent = agent as FreestylerAgent ;
619- break ;
620- case AgentType . DRJONES_FILE :
621- if ( this . #drJonesFileAgent === agent && agent . type === this . #viewProps. agentType ) {
622- return ;
623- }
624- this . #drJonesFileAgent = agent as DrJonesFileAgent ;
625- break ;
626- case AgentType . DRJONES_NETWORK_REQUEST :
627- if ( this . #drJonesNetworkAgent === agent && agent . type === this . #viewProps. agentType ) {
628- return ;
629- }
630- this . #drJonesNetworkAgent = agent as DrJonesNetworkAgent ;
631- break ;
632- case AgentType . DRJONES_PERFORMANCE :
633- if ( this . #drJonesPerformanceAgent === agent && agent . type === this . #viewProps. agentType ) {
634- return ;
635- }
636- this . #drJonesPerformanceAgent = agent as DrJonesPerformanceAgent ;
637- break ;
638- }
587+ this . #currentAgent = agent ;
639588 this . #viewProps. messages = [ ] ;
640589 this . #viewProps. agentType = agent . type ;
641590 await this . #doConversation( agent . runFromHistory ( ) ) ;
@@ -644,21 +593,9 @@ export class FreestylerPanel extends UI.Panel.Panel {
644593 #clearMessages( ) : void {
645594 this . #viewProps. messages = [ ] ;
646595 this . #viewProps. isLoading = false ;
647- switch ( this . #viewProps. agentType ) {
648- case AgentType . FREESTYLER :
649- this . #freestylerAgent = this . #createFreestylerAgent( ) ;
650- break ;
651- case AgentType . DRJONES_FILE :
652- this . #drJonesFileAgent = this . #createDrJonesFileAgent( ) ;
653- break ;
654- case AgentType . DRJONES_NETWORK_REQUEST :
655- this . #drJonesNetworkAgent = this . #createDrJonesNetworkAgent( ) ;
656- break ;
657- case AgentType . DRJONES_PERFORMANCE :
658- this . #drJonesPerformanceAgent = this . #createDrJonesPerformanceAgent( ) ;
659- break ;
596+ if ( this . #currentAgent) {
597+ this . #currentAgent = this . #createAgent( this . #currentAgent. type ) ;
660598 }
661-
662599 this . #cancel( ) ;
663600 this . doUpdate ( ) ;
664601 UI . ARIAUtils . alert ( i18nString ( UIStrings . chatCleared ) ) ;
@@ -672,25 +609,25 @@ export class FreestylerPanel extends UI.Panel.Panel {
672609 }
673610
674611 async #startConversation( text : string ) : Promise < void > {
675- if ( ! this . #viewProps . agentType ) {
612+ if ( ! this . #currentAgent ) {
676613 return ;
677614 }
678615 this . #runAbortController = new AbortController ( ) ;
679616 const signal = this . #runAbortController. signal ;
680617
681618 let runner : AsyncGenerator < ResponseData , void , void > | undefined ;
682- switch ( this . #viewProps . agentType ) {
619+ switch ( this . #currentAgent . type ) {
683620 case AgentType . FREESTYLER :
684- runner = this . #freestylerAgent . run ( text , { signal, selected : this . #viewProps. selectedElement } ) ;
621+ runner = this . #currentAgent . run ( text , { signal, selected : this . #viewProps. selectedElement } ) ;
685622 break ;
686623 case AgentType . DRJONES_FILE :
687- runner = this . #drJonesFileAgent . run ( text , { signal, selected : this . #viewProps. selectedFile } ) ;
624+ runner = this . #currentAgent . run ( text , { signal, selected : this . #viewProps. selectedFile } ) ;
688625 break ;
689626 case AgentType . DRJONES_NETWORK_REQUEST :
690- runner = this . #drJonesNetworkAgent . run ( text , { signal, selected : this . #viewProps. selectedNetworkRequest } ) ;
627+ runner = this . #currentAgent . run ( text , { signal, selected : this . #viewProps. selectedNetworkRequest } ) ;
691628 break ;
692629 case AgentType . DRJONES_PERFORMANCE :
693- runner = this . #drJonesPerformanceAgent . run ( text , { signal, selected : this . #viewProps. selectedAiCallTree } ) ;
630+ runner = this . #currentAgent . run ( text , { signal, selected : this . #viewProps. selectedAiCallTree } ) ;
694631 break ;
695632 }
696633
0 commit comments