open-memex
Version:
Local-first memory layer and protocol for AI coding agents. Markdown source of truth, SQLite FTS5 index, zero cloud.
40 lines (38 loc) • 1.35 kB
text/typescript
/**
* Profile-section selection for the session-start injection block (D98).
*
* Pure: no DB, no I/O. The profile list is capped (`maxProfileItems`,
* default 5) and the raw order is newest-first, so without this the oldest
* preference is the first thing a sixth personal memory pushes out of the
* block — silently (issue #92 part 2). Preferences are the load-bearing
* part of the profile section (reply language, doc language, conventions),
* so they go first; every other type fills the remaining slots in recency
* order. Both inputs are already visible, deduped-by-chain lists from
* `list()`; the merge only reorders and caps, never re-sorts within a
* group, so the newest-first order inside each group is preserved.
*/
export interface ProfileItem {
id: string;
type: string;
}
export function selectProfileItems<T extends ProfileItem>(
preferences: readonly T[],
allNewestFirst: readonly T[],
cap: number,
): T[] {
const out: T[] = [];
const seen = new Set<string>();
for (const m of preferences) {
if (out.length >= cap) break;
if (seen.has(m.id)) continue;
seen.add(m.id);
out.push(m);
}
for (const m of allNewestFirst) {
if (out.length >= cap) break;
if (seen.has(m.id)) continue;
seen.add(m.id);
out.push(m);
}
return out;
}