fix outputfilter for think tokens

This commit is contained in:
Beingpax 2025-07-23 21:13:29 +05:45
parent f9adfd5269
commit 2e1ab6e57c

View File

@ -2,6 +2,7 @@ import Foundation
struct AIEnhancementOutputFilter {
static func filter(_ text: String) -> String {
var processedText = text
let patterns = [
#"(?s)<thinking>(.*?)</thinking>"#,
#"(?s)<think>(.*?)</think>"#,
@ -10,18 +11,11 @@ struct AIEnhancementOutputFilter {
for pattern in patterns {
if let regex = try? NSRegularExpression(pattern: pattern) {
let range = NSRange(text.startIndex..., in: text)
if let match = regex.firstMatch(in: text, options: [], range: range) {
// Extract content from the first capturing group
if match.numberOfRanges > 1, let contentRange = Range(match.range(at: 1), in: text) {
let extractedText = String(text[contentRange])
return extractedText.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
let range = NSRange(processedText.startIndex..., in: processedText)
processedText = regex.stringByReplacingMatches(in: processedText, options: [], range: range, withTemplate: "")
}
}
// If no tags are found, return the original text as is.
return text
return processedText.trimmingCharacters(in: .whitespacesAndNewlines)
}
}