Skip to content

Commit 6ca62b6

Browse files
committed
fix: optimize reasoning formatting with targeted regex
- Only apply formatting when ** patterns are detected in the message - Specifically target titles after sentence endings (. ! ?) - Addresses performance concern by avoiding regex on every chunk - Addresses specificity concern by targeting actual section headers - Prevents false matches on inline bold text mid-sentence
1 parent d7f7dda commit 6ca62b6

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/core/task/Task.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,17 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
19431943
switch (chunk.type) {
19441944
case "reasoning": {
19451945
reasoningMessage += chunk.text
1946-
// Add line breaks before **Title** patterns for better formatting
1947-
const formattedReasoning = reasoningMessage.replace(
1948-
/([^\n])\*\*([^*\n]+)\*\*/g,
1949-
"$1\n\n**$2**",
1950-
)
1946+
// Only apply formatting if the message contains sentence-ending punctuation followed by **
1947+
let formattedReasoning = reasoningMessage
1948+
if (reasoningMessage.includes("**")) {
1949+
// Add line breaks before **Title** patterns that appear after sentence endings
1950+
// This targets section headers like "...end of sentence.**Title Here**"
1951+
// Handles periods, exclamation marks, and question marks
1952+
formattedReasoning = reasoningMessage.replace(
1953+
/([.!?])\*\*([^*\n]+)\*\*/g,
1954+
"$1\n\n**$2**",
1955+
)
1956+
}
19511957
await this.say("reasoning", formattedReasoning, undefined, true)
19521958
break
19531959
}

0 commit comments

Comments
 (0)