diff --git a/packages/webui/src/public/display.js b/packages/webui/src/public/display.js index 082b759c1..c6acace7f 100644 --- a/packages/webui/src/public/display.js +++ b/packages/webui/src/public/display.js @@ -7,9 +7,9 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( (() => { const systemFetch = window.fetch.bind(window); const POLL_MS = 4000; - // Control, format (direction, zero-width, soft hyphen, word joiner, tag), separator and other invisible + // Control, format (direction, zero-width, soft hyphen, word joiner, tag), separator and default-ignorable // characters (fillers, variation selectors) never reach the page; newline and tab stay. - const HIDDEN = /(?![\n\t])[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\u034f\u115f\u1160\u180b-\u180d\u3164\ufe00-\ufe0f\uffa0\u{e0100}-\u{e01ef}]/gu; + const HIDDEN = /(?![\n\t])[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\p{Default_Ignorable_Code_Point}]/gu; const clean = (value, max = 8000) => (typeof value === 'string' ? value : value === null || value === undefined ? '' : String(value)).replace(/\r\n?/g, '\n').replace(HIDDEN, '').slice(0, max); const when = value => window.MosaicLocalTime(clean(value, 64)); const el = (tag, cls, text) => { const n = document.createElement(tag); if (cls) n.className = cls; if (text !== undefined) n.textContent = clean(text); return n; }; @@ -188,17 +188,22 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( let anchor = null; if (!first && !atBottom) { const i = [...t.children].findIndex(n => n.getBoundingClientRect().bottom > edge() + 1); - if (i >= 0 && shown[i] !== undefined) anchor = { key: shown[i], off: t.children[i].getBoundingClientRect().top - edge() }; + if (i >= 0 && shown[i] !== undefined) anchor = { i, key: shown[i], off: t.children[i].getBoundingClientRect().top - edge() }; } lastTranscript = key; const owners = new Map(list.filter(m => m.from === 'owner' && typeof m.id === 'string').map(m => [m.id, clean(m.text)])); if (list.length) t.replaceChildren(...list.map(m => entryNode(m, owners))); else t.replaceChildren(el('li', 'triage-empty', 'No messages yet.')); - shown = list.map(m => JSON.stringify(m)); + const next = list.map(m => JSON.stringify(m)); + // Lines only drop from the top and add at the bottom, so the anchor moves up by the number dropped. + // That finds the right one of two identical lines; anything else falls back to the first match. + const fits = d => shown.length - d <= next.length && shown.slice(d).every((k, n) => next[n] === k); + let j = -1; + if (anchor) { let d = 0; while (d <= anchor.i && !fits(d)) d += 1; j = d <= anchor.i ? anchor.i - d : next.indexOf(anchor.key); } + shown = next; if (first || atBottom) t.scrollTop = t.scrollHeight; else { t.scrollTop = top; - const j = anchor ? shown.indexOf(anchor.key) : -1; if (j >= 0) t.scrollTop += t.children[j].getBoundingClientRect().top - edge() - anchor.off; else if (anchor) t.scrollTop = 0; } @@ -245,7 +250,7 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( // These come after the host wrote the message, with its "did not start" line under it. if (code === 'triage-route-invalid' || code === 'triage-start-failed') return { text: `Not delivered: ${code === 'triage-start-failed' ? 'the Triage session did not start' : 'Triage did not start because the model route\'s answer is not usable'}. The message is in the conversation with the Console's note; it was not sent again.` }; // This one can come before or after the message is written. - if (code === 'triage-dir-unsafe') return { text: 'Not delivered: a Triage directory is not safe to use, so Triage did not start. It was not sent again: check the conversation before you send it again.' }; + if (code === 'triage-dir-unsafe') return { text: 'Not delivered: a Triage directory is not safe to use. It was not sent again: check the conversation before you send it again.' }; if (status === 409) return { text: code === 'triage-no-route' ? 'Not sent: no model route is set for Triage. The instance owner sets the route first.' : code === 'triage-route-refused' ? 'Not sent: the model route refused to start Triage.' : 'Not sent: the model route\'s answer is not usable.' }; if (status === 429) { if (code === 'triage-over-cap') { diff --git a/packages/webui/tests/triage-drawer-browser.test.mjs b/packages/webui/tests/triage-drawer-browser.test.mjs index 4a1634a92..dca0f8965 100644 --- a/packages/webui/tests/triage-drawer-browser.test.mjs +++ b/packages/webui/tests/triage-drawer-browser.test.mjs @@ -286,6 +286,7 @@ test('item 3: a send that reached the conversation but did not start Triage says host.h.next = new TriageError('triage-dir-unsafe', 'x'); status = await sendAndWait(f, status); assert.match(status, /Not delivered.*not safe to use.*check the conversation/i); + assert.doesNotMatch(status, /did not start/i, 'this one can come while a session runs'); }); test('item 3: a read that turns sending off hides the box and keeps the draft; one that turns it on again shows it', async t => { @@ -350,25 +351,28 @@ test('item 4: a poll keeps the draft and the cursor, and keeps the scroll positi assert.equal(await f.b.evaluate(`(() => { const s = ${scroller}; return s.scrollHeight - s.scrollTop - s.clientHeight < 4; })()`), true); }); -test('item 4: at the 200-line cap the oldest lines drop, and a scrolled-up reader keeps the same line in view', async t => { +test('item 4: at the 200-line cap the oldest lines drop, and a scrolled-up reader keeps the same lines in view, even with an identical line above', async t => { const line = i => ({ at: new Date(Date.now() - (300 - i) * 60000).toISOString(), from: 'host', text: `line ${i}` }); const transcript = Array.from({ length: 200 }, (_, i) => line(i)); + transcript[4] = transcript[8]; // an identical twin of the line that is first in view const host = fakeHost({ running: true, transcript }); const f = await setup(t, { host, viewport: [1440, 700] }); await open(f); - const firstInView = `(() => { const s = document.querySelector("#triage-transcript"), top = s.getBoundingClientRect().top; const li = [...s.children].find(n => n.getBoundingClientRect().bottom > top + 1); return { text: li.textContent, off: Math.round(li.getBoundingClientRect().top - top) }; })()`; + const firstInView = `(() => { const s = document.querySelector("#triage-transcript"), top = s.getBoundingClientRect().top; const li = [...s.children].find(n => n.getBoundingClientRect().bottom > top + 1); return { text: li.textContent, second: li.nextElementSibling.textContent, off: Math.round(li.getBoundingClientRect().top - top) }; })()`; await f.b.evaluate('document.querySelector("#triage-transcript").scrollTop = 600'); const before = await f.b.evaluate(firstInView); + assert.match(before.text, /line 8$/, 'the twin line is the one in view'); host.h.transcript = [...transcript.slice(3), line(200), line(201), line(202)]; await f.wait('document.querySelector("#triage-transcript").textContent.includes("line 202")', 300); const after = await f.b.evaluate(firstInView); assert.equal(after.text, before.text, 'the same line is first in view'); + assert.equal(after.second, before.second, 'and the same line follows it'); assert.ok(Math.abs(after.off - before.off) <= 1, `at the same place: ${before.off} then ${after.off}`); }); test('item 5: markup and control characters in every field stay text: no element, no link, no script', async t => { const BAD = 'linkbold'; - const CTRL = '\u0007\u001b[31m\u202eevil\u200b\u2028\ufeff\u00ad\u2060\u034f\ufe0f\u{E0041}\u{E0100}'; + const CTRL = '\u0007\u001b[31m\u202eevil\u200b\u2028\ufeff\u00ad\u2060\u034f\ufe0f\u{E0041}\u{E0100}\u{E0002}\u17b4\ufff0'; const v = s => `${BAD}${CTRL}${s}`; const snapshot = { snapshotVersion: 1, at: v('at'), console: { state: v('state'), at: v('cat') }, @@ -401,7 +405,7 @@ test('item 5: markup and control characters in every field stay text: no element for (const word of ['owner text', 'triage text', 'host text', 'cmd', 'corg', 'step', 'org', 'oc', 'code', 'active']) assert.ok(out.text.includes(word), `${word} shown as text`); assert.ok(out.text.includes('