@tduyng/pi-ask 0.0.1 → 0.0.2

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
@@ -49,5 +49,5 @@ Local checkout: `pi install /path/to/extensions/pi-ask`
49
49
  ## Tests
50
50
 
51
51
  ```bash
52
- pnpm vitest run extensions/pi-ask
52
+ bun test extensions/pi-ask
53
53
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tduyng/pi-ask",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "One tool that stops and waits: ask the user up to 4 questions with options and a stated guess, answered in the terminal.",
5
5
  "keywords": [
6
6
  "pi",
package/src/index.ts CHANGED
@@ -29,24 +29,43 @@ function title(question: AskParams['questions'][number]): string {
29
29
  export async function askUser(params: AskParams, ctx: Dialog): Promise<string> {
30
30
  if (!ctx.hasUI) return NO_UI_ANSWER
31
31
  const lines: string[] = []
32
- for (const question of params.questions.slice(0, MAX_QUESTIONS)) {
32
+ const questions = params.questions.slice(0, MAX_QUESTIONS)
33
+ for (let index = 0; index < questions.length; index++) {
34
+ const question = questions[index]!
33
35
  let answer: string
34
36
  if (question.options && question.options.length > 0) {
35
37
  const pick = await ctx.ui.select(title(question), [...question.options, OTHER])
36
- answer =
37
- pick === undefined
38
- ? '(skipped)'
39
- : pick === OTHER
40
- ? ((await ctx.ui.input(title(question), 'your answer')) ?? '(skipped)')
41
- : pick
38
+ if (pick === undefined) {
39
+ lines.push(...cancelled(questions.slice(index)))
40
+ break
41
+ }
42
+ if (pick === OTHER) {
43
+ const typed = await ctx.ui.input(title(question), 'your answer')
44
+ if (typed === undefined) {
45
+ lines.push(...cancelled(questions.slice(index)))
46
+ break
47
+ }
48
+ answer = typed.trim() || '(skipped)'
49
+ } else {
50
+ answer = pick
51
+ }
42
52
  } else {
43
- answer = (await ctx.ui.input(title(question))) ?? '(skipped)'
53
+ const typed = await ctx.ui.input(title(question))
54
+ if (typed === undefined) {
55
+ lines.push(...cancelled(questions.slice(index)))
56
+ break
57
+ }
58
+ answer = typed.trim() || '(skipped)'
44
59
  }
45
60
  lines.push(`Q: ${question.question}\nA: ${answer}`)
46
61
  }
47
62
  return lines.join('\n\n') || 'No well-formed questions were provided.'
48
63
  }
49
64
 
65
+ function cancelled(questions: AskParams['questions']): string[] {
66
+ return questions.map((question) => `Q: ${question.question}\nA: (cancelled)`)
67
+ }
68
+
50
69
  export default function askUserExtension(pi: Pick<ExtensionAPI, 'registerTool'>): void {
51
70
  pi.registerTool({
52
71
  name: 'ask_user',