(facts: string[], opts: MemorySelectOptions = {})
| 50 | * newest others until the token budget is exhausted — preserving the input order. |
| 51 | */ |
| 52 | export function selectInjectedFacts(facts: string[], opts: MemorySelectOptions = {}): string[] { |
| 53 | if (opts.mode !== 'lightweight') return facts; |
| 54 | const budget = opts.injectMaxTokens ?? 2000; |
| 55 | const out: string[] = []; |
| 56 | let spent = 0; |
| 57 | // Important facts are always in and don't draw from the budget. |
| 58 | for (const f of facts) if (isImportantFact(f)) out.push(f); |
| 59 | // Fill the rest newest-first until the budget runs out, keeping the original ordering in the result. |
| 60 | const kept = new Set(out); |
| 61 | const filler: string[] = []; |
| 62 | for (const f of facts) { |
| 63 | if (kept.has(f)) continue; |
| 64 | const cost = estimateTokens(f); |
| 65 | if (spent + cost > budget) continue; |
| 66 | spent += cost; filler.push(f); kept.add(f); |
| 67 | } |
| 68 | // Return in the original (newest-first) order, important + budgeted-filler interleaved as they appeared. |
| 69 | return facts.filter(f => kept.has(f)); |
| 70 | } |
no test coverage detected