Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pinOrderKey.ts

90 lines3.7 KB

1/**
2 * Fractional order keys for the sidebar's Pinned section. Keys are lowercase
3 * digit strings compared as plain strings; a smaller key sorts higher. Shared
4 * so the server places an agent-issued pin the same way a client pin is placed.
5 */
6const PIN_ORDER_DIGITS = "abcdefghijklmnopqrstuvwxyz";
7/** Prefix of a tier that sorts before every plain key (see `activeThreadSortKey`). */
8export const LEADING_ORDER_TIER = "0";
9​
10function isValidPinOrderKey(key: string): boolean {
11 if (key.startsWith(LEADING_ORDER_TIER)) key = key.slice(LEADING_ORDER_TIER.length);
12 if (key.length === 0) return false;
13 for (const char of key) {
14 if (!PIN_ORDER_DIGITS.includes(char)) return false;
15 }
16 // A trailing minimum digit would leave no room to sort a key immediately
17 // before this one; generators never produce it, so treat it as corrupt.
18 return key.at(-1) !== PIN_ORDER_DIGITS[0];
20​
21/** Midpoint of two digit strings interpreted as fractions in (0, 1).
22 "" stands for the open bound on either side. Requires a < b. */
23function pinOrderMidpoint(a: string, b: string): string {
24 if (b !== "" && a >= b) throw new Error("pinOrderMidpoint: bounds out of order");
25 if (b !== "") {
26 let n = 0;
27 while ((a.charAt(n) || PIN_ORDER_DIGITS[0]) === b.charAt(n)) n += 1;
28 if (n > 0) return b.slice(0, n) + pinOrderMidpoint(a.slice(n), b.slice(n));
29 }
30 const digitA = a === "" ? 0 : PIN_ORDER_DIGITS.indexOf(a.charAt(0));
31 const digitB = b === "" ? PIN_ORDER_DIGITS.length : PIN_ORDER_DIGITS.indexOf(b.charAt(0));
32 if (digitB - digitA > 1) {
33 return PIN_ORDER_DIGITS.charAt(Math.round((digitA + digitB) / 2));
34 }
35 if (b.length > 1) return b.charAt(0);
36 return PIN_ORDER_DIGITS.charAt(digitA) + pinOrderMidpoint(a.slice(1), "");
38​
39/** Key that sorts strictly between two neighbors; null bounds mean "top of
40 the pinned block" / "bottom of the keyed run". Returns null instead of
41 throwing when existing keys are corrupt or out of order — callers fall
42 back to rewriting the section. */
43export function pinOrderKeyBetween(before: string | null, after: string | null): string | null {
44 const a = before ?? "";
45 const b = after ?? "";
46 if (a !== "" && !isValidPinOrderKey(a)) return null;
47 if (b !== "" && !isValidPinOrderKey(b)) return null;
48 if (b !== "" && a >= b) return null;
49 const tier = LEADING_ORDER_TIER;
50 if (b.startsWith(tier))
51 return tier + pinOrderMidpoint(a.slice(tier.length), b.slice(tier.length));
52 if (a.startsWith(tier)) return tier + pinOrderMidpoint(a.slice(tier.length), "");
53 return pinOrderMidpoint(a, b);
55​
56/** Key for a newly pinned thread at the top of the pinned run: before the
57 smallest existing key, or a fresh key when there is none. Null when the
58 smallest key is corrupt; callers then send no key. */
59export function topOfPinnedRunOrderKey(
60 existingKeys: Iterable<string | null | undefined>,
61): string | null {
62 let firstKey: string | null = null;
63 for (const key of existingKeys) {
64 if (key == null) continue;
65 if (firstKey === null || key < firstKey) firstKey = key;
66 }
67 return pinOrderKeyBetween(null, firstKey);
69​
70export function generateSpreadPinOrderKeys(count: number): string[] {
71 let width = 2;
72 let space = PIN_ORDER_DIGITS.length ** width;
73 while (space <= (count + 1) * 2) {
74 width += 1;
75 space *= PIN_ORDER_DIGITS.length;
76 }
77 const step = space / (count + 1);
78 const keys: string[] = [];
79 for (let i = 0; i < count; i += 1) {
80 let value = Math.round(step * (i + 1));
81 if (value % PIN_ORDER_DIGITS.length === 0) value += 1;
82 let key = "";
83 for (let digit = 0; digit < width; digit += 1) {
84 key = PIN_ORDER_DIGITS.charAt(value % PIN_ORDER_DIGITS.length) + key;
85 value = Math.floor(value / PIN_ORDER_DIGITS.length);
86 }
87 keys.push(key);
88 }
89 return keys;