feat(h5): inject direct-chat history on agent escalation and defer ambiguous routing.
Phase B appends snapshot context when leaving h5direct sessions so Goose no longer starts blind. Phase C lets the 0.72 fallback return null on gated chat text so the existing LLM router can run, flags default off. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
function envFlag(value, fallback = false) {
|
||||
const raw = String(value ?? '').trim().toLowerCase();
|
||||
if (!raw) return fallback;
|
||||
return ['1', 'true', 'yes', 'on'].includes(raw);
|
||||
}
|
||||
|
||||
function parseUserIdSet(raw) {
|
||||
return new Set(
|
||||
String(raw ?? '')
|
||||
.split(/[,;\s]+/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
}
|
||||
|
||||
function boundedNumber(value, fallback, { min = 0, max = Number.MAX_SAFE_INTEGER } = {}) {
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed)) return fallback;
|
||||
return Math.min(max, Math.max(min, parsed));
|
||||
}
|
||||
|
||||
export function resolveDirectEscalationContextPolicy(env = process.env) {
|
||||
return {
|
||||
enabled: envFlag(env?.MEMIND_DIRECT_ESCALATION_CONTEXT_ENABLED, false),
|
||||
canaryUserIds: parseUserIdSet(env?.MEMIND_DIRECT_ESCALATION_CONTEXT_CANARY_USER_IDS),
|
||||
};
|
||||
}
|
||||
|
||||
export function isDirectEscalationContextEnabledForUser(userId, policy) {
|
||||
if (!policy?.enabled) return false;
|
||||
if (!policy.canaryUserIds?.size) return true;
|
||||
return policy.canaryUserIds.has(String(userId ?? '').trim());
|
||||
}
|
||||
|
||||
export function resolveChatSessionDeferPolicy(env = process.env) {
|
||||
return {
|
||||
enabled: envFlag(env?.MEMIND_CHAT_ROUTER_CHAT_SESSION_DEFER_ENABLED, false),
|
||||
canaryUserIds: parseUserIdSet(env?.MEMIND_CHAT_ROUTER_CHAT_SESSION_DEFER_CANARY_USER_IDS),
|
||||
minTextLength: Math.round(boundedNumber(
|
||||
env?.MEMIND_CHAT_ROUTER_CHAT_SESSION_DEFER_MIN_TEXT_LENGTH,
|
||||
12,
|
||||
{ min: 4, max: 200 },
|
||||
)),
|
||||
};
|
||||
}
|
||||
|
||||
export function isChatSessionDeferEnabledForUser(userId, policy) {
|
||||
if (!policy?.enabled) return false;
|
||||
if (!policy.canaryUserIds?.size) return true;
|
||||
return policy.canaryUserIds.has(String(userId ?? '').trim());
|
||||
}
|
||||
|
||||
export function shouldDeferChatSessionRoutingToLlm({
|
||||
enabled = false,
|
||||
text = '',
|
||||
minTextLength = 12,
|
||||
isExplicitDirectChatOnlyText = () => false,
|
||||
} = {}) {
|
||||
if (!enabled) return false;
|
||||
const normalized = String(text ?? '').trim();
|
||||
if (!normalized || isExplicitDirectChatOnlyText(normalized)) return false;
|
||||
return normalized.length >= Math.max(1, Number(minTextLength) || 12);
|
||||
}
|
||||
Reference in New Issue
Block a user