Skip to content

Commit d8a167b

Browse files
Samiya CaurDevtools-frontend LUCI CQ
authored andcommitted
Add debug logs for AI code completion
This CL also removes stop sequence for Sources panel code completion requests Bug: 438462595 Bypass-Check-License: Only updates files, no new files are added. Change-Id: Ie1eee829423899368cdcfc6c98fa2854a77d6e7f Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6846799 Reviewed-by: Ergün Erdoğmuş <[email protected]> Commit-Queue: Samiya Caur <[email protected]>
1 parent 4334aa3 commit d8a167b

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

front_end/models/ai_code_completion/AiCodeCompletion.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describeWithEnvironment('AiCodeCompletion', () => {
3737
const aiCodeCompletion = new AiCodeCompletion.AiCodeCompletion(
3838
{aidaClient: mockAidaClient},
3939
sinon.createStubInstance(TextEditor.TextEditor.TextEditor),
40+
['\n'],
4041
);
4142

4243
aiCodeCompletion.onTextChanged('prefix', 'suffix', 6);

front_end/models/ai_code_completion/AiCodeCompletion.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import * as Host from '../../core/host/host.js';
77
import * as Root from '../../core/root/root.js';
88
import * as TextEditor from '../../ui/components/text_editor/text_editor.js';
99

10+
import {debugLog} from './debug.js';
11+
1012
export const DELAY_BEFORE_SHOWING_RESPONSE_MS = 500;
1113
export const AIDA_REQUEST_DEBOUNCE_TIMEOUT_MS = 200;
1214

@@ -40,17 +42,19 @@ interface RequestOptions {
4042
*/
4143
export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
4244
#editor: TextEditor.TextEditor.TextEditor;
45+
#stopSequences: string[];
4346
#renderingTimeout?: number;
4447

4548
readonly #sessionId: string = crypto.randomUUID();
4649
readonly #aidaClient: Host.AidaClient.AidaClient;
4750
readonly #serverSideLoggingEnabled: boolean;
4851

49-
constructor(opts: AgentOptions, editor: TextEditor.TextEditor.TextEditor) {
52+
constructor(opts: AgentOptions, editor: TextEditor.TextEditor.TextEditor, stopSequences?: string[]) {
5053
super();
5154
this.#aidaClient = opts.aidaClient;
5255
this.#serverSideLoggingEnabled = opts.serverSideLoggingEnabled ?? false;
5356
this.#editor = editor;
57+
this.#stopSequences = stopSequences ?? [];
5458
}
5559

5660
#debouncedRequestAidaSuggestion = Common.Debouncer.debounce(
@@ -75,7 +79,7 @@ export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTy
7579
inference_language: inferenceLanguage,
7680
temperature: validTemperature(this.#options.temperature),
7781
model_id: this.#options.modelId || undefined,
78-
stop_sequences: ['\n'], // We are prioritizing single line suggestions to reduce noise
82+
stop_sequences: this.#stopSequences,
7983
},
8084
metadata: {
8185
disable_user_content_logging: !(this.#serverSideLoggingEnabled ?? false),
@@ -92,6 +96,7 @@ export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTy
9296

9397
try {
9498
const response = await this.#aidaClient.completeCode(request);
99+
debugLog('At cursor position', cursor, {request, response});
95100
if (response && response.generatedSamples.length > 0 && response.generatedSamples[0].generationString) {
96101
if (response.generatedSamples[0].attributionMetadata?.attributionAction ===
97102
Host.AidaClient.RecitationAction.BLOCK) {
@@ -116,6 +121,7 @@ export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTy
116121
sampleId: response.generatedSamples[0].sampleId,
117122
})
118123
});
124+
debugLog('Suggestion dispatched to the editor', response.generatedSamples[0], 'at cursor position', cursor);
119125
if (response.metadata.rpcGlobalId) {
120126
const latency = performance.now() - startTime;
121127
this.#registerUserImpression(response.metadata.rpcGlobalId, response.generatedSamples[0].sampleId, latency);
@@ -167,6 +173,7 @@ export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTy
167173
},
168174
},
169175
});
176+
debugLog('Registered user impression with latency {seconds:', seconds, ', nanos:', nanos, '}');
170177
}
171178

172179
registerUserAcceptance(rpcGlobalId: Host.AidaClient.RpcGlobalId, sampleId: number): void {
@@ -181,6 +188,7 @@ export class AiCodeCompletion extends Common.ObjectWrapper.ObjectWrapper<EventTy
181188
},
182189
},
183190
});
191+
debugLog('Registered user acceptance');
184192
}
185193

186194
onTextChanged(

front_end/models/ai_code_completion/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import("../../../scripts/build/typescript/typescript.gni")
88
import("../visibility.gni")
99

1010
devtools_module("ai_code_completion") {
11-
sources = [ "AiCodeCompletion.ts" ]
11+
sources = [
12+
"AiCodeCompletion.ts",
13+
"debug.ts",
14+
]
1215

1316
deps = [
1417
"../../core/common:bundle",

front_end/models/ai_code_completion/ai_code_completion.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import * as AiCodeCompletion from './AiCodeCompletion.js';
6-
7-
export {
8-
AiCodeCompletion,
9-
};
5+
export * from './debug.js';
6+
export * as AiCodeCompletion from './AiCodeCompletion.js';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 The Chromium Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/**
6+
* @file Local debugging utilities.
7+
*/
8+
9+
export function isDebugMode(): boolean {
10+
return Boolean(localStorage.getItem('debugAiCodeCompletionEnabled'));
11+
}
12+
13+
export function debugLog(...log: unknown[]): void {
14+
if (!isDebugMode()) {
15+
return;
16+
}
17+
18+
// eslint-disable-next-line no-console
19+
console.log(...log);
20+
}
21+
22+
function setDebugAiCodeCompletionEnabled(enabled: boolean): void {
23+
if (enabled) {
24+
localStorage.setItem('debugAiCodeCompletionEnabled', 'true');
25+
} else {
26+
localStorage.removeItem('debugAiCodeCompletionEnabled');
27+
}
28+
}
29+
// @ts-expect-error
30+
globalThis.setDebugAiCodeCompletionEnabled = setDebugAiCodeCompletionEnabled;

front_end/panels/console/ConsolePrompt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,9 @@ export class ConsolePrompt extends Common.ObjectWrapper.eventMixin<EventTypes, t
511511
this.detachAiCodeCompletionTeaser();
512512
this.teaser = undefined;
513513
}
514+
// We are prioritizing single line suggestions in Console panel to reduce noise.
514515
this.aiCodeCompletion =
515-
new AiCodeCompletion.AiCodeCompletion.AiCodeCompletion({aidaClient: this.aidaClient}, this.editor);
516+
new AiCodeCompletion.AiCodeCompletion.AiCodeCompletion({aidaClient: this.aidaClient}, this.editor, ['\n']);
516517
this.aiCodeCompletion.addEventListener(AiCodeCompletion.AiCodeCompletion.Events.RESPONSE_RECEIVED, event => {
517518
this.aiCodeCompletionCitations = event.data.citations;
518519
this.dispatchEventToListeners(Events.AI_CODE_COMPLETION_RESPONSE_RECEIVED, event.data);

0 commit comments

Comments
 (0)