// Native diagnostic trial: archive the current observation, then one input and immediate refresh. globalThis.recordDiagnosticTrial = async function(label) { if (!/^[a-z0-9-]+$/.test(label)) throw Error('Invalid evidence label'); const s = diagnosticState; if (!s) throw Error('No current native observation'); const n = String(++diagnosticSequence).padStart(3, '0'); const meta = {atUtc:new Date().toISOString(), label, window:s.window, accessibility:s.accessibility, screenshots:[]}; for (let i = 0; i < s.screenshots.length; i++) { const {url, ...info} = s.screenshots[i]; const filename = n + '-' + label + '-' + i + '.jpg'; await diagnosticFs.writeFile(diagnosticEvidence + '/' + filename, Buffer.from(url.split(',')[1], 'base64'), {flag:'wx'}); meta.screenshots.push({...info, file:filename}); } await diagnosticFs.writeFile(diagnosticEvidence + '/' + n + '-' + label + '.json', JSON.stringify(meta, null, 2) + '\n', {flag:'wx'}); }; globalThis.diagnosticAct = async function(kind, args, label, screenshotIndex = 0) { const allowed = ['click', 'scroll', 'press_key', 'type_text', 'drag']; if (!allowed.includes(kind)) throw Error('Unsupported trial action'); const s = diagnosticState; if (!s) throw Error('Reobserve before acting'); const input = {...args, window:s.window}; if (['click', 'scroll', 'drag'].includes(kind)) { if (!s.screenshots[screenshotIndex]) throw Error('Missing observed screenshot'); input.screenshotId = s.screenshots[screenshotIndex].id; } diagnosticState = null; await sky[kind](input); diagnosticState = await sky.get_window_state({window:s.window, include_screenshot:true, include_text:true}); if (label) await recordDiagnosticTrial(label); nodeRepl.write(String(diagnosticState.accessibility?.tree || diagnosticState.accessibility?.document_text || '')); };