@snap/valdi 1.1.7 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/.metadata/.bazelrc.template +9 -7
  2. package/.metadata/AGENTS.md.template +7 -0
  3. package/.metadata/MODULE.bazel.template +47 -3
  4. package/.metadata/README.md.template +2 -2
  5. package/.metadata/bzl/patches/rules_python/1.9.0/BUILD.bazel.template +1 -0
  6. package/.metadata/bzl/patches/rules_python/1.9.0/remove_setuptools_spaces.patch.template +96 -0
  7. package/.metadata/skill-tests-README.md.template +1 -1
  8. package/README.md +47 -14
  9. package/bundled-skills/registry.json +2 -1
  10. package/bundled-skills/skills/valdi-animations/skill.md +49 -0
  11. package/bundled-skills/skills/valdi-async/skill.md +3 -2
  12. package/bundled-skills/skills/valdi-async/tests/README.md +1 -1
  13. package/bundled-skills/skills/valdi-bazel/skill.md +2 -6
  14. package/bundled-skills/skills/valdi-component-tests/tests/README.md +1 -1
  15. package/bundled-skills/skills/valdi-cpp-runtime/skill.md +18 -0
  16. package/bundled-skills/skills/valdi-custom-view/skill.md +1 -1
  17. package/bundled-skills/skills/valdi-error-handling/skill.md +1 -1
  18. package/bundled-skills/skills/valdi-gestures/skill.md +3 -3
  19. package/bundled-skills/skills/valdi-migrate/skill.md +2 -1
  20. package/bundled-skills/skills/valdi-migrate/tests/README.md +2 -2
  21. package/bundled-skills/skills/valdi-mock-breakdown/skill.md +4 -4
  22. package/bundled-skills/skills/valdi-mock-to-module/skill.md +9 -6
  23. package/bundled-skills/skills/valdi-overview/skill.md +2 -1
  24. package/bundled-skills/skills/valdi-perf/skill.md +121 -5
  25. package/bundled-skills/skills/valdi-perf/tests/README.md +1 -1
  26. package/bundled-skills/skills/valdi-pr-review/skill.md +189 -0
  27. package/bundled-skills/skills/valdi-profile/skill.md +5 -3
  28. package/bundled-skills/skills/valdi-setup/skill.md +3 -6
  29. package/bundled-skills/skills/valdi-testing/skill.md +1 -1
  30. package/bundled-skills/skills/valdi-tsx/skill.md +47 -11
  31. package/bundled-skills/skills/valdi-tsx/tests/README.md +1 -1
  32. package/debugger/README.md +356 -0
  33. package/debugger/debugger-actions.js +373 -0
  34. package/debugger/debugger-api.js +225 -0
  35. package/debugger/debugger-bootstrap.js +295 -0
  36. package/debugger/debugger-model.js +790 -0
  37. package/debugger/debugger-performance.js +573 -0
  38. package/debugger/debugger-preview-html.js +676 -0
  39. package/debugger/debugger-providers.js +389 -0
  40. package/debugger/debugger-render.js +529 -0
  41. package/debugger/debugger-runtime.js +387 -0
  42. package/debugger/debugger-session.js +188 -0
  43. package/debugger/debugger-settings.js +209 -0
  44. package/debugger/debugger-state.js +221 -0
  45. package/debugger/debugger-tree-model.js +539 -0
  46. package/debugger/debugger.css +1865 -0
  47. package/debugger/devtools-panel.css +1318 -0
  48. package/debugger/devtools-panel.html +273 -0
  49. package/debugger/devtools-panel.js +3329 -0
  50. package/debugger/index.html +279 -0
  51. package/dist/commands/bootstrap.js +7 -6
  52. package/dist/commands/build.js +7 -1
  53. package/dist/commands/debugger.js +163 -0
  54. package/dist/commands/doctor.js +3 -62
  55. package/dist/commands/export.js +47 -16
  56. package/dist/commands/hotreload.js +247 -27
  57. package/dist/commands/inspect.js +5 -2
  58. package/dist/commands/inspect_commands/input.js +176 -0
  59. package/dist/commands/inspect_commands/snapshot.js +1 -1
  60. package/dist/commands/inspect_commands/tree.js +1 -1
  61. package/dist/commands/install.js +79 -2
  62. package/dist/commands/projectsync.js +15 -0
  63. package/dist/commands/webHotreload.js +31 -0
  64. package/dist/core/constants.js +34 -7
  65. package/dist/debugger/chromiumConsole.js +279 -0
  66. package/dist/debugger/inputClient.js +473 -0
  67. package/dist/debugger/server.js +3801 -0
  68. package/dist/debugger/targetRegistry.js +436 -0
  69. package/dist/debugger/webPreviewPerformance.js +864 -0
  70. package/dist/index.js +0 -0
  71. package/dist/utils/BazelClient.js +34 -5
  72. package/dist/utils/applicationUtils.js +20 -2
  73. package/dist/utils/buildInfo.js +5 -1
  74. package/dist/utils/chromiumDevToolsClient.js +350 -0
  75. package/dist/utils/cliUtils.js +2 -2
  76. package/dist/utils/daemonClient.js +364 -44
  77. package/dist/utils/directorySync.js +153 -0
  78. package/dist/utils/githubUtils.js +1 -1
  79. package/dist/utils/hermesClient.js +82 -176
  80. package/dist/utils/loopbackHost.js +14 -0
  81. package/dist/utils/owlCdpClient.js +282 -0
  82. package/dist/utils/owlDevToolsExtension.js +142 -0
  83. package/dist/utils/processUtils.js +14 -0
  84. package/dist/utils/stringUtils.js +13 -1
  85. package/dist/utils/webApplicationSession.js +58 -0
  86. package/dist/utils/webHotReloadSession.js +212 -0
  87. package/dist/utils/webServerUtils.js +195 -0
  88. package/dist/utils/zipUtils.js +20 -2
  89. package/package.json +4 -2
  90. package/dist/commands/inspect_commands/snapshot.spec.js +0 -105
  91. package/dist/core/packageFiles.spec.js +0 -29
  92. package/dist/preview/previewGenerator.spec.js +0 -203
  93. package/dist/preview/viewModelParser.spec.js +0 -171
  94. package/dist/utils/linuxDistro.spec.js +0 -122
  95. package/dist/utils/skillsAdapters.spec.js +0 -141
  96. package/dist/utils/stringUtils.spec.js +0 -81
@@ -0,0 +1,373 @@
1
+ // Section switching, auto-refresh, action dispatch, and console commands.
2
+ function normalizeSection(section) {
3
+ const normalized = String(section || '')
4
+ .trim()
5
+ .toLowerCase();
6
+ if (normalized === 'logger' || normalized === 'loger') return 'logs';
7
+ return normalized;
8
+ }
9
+
10
+ function setActiveSection(section) {
11
+ const normalized = normalizeSection(section);
12
+ if (!elements.sectionPanels.some(panel => panel.dataset.sectionPanel === normalized)) {
13
+ addLog('warn', 'debugger', `Unknown debugger section: ${section}.`);
14
+ return;
15
+ }
16
+
17
+ state.activeSection = normalized;
18
+ elements.sectionButtons.forEach(button => {
19
+ button.classList.toggle('active', button.dataset.sectionButton === normalized);
20
+ });
21
+ elements.sectionPanels.forEach(panel => {
22
+ panel.classList.toggle('active', panel.dataset.sectionPanel === normalized);
23
+ });
24
+ if (normalized === 'logs') {
25
+ renderLogs();
26
+ startRuntimeLogStream({ silent: true });
27
+ } else {
28
+ stopRuntimeLogStream();
29
+ }
30
+ if (normalized === UI_SECTION && state.autoRefresh && state.source === 'daemon' && isDebuggerAttached()) {
31
+ window.setTimeout(() => refreshLiveSnapshot(), 0);
32
+ }
33
+ maybeRefreshDebuggerTools(normalized);
34
+ }
35
+
36
+ function shouldAutoRefresh() {
37
+ return (
38
+ !document.hidden &&
39
+ !state.performance.traceActive &&
40
+ !state.performance.traceCapturePending &&
41
+ !state.performance.traceResultPending &&
42
+ !state.performance.traceStateUnknown &&
43
+ !state.performance.profileActive
44
+ );
45
+ }
46
+
47
+ function shouldAutoRefreshLiveSnapshot() {
48
+ return (
49
+ shouldAutoRefresh() && state.activeSection === UI_SECTION && !document.querySelector('.performance-panel:hover')
50
+ );
51
+ }
52
+
53
+ function autoRefreshLiveDebugger() {
54
+ if (!shouldAutoRefresh()) {
55
+ return;
56
+ }
57
+
58
+ if (state.followLatestTarget && !state.manualDetach) {
59
+ void refreshTargets({
60
+ silent: true,
61
+ autoAttach: state.activeSection === UI_SECTION,
62
+ skipSnapshotIfSame: state.activeSection !== UI_SECTION,
63
+ queueIfBusy: true,
64
+ });
65
+ return;
66
+ }
67
+
68
+ if (state.source !== 'daemon' || !isDebuggerAttached()) {
69
+ return;
70
+ }
71
+
72
+ if (shouldAutoRefreshLiveSnapshot()) {
73
+ void loadRealSnapshot(null, { silent: true, preserveSelection: true, queueIfBusy: true });
74
+ }
75
+ }
76
+
77
+ function setAutoRefresh(enabled, options = {}) {
78
+ state.autoRefresh = enabled;
79
+ if (state.autoRefreshTimer) {
80
+ clearInterval(state.autoRefreshTimer);
81
+ state.autoRefreshTimer = null;
82
+ }
83
+
84
+ if (enabled) {
85
+ state.autoRefreshTimer = window.setInterval(() => {
86
+ autoRefreshLiveDebugger();
87
+ }, AUTO_REFRESH_INTERVAL_MS);
88
+ if (!options.silent) addLog('info', 'debugger', 'Auto-refresh enabled.');
89
+ } else {
90
+ if (!options.silent) addLog('info', 'debugger', 'Auto-refresh disabled.');
91
+ }
92
+ renderHeader();
93
+ }
94
+
95
+ function refreshLiveSnapshot() {
96
+ const target = state.snapshot.target || {};
97
+ if (state.followLatestTarget) {
98
+ state.manualDetach = false;
99
+ refreshTargets();
100
+ return;
101
+ }
102
+ if (state.source === 'daemon' || (target.clientId && target.contextId)) {
103
+ state.manualDetach = false;
104
+ loadRealSnapshot(null, { preserveSelection: true, queueIfBusy: true });
105
+ } else {
106
+ addLog('info', 'daemon', 'No live Valdi daemon target is selected yet. Refreshing targets first.');
107
+ refreshTargets();
108
+ }
109
+ }
110
+
111
+ function scrollSelectedTreeNodeIntoView() {
112
+ const selectedRow = Array.from(elements.tree.querySelectorAll('.tree-node')).find(
113
+ row => row.dataset.nodeId === state.selectedNodeId,
114
+ );
115
+ selectedRow?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
116
+ }
117
+
118
+ function selectNode(id, options = {}) {
119
+ const node = findNode(id);
120
+ if (!node) {
121
+ addLog('warn', 'console', `No node found for id ${id}.`);
122
+ return;
123
+ }
124
+ state.selectedNodeId = getNodeId(node);
125
+ state.selectedSnapshotImage = null;
126
+ expandPathToNode(state.selectedNodeId);
127
+ renderTree();
128
+ if (options.scrollTree !== false) {
129
+ window.requestAnimationFrame(scrollSelectedTreeNodeIntoView);
130
+ }
131
+ renderOverlay();
132
+ renderInspector();
133
+ }
134
+
135
+ function setActiveTab(tab) {
136
+ if (!['overview', 'props', 'state', 'issues', 'raw'].includes(tab)) {
137
+ addLog('warn', 'debugger', `Unknown inspector tab: ${tab}.`);
138
+ return;
139
+ }
140
+ state.activeTab = tab;
141
+ document
142
+ .querySelectorAll('.tabs button')
143
+ .forEach(button => button.classList.toggle('active', button.dataset.tab === tab));
144
+ renderInspector();
145
+ }
146
+
147
+ function attachDebuggerView() {
148
+ state.manualDetach = false;
149
+ state.followLatestTarget = true;
150
+ refreshTargets();
151
+ }
152
+
153
+ function detachDebuggerView() {
154
+ state.attached = false;
155
+ state.manualDetach = true;
156
+ state.rootSnapshotImage = null;
157
+ state.rootSnapshotRequestId++;
158
+ resetDebuggerToolsForTarget();
159
+ stopRuntimeLogStream();
160
+ addLog('warn', 'proxy', 'Detached debugger view from live daemon data.');
161
+ render();
162
+ }
163
+
164
+ async function setTargetPort(port, nextTarget) {
165
+ if (!(await preparePerformanceTraceTargetSwitch(nextTarget))) return;
166
+ elements.portSelect.value = String(port);
167
+ state.snapshot.target = {
168
+ ...state.snapshot.target,
169
+ proxyPort: port,
170
+ port,
171
+ };
172
+ state.attached = false;
173
+ state.performance.traceSupported = true;
174
+ state.manualDetach = false;
175
+ state.followLatestTarget = true;
176
+ state.rootSnapshotImage = null;
177
+ state.rootSnapshotRequestId++;
178
+ resetDebuggerToolsForTarget();
179
+ stopRuntimeLogStream();
180
+ render();
181
+ }
182
+
183
+ function clearDebuggerLogs() {
184
+ state.snapshot.logs = [];
185
+ renderLogs();
186
+ }
187
+
188
+ function applyDebuggerAction(action, params = {}, result) {
189
+ if (action === 'selectNode') {
190
+ const nodeId = actionString(params, 'id', 'nodeId');
191
+ if (nodeId) selectNode(nodeId);
192
+ return;
193
+ }
194
+
195
+ if (action === 'selectTarget') {
196
+ const targetId = actionString(params, 'id', 'targetId');
197
+ if (targetId) selectTarget(targetId);
198
+ return;
199
+ }
200
+
201
+ if (action === 'setActiveTab') {
202
+ setActiveTab(actionString(params, 'tab'));
203
+ return;
204
+ }
205
+
206
+ if (action === 'setActiveSection') {
207
+ setActiveSection(actionString(params, 'section'));
208
+ return;
209
+ }
210
+
211
+ if (action === 'setOverlayMode') {
212
+ setOverlayMode(actionString(params, 'mode'));
213
+ return;
214
+ }
215
+
216
+ if (action === 'setAutoRefresh') {
217
+ setAutoRefresh(actionBoolean(params, 'enabled', state.autoRefresh));
218
+ return;
219
+ }
220
+
221
+ if (action === 'setPort') {
222
+ const port = actionNumber(params, 'port');
223
+ if (port !== null) void setTargetPort(port, { port });
224
+ return;
225
+ }
226
+
227
+ if (action === 'attach') {
228
+ attachDebuggerView();
229
+ return;
230
+ }
231
+
232
+ if (action === 'detach') {
233
+ detachDebuggerView();
234
+ return;
235
+ }
236
+
237
+ if (action === 'refreshTargets') {
238
+ refreshTargets();
239
+ return;
240
+ }
241
+
242
+ if (action === 'refreshSnapshot') {
243
+ refreshLiveSnapshot();
244
+ return;
245
+ }
246
+
247
+ if (action === 'clearLogs') {
248
+ clearDebuggerLogs();
249
+ return;
250
+ }
251
+
252
+ if (action === 'captureElementSnapshot') {
253
+ void captureSelectedElementSnapshot();
254
+ return;
255
+ }
256
+
257
+ if (action === 'dumpHeap') {
258
+ void dumpHeap();
259
+ return;
260
+ }
261
+
262
+ if (action === 'startRendererTrace') {
263
+ void startPerformanceTrace();
264
+ return;
265
+ }
266
+
267
+ if (action === 'stopRendererTrace') {
268
+ void stopPerformanceTrace({});
269
+ return;
270
+ }
271
+
272
+ if (action === 'captureRendererTrace') {
273
+ void capturePerformanceTrace();
274
+ return;
275
+ }
276
+
277
+ if (action === 'refreshHermesContexts') {
278
+ void refreshProfileContexts({ silent: false });
279
+ return;
280
+ }
281
+
282
+ if (action === 'startCpuProfile') {
283
+ void startCpuProfile();
284
+ return;
285
+ }
286
+
287
+ if (action === 'stopCpuProfile') {
288
+ void stopCpuProfile();
289
+ return;
290
+ }
291
+
292
+ if (action === 'captureCpuProfile') {
293
+ void captureCpuProfile();
294
+ return;
295
+ }
296
+
297
+ if (action === 'refreshDebuggerProviders') {
298
+ if (result) applyDebuggerProvidersResult(result, params);
299
+ else void refreshDebuggerProviders();
300
+ return;
301
+ }
302
+
303
+ if (action === 'refreshDebugSettings') {
304
+ if (result) applyDebugSettingsResult(result, params);
305
+ else void refreshDebugSettings();
306
+ return;
307
+ }
308
+
309
+ if (action === 'setDebugSetting' || action === 'resetDebugSetting') {
310
+ if (result) applyDebugSettingsResult(result, params);
311
+ else void refreshDebugSettings({ silent: true });
312
+ return;
313
+ }
314
+
315
+ addLog('warn', 'debugger', `Unknown debugger action: ${action}.`);
316
+ }
317
+
318
+ function runCommand(rawCommand) {
319
+ const command = rawCommand.trim();
320
+ if (!command) return;
321
+ addLog('info', 'console', `> ${command}`);
322
+
323
+ if (command === 'help') {
324
+ addLog(
325
+ 'info',
326
+ 'console',
327
+ 'Commands: help, section <ui|performance|data|settings|logs>, data, settings, path, issues, select <id>, connect, refresh, reload, status, snapshot, heap, trace, profile, auto on, auto off, clear.',
328
+ );
329
+ } else if (command.startsWith('section ')) {
330
+ void requestDebuggerAction('setActiveSection', { section: command.slice('section '.length).trim() });
331
+ } else if (command === 'data') {
332
+ void requestDebuggerAction('setActiveSection', { section: 'data' });
333
+ void requestDebuggerAction('refreshDebuggerProviders', getSelectedTargetParams());
334
+ } else if (command === 'settings') {
335
+ void requestDebuggerAction('setActiveSection', { section: 'settings' });
336
+ void requestDebuggerAction('refreshDebugSettings', getSelectedTargetParams());
337
+ } else if (command === 'path') {
338
+ const path = getPathToNode(state.selectedNodeId)
339
+ .map(node => `${node.tag}#${getNodeId(node)}`)
340
+ .join(' > ');
341
+ addLog('info', 'inspector', path || 'No node selected.');
342
+ } else if (command === 'issues') {
343
+ addLog(
344
+ 'info',
345
+ 'inspector',
346
+ `${state.snapshot.issues.length} issue(s): ${state.snapshot.issues.map(issue => issue.title).join(', ')}`,
347
+ );
348
+ } else if (command.startsWith('select ')) {
349
+ void requestDebuggerAction('selectNode', { id: command.slice('select '.length).trim() });
350
+ } else if (command === 'connect') {
351
+ void requestDebuggerAction('attach');
352
+ } else if (command === 'status') {
353
+ void requestDebuggerAction('refreshTargets');
354
+ } else if (command === 'refresh' || command === 'reload') {
355
+ void requestDebuggerAction('refreshSnapshot');
356
+ } else if (command === 'snapshot') {
357
+ void requestDebuggerAction('captureElementSnapshot');
358
+ } else if (command === 'heap') {
359
+ void requestDebuggerAction('dumpHeap');
360
+ } else if (command === 'trace') {
361
+ void requestDebuggerAction('captureRendererTrace');
362
+ } else if (command === 'profile') {
363
+ void requestDebuggerAction('captureCpuProfile');
364
+ } else if (command === 'auto on') {
365
+ void requestDebuggerAction('setAutoRefresh', { enabled: true });
366
+ } else if (command === 'auto off') {
367
+ void requestDebuggerAction('setAutoRefresh', { enabled: false });
368
+ } else if (command === 'clear') {
369
+ void requestDebuggerAction('clearLogs');
370
+ } else {
371
+ addLog('warn', 'console', `Unknown command: ${command}. Try help.`);
372
+ }
373
+ }
@@ -0,0 +1,225 @@
1
+ // HTTP helpers and the external debugger action/event bridge.
2
+ const DEBUGGER_API_TOKEN_HEADER = 'X-Valdi-Debugger-Token';
3
+ const DEBUGGER_API_TOKEN_QUERY_PARAMETER = 'valdiDebuggerToken';
4
+ const debuggerApiToken = document.querySelector('meta[name="valdi-debugger-api-token"]')?.content || '';
5
+
6
+ function debuggerApiHeaders(headers) {
7
+ return {
8
+ ...headers,
9
+ [DEBUGGER_API_TOKEN_HEADER]: debuggerApiToken,
10
+ };
11
+ }
12
+
13
+ function debuggerEventSourceUrl(path) {
14
+ const url = new URL(path, window.location.origin);
15
+ url.searchParams.set(DEBUGGER_API_TOKEN_QUERY_PARAMETER, debuggerApiToken);
16
+ return url.toString();
17
+ }
18
+
19
+ async function apiGet(path, params = {}, options = {}) {
20
+ const url = new URL(path, window.location.origin);
21
+ for (const [key, value] of Object.entries(params)) {
22
+ if (value !== undefined && value !== null && value !== '') {
23
+ url.searchParams.set(key, String(value));
24
+ }
25
+ }
26
+
27
+ const controller = new AbortController();
28
+ const timeout = window.setTimeout(() => controller.abort(), options.timeoutMs || 8000);
29
+ try {
30
+ const response = await fetch(url, {
31
+ cache: 'no-store',
32
+ signal: controller.signal,
33
+ headers: debuggerApiHeaders({}),
34
+ });
35
+ const data = await response.json();
36
+ if (!response.ok) {
37
+ throw new Error(data.error || `Request failed: ${response.status}`);
38
+ }
39
+ return data;
40
+ } catch (error) {
41
+ if (error.name === 'AbortError') {
42
+ throw new Error(`Timed out fetching ${url.pathname}.`);
43
+ }
44
+ throw error;
45
+ } finally {
46
+ window.clearTimeout(timeout);
47
+ }
48
+ }
49
+
50
+ async function apiPost(path, params = {}, body = {}, options = {}) {
51
+ const url = new URL(path, window.location.origin);
52
+ for (const [key, value] of Object.entries(params)) {
53
+ if (value !== undefined && value !== null && value !== '') {
54
+ url.searchParams.set(key, String(value));
55
+ }
56
+ }
57
+
58
+ const controller = new AbortController();
59
+ const timeout = window.setTimeout(() => controller.abort(), options.timeoutMs || 8000);
60
+ try {
61
+ const response = await fetch(url, {
62
+ method: 'POST',
63
+ cache: 'no-store',
64
+ signal: controller.signal,
65
+ headers: debuggerApiHeaders({ 'Content-Type': 'application/json' }),
66
+ body: JSON.stringify(body),
67
+ });
68
+ const data = await response.json();
69
+ if (!response.ok) {
70
+ throw new Error(data.error || `Request failed: ${response.status}`);
71
+ }
72
+ return data;
73
+ } catch (error) {
74
+ if (error.name === 'AbortError') {
75
+ throw new Error(`Timed out posting ${url.pathname}.`);
76
+ }
77
+ throw error;
78
+ } finally {
79
+ window.clearTimeout(timeout);
80
+ }
81
+ }
82
+
83
+ function startDebuggerDevReload() {
84
+ if (!window.EventSource || window.location.search.includes('noDevReload=1')) return;
85
+ const source = new EventSource(debuggerEventSourceUrl('/api/dev-events'));
86
+ let loadedRevision = null;
87
+
88
+ source.addEventListener('ready', event => {
89
+ const payload = JSON.parse(event.data);
90
+ loadedRevision = payload.revision;
91
+ });
92
+
93
+ source.addEventListener('reload', event => {
94
+ const payload = JSON.parse(event.data);
95
+ if (loadedRevision === null) {
96
+ loadedRevision = payload.revision;
97
+ return;
98
+ }
99
+ if (payload.revision > loadedRevision) {
100
+ persistDebuggerSessionState();
101
+ window.location.reload();
102
+ }
103
+ });
104
+ }
105
+
106
+ function startDebuggerActionStream() {
107
+ if (!window.EventSource) return;
108
+ const source = new EventSource(debuggerEventSourceUrl('/api/debugger/events'));
109
+ state.debuggerEventStream = source;
110
+
111
+ source.addEventListener('ready', event => {
112
+ state.debuggerEventsConnected = true;
113
+ applyDebuggerStatePayload(JSON.parse(event.data));
114
+ });
115
+
116
+ source.addEventListener('debugger-action', event => {
117
+ state.debuggerEventsConnected = true;
118
+ applyDebuggerActionPayload(JSON.parse(event.data));
119
+ });
120
+
121
+ source.addEventListener('error', () => {
122
+ state.debuggerEventsConnected = false;
123
+ });
124
+ }
125
+
126
+ function applyDebuggerStatePayload(payload) {
127
+ const debuggerState = payload?.state;
128
+ if (!debuggerState) return;
129
+ const revision = Number(debuggerState.revision) || 0;
130
+ if (revision && revision <= state.lastDebuggerRevision) return;
131
+ syncDebuggerSessionState(debuggerState);
132
+
133
+ if (revision) state.lastDebuggerRevision = revision;
134
+ }
135
+
136
+ function syncDebuggerSessionState(debuggerState) {
137
+ if (debuggerState.activeSection && debuggerState.activeSection !== state.activeSection) {
138
+ setActiveSection(debuggerState.activeSection);
139
+ }
140
+
141
+ if (debuggerState.overlayMode && debuggerState.overlayMode !== state.overlayMode) {
142
+ setOverlayMode(debuggerState.overlayMode);
143
+ }
144
+
145
+ if (debuggerState.activeTab && debuggerState.activeTab !== state.activeTab) {
146
+ setActiveTab(debuggerState.activeTab);
147
+ }
148
+
149
+ if (typeof debuggerState.autoRefresh === 'boolean' && debuggerState.autoRefresh !== state.autoRefresh) {
150
+ setAutoRefresh(debuggerState.autoRefresh, { silent: true });
151
+ }
152
+
153
+ if (
154
+ debuggerState.selectedNodeId &&
155
+ debuggerState.selectedNodeId !== state.selectedNodeId &&
156
+ findNode(debuggerState.selectedNodeId)
157
+ ) {
158
+ selectNode(debuggerState.selectedNodeId, { scrollTree: false });
159
+ }
160
+ }
161
+
162
+ function applyDebuggerActionPayload(payload) {
163
+ const revision = Number(payload?.state?.revision) || 0;
164
+ if (revision && revision <= state.lastDebuggerRevision) return;
165
+ if (payload?.action) {
166
+ applyDebuggerAction(payload.action, payload.params || {}, payload.result);
167
+ }
168
+ if (revision) state.lastDebuggerRevision = revision;
169
+ }
170
+
171
+ async function requestDebuggerAction(action, params = {}) {
172
+ const runtimeActions = new Set([
173
+ 'refreshDebuggerProviders',
174
+ 'refreshDebugSettings',
175
+ 'setDebugSetting',
176
+ 'resetDebugSetting',
177
+ ]);
178
+ const shouldApplyLocally = !state.debuggerEventsConnected && !runtimeActions.has(action);
179
+ if (shouldApplyLocally) {
180
+ applyDebuggerAction(action, params);
181
+ }
182
+
183
+ try {
184
+ const response = await apiPost(
185
+ '/api/debugger/actions',
186
+ {},
187
+ {
188
+ action,
189
+ params,
190
+ source: 'ui',
191
+ },
192
+ { timeoutMs: 5000 },
193
+ );
194
+ const revision = Number(response?.state?.revision) || 0;
195
+ if (response?.result && (!revision || revision > state.lastDebuggerRevision)) {
196
+ applyDebuggerAction(action, params, response.result);
197
+ if (revision) state.lastDebuggerRevision = revision;
198
+ }
199
+ return response;
200
+ } catch (error) {
201
+ if (!shouldApplyLocally) {
202
+ applyDebuggerAction(action, params);
203
+ }
204
+ addLog('warn', 'debugger', `Debugger action bus unavailable; applied ${action} locally. ${error.message}`);
205
+ return null;
206
+ }
207
+ }
208
+
209
+ function getSelectedTargetParams() {
210
+ const target = state.snapshot.target || {};
211
+ return {
212
+ port: target.proxyPort || target.port || selectedDaemonPort(),
213
+ clientId: target.clientId,
214
+ contextId: target.contextId,
215
+ };
216
+ }
217
+
218
+ function hasSelectedLiveTarget() {
219
+ const params = getSelectedTargetParams();
220
+ return Boolean(params.clientId && params.contextId && Number.isFinite(params.port));
221
+ }
222
+
223
+ function isDebuggerAttached() {
224
+ return state.attached || (state.source === 'daemon' && !state.manualDetach && hasSelectedLiveTarget());
225
+ }