Skip to content

Commit a7919c1

Browse files
committed
feat(ai): wire console-error capture from injected script to AI Assistant
End-to-end wiring for the recent-console-errors signal: - The injected script installs consoleErrorCapture on load and pushes a fresh snapshot to the panel via the existing devtools port protocol ('on-console-errors-updated') on every recorded event. - Panel main.js caches the snapshot in frameData[frameId].consoleErrors and wires getConsoleErrors / clearConsoleErrors to the AI Assistant. - clearConsoleErrors sends 'do-clear-console-errors' to the injected script so the panel's cache and the page-side buffer stay in lock-step when the developer invokes Clear Conversation. - AIChat forwards the two new callbacks into AssistantController. Closes .scratch/ai-prompt-context-quality/issues/03.
1 parent d9edad7 commit a7919c1

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

app/scripts/devtools/panel/ui5/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,26 @@
433433
getAppInfo: function () {
434434
var currentFrameId = framesSelect.getSelectedId();
435435
return frameData[currentFrameId] ? frameData[currentFrameId].applicationInformation : null;
436+
},
437+
getConsoleErrors: function () {
438+
var currentFrameId = framesSelect.getSelectedId();
439+
return frameData[currentFrameId] && frameData[currentFrameId].consoleErrors ?
440+
frameData[currentFrameId].consoleErrors : [];
441+
},
442+
clearConsoleErrors: function () {
443+
var currentFrameId = framesSelect.getSelectedId();
444+
if (currentFrameId === undefined || currentFrameId === null) {
445+
return;
446+
}
447+
if (frameData[currentFrameId]) {
448+
frameData[currentFrameId].consoleErrors = [];
449+
}
450+
// Also tell the injected script to clear its live buffer so the panel's cache and
451+
// the page-side buffer stay in lock-step.
452+
port.postMessage({
453+
action: 'do-clear-console-errors',
454+
frameId: currentFrameId
455+
});
436456
}
437457
});
438458

@@ -692,6 +712,20 @@
692712
if (bFrameUpdate) {
693713
framesSelect.setData(frameData);
694714
}
715+
},
716+
717+
/**
718+
* Store the recent-console-errors snapshot the injected script pushes on every error
719+
* event. The AI Assistant reads this cache via its `getConsoleErrors` seam on each
720+
* `sendUserMessage`.
721+
* @param {Object} message
722+
*/
723+
'on-console-errors-updated': function (message, messageSender) {
724+
var frameId = messageSender.frameId;
725+
if (!frameData[frameId]) {
726+
return;
727+
}
728+
frameData[frameId].consoleErrors = message.consoleErrors || [];
695729
}
696730
};
697731

app/scripts/injected/main.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ sap.ui.require(['ToolsAPI'], function (ToolsAPI) {
66
var controlUtils = require('../modules/injected/controlUtils.js');
77
var rightClickHandler = require('../modules/injected/rightClickHandler.js');
88
var applicationUtils = require('../modules/injected/applicationUtils');
9+
var consoleErrorCapture = require('../modules/injected/consoleErrorCapture.js');
910

1011
var ui5TempName = 'ui5$temp';
1112
var ui5Temp = window[ui5TempName] = {}; // Container for all temp. variables
@@ -16,6 +17,20 @@ sap.ui.require(['ToolsAPI'], function (ToolsAPI) {
1617
// Create global reference for the extension.
1718
ui5inspector.createReferences();
1819

20+
// Install the recent-console-errors capture. The buffer feeds the AI Assistant's per-turn
21+
// Recent Console Errors section — a bounded FIFO of the three most-recent, deduplicated
22+
// console errors and warnings from the inspected page. The `onRecord` callback pushes the
23+
// current snapshot to the panel on every event so the panel-side cache stays fresh; the
24+
// AssistantController's `getConsoleErrors` seam then reads from that cache per send.
25+
var consoleErrorHandle = consoleErrorCapture.install(window, {
26+
onRecord: function () {
27+
message.send({
28+
action: 'on-console-errors-updated',
29+
consoleErrors: consoleErrorHandle.buffer.snapshot()
30+
});
31+
}
32+
});
33+
1934
/**
2035
* Mutation observer for DOM elements
2136
* @type {{init: Function, _observer: MutationObserver, _options: {subtree: boolean, childList: boolean, attributes: boolean}}}
@@ -365,6 +380,17 @@ sap.ui.require(['ToolsAPI'], function (ToolsAPI) {
365380
} else {
366381
log(`No Control with id ${sControlId} exists`);
367382
}
383+
},
384+
385+
/**
386+
* Clear the recent-console-errors buffer. Invoked by the panel's Clear Conversation flow.
387+
*/
388+
'do-clear-console-errors': function () {
389+
consoleErrorHandle.buffer.clear();
390+
message.send({
391+
action: 'on-console-errors-updated',
392+
consoleErrors: []
393+
});
368394
}
369395
};
370396

app/scripts/modules/ui/AIChat.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const AssistantTranscript = require('../ai/AssistantTranscript.js');
1515
* @param {string} containerId
1616
* @param {Object} [options]
1717
* @param {Function} [options.getAppInfo] - Returns the UI5 metadata snapshot for PromptBuilder.
18+
* @param {Function} [options.getConsoleErrors] - Returns the recent-console-errors snapshot
19+
* captured from the inspected page. Forwarded to the controller so it reaches
20+
* {@link PromptBuilder#buildUserPrompt} on each send.
21+
* @param {Function} [options.clearConsoleErrors] - Clears the recent-console-errors buffer for the
22+
* current URL. Called by the controller on Clear Conversation and URL change.
1823
* @param {AssistantController} [options.controller] - Pre-built controller for tests. Defaults to a
1924
* fresh AssistantController.
2025
* @param {Function} [options.transcriptFactory] - Test seam: `(container, options) => AssistantTranscript`.
@@ -24,6 +29,8 @@ const AssistantTranscript = require('../ai/AssistantTranscript.js');
2429
*/
2530
function AIChat(containerId, {
2631
getAppInfo = null,
32+
getConsoleErrors = null,
33+
clearConsoleErrors = null,
2734
controller = null,
2835
transcriptFactory = function (host, options) {
2936
return new AssistantTranscript(host, options);
@@ -32,8 +39,12 @@ function AIChat(containerId, {
3239
this._container = document.getElementById(containerId);
3340

3441
this._getAppInfo = getAppInfo;
42+
this._getConsoleErrors = getConsoleErrors;
43+
this._clearConsoleErrors = clearConsoleErrors;
3544
this._controller = controller || new AssistantController({
36-
getAppInfo: this._getAppInfo || function () { return null; }
45+
getAppInfo: this._getAppInfo || function () { return null; },
46+
getConsoleErrors: this._getConsoleErrors || function () { return []; },
47+
clearConsoleErrors: this._clearConsoleErrors || function () {}
3748
});
3849
this._transcriptFactory = transcriptFactory;
3950

0 commit comments

Comments
 (0)