/** * Interactive identity setup — prompts for missing fields in pal-settings.json. * Called during `pal install`. Skips fields that already have values. */ import * as clack from "@clack/prompts"; import { type PalSettingsData, raw as readSettings, write as writeSettings, } from "../hooks/lib/settings"; import { isValidTimeZone } from "../hooks/lib/wall-clock"; /** * Empty means the machine's guess, which clack substitutes after validation runs, * so pressing Enter has to pass. * @lintignore exercised directly by test/setup-identity.test.ts */ export function timezoneProblem(input: string | undefined): string | undefined { if (!input || isValidTimeZone(input)) return undefined; return `Not a timezone Intl recognises: ${input}`; } /** Prompt for missing identity fields; true when it asked anything. */ export async function promptIdentity(): Promise { // Skip interactive prompts in non-TTY environments (tests, CI) if (!process.stdin.isTTY) return false; const settings: PalSettingsData = { ...readSettings() }; settings.identity ??= {}; settings.identity.ai ??= {}; settings.identity.principal ??= {}; const ai = settings.identity.ai; const principal = settings.identity.principal; // Check if anything is missing const needsPrincipal = !principal.name; const needsAi = !ai.name; const needsCatchphrase = !ai.catchphrase; const needsTimezone = !principal.timezone; if (!needsPrincipal && !needsAi && !needsCatchphrase && !needsTimezone) return false; if (needsPrincipal && (await cancelledAskingPrincipalName(principal))) return true; if (needsAi && (await cancelledAskingAiName(ai))) return true; if (needsCatchphrase && (await cancelledAskingCatchphrase(ai))) return true; if (needsTimezone && (await cancelledAskingTimezone(principal))) return true; writeSettings(settings); return true; } type Identity = NonNullable; type AiIdentity = NonNullable; type PrincipalIdentity = NonNullable; async function askText(opts: Parameters[0]): Promise { const value = await clack.text(opts); if (clack.isCancel(value)) { clack.cancel("Setup cancelled"); return null; } return value; } async function cancelledAskingPrincipalName( principal: PrincipalIdentity ): Promise { const name = await askText({ message: "What's your name?", placeholder: "e.g. John", validate: (v) => (!v || v.length === 0 ? "Name is required" : undefined), }); if (name === null) return true; principal.name = name; return false; } async function cancelledAskingAiName(ai: AiIdentity): Promise { const name = await askText({ message: "Name your assistant", defaultValue: "Assistant", placeholder: "e.g. Jarvis, Friday, Atlas", }); if (name === null) return true; ai.name = name; ai.fullName = `${name} — Personal AI`; ai.displayName = name.toUpperCase(); return false; } async function cancelledAskingCatchphrase(ai: AiIdentity): Promise { const catchphrase = await askText({ message: "Startup catchphrase ({name} gets replaced with AI name)", defaultValue: "{name} here, ready when you are.", placeholder: "{name} online. What's the mission?", }); if (catchphrase === null) return true; ai.catchphrase = catchphrase; return false; } async function cancelledAskingTimezone(principal: PrincipalIdentity): Promise { const guess = Intl.DateTimeFormat().resolvedOptions().timeZone; const tz = await askText({ message: `Your timezone (Enter keeps ${guess})`, defaultValue: guess, validate: timezoneProblem, }); if (tz === null) return true; principal.timezone = tz; return false; }