|
14 | 14 | return node; |
15 | 15 | }; |
16 | 16 | const number = value => Number(value).toLocaleString(); |
| 17 | + // A picture of the conversation over the page: one dialog, made at the first, closed by Close, Esc or a click |
| 18 | + // outside the picture (on the backdrop, which is the dialog itself) |
| 19 | + let viewer = null; |
| 20 | + const viewPicture = (url, alt) => { |
| 21 | + if (!viewer) { |
| 22 | + viewer = el('dialog', 'picture-view'); viewer.setAttribute('aria-label', 'Picture'); |
| 23 | + const box = el('div', 'picture-view-box'); |
| 24 | + box.append(button('Close', () => viewer.close(), false, 'btn sm picture-view-close'), el('img', 'picture-view-img')); |
| 25 | + viewer.append(box); |
| 26 | + viewer.addEventListener('click', event => { if (event.target === viewer) viewer.close(); }); |
| 27 | + document.body.append(viewer); |
| 28 | + } |
| 29 | + const img = viewer.querySelector('img'); img.src = url; img.alt = alt; |
| 30 | + viewer.showModal(); |
| 31 | + }; |
17 | 32 | const readJSON = async (url, signal) => { |
18 | 33 | const response = await nulliusFetch(url, {signal, headers: {'Accept': 'application/json'}, cache: 'no-store'}); |
19 | 34 | if (!response.ok) throw new Error(response.status === 404 ? 'This evidence is no longer in the current workspace or trace view.' : 'Could not load this evidence. Try again.'); |
|
85 | 100 | let loadConversation = () => {}; |
86 | 101 | if (reader?.querySelector('.reader-layout')) { |
87 | 102 | const urlState = new URL(location.href).searchParams; |
88 | | - let state = {q: urlState.get('c_q') || '', kind: urlState.get('c_kind') || 'all', |
| 103 | + let state = {q: urlState.get('c_q') || '', kind: urlState.get('c_kind') || reader.dataset.defaultKind || 'all', |
89 | 104 | page: urlState.get('c_page') || 1, position: urlState.get('c_position'), |
90 | 105 | section: urlState.get('c_section'), chunk: urlState.get('c_chunk') || 0}; |
91 | 106 | let controller, latest; |
|
96 | 111 | const indexHost = reader.querySelector('.reader-rows'); |
97 | 112 | const pages = reader.querySelector('.reader-pages'); |
98 | 113 | const change = patch => { state = {...state, ...patch}; loadConversation(); }; |
| 114 | + // The view by model call: one row a call, then what it sent, its answer and the steps under it. |
| 115 | + const clock = iso => { const at = new Date(iso); return Number.isNaN(at.getTime()) ? '' : at.toLocaleTimeString(); }; |
| 116 | + const size = value => value >= 1048576 ? `${(value / 1048576).toFixed(1)} MB` : `${number(Math.max(1, Math.round(value / 1024)))} KB`; |
| 117 | + const said = value => typeof value === 'string' ? value : JSON.stringify(value); |
| 118 | + const part = (title, children) => { const box = el('div', 'call-part'); box.append(el('h4', '', title), ...children); return box; }; |
| 119 | + const picture = p => { |
| 120 | + if (!p.url) return el('span', 'call-picture-kept', [p.type, p.bytes ? size(p.bytes) : '', 'not kept'].filter(Boolean).join(' · ')); |
| 121 | + const link = el('a', 'call-picture-link'); link.href = p.url; link.target = '_blank'; link.rel = 'noopener'; |
| 122 | + const img = el('img', 'call-picture'); img.src = p.url; img.alt = `Picture ${p.sha256.slice(0, 12)}, ${p.type}`; |
| 123 | + img.loading = 'lazy'; img.decoding = 'async'; link.title = `sha256 ${p.sha256}`; |
| 124 | + // a click opens it over the page; a click with a key held still opens it in a tab of its own |
| 125 | + link.addEventListener('click', event => { |
| 126 | + if (event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; |
| 127 | + event.preventDefault(); viewPicture(p.url, img.alt); |
| 128 | + }); |
| 129 | + link.append(img); return link; |
| 130 | + }; |
| 131 | + const item = (entry, selected) => { |
| 132 | + const node = button('', () => change({section: entry.key, chunk: 0}), false, 'call-item'); |
| 133 | + node.setAttribute('aria-pressed', String(selected === entry.key)); |
| 134 | + node.append(el('span', 'call-item-kind', entry.label), el('span', 'call-item-preview', entry.preview)); |
| 135 | + if (entry.characters) node.append(el('span', 'call-item-size mono', number(entry.characters))); |
| 136 | + return node; |
| 137 | + }; |
| 138 | + const step = entry => { |
| 139 | + const node = button('', () => { searchInput.value = ''; change({kind: 'all', q: '', step: String(entry.step_id), position: null, page: 1, section: null, chunk: 0}); }, false, 'call-item'); |
| 140 | + node.append(el('span', 'call-item-kind mono', `#${entry.step_id} ${entry.source || ''}`), el('span', 'call-item-preview', entry.preview)); |
| 141 | + return node; |
| 142 | + }; |
| 143 | + const textWindow = content => { |
| 144 | + const block = el('div', 'reader-content'), head = el('div', 'reader-content-head'); |
| 145 | + head.append(el('h4', '', content.label), el('span', 'mono', `${number(content.characters)} characters`)); |
| 146 | + block.append(head); |
| 147 | + if (content.text) { const pre = el('pre', '', content.text); pre.tabIndex = 0; pre.setAttribute('aria-label', content.label); block.append(pre); } |
| 148 | + else block.append(el('p', 'muted', 'No content recorded.')); |
| 149 | + const nav = el('nav', 'reader-content-nav'); nav.setAttribute('aria-label', 'Content pages'); |
| 150 | + nav.append(button('← Previous', () => change({section: content.key, chunk: content.chunk - 1}), content.chunk === 0), |
| 151 | + el('span', '', `Part ${number(content.chunk + 1)} / ${number(content.chunks)} · ${number(content.start + (content.characters ? 1 : 0))}–${number(content.end)}`), |
| 152 | + button('Next →', () => change({section: content.key, chunk: content.chunk + 1}), content.chunk + 1 >= content.chunks)); |
| 153 | + block.append(nav); return block; |
| 154 | + }; |
| 155 | + const renderCalls = data => { |
| 156 | + status.textContent = `${number(data.matches)} of ${number(data.total)} calls${data.query ? ` match “${data.query}”` : ''}. ${data.notice || 'Select a call to see what it sent and answered.'}`; |
| 157 | + indexHost.replaceChildren(); |
| 158 | + data.rows.forEach(row => { |
| 159 | + const node = button('', () => change({position: row.position, section: null, chunk: 0}), false, 'reader-row'); |
| 160 | + node.setAttribute('aria-current', String(row.position === data.selected?.position)); |
| 161 | + const meta = el('span', 'reader-row-meta'); |
| 162 | + meta.append(el('span', 'mono', row.label)); |
| 163 | + if (row.time) meta.append(el('span', '', clock(row.time))); |
| 164 | + if (row.tokens && row.tokens.input !== undefined) meta.append(el('span', '', `${number(row.tokens.input)} in · ${number(row.tokens.output || 0)} out`)); |
| 165 | + if (row.pictures) meta.append(el('span', '', `${row.pictures} picture${row.pictures === 1 ? '' : 's'}`)); |
| 166 | + if (row.unreadable || row.status !== 200) meta.append(el('span', 'reader-error', '!')); |
| 167 | + node.append(meta, el('span', 'reader-row-preview', row.preview)); indexHost.append(node); |
| 168 | + }); |
| 169 | + pages.querySelector('span').textContent = `${data.page} / ${data.pages}`; |
| 170 | + pages.querySelector('[data-page=previous]').disabled = data.page <= 1; |
| 171 | + pages.querySelector('[data-page=next]').disabled = data.page >= data.pages; |
| 172 | + selectedHost.replaceChildren(); |
| 173 | + const chosen = data.selected; |
| 174 | + if (!chosen) { selectedHost.append(el('p', 'muted', data.notice || 'No calls match.')); return; } |
| 175 | + const header = el('div', 'reader-step-nav'), info = el('div'); |
| 176 | + info.append(el('h3', '', [chosen.label, chosen.model, chosen.side ? 'side call' : ''].filter(Boolean).join(' · '))); |
| 177 | + const facts = [`${chosen.match_number} of ${data.matches} calls`]; |
| 178 | + if (chosen.time) facts.push(chosen.time); |
| 179 | + if (chosen.seconds !== null) facts.push(`${chosen.seconds.toFixed(2)}s`); |
| 180 | + const t = chosen.tokens || {}; |
| 181 | + if (t.input !== undefined) facts.push(`input ${number(t.input)}${t.cached ? ` (cached ${number(t.cached)})` : ''} · output ${number(t.output || 0)}${t.reasoning ? ` (reasoning ${number(t.reasoning)})` : ''}`); |
| 182 | + if (chosen.cost_usd !== null) facts.push(`$${chosen.cost_usd.toFixed(4)}${chosen.cost_from === 'estimate' ? ' estimated' : ''}`); |
| 183 | + info.append(el('p', '', facts.join(' · '))); |
| 184 | + const actions = el('div', 'reader-step-actions'); |
| 185 | + actions.append(button('← Previous', () => change({position: chosen.previous, section: null, chunk: 0}), chosen.previous === null), button('Next →', () => change({position: chosen.next, section: null, chunk: 0}), chosen.next === null)); |
| 186 | + header.append(info, actions); selectedHost.append(header); |
| 187 | + const selected = chosen.content?.key, sent = [], answer = []; |
| 188 | + if (!chosen.sent.recorded) sent.push(el('p', chosen.sent.unreadable ? 'small bad' : 'muted', chosen.sent.note)); |
| 189 | + else { |
| 190 | + if (!chosen.sent.items.length && !chosen.sent.folded) sent.push(el('p', 'muted', 'Nothing the call before did not send.')); |
| 191 | + chosen.sent.items.forEach(entry => { |
| 192 | + sent.push(item(entry, selected)); |
| 193 | + if (entry.pictures.length) { const row = el('div', 'call-pictures'); entry.pictures.forEach(p => row.append(picture(p))); sent.push(row); } |
| 194 | + }); |
| 195 | + const folded = chosen.sent.folded; |
| 196 | + if (folded) sent.push(item({key: folded.key, label: 'Rewritten or dropped', preview: [folded.rewritten ? `${folded.rewritten} rewritten` : '', folded.dropped ? `${folded.dropped} dropped` : ''].filter(Boolean).join(' · ')}, selected)); |
| 197 | + } |
| 198 | + selectedHost.append(part('Sent', sent)); |
| 199 | + const a = chosen.answer; |
| 200 | + const ended = [a.end ? `ended ${said(a.end)}` : '', a.served_by ? `served by ${a.served_by}` : '', a.abridged ? 'abridged in the call record' : ''].filter(Boolean); |
| 201 | + if (ended.length) answer.push(el('p', 'call-facts', ended.join(' · '))); |
| 202 | + if (a.error) answer.push(el('p', 'small bad', `Error in the reply · ${a.error.message || said(a.error)}`)); |
| 203 | + a.items.forEach(entry => answer.push(item(entry, selected))); |
| 204 | + if (!a.items.length) answer.push(el('p', 'muted', chosen.refused ? 'Refused: nothing was sent.' : 'No answer recorded.')); |
| 205 | + selectedHost.append(part('Answer', answer)); |
| 206 | + if (chosen.content) selectedHost.append(textWindow(chosen.content)); |
| 207 | + if (chosen.before.length) selectedHost.append(part('Before the first call', chosen.before.map(step))); |
| 208 | + if (chosen.steps.length) selectedHost.append(part('Steps under it', [...chosen.steps.map(step), ...(chosen.more_steps ? [el('p', 'muted small', `and ${number(chosen.more_steps)} more`)] : [])])); |
| 209 | + }; |
99 | 210 | loadConversation = async () => { |
100 | 211 | conversationStarted = true; |
101 | 212 | controller?.abort(); controller = new AbortController(); |
|
118 | 229 | else current.searchParams.delete(`c_${key}`); |
119 | 230 | }); |
120 | 231 | history.replaceState(null, '', current); |
| 232 | + reader.querySelectorAll('[data-kind]').forEach(node => node.setAttribute('aria-pressed', String(node.dataset.kind === data.kind))); |
| 233 | + if (data.view === 'calls') { renderCalls(data); return; } |
121 | 234 | status.textContent = `${number(data.matches)} of ${number(data.total)} source steps${data.query ? ` match “${data.query}”` : ''}. ${data.notice || 'Select a row to keep its message and tools together.'}`; |
122 | 235 | reader.querySelectorAll('[data-kind]').forEach(node => node.setAttribute('aria-pressed', String(node.dataset.kind === data.kind))); |
123 | 236 | indexHost.replaceChildren(); |
|
180 | 293 | if (next !== undefined) { event.preventDefault(); tabs[next].focus(); openTab(tabs[next].dataset.tab); } |
181 | 294 | }); |
182 | 295 | }); |
| 296 | + // a link of the Artifacts tab to another tab of this page opens it in place |
| 297 | + document.getElementById('trial-artifacts')?.addEventListener('click', event => { |
| 298 | + const link = event.target.closest('a[data-open-tab]'); |
| 299 | + if (!link || !tabs.some(tab => tab.dataset.tab === link.dataset.openTab)) return; |
| 300 | + event.preventDefault(); openTab(link.dataset.openTab); document.getElementById('tab-' + link.dataset.openTab)?.focus(); |
| 301 | + }); |
183 | 302 | openTab(location.hash.startsWith('#span=') ? 'trace' : new URL(location.href).searchParams.get('tab') || 'overview', false); |
184 | 303 |
|
185 | 304 | let spanController; |
|
0 commit comments