Official
chat
Built-in chat UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/chat@0.2.7
Permissions in 0.2.7
Files
window.ts7.2 KB
// A long list draws only the rows around where the reader is. The
// kernel's scroller holds room for the others (`rowsBefore`, `rowsAfter`)
// and says where its top is (`onRange`) when it comes near them, so the
// loaded rows are there before they come on screen. Unloaded history is
// paged separately by onReachStart; these indexes describe loaded entries
// only, while the kernel holds a few hundred rendered rows of them. Bounded
// Div lists opt in through their negotiated capability before the first draw.
//
// A kernel without windows never calls `onRange`, and the transcript then
// draws every row it has (and pages its older rows in as it scrolls up), as
// it did before.
import type { FoldEntry } from "./grouping.ts";
import type { Transcript } from "./state.ts";
/** Entries a window keeps on each side of the reader. */
export const SIDE = 200;
/** How close to a row left out the kernel lets the top come before it
* says where the top is (`RANGE_MARGIN` in the kernel). */
const MARGIN = 150;
/** About how many rows a screen shows below its top row. */
const SCREEN = 40;
/** Entries a window gains on a side in one tree past what puts the margin
* back, and the most it gains in one tree: each patch has the kernel build
* a few rows (a row costs it a fraction of a millisecond), so a reader who
* scrolls ahead of the window is caught up over a few trees, well inside
* the margin. A jump far away fills in the same way. */
export const STEP = 10;
const MOST = 30;
/** How many entries a transcript has before it draws a window: one this
* short is drawn whole and scrolls without the kernel building a row,
* and stays well inside a view's 50 000 nodes (a few dozen an entry). */
export const LONG = 1000;
let windowing = false;
/** Whether the kernel draws windows: it has said where a scroller's top is. */
export function windows(): boolean {
return windowing;
}
/** The kernel said where a scroller's top is. True the first time. */
export function noteWindows(): boolean {
const first = !windowing;
windowing = true;
return first;
}
/** One end of a window: the entry it starts at, by id and by the index it
* had (a page of older rows moves every index, an id stays). */
type Edge = { id: string; index: number };
/** A transcript's window: its first entry and the one after its last, or
* `null` when it reaches the end (and then follows the rows that come),
* and the entry at the top when it was placed. */
type Window = { lo: Edge; hi: Edge | null; top: number };
// A transcript owns only tiny anchors for its visited lists, never their items.
// The host maps canonical item identities, including subagent items. Keep an
// unmounted list's anchors until its transcript goes; the rendered range is not
// canonical membership and cannot be used to prune nested list positions.
const OUTER = Symbol("transcript");
const windowsOf = new WeakMap<Transcript, Map<string | symbol, Window>>();
export function entryId(entry: FoldEntry): string {
return entry.kind === "item" ? entry.item.id : entry.id;
}
function edge<T>(entries: readonly T[], index: number, id: (entry: T) => string): Edge {
return { id: id(entries[index]!), index };
}
/** Where `edge` is now: where it was, else found by its id, else its old
* index (its entry went: a fold took it in). */
function find<T>(entries: readonly T[], edge: Edge, id: (entry: T) => string): number {
const at = entries[edge.index];
if (at && id(at) === edge.id) return edge.index;
const index = entries.findIndex((entry) => id(entry) === edge.id);
edge.index = index >= 0 ? index : Math.min(edge.index, entries.length);
return edge.index;
}
/** The entries `[lo, hi)` the transcript draws: all of a short one; of a
* long one its window, or its end before the reader went anywhere. */
export function shownRange<T>(t: Transcript, entries: readonly T[], id: (entry: T) => string, key: string | symbol = OUTER): [number, number] {
const count = entries.length;
if (count <= LONG) {
windowsOf.get(t)?.delete(key);
return [0, count];
}
if (key === OUTER && !windowing) return [0, count];
const window = windowsOf.get(t)?.get(key);
if (!window) return [Math.max(0, count - SIDE - SCREEN), count];
const lo = find(entries, window.lo, id);
const hi = window.hi ? find(entries, window.hi, id) : count;
return hi > lo ? [lo, hi] : [lo, Math.min(count, lo + SCREEN)];
}
/** The row at the top of the scroller is `first`: the window moves toward
* the entries around it, by `STEP` new ones a side, and on the side the
* reader scrolls toward by what keeps the kernel's margin too, up to
* `MOST`. One far from what is shown starts again at the screen. (Request rows are few; a
* row is taken for the entry of its number.) */
export function moveWindow<T>(t: Transcript, entries: readonly T[], first: number, id: (entry: T) => string, key: string | symbol = OUTER): boolean {
const count = entries.length;
if (count <= LONG) {
return windowsOf.get(t)?.delete(key) ?? false;
}
const top = Math.min(Math.max(first, 0), count - 1);
const want = [Math.max(0, top - SIDE), Math.min(count, top + SCREEN + SIDE)] as const;
const before = shownRange(t, entries, id, key);
let [lo, hi] = before;
// Before any window the transcript shows its end.
const moved = top - (windowsOf.get(t)?.get(key)?.top ?? count - SCREEN);
if (hi <= top || lo > top + SCREEN) {
lo = Math.max(want[0], top - STEP);
hi = Math.min(want[1], top + SCREEN + STEP);
} else {
const above = Math.min(MOST, STEP + (moved < 0 ? Math.max(0, MARGIN - (top - lo)) : 0));
const below = Math.min(MOST, STEP + (moved > 0 ? Math.max(0, MARGIN - (hi - top)) : 0));
lo = lo > want[0] ? Math.max(want[0], lo - above) : want[0];
hi = hi < want[1] ? Math.min(want[1], hi + below) : want[1];
}
let lists = windowsOf.get(t);
if (!lists) windowsOf.set(t, lists = new Map());
lists.set(key, { lo: edge(entries, lo, id), hi: hi >= count ? null : edge(entries, hi, id), top });
return lo !== before[0] || hi !== before[1];
}
/** What a window leaves out: rows before and after it, and whether it
* reaches the transcript's end (and so draws the rows that end it). */
export type Unsent = { before: number; after: number; end: boolean };
/** The window `[lo, hi)` of `entries` leaves out the entries around it and
* the requests placed after them (`unsent` counts them); the requests it
* draws come back. A request after no entry ends the transcript. */
export function windowRequests<R extends { afterItemId?: string | null }>(
entries: readonly FoldEntry[],
[lo, hi]: readonly [number, number],
requests: readonly R[],
unsent: Unsent,
): R[] {
unsent.before = lo;
unsent.after = entries.length - hi;
unsent.end = hi === entries.length;
if (!requests.length) return [];
const at = new Map<string, number>();
entries.forEach((entry, i) => {
for (const item of entry.kind === "item" ? [entry.item] : entry.items) at.set(item.id, i);
});
return requests.filter((request) => {
const index = request.afterItemId ? at.get(request.afterItemId) : undefined;
if (index === undefined ? unsent.end : index >= lo && index < hi) return true;
if (index !== undefined && index < lo) unsent.before++;
else unsent.after++;
return false;
});
}Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.9latest | Nov 21, 2026 | >=5 <6 | 349 KB | 5 permissions | Listed |
| 0.2.8 | Nov 16, 2026 | >=5 <6 | 348.6 KB | 5 permissions | Listed |
| 0.2.7 | Nov 11, 2026 | >=5 <6 | 314.3 KB | 5 permissions | Listed |
| 0.2.6 | Nov 9, 2026 | >=5 <6 | 306.2 KB | 5 permissions | Listed |
| 0.2.5 | Oct 27, 2026 | >=2 <4 | 178.6 KB | 5 permissions | Listed |
No comments yet.