From de679fdede25af6b0863fcf9ff483a23de520102 Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Mon, 2 Nov 2026 20:51:11 +0100 Subject: [PATCH] fix(cli): abort non-interactive runs when a script opens a prompt --- src/api/requestInputs.ts | 7 +++++ src/interactive/routePrompt.ts | 20 ++++++++++++++ src/quickAddApi.audit-api-prompts.test.ts | 32 +++++++++++++++++++++++ src/quickAddApi.ts | 22 +++++++++++----- 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/api/requestInputs.ts b/src/api/requestInputs.ts index d61f669cd..a32bce14b 100644 --- a/src/api/requestInputs.ts +++ b/src/api/requestInputs.ts @@ -5,6 +5,7 @@ import { OnePageInputModal } from "../preflight/OnePageInputModal"; import type { NumericInputConfig, SliderConfig } from "../utils/valueSyntax"; import { formatISODate } from "../utils/dateParser"; import { rethrowPromptError } from "./promptApi"; +import { abortIfHeadless } from "../interactive/routePrompt"; function sanitizeNumericConfig( value: NumericInputConfig | undefined, @@ -125,6 +126,12 @@ export async function requestInputs(app: App, choiceExecutor: IChoiceExecutor, i let collected: Record = {}; if (missing.length > 0) { + abortIfHeadless( + choiceExecutor, + "an input form", + missing.map((input) => input.label).join(", "), + `Pass ${missing.map((input) => input.id).join(", ")} in vars, or re-run with the ui flag.`, + ); // Route the batch form to a remote interactive session (Raycast) // when one is driving this run; otherwise open the Obsidian modal. const provider = choiceExecutor?.promptProvider; diff --git a/src/interactive/routePrompt.ts b/src/interactive/routePrompt.ts index dec4fdbf8..a8c791de8 100644 --- a/src/interactive/routePrompt.ts +++ b/src/interactive/routePrompt.ts @@ -27,6 +27,7 @@ */ import type { IChoiceExecutor } from "../IChoiceExecutor"; +import { ChoiceAbortError } from "../errors/ChoiceAbortError"; import { UserCancelError } from "../errors/UserCancelError"; import { isCancellationError } from "../utils/errorUtils"; import type { PromptProvider } from "./promptProvider"; @@ -71,3 +72,22 @@ export async function routePrompt( throw error; } } + +/** + * Script-facing prompts (`quickAddApi.suggester`, `inputPrompt`, `requestInputs`, ...) + * have no headless answer: in a non-interactive run the modal would open on a desktop + * nobody is watching and the CLI call would never return. Abort instead, naming the + * prompt so the caller knows which value to preset. + */ +export function abortIfHeadless( + executor: PromptRoutingContext | undefined, + prompt: string, + label?: string, + remedy = "Re-run with the ui flag.", +): void { + if (!executor || executor.promptProvider || executor.interactive !== false) return; + const named = label ? `${prompt} "${label}"` : prompt; + throw new ChoiceAbortError( + `A script opened ${named}, but this run is non-interactive. ${remedy}`, + ); +} diff --git a/src/quickAddApi.audit-api-prompts.test.ts b/src/quickAddApi.audit-api-prompts.test.ts index 7cda504c5..74fef5efa 100644 --- a/src/quickAddApi.audit-api-prompts.test.ts +++ b/src/quickAddApi.audit-api-prompts.test.ts @@ -215,3 +215,35 @@ describe("getFieldValues frontmatter array safety", () => { expect(values).toEqual(["high"]); }); }); + +describe("script prompts in a non-interactive run", () => { + type Api = ReturnType; + const prompts: Array<[string, (api: Api) => Promise]> = [ + ["suggester", (api) => api.suggester(["a"], ["a"], "Pick a source")], + ["inputPrompt", (api) => api.inputPrompt("Title")], + ["wideInputPrompt", (api) => api.wideInputPrompt("Body")], + ["yesNoPrompt", (api) => api.yesNoPrompt("Sure?")], + ["checkboxPrompt", (api) => api.checkboxPrompt(["x"], [], "Tags")], + ["infoDialog", (api) => api.infoDialog("Heads up", "text")], + ["datePrompt", (api) => api.datePrompt("When")], + ["requestInputs", (api) => api.requestInputs([{ id: "q", label: "Question", type: "text" }])], + ]; + + it.each(prompts)("%s rejects with an abort instead of opening a modal", async (_, open) => { + const executor = { ...makeChoiceExecutor(), interactive: false }; + const { api } = getApi(makeApp(), makePlugin(), executor); + + await expect(open(api)).rejects.toMatchObject({ + name: "MacroAbortError", + message: expect.stringContaining("this run is non-interactive"), + }); + }); + + it("requestInputs still resolves from variables preset by the caller", async () => { + const executor = { ...makeChoiceExecutor(), interactive: false }; + executor.variables.set("q", "preset"); + const { api } = getApi(makeApp(), makePlugin(), executor); + + await expect(api.requestInputs([{ id: "q", label: "Question", type: "text" }])).resolves.toEqual({ q: "preset" }); + }); +}); diff --git a/src/quickAddApi.ts b/src/quickAddApi.ts index 5d991445c..93afeb999 100644 --- a/src/quickAddApi.ts +++ b/src/quickAddApi.ts @@ -15,6 +15,7 @@ import type { ChoiceRef, ChoiceSummary } from "./api/choiceApi"; import { findChoice, importPackage, pickChoice, summarizeChoice } from "./api/choiceApi"; import { PromptApi } from "./api/promptApi"; import { requestInputs } from "./api/requestInputs"; +import { abortIfHeadless } from "./interactive/routePrompt"; import { createAiApi } from "./api/aiApi"; import { createFieldSuggestionsApi } from "./api/fieldSuggestionsApi"; @@ -137,17 +138,18 @@ export class QuickAddApi { }; return { requestInputs: (inputs: Parameters[2]) => requestInputs(app, choiceExecutor, inputs), - inputPrompt: ( + inputPrompt: async ( header: string, placeholder?: string, value?: string, options?: InputPromptOptions, ) => { const provider = choiceExecutor?.promptProvider; + abortIfHeadless(choiceExecutor, "an input prompt", header); if (provider) return provider.inputPrompt(header, placeholder, value); return QuickAddApi.inputPrompt(app, header, placeholder, value, options); }, - datePrompt: ( + datePrompt: async ( header: string, options?: { placeholder?: string; @@ -156,16 +158,18 @@ export class QuickAddApi { }, ) => { const provider = choiceExecutor?.promptProvider; + abortIfHeadless(choiceExecutor, "a date prompt", header); if (provider) return provider.datePrompt(header, options); return QuickAddApi.datePrompt(app, header, options); }, - wideInputPrompt: ( + wideInputPrompt: async ( header: string, placeholder?: string, value?: string, options?: InputPromptOptions, ) => { const provider = choiceExecutor?.promptProvider; + abortIfHeadless(choiceExecutor, "a wide input prompt", header); if (provider) return provider.wideInputPrompt(header, placeholder, value); return QuickAddApi.wideInputPrompt( app, @@ -175,17 +179,19 @@ export class QuickAddApi { options, ); }, - yesNoPrompt: (header: string, text?: string) => { + yesNoPrompt: async (header: string, text?: string) => { const provider = choiceExecutor?.promptProvider; + abortIfHeadless(choiceExecutor, "a yes/no prompt", header); if (provider) return provider.yesNoPrompt(header, text); return QuickAddApi.yesNoPrompt(app, header, text); }, - infoDialog: (header: string, text: string[] | string) => { + infoDialog: async (header: string, text: string[] | string) => { const provider = choiceExecutor?.promptProvider; + abortIfHeadless(choiceExecutor, "an info dialog", header); if (provider) return provider.infoDialog(header, text); return QuickAddApi.infoDialog(app, header, text); }, - suggester: ( + suggester: async ( displayItems: | string[] | ((value: string, index?: number, arr?: string[]) => string), @@ -196,6 +202,7 @@ export class QuickAddApi { ) => { // Route to a remote interactive session (Raycast) when one is driving // this execution; otherwise open the Obsidian suggester modal. + abortIfHeadless(choiceExecutor, "a suggester", placeholder); const provider = choiceExecutor?.promptProvider; if (provider) { return provider.suggester( @@ -214,11 +221,12 @@ export class QuickAddApi { options, ); }, - checkboxPrompt: ( + checkboxPrompt: async ( items: string[], selectedItems?: string[], header?: string, ) => { + abortIfHeadless(choiceExecutor, "a checkbox prompt", header); const provider = choiceExecutor?.promptProvider; if (provider) { return provider.checkboxPrompt(items, selectedItems, header);