UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

102 lines (101 loc) • 2.99 kB
//#region packages/memory-host-sdk/src/host/curated-annotations.ts const INVALID_PROJECT_ANNOTATION_KEY = "!invalid-project-annotation"; function* scanMemoryAnnotations(e, t, n = false) { let r = -1; let i = -1; const a = /[\r\n]/gu; for (let o = t.exec(e); o; o = t.exec(e)) { const s = t.lastIndex; if (r < s) { r = e.indexOf("-->", s); if (r < 0) return; } if (n) { if (i < s) { a.lastIndex = s; i = a.exec(e)?.index ?? e.length; } if (i < r) continue; } yield { start: o.index, end: r + 3, kind: o[1]?.toLowerCase(), value: e.slice(s, r).trim() }; t.lastIndex = r + 3; } } function stripMemoryAnnotationCarriers(e) { const t = []; let n = 0; for (const r of scanMemoryAnnotations(e, /<!--\s*(trigger|importance|project)\s*:/giu, true)) { t.push(e.slice(n, r.start)); n = r.end; } if (n === 0) return e; t.push(e.slice(n)); return t.join("").replace(/[ \t]+/gu, (e, t, n) => { const r = t + e.length; return r === n.length || /[\r\n\u2028\u2029]/u.test(n.charAt(r)) ? "" : e; }); } function normalizeProjectAnnotationKey(e) { const t = e.trim(); if (!t || /[\r\n<>]/u.test(t)) return null; if (t.startsWith("path:")) return t; const n = t.indexOf("/"); if (n < 1) return t; return `${t.slice(0, n).toLowerCase()}${t.slice(n)}`; } function extractProjectKeysFromCuratedEntry(e) { const t = /* @__PURE__ */ new Set(); const n = [...e.matchAll(/<!--\s*project\s*:/giu)].length; let r = 0; let i = 0; let a = 0; for (const n of scanMemoryAnnotations(e, /<!--\s*(project)\s*:/giu)) { r += 1; for (const e of n.value.split(";")) { i += 1; const n = normalizeProjectAnnotationKey(e); if (n) { t.add(n); a += 1; } } } const o = n > 0; return { annotated: o, valid: !o || r === n && i > 0 && i === a, keys: [...t], rawCount: i, validCount: a }; } function extractCuratedEntryRecallMetadata(e) { const t = /* @__PURE__ */ new Set(); let n = null; const r = e.projectScopeEligible ? extractProjectKeysFromCuratedEntry(e.sourceLines.join("\n")) : { annotated: false, valid: true, keys: [] }; for (const r of e.curatedRoot ? e.sourceLines : []) { if (!r.trimEnd().endsWith("-->")) continue; for (const { kind: e, value: i } of scanMemoryAnnotations(r, /<!--\s*(trigger|importance|project)\s*:/giu)) if (e === "trigger") { for (const e of i.split(/[,;]/u).map((e) => e.trim())) if (e) t.add(e); } else if (e === "importance" && /^\d+$/u.test(i)) { const e = Number.parseInt(i, 10); if (e >= 1 && e <= 10) n = Math.max(n ?? e, e); } } return { importance: n, triggers: t.size > 0 ? [...t].join("; ") : null, projectKey: r.annotated && !r.valid ? INVALID_PROJECT_ANNOTATION_KEY : r.keys.length > 0 ? r.keys.join("; ") : null }; } //#endregion export { stripMemoryAnnotationCarriers as a, normalizeProjectAnnotationKey as i, extractCuratedEntryRecallMetadata as n, extractProjectKeysFromCuratedEntry as r, INVALID_PROJECT_ANNOTATION_KEY as t };