Open History to see the last commit
1import { truncate } from "@t3tools/shared/String";
2
3export interface ThreadTitleSeedInput {
4 readonly text: string;
5 readonly attachments: ReadonlyArray<{ readonly name: string }>;
6 readonly fallbackLabels?: ReadonlyArray<string | null | undefined>;
7}
8
9function normalizeTitleSeed(value: string): string {
10 return value.trim().replace(/\s+/gu, " ");
11}
12
13export function deriveThreadTitleSeed(input: ThreadTitleSeedInput): string {
14 const text = normalizeTitleSeed(input.text);
15 if (text.length > 0) {
16 return truncate(text);
17 }
18
19 const attachmentName = normalizeTitleSeed(input.attachments[0]?.name ?? "");
20 if (attachmentName.length > 0) {
21 return truncate(`Image: ${attachmentName}`);
22 }
23
24 for (const label of input.fallbackLabels ?? []) {
25 const normalized = normalizeTitleSeed(label ?? "");
26 if (normalized.length > 0) {
27 return truncate(normalized);
28 }
29 }
30
31 return "New thread";
32}