Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(chattitle): strip multi-line fenced code before choosing a title (#11616)

T Mohan Aditya04890de79bdb
diff --git a/server/internal/chattitle/title.go b/server/internal/chattitle/title.go
index a05405ca11..eddd9c25d3 100644
--- a/server/internal/chattitle/title.go
+++ b/server/internal/chattitle/title.go
@@ -9,7 +9,8 @@ import (
const deterministicTitleLimit = 30
var (
- markdownFence = regexp.MustCompile("```.*?```")
+ fencePrefix = regexp.MustCompile("^(?:`{3,}|~{3,})")
+ inlineFence = regexp.MustCompile("```.*?```")
markdownMarks = regexp.MustCompile("[#*`>~_]")
markdownLink = regexp.MustCompile(`!?\[([^\]]*)\]\([^)]*\)`)
titleSpace = regexp.MustCompile(`[[:space:]]+`)
@@ -20,16 +21,40 @@ var (
// Derive is the single deterministic title policy for a Chat's first message,
// shared by first-party clients (which create sessions untitled) and channels:
-// first non-empty line that is not only mentions (or the first non-empty line
-// when every line is), Markdown stripped, whitespace collapsed, capped at 30
-// code points with a single ellipsis.
+// first non-empty line outside fenced code, preferring lines with text beyond
+// mentions. The title omits Markdown, collapses whitespace, and is capped at
+// 30 code points with a single ellipsis.
func Derive(body string) string {
+ return derive(body, true)
+}
+
+// DeriveFromFilename applies the title cleanup to an attachment filename.
+func DeriveFromFilename(filename string) string {
+ return derive(filename, false)
+}
+
+func derive(body string, skipBlockFences bool) string {
line := ""
+ openFence := ""
for _, candidate := range strings.Split(body, "\n") {
trimmed := strings.TrimSpace(candidate)
if trimmed == "" {
continue
}
+ if skipBlockFences {
+ fence := fencePrefix.FindString(trimmed)
+ if openFence != "" {
+ if fence == trimmed && strings.HasPrefix(fence, openFence) {
+ openFence = ""
+ }
+ continue
+ }
+ // An opening backtick fence requires a backtick-free info string.
+ if fence != "" && (fence[0] == '~' || !strings.ContainsRune(trimmed[len(fence):], '`')) {
+ openFence = fence
+ continue
+ }
+ }
if line == "" {
line = candidate
}
@@ -38,7 +63,7 @@ func Derive(body string) string {
break
}
}
- line = markdownFence.ReplaceAllString(line, " ")
+ line = inlineFence.ReplaceAllString(line, " ")
line = markdownMarks.ReplaceAllString(line, "")
line = markdownLink.ReplaceAllString(line, "$1")
line = strings.TrimSpace(titleSpace.ReplaceAllString(line, " "))
diff --git a/server/internal/chattitle/title_test.go b/server/internal/chattitle/title_test.go
index 11ea0889f3..49f9f93e50 100644
--- a/server/internal/chattitle/title_test.go
+++ b/server/internal/chattitle/title_test.go
@@ -21,3 +21,36 @@ func TestDeriveSkipsALineOfMentions(t *testing.T) {
}
}
}
+
+// A fenced code block can precede the sentence that names the chat. Its
+// language tag is not a title, and neither is any code inside the fence.
+func TestDeriveSkipsFencedCodeBlocks(t *testing.T) {
+ tests := []struct{ input, want string }{
+ {"```go\nfmt.Println(\"hi\")\n```\nWhat should I fix?", "What should I fix?"},
+ {"```\nplain code\n```\nSecond line", "Second line"},
+ {"```go\nfmt.Println(\"hi\")\n```", ""},
+ {"Before the fence\n```go\ncode\n```", "Before the fence"},
+ {"Use ```code``` here", "Use here"},
+ {"```code``` here", "here"},
+ {"How do I type ``` in markdown?\n```go\ncode\n```\nthanks", "How do I type in markdown?"},
+ {"Why does ``` break?\nsee below\n```go\ncode\n```", "Why does break?"},
+ {" ~~~go\ncode\n ~~~\nAfter the block", "After the block"},
+ {"```go\ncode without a closing fence", ""},
+ {"````md\n```go\ncode\n```\n`````\nAfter the outer fence", "After the outer fence"},
+ {"```go\n~~~\n```not a closing fence\ncode\n```\nAfter the block", "After the block"},
+ {"[DEV-43](mention://issue/issue-1)\n```go\ncode\n```\nWhy?", "Why?"},
+ }
+ for _, tc := range tests {
+ if got := Derive(tc.input); got != tc.want {
+ t.Errorf("Derive(%q) = %q, want %q", tc.input, got, tc.want)
+ }
+ }
+}
+
+func TestDeriveFromFilenameKeepsFencePrefixedNames(t *testing.T) {
+ for _, filename := range []string{"```report.md", "~~~report.md"} {
+ if got := DeriveFromFilename(filename); got != "report.md" {
+ t.Errorf("DeriveFromFilename(%q) = %q, want %q", filename, got, "report.md")
+ }
+ }
+}
diff --git a/server/internal/integrations/channel/engine/session.go b/server/internal/integrations/channel/engine/session.go
index 0af3860dc2..32bb036e05 100644
--- a/server/internal/integrations/channel/engine/session.go
+++ b/server/internal/integrations/channel/engine/session.go
@@ -16,6 +16,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/channelmedia"
+ "github.com/multica-ai/multica/server/internal/chattitle"
"github.com/multica-ai/multica/server/internal/integrations/channel"
db "github.com/multica-ai/multica/server/pkg/db/generated"
"github.com/multica-ai/multica/server/pkg/dbid"
@@ -1059,7 +1060,7 @@ func (s *ChatSession) bindMediaRefs(ctx context.Context, qtx SessionQueries, in
if strings.TrimSpace(source) == "" {
source = mediaTypeTitle(media.ref.Type)
}
- title := DeriveChatTitle(source)
+ title := chattitle.DeriveFromFilename(source)
if title == "" {
continue
}
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index 651db7c279..1c812af1f3 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -2685,7 +2685,7 @@ func (s *TaskService) sendDirectChatMessage(
return fmt.Errorf("list bound chat attachments for title: %w", err)
}
if len(attachments) > 0 {
- title := chattitle.Derive(attachments[0].Filename)
+ title := chattitle.DeriveFromFilename(attachments[0].Filename)
if title != "" {
if _, err := qtx.InitializeChatSessionMediaTitle(ctx, db.InitializeChatSessionMediaTitleParams{
ID: session.ID, MessageID: msg.ID, Title: title,