Fallback chat titles (used when AI title generation doesn't run or hasn't finished) cut the first user message to six words and added "…". Clients then showed titles cut at word boundaries no matter how much space they had. Fallback titles now keep the whole message with whitespace collapsed, and only add an ellipsis at the existing 80-rune storage cap. Clients truncate to fit their layout. - Subagent fallback titles reuse `chatprompt.FallbackTitle` instead of a duplicate six-word helper that used "...". - Auto-titling is unaffected: since #29486 it checks `title_source`, not whether the title text matches the fallback, so no compatibility shim for older six-word titles is needed. - Titles already stored in the old format are not rewritten. Follow-up to #29942, where shared chats in the sidebar showed these pre-truncated titles. Testing: `make pre-commit`; `go test ./coderd/x/chatd/...`; `TestPostChats*` and `TestChatMessageWithFiles` in `coderd`. --- _Generated by Coder Agents on behalf of @tracyjohnsonux._
116 lines
3.9 KiB
Go
116 lines
3.9 KiB
Go
package chatprompt
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
stringutil "github.com/coder/coder/v2/coderd/util/strings"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// syntheticPasteTitleBudget caps, in runes, how much of a pasted-text
|
|
// attachment feeds title generation. It is far smaller than
|
|
// syntheticPasteInlineBudget because it only seeds a short title, not
|
|
// the model prompt.
|
|
const syntheticPasteTitleBudget = 16 * 1024
|
|
|
|
// TitlePasteBytePrefix caps, in bytes, how much of a pasted-text blob
|
|
// feeds title derivation: four bytes per rune (the UTF-8 maximum)
|
|
// covers syntheticPasteTitleBudget runes. Database callers pass it to
|
|
// GetChatFileDataPrefixesByIDs to bound the fetch itself.
|
|
const TitlePasteBytePrefix = 4 * syntheticPasteTitleBudget
|
|
|
|
// TitlePasteText converts pasted-text content to TitleText input,
|
|
// copying at most TitlePasteBytePrefix bytes. Every caller that
|
|
// builds a pasteText map must use it so all derivation paths feed
|
|
// TitleText identical strings.
|
|
func TitlePasteText(data []byte) string {
|
|
return string(data[:min(len(data), TitlePasteBytePrefix)])
|
|
}
|
|
|
|
// TitleText derives title-generation input from message parts. Text,
|
|
// file-reference, workspace-file-reference, and skill-invocation parts
|
|
// are joined in part order. When they yield nothing, the content of synthetic
|
|
// pasted-text attachments is used instead, looked up in pasteText by
|
|
// file ID and truncated to syntheticPasteTitleBudget runes per file.
|
|
func TitleText(parts []codersdk.ChatMessagePart, pasteText map[uuid.UUID]string) string {
|
|
texts := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
switch part.Type {
|
|
case codersdk.ChatMessagePartTypeText:
|
|
text := strings.TrimSpace(part.Text)
|
|
if text == "" {
|
|
continue
|
|
}
|
|
texts = append(texts, text)
|
|
case codersdk.ChatMessagePartTypeFileReference:
|
|
texts = append(texts, fileReferencePartToText(part))
|
|
case codersdk.ChatMessagePartTypeWorkspaceFileReference:
|
|
// The file name alone: this text can become the visible
|
|
// fallback title, so it must not carry prompt markers.
|
|
name := strings.TrimSpace(part.WorkspaceFileName)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
texts = append(texts, name)
|
|
case codersdk.ChatMessagePartTypeSkillInvocation:
|
|
texts = append(texts, "/"+SkillInvocationAlias(part))
|
|
}
|
|
}
|
|
if joined := strings.TrimSpace(strings.Join(texts, " ")); joined != "" {
|
|
return joined
|
|
}
|
|
|
|
pastes := make([]string, 0, len(pasteText))
|
|
for _, part := range parts {
|
|
if part.Type != codersdk.ChatMessagePartTypeFile || !part.FileID.Valid {
|
|
continue
|
|
}
|
|
content := strings.TrimSpace(pasteText[part.FileID.UUID])
|
|
if content == "" {
|
|
continue
|
|
}
|
|
pastes = append(pastes, stringutil.Truncate(content, syntheticPasteTitleBudget))
|
|
}
|
|
return strings.TrimSpace(strings.Join(pastes, "\n\n"))
|
|
}
|
|
|
|
// SyntheticPasteFileIDs returns the file IDs of file parts that are
|
|
// synthetic pasted-text attachments created by the chat UI. Callers
|
|
// resolve these to file content and pass the result to TitleText.
|
|
func SyntheticPasteFileIDs(parts []codersdk.ChatMessagePart) []uuid.UUID {
|
|
var ids []uuid.UUID
|
|
for _, part := range parts {
|
|
if part.Type != codersdk.ChatMessagePartTypeFile || !part.FileID.Valid {
|
|
continue
|
|
}
|
|
if !IsSyntheticPaste(part.Name, part.MediaType) {
|
|
continue
|
|
}
|
|
ids = append(ids, part.FileID.UUID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// DefaultChatTitle is the placeholder title for chats created without
|
|
// any text to derive a title from (for example chats created with no
|
|
// initial user message). It is always considered replaceable by
|
|
// automatic title generation.
|
|
const DefaultChatTitle = "New Chat"
|
|
|
|
// FallbackTitle returns title text with whitespace collapsed, ellipsized
|
|
// past 80 runes. Empty input yields DefaultChatTitle.
|
|
func FallbackTitle(message string) string {
|
|
const maxRunes = 80
|
|
|
|
title := strings.Join(strings.Fields(message), " ")
|
|
if title == "" {
|
|
return DefaultChatTitle
|
|
}
|
|
if len([]rune(title)) > maxRunes {
|
|
return stringutil.Truncate(title, maxRunes-1) + "…"
|
|
}
|
|
return title
|
|
}
|