|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/signature" |
| 10 | + "github.com/tidwall/gjson" |
| 11 | + "github.com/tidwall/sjson" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // CodexReasoningReplayCacheTTL limits how long encrypted reasoning replay |
| 16 | + // items stay in process memory. |
| 17 | + CodexReasoningReplayCacheTTL = 1 * time.Hour |
| 18 | + |
| 19 | + // CodexReasoningReplayCacheMaxEntries bounds process memory for replay |
| 20 | + // continuity. Oldest entries are evicted first. |
| 21 | + CodexReasoningReplayCacheMaxEntries = 10240 |
| 22 | + |
| 23 | + // CodexReasoningReplayCacheEvictBatchSize leaves headroom after the cache |
| 24 | + // reaches capacity so high write volume does not rescan the map every turn. |
| 25 | + CodexReasoningReplayCacheEvictBatchSize = 128 |
| 26 | +) |
| 27 | + |
| 28 | +type codexReasoningReplayEntry struct { |
| 29 | + Items [][]byte |
| 30 | + Timestamp time.Time |
| 31 | +} |
| 32 | + |
| 33 | +var ( |
| 34 | + codexReasoningReplayMu sync.Mutex |
| 35 | + codexReasoningReplayEntries = make(map[string]codexReasoningReplayEntry) |
| 36 | +) |
| 37 | + |
| 38 | +// CacheCodexReasoningReplayItem stores a final GPT/Codex reasoning item for |
| 39 | +// stateless replay. The stored item is normalized to the minimal shape accepted |
| 40 | +// by Responses input replay. |
| 41 | +func CacheCodexReasoningReplayItem(modelName, sessionKey string, item []byte) bool { |
| 42 | + return CacheCodexReasoningReplayItems(modelName, sessionKey, [][]byte{item}) |
| 43 | +} |
| 44 | + |
| 45 | +// CacheCodexReasoningReplayItems stores the final GPT/Codex assistant output |
| 46 | +// items needed to replay a stateless next turn. |
| 47 | +func CacheCodexReasoningReplayItems(modelName, sessionKey string, items [][]byte) bool { |
| 48 | + key := codexReasoningReplayCacheKey(modelName, sessionKey) |
| 49 | + if key == "" { |
| 50 | + return false |
| 51 | + } |
| 52 | + normalized, ok := normalizeCodexReasoningReplayItems(items) |
| 53 | + if !ok { |
| 54 | + return false |
| 55 | + } |
| 56 | + |
| 57 | + cacheCleanupOnce.Do(startCacheCleanup) |
| 58 | + now := time.Now() |
| 59 | + codexReasoningReplayMu.Lock() |
| 60 | + defer codexReasoningReplayMu.Unlock() |
| 61 | + codexReasoningReplayEntries[key] = codexReasoningReplayEntry{ |
| 62 | + Items: normalized, |
| 63 | + Timestamp: now, |
| 64 | + } |
| 65 | + if len(codexReasoningReplayEntries) > CodexReasoningReplayCacheMaxEntries { |
| 66 | + evictOldestCodexReasoningReplayEntries(CodexReasoningReplayCacheEvictBatchSize) |
| 67 | + } |
| 68 | + return true |
| 69 | +} |
| 70 | + |
| 71 | +// GetCodexReasoningReplayItem retrieves a normalized reasoning replay item. |
| 72 | +func GetCodexReasoningReplayItem(modelName, sessionKey string) ([]byte, bool) { |
| 73 | + items, ok := GetCodexReasoningReplayItems(modelName, sessionKey) |
| 74 | + if !ok || len(items) == 0 { |
| 75 | + return nil, false |
| 76 | + } |
| 77 | + return items[0], true |
| 78 | +} |
| 79 | + |
| 80 | +// GetCodexReasoningReplayItems retrieves normalized assistant output items. |
| 81 | +func GetCodexReasoningReplayItems(modelName, sessionKey string) ([][]byte, bool) { |
| 82 | + key := codexReasoningReplayCacheKey(modelName, sessionKey) |
| 83 | + if key == "" { |
| 84 | + return nil, false |
| 85 | + } |
| 86 | + |
| 87 | + cacheCleanupOnce.Do(startCacheCleanup) |
| 88 | + now := time.Now() |
| 89 | + codexReasoningReplayMu.Lock() |
| 90 | + defer codexReasoningReplayMu.Unlock() |
| 91 | + entry, ok := codexReasoningReplayEntries[key] |
| 92 | + if !ok { |
| 93 | + return nil, false |
| 94 | + } |
| 95 | + if now.Sub(entry.Timestamp) > CodexReasoningReplayCacheTTL { |
| 96 | + delete(codexReasoningReplayEntries, key) |
| 97 | + return nil, false |
| 98 | + } |
| 99 | + entry.Timestamp = now |
| 100 | + codexReasoningReplayEntries[key] = entry |
| 101 | + return cloneCodexReasoningReplayItems(entry.Items), true |
| 102 | +} |
| 103 | + |
| 104 | +// DeleteCodexReasoningReplayItem removes one replay item after upstream rejects |
| 105 | +// it or the caller otherwise knows it is stale. |
| 106 | +func DeleteCodexReasoningReplayItem(modelName, sessionKey string) { |
| 107 | + key := codexReasoningReplayCacheKey(modelName, sessionKey) |
| 108 | + if key == "" { |
| 109 | + return |
| 110 | + } |
| 111 | + codexReasoningReplayMu.Lock() |
| 112 | + delete(codexReasoningReplayEntries, key) |
| 113 | + codexReasoningReplayMu.Unlock() |
| 114 | +} |
| 115 | + |
| 116 | +// ClearCodexReasoningReplayCache clears all Codex reasoning replay state. |
| 117 | +func ClearCodexReasoningReplayCache() { |
| 118 | + codexReasoningReplayMu.Lock() |
| 119 | + codexReasoningReplayEntries = make(map[string]codexReasoningReplayEntry) |
| 120 | + codexReasoningReplayMu.Unlock() |
| 121 | +} |
| 122 | + |
| 123 | +func codexReasoningReplayCacheKey(modelName, sessionKey string) string { |
| 124 | + modelName = strings.TrimSpace(modelName) |
| 125 | + sessionKey = strings.TrimSpace(sessionKey) |
| 126 | + if modelName == "" || sessionKey == "" { |
| 127 | + return "" |
| 128 | + } |
| 129 | + // The session key is the continuity boundary. Keep this independent from |
| 130 | + // the selected upstream Codex credential so auth failover can preserve replay. |
| 131 | + return strings.Join([]string{"codex-reasoning-replay", modelName, sessionKey}, "\x00") |
| 132 | +} |
| 133 | + |
| 134 | +func normalizeCodexReasoningReplayItems(items [][]byte) ([][]byte, bool) { |
| 135 | + normalized := make([][]byte, 0, len(items)) |
| 136 | + for _, item := range items { |
| 137 | + normalizedItem, ok := normalizeCodexReasoningReplayItem(item) |
| 138 | + if ok { |
| 139 | + normalized = append(normalized, normalizedItem) |
| 140 | + } |
| 141 | + } |
| 142 | + return normalized, len(normalized) > 0 |
| 143 | +} |
| 144 | + |
| 145 | +func normalizeCodexReasoningReplayItem(item []byte) ([]byte, bool) { |
| 146 | + itemResult := gjson.ParseBytes(item) |
| 147 | + switch strings.TrimSpace(itemResult.Get("type").String()) { |
| 148 | + case "reasoning": |
| 149 | + return normalizeCodexReasoningReplayReasoningItem(itemResult) |
| 150 | + case "function_call": |
| 151 | + return normalizeCodexReasoningReplayFunctionCallItem(itemResult) |
| 152 | + case "custom_tool_call": |
| 153 | + return normalizeCodexReasoningReplayCustomToolCallItem(itemResult) |
| 154 | + default: |
| 155 | + return nil, false |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func normalizeCodexReasoningReplayReasoningItem(itemResult gjson.Result) ([]byte, bool) { |
| 160 | + encryptedContentResult := itemResult.Get("encrypted_content") |
| 161 | + if encryptedContentResult.Type != gjson.String { |
| 162 | + return nil, false |
| 163 | + } |
| 164 | + encryptedContent := encryptedContentResult.String() |
| 165 | + if encryptedContent != strings.TrimSpace(encryptedContent) { |
| 166 | + return nil, false |
| 167 | + } |
| 168 | + if _, err := signature.InspectGPTReasoningSignature(encryptedContent); err != nil { |
| 169 | + return nil, false |
| 170 | + } |
| 171 | + |
| 172 | + normalized := []byte(`{"type":"reasoning","summary":[],"content":null}`) |
| 173 | + normalized, _ = sjson.SetBytes(normalized, "encrypted_content", encryptedContent) |
| 174 | + return normalized, true |
| 175 | +} |
| 176 | + |
| 177 | +func normalizeCodexReasoningReplayFunctionCallItem(itemResult gjson.Result) ([]byte, bool) { |
| 178 | + callID := strings.TrimSpace(itemResult.Get("call_id").String()) |
| 179 | + name := strings.TrimSpace(itemResult.Get("name").String()) |
| 180 | + arguments := itemResult.Get("arguments") |
| 181 | + if callID == "" || name == "" || arguments.Type != gjson.String { |
| 182 | + return nil, false |
| 183 | + } |
| 184 | + |
| 185 | + normalized := []byte(`{"type":"function_call"}`) |
| 186 | + normalized, _ = sjson.SetBytes(normalized, "call_id", callID) |
| 187 | + normalized, _ = sjson.SetBytes(normalized, "name", name) |
| 188 | + normalized, _ = sjson.SetBytes(normalized, "arguments", arguments.String()) |
| 189 | + return normalized, true |
| 190 | +} |
| 191 | + |
| 192 | +func normalizeCodexReasoningReplayCustomToolCallItem(itemResult gjson.Result) ([]byte, bool) { |
| 193 | + callID := strings.TrimSpace(itemResult.Get("call_id").String()) |
| 194 | + name := strings.TrimSpace(itemResult.Get("name").String()) |
| 195 | + input := itemResult.Get("input") |
| 196 | + if callID == "" || name == "" || !input.Exists() { |
| 197 | + return nil, false |
| 198 | + } |
| 199 | + |
| 200 | + normalized := []byte(`{"type":"custom_tool_call","status":"completed"}`) |
| 201 | + if status := strings.TrimSpace(itemResult.Get("status").String()); status != "" { |
| 202 | + normalized, _ = sjson.SetBytes(normalized, "status", status) |
| 203 | + } |
| 204 | + normalized, _ = sjson.SetBytes(normalized, "call_id", callID) |
| 205 | + normalized, _ = sjson.SetBytes(normalized, "name", name) |
| 206 | + if input.Type == gjson.String { |
| 207 | + normalized, _ = sjson.SetBytes(normalized, "input", input.String()) |
| 208 | + } else { |
| 209 | + normalized, _ = sjson.SetRawBytes(normalized, "input", []byte(input.Raw)) |
| 210 | + } |
| 211 | + return normalized, true |
| 212 | +} |
| 213 | + |
| 214 | +func cloneCodexReasoningReplayItems(items [][]byte) [][]byte { |
| 215 | + cloned := make([][]byte, 0, len(items)) |
| 216 | + for _, item := range items { |
| 217 | + cloned = append(cloned, append([]byte(nil), item...)) |
| 218 | + } |
| 219 | + return cloned |
| 220 | +} |
| 221 | + |
| 222 | +func evictOldestCodexReasoningReplayEntries(count int) { |
| 223 | + if count <= 0 || len(codexReasoningReplayEntries) == 0 { |
| 224 | + return |
| 225 | + } |
| 226 | + type candidate struct { |
| 227 | + key string |
| 228 | + timestamp time.Time |
| 229 | + } |
| 230 | + candidates := make([]candidate, 0, len(codexReasoningReplayEntries)) |
| 231 | + for key, entry := range codexReasoningReplayEntries { |
| 232 | + candidates = append(candidates, candidate{key: key, timestamp: entry.Timestamp}) |
| 233 | + } |
| 234 | + sort.Slice(candidates, func(i, j int) bool { |
| 235 | + return candidates[i].timestamp.Before(candidates[j].timestamp) |
| 236 | + }) |
| 237 | + if count > len(candidates) { |
| 238 | + count = len(candidates) |
| 239 | + } |
| 240 | + for i := 0; i < count; i++ { |
| 241 | + delete(codexReasoningReplayEntries, candidates[i].key) |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +func purgeExpiredCodexReasoningReplayCache(now time.Time) { |
| 246 | + codexReasoningReplayMu.Lock() |
| 247 | + for key, entry := range codexReasoningReplayEntries { |
| 248 | + if now.Sub(entry.Timestamp) > CodexReasoningReplayCacheTTL { |
| 249 | + delete(codexReasoningReplayEntries, key) |
| 250 | + } |
| 251 | + } |
| 252 | + codexReasoningReplayMu.Unlock() |
| 253 | +} |
0 commit comments