UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

191 lines (190 loc) • 5.65 kB
import { r as WizardNavigationError } from "./prompts-CCXo2K24.mjs"; //#region src/wizard/navigation-prompter.ts const basePrompterByNavigationPrompter = /* @__PURE__ */ new WeakMap(); function unwrapNavigationPrompter(e) { let t = e; let n = basePrompterByNavigationPrompter.get(t); while (n) { t = n; n = basePrompterByNavigationPrompter.get(t); } return t; } function stableKey(e) { if (e === void 0) return "undefined"; try { return JSON.stringify(e); } catch { return Object.prototype.toString.call(e); } } function optionSignature(e) { return stableKey(e.map((e) => [stableKey(e.value), e.label])); } function buildPromptSignature(e, t) { return stableKey({ kind: e, message: t.message, options: t.options ? optionSignature(t.options) : void 0, layout: t.layout }); } var WizardPromptNavigator = class { constructor(e, t = {}) { this.base = e; this.options = t; this.cursor = 0; this.restartRequested = false; this.boundaryBackRequested = false; this.backNavigationDisabled = false; this.records = []; this.prompter = { intro: (e) => this.display(() => this.base.intro(e)), outro: (e) => this.display(() => this.base.outro(e)), note: (e, t) => this.display(() => this.base.note(e, t)), ...this.base.deviceCode ? { deviceCode: (e) => this.display(() => this.base.deviceCode?.(e)) } : {}, plain: (e) => this.display(() => this.base.plain?.(e)), select: async (e) => await this.prompt({ params: e, signature: buildPromptSignature("select", e), cacheAnswer: true, withInitial: (e, t) => ({ ...e, initialValue: t }), call: (e) => this.base.select(e) }), multiselect: async (e) => await this.prompt({ params: e, signature: buildPromptSignature("multiselect", e), cacheAnswer: true, withInitial: (e, t) => ({ ...e, initialValues: Array.isArray(t) ? t : e.initialValues }), call: (e) => this.base.multiselect(e) }), text: async (e) => await this.prompt({ params: e, signature: buildPromptSignature("text", e), cacheAnswer: e.sensitive !== true, withInitial: (e, t) => ({ ...e, initialValue: typeof t === "string" ? t : e.initialValue }), call: (e) => this.base.text(e) }), confirm: async (e) => await this.prompt({ params: e, signature: buildPromptSignature("confirm", e), cacheAnswer: true, withInitial: (e, t) => ({ ...e, initialValue: typeof t === "boolean" ? t : e.initialValue }), call: (e) => this.base.confirm(e) }), progress: (e) => this.shouldSuppressOutput() ? { update: () => {}, stop: () => {} } : this.base.progress(e), ...this.base.openUrl ? { openUrl: (e) => this.display(() => this.base.openUrl?.(e)) } : {}, disableBackNavigation: () => { this.backNavigationDisabled = true; this.targetIndex = void 0; } }; basePrompterByNavigationPrompter.set(this.prompter, unwrapNavigationPrompter(e)); } async run(e) { while (true) { this.cursor = 0; this.restartRequested = false; this.boundaryBackRequested = false; try { return { status: "completed", value: await e(this.prompter) }; } catch (e) { if (e instanceof WizardNavigationError && e.direction === "back") { if (this.restartRequested) continue; if (this.boundaryBackRequested) return { status: "back" }; } throw e; } } } shouldSuppressOutput() { return this.targetIndex !== void 0 && this.cursor <= this.targetIndex; } async display(e) { if (!this.shouldSuppressOutput()) await e(); } matchingRecord(e, t) { const n = this.records[e]; if (!n) return; if (n.signature === t) return n; this.records.splice(e); if (this.targetIndex !== void 0 && e < this.targetIndex) this.targetIndex = void 0; } remember(e, t, n) { if (!t.cacheAnswer) { this.records[e] = void 0; this.records.splice(e + 1); return; } const r = stableKey(n); const i = this.records[e]; this.records[e] = { signature: t.signature, answer: n, answerKey: r }; if (!i || i.answerKey !== r || i.signature !== t.signature) this.records.splice(e + 1); } async prompt(e) { const t = this.cursor; const n = this.matchingRecord(t, e.signature); if (this.targetIndex !== void 0 && t < this.targetIndex && n) { this.cursor = t + 1; return n.answer; } const r = { ...n ? e.withInitial(e.params, n.answer) : e.params, navigation: { canGoBack: !this.backNavigationDisabled && (t > 0 || this.options.allowBackFromStart === true), canGoForward: n !== void 0 } }; try { const n = await e.call(r); this.remember(t, e, n); this.cursor = t + 1; if (this.targetIndex !== void 0 && t >= this.targetIndex) this.targetIndex = void 0; return n; } catch (e) { if (e instanceof WizardNavigationError) { if (e.direction === "forward" && n) { this.cursor = t + 1; this.targetIndex = void 0; return n.answer; } if (e.direction === "back" && !this.backNavigationDisabled && t === 0 && this.options.allowBackFromStart === true) this.boundaryBackRequested = true; if (e.direction === "back" && !this.backNavigationDisabled && t > 0) { this.targetIndex = t - 1; this.restartRequested = true; } } throw e; } } }; async function runWizardWithPromptNavigationScope(e, t) { return new WizardPromptNavigator(unwrapNavigationPrompter(e), { allowBackFromStart: true }).run(t); } async function runWizardWithPromptNavigation(e, t) { await new WizardPromptNavigator(e).run(t); } //#endregion export { runWizardWithPromptNavigationScope as n, runWizardWithPromptNavigation as t };