The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | OrchestrationV2ProjectedTurnItem, |
| 3 | OrchestrationV2Subagent, |
| 4 | OrchestrationV2ThreadProjection, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { isOrchestrationV2TurnItemVisible } from "@t3tools/shared/orchestrationV2Timeline"; |
| 7 | |
| 8 | export type ThreadHistoryMeta = { |
| 9 | readonly historyCursor: string | null; |
| 10 | readonly hasMoreHistory: boolean; |
| 11 | readonly loading: boolean; |
| 12 | readonly error: string | null; |
| 13 | readonly expanded: boolean; |
| 14 | readonly latestLocalTurnOrdinal: number | null; |
| 15 | }; |
| 16 | |
| 17 | export const EMPTY_THREAD_HISTORY_META: ThreadHistoryMeta = { |
| 18 | historyCursor: null, |
| 19 | hasMoreHistory: false, |
| 20 | loading: false, |
| 21 | error: null, |
| 22 | expanded: false, |
| 23 | latestLocalTurnOrdinal: null, |
| 24 | }; |
| 25 | |
| 26 | export function isActiveHistoryRequestCursor( |
| 27 | requestCursor: string, |
| 28 | history: { readonly historyCursor: string | null }, |
| 29 | ): boolean { |
| 30 | return history.historyCursor === requestCursor; |
| 31 | } |
| 32 | |
| 33 | export function clearActiveHistoryLoading( |
| 34 | requestCursor: string, |
| 35 | history: ThreadHistoryMeta, |
| 36 | ): ThreadHistoryMeta { |
| 37 | if (!isActiveHistoryRequestCursor(requestCursor, history) || !history.loading) { |
| 38 | return history; |
| 39 | } |
| 40 | return { ...history, loading: false }; |
| 41 | } |
| 42 | |
| 43 | function projectedItemKey(row: OrchestrationV2ProjectedTurnItem): string { |
| 44 | return `${row.sourceThreadId}:${row.sourceItemId}`; |
| 45 | } |
| 46 | |
| 47 | function renumberVisible( |
| 48 | rows: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 49 | ): OrchestrationV2ProjectedTurnItem[] { |
| 50 | return rows.map((row, position) => (row.position === position ? row : { ...row, position })); |
| 51 | } |
| 52 | |
| 53 | // A page is a contiguous history range, so current rows before its first |
| 54 | // visible row are older than the page start and new page rows sit just |
| 55 | // before the next row both lists share. |
| 56 | function interleavePage( |
| 57 | current: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 58 | page: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 59 | pageKeys: ReadonlySet<string>, |
| 60 | ): OrchestrationV2ProjectedTurnItem[] { |
| 61 | const currentIndex = new Map(current.map((row, index) => [projectedItemKey(row), index])); |
| 62 | const merged: OrchestrationV2ProjectedTurnItem[] = []; |
| 63 | let pending: OrchestrationV2ProjectedTurnItem[] = []; |
| 64 | let next = 0; |
| 65 | const emitCurrentThrough = (end: number) => { |
| 66 | for (; next < end; next += 1) { |
| 67 | const row = current[next]!; |
| 68 | if (!pageKeys.has(projectedItemKey(row))) merged.push(row); |
| 69 | } |
| 70 | }; |
| 71 | for (const row of page) { |
| 72 | const index = currentIndex.get(projectedItemKey(row)); |
| 73 | if (index === undefined) { |
| 74 | pending.push(row); |
| 75 | continue; |
| 76 | } |
| 77 | emitCurrentThrough(index + 1); |
| 78 | merged.push(...pending, row); |
| 79 | pending = []; |
| 80 | } |
| 81 | merged.push(...pending); |
| 82 | emitCurrentThrough(current.length); |
| 83 | return merged; |
| 84 | } |
| 85 | |
| 86 | export function mergeOlderHistoryIntoProjection( |
| 87 | projection: OrchestrationV2ThreadProjection, |
| 88 | olderItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 89 | work?: { readonly anchorId: string; readonly nextCursor: string | null }, |
| 90 | ): OrchestrationV2ThreadProjection { |
| 91 | if (olderItems.length === 0 && work === undefined) { |
| 92 | return projection; |
| 93 | } |
| 94 | |
| 95 | const start = |
| 96 | work === undefined |
| 97 | ? -1 |
| 98 | : projection.visibleTurnItems.findIndex((row) => row.sourceItemId === work.anchorId); |
| 99 | const anchor = projection.visibleTurnItems[start]; |
| 100 | const deferred = anchor?.item.deferredWork; |
| 101 | if (work !== undefined && deferred === undefined) return projection; |
| 102 | const currentByKey = new Map( |
| 103 | projection.visibleTurnItems.map((row) => [projectedItemKey(row), row]), |
| 104 | ); |
| 105 | const turnItemById = new Map(projection.turnItems.map((item) => [String(item.id), item])); |
| 106 | const pageKeys = new Set<string>(); |
| 107 | const page: OrchestrationV2ProjectedTurnItem[] = []; |
| 108 | for (const pageRow of olderItems) { |
| 109 | if ( |
| 110 | work && |
| 111 | anchor && |
| 112 | deferred && |
| 113 | (pageRow.sourceThreadId !== anchor.sourceThreadId || |
| 114 | pageRow.item.ordinal < anchor.item.ordinal || |
| 115 | pageRow.item.ordinal > deferred.lastOrdinal) |
| 116 | ) |
| 117 | continue; |
| 118 | const key = projectedItemKey(pageRow); |
| 119 | if (pageKeys.has(key)) continue; |
| 120 | let row = currentByKey.get(key) ?? pageRow; |
| 121 | if ( |
| 122 | !currentByKey.has(key) && |
| 123 | (row.visibility === "local" || row.sourceThreadId === projection.thread.id) |
| 124 | ) { |
| 125 | const currentItem = turnItemById.get(String(row.sourceItemId)); |
| 126 | // Hidden live items stay hidden. Snapshots also retain interrupt requests and |
| 127 | // pending request cards outside the visible window; their history page shows them. |
| 128 | if (currentItem !== undefined) { |
| 129 | if ( |
| 130 | (currentItem.type !== "run_interrupt_request" && |
| 131 | currentItem.type !== "approval_request" && |
| 132 | currentItem.type !== "user_input_request") || |
| 133 | !isOrchestrationV2TurnItemVisible({ |
| 134 | item: currentItem, |
| 135 | runs: projection.runs, |
| 136 | attempts: projection.attempts, |
| 137 | items: projection.turnItems, |
| 138 | }) |
| 139 | ) |
| 140 | continue; |
| 141 | row = { ...row, item: currentItem }; |
| 142 | } |
| 143 | } |
| 144 | pageKeys.add(key); |
| 145 | page.push(row); |
| 146 | if ( |
| 147 | (row.visibility === "local" || row.sourceThreadId === projection.thread.id) && |
| 148 | !turnItemById.has(String(row.sourceItemId)) |
| 149 | ) |
| 150 | turnItemById.set(String(row.sourceItemId), row.item); |
| 151 | } |
| 152 | if (page.length === 0 && work === undefined) return projection; |
| 153 | let visible: OrchestrationV2ProjectedTurnItem[]; |
| 154 | if (work && anchor && deferred) { |
| 155 | let end = start + 1; |
| 156 | while (end < projection.visibleTurnItems.length) { |
| 157 | const row = projection.visibleTurnItems[end]!; |
| 158 | if ( |
| 159 | row.sourceThreadId !== anchor.sourceThreadId || |
| 160 | row.visibility === "synthetic" || |
| 161 | row.item.ordinal > deferred.lastOrdinal |
| 162 | ) |
| 163 | break; |
| 164 | end += 1; |
| 165 | } |
| 166 | const item = { ...anchor.item }; |
| 167 | if (work.nextCursor === null) delete item.deferredWork; |
| 168 | else item.deferredWork = { ...deferred, cursor: work.nextCursor }; |
| 169 | const updated = { ...anchor, item }; |
| 170 | const section = [ |
| 171 | ...page, |
| 172 | ...projection.visibleTurnItems |
| 173 | .slice(start, end) |
| 174 | .filter((row) => !pageKeys.has(projectedItemKey(row))), |
| 175 | ]; |
| 176 | const ordered = section |
| 177 | .map((row) => (row.sourceItemId === anchor.sourceItemId ? updated : row)) |
| 178 | .sort((left, right) => left.item.ordinal - right.item.ordinal); |
| 179 | visible = [ |
| 180 | ...projection.visibleTurnItems.slice(0, start), |
| 181 | ...ordered, |
| 182 | ...projection.visibleTurnItems.slice(end), |
| 183 | ]; |
| 184 | if (anchor.visibility === "local") turnItemById.set(String(updated.item.id), updated.item); |
| 185 | } else { |
| 186 | visible = interleavePage(projection.visibleTurnItems, page, pageKeys); |
| 187 | } |
| 188 | if ( |
| 189 | visible.length === projection.visibleTurnItems.length && |
| 190 | visible.every((row, index) => row === projection.visibleTurnItems[index]) |
| 191 | ) |
| 192 | return projection; |
| 193 | return { |
| 194 | ...projection, |
| 195 | turnItems: [...turnItemById.values()], |
| 196 | visibleTurnItems: renumberVisible(visible), |
| 197 | }; |
| 198 | } |
| 199 | |
| 200 | /** Adds the records of older subagents; a record already held is newer and stays. */ |
| 201 | export function mergeOlderSubagentsIntoProjection( |
| 202 | projection: OrchestrationV2ThreadProjection, |
| 203 | olderSubagents: ReadonlyArray<OrchestrationV2Subagent> | undefined, |
| 204 | ): OrchestrationV2ThreadProjection { |
| 205 | const held = new Set(projection.subagents.map((agent) => agent.id)); |
| 206 | const added = olderSubagents?.filter((agent) => !held.has(agent.id)) ?? []; |
| 207 | if (added.length === 0) return projection; |
| 208 | return { ...projection, subagents: [...added, ...projection.subagents] }; |
| 209 | } |
| 210 | |
| 211 | export function applyHistoryPageMeta( |
| 212 | current: ThreadHistoryMeta, |
| 213 | page: { |
| 214 | readonly nextCursor: string | null; |
| 215 | readonly hasMoreHistory: boolean; |
| 216 | }, |
| 217 | ): ThreadHistoryMeta { |
| 218 | return { |
| 219 | historyCursor: page.nextCursor, |
| 220 | hasMoreHistory: page.hasMoreHistory, |
| 221 | loading: false, |
| 222 | error: null, |
| 223 | expanded: true, |
| 224 | latestLocalTurnOrdinal: current.latestLocalTurnOrdinal, |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | export function shouldShowLoadEarlierControl(history: ThreadHistoryMeta): boolean { |
| 229 | return history.hasMoreHistory || history.error !== null; |
| 230 | } |