Files
memind/user-model-service/canonical-user.mjs
T
john 212ff3ff80 Add User Model Service and Temporal Recall for MeMind V0.1.
Introduce UMS ingest/snapshot pipeline, Context Planner with multi-source recall, runtime context injection, canonical user mapping, and session snapshot loading on auth/me.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-03 23:25:18 +08:00

51 lines
1.2 KiB
JavaScript

/**
* Resolve login user_id → canonical user_id for UMS / Temporal Recall / MeInput queries.
*
* Env MEMIND_CANONICAL_USER_MAP:
* from=to,from2=to2
*
* Default: tang19821002 → 唐 (wx_ul610et8)
*/
const DEFAULT_MAP = new Map([
[
'd0678bbc-2a50-4e08-8bf0-6b6c9301e2d6',
'a70ff537-8908-486e-9b6c-042e07cc25db',
],
]);
let cachedMap = null;
function parseCanonicalUserMap(raw) {
const map = new Map(DEFAULT_MAP);
const text = String(raw ?? '').trim();
if (!text) return map;
for (const part of text.split(',')) {
const pair = part.trim();
if (!pair) continue;
const eq = pair.indexOf('=');
if (eq <= 0) continue;
const from = pair.slice(0, eq).trim();
const to = pair.slice(eq + 1).trim();
if (from && to) map.set(from, to);
}
return map;
}
function canonicalMap() {
if (!cachedMap) {
cachedMap = parseCanonicalUserMap(process.env.MEMIND_CANONICAL_USER_MAP);
}
return cachedMap;
}
/** @param {string | null | undefined} userId */
export function resolveCanonicalUserId(userId) {
const id = String(userId ?? '').trim();
if (!id) return id;
return canonicalMap().get(id) ?? id;
}
export function resetCanonicalUserMapCache() {
cachedMap = null;
}