The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type UserInputAttachmentAnswerPayload } from "@t3tools/contracts"; |
| 2 | import { composerContextProviderTextForDisplay } from "@t3tools/shared/composerContextReferences"; |
| 3 | |
| 4 | function record(value: unknown): Record<string, unknown> | undefined { |
| 5 | return value !== null && typeof value === "object" && !Array.isArray(value) |
| 6 | ? (value as Record<string, unknown>) |
| 7 | : undefined; |
| 8 | } |
| 9 | |
| 10 | export function getQuestionAnswerText(value: unknown): string { |
| 11 | // Answers holding context chips are stored in provider form. |
| 12 | if (typeof value === "string") return composerContextProviderTextForDisplay(value); |
| 13 | if (Array.isArray(value)) return value.map(getQuestionAnswerText).filter(Boolean).join(", "); |
| 14 | const nested = record(value); |
| 15 | return nested ? getQuestionAnswerText(nested.answers) : ""; |
| 16 | } |
| 17 | |
| 18 | export function getQuestionTextPreview(answer: UserInputAttachmentAnswerPayload): string { |
| 19 | return Object.values(answer.questionTextById ?? {}) |
| 20 | .map((text) => text.replace(/\s+/g, " ").trim()) |
| 21 | .filter(Boolean) |
| 22 | .join(" · "); |
| 23 | } |
| 24 | |
| 25 | export function getQuestionAnswerPreview(answer: UserInputAttachmentAnswerPayload): string { |
| 26 | const answers = Object.values(answer.answers).map(getQuestionAnswerText).filter(Boolean); |
| 27 | const attachments = Object.values(answer.attachmentsByQuestionId) |
| 28 | .flat() |
| 29 | .map((attachment) => attachment.name); |
| 30 | return ( |
| 31 | answers.length > 0 |
| 32 | ? answers.join(" · ") |
| 33 | : attachments.length > 0 |
| 34 | ? attachments.join(", ") |
| 35 | : Object.values(answer.questionTextById ?? {}).join(" · ") |
| 36 | ) |
| 37 | .replace(/\s+/g, " ") |
| 38 | .trim(); |
| 39 | } |
| 40 | |
| 41 | export function hasQuestionAnswer(answer: UserInputAttachmentAnswerPayload): boolean { |
| 42 | return ( |
| 43 | Object.values(answer.answers).some(getQuestionAnswerText) || |
| 44 | Object.values(answer.attachmentsByQuestionId).some((attachments) => attachments.length > 0) |
| 45 | ); |
| 46 | } |