openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 3.08 kB
JavaScript
//!extensions/memory-core/src/memory-budget.ts
const PROMOTION_SECTION_HEADING_RE=/^## Promoted From Short-Term Memory \(([^)]+)\)\s*$/;const PROMOTION_SUBSECTION_HEADING_RE=/^### (?:Global|Project: .+?)\s*$/;const PROMOTION_ENTRY_MARKER_RE=/^<!--\s*openclaw-memory-promotion:.*-->\s*$/i;const GENERATED_PROMOTION_ENTRY_MARKER_RE=/^<!--\s*openclaw-memory-promotion:(?:(?!-->).)*-->\s*$/i;const ATX_HEADING_RE=/^ {0,3}#{1,6}(?:[ \t]|$)/;const SETEXT_HEADING_UNDERLINE_RE=/^ {0,3}(?:=+|-+)[ \t]*$/;const DEFAULT_MEMORY_FILE_MAX_CHARS=1e4;const WRITE_OVERHEAD_RESERVE=21;function isGeneratedPromotionBlock(e){let t=false;let n=1;while(n<e.length){const r=e[n]??``;if(r.trim().length===0){n+=1;continue}if(PROMOTION_SUBSECTION_HEADING_RE.test(r)){n+=1;while(n<e.length&&(e[n]??``).trim().length===0)n+=1}if(!GENERATED_PROMOTION_ENTRY_MARKER_RE.test(e[n]??``))return false;if(!(e[n+1]??``).startsWith(`- `))return false;t=true;n+=2}return t}function startsGeneratedPromotionSubsection(e,t){if(!PROMOTION_SUBSECTION_HEADING_RE.test(e[t]??``))return false;for(let n=t+1;n<e.length;n+=1){const t=e[n]??``;if(t.trim().length===0)continue;return PROMOTION_ENTRY_MARKER_RE.test(t)}return false}function takeSetextHeadingLines(e){let t=e.length;while(t>1&&e[t-1]?.trim().length!==0)t-=1;if(t===e.length)return;const n=e.slice(t);if(n.some(e=>PROMOTION_ENTRY_MARKER_RE.test(e)))return;e.splice(t);return n}function parseMemoryBlocks(e){if(e.length===0)return[];const t=e.split(/\r?\n/);const n=[];let r=[];let i;const flush=()=>{if(r.length===0)return;const e=r.join(`
`);if(i&&isGeneratedPromotionBlock(r))n.push({kind:`promotion`,date:i,text:e,entryCount:r.filter(e=>PROMOTION_ENTRY_MARKER_RE.test(e)).length});else n.push({kind:`preserved`,text:e});r=[];i=void 0};for(const[e,n]of t.entries()){if(i&&SETEXT_HEADING_UNDERLINE_RE.test(n)){const e=takeSetextHeadingLines(r);if(e){flush();r=[...e,n];continue}}const a=i&&startsGeneratedPromotionSubsection(t,e);if(ATX_HEADING_RE.test(n)&&!a){flush();i=PROMOTION_SECTION_HEADING_RE.exec(n)?.[1];r=[n]}else r.push(n)}flush();return n}function compactMemoryForBudget(e){const{existingMemory:t,newSection:n,budgetChars:r}=e;if(r<=0)return{compacted:t,droppedDates:[]};const i=Math.max(0,r-WRITE_OVERHEAD_RESERVE);if(t.length+n.length<=i)return{compacted:t,droppedDates:[]};const a=parseMemoryBlocks(t);const o=a.flatMap((e,t)=>e.kind===`promotion`?[{block:e,index:t}]:[]).toSorted((e,t)=>e.block.date.localeCompare(t.block.date));if(o.length===0)return{compacted:t,droppedDates:[]};const s=/* @__PURE__ */ new Set;const c=[];const l=o.reduce((e,{block:t})=>e+t.entryCount,0);const u=Math.max(0,Math.min(1,e.maxPriorEntryLossFraction??1));let d=0;let f=t.length;const p=a.length>1?1:0;for(const{block:e,index:t}of o){if(f+n.length<=i)break;if(l>0&&(d+e.entryCount)/l>u)break;s.add(t);c.push(e.date);d+=e.entryCount;f=Math.max(0,f-e.text.length-p)}if(s.size===0)return{compacted:t,droppedDates:[]};return{compacted:a.filter((e,t)=>!s.has(t)).map(e=>e.text).join(`
`),droppedDates:c}}export{compactMemoryForBudget as n,DEFAULT_MEMORY_FILE_MAX_CHARS as t};