@mrclrchtr/supi-ask-user 1.13.0 → 1.14.1
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 +157 -65
- package/node_modules/@mrclrchtr/supi-core/README.md +10 -0
- package/node_modules/@mrclrchtr/supi-core/package.json +2 -1
- package/node_modules/@mrclrchtr/supi-core/src/api.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/config/config.ts +3 -2
- package/node_modules/@mrclrchtr/supi-core/src/index.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/progress-widget.ts +98 -17
- package/node_modules/@mrclrchtr/supi-core/src/report.ts +121 -0
- package/package.json +2 -2
- package/src/api.ts +7 -5
- package/src/ask-user.ts +23 -7
- package/src/index.ts +7 -5
- package/src/normalize.ts +47 -30
- package/src/render/result.ts +72 -49
- package/src/render/transcript.ts +90 -62
- package/src/schema.ts +8 -30
- package/src/session/controller.ts +226 -193
- package/src/session/lock.ts +1 -1
- package/src/tool/guidance.ts +4 -1
- package/src/types.ts +46 -35
- package/src/ui/choose-renderer.ts +20 -4
- package/src/ui/form-component.ts +574 -0
- package/src/ui/form-render-primitives.ts +71 -0
- package/src/ui/form-render.ts +283 -0
- package/src/ui/form-review-render.ts +144 -0
- package/src/ui/form-view.ts +23 -0
- package/src/ui/{overlay.ts → form.ts} +12 -9
- package/src/ui/types.ts +7 -3
- package/src/ui/overlay-actions.ts +0 -42
- package/src/ui/overlay-component.ts +0 -400
- package/src/ui/overlay-render.ts +0 -230
- package/src/ui/overlay-view.ts +0 -327
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/** Minimal theme surface required by the shared report helpers. */
|
|
5
|
+
export type ReportTheme = Pick<Theme, "fg">;
|
|
6
|
+
|
|
7
|
+
/** Color keys accepted by the report helpers. */
|
|
8
|
+
export type ReportColor = Parameters<Theme["fg"]>[0];
|
|
9
|
+
|
|
10
|
+
/** Options for rendering a themed key/value report row. */
|
|
11
|
+
export interface KeyValueLineOptions {
|
|
12
|
+
/** The label shown on the left. */
|
|
13
|
+
label: string;
|
|
14
|
+
/** The value shown on the right. */
|
|
15
|
+
value: string;
|
|
16
|
+
/** Theme used for color formatting. */
|
|
17
|
+
theme: ReportTheme;
|
|
18
|
+
/** Maximum rendered width. */
|
|
19
|
+
width: number;
|
|
20
|
+
/** Left indentation in spaces. */
|
|
21
|
+
indent?: number;
|
|
22
|
+
/** Theme color applied to the label. Defaults to `"text"`. */
|
|
23
|
+
labelColor?: ReportColor;
|
|
24
|
+
/** Theme color applied to the value. Defaults to `"dim"`. */
|
|
25
|
+
valueColor?: ReportColor;
|
|
26
|
+
/** Separator placed between label and value. Defaults to `": "`. */
|
|
27
|
+
separator?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Options for rendering a preview-overflow hint. */
|
|
31
|
+
export interface OverflowHintOptions {
|
|
32
|
+
/** Optional follow-up hint such as `run /supi-context full`. */
|
|
33
|
+
hint?: string | null;
|
|
34
|
+
/** Left indentation in spaces. Defaults to `2`. */
|
|
35
|
+
indent?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Ensure a report width never drops below the minimum readable width. */
|
|
39
|
+
export function clampReportWidth(width: number, minWidth = 24): number {
|
|
40
|
+
return Math.max(minWidth, width);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Render a top-level report title line with theme color and truncation. */
|
|
44
|
+
export function formatReportTitle(
|
|
45
|
+
title: string,
|
|
46
|
+
theme: ReportTheme,
|
|
47
|
+
width: number,
|
|
48
|
+
color: ReportColor = "accent",
|
|
49
|
+
): string {
|
|
50
|
+
return truncateToWidth(theme.fg(color, title), width);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Render a section header with optional dimmed metadata.
|
|
55
|
+
*
|
|
56
|
+
* Example: `Usage by category 42.3k tokens`
|
|
57
|
+
*/
|
|
58
|
+
export function formatSectionHeader(
|
|
59
|
+
title: string,
|
|
60
|
+
meta: string | null,
|
|
61
|
+
theme: ReportTheme,
|
|
62
|
+
width: number,
|
|
63
|
+
): string {
|
|
64
|
+
const left = theme.fg("text", title);
|
|
65
|
+
const content = meta ? `${left}${theme.fg("dim", ` ${meta}`)}` : left;
|
|
66
|
+
return truncateToWidth(content, width);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Render a single dimmed report line with optional left indentation. */
|
|
70
|
+
export function formatDimLine(text: string, theme: ReportTheme, width: number, indent = 0): string {
|
|
71
|
+
const safeIndent = Math.max(0, indent);
|
|
72
|
+
return truncateToWidth(`${" ".repeat(safeIndent)}${theme.fg("dim", text)}`, width);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Render a dimmed preview-overflow hint such as `… and 3 more — run /foo full`. */
|
|
76
|
+
export function formatOverflowHint(
|
|
77
|
+
hiddenCount: number,
|
|
78
|
+
theme: ReportTheme,
|
|
79
|
+
width: number,
|
|
80
|
+
options: OverflowHintOptions = {},
|
|
81
|
+
): string {
|
|
82
|
+
const { hint = null, indent = 2 } = options;
|
|
83
|
+
const suffix = hint ? ` — ${hint}` : "";
|
|
84
|
+
return formatDimLine(`… and ${hiddenCount} more${suffix}`, theme, width, indent);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Render a single themed `label: value` row with truncation. */
|
|
88
|
+
export function formatKeyValueLine(options: KeyValueLineOptions): string {
|
|
89
|
+
const {
|
|
90
|
+
label,
|
|
91
|
+
value,
|
|
92
|
+
theme,
|
|
93
|
+
width,
|
|
94
|
+
indent = 2,
|
|
95
|
+
labelColor = "text",
|
|
96
|
+
valueColor = "dim",
|
|
97
|
+
separator = ": ",
|
|
98
|
+
} = options;
|
|
99
|
+
const safeIndent = Math.max(0, indent);
|
|
100
|
+
|
|
101
|
+
return truncateToWidth(
|
|
102
|
+
`${" ".repeat(safeIndent)}${theme.fg(labelColor, label)}${separator}${theme.fg(valueColor, value)}`,
|
|
103
|
+
width,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Wrap a report text block to the available width and optionally prefix each line.
|
|
109
|
+
*
|
|
110
|
+
* This is useful for wrapped bullets or explanatory notes that should align
|
|
111
|
+
* under an existing report indent.
|
|
112
|
+
*/
|
|
113
|
+
export function wrapReportText(
|
|
114
|
+
text: string,
|
|
115
|
+
width: number,
|
|
116
|
+
options: { indent?: string } = {},
|
|
117
|
+
): string[] {
|
|
118
|
+
const indent = options.indent ?? "";
|
|
119
|
+
const wrapped = wrapTextWithAnsi(text, Math.max(1, width - indent.length));
|
|
120
|
+
return indent ? wrapped.map((line) => `${indent}${line}`) : wrapped;
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrclrchtr/supi-ask-user",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.1",
|
|
4
4
|
"description": "SuPi ask-user extension — rich questionnaire UI for structured agent-user decisions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"main": "src/api.ts",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@mrclrchtr/supi-core": "1.
|
|
24
|
+
"@mrclrchtr/supi-core": "1.14.1"
|
|
25
25
|
},
|
|
26
26
|
"bundledDependencies": [
|
|
27
27
|
"@mrclrchtr/supi-core"
|
package/src/api.ts
CHANGED
|
@@ -4,17 +4,19 @@ export { AskUserParamsSchema } from "./schema.ts";
|
|
|
4
4
|
export { AskUserController } from "./session/controller.ts";
|
|
5
5
|
export { ActiveQuestionnaireLock } from "./session/lock.ts";
|
|
6
6
|
export type {
|
|
7
|
-
Answer,
|
|
8
7
|
AskUserDetails,
|
|
9
8
|
AskUserErrorDetails,
|
|
9
|
+
AskUserInteractionAbort,
|
|
10
|
+
AskUserInteractionCancel,
|
|
11
|
+
AskUserInteractionResult,
|
|
10
12
|
AskUserOutcome,
|
|
11
|
-
|
|
13
|
+
AskUserOutcomeKind,
|
|
14
|
+
AskUserResponse,
|
|
12
15
|
AskUserToolDetails,
|
|
13
|
-
|
|
14
|
-
CustomAnswer,
|
|
16
|
+
ChoiceQuestionResponse,
|
|
15
17
|
NormalizedChoiceQuestion,
|
|
16
18
|
NormalizedQuestion,
|
|
17
19
|
NormalizedQuestionnaire,
|
|
18
20
|
NormalizedTextQuestion,
|
|
19
|
-
|
|
21
|
+
TextQuestionResponse,
|
|
20
22
|
} from "./types.ts";
|
package/src/ask-user.ts
CHANGED
|
@@ -7,13 +7,18 @@ import { buildTreeSummaryLabel } from "./render/tree-summary.ts";
|
|
|
7
7
|
import { type AskUserParams, AskUserParamsSchema } from "./schema.ts";
|
|
8
8
|
import { ActiveQuestionnaireLock } from "./session/lock.ts";
|
|
9
9
|
import { promptGuidelines, promptSnippet, toolDescription } from "./tool/guidance.ts";
|
|
10
|
-
import type {
|
|
10
|
+
import type {
|
|
11
|
+
AskUserInteractionResult,
|
|
12
|
+
AskUserOutcome,
|
|
13
|
+
AskUserToolDetails,
|
|
14
|
+
NormalizedQuestionnaire,
|
|
15
|
+
} from "./types.ts";
|
|
11
16
|
import { runQuestionnaire } from "./ui/choose-renderer.ts";
|
|
12
17
|
|
|
13
18
|
const TOOL_NAME = "ask_user";
|
|
14
19
|
const TOOL_LABEL = "Ask User";
|
|
15
20
|
|
|
16
|
-
export type AskUserExecutionContext = Pick<ExtensionContext, "cwd" | "hasUI" | "abort"> & {
|
|
21
|
+
export type AskUserExecutionContext = Pick<ExtensionContext, "cwd" | "hasUI" | "mode" | "abort"> & {
|
|
17
22
|
ui: {
|
|
18
23
|
custom?: unknown;
|
|
19
24
|
notify?(message: string, type?: "info" | "warning" | "error"): void;
|
|
@@ -61,9 +66,9 @@ export async function executeAskUser(
|
|
|
61
66
|
throw error;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
if (!ctx.hasUI) {
|
|
69
|
+
if (!ctx.hasUI || ctx.mode !== "tui") {
|
|
65
70
|
return buildErrorResult(
|
|
66
|
-
"Error: ask_user requires an interactive
|
|
71
|
+
"Error: ask_user requires an interactive TUI session. No user-facing form UI is available in the current mode.",
|
|
67
72
|
);
|
|
68
73
|
}
|
|
69
74
|
if (!lock.acquire()) {
|
|
@@ -91,12 +96,14 @@ export async function executeAskUser(
|
|
|
91
96
|
|
|
92
97
|
if (outcome === "unsupported") {
|
|
93
98
|
return buildErrorResult(
|
|
94
|
-
"Error: ask_user requires a TUI with custom
|
|
99
|
+
"Error: ask_user requires a TUI with custom form support. Do not use ask_user in non-interactive or degraded UI sessions.",
|
|
95
100
|
);
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
|
|
103
|
+
// Internal cancel/abort: treat as control flow, abort the turn
|
|
104
|
+
if (isInternalInteractionResult(outcome)) {
|
|
99
105
|
ctx.abort();
|
|
106
|
+
return buildErrorResult("The user interaction was cancelled.");
|
|
100
107
|
}
|
|
101
108
|
|
|
102
109
|
pi.appendEntry(buildTreeSummaryLabel(questionnaire));
|
|
@@ -109,6 +116,16 @@ export async function executeAskUser(
|
|
|
109
116
|
}
|
|
110
117
|
}
|
|
111
118
|
|
|
119
|
+
function isInternalInteractionResult(
|
|
120
|
+
outcome: AskUserOutcome | AskUserInteractionResult | "unsupported",
|
|
121
|
+
): outcome is AskUserInteractionResult {
|
|
122
|
+
return (
|
|
123
|
+
typeof outcome === "object" &&
|
|
124
|
+
"kind" in outcome &&
|
|
125
|
+
(outcome.kind === "cancel" || outcome.kind === "abort")
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
112
129
|
function signalAttention(ctx: AskUserExecutionContext): void {
|
|
113
130
|
signalWaiting(ctx, "pi — waiting for your input");
|
|
114
131
|
}
|
|
@@ -122,7 +139,6 @@ function asFunction<T extends (...args: never[]) => unknown>(value: unknown): T
|
|
|
122
139
|
}
|
|
123
140
|
|
|
124
141
|
export { AskUserValidationError, normalizeQuestionnaire } from "./normalize.ts";
|
|
125
|
-
export { buildErrorResult, buildResult } from "./render/result.ts";
|
|
126
142
|
export { AskUserController } from "./session/controller.ts";
|
|
127
143
|
export { ActiveQuestionnaireLock } from "./session/lock.ts";
|
|
128
144
|
export {
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
export type {
|
|
2
|
-
Answer,
|
|
3
2
|
AskUserDetails,
|
|
4
3
|
AskUserErrorDetails,
|
|
4
|
+
AskUserInteractionAbort,
|
|
5
|
+
AskUserInteractionCancel,
|
|
6
|
+
AskUserInteractionResult,
|
|
5
7
|
AskUserOutcome,
|
|
6
|
-
|
|
8
|
+
AskUserOutcomeKind,
|
|
9
|
+
AskUserResponse,
|
|
7
10
|
AskUserToolDetails,
|
|
8
|
-
|
|
9
|
-
CustomAnswer,
|
|
11
|
+
ChoiceQuestionResponse,
|
|
10
12
|
NormalizedChoiceQuestion,
|
|
11
13
|
NormalizedQuestion,
|
|
12
14
|
NormalizedQuestionnaire,
|
|
13
15
|
NormalizedTextQuestion,
|
|
14
|
-
|
|
16
|
+
TextQuestionResponse,
|
|
15
17
|
} from "./api.ts";
|
|
16
18
|
export {
|
|
17
19
|
ActiveQuestionnaireLock,
|
package/src/normalize.ts
CHANGED
|
@@ -15,6 +15,10 @@ import {
|
|
|
15
15
|
type NormalizedTextQuestion,
|
|
16
16
|
} from "./types.ts";
|
|
17
17
|
|
|
18
|
+
const DEPRECATED_TOP_LEVEL_KEYS = ["allowPartialSubmit"] as const;
|
|
19
|
+
const DEPRECATED_CHOICE_KEYS = ["required", "initial", "allowOther"] as const;
|
|
20
|
+
const DEPRECATED_TEXT_KEYS = ["required", "initial"] as const;
|
|
21
|
+
|
|
18
22
|
export class AskUserValidationError extends Error {
|
|
19
23
|
constructor(message: string) {
|
|
20
24
|
super(message);
|
|
@@ -23,6 +27,15 @@ export class AskUserValidationError extends Error {
|
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
export function normalizeQuestionnaire(params: AskUserParams): NormalizedQuestionnaire {
|
|
30
|
+
// Reject deprecated top-level fields
|
|
31
|
+
for (const key of DEPRECATED_TOP_LEVEL_KEYS) {
|
|
32
|
+
if (key in params) {
|
|
33
|
+
throw new AskUserValidationError(
|
|
34
|
+
`The "${key}" field is no longer supported. All questions are always required for a full submission. Use "needs_discussion" outcome for unanswered questions.`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
26
39
|
validateQuestionCount(params.questions.length);
|
|
27
40
|
const title = trimOptional(params.title);
|
|
28
41
|
const intro = trimOptional(params.intro);
|
|
@@ -49,7 +62,6 @@ export function normalizeQuestionnaire(params: AskUserParams): NormalizedQuestio
|
|
|
49
62
|
...(title ? { title } : {}),
|
|
50
63
|
...(intro ? { intro } : {}),
|
|
51
64
|
questions,
|
|
52
|
-
allowPartialSubmit: params.allowPartialSubmit ?? false,
|
|
53
65
|
};
|
|
54
66
|
}
|
|
55
67
|
|
|
@@ -91,45 +103,47 @@ function validateCommonFields(question: ExternalQuestion): void {
|
|
|
91
103
|
}
|
|
92
104
|
|
|
93
105
|
function normalizeChoice(question: ExternalChoiceQuestion): NormalizedChoiceQuestion {
|
|
106
|
+
// Reject deprecated fields on choice questions
|
|
107
|
+
for (const key of DEPRECATED_CHOICE_KEYS) {
|
|
108
|
+
if (key in question) {
|
|
109
|
+
throw new AskUserValidationError(
|
|
110
|
+
`The "${key}" field on choice questions is no longer supported. Use "recommendation" for suggested options.`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
94
115
|
const options = normalizeOptions(question.id.trim(), question.options);
|
|
95
116
|
const multi = question.multi ?? false;
|
|
96
|
-
const allowOther = question.allowOther ?? false;
|
|
97
|
-
if (multi && allowOther) {
|
|
98
|
-
throw new AskUserValidationError(
|
|
99
|
-
`choice question "${question.id}" cannot use allowOther together with multi-select.`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
117
|
|
|
103
|
-
|
|
104
|
-
validateSelectionShape(question.id, question.initial, multi, "initial");
|
|
118
|
+
validateRecommendationShape(question.id, question.recommendation, multi);
|
|
105
119
|
|
|
106
120
|
return {
|
|
107
121
|
id: question.id.trim(),
|
|
108
122
|
header: question.header.trim(),
|
|
109
123
|
prompt: question.prompt.trim(),
|
|
110
|
-
required: question.required ?? true,
|
|
111
124
|
type: "choice",
|
|
112
125
|
options,
|
|
113
126
|
multi,
|
|
114
|
-
allowOther,
|
|
115
127
|
recommendedIndexes: resolveIndexes({
|
|
116
128
|
questionId: question.id,
|
|
117
129
|
options,
|
|
118
130
|
value: question.recommendation,
|
|
119
131
|
multi,
|
|
120
|
-
|
|
121
|
-
}),
|
|
122
|
-
initialIndexes: resolveIndexes({
|
|
123
|
-
questionId: question.id,
|
|
124
|
-
options,
|
|
125
|
-
value: question.initial,
|
|
126
|
-
multi,
|
|
127
|
-
kind: "initial",
|
|
132
|
+
defaultToFirst: !multi,
|
|
128
133
|
}),
|
|
129
134
|
};
|
|
130
135
|
}
|
|
131
136
|
|
|
132
137
|
function normalizeText(question: ExternalTextQuestion): NormalizedTextQuestion {
|
|
138
|
+
// Reject deprecated fields on text questions
|
|
139
|
+
for (const key of DEPRECATED_TEXT_KEYS) {
|
|
140
|
+
if (key in question) {
|
|
141
|
+
throw new AskUserValidationError(
|
|
142
|
+
`The "${key}" field on text questions is no longer supported. Use "recommendation" for suggested text.`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
133
147
|
const placeholder = trimOptional(question.placeholder);
|
|
134
148
|
if (placeholder && placeholder.length > ASK_USER_LIMITS.maxPlaceholderLength) {
|
|
135
149
|
throw new AskUserValidationError(
|
|
@@ -137,13 +151,14 @@ function normalizeText(question: ExternalTextQuestion): NormalizedTextQuestion {
|
|
|
137
151
|
);
|
|
138
152
|
}
|
|
139
153
|
|
|
154
|
+
const recommendation = trimOptional(question.recommendation);
|
|
155
|
+
|
|
140
156
|
return {
|
|
141
157
|
id: question.id.trim(),
|
|
142
158
|
header: question.header.trim(),
|
|
143
159
|
prompt: question.prompt.trim(),
|
|
144
|
-
required: question.required ?? true,
|
|
145
160
|
type: "text",
|
|
146
|
-
...(
|
|
161
|
+
...(recommendation ? { recommendation } : {}),
|
|
147
162
|
...(placeholder ? { placeholder } : {}),
|
|
148
163
|
};
|
|
149
164
|
}
|
|
@@ -185,21 +200,20 @@ function normalizeOptions(
|
|
|
185
200
|
});
|
|
186
201
|
}
|
|
187
202
|
|
|
188
|
-
function
|
|
203
|
+
function validateRecommendationShape(
|
|
189
204
|
questionId: string,
|
|
190
205
|
value: string | string[] | undefined,
|
|
191
206
|
multi: boolean,
|
|
192
|
-
kind: "recommendation" | "initial",
|
|
193
207
|
): void {
|
|
194
208
|
if (value === undefined) return;
|
|
195
209
|
if (multi && !Array.isArray(value)) {
|
|
196
210
|
throw new AskUserValidationError(
|
|
197
|
-
`multi-select question "${questionId}"
|
|
211
|
+
`multi-select question "${questionId}" recommendation must be an array, not a string.`,
|
|
198
212
|
);
|
|
199
213
|
}
|
|
200
214
|
if (!multi && Array.isArray(value)) {
|
|
201
215
|
throw new AskUserValidationError(
|
|
202
|
-
`single-select question "${questionId}"
|
|
216
|
+
`single-select question "${questionId}" recommendation must be a string, not an array.`,
|
|
203
217
|
);
|
|
204
218
|
}
|
|
205
219
|
}
|
|
@@ -209,24 +223,27 @@ function resolveIndexes(args: {
|
|
|
209
223
|
options: NormalizedOption[];
|
|
210
224
|
value: string | string[] | undefined;
|
|
211
225
|
multi: boolean;
|
|
212
|
-
|
|
226
|
+
defaultToFirst: boolean;
|
|
213
227
|
}): number[] {
|
|
214
|
-
const { questionId, options, value, multi,
|
|
215
|
-
if (value === undefined)
|
|
228
|
+
const { questionId, options, value, multi, defaultToFirst } = args;
|
|
229
|
+
if (value === undefined) {
|
|
230
|
+
return defaultToFirst ? [0] : [];
|
|
231
|
+
}
|
|
232
|
+
|
|
216
233
|
const values = multi ? (value as string[]) : [value as string];
|
|
217
234
|
const seen = new Set<string>();
|
|
218
235
|
return values.map((entry: string) => {
|
|
219
236
|
const trimmed = entry.trim();
|
|
220
237
|
if (seen.has(trimmed)) {
|
|
221
238
|
throw new AskUserValidationError(
|
|
222
|
-
`choice question "${questionId}" has duplicate
|
|
239
|
+
`choice question "${questionId}" has duplicate recommendation value "${trimmed}".`,
|
|
223
240
|
);
|
|
224
241
|
}
|
|
225
242
|
seen.add(trimmed);
|
|
226
243
|
const index = options.findIndex((option) => option.value === trimmed);
|
|
227
244
|
if (index < 0) {
|
|
228
245
|
throw new AskUserValidationError(
|
|
229
|
-
`choice question "${questionId}"
|
|
246
|
+
`choice question "${questionId}" recommendation value "${trimmed}" does not match any option value.`,
|
|
230
247
|
);
|
|
231
248
|
}
|
|
232
249
|
return index;
|
package/src/render/result.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type {
|
|
3
|
-
Answer,
|
|
4
3
|
AskUserDetails,
|
|
5
4
|
AskUserErrorDetails,
|
|
6
5
|
AskUserOutcome,
|
|
6
|
+
AskUserResponse,
|
|
7
7
|
AskUserToolDetails,
|
|
8
|
+
ChoiceQuestionResponse,
|
|
8
9
|
NormalizedQuestion,
|
|
9
10
|
NormalizedQuestionnaire,
|
|
10
11
|
} from "../types.ts";
|
|
@@ -19,10 +20,9 @@ export function buildResult(
|
|
|
19
20
|
...(questionnaire.title ? { title: questionnaire.title } : {}),
|
|
20
21
|
...(questionnaire.intro ? { intro: questionnaire.intro } : {}),
|
|
21
22
|
questions: questionnaire.questions,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
...(outcome.discussMessage ? { discussMessage: outcome.discussMessage } : {}),
|
|
23
|
+
outcome: outcome.outcome,
|
|
24
|
+
comment: outcome.comment,
|
|
25
|
+
responses: outcome.responses,
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
return {
|
|
@@ -39,64 +39,87 @@ export function buildErrorResult(message: string): AskUserToolResult {
|
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
function summarizeOutcome(questions: NormalizedQuestion[], outcome: AskUserOutcome): string {
|
|
43
|
+
const responseLines = outcome.responses.flatMap((response) =>
|
|
44
|
+
formatResponseSummaryLines(questions, response),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const headerLines =
|
|
48
|
+
outcome.outcome === "submitted"
|
|
49
|
+
? []
|
|
50
|
+
: [
|
|
51
|
+
"User needs discussion before a complete decision.",
|
|
52
|
+
...formatUnansweredSummary(questions, outcome.responses),
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
const commentLines = outcome.comment ? [`Form comment: ${outcome.comment}`] : [];
|
|
56
|
+
const lines = [...headerLines, ...commentLines, ...responseLines];
|
|
57
|
+
|
|
58
|
+
if (lines.length > 0) return lines.join("\n");
|
|
59
|
+
return outcome.outcome === "submitted"
|
|
60
|
+
? "User submitted the form."
|
|
61
|
+
: "User needs discussion before a complete decision.";
|
|
51
62
|
}
|
|
52
63
|
|
|
53
|
-
function
|
|
54
|
-
|
|
64
|
+
function formatUnansweredSummary(
|
|
65
|
+
questions: NormalizedQuestion[],
|
|
66
|
+
responses: AskUserResponse[],
|
|
67
|
+
): string[] {
|
|
68
|
+
const unanswered = responses
|
|
69
|
+
.filter((response) => !response.answer.answered)
|
|
70
|
+
.map((response) => questionHeader(questions, response.questionId));
|
|
71
|
+
|
|
72
|
+
return unanswered.length > 0 ? [`Unanswered: ${unanswered.join(", ")}`] : [];
|
|
55
73
|
}
|
|
56
74
|
|
|
57
|
-
function
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
function formatResponseSummaryLines(
|
|
76
|
+
questions: NormalizedQuestion[],
|
|
77
|
+
response: AskUserResponse,
|
|
78
|
+
): string[] {
|
|
79
|
+
const header = questionHeader(questions, response.questionId);
|
|
80
|
+
const answerLine = formatAnswerSummaryLine(header, response);
|
|
81
|
+
const lines = answerLine ? [answerLine] : [];
|
|
60
82
|
|
|
61
|
-
|
|
62
|
-
|
|
83
|
+
if (response.questionComment) {
|
|
84
|
+
lines.push(`${header} question comment: ${response.questionComment}`);
|
|
85
|
+
}
|
|
63
86
|
|
|
64
|
-
if (
|
|
65
|
-
|
|
87
|
+
if (response.answer.kind === "choice") {
|
|
88
|
+
lines.push(...formatUnselectedOptionCommentLines(header, response as ChoiceQuestionResponse));
|
|
66
89
|
}
|
|
67
90
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
91
|
+
return lines;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function formatAnswerSummaryLine(header: string, response: AskUserResponse): string | undefined {
|
|
95
|
+
if (!response.answer.answered) return undefined;
|
|
96
|
+
|
|
97
|
+
if (response.answer.kind === "choice") {
|
|
98
|
+
const selected = response.answer.options
|
|
99
|
+
.filter((option) => option.selected)
|
|
100
|
+
.map((option) =>
|
|
101
|
+
option.comment ? `${option.label} (comment: ${option.comment})` : option.label,
|
|
102
|
+
);
|
|
103
|
+
return selected.length > 0 ? `${header}: ${selected.join("; ")}` : undefined;
|
|
74
104
|
}
|
|
75
105
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
].join("\n");
|
|
106
|
+
if (response.answer.kind === "text" && response.answer.value) {
|
|
107
|
+
return `${header}: ${response.answer.value}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return undefined;
|
|
82
111
|
}
|
|
83
112
|
|
|
84
|
-
function
|
|
85
|
-
|
|
86
|
-
|
|
113
|
+
function formatUnselectedOptionCommentLines(
|
|
114
|
+
header: string,
|
|
115
|
+
response: ChoiceQuestionResponse,
|
|
87
116
|
): string[] {
|
|
88
|
-
return
|
|
89
|
-
|
|
90
|
-
return
|
|
117
|
+
return response.answer.options.flatMap((option) => {
|
|
118
|
+
if (option.selected || !option.comment) return [];
|
|
119
|
+
return [`${header} option comment (${option.label}): ${option.comment}`];
|
|
91
120
|
});
|
|
92
121
|
}
|
|
93
122
|
|
|
94
|
-
|
|
95
|
-
questions
|
|
96
|
-
missingQuestionIds: string[],
|
|
97
|
-
): string | undefined {
|
|
98
|
-
const headers = questions
|
|
99
|
-
.filter((question) => missingQuestionIds.includes(question.id))
|
|
100
|
-
.map((question) => question.header);
|
|
101
|
-
return headers.length > 0 ? headers.join(", ") : undefined;
|
|
123
|
+
function questionHeader(questions: NormalizedQuestion[], questionId: string): string {
|
|
124
|
+
return questions.find((question) => question.id === questionId)?.header ?? questionId;
|
|
102
125
|
}
|