// @generated by scripts/build-runtime.mjs; do not edit. // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader. // src/command.ts var YES_FLAG_COMPLETIONS = [ { value: "--yes", label: "--yes", description: "Skip confirmation prompts" }, { value: "-y", label: "-y", description: "Skip confirmation prompts" } ]; var SYNC_COMMANDS = [ { name: "help", description: "Show command usage" }, { name: "use", description: "Switch the current sync setup", usageSuffix: " " }, { name: "init", description: "Create local config template" }, { name: "config", description: "Show resolved configuration" }, { name: "files", description: "Choose included content" }, { name: "status", description: "Show sync status" }, { name: "diff", description: "Show local/remote diff" }, { name: "doctor", description: "Check config, secrets, and lock state" }, { name: "push", description: "Upload local settings" }, { name: "pull", description: "Apply remote settings" }, { name: "sync", description: "Push or pull as needed" }, { name: "history", description: "Show recent remote snapshots" }, { name: "rollback", description: "Apply a previous snapshot", usageSuffix: " " }, { name: "migrate-state", description: "Move legacy state into pi-sync/" }, { name: "unlock", description: "Remove a stale local lock", usageSuffix: " --stale" } ]; var SYNC_COMMAND_COMPLETIONS = SYNC_COMMANDS.map( ({ name, description }) => ({ value: name, label: name, description }) ); var setupCompletionNames = []; function setSyncSetupCompletions(names) { setupCompletionNames = [...new Set(names)].sort((left, right) => left.localeCompare(right)); } var SETUP_FLAG_COMPLETION = { value: "--setup", label: "--setup", description: "Address a sync setup without switching" }; var SYNC_FLAG_COMPLETIONS = { config: [SETUP_FLAG_COMPLETION], files: [SETUP_FLAG_COMPLETION], status: [SETUP_FLAG_COMPLETION], diff: [SETUP_FLAG_COMPLETION], doctor: [SETUP_FLAG_COMPLETION], push: [ ...YES_FLAG_COMPLETIONS, { value: "--force", label: "--force", description: "Overwrite visible remote changes" }, SETUP_FLAG_COMPLETION ], pull: [ ...YES_FLAG_COMPLETIONS, { value: "--force", label: "--force", description: "Overwrite local changes" }, SETUP_FLAG_COMPLETION ], sync: [ ...YES_FLAG_COMPLETIONS, { value: "--force", label: "--force", description: "Resolve conflicts by forcing action" }, SETUP_FLAG_COMPLETION ], history: [SETUP_FLAG_COMPLETION], rollback: [...YES_FLAG_COMPLETIONS, SETUP_FLAG_COMPLETION], "migrate-state": YES_FLAG_COMPLETIONS, unlock: [{ value: "--stale", label: "--stale", description: "Remove only a stale lock" }] }; function splitArgs(input) { return input.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g)?.map((arg) => arg.replace(/^['"]|['"]$/g, "")) ?? []; } function parseOptions(args) { const values = []; let yes = false; let force = false; let stale = false; let setup; for (let index = 0; index < args.length; index += 1) { const arg = args[index]; if (arg === "--yes" || arg === "-y") yes = true; else if (arg === "--force") force = true; else if (arg === "--stale") stale = true; else if (arg === "--setup") { const name = args[index + 1]; if (!name || name.startsWith("-")) throw new Error("--setup requires a sync setup name."); if (setup !== void 0) throw new Error("--setup may be provided only once."); setup = name; index += 1; } else if (arg.startsWith("-")) { throw new Error(`Unknown sync option: ${arg}`); } else values.push(arg); } return { yes, force, stale, silent: false, reload: true, auto: false, ...setup === void 0 ? {} : { setup }, args: values }; } function validateCommandOptions(command, options) { const setupAllowed = /* @__PURE__ */ new Set([ "config", "files", "status", "diff", "doctor", "push", "pull", "sync", "history", "rollback" ]); if (options.setup && !setupAllowed.has(command)) { throw new Error(`--setup is not supported by /sync ${command}.`); } if (options.yes && !["push", "pull", "sync", "rollback", "migrate-state"].includes(command)) { throw new Error(`Confirmation/force options are not supported by /sync ${command}.`); } if (options.force && !["push", "pull", "sync", "rollback"].includes(command)) { throw new Error(`Confirmation/force options are not supported by /sync ${command}.`); } if (options.stale && command !== "unlock") { throw new Error(`--stale is not supported by /sync ${command}.`); } const expectedValues = command === "rollback" || command === "use" ? 1 : 0; if (options.args.length !== expectedValues) { if (command === "rollback") throw new Error("Usage: /sync rollback [--yes] [--setup ]"); if (command === "use") throw new Error("Usage: /sync use "); throw new Error(`Unexpected argument for /sync ${command}: ${options.args.join(" ")}`); } } function completeSyncArguments(argumentPrefix) { const prefix = argumentPrefix.trimStart(); if (prefix === "") return [...SYNC_COMMAND_COMPLETIONS]; const trailingSpace = /\s$/.test(prefix); const tokens = splitArgs(prefix); if (tokens.length === 0) return [...SYNC_COMMAND_COMPLETIONS]; const [command] = tokens; if (tokens.length === 1 && !trailingSpace) { const matches2 = SYNC_COMMAND_COMPLETIONS.filter((item) => item.value.startsWith(command)); return matches2.length > 0 ? [...matches2] : null; } const args = tokens.slice(1); if (command === "use") { if (args.length > 1 || trailingSpace && args.length > 0) return null; return completeSetupValue(prefix, trailingSpace ? "" : args[0] ?? ""); } const setupFlagIndex = args.lastIndexOf("--setup"); if (setupFlagIndex >= 0 && setupFlagIndex === args.length - (trailingSpace ? 1 : 2)) { const currentSetup = trailingSpace ? "" : args.at(-1) ?? ""; if (!currentSetup.startsWith("-")) return completeSetupValue(prefix, currentSetup); } const flagCompletions = SYNC_FLAG_COMPLETIONS[command]; if (!flagCompletions) return null; const completedArgs = trailingSpace ? args : args.slice(0, -1); const completedValues = completedArgs.filter((arg) => !arg.startsWith("-")); if (command === "rollback" ? completedValues.length > 1 : completedValues.length > 0) { return null; } const current = trailingSpace ? "" : args.at(-1) ?? ""; if (current && !current.startsWith("-")) return null; const currentRaw = trailingSpace ? "" : prefix.match(/\S+$/)?.[0] ?? ""; const completionPrefix = trailingSpace ? prefix : prefix.slice(0, prefix.length - currentRaw.length); const matches = flagCompletions.filter((item) => item.value.startsWith(current)); return matches.length > 0 ? matches.map((item) => ({ ...item, value: `${completionPrefix}${item.value}` })) : null; } function completeSetupValue(prefix, current) { const currentRaw = current ? prefix.match(/\S+$/u)?.[0] ?? "" : ""; const completionPrefix = currentRaw ? prefix.slice(0, prefix.length - currentRaw.length) : prefix; const matches = setupCompletionNames.filter((name) => name.startsWith(current)); return matches.length > 0 ? matches.map((name) => ({ value: `${completionPrefix}${name}`, label: name, description: "Sync setup" })) : null; } function syncMenuOptions() { return SYNC_COMMANDS.map(({ name, description }) => `${name} \u2014 ${description}`); } function syncCommandFromMenuOption(option) { return SYNC_COMMANDS.find(({ name, description }) => option === `${name} \u2014 ${description}`)?.name; } async function resolveSyncCommand(input, ctx) { const [subcommand, ...rest] = splitArgs(input); if (subcommand) return { subcommand, rest }; if (!ctx.hasUI) { ctx.ui.notify(usage(), "info"); return void 0; } const selectedOption = await ctx.ui.select("pi-sync", syncMenuOptions()); const selected = selectedOption ? syncCommandFromMenuOption(selectedOption) : void 0; if (!selected) return void 0; if (selected !== "rollback") return { subcommand: selected, rest: [] }; const target = (await ctx.ui.input("Rollback snapshot", "snapshot id"))?.trim(); if (!target) { ctx.ui.notify("Rollback cancelled.", "info"); return void 0; } return { subcommand: selected, rest: [target] }; } function usage() { const commands = SYNC_COMMANDS.map( (command) => `${command.name}${"usageSuffix" in command ? command.usageSuffix : ""}` ).join(", "); return [ "Usage: /sync ", `Commands: ${commands}`, "Settings: use /sync init or edit storage connections and sync setups in ~/.pi/agent/pi-sync.json (or the configured Pi agent directory). Version 1 and version 2 settings are unsupported and are never rewritten." ].join("\n"); } export { setSyncSetupCompletions, splitArgs, parseOptions, validateCommandOptions, completeSyncArguments, resolveSyncCommand, usage }; //# sourceMappingURL=chunk-4U6AFLJB.ts.map