@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 180 lines 5.83 KB
Raw
1 /** The question fields answer handling reads. Web and mobile question types both fit it. */
2 export interface PendingUserInputQuestion {
3 readonly id: string;
4 readonly options: ReadonlyArray<{ readonly label: string; readonly value?: string | undefined }>;
5 readonly multiSelect?: boolean | undefined;
6 readonly allowCustomAnswer?: boolean | undefined;
7 readonly initialAnswer?: string | undefined;
8 readonly required?: boolean | undefined;
9 }
10
11 export interface PendingUserInputDraftAnswer {
12 readonly selectedOptionValues?: ReadonlyArray<string>;
13 readonly customAnswer?: string;
14 readonly attachmentCount?: number;
15 readonly attachmentsBlocked?: boolean;
16 }
17
18 export type PendingUserInputAnswer = string | ReadonlyArray<string>;
19
20 function normalizeDraftAnswer(value: string | undefined): string | null {
21 if (typeof value !== "string") return null;
22 const trimmed = value.trim();
23 return trimmed.length > 0 ? trimmed : null;
24 }
25
26 // Provider option values stay exact, including whitespace. Only options without a value match by label.
27 function resolvePendingUserInputOptionValue(
28 question: PendingUserInputQuestion,
29 value: string,
30 ): string | null {
31 if (question.options.some((option) => option.value === value)) {
32 return value;
33 }
34
35 const label = value.trim();
36 return label.length > 0 &&
37 question.options.some((option) => option.value === undefined && option.label.trim() === label)
38 ? label
39 : null;
40 }
41
42 /** The draft's selections that name an offered option, deduplicated, in selection order. */
43 export function normalizeSelectedOptionValues(
44 question: PendingUserInputQuestion,
45 value: ReadonlyArray<string> | undefined,
46 ): ReadonlyArray<string> {
47 if (!Array.isArray(value)) {
48 return [];
49 }
50
51 return Array.from(
52 new Set(
53 value
54 .map((entry) => resolvePendingUserInputOptionValue(question, entry))
55 .filter((entry): entry is string => entry !== null),
56 ),
57 );
58 }
59
60 /** The answer to submit for one question, or null while it is unanswered. */
61 export function resolvePendingUserInputAnswer(
62 question: PendingUserInputQuestion,
63 draft: PendingUserInputDraftAnswer | undefined,
64 ): PendingUserInputAnswer | null {
65 if (draft?.attachmentsBlocked) return null;
66 const customAnswer =
67 question.allowCustomAnswer === false
68 ? null
69 : question.initialAnswer !== undefined
70 ? (draft?.customAnswer ?? null)
71 : normalizeDraftAnswer(draft?.customAnswer);
72 if (customAnswer !== null && customAnswer.length > 0) {
73 return customAnswer;
74 }
75 if (question.required === false && question.options.length === 0) {
76 return "";
77 }
78
79 const selectedOptionValues = normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
80 if (question.multiSelect) {
81 return selectedOptionValues.length > 0
82 ? selectedOptionValues
83 : (customAnswer ??
84 (question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null));
85 }
86 return (
87 selectedOptionValues[0] ??
88 customAnswer ??
89 (question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null)
90 );
91 }
92
93 export function setPendingUserInputCustomAnswer(
94 question: PendingUserInputQuestion,
95 draft: PendingUserInputDraftAnswer | undefined,
96 customAnswer: string,
97 ): PendingUserInputDraftAnswer {
98 if (question.allowCustomAnswer === false) {
99 return draft ?? {};
100 }
101
102 const selectedOptionValues =
103 question.initialAnswer !== undefined || customAnswer.trim().length > 0
104 ? undefined
105 : normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
106 return {
107 customAnswer,
108 ...(selectedOptionValues && selectedOptionValues.length > 0 ? { selectedOptionValues } : {}),
109 };
110 }
111
112 export function isPendingUserInputOptionSelected(
113 question: PendingUserInputQuestion,
114 draft: PendingUserInputDraftAnswer | undefined,
115 optionValue: string,
116 ): boolean {
117 if (question.allowCustomAnswer !== false && normalizeDraftAnswer(draft?.customAnswer)) {
118 return false;
119 }
120
121 const resolvedOptionValue = resolvePendingUserInputOptionValue(question, optionValue);
122 return (
123 resolvedOptionValue !== null &&
124 normalizeSelectedOptionValues(question, draft?.selectedOptionValues).includes(
125 resolvedOptionValue,
126 )
127 );
128 }
129
130 /** Toggles an offered option. An unknown option returns the same draft. */
131 export function togglePendingUserInputOptionSelection(
132 question: PendingUserInputQuestion,
133 draft: PendingUserInputDraftAnswer | undefined,
134 optionValue: string,
135 ): PendingUserInputDraftAnswer {
136 const resolvedOptionValue = resolvePendingUserInputOptionValue(question, optionValue);
137 if (resolvedOptionValue === null) {
138 return draft ?? {};
139 }
140
141 if (question.multiSelect) {
142 const selectedOptionValues = normalizeSelectedOptionValues(
143 question,
144 draft?.selectedOptionValues,
145 );
146 const nextSelectedOptionValues = selectedOptionValues.includes(resolvedOptionValue)
147 ? selectedOptionValues.filter((value) => value !== resolvedOptionValue)
148 : [...selectedOptionValues, resolvedOptionValue];
149
150 return {
151 customAnswer: "",
152 ...(nextSelectedOptionValues.length > 0
153 ? { selectedOptionValues: nextSelectedOptionValues }
154 : {}),
155 };
156 }
157
158 return {
159 customAnswer: "",
160 selectedOptionValues: [resolvedOptionValue],
161 };
162 }
163
164 /** Every question's answer keyed by question id, or null while any is unanswered. */
165 export function buildPendingUserInputAnswers(
166 questions: ReadonlyArray<PendingUserInputQuestion>,
167 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
168 ): Record<string, PendingUserInputAnswer> | null {
169 const entries: Array<[string, PendingUserInputAnswer]> = [];
170
171 for (const question of questions) {
172 const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
173 if (answer === null) {
174 return null;
175 }
176 entries.push([question.id, answer]);
177 }
178
179 return Object.fromEntries(entries);
180 }