Skip to content

Commit 70e32b5

Browse files
authored
Merge pull request #1501 from krissetto/oneline-title
Ensure generated title is a single line string
2 parents 125742e + 21b95ee commit 70e32b5

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

e2e/testdata/cassettes/TestRuntime_Mistral_Basic.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interactions:
5858
proto_minor: 1
5959
content_length: 0
6060
host: api.mistral.ai
61-
body: '{"messages":[{"content":"You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a title that captures the main topic.","role":"system"},{"content":"Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text, nothing else.\n\nUser message: What''s 2+2?\n\n","role":"user"}],"model":"mistral-small","stream_options":{"include_usage":true},"stream":true}'
61+
body: '{"messages":[{"content":"You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a single-line title that captures the main topic. Never use newlines or line breaks in your response.","role":"system"},{"content":"Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text on a single line, nothing else. Do not include any newlines, explanations, or formatting.\n\nUser message: What''s 2+2?\n\n","role":"user"}],"model":"mistral-small","stream_options":{"include_usage":true},"stream":true}'
6262
url: https://api.mistral.ai/v1/chat/completions
6363
method: POST
6464
response:

e2e/testdata/cassettes/TestRuntime_OpenAI_Basic.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interactions:
5252
proto_minor: 1
5353
content_length: 0
5454
host: api.openai.com
55-
body: '{"messages":[{"content":"You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a title that captures the main topic.","role":"system"},{"content":"Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text, nothing else.\n\nUser message: What''s 2+2?\n\n","role":"user"}],"model":"gpt-3.5-turbo","stream_options":{"include_usage":true},"stream":true}'
55+
body: '{"messages":[{"content":"You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a single-line title that captures the main topic. Never use newlines or line breaks in your response.","role":"system"},{"content":"Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text on a single line, nothing else. Do not include any newlines, explanations, or formatting.\n\nUser message: What''s 2+2?\n\n","role":"user"}],"model":"gpt-3.5-turbo","stream_options":{"include_usage":true},"stream":true}'
5656
url: https://api.openai.com/v1/chat/completions
5757
method: POST
5858
response:

pkg/runtime/title_generator.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log/slog"
7+
"strings"
78
"sync"
89

910
"github.com/docker/cagent/pkg/agent"
@@ -14,8 +15,8 @@ import (
1415
)
1516

1617
const (
17-
titleSystemPrompt = "You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a title that captures the main topic."
18-
titleUserPromptFormat = "Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text, nothing else.\n\nUser message: %s\n\n"
18+
titleSystemPrompt = "You are a helpful AI assistant that generates concise, descriptive titles for conversations. You will be given a conversation history and asked to create a single-line title that captures the main topic. Never use newlines or line breaks in your response."
19+
titleUserPromptFormat = "Based on the following message a user sent to an AI assistant, generate a short, descriptive title (maximum 50 characters) that captures the main topic or purpose of the conversation. Return ONLY the title text on a single line, nothing else. Do not include any newlines, explanations, or formatting.\n\nUser message: %s\n\n"
1920
)
2021

2122
type titleGenerator struct {
@@ -82,7 +83,29 @@ func (t *titleGenerator) generate(ctx context.Context, sess *session.Session, fi
8283
return
8384
}
8485

86+
// Sanitize the title to ensure it's a single line
87+
title = sanitizeTitle(title)
88+
if title == "" {
89+
return
90+
}
91+
8592
sess.Title = title
8693
slog.Debug("Generated session title", "session_id", sess.ID, "title", title)
8794
events <- SessionTitle(sess.ID, title)
8895
}
96+
97+
// sanitizeTitle ensures the title is a single line by taking only the first
98+
// non-empty line and stripping any control characters that could break TUI rendering.
99+
func sanitizeTitle(title string) string {
100+
// Split by newlines and take the first non-empty line
101+
lines := strings.Split(title, "\n")
102+
for _, line := range lines {
103+
line = strings.TrimSpace(line)
104+
if line != "" {
105+
// Remove any remaining carriage returns
106+
line = strings.ReplaceAll(line, "\r", "")
107+
return line
108+
}
109+
}
110+
return ""
111+
}

0 commit comments

Comments
 (0)