116b1f9e4e
🤖 Generated with assistance of OhMyOpenCode
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import type { AutoCompactState, RetryState, TruncateState } from "./types"
|
|
|
|
export function getOrCreateRetryState(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
): RetryState {
|
|
let state = autoCompactState.retryStateBySession.get(sessionID)
|
|
if (!state) {
|
|
state = { attempt: 0, lastAttemptTime: 0, firstAttemptTime: 0 }
|
|
autoCompactState.retryStateBySession.set(sessionID, state)
|
|
}
|
|
return state
|
|
}
|
|
|
|
export function getOrCreateTruncateState(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
): TruncateState {
|
|
let state = autoCompactState.truncateStateBySession.get(sessionID)
|
|
if (!state) {
|
|
state = { truncateAttempt: 0 }
|
|
autoCompactState.truncateStateBySession.set(sessionID, state)
|
|
}
|
|
return state
|
|
}
|
|
|
|
export function clearSessionState(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
): void {
|
|
const retryTimer = autoCompactState.retryTimerBySession.get(sessionID)
|
|
if (retryTimer !== undefined) {
|
|
clearTimeout(retryTimer)
|
|
autoCompactState.retryTimerBySession.delete(sessionID)
|
|
}
|
|
autoCompactState.pendingCompact.delete(sessionID)
|
|
autoCompactState.errorDataBySession.delete(sessionID)
|
|
autoCompactState.retryStateBySession.delete(sessionID)
|
|
autoCompactState.truncateStateBySession.delete(sessionID)
|
|
autoCompactState.emptyContentAttemptBySession.delete(sessionID)
|
|
autoCompactState.compactionInProgress.delete(sessionID)
|
|
}
|
|
|
|
export function setRetryTimer(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
timeout: ReturnType<typeof setTimeout>,
|
|
): void {
|
|
const existingTimer = autoCompactState.retryTimerBySession.get(sessionID)
|
|
if (existingTimer !== undefined) {
|
|
clearTimeout(existingTimer)
|
|
}
|
|
autoCompactState.retryTimerBySession.set(sessionID, timeout)
|
|
}
|
|
|
|
export function clearRetryTimer(autoCompactState: AutoCompactState, sessionID: string): void {
|
|
const retryTimer = autoCompactState.retryTimerBySession.get(sessionID)
|
|
if (retryTimer !== undefined) {
|
|
clearTimeout(retryTimer)
|
|
autoCompactState.retryTimerBySession.delete(sessionID)
|
|
}
|
|
}
|
|
|
|
export function getEmptyContentAttempt(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
): number {
|
|
return autoCompactState.emptyContentAttemptBySession.get(sessionID) ?? 0
|
|
}
|
|
|
|
export function incrementEmptyContentAttempt(
|
|
autoCompactState: AutoCompactState,
|
|
sessionID: string,
|
|
): number {
|
|
const attempt = getEmptyContentAttempt(autoCompactState, sessionID)
|
|
autoCompactState.emptyContentAttemptBySession.set(sessionID, attempt + 1)
|
|
return attempt
|
|
}
|