fix(webui): follow transcripts in page flow

This commit is contained in:
jason.woltje committed 2026-11-07 03:21:51 -06:00
1 parent 2f4470eed2
commit 388fa5d0ca
2 files changed
+146 -13

No files matched your search

+53 -13
View File
@@ -8,10 +8,16 @@
const txt = value => esc(inert(value));
const states = new Map(), forms = new Map(), knownRequests = new Map();
const pinnedScrolls = new WeakMap();
const pageFlow = log => log.closest('.project-chat') && matchMedia('(max-width: 319px), (max-height: 540px)').matches;
const transcriptAtTail = log => pageFlow(log)
? (log.lastElementChild?.getBoundingClientRect().bottom ?? 0) - innerHeight < 40
: log.scrollHeight - log.scrollTop - log.clientHeight < 40;
const pinTranscript = log => {
const before = log.scrollTop;
log.scrollTop = log.scrollHeight;
if (log.scrollTop !== before) pinnedScrolls.set(log, log.scrollTop);
const scroller = pageFlow(log) ? document.scrollingElement : log;
const before = scroller.scrollTop;
if (scroller === log) log.scrollTop = log.scrollHeight;
else if (log.lastElementChild) window.scrollBy(0, log.lastElementChild.getBoundingClientRect().bottom - innerHeight);
if (scroller.scrollTop !== before) pinnedScrolls.set(scroller, scroller.scrollTop);
};
let current = null, controller = null, generation = 0, signedOut = false;
const out = () => signedOut || document.body.classList.contains('signed-out');
@@ -426,7 +432,7 @@
const content = current.container.querySelector('[data-project-content]');
const projectFocus = content?.contains(active) ? {id:active.id,index:[...content.querySelectorAll('a,button,select,input,textarea,summary,[tabindex]')].indexOf(active)} : null;
const disclosures = content ? [...content.querySelectorAll('details')].map((e,i)=>e.open?i:-1).filter(i=>i>=0) : [];
return { field, dialog:dialog?.id, dialogValues, dialogFocus, toggles, projectFocus, disclosures, detailsOpen: current.container.querySelector('.chat-details')?.open, top: log?.scrollTop, tail: log ? log.scrollHeight - log.scrollTop - log.clientHeight < 40 : true };
return { field, dialog:dialog?.id, dialogValues, dialogFocus, toggles, projectFocus, disclosures, detailsOpen: current.container.querySelector('.chat-details')?.open, top: log ? (pageFlow(log) ? document.scrollingElement.scrollTop : log.scrollTop) : undefined, tail: log ? transcriptAtTail(log) : true };
}
let transcriptObserver = null, transcriptResize = null;
function paint(navigated = false) {
@@ -472,7 +478,11 @@
transcriptObserver = new MutationObserver(pin);
transcriptObserver.observe(observedLog, { childList: true, subtree: true, characterData: true });
// The log's height settles after paint (composer and request card layout); re-pin while following.
if (typeof ResizeObserver === 'function') { transcriptResize = new ResizeObserver(pin); transcriptResize.observe(observedLog); }
if (typeof ResizeObserver === 'function') {
transcriptResize = new ResizeObserver(pin);
transcriptResize.observe(observedLog);
if (pageFlow(observedLog)) transcriptResize.observe(c.container);
}
}
window.MosaicRequests?.mount?.(c.container, { archiveUnknown: !!c.archiveUnknown, projectArchived: !!c.projectArchived });
const t = c.kind === 'thread' ? c.thread : null;
@@ -520,11 +530,12 @@
} else dialog.querySelector('h2')?.focus({preventScroll:true});
}
}
if (navigated) c.container.querySelector('.project-tabs [aria-current]')?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
const log = c.container.querySelector('.chat-log');
if (log) {
const s = state(c.key ?? c.id);
if (navigated || s.tailRequested || s.follow && (keep?.tail ?? true)) pinTranscript(log);
else if (keep?.top !== undefined) log.scrollTop = keep.top;
else if (keep?.top !== undefined) { if (pageFlow(log)) window.scrollTo(0, keep.top); else log.scrollTop = keep.top; }
s.tailRequested = false;
}
if (!navigated && keep?.field) {
@@ -535,7 +546,6 @@
}
} else if (navigated) c.container.querySelector('h1')?.focus({ preventScroll: true });
updateRoomCompletion();
if (navigated) c.container.querySelector('.project-tabs [aria-current]')?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
function readError(err) {
return err.code === 'channel-disabled' ? channelMessage : err.code === 'thread-not-owned' ? 'This thread is not available to this user.' :
@@ -776,14 +786,44 @@
if (field === 'instance') { draft.instanceChosen = true; draft.receipt = ''; paint(); }
}
});
let pageReaderInput = false;
// User input cancels a pending page pin, including scrollbar dragging.
const readerInput = event => {
if (!['thread', 'room'].includes(current?.kind)) return;
if (event.type === 'keydown' && (!['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End', ' '].includes(event.key) || event.target.closest?.('input, textarea, select, button, a, [contenteditable=true]'))) return;
if (event.type === 'pointerdown' && event.target !== document.documentElement && event.target !== document.body && !event.target.matches?.('.chat-log')) return;
const log = current.container.querySelector('.chat-log');
if (!log) return;
if (pageFlow(log)) { pageReaderInput = true; pinnedScrolls.delete(document.scrollingElement); }
else if (event.target === log || log.contains(event.target)) pinnedScrolls.delete(log);
};
for (const type of ['wheel', 'touchstart', 'pointerdown', 'keydown']) document.addEventListener(type, readerInput, true);
document.addEventListener('scroll', event => {
if (event.target.matches?.('.chat-log') && ['thread', 'room'].includes(current?.kind)) {
const el = event.target;
const pinned = pinnedScrolls.get(el);
pinnedScrolls.delete(el);
if (pinned !== undefined && el.scrollTop === pinned) return;
state(current.key ?? current.id).follow = el.scrollHeight - el.scrollTop - el.clientHeight < 40;
if (!['thread', 'room'].includes(current?.kind)) return;
const log = current.container.querySelector('.chat-log');
if (!log) return;
const scroller = pageFlow(log) ? document.scrollingElement : log;
if (event.target !== scroller && !(scroller === document.scrollingElement && event.target === document)) return;
const pinned = pinnedScrolls.get(scroller);
pinnedScrolls.delete(scroller);
const s = state(current.key ?? current.id);
// Scroll anchoring can move the document during layout without reader input.
if (scroller !== log) {
if (!pageReaderInput && s.follow) { pinTranscript(log); return; }
pageReaderInput = false;
s.follow = transcriptAtTail(log);
return;
}
if (pinned !== undefined && scroller.scrollTop === pinned) {
if (transcriptAtTail(log)) return;
// Allow the resize observer to settle a shrinking log before treating an
// old matching offset as a reader scroll away from newly grown content.
requestAnimationFrame(() => requestAnimationFrame(() => {
if (log.isConnected && scroller.scrollTop === pinned && !transcriptAtTail(log)) s.follow = false;
}));
return;
}
s.follow = transcriptAtTail(log);
}, true);
async function write(action, key, value, path) {
if (!current || !current.canSend || out() || current.channelDisabled) return;
@@ -0,0 +1,93 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, writeFile, chmod, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fixture } from './fixture.mjs';
import { browser } from './browser.mjs';
import { P, T, stub } from './helpers/projects-review-fixture.mjs';
// Use Chromium tab zoom, not CSS zoom or device-scale emulation. The profile's
// default zoom and extension both set the same 200% level for fixture pages.
async function zoomLauncher() {
const dir = await mkdtemp(join(tmpdir(), 's9-zoom-'));
const ext = join(dir, 'extension');
await mkdir(ext);
await writeFile(join(ext, 'manifest.json'), JSON.stringify({manifest_version:3,name:'S9 fixture zoom',version:'1.0',permissions:['tabs'],background:{service_worker:'zoom.js'}}));
await writeFile(join(ext, 'zoom.js'), "chrome.tabs.onUpdated.addListener((id,change,tab)=>{if(tab.url?.startsWith('http://127.0.0.1:')) chrome.tabs.setZoom(id,2)});\n");
const launcher = join(dir, 'chromium');
await writeFile(launcher, `#!/usr/bin/env python3\nimport json, math, os, pathlib, sys\np = pathlib.Path(next(a.split('=',1)[1] for a in sys.argv[1:] if a.startswith('--user-data-dir='))) / 'Default'\np.mkdir(exist_ok=True)\n(p / 'Preferences').write_text(json.dumps({'partition': {'default_zoom_level': math.log(2)/math.log(1.2)}}))\nos.execv('/usr/bin/chromium', ['/usr/bin/chromium', '--disable-extensions-except=${ext}', '--load-extension=${ext}', *sys.argv[1:]])\n`);
await chmod(launcher, 0o755);
return {dir,launcher};
}
async function setup(t, width, zoom = false, source = stub, height = width===390?900:1000) {
const f = await fixture();
const previous = process.env.CHROMIUM;
const z = zoom ? await zoomLauncher() : null;
if (z) process.env.CHROMIUM = z.launcher;
let b;
try { b = await browser({machine:false}); } finally { if (previous === undefined) delete process.env.CHROMIUM; else process.env.CHROMIUM = previous; }
t.after(async () => { await b.close(); await f.close(); if(z) await rm(z.dir,{recursive:true,force:true}); });
await b.call('Fetch.enable',{patterns:[{urlPattern:'*'}]});
b.on('Fetch.requestPaused',p=>void b.call(p.request.url.startsWith(f.base+'/')?'Fetch.continueRequest':'Fetch.failRequest',p.request.url.startsWith(f.base+'/')?{requestId:p.requestId}:{requestId:p.requestId,errorReason:'BlockedByClient'}).catch(()=>{}));
await b.call('Page.addScriptToEvaluateOnNewDocument',{source});
await b.viewport(width,height);
await b.navigate(f.base+'/#/projects/'+P);
const wait=expr=>b.evaluate(`(async()=>{for(let i=0;i<200;i++){if(${expr})return;await new Promise(r=>setTimeout(r,30))}throw Error('Timeout '+${JSON.stringify(expr)})})()`);
if(zoom) await wait(`devicePixelRatio===2 && innerWidth===${width / 2}`);
await wait("document.querySelector('[data-project-content] [data-project-room-status]')");
return {b,wait};
}
// Enough history to expose page-flow behavior at both zoomed sizes.
const history = stub + `;for(let i=0;i<42;i++){const n=String(i+10).padStart(12,'0'),body='History '+i+' '+('message text '.repeat(8)),at='2026-11-06T10:26:00Z';fixtureMessages.push({id:'40000000-0000-4000-8000-'+n,from_role:'human',body,at});const id='50000000-0000-4000-8000-'+n;fixtureRoom.push({id,ids:[id],from_role:'human',to:['assistant'],body,at})}`;
const settle = b => b.evaluate('(async()=>{for(let i=0;i<12;i++)await new Promise(requestAnimationFrame)})()');
const metrics = b => b.evaluate(`(()=>{const log=document.querySelector('.project-chat .chat-log'),r=log.lastElementChild.getBoundingClientRect(),d=document.documentElement;return {top:scrollY,height:innerHeight,documentHeight:d.scrollHeight,lastTop:r.top,lastBottom:r.bottom,logTop:log.scrollTop,logBottom:log.scrollHeight-log.clientHeight-log.scrollTop}})()`);
const visible = m => m.lastTop>=-1 && m.lastBottom<=m.height+1;
for (const width of [1440,390]) for(const [name,route] of [['room','/projects/rooms/'+P],['conversation','/projects/threads/'+T]]) {
test(`S11 ${name} follows page-flow transcript at ${width} with 200% tab zoom`,async t=>{
const {b,wait}=await setup(t,width,true,history);
await b.evaluate(`location.hash=${JSON.stringify('#'+route)}`);
await wait("document.querySelectorAll('.project-chat .chat-log > li').length >= 42");
await settle(b);
const initial=await metrics(b);
console.log('S11 initial',width,name,JSON.stringify(initial));
if(process.env.S11_CAPTURE) await b.screenshot(`${process.env.S11_CAPTURE}/${process.env.S11_PHASE}-${name}-${width}-200.png`);
assert.ok(initial.documentHeight >= initial.height*3,JSON.stringify(initial));
assert.ok(visible(initial),`newest message not visible on opening: ${JSON.stringify(initial)}`);
await b.call('Input.dispatchMouseEvent',{type:'mouseWheel',x:80,y:100,deltaX:0,deltaY:-initial.height*2}); await settle(b);
const up=await metrics(b);
assert.ok(up.top < initial.top-100,JSON.stringify({initial,up}));
await b.evaluate(`(()=>{const id='${name==='room'?'50000000':'40000000'}-0000-4000-8000-000000000099',m={id,from_role:'human',body:'New message while reading',at:'2026-11-06T10:27:00Z'};${name==='room'?"m.ids=[id];m.to=['assistant'];fixtureRoom.push(m)":'fixtureMessages.push(m)'};document.querySelector('#refresh').click()})()`);
await wait("document.querySelector('[data-message$=\"000000000099\"]')"); await settle(b);
const held=await metrics(b); console.log('S11 held',width,name,JSON.stringify({up,held}));
assert.ok(Math.abs(held.top-up.top)<=2,`reader moved: ${JSON.stringify({up,held})}`);
assert.ok(!visible(held),JSON.stringify(held));
await b.evaluate("document.querySelector('.chat-log').lastElementChild.scrollIntoView({block:'end'})"); await settle(b);
await b.evaluate(`(()=>{const id='${name==='room'?'50000000':'40000000'}-0000-4000-8000-000000000100',m={id,from_role:'human',body:'New message at tail',at:'2026-11-06T10:28:00Z'};${name==='room'?"m.ids=[id];m.to=['assistant'];fixtureRoom.push(m)":'fixtureMessages.push(m)'};document.querySelector('#refresh').click()})()`);
await wait("document.querySelector('[data-message$=\"000000000100\"]')"); await settle(b);
const followed=await metrics(b); console.log('S11 followed',width,name,JSON.stringify(followed));
assert.ok(visible(followed),`new message not followed: ${JSON.stringify(followed)}`);
});
}
test('S11 reader ending at an old pin after growth keeps position on the log scroller',async t=>{
const {b,wait}=await setup(t,1440,false,history);
await b.evaluate(`location.hash=${JSON.stringify('#/projects/threads/'+T)}`);
await wait("document.querySelectorAll('.project-chat .chat-log > li').length >= 42"); await settle(b);
const before=await b.evaluate(`(async()=>{const l=document.querySelector('.chat-log'),last=l.lastElementChild.querySelector('.chat-body');last.append(' pin '.repeat(150));await Promise.resolve();const pin=l.scrollTop;last.style.minHeight=(last.getBoundingClientRect().height+250)+'px';l.scrollTop=pin+100;l.scrollTop=pin;return {pin,top:l.scrollTop,gap:l.scrollHeight-l.clientHeight-l.scrollTop}})()`);
await settle(b);
await b.evaluate("document.querySelector('.chat-log li:last-child .chat-body').append(' next '.repeat(150))"); await settle(b);
const after=await metrics(b); console.log('S11 collision',JSON.stringify({before,after}));
assert.ok(before.gap>=249,JSON.stringify(before));
assert.ok(Math.abs(after.logTop-before.pin)<=2,`reader was pulled to tail: ${JSON.stringify({before,after})}`);
});
for(const height of [1000,700]) test(`S11 conversation retains ordinary layout at 1440x${height}`,async t=>{
const {b,wait}=await setup(t,1440,false,history,height);
await b.evaluate(`location.hash=${JSON.stringify('#/projects/threads/'+T)}`);
await wait("document.querySelectorAll('.project-chat .chat-log > li').length >= 42"); await settle(b);
const m=await metrics(b); console.log('S11 ordinary',height,JSON.stringify(m));
if(process.env.S11_CAPTURE && process.env.S11_PHASE==='after') await b.screenshot(`${process.env.S11_CAPTURE}/after-conversation-1440x${height}-100.png`);
assert.ok(m.documentHeight<=m.height && m.logBottom<=1,JSON.stringify(m));
});