@geoqiao/pi-ask 1.1.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 (59) hide show
  1. package/CHANGELOG.md +194 -0
  2. package/LICENSE +22 -0
  3. package/README.md +282 -0
  4. package/docs/README.md +33 -0
  5. package/docs/configuration.md +406 -0
  6. package/docs/contract.md +309 -0
  7. package/docs/remote-events.md +187 -0
  8. package/package.json +130 -0
  9. package/skills/ask-user/SKILL.md +110 -0
  10. package/src/answer-commands.ts +361 -0
  11. package/src/answer-extraction.ts +354 -0
  12. package/src/ask-payload-store.ts +86 -0
  13. package/src/ask-settings-command.ts +14 -0
  14. package/src/ask-tool-helpers.ts +172 -0
  15. package/src/ask-tool.ts +84 -0
  16. package/src/config/defaults.ts +216 -0
  17. package/src/config/migrate.ts +70 -0
  18. package/src/config/migrations/index.ts +139 -0
  19. package/src/config/migrations/types.ts +10 -0
  20. package/src/config/schema.ts +287 -0
  21. package/src/config/store.ts +227 -0
  22. package/src/constants/keymaps.ts +721 -0
  23. package/src/constants/text.ts +12 -0
  24. package/src/constants/ui.ts +22 -0
  25. package/src/index.ts +30 -0
  26. package/src/math.ts +3 -0
  27. package/src/notifications.ts +119 -0
  28. package/src/remote-ask.ts +563 -0
  29. package/src/result-format.ts +157 -0
  30. package/src/result.ts +23 -0
  31. package/src/schema.ts +74 -0
  32. package/src/state/answers.ts +251 -0
  33. package/src/state/create.ts +18 -0
  34. package/src/state/editor.ts +70 -0
  35. package/src/state/navigation.ts +86 -0
  36. package/src/state/normalize.ts +326 -0
  37. package/src/state/question-type.ts +128 -0
  38. package/src/state/result.ts +263 -0
  39. package/src/state/selectors.ts +135 -0
  40. package/src/state/transitions.ts +330 -0
  41. package/src/state/view.ts +28 -0
  42. package/src/text.ts +98 -0
  43. package/src/types.ts +169 -0
  44. package/src/ui/auto-submit.ts +36 -0
  45. package/src/ui/autocomplete.ts +52 -0
  46. package/src/ui/controller.ts +645 -0
  47. package/src/ui/dismiss-guard.ts +26 -0
  48. package/src/ui/input.ts +160 -0
  49. package/src/ui/render-frame.ts +235 -0
  50. package/src/ui/render-helpers.ts +385 -0
  51. package/src/ui/render-question.ts +288 -0
  52. package/src/ui/render-submit.ts +168 -0
  53. package/src/ui/render-types.ts +33 -0
  54. package/src/ui/render.ts +53 -0
  55. package/src/ui/review-shortcuts.ts +43 -0
  56. package/src/ui/settings-list.ts +461 -0
  57. package/src/ui/show-settings.ts +37 -0
  58. package/src/ui/view-models/question.ts +203 -0
  59. package/src/ui/view-models/review.ts +100 -0
package/src/result.ts ADDED
@@ -0,0 +1,23 @@
1
+ import {
2
+ CANCELLED_RESULT_TEXT,
3
+ ELABORATED_RESULT_TEXT,
4
+ SUBMITTED_RESULT_TEXT,
5
+ } from "./constants/text.ts";
6
+ import { formatElaborationLines, formatResultLines } from "./result-format.ts";
7
+ import type { AskResult } from "./types.ts";
8
+
9
+ export function renderResultText(result: AskResult): string {
10
+ if (result.error) {
11
+ return "Invalid input";
12
+ }
13
+ if (result.cancelled) {
14
+ return CANCELLED_RESULT_TEXT;
15
+ }
16
+ if (result.mode === "elaborate") {
17
+ const lines = formatElaborationLines(result, { mode: "render" });
18
+ return lines.join("\n") || ELABORATED_RESULT_TEXT;
19
+ }
20
+
21
+ const lines = formatResultLines(result, { mode: "render" });
22
+ return lines.join("\n") || SUBMITTED_RESULT_TEXT;
23
+ }
package/src/schema.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { Type } from "typebox";
2
+
3
+ export const AskOptionSchema = Type.Object({
4
+ value: Type.Optional(
5
+ Type.String({
6
+ description:
7
+ "Required machine-readable value returned for this option in the result",
8
+ })
9
+ ),
10
+ label: Type.Optional(
11
+ Type.String({
12
+ description: "Required short visible option label shown in the list",
13
+ })
14
+ ),
15
+ description: Type.Optional(
16
+ Type.String({
17
+ description: "Optional one-line explanation to help the user choose",
18
+ })
19
+ ),
20
+ preview: Type.Optional(
21
+ Type.String({
22
+ description:
23
+ "Optional preview content shown in the dedicated preview pane for preview questions",
24
+ })
25
+ ),
26
+ });
27
+
28
+ export const AskQuestionSchema = Type.Object({
29
+ id: Type.Optional(
30
+ Type.String({
31
+ description:
32
+ "Required stable question identifier used as the key in returned answers",
33
+ })
34
+ ),
35
+ label: Type.Optional(
36
+ Type.String({
37
+ description: "Short tab label, e.g. Goal, Audience, Tone, Scope",
38
+ })
39
+ ),
40
+ prompt: Type.Optional(
41
+ Type.String({
42
+ description:
43
+ "Required direct question shown to the user; ask about one decision at a time",
44
+ })
45
+ ),
46
+ type: Type.Optional(
47
+ Type.String({
48
+ description:
49
+ "Question type: `single` means one answer is expected, `multi` means multiple answers could reasonably be selected, and `preview` means options need preview-pane detail. Use `preview` only when every option includes `preview` text; descriptions alone are not enough.",
50
+ })
51
+ ),
52
+ required: Type.Optional(
53
+ Type.Boolean({
54
+ description:
55
+ "Advisory only; marks the question as important but never blocks submission",
56
+ })
57
+ ),
58
+ options: Type.Array(AskOptionSchema, {
59
+ description:
60
+ "Answer options; provide clear, distinct choices and do not add filler options",
61
+ }),
62
+ });
63
+
64
+ export const AskParamsSchema = Type.Object({
65
+ title: Type.Optional(
66
+ Type.String({
67
+ description:
68
+ "Optional short title shown above the clarification flow, e.g. README direction",
69
+ })
70
+ ),
71
+ questions: Type.Array(AskQuestionSchema, {
72
+ description: "Questions to ask in the interactive clarification flow",
73
+ }),
74
+ });
@@ -0,0 +1,251 @@
1
+ import type {
2
+ AskDisplayOption,
3
+ AskResultAnswer,
4
+ AskSelectedOption,
5
+ AskStateAnswer,
6
+ } from "../types.ts";
7
+
8
+ export interface ExtraOptionNote {
9
+ label: string;
10
+ note: string;
11
+ }
12
+
13
+ export function emptyAnswer(): AskStateAnswer {
14
+ return { selected: [] };
15
+ }
16
+
17
+ export function cloneAnswer(answer: AskStateAnswer): AskStateAnswer {
18
+ return {
19
+ selected: answer.selected.map(cloneSelection),
20
+ customSelected: answer.customSelected,
21
+ customText: answer.customText,
22
+ note: answer.note,
23
+ optionNotes: answer.optionNotes ? { ...answer.optionNotes } : undefined,
24
+ };
25
+ }
26
+
27
+ export function cloneResultAnswer(answer: AskResultAnswer): AskResultAnswer {
28
+ return {
29
+ values: [...answer.values],
30
+ labels: [...answer.labels],
31
+ indices: [...answer.indices],
32
+ customText: answer.customText,
33
+ note: answer.note,
34
+ optionNotes: answer.optionNotes ? { ...answer.optionNotes } : undefined,
35
+ };
36
+ }
37
+
38
+ export function toggleSelection(
39
+ answer: AskStateAnswer,
40
+ option: AskDisplayOption,
41
+ index: number
42
+ ): AskStateAnswer {
43
+ const next = cloneAnswer(answer);
44
+ const selectedIndex = next.selected.findIndex(
45
+ (selection) => selection.value === option.value
46
+ );
47
+
48
+ if (selectedIndex >= 0) {
49
+ next.selected.splice(selectedIndex, 1);
50
+ return next;
51
+ }
52
+
53
+ next.selected.push({
54
+ value: option.value,
55
+ label: option.label,
56
+ index: index + 1,
57
+ });
58
+ return next;
59
+ }
60
+
61
+ export function setSingleSelection(
62
+ answer: AskStateAnswer,
63
+ option: AskDisplayOption,
64
+ index: number
65
+ ): AskStateAnswer {
66
+ return {
67
+ ...emptyAnswer(),
68
+ note: answer.note,
69
+ optionNotes: answer.optionNotes ? { ...answer.optionNotes } : undefined,
70
+ selected: [
71
+ {
72
+ value: option.value,
73
+ label: option.label,
74
+ index: index + 1,
75
+ },
76
+ ],
77
+ };
78
+ }
79
+
80
+ export function saveCustomText(
81
+ answer: AskStateAnswer,
82
+ rawValue: string,
83
+ mode: "single" | "multi"
84
+ ): AskStateAnswer {
85
+ const trimmed = rawValue.trim();
86
+ const next = cloneAnswer(answer);
87
+ if (mode === "single") {
88
+ next.selected = [];
89
+ }
90
+ if (!trimmed) {
91
+ next.customSelected = undefined;
92
+ next.customText = undefined;
93
+ return next;
94
+ }
95
+ next.customSelected = true;
96
+ next.customText = rawValue;
97
+ return next;
98
+ }
99
+
100
+ export function setCustomSelected(
101
+ answer: AskStateAnswer,
102
+ selected: boolean
103
+ ): AskStateAnswer {
104
+ const next = cloneAnswer(answer);
105
+ next.customSelected = selected || undefined;
106
+ return next;
107
+ }
108
+
109
+ export function saveQuestionNote(
110
+ answer: AskStateAnswer,
111
+ rawValue: string
112
+ ): AskStateAnswer {
113
+ const next = cloneAnswer(answer);
114
+ if (rawValue.trim()) {
115
+ next.note = rawValue;
116
+ return next;
117
+ }
118
+ next.note = undefined;
119
+ return next;
120
+ }
121
+
122
+ export function saveOptionNote(
123
+ answer: AskStateAnswer,
124
+ optionValue: string,
125
+ rawValue: string
126
+ ): AskStateAnswer {
127
+ const next = cloneAnswer(answer);
128
+ const optionNotes = { ...(next.optionNotes ?? {}) };
129
+ if (rawValue.trim()) {
130
+ optionNotes[optionValue] = rawValue;
131
+ } else {
132
+ delete optionNotes[optionValue];
133
+ }
134
+ next.optionNotes =
135
+ Object.keys(optionNotes).length > 0 ? optionNotes : undefined;
136
+ return next;
137
+ }
138
+
139
+ export function isAnswerEmpty(answer: AskStateAnswer): boolean {
140
+ return (
141
+ answer.selected.length === 0 &&
142
+ !answer.customText &&
143
+ !answer.note &&
144
+ (!answer.optionNotes || Object.keys(answer.optionNotes).length === 0)
145
+ );
146
+ }
147
+
148
+ export function isAnswerAnswered(answer?: AskStateAnswer): boolean {
149
+ if (!answer) {
150
+ return false;
151
+ }
152
+ return (
153
+ answer.selected.length > 0 ||
154
+ !!(answer.customSelected && answer.customText?.trim())
155
+ );
156
+ }
157
+
158
+ export function hasAnswerNotes(answer?: AskStateAnswer): boolean {
159
+ return !!(answer?.note || answer?.optionNotes);
160
+ }
161
+
162
+ export function isResultAnswerEmpty(answer: AskResultAnswer): boolean {
163
+ return (
164
+ answer.values.length === 0 &&
165
+ answer.labels.length === 0 &&
166
+ answer.indices.length === 0 &&
167
+ !answer.customText &&
168
+ !answer.note &&
169
+ (!answer.optionNotes || Object.keys(answer.optionNotes).length === 0)
170
+ );
171
+ }
172
+
173
+ export function isResultAnswerCommitted(answer: AskResultAnswer): boolean {
174
+ return (
175
+ answer.values.length > 0 ||
176
+ answer.labels.length > 0 ||
177
+ answer.indices.length > 0 ||
178
+ !!answer.customText
179
+ );
180
+ }
181
+
182
+ export function isCustomOnlyAnswer(answer: AskResultAnswer): boolean {
183
+ return answer.indices.length === 0 && !!answer.customText;
184
+ }
185
+
186
+ export function isOptionSelected(
187
+ answer: AskStateAnswer | undefined,
188
+ optionValue: string
189
+ ): boolean {
190
+ return !!answer?.selected.some(
191
+ (selection) => selection.value === optionValue
192
+ );
193
+ }
194
+
195
+ export function serializeAnswer(answer: AskStateAnswer): AskResultAnswer {
196
+ const selectedCustomText = answer.customSelected
197
+ ? answer.customText
198
+ : undefined;
199
+ const values = [
200
+ ...answer.selected.map((selection) => selection.value),
201
+ ...(selectedCustomText ? [selectedCustomText] : []),
202
+ ];
203
+ const labels = [
204
+ ...answer.selected.map((selection) => selection.label),
205
+ ...(selectedCustomText ? [selectedCustomText] : []),
206
+ ];
207
+ const indices = answer.selected.map((selection) => selection.index);
208
+ const selectedNotes = answer.optionNotes
209
+ ? Object.fromEntries(
210
+ answer.selected
211
+ .map((selection) => [
212
+ selection.value,
213
+ answer.optionNotes?.[selection.value],
214
+ ])
215
+ .filter((entry): entry is [string, string] => !!entry[1])
216
+ )
217
+ : undefined;
218
+
219
+ return {
220
+ values,
221
+ labels,
222
+ indices,
223
+ customText: selectedCustomText,
224
+ note: answer.note,
225
+ optionNotes:
226
+ selectedNotes && Object.keys(selectedNotes).length > 0
227
+ ? selectedNotes
228
+ : undefined,
229
+ };
230
+ }
231
+
232
+ export function getExtraOptionNotes(args: {
233
+ answer: AskStateAnswer;
234
+ questionOptions: Array<{ value: string; label: string }>;
235
+ selectedValues?: Iterable<string>;
236
+ }): ExtraOptionNote[] {
237
+ const selectedValues = new Set(args.selectedValues ?? []);
238
+ return Object.entries(args.answer.optionNotes ?? {})
239
+ .filter(([value, note]) => !selectedValues.has(value) && Boolean(note))
240
+ .map(([value, note]) => {
241
+ const option = args.questionOptions.find(
242
+ (candidate) => candidate.value === value
243
+ );
244
+ return option ? { label: option.label, note } : undefined;
245
+ })
246
+ .filter((entry): entry is ExtraOptionNote => Boolean(entry));
247
+ }
248
+
249
+ function cloneSelection(selection: AskSelectedOption): AskSelectedOption {
250
+ return { ...selection };
251
+ }
@@ -0,0 +1,18 @@
1
+ import type { AskParams } from "../types.ts";
2
+ import { normalizeQuestions } from "./normalize.ts";
3
+ import { createInitialState as createBaseState } from "./transitions.ts";
4
+
5
+ interface CreateInitialStateOptions {
6
+ allowFreeform?: boolean;
7
+ presentSingleAsMulti?: boolean;
8
+ }
9
+
10
+ export function createInitialState(
11
+ params: AskParams,
12
+ options: CreateInitialStateOptions = {}
13
+ ) {
14
+ return createBaseState({
15
+ title: params.title,
16
+ questions: normalizeQuestions(params, options),
17
+ });
18
+ }
@@ -0,0 +1,70 @@
1
+ import type { AskState } from "../types.ts";
2
+ import {
3
+ getAnswer,
4
+ getCurrentOption,
5
+ getCurrentQuestion,
6
+ getOptionNote,
7
+ getQuestionNote,
8
+ isSubmitTab,
9
+ } from "./selectors.ts";
10
+ import {
11
+ enterInputMode,
12
+ saveCustomAnswer,
13
+ saveNote,
14
+ submitCustomAnswer,
15
+ } from "./transitions.ts";
16
+ import { isEditingView } from "./view.ts";
17
+
18
+ export function getEditorDraft(state: AskState): string {
19
+ if (state.view.kind === "input") {
20
+ return getAnswer(state, state.view.questionId)?.customText ?? "";
21
+ }
22
+ if (state.view.kind === "note") {
23
+ if (state.view.optionValue) {
24
+ return (
25
+ getOptionNote(state, state.view.questionId, state.view.optionValue) ??
26
+ ""
27
+ );
28
+ }
29
+ return getQuestionNote(state, state.view.questionId) ?? "";
30
+ }
31
+ return "";
32
+ }
33
+
34
+ export function saveEditorDraft(state: AskState, text: string): AskState {
35
+ if (state.view.kind === "input") {
36
+ return saveCustomAnswer(state, text);
37
+ }
38
+ if (state.view.kind === "note") {
39
+ return saveNote(state, text);
40
+ }
41
+ return state;
42
+ }
43
+
44
+ export function submitEditorDraft(state: AskState, text: string): AskState {
45
+ if (state.view.kind === "input") {
46
+ return submitCustomAnswer(state, text);
47
+ }
48
+ if (state.view.kind === "note") {
49
+ return saveNote(state, text);
50
+ }
51
+ return state;
52
+ }
53
+
54
+ export function syncStateToSelection(state: AskState): AskState {
55
+ if (isEditingView(state) || isSubmitTab(state)) {
56
+ return state;
57
+ }
58
+
59
+ const question = getCurrentQuestion(state);
60
+ const option = getCurrentOption(state);
61
+ if (!(question && option?.isCustomOption)) {
62
+ return state;
63
+ }
64
+
65
+ if (getAnswer(state, question.id)?.customText?.trim()) {
66
+ return state;
67
+ }
68
+
69
+ return enterInputMode(state, question.id);
70
+ }
@@ -0,0 +1,86 @@
1
+ import { clamp } from "../math.ts";
2
+ import type { AskState } from "../types.ts";
3
+ import {
4
+ getCurrentQuestion,
5
+ getRenderableOptions,
6
+ isSubmitTab,
7
+ } from "./selectors.ts";
8
+ import { isEditingView, navigateView, submitView } from "./view.ts";
9
+
10
+ const SUBMIT_ACTION_COUNT = 3;
11
+
12
+ export function createInitialState(params: {
13
+ title?: string;
14
+ questions: AskState["questions"];
15
+ }): AskState {
16
+ return {
17
+ title: params.title,
18
+ questions: params.questions,
19
+ activeTabIndex: 0,
20
+ activeOptionIndex: 0,
21
+ activeSubmitActionIndex: 0,
22
+ view: navigateView(),
23
+ answers: {},
24
+ completed: false,
25
+ cancelled: false,
26
+ mode: "submit",
27
+ };
28
+ }
29
+
30
+ export function moveTab(state: AskState, delta: number): AskState {
31
+ const normalizedDelta = delta < 0 ? -1 : 1;
32
+ const totalTabs = state.questions.length + 1;
33
+ const activeTabIndex =
34
+ (state.activeTabIndex + normalizedDelta + totalTabs) % totalTabs;
35
+ return {
36
+ ...state,
37
+ activeTabIndex,
38
+ activeOptionIndex: 0,
39
+ activeSubmitActionIndex: 0,
40
+ view:
41
+ activeTabIndex >= state.questions.length ? submitView() : navigateView(),
42
+ };
43
+ }
44
+
45
+ export function moveOption(state: AskState, delta: number): AskState {
46
+ const normalizedDelta = delta < 0 ? -1 : 1;
47
+ if (isSubmitTab(state)) {
48
+ return {
49
+ ...state,
50
+ activeSubmitActionIndex: clamp(
51
+ state.activeSubmitActionIndex + normalizedDelta,
52
+ 0,
53
+ SUBMIT_ACTION_COUNT - 1
54
+ ),
55
+ };
56
+ }
57
+
58
+ const options = getRenderableOptions(getCurrentQuestion(state));
59
+ return {
60
+ ...state,
61
+ activeOptionIndex: clamp(
62
+ state.activeOptionIndex + normalizedDelta,
63
+ 0,
64
+ Math.max(0, options.length - 1)
65
+ ),
66
+ };
67
+ }
68
+
69
+ export function cancelFlow(state: AskState): AskState {
70
+ if (isEditingView(state)) {
71
+ return {
72
+ ...state,
73
+ view: isSubmitTab(state) ? submitView() : navigateView(),
74
+ };
75
+ }
76
+ return {
77
+ ...state,
78
+ cancelled: true,
79
+ completed: true,
80
+ };
81
+ }
82
+
83
+ export function dismissFlow(state: AskState): AskState {
84
+ const nextState = cancelFlow(state);
85
+ return nextState.completed ? nextState : cancelFlow(nextState);
86
+ }