@mrclrchtr/supi-ask-user 1.12.1 → 1.14.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.
@@ -1,9 +1,14 @@
1
1
  import { type AgentToolResult, keyText, type Theme } from "@earendil-works/pi-coding-agent";
2
2
  import { Text } from "@earendil-works/pi-tui";
3
3
  import type { AskUserParams } from "../schema.ts";
4
- import type { AskUserDetails, AskUserStatus, AskUserToolDetails } from "../types.ts";
4
+ import type {
5
+ AskUserDetails,
6
+ AskUserOutcomeKind,
7
+ AskUserResponse,
8
+ AskUserToolDetails,
9
+ NormalizedQuestion,
10
+ } from "../types.ts";
5
11
  import { isErrorDetails } from "../types.ts";
6
- import { formatAnswerSummary, formatMissingHeaders } from "./result.ts";
7
12
 
8
13
  const COLLAPSED_ANSWER_LIMIT = 2;
9
14
  const DEFAULT_REVIEW_KEY = "Ctrl+O";
@@ -37,9 +42,6 @@ export function renderAskUserResult(
37
42
  }
38
43
 
39
44
  function buildCollapsedResultLines(details: AskUserDetails, theme: Theme): string[] {
40
- if (details.status === "cancelled") return [theme.fg("warning", "Cancelled")];
41
- if (details.status === "aborted") return [theme.fg("error", "Aborted")];
42
-
43
45
  const answerLines = buildAnswerLines(details, theme);
44
46
  const shownAnswerLines = answerLines.slice(0, COLLAPSED_ANSWER_LIMIT);
45
47
  const hiddenAnswerCount = Math.max(answerLines.length - shownAnswerLines.length, 0);
@@ -49,34 +51,36 @@ function buildCollapsedResultLines(details: AskUserDetails, theme: Theme): strin
49
51
  }
50
52
 
51
53
  function buildExpandedResultLines(details: AskUserDetails, theme: Theme): string[] {
52
- if (details.status === "cancelled") return [theme.fg("warning", "Cancelled")];
53
- if (details.status === "aborted") return [theme.fg("error", "Aborted")];
54
-
55
54
  const lines = [formatStatusLine(details, theme)];
56
55
  const title = details.title?.trim();
57
56
  const intro = details.intro?.trim();
58
- const discussMessage = details.discussMessage?.trim();
59
- const missing = formatMissingSummary(details);
57
+ const comment = details.comment?.trim();
58
+ const unansweredCount = details.responses.filter((r) => !r.answer.answered).length;
60
59
 
61
60
  if (title) lines.push(theme.fg("accent", title));
62
61
  if (intro) lines.push(theme.fg("text", intro));
63
- if (details.status === "discuss" && discussMessage) {
64
- lines.push(theme.fg("text", `Message: ${discussMessage}`));
65
- }
66
- if (missing) {
67
- lines.push(theme.fg("dim", missing));
68
- }
62
+ if (comment) lines.push(theme.fg("text", `Comment: ${comment}`));
69
63
 
70
64
  for (const question of details.questions) {
71
65
  lines.push("");
72
66
  lines.push(theme.fg("accent", question.header));
73
67
  lines.push(theme.fg("dim", question.prompt));
74
68
 
75
- const answer = details.answersById[question.id];
69
+ const resp = details.responses.find((r) => r.questionId === question.id);
70
+ if (resp) {
71
+ lines.push(...formatResponseLines(resp, question, theme));
72
+ if (resp.questionComment) {
73
+ lines.push(theme.fg("dim", ` Question comment: ${resp.questionComment}`));
74
+ }
75
+ } else {
76
+ lines.push(theme.fg("dim", "Not answered"));
77
+ }
78
+ }
79
+
80
+ if (unansweredCount > 0 && details.outcome === "needs_discussion") {
81
+ lines.push("");
76
82
  lines.push(
77
- answer
78
- ? theme.fg("text", `Answer: ${formatAnswerSummary(question, answer)}`)
79
- : theme.fg("dim", "Not answered"),
83
+ theme.fg("dim", `${unansweredCount} question${unansweredCount > 1 ? "s" : ""} unanswered`),
80
84
  );
81
85
  }
82
86
 
@@ -84,28 +88,74 @@ function buildExpandedResultLines(details: AskUserDetails, theme: Theme): string
84
88
  }
85
89
 
86
90
  function buildAnswerLines(details: AskUserDetails, theme: Theme): string[] {
87
- return details.questions.flatMap((question) => {
88
- const answer = details.answersById[question.id];
89
- return answer
91
+ return details.responses.flatMap((resp) => {
92
+ if (!resp.answer.answered) return [];
93
+ const question = details.questions.find((q) => q.id === resp.questionId);
94
+ if (!question) return [];
95
+
96
+ const summary = formatAnswerLine(resp);
97
+ return summary
90
98
  ? [
91
- `${theme.fg("success", "✓ ")}${theme.fg("accent", question.header)}: ${theme.fg("text", formatAnswerSummary(question, answer))}`,
99
+ `${theme.fg("success", "\u2713 ")}${theme.fg("accent", question.header)}: ${theme.fg("text", summary)}`,
92
100
  ]
93
101
  : [];
94
102
  });
95
103
  }
96
104
 
105
+ function formatAnswerLine(resp: AskUserResponse): string | undefined {
106
+ if (!resp.answer.answered) return undefined;
107
+
108
+ if (resp.answer.kind === "choice") {
109
+ const selected = resp.answer.options.filter((o) => o.selected);
110
+ if (selected.length === 0) return undefined;
111
+ return selected
112
+ .map((o) => (o.comment ? `${o.label} (comment: ${o.comment})` : o.label))
113
+ .join("; ");
114
+ }
115
+
116
+ if (resp.answer.kind === "text" && resp.answer.value) {
117
+ return resp.answer.value;
118
+ }
119
+
120
+ return undefined;
121
+ }
122
+
123
+ function formatResponseLines(
124
+ resp: AskUserResponse,
125
+ question: NormalizedQuestion,
126
+ theme: Theme,
127
+ ): string[] {
128
+ if (resp.answer.kind === "choice") {
129
+ const multi = question.type === "choice" && question.multi;
130
+ const optionLines = resp.answer.options.map((opt) => {
131
+ const selected = opt.selected
132
+ ? theme.fg("success", multi ? "[x]" : "(*)")
133
+ : theme.fg("dim", multi ? "[ ]" : "( )");
134
+ const comment = opt.comment ? theme.fg("dim", ` (comment: ${opt.comment})`) : "";
135
+ return `${selected} ${opt.label}${comment}`;
136
+ });
137
+
138
+ if (resp.answer.answered) return optionLines;
139
+ return [theme.fg("dim", "Not answered"), ...optionLines];
140
+ }
141
+
142
+ if (resp.answer.kind === "text" && resp.answer.answered && resp.answer.value) {
143
+ return [theme.fg("text", resp.answer.value)];
144
+ }
145
+
146
+ return [theme.fg("dim", "Not answered")];
147
+ }
148
+
97
149
  function buildCollapsedMetaLine(
98
150
  details: AskUserDetails,
99
151
  hiddenAnswerCount: number,
100
152
  theme: Theme,
101
153
  ): string {
102
154
  const parts: string[] = [];
155
+ const unansweredCount = details.responses.filter((r) => !r.answer.answered).length;
103
156
 
104
- if (details.missingQuestionIds.length > 0) {
105
- parts.push(`${details.missingQuestionIds.length} required missing`);
106
- }
107
- if (details.status === "discuss" && details.discussMessage?.trim()) {
108
- parts.push("discussion message included");
157
+ if (unansweredCount > 0) {
158
+ parts.push(`${unansweredCount} unanswered`);
109
159
  }
110
160
  if (hiddenAnswerCount > 0) {
111
161
  parts.push(`${hiddenAnswerCount} more answer${hiddenAnswerCount === 1 ? "" : "s"}`);
@@ -119,50 +169,28 @@ function buildCollapsedMetaLine(
119
169
  }
120
170
 
121
171
  function formatStatusLine(details: AskUserDetails, theme: Theme): string {
122
- const answeredCount = countAnsweredQuestions(details);
123
- const totalCount = details.questions.length;
172
+ const answeredCount = details.responses.filter((r) => r.answer.answered).length;
173
+ const totalCount = details.responses.length;
124
174
  return theme.fg(
125
- statusColor(details.status),
126
- `${statusLabel(details.status)} · ${answeredCount}/${totalCount} answered`,
175
+ statusColor(details.outcome),
176
+ `${statusLabel(details.outcome)} \u00B7 ${answeredCount}/${totalCount} answered`,
127
177
  );
128
178
  }
129
179
 
130
- function countAnsweredQuestions(details: AskUserDetails): number {
131
- return details.questions.filter((question) => question.id in details.answersById).length;
132
- }
133
-
134
- function formatMissingSummary(details: AskUserDetails): string | undefined {
135
- const missing = formatMissingHeaders(details.questions, details.missingQuestionIds);
136
- if (!missing) return undefined;
137
- return details.status === "partial"
138
- ? `Missing required: ${missing}`
139
- : `Still missing: ${missing}`;
140
- }
141
-
142
- function statusColor(status: AskUserStatus): "success" | "warning" | "error" {
143
- switch (status) {
180
+ function statusColor(outcome: AskUserOutcomeKind): "success" | "warning" | "error" {
181
+ switch (outcome) {
144
182
  case "submitted":
145
183
  return "success";
146
- case "partial":
147
- case "discuss":
148
- case "cancelled":
184
+ case "needs_discussion":
149
185
  return "warning";
150
- case "aborted":
151
- return "error";
152
186
  }
153
187
  }
154
188
 
155
- function statusLabel(status: AskUserStatus): string {
156
- switch (status) {
189
+ function statusLabel(outcome: AskUserOutcomeKind): string {
190
+ switch (outcome) {
157
191
  case "submitted":
158
192
  return "Submitted";
159
- case "partial":
160
- return "Partial";
161
- case "discuss":
162
- return "Discuss";
163
- case "cancelled":
164
- return "Cancelled";
165
- case "aborted":
166
- return "Aborted";
193
+ case "needs_discussion":
194
+ return "Needs discussion";
167
195
  }
168
196
  }
package/src/schema.ts CHANGED
@@ -7,7 +7,7 @@ const OptionSchema = Type.Object({
7
7
  label: Type.String({ description: "Displayed label" }),
8
8
  description: Type.Optional(
9
9
  Type.String({
10
- description: "Optional note",
10
+ description: "Optional description",
11
11
  }),
12
12
  ),
13
13
  preview: Type.Optional(
@@ -17,6 +17,9 @@ const OptionSchema = Type.Object({
17
17
  ),
18
18
  });
19
19
 
20
+ const ChoiceOptionCount = { minItems: 2, maxItems: 12 } as const;
21
+ const QuestionCount = { minItems: 1, maxItems: 10 } as const;
22
+
20
23
  const ChoiceQuestionSchema = Type.Object({
21
24
  type: Type.Literal("choice"),
22
25
  id: Type.String({ description: "Question id" }),
@@ -24,34 +27,19 @@ const ChoiceQuestionSchema = Type.Object({
24
27
  prompt: Type.String({ description: "Question text" }),
25
28
  options: Type.Array(OptionSchema, {
26
29
  description: "Allowed options (2-12)",
30
+ ...ChoiceOptionCount,
27
31
  }),
28
- required: Type.Optional(
29
- Type.Boolean({
30
- default: true,
31
- description: "Required for full submit",
32
- }),
33
- ),
34
32
  multi: Type.Optional(
35
33
  Type.Boolean({
36
34
  default: false,
37
35
  description: "Allow multiple selections",
38
36
  }),
39
37
  ),
40
- allowOther: Type.Optional(
41
- Type.Boolean({
42
- description: "Allow a custom option; single-select only",
43
- }),
44
- ),
45
38
  recommendation: Type.Optional(
46
39
  Type.Union([Type.String(), Type.Array(Type.String())], {
47
40
  description: "Recommended value(s)",
48
41
  }),
49
42
  ),
50
- initial: Type.Optional(
51
- Type.Union([Type.String(), Type.Array(Type.String())], {
52
- description: "Initial value(s)",
53
- }),
54
- ),
55
43
  });
56
44
 
57
45
  const TextQuestionSchema = Type.Object({
@@ -59,13 +47,7 @@ const TextQuestionSchema = Type.Object({
59
47
  id: Type.String({ description: "Question id" }),
60
48
  header: Type.String({ description: "Short label" }),
61
49
  prompt: Type.String({ description: "Question text" }),
62
- required: Type.Optional(
63
- Type.Boolean({
64
- default: true,
65
- description: "Required for full submit",
66
- }),
67
- ),
68
- initial: Type.Optional(Type.String({ description: "Initial text" })),
50
+ recommendation: Type.Optional(Type.String({ description: "Suggested prefilled text" })),
69
51
  placeholder: Type.Optional(Type.String({ description: "Editor placeholder" })),
70
52
  });
71
53
 
@@ -79,13 +61,9 @@ export const AskUserParamsSchema = Type.Object({
79
61
  }),
80
62
  ),
81
63
  questions: Type.Array(QuestionSchema, {
82
- description: "1-4 focused questions for one decision",
64
+ description: "1-10 focused questions for one decision",
65
+ ...QuestionCount,
83
66
  }),
84
- allowPartialSubmit: Type.Optional(
85
- Type.Boolean({
86
- description: "Allow partial submission",
87
- }),
88
- ),
89
67
  });
90
68
 
91
69
  export type AskUserParams = Static<typeof AskUserParamsSchema>;