|
| 1 | +import type { ChatAttachment } from "../../lib/chat/chat-types.ts"; |
| 2 | +import { |
| 3 | + DEFAULT_MAIN_KEY, |
| 4 | + buildAgentMainSessionKey, |
| 5 | + resolveUiConfiguredMainKey, |
| 6 | + resolveUiDefaultAgentId, |
| 7 | + resolveUiKnownSelectedGlobalAgentId, |
| 8 | +} from "../../lib/sessions/session-key.ts"; |
| 9 | +import type { ChatComposerMemoryFallback, ChatPageHost } from "./chat-state-host.ts"; |
| 10 | +import { |
| 11 | + loadChatComposerCommittedDraftRevision, |
| 12 | + loadChatComposerDraftRevision, |
| 13 | + resolveStoredChatOutboxScope, |
| 14 | + storedChatOutboxScopeKey, |
| 15 | + type ChatComposerDraftRetry, |
| 16 | + type StoredChatOutboxScope, |
| 17 | +} from "./composer-persistence.ts"; |
| 18 | + |
| 19 | +let lastChatComposerMemoryFallbackSequence = 0; |
| 20 | + |
| 21 | +export type ChatComposerMemoryFallbackOwnership = { |
| 22 | + sequence: number; |
| 23 | +}; |
| 24 | + |
| 25 | +export function resolveChatComposerMemoryFallback( |
| 26 | + state: ChatPageHost, |
| 27 | + sessionKey: string, |
| 28 | + scopeOverride?: StoredChatOutboxScope, |
| 29 | +): { fallback?: ChatComposerMemoryFallback; scopeKey: string } { |
| 30 | + const scope = scopeOverride ?? resolveStoredChatOutboxScope(state, sessionKey); |
| 31 | + const scopeKey = storedChatOutboxScopeKey(scope); |
| 32 | + const fallback = state.chatComposerFallbackByScope[scopeKey]; |
| 33 | + const selectedGlobalAgentId = resolveUiKnownSelectedGlobalAgentId(state); |
| 34 | + if (scope.sessionKey !== "global" || !scope.agentId) { |
| 35 | + return { fallback, scopeKey }; |
| 36 | + } |
| 37 | + const configuredMainKey = resolveUiConfiguredMainKey(state); |
| 38 | + const isSelectedTarget = scope.agentId === selectedGlobalAgentId; |
| 39 | + const isDefaultTarget = scope.agentId === resolveUiDefaultAgentId(state); |
| 40 | + const qualifiedMainScopeKey = |
| 41 | + configuredMainKey === DEFAULT_MAIN_KEY |
| 42 | + ? undefined |
| 43 | + : storedChatOutboxScopeKey({ |
| 44 | + sessionKey: buildAgentMainSessionKey({ |
| 45 | + agentId: scope.agentId, |
| 46 | + mainKey: configuredMainKey, |
| 47 | + }), |
| 48 | + agentId: scope.agentId, |
| 49 | + }); |
| 50 | + if (!isSelectedTarget && !isDefaultTarget && !qualifiedMainScopeKey) { |
| 51 | + return { fallback, scopeKey }; |
| 52 | + } |
| 53 | + const fallbackSourceKeys = new Set([scopeKey]); |
| 54 | + if (isSelectedTarget) { |
| 55 | + fallbackSourceKeys.add(storedChatOutboxScopeKey({ sessionKey: "global" })); |
| 56 | + } |
| 57 | + if (isDefaultTarget) { |
| 58 | + fallbackSourceKeys.add(storedChatOutboxScopeKey({ sessionKey: DEFAULT_MAIN_KEY })); |
| 59 | + fallbackSourceKeys.add(storedChatOutboxScopeKey({ sessionKey: configuredMainKey })); |
| 60 | + } |
| 61 | + if (qualifiedMainScopeKey) { |
| 62 | + fallbackSourceKeys.add(qualifiedMainScopeKey); |
| 63 | + } |
| 64 | + const candidates = [...fallbackSourceKeys] |
| 65 | + .map((candidateScopeKey) => ({ |
| 66 | + fallback: state.chatComposerFallbackByScope[candidateScopeKey], |
| 67 | + scopeKey: candidateScopeKey, |
| 68 | + })) |
| 69 | + .filter( |
| 70 | + (candidate): candidate is { fallback: ChatComposerMemoryFallback; scopeKey: string } => |
| 71 | + candidate.fallback !== undefined, |
| 72 | + ); |
| 73 | + const newest = candidates.toSorted( |
| 74 | + (left, right) => right.fallback.sequence - left.fallback.sequence, |
| 75 | + )[0]; |
| 76 | + if (!newest) { |
| 77 | + return { scopeKey }; |
| 78 | + } |
| 79 | + const sourceKey = newest.scopeKey; |
| 80 | + const sourceFallback = newest.fallback; |
| 81 | + if (candidates.length === 1 && sourceKey === scopeKey) { |
| 82 | + return { fallback: sourceFallback, scopeKey }; |
| 83 | + } |
| 84 | + let adoptedFallback = sourceFallback; |
| 85 | + if (sourceKey !== scopeKey && sourceFallback.draftRetry) { |
| 86 | + const committedRevision = loadChatComposerCommittedDraftRevision( |
| 87 | + state, |
| 88 | + sessionKey, |
| 89 | + scope.agentId, |
| 90 | + ); |
| 91 | + const latestRevision = loadChatComposerDraftRevision(state, sessionKey, scope.agentId); |
| 92 | + // Rebase only when this unresolved edit is newer than every resolved |
| 93 | + // attempt. Otherwise its original CAS must keep newer pane input intact. |
| 94 | + if (sourceFallback.draftRetry.draftRevision > latestRevision) { |
| 95 | + adoptedFallback = { |
| 96 | + ...sourceFallback, |
| 97 | + draftRetry: { |
| 98 | + ...sourceFallback.draftRetry, |
| 99 | + expectedDraftRevision: committedRevision, |
| 100 | + }, |
| 101 | + }; |
| 102 | + } |
| 103 | + } |
| 104 | + const nextFallbacks = { ...state.chatComposerFallbackByScope }; |
| 105 | + for (const candidate of candidates) { |
| 106 | + delete nextFallbacks[candidate.scopeKey]; |
| 107 | + } |
| 108 | + nextFallbacks[scopeKey] = adoptedFallback; |
| 109 | + state.chatComposerFallbackByScope = nextFallbacks; |
| 110 | + return { fallback: adoptedFallback, scopeKey }; |
| 111 | +} |
| 112 | + |
| 113 | +export function storeChatComposerMemoryFallback( |
| 114 | + state: ChatPageHost, |
| 115 | + scope: StoredChatOutboxScope, |
| 116 | + composer: { |
| 117 | + message: string; |
| 118 | + attachments: ChatAttachment[]; |
| 119 | + draftRetry?: ChatComposerDraftRetry; |
| 120 | + }, |
| 121 | +): ChatComposerMemoryFallbackOwnership { |
| 122 | + const sequence = ++lastChatComposerMemoryFallbackSequence; |
| 123 | + state.chatComposerFallbackByScope = { |
| 124 | + ...state.chatComposerFallbackByScope, |
| 125 | + [storedChatOutboxScopeKey(scope)]: { |
| 126 | + message: composer.message, |
| 127 | + attachments: [...composer.attachments], |
| 128 | + storageFailed: composer.draftRetry !== undefined, |
| 129 | + sequence, |
| 130 | + ...(composer.draftRetry ? { draftRetry: composer.draftRetry } : {}), |
| 131 | + }, |
| 132 | + }; |
| 133 | + return { sequence }; |
| 134 | +} |
| 135 | + |
| 136 | +function chatAttachmentsMatch( |
| 137 | + left: readonly ChatAttachment[], |
| 138 | + right: readonly ChatAttachment[], |
| 139 | +): boolean { |
| 140 | + return ( |
| 141 | + left.length === right.length && |
| 142 | + left.every((attachment, index) => attachment.id === right[index]?.id) |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +export function retainChatComposerMemoryFallback( |
| 147 | + state: ChatPageHost, |
| 148 | + scope: StoredChatOutboxScope, |
| 149 | + composer: { message: string; attachments: ChatAttachment[] }, |
| 150 | +): ChatComposerMemoryFallbackOwnership | undefined { |
| 151 | + const { fallback: existing, scopeKey } = resolveChatComposerMemoryFallback( |
| 152 | + state, |
| 153 | + scope.sessionKey, |
| 154 | + scope, |
| 155 | + ); |
| 156 | + const existingMatches = |
| 157 | + existing?.message === composer.message && |
| 158 | + chatAttachmentsMatch(existing.attachments, composer.attachments); |
| 159 | + if (existing && existingMatches) { |
| 160 | + return { sequence: existing.sequence }; |
| 161 | + } |
| 162 | + if (existing?.storageFailed && !existing.message.trim() && existing.attachments.length === 0) { |
| 163 | + state.chatComposerFallbackByScope = { |
| 164 | + ...state.chatComposerFallbackByScope, |
| 165 | + [scopeKey]: { |
| 166 | + ...existing, |
| 167 | + message: composer.message, |
| 168 | + attachments: [...composer.attachments], |
| 169 | + }, |
| 170 | + }; |
| 171 | + return { sequence: existing.sequence }; |
| 172 | + } |
| 173 | + if ( |
| 174 | + existing && |
| 175 | + (existing.storageFailed || existing.message.trim() || existing.attachments.length > 0) |
| 176 | + ) { |
| 177 | + return undefined; |
| 178 | + } |
| 179 | + return storeChatComposerMemoryFallback(state, scope, composer); |
| 180 | +} |
| 181 | + |
| 182 | +export function captureChatComposerMemoryFallbackOwnership( |
| 183 | + state: ChatPageHost, |
| 184 | + scope: StoredChatOutboxScope, |
| 185 | + composer: { message: string; attachments: ChatAttachment[] }, |
| 186 | +): ChatComposerMemoryFallbackOwnership | undefined { |
| 187 | + const { fallback: existing } = resolveChatComposerMemoryFallback(state, scope.sessionKey, scope); |
| 188 | + if ( |
| 189 | + existing?.message !== composer.message || |
| 190 | + !chatAttachmentsMatch(existing.attachments, composer.attachments) |
| 191 | + ) { |
| 192 | + return undefined; |
| 193 | + } |
| 194 | + return { sequence: existing.sequence }; |
| 195 | +} |
| 196 | + |
| 197 | +export function ownsChatComposerMemoryFallback( |
| 198 | + state: Pick<ChatPageHost, "chatComposerFallbackByScope">, |
| 199 | + ownership: ChatComposerMemoryFallbackOwnership, |
| 200 | +): boolean { |
| 201 | + return Object.values(state.chatComposerFallbackByScope).some( |
| 202 | + (fallback) => fallback.sequence === ownership.sequence, |
| 203 | + ); |
| 204 | +} |
| 205 | + |
| 206 | +export function clearChatComposerMemoryFallback( |
| 207 | + state: Pick<ChatPageHost, "chatComposerFallbackByScope">, |
| 208 | + ownership: ChatComposerMemoryFallbackOwnership | undefined, |
| 209 | +): boolean { |
| 210 | + if (!ownership) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + const ownedEntries = Object.entries(state.chatComposerFallbackByScope).filter( |
| 214 | + ([, fallback]) => fallback.sequence === ownership.sequence, |
| 215 | + ); |
| 216 | + if (ownedEntries.length === 0) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + const nextFallbacks = { ...state.chatComposerFallbackByScope }; |
| 220 | + for (const [scopeKey] of ownedEntries) { |
| 221 | + delete nextFallbacks[scopeKey]; |
| 222 | + } |
| 223 | + state.chatComposerFallbackByScope = nextFallbacks; |
| 224 | + return true; |
| 225 | +} |
0 commit comments