@optique/prompt 1.4.0-dev.2603 → 1.4.0-dev.2604

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/dist/index.cjs CHANGED
@@ -40,14 +40,22 @@ const derivedPromptConfigMarker = Symbol.for("@optique/prompt/derivedPromptConfi
40
40
  function isDerivedPromptConfig(config) {
41
41
  return config != null && typeof config === "object" && derivedPromptConfigMarker in config && config[derivedPromptConfigMarker] === true;
42
42
  }
43
- function derivePromptConfig(source, resolver, options) {
43
+ function derivePromptConfig(source, resolverOrOptions, options) {
44
+ if (typeof source === "function") return deriveNoDepsPromptConfig(source, resolverOrOptions);
45
+ const resolver = resolverOrOptions;
44
46
  const isTuple = Array.isArray(source);
45
47
  const dependencies = isTuple ? source : [source];
46
48
  if (dependencies.length === 0) throw new TypeError("derivePromptConfig() requires at least one dependency source.");
47
49
  const infos = dependencies.map(__optique_core_dependency.getDependencySourceInfo);
48
50
  const singleDefault = options?.defaultValue;
49
51
  const defaultValues = isTuple ? options?.defaultValues : singleDefault == null ? void 0 : () => [singleDefault()];
50
- const resolve = isTuple ? (values, usedDefaults) => resolver(values, { usedDefaults }) : (values, usedDefaults) => resolver(values[0], { usedDefault: usedDefaults[0] });
52
+ const resolve = isTuple ? (values, usedDefaults, context) => resolver(values, {
53
+ usedDefaults,
54
+ ...context
55
+ }) : (values, usedDefaults, context) => resolver(values[0], {
56
+ usedDefault: usedDefaults[0],
57
+ ...context
58
+ });
51
59
  return {
52
60
  [derivedPromptConfigMarker]: true,
53
61
  dependencies,
@@ -61,6 +69,19 @@ function derivePromptConfig(source, resolver, options) {
61
69
  }
62
70
  };
63
71
  }
72
+ function deriveNoDepsPromptConfig(resolver, options) {
73
+ return {
74
+ [derivedPromptConfigMarker]: true,
75
+ dependencies: [],
76
+ dependencyIds: [],
77
+ dependencyLabels: [],
78
+ resolve: (_values, _usedDefaults, context) => resolver({ ...context }),
79
+ ...options?.when == null ? {} : {
80
+ when: options.when,
81
+ otherwise: options.otherwise
82
+ }
83
+ };
84
+ }
64
85
  function describeThrown(error) {
65
86
  return error instanceof Error ? error.message : String(error);
66
87
  }
@@ -73,7 +94,7 @@ function describeThrown(error) {
73
94
  * (synchronous throws and rejections alike) become prompt failures so
74
95
  * the scheduler treats them like a cancelled prompt.
75
96
  */
76
- async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel) {
97
+ async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel, signal) {
77
98
  const runtime = exec?.dependencyRuntime;
78
99
  const ids = config.dependencyIds;
79
100
  if (runtime != null) {
@@ -124,7 +145,7 @@ async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel) {
124
145
  try {
125
146
  return {
126
147
  ok: true,
127
- config: await config.resolve(values, usedDefaults)
148
+ config: await config.resolve(values, usedDefaults, signal === void 0 ? {} : { signal })
128
149
  };
129
150
  } catch (error) {
130
151
  return {
@@ -288,8 +309,7 @@ function createPromptAdapter(adapter) {
288
309
  if (!isDerivedPromptConfig(config)) resolvedConfig = config;
289
310
  else {
290
311
  const source = promptedParser.dependencyMetadata?.source;
291
- const resolved = await resolveDerivedPromptConfig(config, exec, source?.sourceId, source?.metavar);
292
- throwIfPromptAborted(signal);
312
+ const resolved = await racePromptWorkWithAbort(signal, () => resolveDerivedPromptConfig(config, exec, source?.sourceId, source?.metavar, signal));
293
313
  if (!resolved.ok) return {
294
314
  success: false,
295
315
  error: resolved.error
package/dist/index.d.cts CHANGED
@@ -55,8 +55,11 @@ interface PromptOptions<TValue> {
55
55
  */
56
56
  readonly maxAttempts?: number;
57
57
  /**
58
- * Signal that stops the active adapter execution or validator. Its reason
59
- * is propagated to the caller without becoming a parse failure.
58
+ * Signal that stops the active adapter execution, validator, or derived
59
+ * configuration resolver. Its reason is propagated to the caller without
60
+ * becoming a parse failure. Derived configuration resolvers also receive
61
+ * it so that they can cancel their own work.
62
+ * @since 1.4.0 Also stops a pending derived configuration resolver.
60
63
  */
61
64
  readonly signal?: AbortSignal;
62
65
  }
@@ -125,6 +128,25 @@ interface DerivePromptConfigContext {
125
128
  * values and are not reported here.
126
129
  */
127
130
  readonly usedDefault: boolean;
131
+ /**
132
+ * Signal supplied through the prompt's shared options, when present.
133
+ * Pass it to cancellable work such as network requests.
134
+ * @since 1.4.0
135
+ */
136
+ readonly signal?: AbortSignal;
137
+ }
138
+ /**
139
+ * Context passed to a prompt configuration resolver that has no
140
+ * dependencies.
141
+ *
142
+ * @since 1.4.0
143
+ */
144
+ interface DerivePromptConfigNoDepsContext {
145
+ /**
146
+ * Signal supplied through the prompt's shared options, when present.
147
+ * Pass it to cancellable work such as network requests.
148
+ */
149
+ readonly signal?: AbortSignal;
128
150
  }
129
151
  /**
130
152
  * Context passed to a multi-dependency prompt configuration resolver.
@@ -139,6 +161,12 @@ interface DerivePromptConfigsContext<Deps extends readonly AnyDependencySource[]
139
161
  * than a published source value.
140
162
  */
141
163
  readonly usedDefaults: { readonly [K in keyof Deps]: boolean };
164
+ /**
165
+ * Signal supplied through the prompt's shared options, when present.
166
+ * Pass it to cancellable work such as network requests.
167
+ * @since 1.4.0
168
+ */
169
+ readonly signal?: AbortSignal;
142
170
  }
143
171
  /**
144
172
  * Options for a single-dependency {@link derivePromptConfig} call.
@@ -161,6 +189,19 @@ type DerivePromptConfigOptions<TDefault, TOtherwise> = {
161
189
  readonly when: () => boolean | Promise<boolean>;
162
190
  readonly otherwise: TOtherwise;
163
191
  });
192
+ /**
193
+ * Options for a zero-dependency {@link derivePromptConfig} call.
194
+ *
195
+ * @typeParam TOtherwise Value type returned when `when` skips the prompt.
196
+ * @since 1.4.0
197
+ */
198
+ type DerivePromptConfigNoDepsOptions<TOtherwise> = {
199
+ readonly when?: never;
200
+ readonly otherwise?: never;
201
+ } | {
202
+ readonly when: () => boolean | Promise<boolean>;
203
+ readonly otherwise: TOtherwise;
204
+ };
164
205
  /**
165
206
  * Options for a multi-dependency {@link derivePromptConfig} call.
166
207
  *
@@ -206,10 +247,11 @@ interface DerivedPromptConfig<TConfig, TOtherwise = never> {
206
247
  readonly dependencyLabels: readonly string[];
207
248
  /**
208
249
  * Resolves the adapter configuration from dependency values. Receives
209
- * one value and one used-default flag per dependency position.
250
+ * one value and one used-default flag per dependency position, and
251
+ * optionally the abort signal to forward to the resolver.
210
252
  * @internal
211
253
  */
212
- readonly resolve: (values: readonly unknown[], usedDefaults: readonly boolean[]) => TConfig | Promise<TConfig>;
254
+ readonly resolve: (values: readonly unknown[], usedDefaults: readonly boolean[], context?: DerivePromptConfigNoDepsContext) => TConfig | Promise<TConfig>;
213
255
  /**
214
256
  * Lazily evaluated fallbacks, one per dependency position.
215
257
  * @internal
@@ -229,6 +271,24 @@ interface DerivedPromptConfig<TConfig, TOtherwise = never> {
229
271
  * @since 1.3.0
230
272
  */
231
273
  declare function isDerivedPromptConfig(config: unknown): config is DerivedPromptConfig<unknown, unknown>;
274
+ /**
275
+ * Derives a prompt configuration without any dependency source.
276
+ *
277
+ * The resolver may return the configuration synchronously or
278
+ * asynchronously. It runs only during the real completion phase,
279
+ * immediately before the adapter executes, so it can load data such as
280
+ * the choices of a selection prompt without running for command-line
281
+ * values, help, or suggestions.
282
+ *
283
+ * @typeParam TConfig Adapter configuration produced by the resolver.
284
+ * @typeParam TOtherwise Value type returned when `when` skips the prompt.
285
+ * @param resolver Produces the adapter configuration. It receives the
286
+ * prompt's abort signal, when one is supplied.
287
+ * @param options Optional runtime condition.
288
+ * @returns A derived configuration accepted by `prompt()` wrappers.
289
+ * @since 1.4.0
290
+ */
291
+ declare function derivePromptConfig<TConfig, const TOtherwise = never>(resolver: (context: DerivePromptConfigNoDepsContext) => TConfig | Promise<TConfig>, options?: DerivePromptConfigNoDepsOptions<TOtherwise>): DerivedPromptConfig<TConfig, TOtherwise>;
232
292
  /**
233
293
  * Derives a prompt configuration from one dependency source value.
234
294
  *
@@ -299,4 +359,4 @@ type PromptConfigInput<TConfig, TValue> = (TConfig & PromptCondition<TValue>) |
299
359
  */
300
360
  declare function createPromptAdapter<TConfig>(adapter: PromptAdapter<TConfig>): <M extends Mode, TValue, TState>(parser: Parser<M, TValue, TState>, config: PromptConfigInput<TConfig, TValue>, options?: PromptOptions<NoInfer<TValue>>) => FluentParser<"async", TValue, TState>;
301
361
  //#endregion
302
- export { DerivePromptConfigContext, DerivePromptConfigOptions, DerivePromptConfigsContext, DerivePromptConfigsOptions, DerivedPromptConfig, PromptAdapter, PromptCondition, PromptConfigInput, PromptExecutionContext, PromptOptions, PromptValidator, createPromptAdapter, derivePromptConfig, isDerivedPromptConfig };
362
+ export { DerivePromptConfigContext, DerivePromptConfigNoDepsContext, DerivePromptConfigNoDepsOptions, DerivePromptConfigOptions, DerivePromptConfigsContext, DerivePromptConfigsOptions, DerivedPromptConfig, PromptAdapter, PromptCondition, PromptConfigInput, PromptExecutionContext, PromptOptions, PromptValidator, createPromptAdapter, derivePromptConfig, isDerivedPromptConfig };
package/dist/index.d.ts CHANGED
@@ -55,8 +55,11 @@ interface PromptOptions<TValue> {
55
55
  */
56
56
  readonly maxAttempts?: number;
57
57
  /**
58
- * Signal that stops the active adapter execution or validator. Its reason
59
- * is propagated to the caller without becoming a parse failure.
58
+ * Signal that stops the active adapter execution, validator, or derived
59
+ * configuration resolver. Its reason is propagated to the caller without
60
+ * becoming a parse failure. Derived configuration resolvers also receive
61
+ * it so that they can cancel their own work.
62
+ * @since 1.4.0 Also stops a pending derived configuration resolver.
60
63
  */
61
64
  readonly signal?: AbortSignal;
62
65
  }
@@ -125,6 +128,25 @@ interface DerivePromptConfigContext {
125
128
  * values and are not reported here.
126
129
  */
127
130
  readonly usedDefault: boolean;
131
+ /**
132
+ * Signal supplied through the prompt's shared options, when present.
133
+ * Pass it to cancellable work such as network requests.
134
+ * @since 1.4.0
135
+ */
136
+ readonly signal?: AbortSignal;
137
+ }
138
+ /**
139
+ * Context passed to a prompt configuration resolver that has no
140
+ * dependencies.
141
+ *
142
+ * @since 1.4.0
143
+ */
144
+ interface DerivePromptConfigNoDepsContext {
145
+ /**
146
+ * Signal supplied through the prompt's shared options, when present.
147
+ * Pass it to cancellable work such as network requests.
148
+ */
149
+ readonly signal?: AbortSignal;
128
150
  }
129
151
  /**
130
152
  * Context passed to a multi-dependency prompt configuration resolver.
@@ -139,6 +161,12 @@ interface DerivePromptConfigsContext<Deps extends readonly AnyDependencySource[]
139
161
  * than a published source value.
140
162
  */
141
163
  readonly usedDefaults: { readonly [K in keyof Deps]: boolean };
164
+ /**
165
+ * Signal supplied through the prompt's shared options, when present.
166
+ * Pass it to cancellable work such as network requests.
167
+ * @since 1.4.0
168
+ */
169
+ readonly signal?: AbortSignal;
142
170
  }
143
171
  /**
144
172
  * Options for a single-dependency {@link derivePromptConfig} call.
@@ -161,6 +189,19 @@ type DerivePromptConfigOptions<TDefault, TOtherwise> = {
161
189
  readonly when: () => boolean | Promise<boolean>;
162
190
  readonly otherwise: TOtherwise;
163
191
  });
192
+ /**
193
+ * Options for a zero-dependency {@link derivePromptConfig} call.
194
+ *
195
+ * @typeParam TOtherwise Value type returned when `when` skips the prompt.
196
+ * @since 1.4.0
197
+ */
198
+ type DerivePromptConfigNoDepsOptions<TOtherwise> = {
199
+ readonly when?: never;
200
+ readonly otherwise?: never;
201
+ } | {
202
+ readonly when: () => boolean | Promise<boolean>;
203
+ readonly otherwise: TOtherwise;
204
+ };
164
205
  /**
165
206
  * Options for a multi-dependency {@link derivePromptConfig} call.
166
207
  *
@@ -206,10 +247,11 @@ interface DerivedPromptConfig<TConfig, TOtherwise = never> {
206
247
  readonly dependencyLabels: readonly string[];
207
248
  /**
208
249
  * Resolves the adapter configuration from dependency values. Receives
209
- * one value and one used-default flag per dependency position.
250
+ * one value and one used-default flag per dependency position, and
251
+ * optionally the abort signal to forward to the resolver.
210
252
  * @internal
211
253
  */
212
- readonly resolve: (values: readonly unknown[], usedDefaults: readonly boolean[]) => TConfig | Promise<TConfig>;
254
+ readonly resolve: (values: readonly unknown[], usedDefaults: readonly boolean[], context?: DerivePromptConfigNoDepsContext) => TConfig | Promise<TConfig>;
213
255
  /**
214
256
  * Lazily evaluated fallbacks, one per dependency position.
215
257
  * @internal
@@ -229,6 +271,24 @@ interface DerivedPromptConfig<TConfig, TOtherwise = never> {
229
271
  * @since 1.3.0
230
272
  */
231
273
  declare function isDerivedPromptConfig(config: unknown): config is DerivedPromptConfig<unknown, unknown>;
274
+ /**
275
+ * Derives a prompt configuration without any dependency source.
276
+ *
277
+ * The resolver may return the configuration synchronously or
278
+ * asynchronously. It runs only during the real completion phase,
279
+ * immediately before the adapter executes, so it can load data such as
280
+ * the choices of a selection prompt without running for command-line
281
+ * values, help, or suggestions.
282
+ *
283
+ * @typeParam TConfig Adapter configuration produced by the resolver.
284
+ * @typeParam TOtherwise Value type returned when `when` skips the prompt.
285
+ * @param resolver Produces the adapter configuration. It receives the
286
+ * prompt's abort signal, when one is supplied.
287
+ * @param options Optional runtime condition.
288
+ * @returns A derived configuration accepted by `prompt()` wrappers.
289
+ * @since 1.4.0
290
+ */
291
+ declare function derivePromptConfig<TConfig, const TOtherwise = never>(resolver: (context: DerivePromptConfigNoDepsContext) => TConfig | Promise<TConfig>, options?: DerivePromptConfigNoDepsOptions<TOtherwise>): DerivedPromptConfig<TConfig, TOtherwise>;
232
292
  /**
233
293
  * Derives a prompt configuration from one dependency source value.
234
294
  *
@@ -299,4 +359,4 @@ type PromptConfigInput<TConfig, TValue> = (TConfig & PromptCondition<TValue>) |
299
359
  */
300
360
  declare function createPromptAdapter<TConfig>(adapter: PromptAdapter<TConfig>): <M extends Mode, TValue, TState>(parser: Parser<M, TValue, TState>, config: PromptConfigInput<TConfig, TValue>, options?: PromptOptions<NoInfer<TValue>>) => FluentParser<"async", TValue, TState>;
301
361
  //#endregion
302
- export { DerivePromptConfigContext, DerivePromptConfigOptions, DerivePromptConfigsContext, DerivePromptConfigsOptions, DerivedPromptConfig, PromptAdapter, PromptCondition, PromptConfigInput, PromptExecutionContext, PromptOptions, PromptValidator, createPromptAdapter, derivePromptConfig, isDerivedPromptConfig };
362
+ export { DerivePromptConfigContext, DerivePromptConfigNoDepsContext, DerivePromptConfigNoDepsOptions, DerivePromptConfigOptions, DerivePromptConfigsContext, DerivePromptConfigsOptions, DerivedPromptConfig, PromptAdapter, PromptCondition, PromptConfigInput, PromptExecutionContext, PromptOptions, PromptValidator, createPromptAdapter, derivePromptConfig, isDerivedPromptConfig };
package/dist/index.js CHANGED
@@ -17,14 +17,22 @@ const derivedPromptConfigMarker = Symbol.for("@optique/prompt/derivedPromptConfi
17
17
  function isDerivedPromptConfig(config) {
18
18
  return config != null && typeof config === "object" && derivedPromptConfigMarker in config && config[derivedPromptConfigMarker] === true;
19
19
  }
20
- function derivePromptConfig(source, resolver, options) {
20
+ function derivePromptConfig(source, resolverOrOptions, options) {
21
+ if (typeof source === "function") return deriveNoDepsPromptConfig(source, resolverOrOptions);
22
+ const resolver = resolverOrOptions;
21
23
  const isTuple = Array.isArray(source);
22
24
  const dependencies = isTuple ? source : [source];
23
25
  if (dependencies.length === 0) throw new TypeError("derivePromptConfig() requires at least one dependency source.");
24
26
  const infos = dependencies.map(getDependencySourceInfo);
25
27
  const singleDefault = options?.defaultValue;
26
28
  const defaultValues = isTuple ? options?.defaultValues : singleDefault == null ? void 0 : () => [singleDefault()];
27
- const resolve = isTuple ? (values$1, usedDefaults) => resolver(values$1, { usedDefaults }) : (values$1, usedDefaults) => resolver(values$1[0], { usedDefault: usedDefaults[0] });
29
+ const resolve = isTuple ? (values$1, usedDefaults, context) => resolver(values$1, {
30
+ usedDefaults,
31
+ ...context
32
+ }) : (values$1, usedDefaults, context) => resolver(values$1[0], {
33
+ usedDefault: usedDefaults[0],
34
+ ...context
35
+ });
28
36
  return {
29
37
  [derivedPromptConfigMarker]: true,
30
38
  dependencies,
@@ -38,6 +46,19 @@ function derivePromptConfig(source, resolver, options) {
38
46
  }
39
47
  };
40
48
  }
49
+ function deriveNoDepsPromptConfig(resolver, options) {
50
+ return {
51
+ [derivedPromptConfigMarker]: true,
52
+ dependencies: [],
53
+ dependencyIds: [],
54
+ dependencyLabels: [],
55
+ resolve: (_values, _usedDefaults, context) => resolver({ ...context }),
56
+ ...options?.when == null ? {} : {
57
+ when: options.when,
58
+ otherwise: options.otherwise
59
+ }
60
+ };
61
+ }
41
62
  function describeThrown(error) {
42
63
  return error instanceof Error ? error.message : String(error);
43
64
  }
@@ -50,7 +71,7 @@ function describeThrown(error) {
50
71
  * (synchronous throws and rejections alike) become prompt failures so
51
72
  * the scheduler treats them like a cancelled prompt.
52
73
  */
53
- async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel) {
74
+ async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel, signal) {
54
75
  const runtime = exec?.dependencyRuntime;
55
76
  const ids = config.dependencyIds;
56
77
  if (runtime != null) {
@@ -101,7 +122,7 @@ async function resolveDerivedPromptConfig(config, exec, ownSourceId, ownLabel) {
101
122
  try {
102
123
  return {
103
124
  ok: true,
104
- config: await config.resolve(values$1, usedDefaults)
125
+ config: await config.resolve(values$1, usedDefaults, signal === void 0 ? {} : { signal })
105
126
  };
106
127
  } catch (error) {
107
128
  return {
@@ -265,8 +286,7 @@ function createPromptAdapter(adapter) {
265
286
  if (!isDerivedPromptConfig(config)) resolvedConfig = config;
266
287
  else {
267
288
  const source = promptedParser.dependencyMetadata?.source;
268
- const resolved = await resolveDerivedPromptConfig(config, exec, source?.sourceId, source?.metavar);
269
- throwIfPromptAborted(signal);
289
+ const resolved = await racePromptWorkWithAbort(signal, () => resolveDerivedPromptConfig(config, exec, source?.sourceId, source?.metavar, signal));
270
290
  if (!resolved.ok) return {
271
291
  success: false,
272
292
  error: resolved.error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/prompt",
3
- "version": "1.4.0-dev.2603",
3
+ "version": "1.4.0-dev.2604",
4
4
  "description": "Generic prompt adapter support for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -60,12 +60,12 @@
60
60
  },
61
61
  "sideEffects": false,
62
62
  "dependencies": {
63
- "@optique/core": "1.4.0-dev.2603+d6d54fca"
63
+ "@optique/core": "1.4.0-dev.2604+e78868f4"
64
64
  },
65
65
  "devDependencies": {
66
- "@optique/config": "1.4.0-dev.2603+d6d54fca",
67
- "@optique/env": "1.4.0-dev.2603+d6d54fca",
68
- "@optique/run": "1.4.0-dev.2603+d6d54fca",
66
+ "@optique/config": "1.4.0-dev.2604+e78868f4",
67
+ "@optique/env": "1.4.0-dev.2604+e78868f4",
68
+ "@optique/run": "1.4.0-dev.2604+e78868f4",
69
69
  "@types/node": "^24.0.0",
70
70
  "fast-check": "^4.7.0",
71
71
  "tsdown": "^0.13.0",