| 50 | /* In every page */ |
| 51 | |
| 52 | function pageProbe() { |
| 53 | try { localStorage.setItem('cbm.workspace.setup', 'done'); } catch { /* without storage it still works */ } |
| 54 | // Everything sent to the model worker, including workers created later. |
| 55 | window.__probeWorker = []; |
| 56 | window.__probeAnswers = []; |
| 57 | // Model workers created in this page: a project switch must not create another (K24). |
| 58 | window.__probeModelWorkers = 0; |
| 59 | // Every request of the page, with the address the page showed when it was sent (K24). |
| 60 | window.__probeFetches = []; |
| 61 | const nativeFetch = window.fetch.bind(window); |
| 62 | window.fetch = (input, init) => { |
| 63 | let entry; |
| 64 | try { |
| 65 | const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 66 | // A log post is kept whole enough to read its page and entries (K24). |
| 67 | const keep = /\/api\/ui-log/.test(url) ? 8000 : 600; |
| 68 | entry = { t: Math.round(performance.now()), url, body: typeof init?.body === 'string' ? init.body.slice(0, keep) : '', page: location.search }; |
| 69 | window.__probeFetches.push(entry); |
| 70 | } catch { /* observation only */ } |
| 71 | const answer = nativeFetch(input, init); |
| 72 | // When it ended and how: a request still open at a switch is either aborted or lands nowhere (K24). |
| 73 | if (entry) answer.then(() => { entry.done = Math.round(performance.now()); }, (error) => { entry.done = Math.round(performance.now()); entry.failed = String(error?.name ?? error); }); |
| 74 | return answer; |
| 75 | }; |
| 76 | window.__probePage = Math.random().toString(36).slice(2); |
| 77 | const NativeWorker = window.Worker; |
| 78 | window.Worker = class ProbeWorker extends NativeWorker { |
| 79 | constructor(url, options) { |
| 80 | super(url, options); |
| 81 | if (/browser-ai/.test(String(url))) window.__probeModelWorkers += 1; |
| 82 | // The model's raw answers, before the chat checks or shortens them. |
| 83 | this.addEventListener('message', (event) => { |
| 84 | if (event.data?.kind === 'answer') window.__probeAnswers.push({ t: Math.round(performance.now()), output: event.data.output, stopReason: event.data.stopReason }); |
| 85 | }); |
| 86 | } |
| 87 | postMessage(message, transfer) { |
| 88 | try { |
| 89 | if (message && typeof message === 'object' && typeof message.kind === 'string') { |
| 90 | window.__probeWorker.push({ t: Math.round(performance.now()), kind: message.kind, profile: message.generationProfile ?? '', |
| 91 | messages: Array.isArray(message.messages) ? message.messages : undefined }); |
| 92 | } |
| 93 | } catch { /* observation only */ } |
| 94 | return super.postMessage(message, transfer); |
| 95 | } |
| 96 | }; |
| 97 | window.__probeErrors = []; |
| 98 | window.addEventListener('error', (event) => window.__probeErrors.push(String(event.message))); |
| 99 | } |
| 100 | |
| 101 | /* ------------------------------------------------------------------ */ |
| 102 | /* Result */ |