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

pendingUserInput.ts

110 lines4.1 KB

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