@k3_2o/pi-ask-user-question 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/index.ts +8 -0
- package/package.json +53 -5
- package/src/dialog.ts +676 -0
- package/src/normalize.ts +148 -0
- package/src/respond.ts +46 -0
- package/src/result-interpretation.ts +20 -0
- package/src/schema.ts +31 -0
- package/src/text-layout.ts +76 -0
- package/src/tool.ts +173 -0
- package/src/transcript.ts +305 -0
- package/src/types.ts +41 -0
- package/src/validate.ts +62 -0
package/src/normalize.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { CanonicalAsk, CanonicalOption, CanonicalQuestion } from './types';
|
|
2
|
+
|
|
3
|
+
export const OTHER_LABEL = 'Type your own answer';
|
|
4
|
+
export const RECOMMENDED_SUFFIX = ' (Recommended)';
|
|
5
|
+
|
|
6
|
+
export function isReservedLabel(label: string): boolean {
|
|
7
|
+
return label === OTHER_LABEL;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function sanitizeCarriageReturns(value: string): string {
|
|
11
|
+
return value.replace(/\r/g, '');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function skipCsi(value: string, start: number): number {
|
|
15
|
+
let j = start + 2;
|
|
16
|
+
while (j < value.length) {
|
|
17
|
+
const c = value.charCodeAt(j);
|
|
18
|
+
if (c >= 0x40 && c <= 0x7e) return j + 1;
|
|
19
|
+
j += 1;
|
|
20
|
+
}
|
|
21
|
+
return j;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function skipOscLike(value: string, start: number): number {
|
|
25
|
+
let j = start + 2;
|
|
26
|
+
while (j < value.length) {
|
|
27
|
+
const c = value.charCodeAt(j);
|
|
28
|
+
if (c === 0x07) return j + 1;
|
|
29
|
+
if (c === 0x1b) return value.charCodeAt(j + 1) === 0x5c ? j + 2 : j + 1;
|
|
30
|
+
j += 1;
|
|
31
|
+
}
|
|
32
|
+
return j;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function sanitizeForDisplay(value: string): string {
|
|
36
|
+
const out: string[] = [];
|
|
37
|
+
let i = 0;
|
|
38
|
+
while (i < value.length) {
|
|
39
|
+
const code = value.charCodeAt(i);
|
|
40
|
+
if (code === 0x1b) {
|
|
41
|
+
const next = value.charCodeAt(i + 1);
|
|
42
|
+
if (next === 0x5b) {
|
|
43
|
+
i = skipCsi(value, i);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (next === 0x5d || next === 0x50 || next === 0x5e || next === 0x5f || next === 0x58) {
|
|
47
|
+
i = skipOscLike(value, i);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (next === 0x5c) {
|
|
51
|
+
i += 2;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
i += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (code === 0x09 || code === 0x0a) {
|
|
58
|
+
out.push(value.charAt(i));
|
|
59
|
+
i += 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (code < 0x20 || (code >= 0x7f && code <= 0x9f)) {
|
|
63
|
+
i += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
out.push(value.charAt(i));
|
|
67
|
+
i += 1;
|
|
68
|
+
}
|
|
69
|
+
return out.join('');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function sanitizeText(value: string): string {
|
|
73
|
+
const m = /\}\s*,\s*\{\s*"(?:label|description|value)"\s*:/.exec(value);
|
|
74
|
+
if (!m) return value;
|
|
75
|
+
return value.slice(0, m.index).replace(/[\s"',:;]+$/, '');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function clean(value: string | undefined): string | undefined {
|
|
79
|
+
if (typeof value !== 'string') return undefined;
|
|
80
|
+
const cleaned = sanitizeText(sanitizeForDisplay(value).trim());
|
|
81
|
+
return cleaned === '' ? undefined : cleaned;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function displayOptionLabels(options: CanonicalOption[], recommended?: number): string[] {
|
|
85
|
+
const badged = options.map((option, index) => {
|
|
86
|
+
const base = sanitizeCarriageReturns(option.label);
|
|
87
|
+
if (recommended !== index || base.endsWith(RECOMMENDED_SUFFIX)) return base;
|
|
88
|
+
return `${base}${RECOMMENDED_SUFFIX}`;
|
|
89
|
+
});
|
|
90
|
+
const seen = new Map<string, number>();
|
|
91
|
+
return badged.map((label) => {
|
|
92
|
+
if (isReservedLabel(label)) return label;
|
|
93
|
+
const count = seen.get(label) ?? 0;
|
|
94
|
+
seen.set(label, count + 1);
|
|
95
|
+
return count === 0 ? label : `${label} (${count + 1})`;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function toOption(value: unknown): CanonicalOption {
|
|
100
|
+
if (!value || typeof value !== 'object') {
|
|
101
|
+
throw new Error('Error: every option must be an object with a label');
|
|
102
|
+
}
|
|
103
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- guarded object boundary above
|
|
104
|
+
const entry = value as Record<string, unknown>;
|
|
105
|
+
const label = clean(typeof entry.label === 'string' ? entry.label : undefined);
|
|
106
|
+
if (!label) throw new Error('Error: every option must have a label');
|
|
107
|
+
const result: CanonicalOption = { label, value: label };
|
|
108
|
+
const description = clean(typeof entry.description === 'string' ? entry.description : undefined);
|
|
109
|
+
if (description) result.description = description;
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function toQuestion(value: unknown, index: number): CanonicalQuestion {
|
|
114
|
+
if (!value || typeof value !== 'object') {
|
|
115
|
+
throw new Error(`Error: question ${index + 1} must be an object`);
|
|
116
|
+
}
|
|
117
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- guarded object boundary above
|
|
118
|
+
const entry = value as Record<string, unknown>;
|
|
119
|
+
const question = clean(typeof entry.question === 'string' ? entry.question : undefined);
|
|
120
|
+
if (!question) throw new Error(`Error: question ${index + 1} is missing a question text`);
|
|
121
|
+
const rawOptions = Array.isArray(entry.options) ? entry.options : [];
|
|
122
|
+
const options = rawOptions.map(toOption);
|
|
123
|
+
if (options.length === 0) {
|
|
124
|
+
throw new Error(`Error: question ${index + 1} must have at least one labeled option`);
|
|
125
|
+
}
|
|
126
|
+
const id = clean(typeof entry.id === 'string' ? entry.id : undefined) ?? `q${index + 1}`;
|
|
127
|
+
const header = clean(typeof entry.header === 'string' ? entry.header : undefined);
|
|
128
|
+
const recommended =
|
|
129
|
+
typeof entry.recommended === 'number' && Number.isInteger(entry.recommended) ? entry.recommended : undefined;
|
|
130
|
+
const result: CanonicalQuestion = {
|
|
131
|
+
id,
|
|
132
|
+
question,
|
|
133
|
+
options,
|
|
134
|
+
multi: entry.multi === true,
|
|
135
|
+
};
|
|
136
|
+
if (header) result.header = header;
|
|
137
|
+
if (recommended !== undefined) result.recommended = recommended;
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function canonicalize(params: unknown): CanonicalAsk {
|
|
142
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- schema boundary
|
|
143
|
+
const raw = (params ?? {}) as Record<string, unknown>;
|
|
144
|
+
if (!Array.isArray(raw.questions) || raw.questions.length === 0) {
|
|
145
|
+
throw new Error('Error: at least one question with options is required');
|
|
146
|
+
}
|
|
147
|
+
return { questions: raw.questions.map(toQuestion) };
|
|
148
|
+
}
|
package/src/respond.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { displayOptionLabels } from './normalize';
|
|
2
|
+
import { interpretResultItem } from './result-interpretation';
|
|
3
|
+
import type { CanonicalQuestion, ResultItem } from './types';
|
|
4
|
+
|
|
5
|
+
export const CANCELLED_TEXT =
|
|
6
|
+
"User cancelled the question. Do not proceed with assumptions - wait for the user's next instruction.";
|
|
7
|
+
|
|
8
|
+
export const CANCELLED_BEFORE_DISPLAY_TEXT =
|
|
9
|
+
"Question cancelled before displaying. Do not proceed with assumptions - wait for the user's next instruction.";
|
|
10
|
+
|
|
11
|
+
export function formatResultItem(item: ResultItem, canonical?: CanonicalQuestion): string {
|
|
12
|
+
const displayLabels = canonical
|
|
13
|
+
? displayOptionLabels(canonical.options, canonical.recommended)
|
|
14
|
+
: (item.options?.map((option) => option.label) ?? []);
|
|
15
|
+
const interpretation = interpretResultItem(item, displayLabels);
|
|
16
|
+
if (interpretation.kind === 'custom') {
|
|
17
|
+
const input = interpretation.text
|
|
18
|
+
.split('\n')
|
|
19
|
+
.map((line) => ` ${line}`)
|
|
20
|
+
.join('\n');
|
|
21
|
+
return `${item.id} (custom):\n${input}`;
|
|
22
|
+
}
|
|
23
|
+
if (interpretation.kind === 'selected') {
|
|
24
|
+
return `${item.id}: ${interpretation.labels.join(', ')}`;
|
|
25
|
+
}
|
|
26
|
+
return interpretation.wasSkipped ? `${item.id}: (skipped, no answer)` : `${item.id}: (cancelled)`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function indented(value: string): string {
|
|
30
|
+
return value
|
|
31
|
+
.split('\n')
|
|
32
|
+
.map((line) => ` ${line}`)
|
|
33
|
+
.join('\n');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function buildSingleResponse(item: ResultItem): string {
|
|
37
|
+
const interpretation = interpretResultItem(item, item.options?.map((option) => option.label) ?? []);
|
|
38
|
+
if (interpretation.kind === 'custom') {
|
|
39
|
+
const input = indented(interpretation.text);
|
|
40
|
+
return `User provided custom input: ${input}`;
|
|
41
|
+
}
|
|
42
|
+
if (interpretation.kind === 'selected') {
|
|
43
|
+
return `User selected: ${interpretation.labels.join(', ')}`;
|
|
44
|
+
}
|
|
45
|
+
return 'User cancelled the question';
|
|
46
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ResultItem } from './types';
|
|
2
|
+
|
|
3
|
+
type ResultInterpretation =
|
|
4
|
+
| { kind: 'custom'; text: string }
|
|
5
|
+
| { kind: 'selected'; labels: string[] }
|
|
6
|
+
| { kind: 'empty'; wasSkipped: boolean };
|
|
7
|
+
|
|
8
|
+
export function interpretResultItem(item: ResultItem, displayLabels: string[]): ResultInterpretation {
|
|
9
|
+
if (item.customInput !== undefined) {
|
|
10
|
+
return { kind: 'custom', text: item.customInput };
|
|
11
|
+
}
|
|
12
|
+
if (item.selectedLabels.length > 0) {
|
|
13
|
+
const labels = item.selectedLabels.map((label) => {
|
|
14
|
+
const index = item.options?.findIndex((option) => option.label === label) ?? -1;
|
|
15
|
+
return index >= 0 ? (displayLabels[index] ?? label) : label;
|
|
16
|
+
});
|
|
17
|
+
return { kind: 'selected', labels };
|
|
18
|
+
}
|
|
19
|
+
return { kind: 'empty', wasSkipped: item.unanswered === true };
|
|
20
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Type } from 'typebox';
|
|
2
|
+
|
|
3
|
+
export const MIN_OPTIONS = 2;
|
|
4
|
+
export const MAX_OPTIONS = 5;
|
|
5
|
+
|
|
6
|
+
const optionSchema = Type.Object({
|
|
7
|
+
label: Type.String({ description: '2-5 words.' }),
|
|
8
|
+
description: Type.Optional(Type.String()),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const questionSchema = Type.Object({
|
|
12
|
+
id: Type.Optional(Type.String()),
|
|
13
|
+
question: Type.String({
|
|
14
|
+
description: 'Be specific.',
|
|
15
|
+
}),
|
|
16
|
+
header: Type.Optional(Type.String({ description: '1-2 words max.' })),
|
|
17
|
+
options: Type.Array(optionSchema, {
|
|
18
|
+
minItems: MIN_OPTIONS,
|
|
19
|
+
maxItems: MAX_OPTIONS,
|
|
20
|
+
}),
|
|
21
|
+
recommended: Type.Optional(Type.Integer({ description: 'Your pick, by index (0 = first).' })),
|
|
22
|
+
multi: Type.Optional(Type.Boolean({ description: 'Allow multiple selections.' })),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const askUserQuestionParamsSchema = Type.Object({
|
|
26
|
+
questions: Type.Array(questionSchema, {
|
|
27
|
+
description: 'Ask one question by default. Batch only independent questions that each block progress.',
|
|
28
|
+
minItems: 1,
|
|
29
|
+
maxItems: 5,
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { stripTerminalSequences, visibleWidth } from '@earendil-works/pi-tui';
|
|
2
|
+
|
|
3
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
4
|
+
|
|
5
|
+
function flushOversizeWord(out: string[], rawWord: string, maxWidth: number): void {
|
|
6
|
+
let chunk = '';
|
|
7
|
+
let chunkWidth = 0;
|
|
8
|
+
for (const { segment } of graphemeSegmenter.segment(rawWord)) {
|
|
9
|
+
const width = visibleWidth(segment);
|
|
10
|
+
if (chunkWidth + width > maxWidth) {
|
|
11
|
+
if (chunkWidth > 0) out.push(chunk);
|
|
12
|
+
if (width > maxWidth) {
|
|
13
|
+
out.push(segment);
|
|
14
|
+
chunk = '';
|
|
15
|
+
chunkWidth = 0;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
chunk = segment;
|
|
19
|
+
chunkWidth = width;
|
|
20
|
+
} else {
|
|
21
|
+
chunk += segment;
|
|
22
|
+
chunkWidth += width;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (chunk) out.push(chunk);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function wordWrap(text: string, maxWidth: number): string[] {
|
|
29
|
+
const width = Math.max(1, maxWidth);
|
|
30
|
+
const out: string[] = [];
|
|
31
|
+
let line = '';
|
|
32
|
+
let lineWidth = 0;
|
|
33
|
+
for (const rawWord of text.split(/\s+/)) {
|
|
34
|
+
if (rawWord === '') continue;
|
|
35
|
+
const wordWidth = visibleWidth(rawWord);
|
|
36
|
+
if (wordWidth > width) {
|
|
37
|
+
if (lineWidth > 0) {
|
|
38
|
+
out.push(line);
|
|
39
|
+
line = '';
|
|
40
|
+
lineWidth = 0;
|
|
41
|
+
}
|
|
42
|
+
flushOversizeWord(out, rawWord, width);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const gap = lineWidth === 0 ? 0 : 1;
|
|
46
|
+
if (lineWidth + gap + wordWidth <= width) {
|
|
47
|
+
if (lineWidth > 0) line += ' ';
|
|
48
|
+
line += rawWord;
|
|
49
|
+
lineWidth += gap + wordWidth;
|
|
50
|
+
} else {
|
|
51
|
+
if (lineWidth > 0) {
|
|
52
|
+
out.push(line);
|
|
53
|
+
line = '';
|
|
54
|
+
lineWidth = 0;
|
|
55
|
+
}
|
|
56
|
+
line = rawWord;
|
|
57
|
+
lineWidth = wordWidth;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (lineWidth > 0) out.push(line);
|
|
61
|
+
return out.length > 0 ? out : [''];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function clipToCells(text: string, cells: number): string {
|
|
65
|
+
if (cells < 1) return '';
|
|
66
|
+
if (visibleWidth(text) <= cells) return text;
|
|
67
|
+
let clipped = '';
|
|
68
|
+
let width = 0;
|
|
69
|
+
for (const { segment } of graphemeSegmenter.segment(stripTerminalSequences(text))) {
|
|
70
|
+
const segmentWidth = visibleWidth(segment);
|
|
71
|
+
if (width + segmentWidth > cells - 1) break;
|
|
72
|
+
clipped += segment;
|
|
73
|
+
width += segmentWidth;
|
|
74
|
+
}
|
|
75
|
+
return `${clipped}\u2026`;
|
|
76
|
+
}
|
package/src/tool.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { ToolDefinition } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
import { askUserQuestionParamsSchema } from './schema';
|
|
3
|
+
import { canonicalize } from './normalize';
|
|
4
|
+
import { validateCanonical } from './validate';
|
|
5
|
+
import { componentFor } from './dialog';
|
|
6
|
+
import { renderCall, renderResult, type AskRendererState } from './transcript';
|
|
7
|
+
import { CANCELLED_BEFORE_DISPLAY_TEXT, CANCELLED_TEXT, buildSingleResponse, formatResultItem } from './respond';
|
|
8
|
+
import type { AskUserQuestionResult, CanonicalAsk, DialogResult } from './types';
|
|
9
|
+
|
|
10
|
+
function cancelledResult(): AskUserQuestionResult {
|
|
11
|
+
return { results: [], cancelled: true };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ASK_WAITING_NOTIFY = 'ask: waiting for your answer...';
|
|
15
|
+
const ANSWER_RECEIVED_NOTIFY = 'ask: answer received';
|
|
16
|
+
|
|
17
|
+
function notifyBestEffort(
|
|
18
|
+
ctx: {
|
|
19
|
+
ui: { notify(message: string, level?: 'info' | 'warning' | 'error'): void };
|
|
20
|
+
},
|
|
21
|
+
message: string,
|
|
22
|
+
): void {
|
|
23
|
+
try {
|
|
24
|
+
ctx.ui.notify(message, 'info');
|
|
25
|
+
} catch (error) {
|
|
26
|
+
// Boundary: the dialog is the source of truth; a failed toast must not fail the answer.
|
|
27
|
+
console.warn('ask: notify failed', message, error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createAskUserQuestionDefinition(): ToolDefinition<
|
|
32
|
+
typeof askUserQuestionParamsSchema,
|
|
33
|
+
AskUserQuestionResult,
|
|
34
|
+
AskRendererState
|
|
35
|
+
> {
|
|
36
|
+
return {
|
|
37
|
+
name: 'ask',
|
|
38
|
+
label: 'Ask',
|
|
39
|
+
description:
|
|
40
|
+
'Pause to ask only when a real ambiguity could change your next action and guessing would be costly or hard to reverse. ' +
|
|
41
|
+
'Otherwise, choose a safe, reversible default.',
|
|
42
|
+
parameters: askUserQuestionParamsSchema,
|
|
43
|
+
promptSnippet: 'Ask the user a question',
|
|
44
|
+
executionMode: 'sequential',
|
|
45
|
+
prepareArguments(args) {
|
|
46
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- passthrough to schema type
|
|
47
|
+
if (!args || typeof args !== 'object') return args as never;
|
|
48
|
+
try {
|
|
49
|
+
const canonical = canonicalize(args);
|
|
50
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- passthrough to schema type
|
|
51
|
+
if (canonical.questions.length === 0) return args as never;
|
|
52
|
+
return {
|
|
53
|
+
questions: canonical.questions.map((question) => ({
|
|
54
|
+
...(question.id ? { id: question.id } : {}),
|
|
55
|
+
question: question.question,
|
|
56
|
+
...(question.header ? { header: question.header } : {}),
|
|
57
|
+
options: question.options.map((option) => ({
|
|
58
|
+
label: option.label,
|
|
59
|
+
...(option.description ? { description: option.description } : {}),
|
|
60
|
+
})),
|
|
61
|
+
...(question.recommended !== undefined ? { recommended: question.recommended } : {}),
|
|
62
|
+
...(question.multi ? { multi: true } : {}),
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
} catch {
|
|
66
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- passthrough to schema type
|
|
67
|
+
return args as never;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
71
|
+
let canonical: CanonicalAsk;
|
|
72
|
+
try {
|
|
73
|
+
canonical = canonicalize(params);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
const message = error instanceof Error ? error.message : 'Error: malformed ask parameters';
|
|
76
|
+
return {
|
|
77
|
+
content: [{ type: 'text' as const, text: message }],
|
|
78
|
+
details: { results: [], error: message },
|
|
79
|
+
isError: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const validationError = validateCanonical(canonical);
|
|
83
|
+
if (validationError) {
|
|
84
|
+
return {
|
|
85
|
+
content: [{ type: 'text' as const, text: validationError }],
|
|
86
|
+
details: { results: [], error: validationError },
|
|
87
|
+
isError: true,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (!ctx.hasUI) {
|
|
91
|
+
return {
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
type: 'text' as const,
|
|
95
|
+
text: 'Error: UI not available (running in non-interactive mode)',
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
details: { results: [], error: 'UI not available' },
|
|
99
|
+
isError: true,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (signal?.aborted) {
|
|
103
|
+
return {
|
|
104
|
+
content: [{ type: 'text' as const, text: CANCELLED_BEFORE_DISPLAY_TEXT }],
|
|
105
|
+
details: cancelledResult(),
|
|
106
|
+
isError: true,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
notifyBestEffort(ctx, ASK_WAITING_NOTIFY);
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const result = await ctx.ui.custom<DialogResult | null>((tui, theme, _kb, done) =>
|
|
115
|
+
componentFor(canonical.questions, { tui, theme, signal, done }),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (!result) {
|
|
119
|
+
return {
|
|
120
|
+
content: [{ type: 'text' as const, text: CANCELLED_TEXT }],
|
|
121
|
+
details: cancelledResult(),
|
|
122
|
+
isError: true,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
notifyBestEffort(ctx, ANSWER_RECEIVED_NOTIFY);
|
|
127
|
+
|
|
128
|
+
const details: AskUserQuestionResult = { results: result.results };
|
|
129
|
+
if (canonical.questions.length === 1 && result.results.length === 1) {
|
|
130
|
+
return {
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: 'text' as const,
|
|
134
|
+
text: buildSingleResponse(result.results[0]),
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
details,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const text = `User answers:\n${result.results
|
|
141
|
+
.map((item) =>
|
|
142
|
+
formatResultItem(
|
|
143
|
+
item,
|
|
144
|
+
canonical.questions.find((question) => question.id === item.id),
|
|
145
|
+
),
|
|
146
|
+
)
|
|
147
|
+
.join('\n')}`;
|
|
148
|
+
return { content: [{ type: 'text' as const, text }], details };
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
151
|
+
return {
|
|
152
|
+
content: [{ type: 'text' as const, text: CANCELLED_TEXT }],
|
|
153
|
+
details: cancelledResult(),
|
|
154
|
+
isError: true,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
161
|
+
return {
|
|
162
|
+
content: [{ type: 'text' as const, text: CANCELLED_BEFORE_DISPLAY_TEXT }],
|
|
163
|
+
details: cancelledResult(),
|
|
164
|
+
isError: true,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
renderCall,
|
|
171
|
+
renderResult,
|
|
172
|
+
};
|
|
173
|
+
}
|