semola 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -45
- package/dist/chunk-CKQMccvm.cjs +28 -0
- package/dist/lib/api/index.cjs +29 -15
- package/dist/lib/api/index.mjs +30 -16
- package/dist/lib/cache/index.cjs +47 -22
- package/dist/lib/cache/index.d.cts +3 -24
- package/dist/lib/cache/index.d.mts +3 -24
- package/dist/lib/cache/index.mjs +48 -23
- package/dist/lib/cron/index.cjs +117 -117
- package/dist/lib/cron/index.mjs +118 -118
- package/dist/lib/errors/index.d.cts +12 -1
- package/dist/lib/errors/index.d.mts +12 -1
- package/dist/lib/logging/index.cjs +1 -0
- package/dist/lib/orm/index.cjs +1642 -0
- package/dist/lib/orm/index.d.cts +402 -0
- package/dist/lib/orm/index.d.mts +402 -0
- package/dist/lib/orm/index.mjs +1630 -0
- package/dist/lib/prompts/index.cjs +89 -89
- package/dist/lib/prompts/index.d.cts +12 -33
- package/dist/lib/prompts/index.d.mts +12 -33
- package/dist/lib/prompts/index.mjs +89 -90
- package/dist/lib/pubsub/index.cjs +43 -19
- package/dist/lib/pubsub/index.d.cts +3 -18
- package/dist/lib/pubsub/index.d.mts +3 -18
- package/dist/lib/pubsub/index.mjs +44 -20
- package/dist/lib/queue/index.cjs +40 -10
- package/dist/lib/queue/index.d.cts +11 -4
- package/dist/lib/queue/index.d.mts +11 -4
- package/dist/lib/queue/index.mjs +39 -11
- package/dist/lib/workflow/index.cjs +285 -282
- package/dist/lib/workflow/index.d.cts +76 -11
- package/dist/lib/workflow/index.d.mts +76 -11
- package/dist/lib/workflow/index.mjs +278 -284
- package/package.json +11 -1
- package/dist/index-BhGNDjPq.d.mts +0 -13
- package/dist/index-DxSbeGP-.d.cts +0 -13
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("../../chunk-CKQMccvm.cjs");
|
|
2
3
|
const require_lib_errors_index = require("../errors/index.cjs");
|
|
3
4
|
let node_util = require("node:util");
|
|
5
|
+
//#region src/lib/prompts/errors.ts
|
|
6
|
+
var PromptIOError = class extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "PromptIOError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var PromptEnvironmentError = class extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "PromptEnvironmentError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var PromptCancelledError = class extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "PromptCancelledError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
4
25
|
//#region src/lib/prompts/core/keys.ts
|
|
5
26
|
const ESC = "\x1B";
|
|
6
27
|
const CSI = "\x1B[";
|
|
@@ -189,8 +210,8 @@ var NodePromptRuntime = class {
|
|
|
189
210
|
return Boolean(this.stdin.isTTY && this.stdout.isTTY && typeof this.stdin.setRawMode === "function");
|
|
190
211
|
}
|
|
191
212
|
init() {
|
|
192
|
-
if (!this.isInteractive())
|
|
193
|
-
if (this.initialized) return
|
|
213
|
+
if (!this.isInteractive()) throw new PromptEnvironmentError("Interactive prompts require a TTY with raw mode support");
|
|
214
|
+
if (this.initialized) return;
|
|
194
215
|
const [initError] = require_lib_errors_index.mightThrowSync(() => {
|
|
195
216
|
this.stdin.setRawMode(true);
|
|
196
217
|
this.stdin.setEncoding("utf8");
|
|
@@ -198,15 +219,17 @@ var NodePromptRuntime = class {
|
|
|
198
219
|
this.stdin.on("data", this.onData);
|
|
199
220
|
this.stdout.write(HIDE_CURSOR);
|
|
200
221
|
});
|
|
201
|
-
if (initError)
|
|
222
|
+
if (initError) throw new PromptIOError("Unable to initialize prompt runtime");
|
|
202
223
|
this.initialized = true;
|
|
203
|
-
return require_lib_errors_index.ok(void 0);
|
|
204
224
|
}
|
|
205
225
|
readKey() {
|
|
206
226
|
const queued = this.queue.shift();
|
|
207
|
-
if (queued) return Promise.resolve(
|
|
208
|
-
return new Promise((resolve) => {
|
|
209
|
-
this.waiters.push(
|
|
227
|
+
if (queued) return Promise.resolve(queued);
|
|
228
|
+
return new Promise((resolve, reject) => {
|
|
229
|
+
this.waiters.push({
|
|
230
|
+
resolve,
|
|
231
|
+
reject
|
|
232
|
+
});
|
|
210
233
|
});
|
|
211
234
|
}
|
|
212
235
|
render(frame) {
|
|
@@ -216,8 +239,7 @@ var NodePromptRuntime = class {
|
|
|
216
239
|
this.stdout.write(frame);
|
|
217
240
|
this.previousLines = countLines(frame);
|
|
218
241
|
});
|
|
219
|
-
if (renderError)
|
|
220
|
-
return require_lib_errors_index.ok(void 0);
|
|
242
|
+
if (renderError) throw new PromptIOError("Unable to render prompt frame");
|
|
221
243
|
}
|
|
222
244
|
done(frame) {
|
|
223
245
|
const [doneError] = require_lib_errors_index.mightThrowSync(() => {
|
|
@@ -226,11 +248,10 @@ var NodePromptRuntime = class {
|
|
|
226
248
|
this.stdout.write(`${frame}\n`);
|
|
227
249
|
this.previousLines = 0;
|
|
228
250
|
});
|
|
229
|
-
if (doneError)
|
|
230
|
-
return require_lib_errors_index.ok(void 0);
|
|
251
|
+
if (doneError) throw new PromptIOError("Unable to finalize prompt frame");
|
|
231
252
|
}
|
|
232
253
|
close() {
|
|
233
|
-
if (!this.initialized) return
|
|
254
|
+
if (!this.initialized) return;
|
|
234
255
|
this.initialized = false;
|
|
235
256
|
const [closeError] = require_lib_errors_index.mightThrowSync(() => {
|
|
236
257
|
this.stdin.off("data", this.onData);
|
|
@@ -238,8 +259,7 @@ var NodePromptRuntime = class {
|
|
|
238
259
|
this.stdin.pause?.();
|
|
239
260
|
this.stdout.write(SHOW_CURSOR);
|
|
240
261
|
});
|
|
241
|
-
if (closeError)
|
|
242
|
-
return require_lib_errors_index.ok(void 0);
|
|
262
|
+
if (closeError) throw new PromptIOError("Unable to close prompt runtime");
|
|
243
263
|
}
|
|
244
264
|
interrupt() {
|
|
245
265
|
this.close();
|
|
@@ -251,13 +271,13 @@ var NodePromptRuntime = class {
|
|
|
251
271
|
this.buffer = "";
|
|
252
272
|
const [parseError, result] = require_lib_errors_index.mightThrowSync(() => parseKeys(combined));
|
|
253
273
|
if (parseError || !result) {
|
|
254
|
-
for (const waiter of this.waiters.splice(0)) waiter
|
|
274
|
+
for (const waiter of this.waiters.splice(0)) waiter.reject(new PromptIOError("Failed to parse input"));
|
|
255
275
|
return;
|
|
256
276
|
}
|
|
257
277
|
this.buffer = result.remaining;
|
|
258
278
|
for (const key of result.keys) {
|
|
259
279
|
const waiter = this.waiters.shift();
|
|
260
|
-
if (waiter) waiter
|
|
280
|
+
if (waiter) waiter.resolve(key);
|
|
261
281
|
else this.queue.push(key);
|
|
262
282
|
}
|
|
263
283
|
};
|
|
@@ -269,17 +289,17 @@ const createNodePromptRuntime = () => {
|
|
|
269
289
|
//#region src/lib/prompts/core/session.ts
|
|
270
290
|
const CANCEL_MESSAGE = "Interrupted, bye!";
|
|
271
291
|
const resolveOutput = async (options, value) => {
|
|
272
|
-
if (!options.transform) return
|
|
292
|
+
if (!options.transform) return value;
|
|
273
293
|
const [transformError, transformed] = await require_lib_errors_index.mightThrow(Promise.resolve().then(() => options.transform?.(value)));
|
|
274
|
-
if (transformError || transformed === null || transformed === void 0)
|
|
275
|
-
return
|
|
294
|
+
if (transformError || transformed === null || transformed === void 0) throw new PromptIOError("Prompt transform callback failed unexpectedly");
|
|
295
|
+
return transformed;
|
|
276
296
|
};
|
|
277
297
|
const runValidation = async (options, value) => {
|
|
278
|
-
if (!options.validate) return
|
|
298
|
+
if (!options.validate) return null;
|
|
279
299
|
const [validationError, validationMessage] = await require_lib_errors_index.mightThrow(Promise.resolve().then(() => options.validate?.(value)));
|
|
280
|
-
if (validationError)
|
|
281
|
-
if (typeof validationMessage === "string" && validationMessage.length > 0) return
|
|
282
|
-
return
|
|
300
|
+
if (validationError) throw new PromptIOError("Prompt validate callback failed unexpectedly");
|
|
301
|
+
if (typeof validationMessage === "string" && validationMessage.length > 0) return validationMessage;
|
|
302
|
+
return null;
|
|
283
303
|
};
|
|
284
304
|
const isCancelKey = (key) => {
|
|
285
305
|
if (key.name === "ctrl_c") return true;
|
|
@@ -287,79 +307,59 @@ const isCancelKey = (key) => {
|
|
|
287
307
|
return false;
|
|
288
308
|
};
|
|
289
309
|
const runPromptSession = async (params) => {
|
|
290
|
-
|
|
291
|
-
if (initError) return require_lib_errors_index.err(initError.type, initError.message);
|
|
310
|
+
params.runtime.init();
|
|
292
311
|
let state = params.initialState;
|
|
293
312
|
let errorMessage = null;
|
|
294
313
|
const closeAndDone = (message) => {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
const [doneErr] = params.runtime.done(message);
|
|
298
|
-
if (doneErr) return require_lib_errors_index.err(doneErr.type, doneErr.message);
|
|
299
|
-
return null;
|
|
314
|
+
params.runtime.close();
|
|
315
|
+
params.runtime.done(message);
|
|
300
316
|
};
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
params.runtime.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
if (isCancelKey(key)) {
|
|
317
|
-
const [closeError] = params.runtime.close();
|
|
318
|
-
if (closeError) return require_lib_errors_index.err(closeError.type, closeError.message);
|
|
319
|
-
const [doneError] = params.runtime.done(`✖ ${CANCEL_MESSAGE}`);
|
|
320
|
-
if (doneError) return require_lib_errors_index.err(doneError.type, doneError.message);
|
|
321
|
-
if (params.runtime.interrupt) {
|
|
322
|
-
const [interruptError] = params.runtime.interrupt(CANCEL_MESSAGE);
|
|
323
|
-
if (interruptError) return require_lib_errors_index.err(interruptError.type, interruptError.message);
|
|
317
|
+
const [sessionError, finalValue] = await require_lib_errors_index.mightThrow((async () => {
|
|
318
|
+
while (true) {
|
|
319
|
+
params.runtime.render(params.render({
|
|
320
|
+
options: params.options,
|
|
321
|
+
state,
|
|
322
|
+
errorMessage
|
|
323
|
+
}));
|
|
324
|
+
const key = await params.runtime.readKey();
|
|
325
|
+
if (!key) throw new PromptIOError("Unable to read prompt input");
|
|
326
|
+
if (isCancelKey(key)) {
|
|
327
|
+
params.runtime.close();
|
|
328
|
+
params.runtime.done(`✖ ${CANCEL_MESSAGE}`);
|
|
329
|
+
if (params.runtime.interrupt) params.runtime.interrupt(CANCEL_MESSAGE);
|
|
330
|
+
throw new PromptCancelledError(CANCEL_MESSAGE);
|
|
324
331
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (transformError) {
|
|
350
|
-
const closeDoneErr = closeAndDone(`✖ ${params.options.message}`);
|
|
351
|
-
if (closeDoneErr) return closeDoneErr;
|
|
352
|
-
return require_lib_errors_index.err(transformError.type, transformError.message);
|
|
332
|
+
if (key.name !== "enter") {
|
|
333
|
+
state = params.onKey(state, key);
|
|
334
|
+
errorMessage = null;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const submitResult = params.onSubmit(state);
|
|
338
|
+
if ("errorMessage" in submitResult) {
|
|
339
|
+
errorMessage = submitResult.errorMessage;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const rawValue = submitResult.value;
|
|
343
|
+
const validationErrorMessage = await runValidation(params.options, rawValue);
|
|
344
|
+
if (validationErrorMessage) {
|
|
345
|
+
errorMessage = validationErrorMessage;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const outputValue = await resolveOutput(params.options, rawValue);
|
|
349
|
+
if (outputValue === null) throw new PromptIOError("Unable to transform prompt value");
|
|
350
|
+
closeAndDone(params.complete({
|
|
351
|
+
options: params.options,
|
|
352
|
+
state,
|
|
353
|
+
value: outputValue
|
|
354
|
+
}));
|
|
355
|
+
return outputValue;
|
|
353
356
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
value: finalValue
|
|
359
|
-
}));
|
|
360
|
-
if (closeDoneErr) return closeDoneErr;
|
|
361
|
-
return require_lib_errors_index.ok(finalValue);
|
|
357
|
+
})());
|
|
358
|
+
if (sessionError) {
|
|
359
|
+
params.runtime.close();
|
|
360
|
+
throw sessionError;
|
|
362
361
|
}
|
|
362
|
+
return finalValue;
|
|
363
363
|
};
|
|
364
364
|
//#endregion
|
|
365
365
|
//#region src/lib/prompts/index.ts
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { i as ok, t as err } from "../../index-DxSbeGP-.cjs";
|
|
2
|
-
|
|
3
1
|
//#region src/lib/prompts/core/types.d.ts
|
|
4
|
-
type PromptResultLike<T> = ReturnType<typeof err<string>> | ReturnType<typeof ok<T>>;
|
|
5
2
|
type KeyName = "character" | "enter" | "backspace" | "delete" | "up" | "down" | "left" | "right" | "ctrl_left" | "ctrl_right" | "shift_left" | "shift_right" | "space" | "escape" | "ctrl_c" | "ctrl_backspace" | "ctrl_a" | "home" | "end" | "shift_ctrl_left" | "shift_ctrl_right" | "tab";
|
|
6
3
|
type Key = {
|
|
7
4
|
name: KeyName;
|
|
@@ -9,12 +6,12 @@ type Key = {
|
|
|
9
6
|
};
|
|
10
7
|
type PromptRuntime = {
|
|
11
8
|
isInteractive: () => boolean;
|
|
12
|
-
init: () =>
|
|
13
|
-
readKey: () => Promise<
|
|
14
|
-
render: (frame: string) =>
|
|
15
|
-
done: (frame: string) =>
|
|
16
|
-
close: () =>
|
|
17
|
-
interrupt?: (message: string) =>
|
|
9
|
+
init: () => void;
|
|
10
|
+
readKey: () => Promise<Key>;
|
|
11
|
+
render: (frame: string) => void;
|
|
12
|
+
done: (frame: string) => void;
|
|
13
|
+
close: () => void;
|
|
14
|
+
interrupt?: (message: string) => void;
|
|
18
15
|
};
|
|
19
16
|
//#endregion
|
|
20
17
|
//#region src/lib/prompts/types.d.ts
|
|
@@ -68,29 +65,11 @@ type MultiselectOptions<TValue extends string> = BasePromptOptions<TValue[]> & {
|
|
|
68
65
|
};
|
|
69
66
|
//#endregion
|
|
70
67
|
//#region src/lib/prompts/index.d.ts
|
|
71
|
-
declare const input: (options: InputOptions, runtime?: PromptRuntime) => Promise<
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
declare const
|
|
76
|
-
|
|
77
|
-
readonly message: string;
|
|
78
|
-
}, null]>;
|
|
79
|
-
declare const confirm: (options: ConfirmOptions, runtime?: PromptRuntime) => Promise<readonly [null, boolean] | readonly [{
|
|
80
|
-
readonly type: string;
|
|
81
|
-
readonly message: string;
|
|
82
|
-
}, null]>;
|
|
83
|
-
declare const number: (options: NumberOptions, runtime?: PromptRuntime) => Promise<readonly [null, number] | readonly [{
|
|
84
|
-
readonly type: string;
|
|
85
|
-
readonly message: string;
|
|
86
|
-
}, null]>;
|
|
87
|
-
declare const select: <TValue extends string>(options: SelectOptions<TValue>, runtime?: PromptRuntime) => Promise<readonly [{
|
|
88
|
-
readonly type: string;
|
|
89
|
-
readonly message: string;
|
|
90
|
-
}, null] | readonly [null, TValue]>;
|
|
91
|
-
declare const multiselect: <TValue extends string>(options: MultiselectOptions<TValue>, runtime?: PromptRuntime) => Promise<readonly [{
|
|
92
|
-
readonly type: string;
|
|
93
|
-
readonly message: string;
|
|
94
|
-
}, null] | readonly [null, TValue[]]>;
|
|
68
|
+
declare const input: (options: InputOptions, runtime?: PromptRuntime) => Promise<string>;
|
|
69
|
+
declare const password: (options: PasswordOptions, runtime?: PromptRuntime) => Promise<string>;
|
|
70
|
+
declare const confirm: (options: ConfirmOptions, runtime?: PromptRuntime) => Promise<boolean>;
|
|
71
|
+
declare const number: (options: NumberOptions, runtime?: PromptRuntime) => Promise<number>;
|
|
72
|
+
declare const select: <TValue extends string>(options: SelectOptions<TValue>, runtime?: PromptRuntime) => Promise<Awaited<TValue> & ({} | undefined)>;
|
|
73
|
+
declare const multiselect: <TValue extends string>(options: MultiselectOptions<TValue>, runtime?: PromptRuntime) => Promise<TValue[]>;
|
|
95
74
|
//#endregion
|
|
96
75
|
export { type ConfirmOptions, type InputOptions, type MultiselectOptions, type NumberOptions, type PasswordOptions, type PromptErrorType, type PromptRuntime, type SelectChoice, type SelectOptions, confirm, input, multiselect, number, password, select };
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { i as ok, t as err } from "../../index-BhGNDjPq.mjs";
|
|
2
|
-
|
|
3
1
|
//#region src/lib/prompts/core/types.d.ts
|
|
4
|
-
type PromptResultLike<T> = ReturnType<typeof err<string>> | ReturnType<typeof ok<T>>;
|
|
5
2
|
type KeyName = "character" | "enter" | "backspace" | "delete" | "up" | "down" | "left" | "right" | "ctrl_left" | "ctrl_right" | "shift_left" | "shift_right" | "space" | "escape" | "ctrl_c" | "ctrl_backspace" | "ctrl_a" | "home" | "end" | "shift_ctrl_left" | "shift_ctrl_right" | "tab";
|
|
6
3
|
type Key = {
|
|
7
4
|
name: KeyName;
|
|
@@ -9,12 +6,12 @@ type Key = {
|
|
|
9
6
|
};
|
|
10
7
|
type PromptRuntime = {
|
|
11
8
|
isInteractive: () => boolean;
|
|
12
|
-
init: () =>
|
|
13
|
-
readKey: () => Promise<
|
|
14
|
-
render: (frame: string) =>
|
|
15
|
-
done: (frame: string) =>
|
|
16
|
-
close: () =>
|
|
17
|
-
interrupt?: (message: string) =>
|
|
9
|
+
init: () => void;
|
|
10
|
+
readKey: () => Promise<Key>;
|
|
11
|
+
render: (frame: string) => void;
|
|
12
|
+
done: (frame: string) => void;
|
|
13
|
+
close: () => void;
|
|
14
|
+
interrupt?: (message: string) => void;
|
|
18
15
|
};
|
|
19
16
|
//#endregion
|
|
20
17
|
//#region src/lib/prompts/types.d.ts
|
|
@@ -68,29 +65,11 @@ type MultiselectOptions<TValue extends string> = BasePromptOptions<TValue[]> & {
|
|
|
68
65
|
};
|
|
69
66
|
//#endregion
|
|
70
67
|
//#region src/lib/prompts/index.d.ts
|
|
71
|
-
declare const input: (options: InputOptions, runtime?: PromptRuntime) => Promise<
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
declare const
|
|
76
|
-
|
|
77
|
-
readonly message: string;
|
|
78
|
-
}, null]>;
|
|
79
|
-
declare const confirm: (options: ConfirmOptions, runtime?: PromptRuntime) => Promise<readonly [null, boolean] | readonly [{
|
|
80
|
-
readonly type: string;
|
|
81
|
-
readonly message: string;
|
|
82
|
-
}, null]>;
|
|
83
|
-
declare const number: (options: NumberOptions, runtime?: PromptRuntime) => Promise<readonly [null, number] | readonly [{
|
|
84
|
-
readonly type: string;
|
|
85
|
-
readonly message: string;
|
|
86
|
-
}, null]>;
|
|
87
|
-
declare const select: <TValue extends string>(options: SelectOptions<TValue>, runtime?: PromptRuntime) => Promise<readonly [{
|
|
88
|
-
readonly type: string;
|
|
89
|
-
readonly message: string;
|
|
90
|
-
}, null] | readonly [null, TValue]>;
|
|
91
|
-
declare const multiselect: <TValue extends string>(options: MultiselectOptions<TValue>, runtime?: PromptRuntime) => Promise<readonly [{
|
|
92
|
-
readonly type: string;
|
|
93
|
-
readonly message: string;
|
|
94
|
-
}, null] | readonly [null, TValue[]]>;
|
|
68
|
+
declare const input: (options: InputOptions, runtime?: PromptRuntime) => Promise<string>;
|
|
69
|
+
declare const password: (options: PasswordOptions, runtime?: PromptRuntime) => Promise<string>;
|
|
70
|
+
declare const confirm: (options: ConfirmOptions, runtime?: PromptRuntime) => Promise<boolean>;
|
|
71
|
+
declare const number: (options: NumberOptions, runtime?: PromptRuntime) => Promise<number>;
|
|
72
|
+
declare const select: <TValue extends string>(options: SelectOptions<TValue>, runtime?: PromptRuntime) => Promise<Awaited<TValue> & ({} | undefined)>;
|
|
73
|
+
declare const multiselect: <TValue extends string>(options: MultiselectOptions<TValue>, runtime?: PromptRuntime) => Promise<TValue[]>;
|
|
95
74
|
//#endregion
|
|
96
75
|
export { type ConfirmOptions, type InputOptions, type MultiselectOptions, type NumberOptions, type PasswordOptions, type PromptErrorType, type PromptRuntime, type SelectChoice, type SelectOptions, confirm, input, multiselect, number, password, select };
|
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mightThrow, mightThrowSync } from "../errors/index.mjs";
|
|
2
2
|
import { styleText } from "node:util";
|
|
3
|
+
//#region src/lib/prompts/errors.ts
|
|
4
|
+
var PromptIOError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "PromptIOError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var PromptEnvironmentError = class extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "PromptEnvironmentError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var PromptCancelledError = class extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "PromptCancelledError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
3
23
|
//#region src/lib/prompts/core/keys.ts
|
|
4
24
|
const ESC = "\x1B";
|
|
5
25
|
const CSI = "\x1B[";
|
|
@@ -188,8 +208,8 @@ var NodePromptRuntime = class {
|
|
|
188
208
|
return Boolean(this.stdin.isTTY && this.stdout.isTTY && typeof this.stdin.setRawMode === "function");
|
|
189
209
|
}
|
|
190
210
|
init() {
|
|
191
|
-
if (!this.isInteractive())
|
|
192
|
-
if (this.initialized) return
|
|
211
|
+
if (!this.isInteractive()) throw new PromptEnvironmentError("Interactive prompts require a TTY with raw mode support");
|
|
212
|
+
if (this.initialized) return;
|
|
193
213
|
const [initError] = mightThrowSync(() => {
|
|
194
214
|
this.stdin.setRawMode(true);
|
|
195
215
|
this.stdin.setEncoding("utf8");
|
|
@@ -197,15 +217,17 @@ var NodePromptRuntime = class {
|
|
|
197
217
|
this.stdin.on("data", this.onData);
|
|
198
218
|
this.stdout.write(HIDE_CURSOR);
|
|
199
219
|
});
|
|
200
|
-
if (initError)
|
|
220
|
+
if (initError) throw new PromptIOError("Unable to initialize prompt runtime");
|
|
201
221
|
this.initialized = true;
|
|
202
|
-
return ok(void 0);
|
|
203
222
|
}
|
|
204
223
|
readKey() {
|
|
205
224
|
const queued = this.queue.shift();
|
|
206
|
-
if (queued) return Promise.resolve(
|
|
207
|
-
return new Promise((resolve) => {
|
|
208
|
-
this.waiters.push(
|
|
225
|
+
if (queued) return Promise.resolve(queued);
|
|
226
|
+
return new Promise((resolve, reject) => {
|
|
227
|
+
this.waiters.push({
|
|
228
|
+
resolve,
|
|
229
|
+
reject
|
|
230
|
+
});
|
|
209
231
|
});
|
|
210
232
|
}
|
|
211
233
|
render(frame) {
|
|
@@ -215,8 +237,7 @@ var NodePromptRuntime = class {
|
|
|
215
237
|
this.stdout.write(frame);
|
|
216
238
|
this.previousLines = countLines(frame);
|
|
217
239
|
});
|
|
218
|
-
if (renderError)
|
|
219
|
-
return ok(void 0);
|
|
240
|
+
if (renderError) throw new PromptIOError("Unable to render prompt frame");
|
|
220
241
|
}
|
|
221
242
|
done(frame) {
|
|
222
243
|
const [doneError] = mightThrowSync(() => {
|
|
@@ -225,11 +246,10 @@ var NodePromptRuntime = class {
|
|
|
225
246
|
this.stdout.write(`${frame}\n`);
|
|
226
247
|
this.previousLines = 0;
|
|
227
248
|
});
|
|
228
|
-
if (doneError)
|
|
229
|
-
return ok(void 0);
|
|
249
|
+
if (doneError) throw new PromptIOError("Unable to finalize prompt frame");
|
|
230
250
|
}
|
|
231
251
|
close() {
|
|
232
|
-
if (!this.initialized) return
|
|
252
|
+
if (!this.initialized) return;
|
|
233
253
|
this.initialized = false;
|
|
234
254
|
const [closeError] = mightThrowSync(() => {
|
|
235
255
|
this.stdin.off("data", this.onData);
|
|
@@ -237,8 +257,7 @@ var NodePromptRuntime = class {
|
|
|
237
257
|
this.stdin.pause?.();
|
|
238
258
|
this.stdout.write(SHOW_CURSOR);
|
|
239
259
|
});
|
|
240
|
-
if (closeError)
|
|
241
|
-
return ok(void 0);
|
|
260
|
+
if (closeError) throw new PromptIOError("Unable to close prompt runtime");
|
|
242
261
|
}
|
|
243
262
|
interrupt() {
|
|
244
263
|
this.close();
|
|
@@ -250,13 +269,13 @@ var NodePromptRuntime = class {
|
|
|
250
269
|
this.buffer = "";
|
|
251
270
|
const [parseError, result] = mightThrowSync(() => parseKeys(combined));
|
|
252
271
|
if (parseError || !result) {
|
|
253
|
-
for (const waiter of this.waiters.splice(0)) waiter(
|
|
272
|
+
for (const waiter of this.waiters.splice(0)) waiter.reject(new PromptIOError("Failed to parse input"));
|
|
254
273
|
return;
|
|
255
274
|
}
|
|
256
275
|
this.buffer = result.remaining;
|
|
257
276
|
for (const key of result.keys) {
|
|
258
277
|
const waiter = this.waiters.shift();
|
|
259
|
-
if (waiter) waiter(
|
|
278
|
+
if (waiter) waiter.resolve(key);
|
|
260
279
|
else this.queue.push(key);
|
|
261
280
|
}
|
|
262
281
|
};
|
|
@@ -268,17 +287,17 @@ const createNodePromptRuntime = () => {
|
|
|
268
287
|
//#region src/lib/prompts/core/session.ts
|
|
269
288
|
const CANCEL_MESSAGE = "Interrupted, bye!";
|
|
270
289
|
const resolveOutput = async (options, value) => {
|
|
271
|
-
if (!options.transform) return
|
|
290
|
+
if (!options.transform) return value;
|
|
272
291
|
const [transformError, transformed] = await mightThrow(Promise.resolve().then(() => options.transform?.(value)));
|
|
273
|
-
if (transformError || transformed === null || transformed === void 0)
|
|
274
|
-
return
|
|
292
|
+
if (transformError || transformed === null || transformed === void 0) throw new PromptIOError("Prompt transform callback failed unexpectedly");
|
|
293
|
+
return transformed;
|
|
275
294
|
};
|
|
276
295
|
const runValidation = async (options, value) => {
|
|
277
|
-
if (!options.validate) return
|
|
296
|
+
if (!options.validate) return null;
|
|
278
297
|
const [validationError, validationMessage] = await mightThrow(Promise.resolve().then(() => options.validate?.(value)));
|
|
279
|
-
if (validationError)
|
|
280
|
-
if (typeof validationMessage === "string" && validationMessage.length > 0) return
|
|
281
|
-
return
|
|
298
|
+
if (validationError) throw new PromptIOError("Prompt validate callback failed unexpectedly");
|
|
299
|
+
if (typeof validationMessage === "string" && validationMessage.length > 0) return validationMessage;
|
|
300
|
+
return null;
|
|
282
301
|
};
|
|
283
302
|
const isCancelKey = (key) => {
|
|
284
303
|
if (key.name === "ctrl_c") return true;
|
|
@@ -286,79 +305,59 @@ const isCancelKey = (key) => {
|
|
|
286
305
|
return false;
|
|
287
306
|
};
|
|
288
307
|
const runPromptSession = async (params) => {
|
|
289
|
-
|
|
290
|
-
if (initError) return err(initError.type, initError.message);
|
|
308
|
+
params.runtime.init();
|
|
291
309
|
let state = params.initialState;
|
|
292
310
|
let errorMessage = null;
|
|
293
311
|
const closeAndDone = (message) => {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const [doneErr] = params.runtime.done(message);
|
|
297
|
-
if (doneErr) return err(doneErr.type, doneErr.message);
|
|
298
|
-
return null;
|
|
312
|
+
params.runtime.close();
|
|
313
|
+
params.runtime.done(message);
|
|
299
314
|
};
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
params.runtime.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
if (isCancelKey(key)) {
|
|
316
|
-
const [closeError] = params.runtime.close();
|
|
317
|
-
if (closeError) return err(closeError.type, closeError.message);
|
|
318
|
-
const [doneError] = params.runtime.done(`✖ ${CANCEL_MESSAGE}`);
|
|
319
|
-
if (doneError) return err(doneError.type, doneError.message);
|
|
320
|
-
if (params.runtime.interrupt) {
|
|
321
|
-
const [interruptError] = params.runtime.interrupt(CANCEL_MESSAGE);
|
|
322
|
-
if (interruptError) return err(interruptError.type, interruptError.message);
|
|
315
|
+
const [sessionError, finalValue] = await mightThrow((async () => {
|
|
316
|
+
while (true) {
|
|
317
|
+
params.runtime.render(params.render({
|
|
318
|
+
options: params.options,
|
|
319
|
+
state,
|
|
320
|
+
errorMessage
|
|
321
|
+
}));
|
|
322
|
+
const key = await params.runtime.readKey();
|
|
323
|
+
if (!key) throw new PromptIOError("Unable to read prompt input");
|
|
324
|
+
if (isCancelKey(key)) {
|
|
325
|
+
params.runtime.close();
|
|
326
|
+
params.runtime.done(`✖ ${CANCEL_MESSAGE}`);
|
|
327
|
+
if (params.runtime.interrupt) params.runtime.interrupt(CANCEL_MESSAGE);
|
|
328
|
+
throw new PromptCancelledError(CANCEL_MESSAGE);
|
|
323
329
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (transformError) {
|
|
349
|
-
const closeDoneErr = closeAndDone(`✖ ${params.options.message}`);
|
|
350
|
-
if (closeDoneErr) return closeDoneErr;
|
|
351
|
-
return err(transformError.type, transformError.message);
|
|
330
|
+
if (key.name !== "enter") {
|
|
331
|
+
state = params.onKey(state, key);
|
|
332
|
+
errorMessage = null;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const submitResult = params.onSubmit(state);
|
|
336
|
+
if ("errorMessage" in submitResult) {
|
|
337
|
+
errorMessage = submitResult.errorMessage;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const rawValue = submitResult.value;
|
|
341
|
+
const validationErrorMessage = await runValidation(params.options, rawValue);
|
|
342
|
+
if (validationErrorMessage) {
|
|
343
|
+
errorMessage = validationErrorMessage;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const outputValue = await resolveOutput(params.options, rawValue);
|
|
347
|
+
if (outputValue === null) throw new PromptIOError("Unable to transform prompt value");
|
|
348
|
+
closeAndDone(params.complete({
|
|
349
|
+
options: params.options,
|
|
350
|
+
state,
|
|
351
|
+
value: outputValue
|
|
352
|
+
}));
|
|
353
|
+
return outputValue;
|
|
352
354
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
value: finalValue
|
|
358
|
-
}));
|
|
359
|
-
if (closeDoneErr) return closeDoneErr;
|
|
360
|
-
return ok(finalValue);
|
|
355
|
+
})());
|
|
356
|
+
if (sessionError) {
|
|
357
|
+
params.runtime.close();
|
|
358
|
+
throw sessionError;
|
|
361
359
|
}
|
|
360
|
+
return finalValue;
|
|
362
361
|
};
|
|
363
362
|
//#endregion
|
|
364
363
|
//#region src/lib/prompts/index.ts
|