@@ -216,6 +216,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
216216 selectedFile : null ,
217217 selectedNetworkRequest : null ,
218218 selectedAiCallTree : null ,
219+ blockedByCrossOrigin : false ,
219220 } ;
220221 }
221222
@@ -433,7 +434,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
433434 }
434435
435436 this . #viewProps. selectedElement = createNodeContext ( selectedElementFilter ( ev . data ) ) ;
436- this . doUpdate ( ) ;
437+ this . #onContextSelectionChanged ( ) ;
437438 } ;
438439
439440 #handleNetworkRequestFlavorChange =
@@ -443,7 +444,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
443444 }
444445
445446 this . #viewProps. selectedNetworkRequest = Boolean ( ev . data ) ? new RequestContext ( ev . data ) : null ;
446- this . doUpdate ( ) ;
447+ this . #onContextSelectionChanged ( ) ;
447448 } ;
448449
449450 #handleTraceEntryNodeFlavorChange =
@@ -453,7 +454,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
453454 }
454455
455456 this . #viewProps. selectedAiCallTree = Boolean ( ev . data ) ? new CallTreeContext ( ev . data ) : null ;
456- this . doUpdate ( ) ;
457+ this . #onContextSelectionChanged ( ) ;
457458 } ;
458459
459460 #handleUISourceCodeFlavorChange =
@@ -463,7 +464,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
463464 }
464465
465466 this . #viewProps. selectedFile = Boolean ( ev . data ) ? new FileContext ( ev . data ) : null ;
466- this . doUpdate ( ) ;
467+ this . #onContextSelectionChanged ( ) ;
467468 } ;
468469
469470 #handleFreestylerEnabledSettingChanged = ( ) : void => {
@@ -568,6 +569,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
568569 this . #viewOutput. freestylerChatUi ?. focusTextInput ( ) ;
569570 Host . userMetrics . actionTaken ( Host . UserMetrics . Action . FreestylerOpenedFromElementsPanelFloatingButton ) ;
570571 this . #viewProps. messages = [ ] ;
572+ this . #onContextSelectionChanged( ) ;
571573 this . doUpdate ( ) ;
572574 void this . #doConversation( this . #currentAgent. runFromHistory ( ) ) ;
573575 }
@@ -618,6 +620,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
618620 }
619621 this . #viewProps. messages = [ ] ;
620622 this . #viewProps. agentType = undefined ;
623+ this . #onContextSelectionChanged( ) ;
621624 this . doUpdate ( ) ;
622625 }
623626
@@ -633,6 +636,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
633636 this . #viewProps. isLoading = false ;
634637 if ( this . #currentAgent) {
635638 this . #currentAgent = this . #createAgent( this . #currentAgent. type ) ;
639+ this . #onContextSelectionChanged( ) ;
636640 }
637641 this . #cancel( ) ;
638642 this . doUpdate ( ) ;
@@ -646,12 +650,26 @@ export class FreestylerPanel extends UI.Panel.Panel {
646650 this . doUpdate ( ) ;
647651 }
648652
649- async #startConversation ( text : string ) : Promise < void > {
653+ #onContextSelectionChanged ( ) : void {
650654 if ( ! this . #currentAgent) {
655+ this . #viewProps. blockedByCrossOrigin = false ;
656+ this . doUpdate ( ) ;
651657 return ;
652658 }
653- this . #runAbortController = new AbortController ( ) ;
654- const signal = this . #runAbortController. signal ;
659+ const currentContext = this . #getConversationContext( ) ;
660+ if ( ! currentContext ) {
661+ this . #viewProps. blockedByCrossOrigin = false ;
662+ this . doUpdate ( ) ;
663+ return ;
664+ }
665+ this . #viewProps. blockedByCrossOrigin = ! currentContext . isOriginAllowed ( this . #currentAgent. origin ) ;
666+ this . doUpdate ( ) ;
667+ }
668+
669+ #getConversationContext( ) : ConversationContext < unknown > | null {
670+ if ( ! this . #currentAgent) {
671+ return null ;
672+ }
655673 let context : ConversationContext < unknown > | null ;
656674 switch ( this . #currentAgent. type ) {
657675 case AgentType . FREESTYLER :
@@ -667,10 +685,21 @@ export class FreestylerPanel extends UI.Panel.Panel {
667685 context = this . #viewProps. selectedAiCallTree ;
668686 break ;
669687 }
688+ return context ;
689+ }
690+
691+ async #startConversation( text : string ) : Promise < void > {
692+ if ( ! this . #currentAgent) {
693+ return ;
694+ }
695+ this . #runAbortController = new AbortController ( ) ;
696+ const signal = this . #runAbortController. signal ;
697+ const context = this . #getConversationContext( ) ;
670698 // If a different context is provided, it must be from the same origin.
671699 if ( context && ! context . isOriginAllowed ( this . #currentAgent. origin ) ) {
672- // TODO: inform the user here.
673- // throw new Error('cross-origin context data should not be included');
700+ // This error should not be reached. If it happens, some
701+ // invariants do not hold anymore.
702+ throw new Error ( 'cross-origin context data should not be included' ) ;
674703 }
675704 const runner = this . #currentAgent. run ( text , {
676705 signal,
0 commit comments