UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

463 lines (462 loc) • 18.7 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { i as stripAnsi } from "./ansi-CZa2t0Hf.mjs"; import { n as isRich, r as theme } from "./theme-BOI0xyRZ.mjs"; import { n as WizardCancelledError, r as WizardNavigationError } from "./prompts-CCXo2K24.mjs"; import { n as stylePromptMessage, r as stylePromptTitle } from "./prompt-style-CLys-6GQ.mjs"; import { n as noteToStream } from "./note-CnLgHvug.mjs"; import { n as createProgressSpinner, t as createCliProgress } from "./progress-DZHhZ1Fk.mjs"; import { t as styleSelectParams } from "./prompt-select-styled-params-x2CCDNEh.mjs"; import { styleText } from "node:util"; import { CANCEL_SYMBOL, S_BAR, S_BAR_END, S_CHECKBOX_ACTIVE, S_CHECKBOX_INACTIVE, S_CHECKBOX_SELECTED, S_PASSWORD_MASK, S_RADIO_ACTIVE, S_RADIO_INACTIVE, autocomplete, autocompleteMultiselect, cancel, confirm, intro, limitOptions, multiselect, outro, password, select, settings, symbol, symbolBar, text } from "@clack/prompts"; import { AutocompletePrompt, ConfirmPrompt, MultiSelectPrompt, PasswordPrompt, SelectPrompt, TextPrompt, settings as settings$1, wrapTextWithPrefix } from "@clack/core"; //#region src/wizard/clack-navigation-prompts.ts function getOptionLabel(e) { return e.label ?? String(e.value ?? ""); } function computeLabel(e, t) { return e.split("\n").map((e) => styleText(t, e)).join("\n"); } function navigationFooterLines(e, t, n, r = []) { if (!n || !n.canGoBack && !n.canGoForward) return []; const i = [ ...n.canGoBack ? [styleText("dim", "← back")] : [], ...n.canGoForward ? [styleText("dim", "→ next")] : [], ...r ].join(" "); return [`${e ? `${styleText(t, S_BAR)} ` : ""}${i}`]; } function renderChoiceOption(e, t, n = false) { const r = getOptionLabel(e); if (t === "cancelled" || t === "submitted") return computeLabel(r, t === "cancelled" ? ["strikethrough", "dim"] : "dim"); const i = t === "active" || t === "active-selected"; const a = t === "selected" || t === "active-selected"; const o = t === "disabled"; const s = a ? S_CHECKBOX_SELECTED : i ? n ? S_CHECKBOX_ACTIVE : S_RADIO_ACTIVE : n ? S_CHECKBOX_INACTIVE : S_RADIO_INACTIVE; const c = o ? "gray" : a || i && !n ? "green" : i ? "cyan" : "dim"; const l = i ? r : computeLabel(r, o ? n ? ["strikethrough", "gray"] : "gray" : "dim"); const u = (i || a || o) && e.hint ? ` ${styleText("dim", `(${e.hint})`)}` : ""; return `${styleText(c, s)} ${l}${u}`; } function selectWithNavigationFooter(e) { return new SelectPrompt({ options: e.options, signal: e.signal, output: e.output, initialValue: e.initialValue, render() { const t = settings$1.withGuide; const n = `${symbol(this.state)} `; const r = `${symbolBar(this.state)} `; const i = wrapTextWithPrefix(e.output, e.message, r, n); const a = `${t ? `${styleText("gray", S_BAR)}\n` : ""}${i}\n`; switch (this.state) { case "submit": case "cancel": { const n = this.state === "cancel"; const r = t ? `${styleText("gray", S_BAR)} ` : ""; return `${a}${wrapTextWithPrefix(e.output, renderChoiceOption(expectDefined(this.options[this.cursor], "options entry at this.cursor"), n ? "cancelled" : "submitted"), r)}${n && t ? `\n${styleText("gray", S_BAR)}` : ""}`; } default: { const n = t ? `${styleText("cyan", S_BAR)} ` : ""; const r = [...navigationFooterLines(t, "cyan", e.navigation, [styleText("dim", "↑/↓ option")]), t ? styleText("cyan", S_BAR_END) : ""]; const i = a.split("\n").length; const o = r.length + 1; return `${a}${n}${limitOptions({ output: e.output, cursor: this.cursor, options: this.options, columnPadding: n.length, rowPadding: i + o, style: (e, t) => renderChoiceOption(e, e.disabled ? "disabled" : t ? "active" : "inactive") }).join(`\n${n}`)}\n${r.join("\n")}\n`; } } } }).prompt(); } function renderAutocompleteOption(e, t, n) { const r = getOptionLabel(t); const i = t.hint && t.value === e.focusedValue && (!e.multiple || e.focusedValue !== void 0) ? styleText("dim", ` (${t.hint})`) : ""; const a = e.multiple ? S_CHECKBOX_INACTIVE : S_RADIO_INACTIVE; if (t.disabled) return `${styleText("gray", a)} ${styleText(["strikethrough", "gray"], r)}`; return `${e.multiple && e.selectedValues.includes(t.value) ? styleText("green", S_CHECKBOX_SELECTED) : n && !e.multiple ? styleText("green", S_RADIO_ACTIVE) : styleText("dim", a)} ${n ? `${r}${i}` : styleText("dim", r)}`; } function renderAutocomplete(e, t) { const n = settings$1.withGuide; const r = [...n ? [styleText("gray", S_BAR)] : [], `${symbol(e.state)} ${t.message}`]; const i = `${r.join("\n")}\n`; const a = e.userInput; if (e.state === "submit") { if (e.multiple) return `${i}${n ? `${styleText("gray", S_BAR)} ` : ""}${styleText("dim", `${e.selectedValues.length} items selected`)}`; const t = e.options.filter((t) => e.selectedValues.includes(t.value)); const r = t.length > 0 ? ` ${styleText("dim", t.map(getOptionLabel).join(", "))}` : ""; return `${i}${n ? styleText("gray", S_BAR) : ""}${r}`; } if (e.state === "cancel") { if (e.multiple) return `${i}${n ? `${styleText("gray", S_BAR)} ` : ""}${styleText(["strikethrough", "dim"], a)}`; const t = a ? ` ${styleText(["strikethrough", "dim"], a)}` : ""; return `${i}${n ? styleText("gray", S_BAR) : ""}${t}`; } const o = e.state === "error" ? "yellow" : "cyan"; const s = n ? `${styleText(o, S_BAR)} ` : ""; const c = e.isNavigating ? styleText("dim", a) : e.userInputWithCursor; if (n || e.multiple) r.push(n ? styleText(o, S_BAR) : ""); const l = e.multiple || !e.isNavigating || a ? ` ${c}` : ""; const u = e.filteredOptions.length !== e.options.length ? styleText("dim", ` (${e.filteredOptions.length} match${e.filteredOptions.length === 1 ? "" : "es"})`) : ""; r.push(`${s}${styleText("dim", "Search:")}${l}${u}`); if (e.filteredOptions.length === 0 && a) r.push(`${s}${styleText("yellow", "No matches found")}`); if (e.state === "error") r.push(`${s}${styleText("yellow", e.error)}`); const d = [ `${s}${[ `${styleText("dim", "↑/↓")} to ${e.multiple ? "navigate" : "select"}`, ...e.multiple ? [`${styleText("dim", e.isNavigating ? "Space/Tab:" : "Tab:")} select`] : [], `${styleText("dim", "Enter:")} confirm`, `${styleText("dim", "Type:")} to search` ].join(" • ")}`, ...navigationFooterLines(n, o, t.navigation), n ? styleText(o, S_BAR_END) : "" ]; const f = !e.multiple && e.filteredOptions.length === 0 ? [] : limitOptions({ cursor: e.cursor, options: e.filteredOptions, ...!e.multiple ? { columnPadding: n ? 3 : 0 } : {}, rowPadding: r.length + d.length, style: (t, n) => renderAutocompleteOption(e, t, n), output: t.output }); return [ ...r, ...f.map((e) => `${s}${e}`), ...d ].join("\n"); } function autocompleteWithNavigationFooter(e) { return new AutocompletePrompt({ options: e.options, initialValue: e.initialValue === void 0 ? void 0 : [e.initialValue], filter: e.filter, signal: e.signal, output: e.output, render() { return renderAutocomplete(this, e); } }).prompt(); } function renderTextPrompt(e, t, n, r, i = false) { const a = settings$1.withGuide; const o = a ? styleText("gray", S_BAR) : ""; const s = `${a ? `${o}\n` : ""}${symbol(e.state)} ${t.message}\n`; if (e.state === "submit" || e.state === "cancel") { const t = e.state === "cancel"; return `${s}${o}${i ? a ? " " : "" : r ? " " : ""}${r ? styleText(t ? ["strikethrough", "dim"] : "dim", r) : ""}${t && (i ? r && a : r.trim()) ? `\n${o}` : ""}`; } const c = e.state === "error"; const l = c ? "yellow" : "cyan"; const u = a ? `${styleText(l, S_BAR)} ` : ""; const d = a ? `${styleText(l, S_BAR_END)}${c && i ? " " : ""}` : ""; const f = navigationFooterLines(a, l, t.navigation); const p = c ? i ? styleText("yellow", e.error) : e.error ? ` ${styleText("yellow", e.error)}` : "" : ""; return `${c ? `${s.trim()}\n` : s}${u}${c && i ? r : n}\n${f.length ? `${f.join("\n")}\n` : ""}${d}${p}\n`; } function textWithNavigationFooter(e) { return new TextPrompt({ validate: e.validate, placeholder: e.placeholder, initialValue: e.initialValue, output: e.output, signal: e.signal, render() { const t = e.placeholder ? styleText("inverse", e.placeholder[0] ?? "") + styleText("dim", e.placeholder.slice(1)) : styleText(["inverse", "hidden"], "_"); return renderTextPrompt(this, e, this.userInput ? this.userInputWithCursor : t, this.value ?? ""); } }).prompt(); } function passwordWithNavigationFooter(e) { return new PasswordPrompt({ validate: e.validate, mask: S_PASSWORD_MASK, signal: e.signal, output: e.output, render() { return renderTextPrompt(this, e, this.userInputWithCursor, this.masked ?? "", true); } }).prompt(); } function multiselectWithNavigationFooter(e) { return new MultiSelectPrompt({ options: e.options, signal: e.signal, output: e.output, initialValues: e.initialValues, validate(e) { if (e === void 0 || e.length === 0) return `Please select at least one option.\n${styleText("reset", styleText("dim", `Press ${styleText([ "gray", "bgWhite", "inverse" ], " space ")} to select, ${styleText("gray", styleText("bgWhite", styleText("inverse", " enter ")))} to submit`))}`; }, render() { const t = settings$1.withGuide; const n = wrapTextWithPrefix(e.output, e.message, t ? `${symbolBar(this.state)} ` : "", `${symbol(this.state)} `); const r = `${t ? `${styleText("gray", S_BAR)}\n` : ""}${n}\n`; const i = this.value ?? []; const styleOption = (e, t) => { if (e.disabled) return renderChoiceOption(e, "disabled", true); const n = i.includes(e.value); if (t && n) return renderChoiceOption(e, "active-selected", true); if (n) return renderChoiceOption(e, "selected", true); return renderChoiceOption(e, t ? "active" : "inactive", true); }; switch (this.state) { case "submit": { const n = this.options.filter(({ value: e }) => i.includes(e)).map((e) => renderChoiceOption(e, "submitted", true)).join(styleText("dim", ", ")) || styleText("dim", "none"); return `${r}${wrapTextWithPrefix(e.output, n, t ? `${styleText("gray", S_BAR)} ` : "")}`; } case "cancel": { const n = this.options.filter(({ value: e }) => i.includes(e)).map((e) => renderChoiceOption(e, "cancelled", true)).join(styleText("dim", ", ")); if (n.trim() === "") return `${r}${styleText("gray", S_BAR)}`; return `${r}${wrapTextWithPrefix(e.output, n, t ? `${styleText("gray", S_BAR)} ` : "")}${t ? `\n${styleText("gray", S_BAR)}` : ""}`; } default: { const n = this.state === "error" ? "yellow" : "cyan"; const i = t ? `${styleText(n, S_BAR)} ` : ""; const a = this.state === "error" ? this.error.split("\n").map((e, n) => n === 0 ? `${t ? `${styleText("yellow", S_BAR_END)} ` : ""}${styleText("yellow", e)}` : ` ${e}`) : [...navigationFooterLines(t, "cyan", e.navigation, [styleText("dim", "↑/↓ option"), styleText("dim", "space select")]), t ? styleText("cyan", S_BAR_END) : ""]; const o = r.split("\n").length; const s = a.length + 1; return `${r}${i}${limitOptions({ output: e.output, options: this.options, cursor: this.cursor, columnPadding: i.length, rowPadding: o + s, style: styleOption }).join(`\n${i}`)}\n${a.join("\n")}\n`; } } } }).prompt(); } function autocompleteMultiselectWithNavigationFooter(e) { return new AutocompletePrompt({ options: e.options, multiple: true, filter: e.filter, initialValue: e.initialValues, signal: e.signal, output: e.output, render() { return renderAutocomplete(this, e); } }).prompt(); } function confirmWithNavigationFooter(e) { const t = "Yes"; const n = "No"; return new ConfirmPrompt({ active: t, inactive: n, signal: e.signal, output: e.output, initialValue: e.initialValue ?? true, render() { const r = settings$1.withGuide; const i = `${symbol(this.state)} `; const a = r ? `${styleText("gray", S_BAR)} ` : ""; const o = wrapTextWithPrefix(e.output, e.message, a, i); const s = `${r ? `${styleText("gray", S_BAR)}\n` : ""}${o}\n`; const c = this.value ? t : n; switch (this.state) { case "submit": return `${s}${r ? `${styleText("gray", S_BAR)} ` : ""}${styleText("dim", c)}`; case "cancel": return `${s}${r ? `${styleText("gray", S_BAR)} ` : ""}${styleText(["strikethrough", "dim"], c)}${r ? `\n${styleText("gray", S_BAR)}` : ""}`; default: { const i = r ? `${styleText("cyan", S_BAR)} ` : ""; const a = r ? styleText("cyan", S_BAR_END) : ""; const o = e.vertical ? r ? `\n${styleText("cyan", S_BAR)} ` : "\n" : ` ${styleText("dim", "/")} `; const c = navigationFooterLines(r, "cyan", e.navigation, [styleText("dim", "↑/↓ option")]); return `${s}${i}${this.value ? `${styleText("green", S_RADIO_ACTIVE)} ${t}` : `${styleText("dim", S_RADIO_INACTIVE)} ${styleText("dim", t)}`}${o}${!this.value ? `${styleText("green", S_RADIO_ACTIVE)} ${n}` : `${styleText("dim", S_RADIO_INACTIVE)} ${styleText("dim", n)}`}\n${c.length > 0 ? `${c.join("\n")}\n` : ""}${a}\n`; } } } }).prompt(); } //#endregion //#region src/wizard/clack-prompter.ts const CLAW_SPINNER_FRAMES = [ "(\\/)", "(||)", "(--)", "(||)" ]; function guardCancel(e, t, n) { if (e === CANCEL_SYMBOL) { if (!n?.aborted) cancel(stylePromptTitle("Setup cancelled.") ?? "Setup cancelled.", { output: t }); throw new WizardCancelledError(); } return e; } function resolveNavigationDirection(e, t) { if (t?.name === "left" && e?.canGoBack) return "back"; if (t?.name === "right" && e?.canGoForward) return "forward"; } async function runPromptWithNavigation(e, t, n, r) { const i = e?.canGoBack === true || e?.canGoForward === true; const a = i && settings.actions.delete("left"); const o = i && settings.actions.delete("right"); try { const i = new AbortController(); const a = r ? AbortSignal.any([i.signal, r]) : i.signal; let o; let s = false; let c; const queueCancellation = () => { if (c || a.aborted || o) return; c = setImmediate(() => { c = void 0; if (!s && !a.aborted && !o) i.abort(); }); }; const onKeypress = (t, n) => { if (t === "" || n?.ctrl === true && n.name === "d") { queueCancellation(); return; } const r = resolveNavigationDirection(e, n); if (!r) return; o ??= r; i.abort(); }; try { process.stdin.once("end", queueCancellation); if (process.stdin.readableEnded) queueCancellation(); process.stdin.on("keypress", onKeypress); const e = await t(a).finally(() => { s = true; }); if (o) throw new WizardNavigationError(o); return guardCancel(e, n, r); } finally { if (c) { clearImmediate(c); c = void 0; } process.stdin.off("end", queueCancellation); process.stdin.off("keypress", onKeypress); } } finally { if (a) settings.actions.add("left"); if (o) settings.actions.add("right"); } } function tokenizedOptionFilter(e, t) { const n = normalizeLowercaseStringOrEmpty(e).split(/\s+/).filter((e) => e.length > 0); if (n.length === 0) return true; const r = stripAnsi(t.label ?? ""); const i = stripAnsi(t.hint ?? ""); const a = String(t.value ?? ""); const o = normalizeLowercaseStringOrEmpty(`${r} ${i} ${a}`); return n.every((e) => o.includes(e)); } function createClackPrompter(e = process.stdout, t) { return { intro: async (t) => { intro(stylePromptTitle(t) ?? t, { output: e }); }, outro: async (t) => { outro(stylePromptTitle(t) ?? t, { output: e }); }, note: async (t, n) => { noteToStream(t, n, e); }, plain: async (t) => { e.write(t.endsWith("\n") ? t : `${t}\n`); }, select: async (n) => { const { message: r, options: i } = styleSelectParams(n); const a = i; return await runPromptWithNavigation(n.navigation, async (t) => { return await (n.searchable ? n.navigation ? autocompleteWithNavigationFooter : autocomplete : n.navigation ? selectWithNavigationFooter : select)({ message: r, options: a, initialValue: n.initialValue, ...n.searchable ? { filter: tokenizedOptionFilter } : {}, signal: t, ...n.navigation ? { navigation: n.navigation } : {}, output: e }); }, e, t); }, multiselect: async (n) => { const { message: r, options: i } = styleSelectParams(n); const a = i; return await runPromptWithNavigation(n.navigation, async (t) => { return await (n.searchable ? n.navigation ? autocompleteMultiselectWithNavigationFooter : autocompleteMultiselect : n.navigation ? multiselectWithNavigationFooter : multiselect)({ message: r, options: a, initialValues: n.initialValues, ...n.searchable ? { filter: tokenizedOptionFilter } : {}, signal: t, ...n.navigation ? { navigation: n.navigation } : {}, output: e }); }, e, t); }, text: async (n) => { return await runPromptWithNavigation(n.navigation, async (t) => { const r = n.validate; return await (n.sensitive ? n.navigation ? passwordWithNavigationFooter : password : n.navigation ? textWithNavigationFooter : text)({ message: stylePromptMessage(n.message), ...!n.sensitive ? { initialValue: n.initialValue, placeholder: n.placeholder } : {}, validate: r ? (e) => r(e ?? "") : void 0, ...n.navigation ? { navigation: n.navigation } : {}, signal: t, output: e }); }, e, n.signal && t ? AbortSignal.any([n.signal, t]) : n.signal ?? t); }, confirm: async (n) => await runPromptWithNavigation(n.navigation, async (t) => { const r = stylePromptMessage(n.message); return await (n.navigation ? confirmWithNavigationFooter : confirm)({ message: r, initialValue: n.initialValue, vertical: n.layout === "vertical", ...n.navigation ? { navigation: n.navigation } : {}, signal: t, output: e }); }, e, t), progress: (t) => { const n = e.isTTY && isRich() && !process.env.CI && !process.env.VITEST; const r = createProgressSpinner(n ? { frames: CLAW_SPINNER_FRAMES, delay: 120, styleFrame: theme.accent, output: e } : { output: e }, 10); r.start(t); const i = createCliProgress({ label: t, indeterminate: true, enabled: true, fallback: "none", stream: e }); return { update: (e) => { r.message(e); i.setLabel(e); }, stop: (e) => { i.done(); r.stop(e); } }; } }; } //#endregion export { tokenizedOptionFilter as n, createClackPrompter as t };