@ftrdotdev/pi-inquiry 0.2.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/LICENSE +21 -0
- package/README.md +161 -0
- package/package.json +68 -0
- package/src/core/domain.test.ts +154 -0
- package/src/core/domain.ts +149 -0
- package/src/core/editor.test.ts +124 -0
- package/src/core/editor.ts +78 -0
- package/src/core/flow.test.ts +445 -0
- package/src/core/flow.ts +98 -0
- package/src/core/keyboard.test.ts +44 -0
- package/src/core/keyboard.ts +59 -0
- package/src/core/machine.test.ts +438 -0
- package/src/core/machine.ts +378 -0
- package/src/core/scene.test.ts +385 -0
- package/src/core/scene.ts +555 -0
- package/src/core/services.ts +81 -0
- package/src/core/text.test.ts +77 -0
- package/src/core/text.ts +207 -0
- package/src/sdk/box-parity.test.ts +76 -0
- package/src/sdk/keys.test.ts +59 -0
- package/src/sdk/keys.ts +77 -0
- package/src/sdk/painter.ts +33 -0
- package/src/sdk/pi-ui.test.ts +185 -0
- package/src/sdk/pi-ui.ts +210 -0
- package/src/sdk/schema.test.ts +67 -0
- package/src/tool/question.test.ts +70 -0
- package/src/tool/question.ts +176 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure interaction state machine for the question flow.
|
|
3
|
+
*
|
|
4
|
+
* Encodes the UX contract of the legacy `question` and `questionnaire` tools:
|
|
5
|
+
* - Single question: simple option list, answering completes immediately.
|
|
6
|
+
* - Multiple questions: tab bar navigation, optional submit tab, answering
|
|
7
|
+
* advances to the next tab.
|
|
8
|
+
* - Returning to an answered tab highlights the current answer; reopening
|
|
9
|
+
* "Type something" pre-fills the editor so the answer can be edited.
|
|
10
|
+
* - "Type something" opens an inline editor; Enter submits the typed answer,
|
|
11
|
+
* Esc returns to the options.
|
|
12
|
+
*
|
|
13
|
+
* `step` is a pure reducer: same state + event -> same next state. The flow
|
|
14
|
+
* program (and its tests) drive it with keyboard events.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Match } from "effect";
|
|
18
|
+
import type { KeyEvent } from "./keyboard.ts";
|
|
19
|
+
import {
|
|
20
|
+
editorBackspace,
|
|
21
|
+
editorDelete,
|
|
22
|
+
editorFromText,
|
|
23
|
+
editorInsert,
|
|
24
|
+
editorMoveEnd,
|
|
25
|
+
editorMoveHome,
|
|
26
|
+
editorMoveLeft,
|
|
27
|
+
editorMoveRight,
|
|
28
|
+
emptyEditor,
|
|
29
|
+
sanitizePaste,
|
|
30
|
+
type EditorState,
|
|
31
|
+
} from "./editor.ts";
|
|
32
|
+
import { Answer, QuestionResult, type Question } from "./domain.ts";
|
|
33
|
+
import type { SuggestionItem } from "./services.ts";
|
|
34
|
+
|
|
35
|
+
/** An option as presented to the user. The last one is the "Type something" entry when `allowOther` is set. */
|
|
36
|
+
export interface OptionChoice {
|
|
37
|
+
readonly label: string;
|
|
38
|
+
readonly description?: string;
|
|
39
|
+
readonly isOther: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The open file-completion popup over the inline editor. */
|
|
43
|
+
export interface SuggestionState {
|
|
44
|
+
/** The `@` token prefix the items replace. */
|
|
45
|
+
readonly prefix: string;
|
|
46
|
+
readonly items: readonly SuggestionItem[];
|
|
47
|
+
/** Index of the highlighted item (wraps at both ends). */
|
|
48
|
+
readonly selectedIndex: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Mode = "single" | "multi";
|
|
52
|
+
|
|
53
|
+
export interface MachineState {
|
|
54
|
+
readonly mode: Mode;
|
|
55
|
+
readonly questions: readonly Question[];
|
|
56
|
+
/** Active tab: 0..questions.length-1 for questions, `questions.length` for the submit tab. */
|
|
57
|
+
readonly currentTab: number;
|
|
58
|
+
readonly optionIndex: number;
|
|
59
|
+
/** Whether the inline "Type something" editor is open. */
|
|
60
|
+
readonly inputMode: boolean;
|
|
61
|
+
readonly answers: ReadonlyMap<string, Answer>;
|
|
62
|
+
readonly editor: EditorState;
|
|
63
|
+
/** Open file-completion popup, or null when closed. */
|
|
64
|
+
readonly suggestions: SuggestionState | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type MachineAction =
|
|
68
|
+
| { readonly type: "none" }
|
|
69
|
+
| { readonly type: "applySuggestion" }
|
|
70
|
+
| { readonly type: "forceSuggestion" }
|
|
71
|
+
| { readonly type: "complete"; readonly result: QuestionResult };
|
|
72
|
+
|
|
73
|
+
export interface StepResult {
|
|
74
|
+
readonly state: MachineState;
|
|
75
|
+
readonly action: MachineAction;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const initialMachineState = (questions: readonly Question[]): MachineState => ({
|
|
79
|
+
mode: questions.length > 1 ? "multi" : "single",
|
|
80
|
+
questions,
|
|
81
|
+
currentTab: 0,
|
|
82
|
+
optionIndex: 0,
|
|
83
|
+
inputMode: false,
|
|
84
|
+
answers: new Map(),
|
|
85
|
+
editor: emptyEditor(),
|
|
86
|
+
suggestions: null,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/** Options of the active question, with the "Type something" entry appended when allowed. */
|
|
90
|
+
export const currentOptions = (state: MachineState): readonly OptionChoice[] =>
|
|
91
|
+
optionsForTab(state.questions, state.currentTab);
|
|
92
|
+
|
|
93
|
+
/** Options of the question on the given tab, with the "Type something" entry appended when allowed. */
|
|
94
|
+
export const optionsForTab = (
|
|
95
|
+
questions: readonly Question[],
|
|
96
|
+
tab: number,
|
|
97
|
+
): readonly OptionChoice[] => {
|
|
98
|
+
const question = questions[tab];
|
|
99
|
+
if (!question) return [];
|
|
100
|
+
const options: OptionChoice[] = question.options.map((o) => ({
|
|
101
|
+
label: o.label,
|
|
102
|
+
description: o.description,
|
|
103
|
+
isOther: false,
|
|
104
|
+
}));
|
|
105
|
+
if (question.allowOther) {
|
|
106
|
+
options.push({ label: "Type something.", isOther: true });
|
|
107
|
+
}
|
|
108
|
+
return options;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const isAllAnswered = (state: MachineState): boolean =>
|
|
112
|
+
state.questions.every((q) => state.answers.has(q.id));
|
|
113
|
+
|
|
114
|
+
export const step = (state: MachineState, event: KeyEvent): StepResult =>
|
|
115
|
+
state.inputMode ? stepInEditor(state, event) : stepInOptions(state, event);
|
|
116
|
+
|
|
117
|
+
/** No-op: the event is intentionally ignored in this mode. */
|
|
118
|
+
const ignore = (state: MachineState): StepResult => ({ state, action: { type: "none" } });
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Text before the cursor, used to detect `@` completion contexts.
|
|
122
|
+
*
|
|
123
|
+
* The pattern mirrors pi-tui's default trigger pattern: `@` at the start of
|
|
124
|
+
* the text or right after whitespace, followed by non-whitespace.
|
|
125
|
+
*/
|
|
126
|
+
export const textBeforeCursor = (editor: EditorState): string => {
|
|
127
|
+
const chars = Array.from(editor.text);
|
|
128
|
+
return chars.slice(0, editor.cursor).join("");
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** Whether the text before the cursor should open the `@` file popup. */
|
|
132
|
+
export const isAtCompletionContext = (before: string): boolean => /(?:^|\s)@[^\s]*$/.test(before);
|
|
133
|
+
|
|
134
|
+
/** Open the popup with the given items, preselecting the best match. */
|
|
135
|
+
export const openSuggestions = (
|
|
136
|
+
state: MachineState,
|
|
137
|
+
prefix: string,
|
|
138
|
+
items: readonly SuggestionItem[],
|
|
139
|
+
): MachineState => ({
|
|
140
|
+
...state,
|
|
141
|
+
suggestions: {
|
|
142
|
+
prefix,
|
|
143
|
+
items,
|
|
144
|
+
selectedIndex: bestSuggestionIndex(items, prefix),
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
/** Apply a new editor state. */
|
|
149
|
+
const edit = (state: MachineState, editor: EditorState): StepResult => ({
|
|
150
|
+
state: { ...state, editor },
|
|
151
|
+
action: { type: "none" },
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Preselect the item that best matches the typed prefix, like pi-tui:
|
|
156
|
+
* exact `value` match wins, else the first item whose `value` starts with the
|
|
157
|
+
* prefix, else the first item.
|
|
158
|
+
*/
|
|
159
|
+
const bestSuggestionIndex = (items: readonly SuggestionItem[], prefix: string): number => {
|
|
160
|
+
for (let i = 0; i < items.length; i++) {
|
|
161
|
+
if (items[i]!.value === prefix) return i;
|
|
162
|
+
}
|
|
163
|
+
for (let i = 0; i < items.length; i++) {
|
|
164
|
+
if (items[i]!.value.startsWith(prefix)) return i;
|
|
165
|
+
}
|
|
166
|
+
return 0;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const stepInOptions = (state: MachineState, event: KeyEvent): StepResult =>
|
|
170
|
+
Match.valueTags(event, {
|
|
171
|
+
// Tab navigation (multi-question mode only)
|
|
172
|
+
tab: () => (state.mode === "multi" ? moveTab(state, 1) : ignore(state)),
|
|
173
|
+
right: () => (state.mode === "multi" ? moveTab(state, 1) : ignore(state)),
|
|
174
|
+
shiftTab: () => (state.mode === "multi" ? moveTab(state, -1) : ignore(state)),
|
|
175
|
+
left: () => (state.mode === "multi" ? moveTab(state, -1) : ignore(state)),
|
|
176
|
+
// Selection moves are inert on the submit tab
|
|
177
|
+
up: () =>
|
|
178
|
+
state.currentTab >= state.questions.length ? ignore(state) : moveSelection(state, -1),
|
|
179
|
+
down: () =>
|
|
180
|
+
state.currentTab >= state.questions.length ? ignore(state) : moveSelection(state, 1),
|
|
181
|
+
enter: () =>
|
|
182
|
+
state.currentTab >= state.questions.length ? maybeSubmit(state) : chooseOption(state),
|
|
183
|
+
escape: () => complete(state, true),
|
|
184
|
+
// Everything else is ignored while navigating options
|
|
185
|
+
char: () => ignore(state),
|
|
186
|
+
paste: () => ignore(state),
|
|
187
|
+
shiftEnter: () => ignore(state),
|
|
188
|
+
backspace: () => ignore(state),
|
|
189
|
+
delete: () => ignore(state),
|
|
190
|
+
home: () => ignore(state),
|
|
191
|
+
end: () => ignore(state),
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const moveTab = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
195
|
+
const totalTabs = state.questions.length + 1;
|
|
196
|
+
const currentTab = (state.currentTab + dir + totalTabs) % totalTabs;
|
|
197
|
+
return { state: enterTab({ ...state, currentTab }), action: { type: "none" } };
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Prepare a tab for display: highlight the option that reflects an existing
|
|
202
|
+
* answer (the chosen option, or "Type something" for a custom answer) and
|
|
203
|
+
* close any open editor.
|
|
204
|
+
*/
|
|
205
|
+
const enterTab = (state: MachineState): MachineState => ({
|
|
206
|
+
...state,
|
|
207
|
+
inputMode: false,
|
|
208
|
+
editor: emptyEditor(),
|
|
209
|
+
suggestions: null,
|
|
210
|
+
optionIndex: answeredOptionIndex(state) ?? 0,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
/** Option index that reflects the existing answer on the current tab, if any. */
|
|
214
|
+
const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
215
|
+
const question = state.questions[state.currentTab];
|
|
216
|
+
if (!question) return undefined;
|
|
217
|
+
const answer = state.answers.get(question.id);
|
|
218
|
+
if (!answer) return undefined;
|
|
219
|
+
const options = optionsForTab(state.questions, state.currentTab);
|
|
220
|
+
if (answer.wasCustom) return options.length - 1; // the "Type something" entry
|
|
221
|
+
if (answer.index === undefined) return undefined;
|
|
222
|
+
return Math.min(Math.max(0, answer.index - 1), options.length - 1);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const moveSelection = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
226
|
+
const options = currentOptions(state);
|
|
227
|
+
const optionIndex = Math.min(Math.max(0, state.optionIndex + dir), options.length - 1);
|
|
228
|
+
return { state: { ...state, optionIndex }, action: { type: "none" } };
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/** Enter on the submit tab: submit only when every question is answered. */
|
|
232
|
+
const maybeSubmit = (state: MachineState): StepResult =>
|
|
233
|
+
isAllAnswered(state) ? complete(state, false) : ignore(state);
|
|
234
|
+
|
|
235
|
+
/** Enter on a question tab: pick the highlighted option, or open the editor. */
|
|
236
|
+
const chooseOption = (state: MachineState): StepResult => {
|
|
237
|
+
const question = state.questions[state.currentTab];
|
|
238
|
+
const option = currentOptions(state)[state.optionIndex];
|
|
239
|
+
if (!question || !option) return ignore(state);
|
|
240
|
+
if (option.isOther) {
|
|
241
|
+
const existing = state.answers.get(question.id);
|
|
242
|
+
return {
|
|
243
|
+
state: {
|
|
244
|
+
...state,
|
|
245
|
+
inputMode: true,
|
|
246
|
+
editor: existing?.wasCustom ? editorFromText(existing.label) : emptyEditor(),
|
|
247
|
+
suggestions: null,
|
|
248
|
+
},
|
|
249
|
+
action: { type: "none" },
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
return advanceAfterAnswer(
|
|
253
|
+
withAnswer(state, question, option.label, false, state.optionIndex + 1),
|
|
254
|
+
);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const stepInEditor = (state: MachineState, event: KeyEvent): StepResult =>
|
|
258
|
+
state.suggestions ? stepInEditorWithSuggestions(state, event) : stepInEditorPlain(state, event);
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Key handling while the completion popup is open. Navigation and selection
|
|
262
|
+
* keys drive the popup; text and cursor keys fall through to normal editor
|
|
263
|
+
* handling (the flow re-queries afterwards).
|
|
264
|
+
*/
|
|
265
|
+
const stepInEditorWithSuggestions = (state: MachineState, event: KeyEvent): StepResult =>
|
|
266
|
+
Match.valueTags(event, {
|
|
267
|
+
up: () => moveSuggestionSelection(state, -1),
|
|
268
|
+
down: () => moveSuggestionSelection(state, 1),
|
|
269
|
+
tab: () => ({ state, action: { type: "applySuggestion" } as const }),
|
|
270
|
+
enter: () => ({ state, action: { type: "applySuggestion" } as const }),
|
|
271
|
+
escape: () => closeSuggestions(state),
|
|
272
|
+
// Everything else is regular editor input; the flow refreshes the popup.
|
|
273
|
+
char: (e) => edit(state, editorInsert(state.editor, e.char)),
|
|
274
|
+
paste: (e) => edit(state, editorInsert(state.editor, sanitizePaste(e.text))),
|
|
275
|
+
shiftEnter: () => edit(state, editorInsert(state.editor, "\n")),
|
|
276
|
+
backspace: () => edit(state, editorBackspace(state.editor)),
|
|
277
|
+
delete: () => edit(state, editorDelete(state.editor)),
|
|
278
|
+
left: () => edit(state, editorMoveLeft(state.editor)),
|
|
279
|
+
right: () => edit(state, editorMoveRight(state.editor)),
|
|
280
|
+
home: () => edit(state, editorMoveHome(state.editor)),
|
|
281
|
+
end: () => edit(state, editorMoveEnd(state.editor)),
|
|
282
|
+
shiftTab: () => ignore(state),
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
/** Regular editor keys: navigation keys are inert while typing. */
|
|
286
|
+
const stepInEditorPlain = (state: MachineState, event: KeyEvent): StepResult =>
|
|
287
|
+
Match.valueTags(event, {
|
|
288
|
+
escape: () => exitEditor(state),
|
|
289
|
+
enter: () => submitEditor(state),
|
|
290
|
+
char: (e) => edit(state, editorInsert(state.editor, e.char)),
|
|
291
|
+
paste: (e) => edit(state, editorInsert(state.editor, sanitizePaste(e.text))),
|
|
292
|
+
shiftEnter: () => edit(state, editorInsert(state.editor, "\n")),
|
|
293
|
+
backspace: () => edit(state, editorBackspace(state.editor)),
|
|
294
|
+
delete: () => edit(state, editorDelete(state.editor)),
|
|
295
|
+
left: () => edit(state, editorMoveLeft(state.editor)),
|
|
296
|
+
right: () => edit(state, editorMoveRight(state.editor)),
|
|
297
|
+
home: () => edit(state, editorMoveHome(state.editor)),
|
|
298
|
+
end: () => edit(state, editorMoveEnd(state.editor)),
|
|
299
|
+
// Navigation keys are inert while typing
|
|
300
|
+
up: () => ignore(state),
|
|
301
|
+
down: () => ignore(state),
|
|
302
|
+
tab: () => ({ state, action: { type: "forceSuggestion" } as const }),
|
|
303
|
+
shiftTab: () => ignore(state),
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
/** Move the popup highlight, wrapping at both ends. */
|
|
307
|
+
const moveSuggestionSelection = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
308
|
+
const suggestions = state.suggestions!;
|
|
309
|
+
const count = suggestions.items.length;
|
|
310
|
+
if (count === 0) return ignore(state);
|
|
311
|
+
const selectedIndex = (((suggestions.selectedIndex + dir) % count) + count) % count;
|
|
312
|
+
return {
|
|
313
|
+
state: { ...state, suggestions: { ...suggestions, selectedIndex } },
|
|
314
|
+
action: { type: "none" },
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/** Close the completion popup, keeping the editor open. */
|
|
319
|
+
const closeSuggestions = (state: MachineState): StepResult => ({
|
|
320
|
+
state: { ...state, suggestions: null },
|
|
321
|
+
action: { type: "none" },
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
/** Esc in the editor: leave without answering. */
|
|
325
|
+
const exitEditor = (state: MachineState): StepResult => ({
|
|
326
|
+
state: { ...state, inputMode: false, editor: emptyEditor(), suggestions: null },
|
|
327
|
+
action: { type: "none" },
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
/** Enter in the editor: submit the typed answer. */
|
|
331
|
+
const submitEditor = (state: MachineState): StepResult => {
|
|
332
|
+
const question = state.questions[state.currentTab];
|
|
333
|
+
if (!question) return ignore(state);
|
|
334
|
+
const label = state.editor.text.trim() || "(no response)";
|
|
335
|
+
const next = withAnswer(state, question, label, true);
|
|
336
|
+
return advanceAfterAnswer({
|
|
337
|
+
...next,
|
|
338
|
+
inputMode: false,
|
|
339
|
+
editor: emptyEditor(),
|
|
340
|
+
suggestions: null,
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const withAnswer = (
|
|
345
|
+
state: MachineState,
|
|
346
|
+
question: Question,
|
|
347
|
+
label: string,
|
|
348
|
+
wasCustom: boolean,
|
|
349
|
+
index?: number,
|
|
350
|
+
): MachineState => {
|
|
351
|
+
const answers = new Map(state.answers);
|
|
352
|
+
answers.set(
|
|
353
|
+
question.id,
|
|
354
|
+
index === undefined
|
|
355
|
+
? new Answer({ id: question.id, label, wasCustom })
|
|
356
|
+
: new Answer({ id: question.id, label, wasCustom, index }),
|
|
357
|
+
);
|
|
358
|
+
return { ...state, answers };
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const advanceAfterAnswer = (state: MachineState): StepResult => {
|
|
362
|
+
if (state.mode === "single") return complete(state, false);
|
|
363
|
+
const currentTab =
|
|
364
|
+
state.currentTab < state.questions.length - 1 ? state.currentTab + 1 : state.questions.length;
|
|
365
|
+
return { state: enterTab({ ...state, currentTab }), action: { type: "none" } };
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const complete = (state: MachineState, cancelled: boolean): StepResult => ({
|
|
369
|
+
state,
|
|
370
|
+
action: {
|
|
371
|
+
type: "complete",
|
|
372
|
+
result: new QuestionResult({
|
|
373
|
+
questions: [...state.questions],
|
|
374
|
+
answers: [...state.answers.values()],
|
|
375
|
+
cancelled,
|
|
376
|
+
}),
|
|
377
|
+
},
|
|
378
|
+
});
|