MCPcopy Index your code
hub / github.com/Opencode-DCP/opencode-dynamic-context-pruning / loadPruneMessagesState

Function loadPruneMessagesState

lib/state/utils.ts:118–289  ·  view source on GitHub ↗
(
    persisted?: PersistedPruneMessagesState,
)

Source from the content-addressed store, hash-verified

116}
117
118export function loadPruneMessagesState(
119 persisted?: PersistedPruneMessagesState,
120): PruneMessagesState {
121 const state = createPruneMessagesState()
122 if (!persisted || typeof persisted !== "object") {
123 return state
124 }
125
126 if (typeof persisted.nextBlockId === "number" && Number.isInteger(persisted.nextBlockId)) {
127 state.nextBlockId = Math.max(1, persisted.nextBlockId)
128 }
129 if (typeof persisted.nextRunId === "number" && Number.isInteger(persisted.nextRunId)) {
130 state.nextRunId = Math.max(1, persisted.nextRunId)
131 }
132
133 if (persisted.byMessageId && typeof persisted.byMessageId === "object") {
134 for (const [messageId, entry] of Object.entries(persisted.byMessageId)) {
135 if (!entry || typeof entry !== "object") {
136 continue
137 }
138
139 const tokenCount = typeof entry.tokenCount === "number" ? entry.tokenCount : 0
140 const allBlockIds = Array.isArray(entry.allBlockIds)
141 ? [
142 ...new Set(
143 entry.allBlockIds.filter(
144 (id): id is number => Number.isInteger(id) && id > 0,
145 ),
146 ),
147 ]
148 : []
149 const activeBlockIds = Array.isArray(entry.activeBlockIds)
150 ? [
151 ...new Set(
152 entry.activeBlockIds.filter(
153 (id): id is number => Number.isInteger(id) && id > 0,
154 ),
155 ),
156 ]
157 : []
158
159 state.byMessageId.set(messageId, {
160 tokenCount,
161 allBlockIds,
162 activeBlockIds,
163 })
164 }
165 }
166
167 if (persisted.blocksById && typeof persisted.blocksById === "object") {
168 for (const [blockIdStr, block] of Object.entries(persisted.blocksById)) {
169 const blockId = Number.parseInt(blockIdStr, 10)
170 if (!Number.isInteger(blockId) || blockId < 1 || !block || typeof block !== "object") {
171 continue
172 }
173
174 const toNumberArray = (value: unknown): number[] =>
175 Array.isArray(value)

Callers 2

buildSessionStateFunction · 0.90
ensureSessionInitializedFunction · 0.90

Calls 4

countTokensFunction · 0.90
createPruneMessagesStateFunction · 0.85
toNumberArrayFunction · 0.85
toStringArrayFunction · 0.85

Tested by

no test coverage detected