refactor: cleanup

This commit is contained in:
cogwheel0
2025-09-02 00:04:21 +05:30
parent 1b65743b06
commit 66935d1b0f
8 changed files with 29 additions and 39 deletions

View File

@@ -14,9 +14,7 @@ import '../models/chat_message.dart';
import '../auth/api_auth_interceptor.dart';
import '../validation/validation_interceptor.dart';
import '../error/api_error_interceptor.dart';
import 'sse_parser.dart';
// Tool-call details are parsed in the UI layer to render collapsible blocks
import 'stream_recovery_service.dart';
import 'persistent_streaming_service.dart';
import '../utils/debug_logger.dart';
@@ -2784,12 +2782,6 @@ class ApiService {
if (!streamController.isClosed) streamController.close();
}
}();
} else {
// Ensure we do not leak background-only identifiers into SSE path
data.remove('session_id');
data.remove('id');
// Use SSE streaming with proper parser (chat_id allowed)
_streamSSE(data, streamController, messageId);
}
return (
@@ -3032,9 +3024,8 @@ class ApiService {
}
} catch (_) {}
}
// SSE streaming with persistent background support - Main Implementation
void _streamSSE(
// SSE helpers removed: background task flow is the only path now.
/* void _streamSSE(
Map<String, dynamic> data,
StreamController<String> streamController,
String messageId,
@@ -3690,6 +3681,7 @@ class ApiService {
}
}
*/
// Legacy Socket.IO and older SSE methods removed
// File upload for RAG

View File

@@ -50,9 +50,9 @@ final shareReceiverInitializerProvider = Provider<void>((ref) {
// React when auth/model changes to process a queued share
ref.listen<AuthNavigationState>(
authNavigationStateProvider,
(_, __) => maybeProcessPending(),
(prev, next) => maybeProcessPending(),
);
ref.listen(selectedModelProvider, (_, __) => maybeProcessPending());
ref.listen(selectedModelProvider, (prev, next) => maybeProcessPending());
// Also poll once shortly after navigation settles to ensure ChatPage is ready
Future.delayed(const Duration(milliseconds: 150), () => maybeProcessPending());

View File

@@ -1,7 +1,7 @@
/// Utility class for parsing and extracting reasoning/thinking content from messages
/// Utility class for parsing and extracting reasoning/thinking content from messages.
class ReasoningParser {
/// Default tag pairs to detect raw reasoning blocks when providers don't emit <details>
/// This mirrors Open WebUI defaults: <think>...</think>, <reasoning>...</reasoning>
/// Default tag pairs to detect raw reasoning blocks when providers don't emit `<details>`.
/// This mirrors Open WebUI defaults: `<think>...</think>`, `<reasoning>...</reasoning>`.
static const List<List<String>> defaultReasoningTagPairs = <List<String>>[
['<think>', '</think>'],
['<reasoning>', '</reasoning>'],
@@ -9,8 +9,8 @@ class ReasoningParser {
/// Parses a message and extracts reasoning content
/// Supports:
/// - <details type="reasoning" ...> blocks (server-emitted)
/// - Raw tag pairs like <think>...</think> or <reasoning>...</reasoning>
/// - `<details type="reasoning" ...>` blocks (server-emitted)
/// - Raw tag pairs like `<think>...</think>` or `<reasoning>...</reasoning>`
/// - Optional custom tag pair override
static ReasoningContent? parseReasoningContent(
String content, {
@@ -19,7 +19,7 @@ class ReasoningParser {
}) {
if (content.isEmpty) return null;
// 1) Prefer server-emitted <details type="reasoning"> blocks
// 1) Prefer server-emitted `<details type="reasoning">` blocks
final detailsRegex = RegExp(
r'<details\s+type="reasoning"(?:\s+done="(true|false)")?(?:\s+duration="(\d+)")?[^>]*>\s*<summary>([^<]*)<\/summary>\s*([\s\S]*?)<\/details>',
multiLine: true,
@@ -44,7 +44,7 @@ class ReasoningParser {
);
}
// 2) Handle partially streamed <details> (opening present, no closing yet)
// 2) Handle partially streamed `<details>` (opening present, no closing yet)
final openingIdx = content.indexOf('<details type="reasoning"');
if (openingIdx >= 0 && !content.contains('</details>')) {
final after = content.substring(openingIdx);
@@ -80,7 +80,7 @@ class ReasoningParser {
for (final pair in tagPairs) {
final start = RegExp.escape(pair[0]);
final end = RegExp.escape(pair[1]);
final tagRegex = RegExp('($start)([\n\r\s\S]*?)($end)', multiLine: true, dotAll: true);
final tagRegex = RegExp('($start)([\s\S]*?)($end)', multiLine: true, dotAll: true);
final match = tagRegex.firstMatch(content);
if (match != null) {
final reasoning = (match.group(2) ?? '').trim();

View File

@@ -32,7 +32,7 @@ class ToolCallsContent {
});
}
/// Utility to parse <details type="tool_calls"> blocks from content
/// Utility to parse `<details type="tool_calls">` blocks from content.
class ToolCallsParser {
static String _unescapeHtml(String s) {
return s
@@ -135,7 +135,7 @@ class ToolCallsParser {
done: done,
arguments: args,
result: result,
files: (files is List) ? files as List : null,
files: (files is List) ? files : null,
),
),
);
@@ -230,10 +230,10 @@ class ToolCallsParser {
if (value == null) return '';
try {
final pretty = const JsonEncoder.withIndent(' ').convert(value);
return pretty.length > max ? pretty.substring(0, max) + '\n' : pretty;
return pretty.length > max ? '${pretty.substring(0, max)}\n' : pretty;
} catch (_) {
final raw = value.toString();
return raw.length > max ? raw.substring(0, max) + '' : raw;
return raw.length > max ? '${raw.substring(0, max)}' : raw;
}
}
}