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.
Files changed (36) hide show
  1. package/README.md +18 -45
  2. package/dist/chunk-CKQMccvm.cjs +28 -0
  3. package/dist/lib/api/index.cjs +29 -15
  4. package/dist/lib/api/index.mjs +30 -16
  5. package/dist/lib/cache/index.cjs +47 -22
  6. package/dist/lib/cache/index.d.cts +3 -24
  7. package/dist/lib/cache/index.d.mts +3 -24
  8. package/dist/lib/cache/index.mjs +48 -23
  9. package/dist/lib/cron/index.cjs +117 -117
  10. package/dist/lib/cron/index.mjs +118 -118
  11. package/dist/lib/errors/index.d.cts +12 -1
  12. package/dist/lib/errors/index.d.mts +12 -1
  13. package/dist/lib/logging/index.cjs +1 -0
  14. package/dist/lib/orm/index.cjs +1642 -0
  15. package/dist/lib/orm/index.d.cts +402 -0
  16. package/dist/lib/orm/index.d.mts +402 -0
  17. package/dist/lib/orm/index.mjs +1630 -0
  18. package/dist/lib/prompts/index.cjs +89 -89
  19. package/dist/lib/prompts/index.d.cts +12 -33
  20. package/dist/lib/prompts/index.d.mts +12 -33
  21. package/dist/lib/prompts/index.mjs +89 -90
  22. package/dist/lib/pubsub/index.cjs +43 -19
  23. package/dist/lib/pubsub/index.d.cts +3 -18
  24. package/dist/lib/pubsub/index.d.mts +3 -18
  25. package/dist/lib/pubsub/index.mjs +44 -20
  26. package/dist/lib/queue/index.cjs +40 -10
  27. package/dist/lib/queue/index.d.cts +11 -4
  28. package/dist/lib/queue/index.d.mts +11 -4
  29. package/dist/lib/queue/index.mjs +39 -11
  30. package/dist/lib/workflow/index.cjs +285 -282
  31. package/dist/lib/workflow/index.d.cts +76 -11
  32. package/dist/lib/workflow/index.d.mts +76 -11
  33. package/dist/lib/workflow/index.mjs +278 -284
  34. package/package.json +11 -1
  35. package/dist/index-BhGNDjPq.d.mts +0 -13
  36. 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()) return require_lib_errors_index.err("PromptEnvironmentError", "Interactive prompts require a TTY with raw mode support");
193
- if (this.initialized) return require_lib_errors_index.ok(void 0);
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) return require_lib_errors_index.err("PromptIOError", "Unable to initialize prompt runtime");
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(require_lib_errors_index.ok(queued));
208
- return new Promise((resolve) => {
209
- this.waiters.push(resolve);
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) return require_lib_errors_index.err("PromptIOError", "Unable to render prompt frame");
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) return require_lib_errors_index.err("PromptIOError", "Unable to finalize prompt frame");
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 require_lib_errors_index.ok(void 0);
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) return require_lib_errors_index.err("PromptIOError", "Unable to close prompt runtime");
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(require_lib_errors_index.err("PromptIOError", "Failed to parse input"));
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(require_lib_errors_index.ok(key));
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 require_lib_errors_index.ok(value);
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) return require_lib_errors_index.err("PromptIOError", "Prompt transform callback failed unexpectedly");
275
- return require_lib_errors_index.ok(transformed);
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 require_lib_errors_index.ok(null);
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) return require_lib_errors_index.err("PromptIOError", "Prompt validate callback failed unexpectedly");
281
- if (typeof validationMessage === "string" && validationMessage.length > 0) return require_lib_errors_index.ok(validationMessage);
282
- return require_lib_errors_index.ok(null);
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
- const [initError] = params.runtime.init();
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
- const [closeErr] = params.runtime.close();
296
- if (closeErr) return require_lib_errors_index.err(closeErr.type, closeErr.message);
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
- while (true) {
302
- const [renderError] = params.runtime.render(params.render({
303
- options: params.options,
304
- state,
305
- errorMessage
306
- }));
307
- if (renderError) {
308
- params.runtime.close();
309
- return require_lib_errors_index.err(renderError.type, renderError.message);
310
- }
311
- const [readError, key] = await params.runtime.readKey();
312
- if (readError || !key) {
313
- params.runtime.close();
314
- return require_lib_errors_index.err(readError?.type ?? "PromptIOError", readError?.message ?? "Unable to read prompt input");
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
- return require_lib_errors_index.err("PromptCancelledError", CANCEL_MESSAGE);
326
- }
327
- if (key.name !== "enter") {
328
- state = params.onKey(state, key);
329
- errorMessage = null;
330
- continue;
331
- }
332
- const submitResult = params.onSubmit(state);
333
- if ("errorMessage" in submitResult) {
334
- errorMessage = submitResult.errorMessage;
335
- continue;
336
- }
337
- const rawValue = submitResult.value;
338
- const [validationRunError, validationErrorMessage] = await runValidation(params.options, rawValue);
339
- if (validationRunError) {
340
- const closeDoneErr = closeAndDone(`✖ ${params.options.message}`);
341
- if (closeDoneErr) return closeDoneErr;
342
- return require_lib_errors_index.err(validationRunError.type, validationRunError.message);
343
- }
344
- if (validationErrorMessage) {
345
- errorMessage = validationErrorMessage;
346
- continue;
347
- }
348
- const [transformError, finalValue] = await resolveOutput(params.options, rawValue);
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
- if (finalValue === null) return require_lib_errors_index.err("PromptIOError", "Unable to transform prompt value");
355
- const closeDoneErr = closeAndDone(params.complete({
356
- options: params.options,
357
- state,
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: () => PromptResultLike<void>;
13
- readKey: () => Promise<PromptResultLike<Key>>;
14
- render: (frame: string) => PromptResultLike<void>;
15
- done: (frame: string) => PromptResultLike<void>;
16
- close: () => PromptResultLike<void>;
17
- interrupt?: (message: string) => PromptResultLike<undefined>;
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<readonly [null, string] | readonly [{
72
- readonly type: string;
73
- readonly message: string;
74
- }, null]>;
75
- declare const password: (options: PasswordOptions, runtime?: PromptRuntime) => Promise<readonly [null, string] | readonly [{
76
- readonly type: string;
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: () => PromptResultLike<void>;
13
- readKey: () => Promise<PromptResultLike<Key>>;
14
- render: (frame: string) => PromptResultLike<void>;
15
- done: (frame: string) => PromptResultLike<void>;
16
- close: () => PromptResultLike<void>;
17
- interrupt?: (message: string) => PromptResultLike<undefined>;
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<readonly [null, string] | readonly [{
72
- readonly type: string;
73
- readonly message: string;
74
- }, null]>;
75
- declare const password: (options: PasswordOptions, runtime?: PromptRuntime) => Promise<readonly [null, string] | readonly [{
76
- readonly type: string;
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 { err, mightThrow, mightThrowSync, ok } from "../errors/index.mjs";
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()) return err("PromptEnvironmentError", "Interactive prompts require a TTY with raw mode support");
192
- if (this.initialized) return ok(void 0);
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) return err("PromptIOError", "Unable to initialize prompt runtime");
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(ok(queued));
207
- return new Promise((resolve) => {
208
- this.waiters.push(resolve);
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) return err("PromptIOError", "Unable to render prompt frame");
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) return err("PromptIOError", "Unable to finalize prompt frame");
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 ok(void 0);
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) return err("PromptIOError", "Unable to close prompt runtime");
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(err("PromptIOError", "Failed to parse input"));
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(ok(key));
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 ok(value);
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) return err("PromptIOError", "Prompt transform callback failed unexpectedly");
274
- return ok(transformed);
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 ok(null);
296
+ if (!options.validate) return null;
278
297
  const [validationError, validationMessage] = await mightThrow(Promise.resolve().then(() => options.validate?.(value)));
279
- if (validationError) return err("PromptIOError", "Prompt validate callback failed unexpectedly");
280
- if (typeof validationMessage === "string" && validationMessage.length > 0) return ok(validationMessage);
281
- return ok(null);
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
- const [initError] = params.runtime.init();
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
- const [closeErr] = params.runtime.close();
295
- if (closeErr) return err(closeErr.type, closeErr.message);
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
- while (true) {
301
- const [renderError] = params.runtime.render(params.render({
302
- options: params.options,
303
- state,
304
- errorMessage
305
- }));
306
- if (renderError) {
307
- params.runtime.close();
308
- return err(renderError.type, renderError.message);
309
- }
310
- const [readError, key] = await params.runtime.readKey();
311
- if (readError || !key) {
312
- params.runtime.close();
313
- return err(readError?.type ?? "PromptIOError", readError?.message ?? "Unable to read prompt input");
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
- return err("PromptCancelledError", CANCEL_MESSAGE);
325
- }
326
- if (key.name !== "enter") {
327
- state = params.onKey(state, key);
328
- errorMessage = null;
329
- continue;
330
- }
331
- const submitResult = params.onSubmit(state);
332
- if ("errorMessage" in submitResult) {
333
- errorMessage = submitResult.errorMessage;
334
- continue;
335
- }
336
- const rawValue = submitResult.value;
337
- const [validationRunError, validationErrorMessage] = await runValidation(params.options, rawValue);
338
- if (validationRunError) {
339
- const closeDoneErr = closeAndDone(`✖ ${params.options.message}`);
340
- if (closeDoneErr) return closeDoneErr;
341
- return err(validationRunError.type, validationRunError.message);
342
- }
343
- if (validationErrorMessage) {
344
- errorMessage = validationErrorMessage;
345
- continue;
346
- }
347
- const [transformError, finalValue] = await resolveOutput(params.options, rawValue);
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
- if (finalValue === null) return err("PromptIOError", "Unable to transform prompt value");
354
- const closeDoneErr = closeAndDone(params.complete({
355
- options: params.options,
356
- state,
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