Files
KrisHD_AI/public/js/chatTitle.js
T
Frederik Spirgi 7723125c93
CI / check (22) (push) Failing after 15m53s
CI / check (24) (push) Failing after 15m50s
make some overall QoL improvements
2026-10-12 13:07:59 +02:00

18 lines
1.1 KiB
JavaScript

export function titleFromFirstMessage(message) {
const firstLine = String(message || "").split(/\r?\n/).find(line => line.trim()) || "";
let subject = firstLine.replace(/^\s*(?:[-*#]+\s*)?/, "").replace(/\s+/g, " ").trim();
subject = subject.replace(/^(?:can|could|would) you (?:please )?(?:help me )?/i, "")
.replace(/^(?:please )?(?:help me (?:with |understand )?|tell me about |explain |describe |summarize |compare |write about |create |make )/i, "")
.replace(/^what (?:is|are|was|were) (?:the |a |an )?/i, "")
.replace(/^how (?:do|does|can) (?:i|we) /i, "")
.replace(/^[\s:,-]+|[\s.!?,;:]+$/g, "");
const sentence = subject.split(/(?<=[.!?])\s+/)[0].replace(/[.!?]$/, "");
if (sentence) subject = sentence;
if (subject.length > 68) {
const words = subject.slice(0, 69).split(" ");
subject = words.length > 1 ? words.slice(0, -1).join(" ") : subject.slice(0, 68);
}
subject = subject.replace(/[\s,;:.-]+$/g, "");
return subject ? subject[0].toLocaleUpperCase() + subject.slice(1) : "New chat";
}