Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/api/requestInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,6 +126,12 @@ export async function requestInputs(app: App, choiceExecutor: IChoiceExecutor, i

let collected: Record<string, string> = {};
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;
Expand Down
20 changes: 20 additions & 0 deletions src/interactive/routePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -71,3 +72,22 @@ export async function routePrompt<T>(
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}`,
);
}
32 changes: 32 additions & 0 deletions src/quickAddApi.audit-api-prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,35 @@ describe("getFieldValues frontmatter array safety", () => {
expect(values).toEqual(["high"]);
});
});

describe("script prompts in a non-interactive run", () => {
type Api = ReturnType<typeof QuickAddApi.GetApi>;
const prompts: Array<[string, (api: Api) => Promise<unknown>]> = [
["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" });
});
});
22 changes: 15 additions & 7 deletions src/quickAddApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -137,17 +138,18 @@ export class QuickAddApi {
};
return {
requestInputs: (inputs: Parameters<typeof requestInputs>[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;
Expand All @@ -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,
Expand All @@ -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) => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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),
Expand All @@ -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(
Expand All @@ -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);
Expand Down
Loading