@juicesharp/rpiv-ask-user-question 0.9.1 → 0.11.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.
@@ -113,41 +113,37 @@ export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
113
113
  handleInput: (data) => {
114
114
  const currentItem = itemAt(selectionIndex, mainItems, chatItems);
115
115
  const isInlineInputActive = !!currentItem?.isOther;
116
- const kb = getKeybindings();
117
-
118
- if (kb.matches(data, KEYBIND_UP)) {
119
- if (isInlineInputActive) mainList.clearInputBuffer();
120
- selectionIndex = wrapIndex(selectionIndex - 1, totalCount);
121
- applySelection();
122
- tui.requestRender();
123
- return;
124
- }
125
- if (kb.matches(data, KEYBIND_DOWN)) {
126
- if (isInlineInputActive) mainList.clearInputBuffer();
127
- selectionIndex = wrapIndex(selectionIndex + 1, totalCount);
128
- applySelection();
129
- tui.requestRender();
130
- return;
131
- }
132
- if (kb.matches(data, KEYBIND_CONFIRM)) {
133
- if (isInlineInputActive) {
134
- done({ label: mainList.getInputBuffer(), isOther: true });
135
- } else if (currentItem) {
136
- done(currentItem);
137
- }
138
- return;
139
- }
140
- if (kb.matches(data, KEYBIND_CANCEL)) {
141
- done(null);
142
- return;
143
- }
144
- if (isInlineInputActive) {
145
- if (BACKSPACE_CHARS.has(data)) {
116
+ const action = dispatchQuestionInput(data, {
117
+ selectionIndex,
118
+ totalCount,
119
+ currentItem,
120
+ isInlineInputActive,
121
+ inputBuffer: mainList.getInputBuffer(),
122
+ keybindings: getKeybindings(),
123
+ });
124
+ switch (action.kind) {
125
+ case "nav":
126
+ if (isInlineInputActive) mainList.clearInputBuffer();
127
+ selectionIndex = action.nextIndex;
128
+ applySelection();
129
+ tui.requestRender();
130
+ return;
131
+ case "confirm":
132
+ done(action.choice);
133
+ return;
134
+ case "cancel":
135
+ done(null);
136
+ return;
137
+ case "backspace":
146
138
  mainList.backspaceInput();
147
- } else if (data && !data.startsWith(ESC_SEQUENCE_PREFIX)) {
148
- mainList.appendInput(data);
149
- }
150
- tui.requestRender();
139
+ tui.requestRender();
140
+ return;
141
+ case "append":
142
+ mainList.appendInput(action.data);
143
+ tui.requestRender();
144
+ return;
145
+ case "ignore":
146
+ return;
151
147
  }
152
148
  },
153
149
  };
@@ -158,14 +154,14 @@ export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
158
154
  });
159
155
  }
160
156
 
161
- function buildMainItems(options: QuestionOption[]): WrappingSelectItem[] {
157
+ export function buildMainItems(options: QuestionOption[]): WrappingSelectItem[] {
162
158
  return [
163
159
  ...options.map((o) => ({ label: o.label, description: o.description })),
164
160
  { label: TYPE_SOMETHING_LABEL, isOther: true },
165
161
  ];
166
162
  }
167
163
 
168
- function itemAt(
164
+ export function itemAt(
169
165
  index: number,
170
166
  mainItems: WrappingSelectItem[],
171
167
  chatItems: WrappingSelectItem[],
@@ -173,11 +169,59 @@ function itemAt(
173
169
  return index < mainItems.length ? mainItems[index] : chatItems[index - mainItems.length];
174
170
  }
175
171
 
176
- function wrapIndex(index: number, total: number): number {
172
+ export function wrapIndex(index: number, total: number): number {
177
173
  return ((index % total) + total) % total;
178
174
  }
179
175
 
180
- function buildDialogContainer(
176
+ export type QuestionInputAction =
177
+ | { kind: "nav"; nextIndex: number }
178
+ | { kind: "confirm"; choice: WrappingSelectItem }
179
+ | { kind: "cancel" }
180
+ | { kind: "backspace" }
181
+ | { kind: "append"; data: string }
182
+ | { kind: "ignore" };
183
+
184
+ export interface DispatchState {
185
+ selectionIndex: number;
186
+ totalCount: number;
187
+ currentItem: WrappingSelectItem | undefined;
188
+ isInlineInputActive: boolean;
189
+ inputBuffer: string;
190
+ keybindings: { matches(data: string, name: string): boolean };
191
+ }
192
+
193
+ export function dispatchQuestionInput(data: string, state: DispatchState): QuestionInputAction {
194
+ const { keybindings: kb } = state;
195
+ if (kb.matches(data, KEYBIND_UP)) {
196
+ return { kind: "nav", nextIndex: wrapIndex(state.selectionIndex - 1, state.totalCount) };
197
+ }
198
+ if (kb.matches(data, KEYBIND_DOWN)) {
199
+ return { kind: "nav", nextIndex: wrapIndex(state.selectionIndex + 1, state.totalCount) };
200
+ }
201
+ if (kb.matches(data, KEYBIND_CONFIRM)) {
202
+ if (state.isInlineInputActive) {
203
+ return { kind: "confirm", choice: { label: state.inputBuffer, isOther: true } };
204
+ }
205
+ if (state.currentItem) {
206
+ return { kind: "confirm", choice: state.currentItem };
207
+ }
208
+ return { kind: "ignore" };
209
+ }
210
+ if (kb.matches(data, KEYBIND_CANCEL)) {
211
+ return { kind: "cancel" };
212
+ }
213
+ if (state.isInlineInputActive) {
214
+ if (BACKSPACE_CHARS.has(data)) {
215
+ return { kind: "backspace" };
216
+ }
217
+ if (data && !data.startsWith(ESC_SEQUENCE_PREFIX)) {
218
+ return { kind: "append", data };
219
+ }
220
+ }
221
+ return { kind: "ignore" };
222
+ }
223
+
224
+ export function buildDialogContainer(
181
225
  theme: Theme,
182
226
  params: QuestionParams,
183
227
  mainList: WrappingSelect,
@@ -203,7 +247,7 @@ function buildDialogContainer(
203
247
  return container;
204
248
  }
205
249
 
206
- function buildResponse(choice: WrappingSelectItem | null, params: QuestionParams) {
250
+ export function buildResponse(choice: WrappingSelectItem | null, params: QuestionParams) {
207
251
  if (!choice) {
208
252
  return buildToolResult(DECLINE_MESSAGE, { question: params.question, answer: null });
209
253
  }
@@ -229,7 +273,7 @@ function buildResponse(choice: WrappingSelectItem | null, params: QuestionParams
229
273
  });
230
274
  }
231
275
 
232
- function buildToolResult(text: string, details: ToolDetails) {
276
+ export function buildToolResult(text: string, details: ToolDetails) {
233
277
  return {
234
278
  content: [{ type: "text" as const, text }],
235
279
  details,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "0.9.1",
3
+ "version": "0.11.0",
4
4
  "description": "Pi extension: lets the model ask you a clarifying question with structured options instead of guessing",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -22,6 +22,9 @@
22
22
  "publishConfig": {
23
23
  "access": "public"
24
24
  },
25
+ "scripts": {
26
+ "test": "vitest run"
27
+ },
25
28
  "files": [
26
29
  "index.ts",
27
30
  "ask-user-question.ts",