+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
203 lines
6.5 KiB
TypeScript
203 lines
6.5 KiB
TypeScript
import type { UserInputQuestion } from "@t3tools/contracts";
|
|
|
|
export interface PendingUserInputDraftAnswer {
|
|
selectedOptionValues?: string[];
|
|
customAnswer?: string;
|
|
attachmentCount?: number;
|
|
attachmentsBlocked?: boolean;
|
|
}
|
|
|
|
export interface PendingUserInputProgress {
|
|
questionIndex: number;
|
|
activeQuestion: UserInputQuestion | null;
|
|
activeDraft: PendingUserInputDraftAnswer | undefined;
|
|
selectedOptionValues: string[];
|
|
customAnswer: string;
|
|
resolvedAnswer: string | string[] | null;
|
|
usingCustomAnswer: boolean;
|
|
answeredQuestionCount: number;
|
|
isLastQuestion: boolean;
|
|
isComplete: boolean;
|
|
canAdvance: boolean;
|
|
}
|
|
|
|
function normalizeDraftAnswer(value: string | undefined): string | null {
|
|
if (typeof value !== "string") {
|
|
return null;
|
|
}
|
|
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
|
|
function normalizeSelectedOptionValues(value: string[] | undefined): string[] {
|
|
if (!Array.isArray(value)) {
|
|
return [];
|
|
}
|
|
|
|
// Provider option IDs must stay unchanged, including whitespace.
|
|
return Array.from(new Set(value.filter((entry) => typeof entry === "string")));
|
|
}
|
|
|
|
export function resolvePendingUserInputAnswer(
|
|
question: UserInputQuestion,
|
|
draft: PendingUserInputDraftAnswer | undefined,
|
|
): string | string[] | null {
|
|
if (draft?.attachmentsBlocked) return null;
|
|
const customAnswer =
|
|
question.allowCustomAnswer === false ? null : normalizeDraftAnswer(draft?.customAnswer);
|
|
if (customAnswer) {
|
|
return customAnswer;
|
|
}
|
|
|
|
const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues).filter(
|
|
(value) => question.options.some((option) => (option.value ?? option.label) === value),
|
|
);
|
|
if (question.multiSelect) {
|
|
return selectedOptionValues.length > 0
|
|
? selectedOptionValues
|
|
: question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0
|
|
? ""
|
|
: null;
|
|
}
|
|
|
|
return (
|
|
selectedOptionValues[0] ??
|
|
(question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null)
|
|
);
|
|
}
|
|
|
|
export function setPendingUserInputCustomAnswer(
|
|
draft: PendingUserInputDraftAnswer | undefined,
|
|
customAnswer: string,
|
|
): PendingUserInputDraftAnswer {
|
|
const selectedOptionValues =
|
|
customAnswer.trim().length > 0
|
|
? undefined
|
|
: normalizeSelectedOptionValues(draft?.selectedOptionValues);
|
|
|
|
return {
|
|
customAnswer,
|
|
...(selectedOptionValues && selectedOptionValues.length > 0 ? { selectedOptionValues } : {}),
|
|
};
|
|
}
|
|
|
|
const DISPLACED_ANSWER_SEPARATOR = "\n\n";
|
|
|
|
/**
|
|
* Selecting an option replaces the custom answer, because a non-empty custom
|
|
* answer outranks selected options in `resolvePendingUserInputAnswer`. Text the
|
|
* user typed into the answer field must not vanish on that click: it moves back
|
|
* into the thread draft, after whatever was already waiting there.
|
|
*/
|
|
export function carryDisplacedCustomAnswerIntoPrompt(
|
|
prompt: string,
|
|
customAnswer: string | undefined,
|
|
): string {
|
|
const displaced = customAnswer?.trim() ?? "";
|
|
if (displaced.length === 0) {
|
|
return prompt;
|
|
}
|
|
if (prompt.trim().length === 0) {
|
|
return displaced;
|
|
}
|
|
return `${prompt.trimEnd()}${DISPLACED_ANSWER_SEPARATOR}${displaced}`;
|
|
}
|
|
|
|
export function togglePendingUserInputOptionSelection(
|
|
question: UserInputQuestion,
|
|
draft: PendingUserInputDraftAnswer | undefined,
|
|
optionValue: string,
|
|
): PendingUserInputDraftAnswer {
|
|
if (question.multiSelect) {
|
|
const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues);
|
|
const nextSelectedOptionValues = selectedOptionValues.includes(optionValue)
|
|
? selectedOptionValues.filter((value) => value !== optionValue)
|
|
: [...selectedOptionValues, optionValue];
|
|
|
|
return {
|
|
customAnswer: "",
|
|
...(nextSelectedOptionValues.length > 0
|
|
? { selectedOptionValues: nextSelectedOptionValues }
|
|
: {}),
|
|
};
|
|
}
|
|
|
|
return {
|
|
customAnswer: "",
|
|
selectedOptionValues: [optionValue],
|
|
};
|
|
}
|
|
|
|
export function buildPendingUserInputAnswers(
|
|
questions: ReadonlyArray<UserInputQuestion>,
|
|
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
|
|
): Record<string, string | string[]> | null {
|
|
const answers: Record<string, string | string[]> = {};
|
|
|
|
for (const question of questions) {
|
|
const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
|
|
if (answer === null) {
|
|
return null;
|
|
}
|
|
answers[question.id] = answer;
|
|
}
|
|
|
|
return answers;
|
|
}
|
|
|
|
export function countAnsweredPendingUserInputQuestions(
|
|
questions: ReadonlyArray<UserInputQuestion>,
|
|
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
|
|
): number {
|
|
return questions.reduce((count, question) => {
|
|
return resolvePendingUserInputAnswer(question, draftAnswers[question.id]) !== null
|
|
? count + 1
|
|
: count;
|
|
}, 0);
|
|
}
|
|
|
|
export function findFirstUnansweredPendingUserInputQuestionIndex(
|
|
questions: ReadonlyArray<UserInputQuestion>,
|
|
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
|
|
): number {
|
|
const unansweredIndex = questions.findIndex(
|
|
(question) => !resolvePendingUserInputAnswer(question, draftAnswers[question.id]),
|
|
);
|
|
|
|
return unansweredIndex === -1 ? Math.max(questions.length - 1, 0) : unansweredIndex;
|
|
}
|
|
|
|
export function derivePendingUserInputProgress(
|
|
questions: ReadonlyArray<UserInputQuestion>,
|
|
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
|
|
questionIndex: number,
|
|
): PendingUserInputProgress {
|
|
const normalizedQuestionIndex =
|
|
questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
|
|
const activeQuestion = questions[normalizedQuestionIndex] ?? null;
|
|
const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
|
|
const resolvedAnswer = activeQuestion
|
|
? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
|
|
: null;
|
|
const customAnswer =
|
|
activeQuestion?.allowCustomAnswer === false ? "" : (activeDraft?.customAnswer ?? "");
|
|
const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
|
|
const isLastQuestion =
|
|
questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
|
|
|
|
return {
|
|
questionIndex: normalizedQuestionIndex,
|
|
activeQuestion,
|
|
activeDraft,
|
|
selectedOptionValues: normalizeSelectedOptionValues(activeDraft?.selectedOptionValues),
|
|
customAnswer,
|
|
resolvedAnswer,
|
|
usingCustomAnswer: customAnswer.trim().length > 0,
|
|
answeredQuestionCount,
|
|
isLastQuestion,
|
|
isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
|
|
canAdvance: resolvedAnswer !== null,
|
|
};
|
|
}
|