titleInput returns the first user message text and whether title generation should proceed. It returns false when the chat already has assistant/tool replies, has more than one visible user message, or the current title doesn't look like a candidate for replacement.
( chat database.Chat, messages []database.ChatMessage, )
| 526 | // has assistant/tool replies, has more than one visible user message, |
| 527 | // or the current title doesn't look like a candidate for replacement. |
| 528 | func titleInput( |
| 529 | chat database.Chat, |
| 530 | messages []database.ChatMessage, |
| 531 | ) (string, bool) { |
| 532 | userCount := 0 |
| 533 | firstUserText := "" |
| 534 | |
| 535 | for _, message := range messages { |
| 536 | if message.Visibility == database.ChatMessageVisibilityModel { |
| 537 | continue |
| 538 | } |
| 539 | |
| 540 | switch message.Role { |
| 541 | case database.ChatMessageRoleAssistant, database.ChatMessageRoleTool: |
| 542 | return "", false |
| 543 | case database.ChatMessageRoleUser: |
| 544 | userCount++ |
| 545 | if firstUserText == "" { |
| 546 | parsed, err := chatprompt.ParseContent(message) |
| 547 | if err != nil { |
| 548 | return "", false |
| 549 | } |
| 550 | firstUserText = strings.TrimSpace( |
| 551 | contentBlocksToText(parsed), |
| 552 | ) |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if userCount != 1 || firstUserText == "" { |
| 558 | return "", false |
| 559 | } |
| 560 | |
| 561 | currentTitle := strings.TrimSpace(chat.Title) |
| 562 | if currentTitle == "" { |
| 563 | return firstUserText, true |
| 564 | } |
| 565 | |
| 566 | if currentTitle != fallbackChatTitle(firstUserText) { |
| 567 | return "", false |
| 568 | } |
| 569 | |
| 570 | return firstUserText, true |
| 571 | } |
| 572 | |
| 573 | func normalizeTitleOutput(title string) string { |
| 574 | title = normalizeShortTextOutput(title) |
no test coverage detected