|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import '../../ui/components/tooltips/tooltips.js'; |
| 6 | + |
| 7 | +import * as i18n from '../../core/i18n/i18n.js'; |
| 8 | +import * as Root from '../../core/root/root.js'; |
| 9 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 10 | +import {Directives, html, nothing, render} from '../../ui/lit/lit.js'; |
| 11 | +import * as VisualLogging from '../../ui/visual_logging/visual_logging.js'; |
| 12 | + |
| 13 | +import styles from './aiCodeCompletionSummaryToolbar.css.js'; |
| 14 | + |
| 15 | +const UIStrings = { |
| 16 | + /** |
| 17 | + *@description Disclaimer text for AI code completion |
| 18 | + */ |
| 19 | + relevantData: 'Relevant data', |
| 20 | + /** |
| 21 | + * @description Disclaimer text for AI code completion |
| 22 | + */ |
| 23 | + isSentToGoogle: 'is sent to Google', |
| 24 | + /** |
| 25 | + *@description Text for tooltip shown on hovering over "Relevant Data" in the disclaimer text for AI code completion. |
| 26 | + */ |
| 27 | + tooltipDisclaimerTextForAiCodeCompletion: |
| 28 | + 'To generate code suggestions, your console input and the history of your current console session are shared with Google. This data may be seen by human reviewers to improve this feature.', |
| 29 | + /** |
| 30 | + *@description Text for tooltip shown on hovering over "Relevant Data" in the disclaimer text for AI code completion. |
| 31 | + */ |
| 32 | + tooltipDisclaimerTextForAiCodeCompletionNoLogging: |
| 33 | + 'To generate code suggestions, your console input and the history of your current console session are shared with Google. This data will not be used to improve Google’s AI models.', |
| 34 | + /** |
| 35 | + *@description Text for tooltip button which redirects to AI settings |
| 36 | + */ |
| 37 | + manageInSettings: 'Manage in settings', |
| 38 | + /** |
| 39 | + *@description Text for recitation notice |
| 40 | + */ |
| 41 | + generatedCodeMayBeSubjectToALicense: 'Generated code may be subject to a license.', |
| 42 | + /** |
| 43 | + *@description Text for citations |
| 44 | + */ |
| 45 | + viewSources: 'View Sources', |
| 46 | +} as const; |
| 47 | + |
| 48 | +const lockedString = i18n.i18n.lockedString; |
| 49 | + |
| 50 | +export interface ViewInput { |
| 51 | + disclaimerTooltipId: string; |
| 52 | + panelName: string; |
| 53 | + citations?: string[]; |
| 54 | + citationsTooltipId: string; |
| 55 | + noLogging: boolean; |
| 56 | + onManageInSettingsTooltipClick: () => void; |
| 57 | +} |
| 58 | + |
| 59 | +export interface ViewOutput { |
| 60 | + tooltipRef?: Directives.Ref<HTMLElement>; |
| 61 | +} |
| 62 | + |
| 63 | +export type View = (input: ViewInput, output: ViewOutput, target: HTMLElement) => void; |
| 64 | + |
| 65 | +export const DEFAULT_SUMMARY_TOOLBAR_VIEW: View = (input, output, target) => { |
| 66 | + output.tooltipRef = output.tooltipRef ?? Directives.createRef<HTMLElement>(); |
| 67 | + |
| 68 | + // clang-format off |
| 69 | + const viewSourcesSpan = input.citations && input.citations.length > 0 ? |
| 70 | + html`<span class="link" role="link" aria-details=${input.citationsTooltipId}> |
| 71 | + ${lockedString(UIStrings.viewSources)} ${lockedString('(' + input.citations.length + ')')}</span>` : nothing; |
| 72 | + const viewSourcesTooltip = input.citations && input.citations.length > 0 ? |
| 73 | + html`<devtools-tooltip |
| 74 | + id=${input.citationsTooltipId} |
| 75 | + variant=${'rich'} |
| 76 | + jslogContext=${input.panelName + '.ai-code-completion-citations'} |
| 77 | + ><div class="citations-tooltip-container"> |
| 78 | + ${Directives.repeat(input.citations, citation => html`<x-link |
| 79 | + href=${citation} |
| 80 | + jslog=${VisualLogging.link(input.panelName + '.ai-code-completion-citations.citation-link').track({ |
| 81 | + click: true |
| 82 | + })}>${citation}</x-link>`)}</div></devtools-tooltip>` : nothing; |
| 83 | + |
| 84 | + render( |
| 85 | + html` |
| 86 | + <style>${styles}</style> |
| 87 | + <div class="ai-code-completion-summary-toolbar"> |
| 88 | + <div class="ai-code-completion-disclaimer"> |
| 89 | + <span |
| 90 | + class="link" |
| 91 | + role="link" |
| 92 | + jslog=${VisualLogging.link('open-ai-settings').track({ |
| 93 | + click: true, |
| 94 | + })} |
| 95 | + aria-details=${input.disclaimerTooltipId} |
| 96 | + @click=${() => { |
| 97 | + void UI.ViewManager.ViewManager.instance().showView('chrome-ai'); |
| 98 | + }} |
| 99 | + >${lockedString(UIStrings.relevantData)}</span> ${lockedString(UIStrings.isSentToGoogle)} |
| 100 | + <devtools-tooltip |
| 101 | + id=${input.disclaimerTooltipId} |
| 102 | + variant=${'rich'} |
| 103 | + jslogContext=${input.panelName + '.ai-code-completion-disclaimer'} |
| 104 | + ${Directives.ref(output.tooltipRef)} |
| 105 | + ><div class="disclaimer-tooltip-container"> |
| 106 | + <div class="tooltip-text"> |
| 107 | + ${input.noLogging ? lockedString(UIStrings.tooltipDisclaimerTextForAiCodeCompletionNoLogging) : lockedString(UIStrings.tooltipDisclaimerTextForAiCodeCompletion)} |
| 108 | + </div> |
| 109 | + <div |
| 110 | + class="link" |
| 111 | + role="link" |
| 112 | + jslog=${VisualLogging.link('open-ai-settings').track({ |
| 113 | + click: true, |
| 114 | + })} |
| 115 | + @click=${input.onManageInSettingsTooltipClick} |
| 116 | + >${lockedString(UIStrings.manageInSettings)}</div></div></devtools-tooltip> |
| 117 | + </div> |
| 118 | + <div class="ai-code-completion-recitation-notice">${lockedString(UIStrings.generatedCodeMayBeSubjectToALicense)} |
| 119 | + ${viewSourcesSpan} |
| 120 | + ${viewSourcesTooltip} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + `, target, {host: input}); |
| 124 | + // clang-format on |
| 125 | +}; |
| 126 | + |
| 127 | +export class AiCodeCompletionSummaryToolbar extends UI.Widget.Widget { |
| 128 | + readonly #view: View; |
| 129 | + #viewOutput: ViewOutput = {}; |
| 130 | + |
| 131 | + #disclaimerTooltipId: string; |
| 132 | + #citationsTooltipId: string; |
| 133 | + #panelName: string; |
| 134 | + #citations: string[] = []; |
| 135 | + |
| 136 | + #noLogging: boolean; // Whether the enterprise setting is `ALLOW_WITHOUT_LOGGING` or not. |
| 137 | + |
| 138 | + constructor(disclaimerTooltipId: string, citationsTooltipId: string, panelName: string, view?: View) { |
| 139 | + super(); |
| 140 | + this.#disclaimerTooltipId = disclaimerTooltipId; |
| 141 | + this.#citationsTooltipId = citationsTooltipId; |
| 142 | + this.#panelName = panelName; |
| 143 | + this.#noLogging = Root.Runtime.hostConfig.aidaAvailability?.enterprisePolicyValue === |
| 144 | + Root.Runtime.GenAiEnterprisePolicyValue.ALLOW_WITHOUT_LOGGING; |
| 145 | + this.#view = view ?? DEFAULT_SUMMARY_TOOLBAR_VIEW; |
| 146 | + this.requestUpdate(); |
| 147 | + } |
| 148 | + |
| 149 | + #onManageInSettingsTooltipClick(): void { |
| 150 | + this.#viewOutput.tooltipRef?.value?.hidePopover(); |
| 151 | + void UI.ViewManager.ViewManager.instance().showView('chrome-ai'); |
| 152 | + } |
| 153 | + |
| 154 | + updateCitations(citations: string[]): void { |
| 155 | + citations.forEach(citation => { |
| 156 | + if (!this.#citations.includes(citation)) { |
| 157 | + this.#citations.push(citation); |
| 158 | + } |
| 159 | + }); |
| 160 | + this.requestUpdate(); |
| 161 | + } |
| 162 | + |
| 163 | + clearCitations(): void { |
| 164 | + this.#citations = []; |
| 165 | + this.requestUpdate(); |
| 166 | + } |
| 167 | + |
| 168 | + override performUpdate(): void { |
| 169 | + this.#view( |
| 170 | + { |
| 171 | + disclaimerTooltipId: this.#disclaimerTooltipId, |
| 172 | + citations: this.#citations, |
| 173 | + citationsTooltipId: this.#citationsTooltipId, |
| 174 | + panelName: this.#panelName, |
| 175 | + noLogging: this.#noLogging, |
| 176 | + onManageInSettingsTooltipClick: this.#onManageInSettingsTooltipClick.bind(this), |
| 177 | + }, |
| 178 | + this.#viewOutput, this.contentElement); |
| 179 | + } |
| 180 | +} |
0 commit comments