@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 +1 -1
- package/package.json +1 -1
- package/src/index.ts +27 -8
package/README.md
CHANGED
package/package.json
CHANGED
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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',
|