11// Copyright 2025 The Chromium Authors. All rights reserved.
22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
4+
45import * as Common from '../../core/common/common.js' ;
56import * as Host from '../../core/host/host.js' ;
67import * as Root from '../../core/root/root.js' ;
@@ -15,6 +16,7 @@ import * as PanelCommon from '../common/common.js';
1516import { Plugin } from './Plugin.js' ;
1617
1718const AI_CODE_COMPLETION_CHARACTER_LIMIT = 20_000 ;
19+ const DISCLAIMER_TOOLTIP_ID = 'sources-ai-code-completion-disclaimer-tooltip' ;
1820
1921export class AiCodeCompletionPlugin extends Plugin {
2022 #aidaClient?: Host . AidaClient . AidaClient ;
@@ -26,6 +28,9 @@ export class AiCodeCompletionPlugin extends Plugin {
2628 #teaser?: PanelCommon . AiCodeCompletionTeaser ;
2729 #teaserDisplayTimeout?: number ;
2830 #editor?: TextEditor . TextEditor . TextEditor ;
31+ #aiCodeCompletionDisclaimer?: PanelCommon . AiCodeCompletionDisclaimer ;
32+ #aiCodeCompletionDisclaimerContainer = document . createElement ( 'div' ) ;
33+ #aiCodeCompletionDisclaimerToolbarItem = new UI . Toolbar . ToolbarItem ( this . #aiCodeCompletionDisclaimerContainer) ;
2934
3035 #boundEditorKeyDown: ( event : Event ) => Promise < void > ;
3136 #boundOnAiCodeCompletionSettingChanged: ( ) => void ;
@@ -51,6 +56,10 @@ export class AiCodeCompletionPlugin extends Plugin {
5156 this . #teaser = undefined ;
5257 this . #aiCodeCompletionSetting. removeChangeListener ( this . #boundOnAiCodeCompletionSettingChanged) ;
5358 this . #editor?. removeEventListener ( 'keydown' , this . #boundEditorKeyDown) ;
59+ this . #aiCodeCompletion?. removeEventListener (
60+ AiCodeCompletion . AiCodeCompletion . Events . REQUEST_TRIGGERED , this . #onAiRequestTriggered, this ) ;
61+ this . #aiCodeCompletion?. removeEventListener (
62+ AiCodeCompletion . AiCodeCompletion . Events . RESPONSE_RECEIVED , this . #onAiResponseReceived, this ) ;
5463 this . #aiCodeCompletion?. remove ( ) ;
5564 super . dispose ( ) ;
5665 }
@@ -72,6 +81,10 @@ export class AiCodeCompletionPlugin extends Plugin {
7281 ] ;
7382 }
7483
84+ override rightToolbarItems ( ) : UI . Toolbar . ToolbarItem [ ] {
85+ return [ this . #aiCodeCompletionDisclaimerToolbarItem] ;
86+ }
87+
7588 #editorUpdate( update : CodeMirror . ViewUpdate ) : void {
7689 if ( this . #teaser) {
7790 if ( update . docChanged ) {
@@ -181,17 +194,42 @@ export class AiCodeCompletionPlugin extends Plugin {
181194 }
182195 this . #aiCodeCompletion =
183196 new AiCodeCompletion . AiCodeCompletion . AiCodeCompletion ( { aidaClient : this . #aidaClient} , this . #editor) ;
197+ this . #aiCodeCompletion. addEventListener (
198+ AiCodeCompletion . AiCodeCompletion . Events . REQUEST_TRIGGERED , this . #onAiRequestTriggered, this ) ;
199+ this . #aiCodeCompletion. addEventListener (
200+ AiCodeCompletion . AiCodeCompletion . Events . RESPONSE_RECEIVED , this . #onAiResponseReceived, this ) ;
201+ }
202+
203+ #createAiCodeCompletionDisclaimer( ) : void {
204+ this . #aiCodeCompletionDisclaimer = new PanelCommon . AiCodeCompletionDisclaimer ( ) ;
205+ this . #aiCodeCompletionDisclaimer. disclaimerTooltipId = DISCLAIMER_TOOLTIP_ID ;
206+ this . #aiCodeCompletionDisclaimer. show ( this . #aiCodeCompletionDisclaimerContainer, undefined , true ) ;
184207 }
185208
186209 #onAiCodeCompletionSettingChanged( ) : void {
187210 if ( this . #aiCodeCompletionSetting. get ( ) ) {
188211 this . #setAiCodeCompletion( ) ;
212+ this . #createAiCodeCompletionDisclaimer( ) ;
189213 } else if ( this . #aiCodeCompletion) {
190214 this . #aiCodeCompletion. remove ( ) ;
191215 this . #aiCodeCompletion = undefined ;
216+ this . #aiCodeCompletionDisclaimerContainer. removeChildren ( ) ;
217+ this . #aiCodeCompletionDisclaimer = undefined ;
192218 }
193219 }
194220
221+ #onAiRequestTriggered = ( ) : void => {
222+ if ( this . #aiCodeCompletionDisclaimer) {
223+ this . #aiCodeCompletionDisclaimer. loading = true ;
224+ }
225+ } ;
226+
227+ #onAiResponseReceived = ( ) : void => {
228+ if ( this . #aiCodeCompletionDisclaimer) {
229+ this . #aiCodeCompletionDisclaimer. loading = false ;
230+ }
231+ } ;
232+
195233 #detachAiCodeCompletionTeaser( ) : void {
196234 this . #editor?. dispatch ( {
197235 effects : this . #teaserCompartment. reconfigure ( [ ] ) ,
0 commit comments