The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { mFallback, msg } from "gt-i18n"; |
| 2 | import type { MFunctionType } from "gt-i18n/types"; |
| 3 | import type { ChatAttachment } from "@t3tools/contracts"; |
| 4 | import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; |
| 5 | import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences"; |
| 6 | import { truncate } from "@t3tools/shared/String"; |
| 7 | |
| 8 | import { collectComposerModelMentions } from "../composerModelMentions.ts"; |
| 9 | |
| 10 | export interface ThreadTitleSeedInput { |
| 11 | readonly text: string; |
| 12 | readonly attachments: ReadonlyArray<Pick<ChatAttachment, "type" | "name">>; |
| 13 | readonly fallbackLabels?: ReadonlyArray<string | null | undefined>; |
| 14 | } |
| 15 | |
| 16 | function normalizeTitleSeed(value: string): string { |
| 17 | return assistantCitationsToPlainText(value).trim().replace(/\s+/gu, " "); |
| 18 | } |
| 19 | |
| 20 | function modelMentionsAsSlugs(value: string): string { |
| 21 | let result = ""; |
| 22 | let cursor = 0; |
| 23 | for (const mention of collectComposerModelMentions(value)) { |
| 24 | result += value.slice(cursor, mention.start) + mention.model; |
| 25 | cursor = mention.end; |
| 26 | } |
| 27 | return result + value.slice(cursor); |
| 28 | } |
| 29 | |
| 30 | function attachedImagesTitle(count: number, resolve: MFunctionType): string { |
| 31 | return count === 1 ? resolve(msg("Attached image")) : resolve(msg("Attached images")); |
| 32 | } |
| 33 | |
| 34 | export function threadTitleTextFromPrompt( |
| 35 | value: string, |
| 36 | resolve: MFunctionType = mFallback, |
| 37 | ): string { |
| 38 | const imageIds = new Set<string>(); |
| 39 | const prose = replaceComposerContextReferences(value, (reference) => { |
| 40 | if (reference.kind === "image") { |
| 41 | imageIds.add(reference.contextId); |
| 42 | return ""; |
| 43 | } |
| 44 | return reference.label; |
| 45 | }); |
| 46 | return ( |
| 47 | normalizeTitleSeed(modelMentionsAsSlugs(prose)) || |
| 48 | (imageIds.size > 0 ? attachedImagesTitle(imageIds.size, resolve) : "") |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | export function deriveThreadTitleSeed( |
| 53 | input: ThreadTitleSeedInput, |
| 54 | resolve: MFunctionType = mFallback, |
| 55 | ): string { |
| 56 | const text = threadTitleTextFromPrompt(input.text, resolve); |
| 57 | if (text.length > 0) { |
| 58 | return truncate(text); |
| 59 | } |
| 60 | |
| 61 | const images = input.attachments.filter((attachment) => attachment.type === "image"); |
| 62 | if (images.length > 0) { |
| 63 | return attachedImagesTitle(images.length, resolve); |
| 64 | } |
| 65 | |
| 66 | const attachmentName = normalizeTitleSeed(input.attachments[0]?.name ?? ""); |
| 67 | if (attachmentName.length > 0) { |
| 68 | return truncate(resolve(msg("File: {name}", { name: attachmentName }))); |
| 69 | } |
| 70 | |
| 71 | for (const label of input.fallbackLabels ?? []) { |
| 72 | const normalized = normalizeTitleSeed(label ?? ""); |
| 73 | if (normalized.length > 0) { |
| 74 | return truncate(normalized); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return resolve(msg("New thread")); |
| 79 | } |