@optique/prompt 1.2.0-dev.0 → 1.2.0-dev.2194

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 CHANGED
@@ -33,12 +33,16 @@ Quick start
33
33
 
34
34
  ~~~~ typescript
35
35
  import { option } from "@optique/core/primitives";
36
+ import { message } from "@optique/core/message";
36
37
  import { string } from "@optique/core/valueparser";
37
38
  import { createPromptAdapter } from "@optique/prompt";
38
39
 
39
40
  const prompt = createPromptAdapter<{ readonly message: string }>({
40
41
  async execute<TValue>(config) {
41
- const value = globalThis.prompt?.(config.message) ?? "";
42
+ const value = globalThis.prompt?.(config.message);
43
+ if (value == null) {
44
+ return { success: false, error: message`Prompt cancelled.` };
45
+ }
42
46
  return { success: true, value: value as TValue };
43
47
  },
44
48
  });
package/dist/index.cjs CHANGED
@@ -48,12 +48,13 @@ function deferredPromptResult(placeholderValue) {
48
48
  deferredKeys: keys
49
49
  };
50
50
  }
51
- function withAnnotatedInnerState(sourceState, innerState, run) {
51
+ function withAnnotatedInnerState(sourceState, innerState, run, inheritPrimitiveAnnotations = false) {
52
52
  const annotations = (0, __optique_core_annotations.getAnnotations)(sourceState);
53
- if (annotations == null || innerState == null || typeof innerState !== "object" || (0, __optique_core_annotations.getAnnotations)(innerState) != null) return run(innerState);
53
+ const innerStateIsObject = innerState != null && typeof innerState === "object";
54
+ if (annotations == null || (0, __optique_core_annotations.getAnnotations)(innerState) != null || !innerStateIsObject && !inheritPrimitiveAnnotations) return run(innerState);
54
55
  const inheritedState = (0, __optique_core_extension.inheritAnnotations)(sourceState, innerState);
55
56
  if (inheritedState !== innerState) return run(inheritedState);
56
- return run((0, __optique_core_extension.withAnnotationView)(innerState, annotations));
57
+ return innerStateIsObject ? run((0, __optique_core_extension.withAnnotationView)(innerState, annotations)) : run(innerState);
57
58
  }
58
59
  function hasSourceBindingMarker(state) {
59
60
  return state != null && typeof state === "object" && "hasCliValue" in state && Object.getOwnPropertySymbols(state).length > 0;
@@ -103,6 +104,7 @@ function createPromptAdapter(adapter) {
103
104
  function executePrompt() {
104
105
  return adapter.execute(config);
105
106
  }
107
+ const parserInheritsAnnotations = (0, __optique_core_extension.getTraits)(parser).inheritsAnnotations === true;
106
108
  const promptedParser = {
107
109
  mode: "async",
108
110
  $valueType: parser.$valueType,
@@ -132,7 +134,7 @@ function createPromptAdapter(adapter) {
132
134
  ...context,
133
135
  state: innerState
134
136
  } : context;
135
- const effectiveInnerState = annotations != null && innerState == null && (0, __optique_core_extension.getTraits)(parser).inheritsAnnotations === true ? (0, __optique_core_extension.injectAnnotations)(innerState, annotations) : innerState;
137
+ const effectiveInnerState = annotations != null && innerState == null && parserInheritsAnnotations ? (0, __optique_core_extension.injectAnnotations)(innerState, annotations) : innerState;
136
138
  const processResult = (result$1) => {
137
139
  if (result$1.success) {
138
140
  const cliState = annotations != null && result$1.next.state != null && typeof result$1.next.state === "object" && (0, __optique_core_annotations.getAnnotations)(result$1.next.state) !== annotations ? (0, __optique_core_extension.injectAnnotations)(result$1.next.state, annotations) : result$1.next.state;
@@ -172,20 +174,20 @@ function createPromptAdapter(adapter) {
172
174
  state: annotatedInnerState
173
175
  } : context;
174
176
  return parser.parse(innerContext);
175
- });
177
+ }, parserInheritsAnnotations);
176
178
  if (result instanceof Promise) return result.then(processResult);
177
179
  return Promise.resolve(processResult(result));
178
180
  },
179
181
  complete: (state, exec) => {
180
182
  if (isPromptBindState(state) && state.hasCliValue) {
181
- const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec));
183
+ const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec), parserInheritsAnnotations);
182
184
  if (r instanceof Promise) return r;
183
185
  return Promise.resolve(r);
184
186
  }
185
187
  const isProbe = exec != null && exec.phase !== "complete";
186
188
  const annotations = (0, __optique_core_annotations.getAnnotations)(state);
187
189
  const innerInitialState = parser.initialState;
188
- const shouldInheritInitialStateAnnotations = annotations != null && (innerInitialState == null || typeof innerInitialState === "object");
190
+ const shouldInheritInitialStateAnnotations = annotations != null && (innerInitialState == null || typeof innerInitialState === "object" || parserInheritsAnnotations);
189
191
  const effectiveInitialState = shouldInheritInitialStateAnnotations ? (0, __optique_core_extension.inheritAnnotations)(state, innerInitialState) : innerInitialState;
190
192
  const readPlaceholder = () => {
191
193
  try {
@@ -195,7 +197,7 @@ function createPromptAdapter(adapter) {
195
197
  }
196
198
  };
197
199
  const finalizePrompt = () => {
198
- const shouldDefer = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => shouldDeferPrompt(parser, annotatedInnerState, exec));
200
+ const shouldDefer = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => shouldDeferPrompt(parser, annotatedInnerState, exec), parserInheritsAnnotations);
199
201
  if (shouldDefer) return Promise.resolve(deferredPromptResult(readPlaceholder()));
200
202
  if (isProbe) return Promise.resolve({
201
203
  success: true,
@@ -221,7 +223,7 @@ function createPromptAdapter(adapter) {
221
223
  return handleCompleteResult(innerR);
222
224
  };
223
225
  if (hasDeferHook) {
224
- const innerR = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec));
226
+ const innerR = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec), parserInheritsAnnotations);
225
227
  const handleDeferHookResult = (res) => {
226
228
  if (res.success && res.value === void 0) return finalizePrompt();
227
229
  if (!res.success) return finalizePrompt();
@@ -235,7 +237,7 @@ function createPromptAdapter(adapter) {
235
237
  state: annotatedState,
236
238
  optionsTerminated: false,
237
239
  usage: parser.usage
238
- }));
240
+ }), parserInheritsAnnotations);
239
241
  if (simParseR instanceof Promise) return simParseR.then(decideFromParse);
240
242
  return decideFromParse(simParseR);
241
243
  },
@@ -255,7 +257,10 @@ function createPromptAdapter(adapter) {
255
257
  return parser.getDocFragments(state, defaultValue);
256
258
  }
257
259
  };
258
- (0, __optique_core_extension.defineTraits)(promptedParser, { inheritsAnnotations: true });
260
+ (0, __optique_core_extension.defineTraits)(promptedParser, {
261
+ inheritsAnnotations: true,
262
+ ...(0, __optique_core_extension.getTraits)(parser).completesFromSource === true ? { completesFromSource: true } : {}
263
+ });
259
264
  if ("placeholder" in parser) Object.defineProperty(promptedParser, "placeholder", {
260
265
  get() {
261
266
  try {
package/dist/index.js CHANGED
@@ -25,12 +25,13 @@ function deferredPromptResult(placeholderValue) {
25
25
  deferredKeys: keys
26
26
  };
27
27
  }
28
- function withAnnotatedInnerState(sourceState, innerState, run) {
28
+ function withAnnotatedInnerState(sourceState, innerState, run, inheritPrimitiveAnnotations = false) {
29
29
  const annotations = getAnnotations(sourceState);
30
- if (annotations == null || innerState == null || typeof innerState !== "object" || getAnnotations(innerState) != null) return run(innerState);
30
+ const innerStateIsObject = innerState != null && typeof innerState === "object";
31
+ if (annotations == null || getAnnotations(innerState) != null || !innerStateIsObject && !inheritPrimitiveAnnotations) return run(innerState);
31
32
  const inheritedState = inheritAnnotations(sourceState, innerState);
32
33
  if (inheritedState !== innerState) return run(inheritedState);
33
- return run(withAnnotationView(innerState, annotations));
34
+ return innerStateIsObject ? run(withAnnotationView(innerState, annotations)) : run(innerState);
34
35
  }
35
36
  function hasSourceBindingMarker(state) {
36
37
  return state != null && typeof state === "object" && "hasCliValue" in state && Object.getOwnPropertySymbols(state).length > 0;
@@ -80,6 +81,7 @@ function createPromptAdapter(adapter) {
80
81
  function executePrompt() {
81
82
  return adapter.execute(config);
82
83
  }
84
+ const parserInheritsAnnotations = getTraits(parser).inheritsAnnotations === true;
83
85
  const promptedParser = {
84
86
  mode: "async",
85
87
  $valueType: parser.$valueType,
@@ -109,7 +111,7 @@ function createPromptAdapter(adapter) {
109
111
  ...context,
110
112
  state: innerState
111
113
  } : context;
112
- const effectiveInnerState = annotations != null && innerState == null && getTraits(parser).inheritsAnnotations === true ? injectAnnotations(innerState, annotations) : innerState;
114
+ const effectiveInnerState = annotations != null && innerState == null && parserInheritsAnnotations ? injectAnnotations(innerState, annotations) : innerState;
113
115
  const processResult = (result$1) => {
114
116
  if (result$1.success) {
115
117
  const cliState = annotations != null && result$1.next.state != null && typeof result$1.next.state === "object" && getAnnotations(result$1.next.state) !== annotations ? injectAnnotations(result$1.next.state, annotations) : result$1.next.state;
@@ -149,20 +151,20 @@ function createPromptAdapter(adapter) {
149
151
  state: annotatedInnerState
150
152
  } : context;
151
153
  return parser.parse(innerContext);
152
- });
154
+ }, parserInheritsAnnotations);
153
155
  if (result instanceof Promise) return result.then(processResult);
154
156
  return Promise.resolve(processResult(result));
155
157
  },
156
158
  complete: (state, exec) => {
157
159
  if (isPromptBindState(state) && state.hasCliValue) {
158
- const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec));
160
+ const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec), parserInheritsAnnotations);
159
161
  if (r instanceof Promise) return r;
160
162
  return Promise.resolve(r);
161
163
  }
162
164
  const isProbe = exec != null && exec.phase !== "complete";
163
165
  const annotations = getAnnotations(state);
164
166
  const innerInitialState = parser.initialState;
165
- const shouldInheritInitialStateAnnotations = annotations != null && (innerInitialState == null || typeof innerInitialState === "object");
167
+ const shouldInheritInitialStateAnnotations = annotations != null && (innerInitialState == null || typeof innerInitialState === "object" || parserInheritsAnnotations);
166
168
  const effectiveInitialState = shouldInheritInitialStateAnnotations ? inheritAnnotations(state, innerInitialState) : innerInitialState;
167
169
  const readPlaceholder = () => {
168
170
  try {
@@ -172,7 +174,7 @@ function createPromptAdapter(adapter) {
172
174
  }
173
175
  };
174
176
  const finalizePrompt = () => {
175
- const shouldDefer = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => shouldDeferPrompt(parser, annotatedInnerState, exec));
177
+ const shouldDefer = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => shouldDeferPrompt(parser, annotatedInnerState, exec), parserInheritsAnnotations);
176
178
  if (shouldDefer) return Promise.resolve(deferredPromptResult(readPlaceholder()));
177
179
  if (isProbe) return Promise.resolve({
178
180
  success: true,
@@ -198,7 +200,7 @@ function createPromptAdapter(adapter) {
198
200
  return handleCompleteResult(innerR);
199
201
  };
200
202
  if (hasDeferHook) {
201
- const innerR = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec));
203
+ const innerR = withAnnotatedInnerState(state, effectiveInitialState, (annotatedInnerState) => parser.complete(annotatedInnerState, exec), parserInheritsAnnotations);
202
204
  const handleDeferHookResult = (res) => {
203
205
  if (res.success && res.value === void 0) return finalizePrompt();
204
206
  if (!res.success) return finalizePrompt();
@@ -212,7 +214,7 @@ function createPromptAdapter(adapter) {
212
214
  state: annotatedState,
213
215
  optionsTerminated: false,
214
216
  usage: parser.usage
215
- }));
217
+ }), parserInheritsAnnotations);
216
218
  if (simParseR instanceof Promise) return simParseR.then(decideFromParse);
217
219
  return decideFromParse(simParseR);
218
220
  },
@@ -232,7 +234,10 @@ function createPromptAdapter(adapter) {
232
234
  return parser.getDocFragments(state, defaultValue);
233
235
  }
234
236
  };
235
- defineTraits(promptedParser, { inheritsAnnotations: true });
237
+ defineTraits(promptedParser, {
238
+ inheritsAnnotations: true,
239
+ ...getTraits(parser).completesFromSource === true ? { completesFromSource: true } : {}
240
+ });
236
241
  if ("placeholder" in parser) Object.defineProperty(promptedParser, "placeholder", {
237
242
  get() {
238
243
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/prompt",
3
- "version": "1.2.0-dev.0",
3
+ "version": "1.2.0-dev.2194",
4
4
  "description": "Generic prompt adapter support for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -60,16 +60,16 @@
60
60
  },
61
61
  "sideEffects": false,
62
62
  "dependencies": {
63
- "@optique/core": "1.2.0"
63
+ "@optique/core": "1.2.0-dev.2194+96000f1d"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@types/node": "^24.0.0",
67
67
  "fast-check": "^4.7.0",
68
68
  "tsdown": "^0.13.0",
69
69
  "typescript": "^5.8.3",
70
- "@optique/config": "1.2.0",
71
- "@optique/run": "1.2.0",
72
- "@optique/env": "1.2.0"
70
+ "@optique/config": "1.2.0-dev.2194+96000f1d",
71
+ "@optique/env": "1.2.0-dev.2194+96000f1d",
72
+ "@optique/run": "1.2.0-dev.2194+96000f1d"
73
73
  },
74
74
  "scripts": {
75
75
  "build": "tsdown",