@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,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure inline text editor state.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the pi-tui `Editor` widget in the core. `cursor` is an index into
|
|
5
|
+
* the code points of `text` (not UTF-16 code units), so backspace and cursor
|
|
6
|
+
* movement operate on full characters, including surrogate pairs.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface EditorState {
|
|
10
|
+
readonly text: string;
|
|
11
|
+
/** Cursor position as a code-point index into `text` (0..= code point count). */
|
|
12
|
+
readonly cursor: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const emptyEditor = (): EditorState => ({ text: "", cursor: 0 });
|
|
16
|
+
|
|
17
|
+
/** Editor state with `text` and the cursor at the end (used to pre-fill an existing answer). */
|
|
18
|
+
export const editorFromText = (text: string): EditorState => ({
|
|
19
|
+
text,
|
|
20
|
+
cursor: Array.from(text).length,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** Insert `input` at the cursor. */
|
|
24
|
+
export const editorInsert = (state: EditorState, input: string): EditorState => {
|
|
25
|
+
if (input === "") return state;
|
|
26
|
+
const chars = Array.from(state.text);
|
|
27
|
+
const at = Math.min(state.cursor, chars.length);
|
|
28
|
+
const inserted = Array.from(input);
|
|
29
|
+
const next = [...chars.slice(0, at), ...inserted, ...chars.slice(at)];
|
|
30
|
+
return { text: next.join(""), cursor: at + inserted.length };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Sanitize pasted text for insertion.
|
|
35
|
+
*
|
|
36
|
+
* Normalizes line endings, expands tabs (mirroring the pi editor), and drops
|
|
37
|
+
* remaining control characters while keeping newlines.
|
|
38
|
+
*/
|
|
39
|
+
export const sanitizePaste = (input: string): string => {
|
|
40
|
+
const normalized = input.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " ");
|
|
41
|
+
return Array.from(normalized)
|
|
42
|
+
.filter((ch) => ch === "\n" || (ch.codePointAt(0) ?? 0) >= 0x20)
|
|
43
|
+
.join("");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Delete the character before the cursor. */
|
|
47
|
+
export const editorBackspace = (state: EditorState): EditorState => {
|
|
48
|
+
if (state.cursor <= 0) return state;
|
|
49
|
+
const chars = Array.from(state.text);
|
|
50
|
+
const at = Math.min(state.cursor, chars.length);
|
|
51
|
+
const next = [...chars.slice(0, at - 1), ...chars.slice(at)];
|
|
52
|
+
return { text: next.join(""), cursor: at - 1 };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Delete the character at the cursor. */
|
|
56
|
+
export const editorDelete = (state: EditorState): EditorState => {
|
|
57
|
+
const chars = Array.from(state.text);
|
|
58
|
+
if (state.cursor >= chars.length) return state;
|
|
59
|
+
const next = [...chars.slice(0, state.cursor), ...chars.slice(state.cursor + 1)];
|
|
60
|
+
return { text: next.join(""), cursor: state.cursor };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const editorMoveLeft = (state: EditorState): EditorState => ({
|
|
64
|
+
...state,
|
|
65
|
+
cursor: Math.max(0, state.cursor - 1),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const editorMoveRight = (state: EditorState): EditorState => ({
|
|
69
|
+
...state,
|
|
70
|
+
cursor: Math.min(Array.from(state.text).length, state.cursor + 1),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const editorMoveHome = (state: EditorState): EditorState => ({ ...state, cursor: 0 });
|
|
74
|
+
|
|
75
|
+
export const editorMoveEnd = (state: EditorState): EditorState => ({
|
|
76
|
+
...state,
|
|
77
|
+
cursor: Array.from(state.text).length,
|
|
78
|
+
});
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { describe, expect, it } from "@effect/vitest";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Option, Question, type Question as QuestionType, type QuestionResult } from "./domain.ts";
|
|
4
|
+
import { FileSuggestions, Keyboard, Renderer, type SuggestionItem } from "./services.ts";
|
|
5
|
+
import { runQuestionnaire } from "./flow.ts";
|
|
6
|
+
import { KeyEvent } from "./keyboard.ts";
|
|
7
|
+
import type { MachineState } from "./machine.ts";
|
|
8
|
+
|
|
9
|
+
const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
10
|
+
new Question({
|
|
11
|
+
id: "q1",
|
|
12
|
+
label: "Q1",
|
|
13
|
+
prompt: "Pick one?",
|
|
14
|
+
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
15
|
+
allowOther: true,
|
|
16
|
+
...over,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/** Scripted keyboard: replays the given keys, then cancels with escape if exhausted. */
|
|
20
|
+
const scriptedKeyboard = (keys: KeyEvent[]): Keyboard["Service"] => {
|
|
21
|
+
const remaining = [...keys];
|
|
22
|
+
return {
|
|
23
|
+
nextKey: () => Effect.sync(() => remaining.shift() ?? KeyEvent.escape),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
interface QueryRecord {
|
|
28
|
+
readonly text: string;
|
|
29
|
+
readonly cursor: number;
|
|
30
|
+
readonly force: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Scripted file completions. `handler` decides each query's result; when it
|
|
35
|
+
* returns null the popup closes. `apply` mirrors the pi-tui provider's `@`
|
|
36
|
+
* completion: replace the prefix, add a space after files (not directories).
|
|
37
|
+
*/
|
|
38
|
+
const scriptedFileSuggestions = (
|
|
39
|
+
handler: (
|
|
40
|
+
text: string,
|
|
41
|
+
cursor: number,
|
|
42
|
+
force: boolean,
|
|
43
|
+
) => { prefix: string; items: SuggestionItem[] } | null,
|
|
44
|
+
): { service: FileSuggestions["Service"]; queries: QueryRecord[] } => {
|
|
45
|
+
const queries: QueryRecord[] = [];
|
|
46
|
+
const service: FileSuggestions["Service"] = {
|
|
47
|
+
query: (text, cursor, force) =>
|
|
48
|
+
Effect.sync(() => {
|
|
49
|
+
queries.push({ text, cursor, force: force ?? false });
|
|
50
|
+
return handler(text, cursor, force ?? false);
|
|
51
|
+
}),
|
|
52
|
+
apply: (text, cursor, item, prefix) =>
|
|
53
|
+
Effect.sync(() => {
|
|
54
|
+
const chars = Array.from(text);
|
|
55
|
+
const at = Math.min(cursor, chars.length);
|
|
56
|
+
const before = chars.slice(0, at - prefix.length).join("");
|
|
57
|
+
const after = chars.slice(at).join("");
|
|
58
|
+
const suffix = item.label.endsWith("/") ? "" : " ";
|
|
59
|
+
const next = before + item.value + suffix + after;
|
|
60
|
+
return { text: next, cursor: Array.from(before + item.value + suffix).length };
|
|
61
|
+
}),
|
|
62
|
+
};
|
|
63
|
+
return { service, queries };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** No completions: every query closes the popup. */
|
|
67
|
+
const noFileSuggestions = (): { service: FileSuggestions["Service"]; queries: QueryRecord[] } =>
|
|
68
|
+
scriptedFileSuggestions(() => null);
|
|
69
|
+
|
|
70
|
+
interface Harness {
|
|
71
|
+
readonly states: MachineState[];
|
|
72
|
+
readonly completed: QuestionResult | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const run = (
|
|
76
|
+
questions: readonly Question[],
|
|
77
|
+
keys: KeyEvent[],
|
|
78
|
+
suggestions: {
|
|
79
|
+
service: FileSuggestions["Service"];
|
|
80
|
+
queries: QueryRecord[];
|
|
81
|
+
} = noFileSuggestions(),
|
|
82
|
+
): Effect.Effect<Harness> =>
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const states: MachineState[] = [];
|
|
85
|
+
let completed: QuestionResult | undefined;
|
|
86
|
+
const renderer: Renderer["Service"] = {
|
|
87
|
+
present: (state) =>
|
|
88
|
+
Effect.sync(() => {
|
|
89
|
+
states.push(state);
|
|
90
|
+
}),
|
|
91
|
+
complete: (result) =>
|
|
92
|
+
Effect.sync(() => {
|
|
93
|
+
completed = result;
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
yield* runQuestionnaire(questions).pipe(
|
|
97
|
+
Effect.provideService(Keyboard, scriptedKeyboard(keys)),
|
|
98
|
+
Effect.provideService(Renderer, renderer),
|
|
99
|
+
Effect.provideService(FileSuggestions, suggestions.service),
|
|
100
|
+
);
|
|
101
|
+
return { states, completed };
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/** Keys that open the editor for the "Type something" option. */
|
|
105
|
+
const openEditor: KeyEvent[] = [KeyEvent.down, KeyEvent.down, KeyEvent.enter];
|
|
106
|
+
|
|
107
|
+
const fileItems: SuggestionItem[] = [
|
|
108
|
+
{ value: "src/main.ts", label: "main.ts", description: "src/main.ts" },
|
|
109
|
+
{ value: "src/utils/", label: "utils/", description: "src/utils/" },
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
describe("runQuestionnaire", () => {
|
|
113
|
+
it.effect("presents the initial state before reading keys", () =>
|
|
114
|
+
Effect.gen(function* () {
|
|
115
|
+
const harness = yield* run([question()], [KeyEvent.enter]);
|
|
116
|
+
expect(harness.states.length).toBeGreaterThanOrEqual(1);
|
|
117
|
+
expect(harness.states[0]?.mode).toBe("single");
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
it.effect("completes with the selected option", () =>
|
|
122
|
+
Effect.gen(function* () {
|
|
123
|
+
const harness = yield* run([question()], [KeyEvent.down, KeyEvent.enter]);
|
|
124
|
+
const result = harness.completed;
|
|
125
|
+
expect(result?.cancelled).toBe(false);
|
|
126
|
+
expect(result?.answers).toHaveLength(1);
|
|
127
|
+
expect(result?.answers[0]?.label).toBe("No");
|
|
128
|
+
expect(result?.answers[0]?.index).toBe(2);
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
it.effect("cancels on escape", () =>
|
|
133
|
+
Effect.gen(function* () {
|
|
134
|
+
const harness = yield* run([question()], [KeyEvent.escape]);
|
|
135
|
+
expect(harness.completed?.cancelled).toBe(true);
|
|
136
|
+
expect(harness.completed?.answers).toHaveLength(0);
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
it.effect("collects a custom answer from the editor", () =>
|
|
141
|
+
Effect.gen(function* () {
|
|
142
|
+
const harness = yield* run(
|
|
143
|
+
[question()],
|
|
144
|
+
[...openEditor, KeyEvent.char("c"), KeyEvent.char("u"), KeyEvent.char("s"), KeyEvent.enter],
|
|
145
|
+
);
|
|
146
|
+
expect(harness.completed?.answers[0]?.label).toBe("cus");
|
|
147
|
+
expect(harness.completed?.answers[0]?.wasCustom).toBe(true);
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
it.effect("handles multi-question flows with a submit tab", () =>
|
|
152
|
+
Effect.gen(function* () {
|
|
153
|
+
const questions = [question(), question({ id: "q2", label: "Q2", prompt: "Second?" })];
|
|
154
|
+
const harness = yield* run(questions, [
|
|
155
|
+
KeyEvent.enter, // q1 -> q2
|
|
156
|
+
KeyEvent.enter, // q2 -> submit tab
|
|
157
|
+
KeyEvent.enter, // confirm
|
|
158
|
+
]);
|
|
159
|
+
expect(harness.completed?.cancelled).toBe(false);
|
|
160
|
+
expect(harness.completed?.answers.map((a) => a.id)).toEqual(["q1", "q2"]);
|
|
161
|
+
}),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
it.effect("presents a scene after every step", () =>
|
|
165
|
+
Effect.gen(function* () {
|
|
166
|
+
const harness = yield* run([question()], [KeyEvent.down, KeyEvent.down, KeyEvent.enter]);
|
|
167
|
+
// initial, down, down, editor open, then the scripted fallback (escape) closes the editor
|
|
168
|
+
expect(harness.states.map((s) => s.optionIndex)).toEqual([0, 1, 2, 2, 2]);
|
|
169
|
+
expect(harness.states[3]?.inputMode).toBe(true);
|
|
170
|
+
expect(harness.completed?.cancelled).toBe(true);
|
|
171
|
+
}),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
it.effect("lets the user edit a custom answer after returning to its tab", () =>
|
|
175
|
+
Effect.gen(function* () {
|
|
176
|
+
const questions = [question(), question({ id: "q2", label: "Q2", prompt: "Second?" })];
|
|
177
|
+
const harness = yield* run(questions, [
|
|
178
|
+
KeyEvent.down,
|
|
179
|
+
KeyEvent.down,
|
|
180
|
+
KeyEvent.enter, // open editor on q1
|
|
181
|
+
KeyEvent.char("h"),
|
|
182
|
+
KeyEvent.char("i"),
|
|
183
|
+
KeyEvent.enter, // submit "hi", advance to q2
|
|
184
|
+
KeyEvent.shiftTab, // back to q1: answer shown and highlighted
|
|
185
|
+
KeyEvent.enter, // reopen the pre-filled editor
|
|
186
|
+
KeyEvent.char("!"),
|
|
187
|
+
KeyEvent.enter, // re-submit "hi!", advance to q2
|
|
188
|
+
KeyEvent.enter, // answer q2 "Yes", advance to submit
|
|
189
|
+
KeyEvent.enter, // confirm
|
|
190
|
+
]);
|
|
191
|
+
expect(harness.completed?.answers.find((a) => a.id === "q1")?.label).toBe("hi!");
|
|
192
|
+
expect(harness.completed?.answers.find((a) => a.id === "q1")?.wasCustom).toBe(true);
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
it.effect("lets the user change an option answer after returning to its tab", () =>
|
|
197
|
+
Effect.gen(function* () {
|
|
198
|
+
const questions = [question(), question({ id: "q2", label: "Q2", prompt: "Second?" })];
|
|
199
|
+
const harness = yield* run(questions, [
|
|
200
|
+
KeyEvent.down,
|
|
201
|
+
KeyEvent.enter, // answer q1 "No", advance to q2
|
|
202
|
+
KeyEvent.shiftTab, // back to q1: "No" highlighted
|
|
203
|
+
KeyEvent.up, // "Yes"
|
|
204
|
+
KeyEvent.enter, // change to "Yes", advance to q2
|
|
205
|
+
KeyEvent.enter, // answer q2 "Yes", advance to submit
|
|
206
|
+
KeyEvent.enter, // confirm
|
|
207
|
+
]);
|
|
208
|
+
expect(harness.completed?.answers.find((a) => a.id === "q1")?.label).toBe("Yes");
|
|
209
|
+
expect(harness.completed?.answers.find((a) => a.id === "q1")?.index).toBe(1);
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
it.effect("opens the popup when typing an @ context, with the best match preselected", () =>
|
|
214
|
+
Effect.gen(function* () {
|
|
215
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
216
|
+
text === "@src" ? { prefix: "@src", items: fileItems } : null,
|
|
217
|
+
);
|
|
218
|
+
const harness = yield* run(
|
|
219
|
+
[question()],
|
|
220
|
+
[
|
|
221
|
+
...openEditor,
|
|
222
|
+
KeyEvent.char("@"),
|
|
223
|
+
KeyEvent.char("s"),
|
|
224
|
+
KeyEvent.char("r"),
|
|
225
|
+
KeyEvent.char("c"),
|
|
226
|
+
],
|
|
227
|
+
suggestions,
|
|
228
|
+
);
|
|
229
|
+
const last = [...harness.states].reverse().find((s) => s.inputMode && s.suggestions !== null);
|
|
230
|
+
expect(last?.suggestions).toEqual({
|
|
231
|
+
prefix: "@src",
|
|
232
|
+
items: fileItems,
|
|
233
|
+
selectedIndex: 0, // no exact or prefix match on value -> first item
|
|
234
|
+
});
|
|
235
|
+
// Queries follow every edit in the @ context
|
|
236
|
+
expect(suggestions.queries.map((q) => q.text)).toEqual(["@", "@s", "@sr", "@src"]);
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
it.effect("preselects the exact-match item", () =>
|
|
241
|
+
Effect.gen(function* () {
|
|
242
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
243
|
+
text.startsWith("@")
|
|
244
|
+
? {
|
|
245
|
+
prefix: "@src/main",
|
|
246
|
+
items: [
|
|
247
|
+
{ value: "@src/main.ts", label: "main.ts" },
|
|
248
|
+
{ value: "@src/main", label: "main", description: "src/main" },
|
|
249
|
+
],
|
|
250
|
+
}
|
|
251
|
+
: null,
|
|
252
|
+
);
|
|
253
|
+
const harness = yield* run(
|
|
254
|
+
[question()],
|
|
255
|
+
[
|
|
256
|
+
...openEditor,
|
|
257
|
+
KeyEvent.char("@"),
|
|
258
|
+
KeyEvent.char("s"),
|
|
259
|
+
KeyEvent.char("r"),
|
|
260
|
+
KeyEvent.char("c"),
|
|
261
|
+
KeyEvent.char("/"),
|
|
262
|
+
KeyEvent.char("m"),
|
|
263
|
+
KeyEvent.char("a"),
|
|
264
|
+
KeyEvent.char("i"),
|
|
265
|
+
KeyEvent.char("n"),
|
|
266
|
+
],
|
|
267
|
+
suggestions,
|
|
268
|
+
);
|
|
269
|
+
const last = [...harness.states].reverse().find((s) => s.inputMode && s.suggestions !== null);
|
|
270
|
+
expect(last?.suggestions?.selectedIndex).toBe(1); // value "@src/main" === prefix
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
it.effect("does not query outside an @ context", () =>
|
|
275
|
+
Effect.gen(function* () {
|
|
276
|
+
const suggestions = noFileSuggestions();
|
|
277
|
+
yield* run(
|
|
278
|
+
[question()],
|
|
279
|
+
[...openEditor, KeyEvent.char("h"), KeyEvent.char("i")],
|
|
280
|
+
suggestions,
|
|
281
|
+
);
|
|
282
|
+
expect(suggestions.queries).toHaveLength(0);
|
|
283
|
+
}),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
it.effect("closes the popup when an edit breaks the @ context", () =>
|
|
287
|
+
Effect.gen(function* () {
|
|
288
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
289
|
+
text.endsWith("@") ? { prefix: "@", items: fileItems } : null,
|
|
290
|
+
);
|
|
291
|
+
const harness = yield* run(
|
|
292
|
+
[question()],
|
|
293
|
+
[...openEditor, KeyEvent.char("@"), KeyEvent.char(" ")],
|
|
294
|
+
suggestions,
|
|
295
|
+
);
|
|
296
|
+
const states = harness.states;
|
|
297
|
+
const popupOpen = [...states].reverse().find((s) => s.suggestions !== null);
|
|
298
|
+
expect(popupOpen?.editor.text).toBe("@");
|
|
299
|
+
const lastEditor = [...states].reverse().find((s) => s.inputMode);
|
|
300
|
+
expect(lastEditor?.suggestions).toBeNull();
|
|
301
|
+
}),
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
it.effect("navigates the popup with up and down, wrapping", () =>
|
|
305
|
+
Effect.gen(function* () {
|
|
306
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
307
|
+
text.startsWith("@") ? { prefix: "@", items: fileItems } : null,
|
|
308
|
+
);
|
|
309
|
+
const harness = yield* run(
|
|
310
|
+
[question()],
|
|
311
|
+
[...openEditor, KeyEvent.char("@"), KeyEvent.down, KeyEvent.down, KeyEvent.up],
|
|
312
|
+
suggestions,
|
|
313
|
+
);
|
|
314
|
+
const last = [...harness.states].reverse().find((s) => s.inputMode && s.suggestions !== null);
|
|
315
|
+
expect(last?.suggestions?.selectedIndex).toBe(1); // 0 -> 1 -> 0 (wrap) -> 1
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
it.effect("escape closes the popup without touching the text", () =>
|
|
320
|
+
Effect.gen(function* () {
|
|
321
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
322
|
+
text.startsWith("@") ? { prefix: "@", items: fileItems } : null,
|
|
323
|
+
);
|
|
324
|
+
const harness = yield* run(
|
|
325
|
+
[question()],
|
|
326
|
+
[...openEditor, KeyEvent.char("@"), KeyEvent.escape, KeyEvent.char("x")],
|
|
327
|
+
suggestions,
|
|
328
|
+
);
|
|
329
|
+
const states = harness.states;
|
|
330
|
+
const afterEscape = [...states]
|
|
331
|
+
.reverse()
|
|
332
|
+
.find((s) => s.inputMode && s.suggestions === null && s.editor.text === "@");
|
|
333
|
+
expect(afterEscape).toBeDefined();
|
|
334
|
+
// Typing after Esc reopens the popup in an @ context, like the pi editor
|
|
335
|
+
const reopened = [...states]
|
|
336
|
+
.reverse()
|
|
337
|
+
.find((s) => s.inputMode && s.editor.text === "@x" && s.suggestions !== null);
|
|
338
|
+
expect(reopened).toBeDefined();
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
it.effect("tab applies the selected completion and closes the popup", () =>
|
|
343
|
+
Effect.gen(function* () {
|
|
344
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
345
|
+
text.startsWith("@") ? { prefix: "@", items: fileItems } : null,
|
|
346
|
+
);
|
|
347
|
+
const harness = yield* run(
|
|
348
|
+
[question()],
|
|
349
|
+
[...openEditor, KeyEvent.char("@"), KeyEvent.down, KeyEvent.tab, KeyEvent.enter],
|
|
350
|
+
suggestions,
|
|
351
|
+
);
|
|
352
|
+
expect(harness.completed?.answers[0]?.label).toBe("src/utils/"); // second item applied, then Enter submits
|
|
353
|
+
expect(harness.completed?.answers[0]?.wasCustom).toBe(true);
|
|
354
|
+
}),
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
it.effect("enter applies the completion instead of submitting while the popup is open", () =>
|
|
358
|
+
Effect.gen(function* () {
|
|
359
|
+
const suggestions = scriptedFileSuggestions((text) =>
|
|
360
|
+
text.startsWith("@") ? { prefix: "@", items: fileItems } : null,
|
|
361
|
+
);
|
|
362
|
+
const harness = yield* run(
|
|
363
|
+
[question()],
|
|
364
|
+
[...openEditor, KeyEvent.char("@"), KeyEvent.enter, KeyEvent.enter],
|
|
365
|
+
suggestions,
|
|
366
|
+
);
|
|
367
|
+
const answer = harness.completed?.answers[0];
|
|
368
|
+
expect(answer?.label).toBe("src/main.ts"); // best match applied on first Enter
|
|
369
|
+
expect(answer?.wasCustom).toBe(true);
|
|
370
|
+
}),
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
it.effect("re-queries after applying a directory so completion can continue", () =>
|
|
374
|
+
Effect.gen(function* () {
|
|
375
|
+
const suggestions = scriptedFileSuggestions((text, _cursor, force) => {
|
|
376
|
+
if (text === "@") return { prefix: "@", items: fileItems };
|
|
377
|
+
// After applying the directory, continuation happens on the plain
|
|
378
|
+
// path token via a forced Tab query.
|
|
379
|
+
if (text === "src/utils/" && force) {
|
|
380
|
+
return {
|
|
381
|
+
prefix: "src/utils/",
|
|
382
|
+
items: [
|
|
383
|
+
{
|
|
384
|
+
value: "src/utils/helpers.ts",
|
|
385
|
+
label: "helpers.ts",
|
|
386
|
+
description: "src/utils/helpers.ts",
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return null;
|
|
392
|
+
});
|
|
393
|
+
const harness = yield* run(
|
|
394
|
+
[question()],
|
|
395
|
+
[
|
|
396
|
+
...openEditor,
|
|
397
|
+
KeyEvent.char("@"),
|
|
398
|
+
KeyEvent.down, // select utils/ (a directory)
|
|
399
|
+
KeyEvent.tab, // applies "src/utils/" without a trailing space
|
|
400
|
+
KeyEvent.tab, // forced query continues completion inside the directory
|
|
401
|
+
KeyEvent.enter, // applies helpers.ts
|
|
402
|
+
KeyEvent.enter, // submits
|
|
403
|
+
],
|
|
404
|
+
suggestions,
|
|
405
|
+
);
|
|
406
|
+
const answer = harness.completed?.answers[0];
|
|
407
|
+
expect(answer?.label).toBe("src/utils/helpers.ts");
|
|
408
|
+
expect(answer?.wasCustom).toBe(true);
|
|
409
|
+
expect(suggestions.queries.map((q) => q.force)).toEqual([false, true]);
|
|
410
|
+
}),
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
it.effect("tab with no popup forces file completion outside an @ context", () =>
|
|
414
|
+
Effect.gen(function* () {
|
|
415
|
+
const suggestions = scriptedFileSuggestions((text, _cursor, force) =>
|
|
416
|
+
text === "src/ut" && force
|
|
417
|
+
? {
|
|
418
|
+
prefix: "src/ut",
|
|
419
|
+
items: [{ value: "src/utils/", label: "utils/", description: "src/utils/" }],
|
|
420
|
+
}
|
|
421
|
+
: null,
|
|
422
|
+
);
|
|
423
|
+
const harness = yield* run(
|
|
424
|
+
[question()],
|
|
425
|
+
[
|
|
426
|
+
...openEditor,
|
|
427
|
+
KeyEvent.char("s"),
|
|
428
|
+
KeyEvent.char("r"),
|
|
429
|
+
KeyEvent.char("c"),
|
|
430
|
+
KeyEvent.char("/"),
|
|
431
|
+
KeyEvent.char("u"),
|
|
432
|
+
KeyEvent.char("t"),
|
|
433
|
+
KeyEvent.tab,
|
|
434
|
+
KeyEvent.enter, // applies the best match
|
|
435
|
+
KeyEvent.enter, // submits
|
|
436
|
+
],
|
|
437
|
+
suggestions,
|
|
438
|
+
);
|
|
439
|
+
expect(harness.completed?.answers[0]?.label).toBe("src/utils/");
|
|
440
|
+
// Plain typing never queried; only the forced Tab did
|
|
441
|
+
expect(suggestions.queries.map((q) => q.text)).toEqual(["src/ut"]);
|
|
442
|
+
expect(suggestions.queries[0]?.force).toBe(true);
|
|
443
|
+
}),
|
|
444
|
+
);
|
|
445
|
+
});
|
package/src/core/flow.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The question flow: an Effect program that drives the interaction machine
|
|
3
|
+
* from keyboard events to a final result.
|
|
4
|
+
*
|
|
5
|
+
* Pure core — no pi imports. The `Keyboard` and `Renderer` services abstract
|
|
6
|
+
* the terminal; the fringe implements them with `ctx.ui.custom`. The
|
|
7
|
+
* `FileSuggestions` service provides `@` file completion for the inline
|
|
8
|
+
* editor; the flow re-queries it after every editor change and applies the
|
|
9
|
+
* chosen item when the machine asks to.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Effect } from "effect";
|
|
13
|
+
import type { Question, QuestionResult } from "./domain.ts";
|
|
14
|
+
import {
|
|
15
|
+
initialMachineState,
|
|
16
|
+
isAtCompletionContext,
|
|
17
|
+
openSuggestions,
|
|
18
|
+
step,
|
|
19
|
+
textBeforeCursor,
|
|
20
|
+
type MachineState,
|
|
21
|
+
} from "./machine.ts";
|
|
22
|
+
import { FileSuggestions, Keyboard, Renderer } from "./services.ts";
|
|
23
|
+
|
|
24
|
+
export const runQuestionnaire = Effect.fn("inquiry.core.runQuestionnaire")(function* (
|
|
25
|
+
questions: readonly Question[],
|
|
26
|
+
): Effect.fn.Return<QuestionResult, never, Keyboard | Renderer | FileSuggestions> {
|
|
27
|
+
const keyboard = yield* Keyboard;
|
|
28
|
+
const renderer = yield* Renderer;
|
|
29
|
+
const fileSuggestions = yield* FileSuggestions;
|
|
30
|
+
|
|
31
|
+
let state = initialMachineState(questions);
|
|
32
|
+
yield* renderer.present(state);
|
|
33
|
+
|
|
34
|
+
while (true) {
|
|
35
|
+
const key = yield* keyboard.nextKey();
|
|
36
|
+
const previousEditor = state.editor;
|
|
37
|
+
const stepped = step(state, key);
|
|
38
|
+
state = stepped.state;
|
|
39
|
+
|
|
40
|
+
if (stepped.action.type === "complete") {
|
|
41
|
+
const result = stepped.action.result;
|
|
42
|
+
yield* renderer.complete(result);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (stepped.action.type === "applySuggestion") {
|
|
47
|
+
const suggestion = state.suggestions;
|
|
48
|
+
const item =
|
|
49
|
+
suggestion?.items[Math.min(suggestion.selectedIndex, suggestion.items.length - 1)];
|
|
50
|
+
if (suggestion && item) {
|
|
51
|
+
const applied = yield* fileSuggestions.apply(
|
|
52
|
+
state.editor.text,
|
|
53
|
+
state.editor.cursor,
|
|
54
|
+
item,
|
|
55
|
+
suggestion.prefix,
|
|
56
|
+
);
|
|
57
|
+
state = { ...state, editor: applied, suggestions: null };
|
|
58
|
+
}
|
|
59
|
+
} else if (stepped.action.type === "forceSuggestion") {
|
|
60
|
+
// Tab with no open popup: force file completion (plain paths, directory
|
|
61
|
+
// continuation), like the pi editor.
|
|
62
|
+
state = yield* refreshSuggestions(state, fileSuggestions, true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Keep the popup in sync with the editor: open it in an `@` context,
|
|
66
|
+
// refresh it on every edit while open, and close it when the provider
|
|
67
|
+
// reports no completion (e.g. the context was broken by a space).
|
|
68
|
+
if (state.inputMode && state.editor !== previousEditor) {
|
|
69
|
+
state = yield* refreshSuggestions(state, fileSuggestions, false);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
yield* renderer.present(state);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Refresh the completion popup after an editor change (or a forced Tab).
|
|
78
|
+
*
|
|
79
|
+
* A closed popup only opens when the text before the cursor is an `@`
|
|
80
|
+
* completion context; an open popup always re-queries so it refreshes with
|
|
81
|
+
* the new prefix — or closes when the provider returns null. Forced queries
|
|
82
|
+
* (Tab) skip the `@` gate so plain path tokens complete too. This mirrors
|
|
83
|
+
* the pi editor's trigger/update behavior.
|
|
84
|
+
*/
|
|
85
|
+
const refreshSuggestions = Effect.fn("inquiry.core.refreshSuggestions")(function* (
|
|
86
|
+
state: MachineState,
|
|
87
|
+
fileSuggestions: FileSuggestions["Service"],
|
|
88
|
+
force: boolean,
|
|
89
|
+
): Effect.fn.Return<MachineState> {
|
|
90
|
+
if (!force && !state.suggestions && !isAtCompletionContext(textBeforeCursor(state.editor))) {
|
|
91
|
+
return state;
|
|
92
|
+
}
|
|
93
|
+
const result = yield* fileSuggestions.query(state.editor.text, state.editor.cursor, force);
|
|
94
|
+
if (result === null) {
|
|
95
|
+
return { ...state, suggestions: null };
|
|
96
|
+
}
|
|
97
|
+
return openSuggestions(state, result.prefix, result.items);
|
|
98
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
import { KeyEvent, KeyEventSchema } from "./keyboard.ts";
|
|
4
|
+
|
|
5
|
+
describe("KeyEvent", () => {
|
|
6
|
+
it("exports a canonical singleton for every tag-only variant", () => {
|
|
7
|
+
const events = [
|
|
8
|
+
KeyEvent.up,
|
|
9
|
+
KeyEvent.down,
|
|
10
|
+
KeyEvent.left,
|
|
11
|
+
KeyEvent.right,
|
|
12
|
+
KeyEvent.tab,
|
|
13
|
+
KeyEvent.shiftTab,
|
|
14
|
+
KeyEvent.enter,
|
|
15
|
+
KeyEvent.shiftEnter,
|
|
16
|
+
KeyEvent.escape,
|
|
17
|
+
KeyEvent.backspace,
|
|
18
|
+
KeyEvent.delete,
|
|
19
|
+
KeyEvent.home,
|
|
20
|
+
KeyEvent.end,
|
|
21
|
+
];
|
|
22
|
+
expect(events).toHaveLength(13);
|
|
23
|
+
expect(new Set(events).size).toBe(13); // distinct instances
|
|
24
|
+
// each singleton survives a decode round-trip
|
|
25
|
+
for (const event of events) {
|
|
26
|
+
expect(Schema.decodeUnknownSync(KeyEventSchema)(event)).toEqual(event);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("builds data-carrying variants through the char and paste factories", () => {
|
|
31
|
+
const typed = KeyEvent.char("a");
|
|
32
|
+
const pasted = KeyEvent.paste("hello\nworld");
|
|
33
|
+
expect(typed).toEqual({ _tag: "char", char: "a" });
|
|
34
|
+
expect(pasted).toEqual({ _tag: "paste", text: "hello\nworld" });
|
|
35
|
+
expect(Schema.decodeUnknownSync(KeyEventSchema)(typed)).toEqual(typed);
|
|
36
|
+
expect(Schema.decodeUnknownSync(KeyEventSchema)(pasted)).toEqual(pasted);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("rejects unknown tags and missing payloads", () => {
|
|
40
|
+
expect(() => Schema.decodeUnknownSync(KeyEventSchema)({ _tag: "nope" })).toThrow();
|
|
41
|
+
expect(() => Schema.decodeUnknownSync(KeyEventSchema)({ _tag: "char" })).toThrow();
|
|
42
|
+
expect(() => Schema.decodeUnknownSync(KeyEventSchema)({ _tag: "paste" })).toThrow();
|
|
43
|
+
});
|
|
44
|
+
});
|