Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pendingUserInput.ts

120 lines4.3 KB

1import type {
2 ReviewCommentContextRecord,
3 ThreadContextRecord,
4 UserInputQuestion,
5} from "@t3tools/contracts";
6import {
7 projectComposerContextForProvider,
8 replaceComposerContextReferences,
9} from "@t3tools/shared/composerContextReferences";
10import { formatThreadLink } from "@t3tools/shared/threadLinks";
11import {
12 buildPendingUserInputAnswers,
13 normalizeSelectedOptionValues,
14 resolvePendingUserInputAnswer,
15 type PendingUserInputAnswer,
16 type PendingUserInputDraftAnswer,
17} from "@t3tools/client-runtime/state/pending-user-input";
18​
19import { collectInlineContextIds } from "./lib/composerContextReferences";
20​
21export function projectPendingUserInputAnswer(
22 answer: unknown,
23 records: ReadonlyArray<ThreadContextRecord | ReviewCommentContextRecord>,
24) {
25 if (typeof answer !== "string" || records.length === 0) return answer;
26 const recordsById = new Map(records.map((record) => [record.contextId, record]));
27 return replaceComposerContextReferences(answer, (reference) => {
28 const record = recordsById.get(reference.contextId);
29 if (record?.kind !== reference.kind) return reference.source;
30 return record.kind === "thread"
31 ? formatThreadLink(record, reference.label)
32 : projectComposerContextForProvider({ text: reference.source, records: [record] });
33 });
35​
36// Question drafts retain deleted chip records for undo; only transfer referenced records.
37export function threadContextsReferencedByAnswer(
38 answer: string,
39 threadContexts: ReadonlyArray<ThreadContextRecord>,
40) {
41 const referencedIds = new Set(collectInlineContextIds(answer));
42 return threadContexts.filter((record) => referencedIds.has(record.contextId));
44​
45export interface PendingUserInputProgress<Q extends UserInputQuestion = UserInputQuestion> {
46 questionIndex: number;
47 activeQuestion: Q | null;
48 activeDraft: PendingUserInputDraftAnswer | undefined;
49 selectedOptionValues: ReadonlyArray<string>;
50 customAnswer: string;
51 resolvedAnswer: PendingUserInputAnswer | null;
52 usingCustomAnswer: boolean;
53 answeredQuestionCount: number;
54 isLastQuestion: boolean;
55 isComplete: boolean;
56 canAdvance: boolean;
58​
59const DISPLACED_ANSWER_SEPARATOR = "\n\n";
60​
61export function carryDisplacedCustomAnswerIntoPrompt(
62 prompt: string,
63 customAnswer: string | undefined,
64): string {
65 const displaced = customAnswer?.trim() ?? "";
66 if (displaced.length === 0) {
67 return prompt;
68 }
69 if (prompt.trim().length === 0) {
70 return displaced;
71 }
72 return `${prompt.trimEnd()}${DISPLACED_ANSWER_SEPARATOR}${displaced}`;
74​
75function countAnsweredPendingUserInputQuestions(
76 questions: ReadonlyArray<UserInputQuestion>,
77 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
78): number {
79 return questions.reduce((count, question) => {
80 return resolvePendingUserInputAnswer(question, draftAnswers[question.id]) !== null
81 ? count + 1
82 : count;
83 }, 0);
85​
86export function derivePendingUserInputProgress<Q extends UserInputQuestion>(
87 questions: ReadonlyArray<Q>,
88 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
89 questionIndex: number,
90): PendingUserInputProgress<Q> {
91 const normalizedQuestionIndex =
92 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
93 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
94 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
95 const resolvedAnswer = activeQuestion
96 ? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
97 : null;
98 const customAnswer =
99 activeQuestion?.allowCustomAnswer === false ? "" : (activeDraft?.customAnswer ?? "");
100 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
101 const isLastQuestion =
102 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
103​
104 return {
105 questionIndex: normalizedQuestionIndex,
106 activeQuestion,
107 activeDraft,
108 selectedOptionValues: activeQuestion
109 ? normalizeSelectedOptionValues(activeQuestion, activeDraft?.selectedOptionValues)
110 : [],
111 customAnswer,
112 resolvedAnswer,
113 usingCustomAnswer:
114 (activeQuestion?.initialAnswer !== undefined ? customAnswer : customAnswer.trim()).length > 0,
115 answeredQuestionCount,
116 isLastQuestion,
117 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
118 canAdvance: resolvedAnswer !== null,
119 };