refactor: optimize regex handling in markdown and tool calls parsing

- Improved performance by pre-compiling regex patterns in the ConduitMarkdownPreprocessor for better efficiency during streaming.
- Enhanced the ToolCallsParser to conditionally run cleanup regex only when necessary, ensuring cleaner and more efficient text processing.
- Updated the AssistantMessageWidget to perform quick checks before cleaning raw tags, reducing unnecessary operations and improving overall performance.
This commit is contained in:
cogwheel0
2025-10-05 23:51:48 +05:30
parent 9dd27bb4e5
commit 3af46b379b
3 changed files with 81 additions and 60 deletions

View File

@@ -724,22 +724,25 @@ class _AssistantMessageWidgetState extends ConsumerState<AssistantMessageWidget>
// and type="tool_calls") via a custom block syntax, so they won't be rendered as
// plain text during streaming. This prevents character flashing.
// We still clean raw reasoning tags (<think>, <reasoning>) as a fallback.
// The server normally converts these to <details> format, but raw mode or
// direct API responses might still use them.
String cleaned = content
.replaceAll(
RegExp(r'<think>[\s\S]*?<\/think>', multiLine: true, dotAll: true),
'',
)
.replaceAll(
RegExp(
r'<reasoning>[\s\S]*?<\/reasoning>',
multiLine: true,
dotAll: true,
),
'',
);
// Quick check: only run cleanup if raw tags might exist (rare case)
String cleaned = content;
if (content.contains('<think>') || content.contains('<reasoning>')) {
// Clean raw reasoning tags as a fallback for raw mode or direct API responses.
// The server normally converts these to <details> format.
cleaned = content
.replaceAll(
RegExp(r'<think>[\s\S]*?<\/think>', multiLine: true, dotAll: true),
'',
)
.replaceAll(
RegExp(
r'<reasoning>[\s\S]*?<\/reasoning>',
multiLine: true,
dotAll: true,
),
'',
);
}
// Process images in the remaining text
final processedContent = _processContentForImages(cleaned);