NeonClary's picture
Workspace widget preview, multi-source reference search, and arXiv CORS proxy.
d0878a6 verified
Raw
History Blame Contribute Delete
1.12 kB
/** Resolve the user's stated goal from profile, facts, or guest label. */
const EMPTY = new Set(['', 'n/a', 'na', 'none', 'not specified', 'unknown', 'unspecified']);
export function normalizeGoalText(value) {
if (value == null) return '';
const text = String(value).replace(/\s+/g, ' ').trim();
if (!text || EMPTY.has(text.toLowerCase())) return '';
return text;
}
export function resolveStatedGoal({ profile, facts, user } = {}) {
const fromFacts = (facts || []).find((fact) => {
const key = String(fact?.key || '').toLowerCase();
return key === 'stated_goal' && normalizeGoalText(fact.value);
});
if (fromFacts) return normalizeGoalText(fromFacts.value);
const fromProfile = normalizeGoalText(profile?.current_goals);
if (fromProfile) return fromProfile;
if (user?.is_guest) {
const label = normalizeGoalText(user.guest_label);
if (label) return label;
}
return '';
}
export function goalCacheKey(goal, userId) {
const uid = userId || 'anon';
const g = (goal || '').trim().toLowerCase().slice(0, 180);
return `${uid}::${g}`;
}