|
1 | 1 | import type { AssistantMessage, Context, Model, ProviderReplayState } from "@openclaw/llm-core"; |
| 2 | +import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
2 | 3 | import { shortHash } from "../utils/hash.js"; |
3 | 4 |
|
4 | 5 | const ANTHROPIC_COMPACTION_REPLAY_TYPE = "anthropic-compaction"; |
@@ -85,40 +86,39 @@ function buildAnthropicReplayContext(model: Model, options?: ReplayOpts): Anthro |
85 | 86 | }; |
86 | 87 | } |
87 | 88 |
|
88 | | -function readAnthropicCompactionState( |
89 | | - value: unknown, |
90 | | -): AnthropicCompactionReplayState | AnthropicCompactionSuppressionState | undefined { |
91 | | - if (!value || typeof value !== "object" || Array.isArray(value)) { |
92 | | - return undefined; |
93 | | - } |
94 | | - const state = value as Record<string, unknown>; |
95 | | - const validEnvelope = |
96 | | - state.v === 1 && |
97 | | - typeof state.data === "string" && |
98 | | - typeof state.provider === "string" && |
99 | | - typeof state.api === "string" && |
100 | | - typeof state.model === "string" && |
101 | | - typeof state.baseUrlHash === "string" && |
102 | | - (state.sessionHash === undefined || typeof state.sessionHash === "string") && |
103 | | - (state.authProfileHash === undefined || typeof state.authProfileHash === "string"); |
104 | | - if (!validEnvelope) { |
105 | | - return undefined; |
106 | | - } |
107 | | - const replayState = state as unknown as |
108 | | - | AnthropicCompactionReplayState |
109 | | - | AnthropicCompactionSuppressionState; |
110 | | - if (replayState.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) { |
111 | | - return replayState.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA ? replayState : undefined; |
112 | | - } |
| 89 | +function isAnthropicCompactionState( |
| 90 | + state: Record<string, unknown>, |
| 91 | +): state is Record<string, unknown> & |
| 92 | + (AnthropicCompactionReplayState | AnthropicCompactionSuppressionState) { |
113 | 93 | if ( |
114 | | - replayState.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE || |
115 | | - replayState.data.length === 0 || |
116 | | - (replayState.replayIndex !== undefined && |
117 | | - (!Number.isSafeInteger(replayState.replayIndex) || replayState.replayIndex < 0)) |
| 94 | + state.v !== 1 || |
| 95 | + typeof state.data !== "string" || |
| 96 | + typeof state.provider !== "string" || |
| 97 | + typeof state.api !== "string" || |
| 98 | + typeof state.model !== "string" || |
| 99 | + typeof state.baseUrlHash !== "string" || |
| 100 | + (state.sessionHash !== undefined && typeof state.sessionHash !== "string") || |
| 101 | + (state.authProfileHash !== undefined && typeof state.authProfileHash !== "string") |
118 | 102 | ) { |
119 | | - return undefined; |
| 103 | + return false; |
| 104 | + } |
| 105 | + if (state.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) { |
| 106 | + return state.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA; |
120 | 107 | } |
121 | | - return replayState; |
| 108 | + return ( |
| 109 | + state.type === ANTHROPIC_COMPACTION_REPLAY_TYPE && |
| 110 | + state.data.length > 0 && |
| 111 | + (state.replayIndex === undefined || |
| 112 | + (typeof state.replayIndex === "number" && |
| 113 | + Number.isSafeInteger(state.replayIndex) && |
| 114 | + state.replayIndex >= 0)) |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +function readAnthropicCompactionState( |
| 119 | + value: unknown, |
| 120 | +): AnthropicCompactionReplayState | AnthropicCompactionSuppressionState | undefined { |
| 121 | + return isRecord(value) && isAnthropicCompactionState(value) ? value : undefined; |
122 | 122 | } |
123 | 123 |
|
124 | 124 | function replayContextMatches( |
|
0 commit comments