The best way to control Claude Code, Codex and more from any computer or phone
| 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 | */ |
| 6 | const PIN_ORDER_DIGITS = "abcdefghijklmnopqrstuvwxyz"; |
| 7 | |
| 8 | function isValidPinOrderKey(key: string): boolean { |
| 9 | if (key.length === 0) return false; |
| 10 | for (const char of key) { |
| 11 | if (!PIN_ORDER_DIGITS.includes(char)) return false; |
| 12 | } |
| 13 | // A trailing minimum digit would leave no room to sort a key immediately |
| 14 | // before this one; generators never produce it, so treat it as corrupt. |
| 15 | return key.at(-1) !== PIN_ORDER_DIGITS[0]; |
| 16 | } |
| 17 | |
| 18 | /** Midpoint of two digit strings interpreted as fractions in (0, 1). |
| 19 | "" stands for the open bound on either side. Requires a < b. */ |
| 20 | function pinOrderMidpoint(a: string, b: string): string { |
| 21 | if (b !== "" && a >= b) throw new Error("pinOrderMidpoint: bounds out of order"); |
| 22 | if (b !== "") { |
| 23 | let n = 0; |
| 24 | while ((a.charAt(n) || PIN_ORDER_DIGITS[0]) === b.charAt(n)) n += 1; |
| 25 | if (n > 0) return b.slice(0, n) + pinOrderMidpoint(a.slice(n), b.slice(n)); |
| 26 | } |
| 27 | const digitA = a === "" ? 0 : PIN_ORDER_DIGITS.indexOf(a.charAt(0)); |
| 28 | const digitB = b === "" ? PIN_ORDER_DIGITS.length : PIN_ORDER_DIGITS.indexOf(b.charAt(0)); |
| 29 | if (digitB - digitA > 1) { |
| 30 | return PIN_ORDER_DIGITS.charAt(Math.round((digitA + digitB) / 2)); |
| 31 | } |
| 32 | if (b.length > 1) return b.charAt(0); |
| 33 | return PIN_ORDER_DIGITS.charAt(digitA) + pinOrderMidpoint(a.slice(1), ""); |
| 34 | } |
| 35 | |
| 36 | /** Key that sorts strictly between two neighbors; null bounds mean "top of |
| 37 | the pinned block" / "bottom of the keyed run". Returns null instead of |
| 38 | throwing when existing keys are corrupt or out of order — callers fall |
| 39 | back to rewriting the section. */ |
| 40 | export function pinOrderKeyBetween(before: string | null, after: string | null): string | null { |
| 41 | const a = before ?? ""; |
| 42 | const b = after ?? ""; |
| 43 | if (a !== "" && !isValidPinOrderKey(a)) return null; |
| 44 | if (b !== "" && !isValidPinOrderKey(b)) return null; |
| 45 | if (b !== "" && a >= b) return null; |
| 46 | return pinOrderMidpoint(a, b); |
| 47 | } |
| 48 | |
| 49 | /** Key for a newly pinned thread at the top of the pinned run: before the |
| 50 | smallest existing key, or a fresh key when there is none. Null when the |
| 51 | smallest key is corrupt; callers then send no key. */ |
| 52 | export function topOfPinnedRunOrderKey( |
| 53 | existingKeys: Iterable<string | null | undefined>, |
| 54 | ): string | null { |
| 55 | let firstKey: string | null = null; |
| 56 | for (const key of existingKeys) { |
| 57 | if (key == null) continue; |
| 58 | if (firstKey === null || key < firstKey) firstKey = key; |
| 59 | } |
| 60 | return pinOrderKeyBetween(null, firstKey); |
| 61 | } |
| 62 | |
| 63 | export function generateSpreadPinOrderKeys(count: number): string[] { |
| 64 | let width = 2; |
| 65 | let space = PIN_ORDER_DIGITS.length ** width; |
| 66 | while (space <= (count + 1) * 2) { |
| 67 | width += 1; |
| 68 | space *= PIN_ORDER_DIGITS.length; |
| 69 | } |
| 70 | const step = space / (count + 1); |
| 71 | const keys: string[] = []; |
| 72 | for (let i = 0; i < count; i += 1) { |
| 73 | let value = Math.round(step * (i + 1)); |
| 74 | if (value % PIN_ORDER_DIGITS.length === 0) value += 1; |
| 75 | let key = ""; |
| 76 | for (let digit = 0; digit < width; digit += 1) { |
| 77 | key = PIN_ORDER_DIGITS.charAt(value % PIN_ORDER_DIGITS.length) + key; |
| 78 | value = Math.floor(value / PIN_ORDER_DIGITS.length); |
| 79 | } |
| 80 | keys.push(key); |
| 81 | } |
| 82 | return keys; |
| 83 | } |