Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061/** * Decides, from the text a page shows, whether a profile's saved session is the one a script needs (P5-3). It is the logic of scripts/checks/session-check.ts, * kept here so it can be tested without a browser. Three ways to fail, each saying what to do: signed out, signed in as someone else, and not able to tell. */export type SessionVerdict = | { ok: true } /** `kind`: signed-out and wrong-account are a definite disagreement with the profile's account; unknown is the page not showing who is signed in (or no account to compare with). */ | { ok: false; kind: "signed-out" | "wrong-account" | "unknown"; message: string; expected: string; found: string };
const SIGNED_OUT = /\b(?:sign[ -]?in|log[ -]?in|log ?on|connect (?:your )?account)\b/i;/** A handle: labels of letters, digits and hyphens joined by dots, not part of a longer word. */const HANDLE = /(?<![a-z0-9@.-])(@?)([a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+)(?![a-z0-9-])/gi;const SHOWN = 120;
const cut = (s: string): string => (s.length > SHOWN ? `${s.slice(0, SHOWN)}...` : s);
function renewal(profile: string, expected: string): string { return `Renew it: run "aite profile open ${profile}", sign in as ${expected} in the window that opens, close Chrome, and run again (docs/runbook-sessions.md).`;}
export function judgeSession(input: { profile: string; expected: string; pageText: string }): SessionVerdict { const expected = input.expected.replace(/^@/, "").toLowerCase(); if (expected === "") { return { ok: false, kind: "unknown", message: `There is no account to expect for profile "${input.profile}": give the profile an "account" in aite.config.json, or call session-check with the handle.`, expected: "", found: "" }; } const text = input.pageText.replace(/\s+/g, " ").trim(); const handles: Array<{ handle: string; marked: boolean; at: number }> = []; for (const m of text.matchAll(HANDLE)) { const before = text.slice(Math.max(0, (m.index ?? 0) - 16), m.index ?? 0).toLowerCase(); handles.push({ handle: m[2]!.toLowerCase(), marked: m[1] === "@" || /\b(?:as|account:?|user:?|handle:?)\s*$/.test(before), at: m.index ?? 0 }); } const signedOut = SIGNED_OUT.test(text); if (!signedOut && handles.some((h) => h.handle === expected)) return { ok: true }; if (signedOut || text === "") { return { ok: false, kind: "signed-out", message: `The saved session of profile "${input.profile}" is signed out or has expired (the page asks to sign in, and the script needs ${expected}). ${renewal(input.profile, expected)}`, expected, found: "signed out", }; } const other = handles.find((h) => h.marked && h.handle !== expected); if (other !== undefined) { return { ok: false, kind: "wrong-account", message: `Wrong account: the page shows ${cut(other.handle)} but this script needs ${expected} (profile "${input.profile}"). ${renewal(input.profile, expected)}`, expected, found: cut(other.handle), }; } return { ok: false, kind: "unknown", message: `I cannot find which account is signed in on this page (expected ${expected}; the page shows: ${JSON.stringify(cut(text))}). If the session is fine, the page may have changed. ${renewal(input.profile, expected)}`, expected, found: cut(text), };}