fix: sources count
This commit is contained in:
@@ -16,6 +16,7 @@ import '../error/api_error_interceptor.dart';
|
||||
// Tool-call details are parsed in the UI layer to render collapsible blocks
|
||||
import 'persistent_streaming_service.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
import '../utils/openwebui_source_parser.dart';
|
||||
|
||||
const bool _traceApiLogs = false;
|
||||
const bool _traceConversationParsing = false;
|
||||
@@ -1284,64 +1285,11 @@ class ApiService {
|
||||
}
|
||||
|
||||
List<ChatSourceReference> _parseSourcesField(dynamic raw) {
|
||||
if (raw is List) {
|
||||
return raw
|
||||
.whereType<Map>()
|
||||
.map((entry) {
|
||||
try {
|
||||
// Convert Map to Map<String, dynamic> safely
|
||||
final Map<String, dynamic> entryMap = {};
|
||||
entry.forEach((key, value) {
|
||||
entryMap[key.toString()] = value;
|
||||
});
|
||||
|
||||
// Handle nested source structure from OpenWebUI
|
||||
// Sources can have structure like: { "source": { "name": "...", "id": "..." }, "document": [...], "metadata": [...] }
|
||||
final sourceData = entryMap['source'];
|
||||
if (sourceData is Map) {
|
||||
// Extract the actual source information from nested structure
|
||||
final Map<String, dynamic> sourceMap = {};
|
||||
sourceData.forEach((key, value) {
|
||||
sourceMap[key.toString()] = value;
|
||||
});
|
||||
|
||||
// Add additional metadata from the outer structure if available
|
||||
if (entryMap.containsKey('document') &&
|
||||
entryMap['document'] is List) {
|
||||
final documents = entryMap['document'] as List;
|
||||
if (documents.isNotEmpty) {
|
||||
sourceMap['snippet'] = documents.first?.toString();
|
||||
}
|
||||
}
|
||||
|
||||
if (entryMap.containsKey('metadata') &&
|
||||
entryMap['metadata'] is List) {
|
||||
final metadata = entryMap['metadata'] as List;
|
||||
if (metadata.isNotEmpty && metadata.first is Map) {
|
||||
sourceMap['metadata'] = metadata.first;
|
||||
}
|
||||
}
|
||||
|
||||
return ChatSourceReference.fromJson(sourceMap);
|
||||
} else {
|
||||
// Fallback: treat the entire entry as a source (for backward compatibility)
|
||||
return ChatSourceReference.fromJson(entryMap);
|
||||
}
|
||||
} catch (e) {
|
||||
// Log the error and skip this entry
|
||||
DebugLogger.log(
|
||||
'source-parse-error',
|
||||
scope: 'api/chat',
|
||||
data: {'error': e.toString(), 'entry': entry.toString()},
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.where((item) => item != null)
|
||||
.cast<ChatSourceReference>()
|
||||
.toList(growable: false);
|
||||
try {
|
||||
return parseOpenWebUISourceList(raw);
|
||||
} catch (_) {
|
||||
return const <ChatSourceReference>[];
|
||||
}
|
||||
return const <ChatSourceReference>[];
|
||||
}
|
||||
|
||||
// Create new conversation using OpenWebUI API
|
||||
|
||||
@@ -12,6 +12,7 @@ import 'navigation_service.dart';
|
||||
import '../../shared/widgets/themed_dialogs.dart';
|
||||
import '../../shared/theme/theme_extensions.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
import '../utils/openwebui_source_parser.dart';
|
||||
|
||||
// Keep local verbosity toggle for socket logs
|
||||
const bool kSocketVerboseLogging = false;
|
||||
@@ -705,38 +706,17 @@ ActiveSocketStream attachUnifiedChunkedStreaming({
|
||||
} catch (_) {}
|
||||
} else {
|
||||
try {
|
||||
// Handle nested source structure from OpenWebUI
|
||||
final sourceData = map['source'];
|
||||
final ChatSourceReference source;
|
||||
|
||||
if (sourceData is Map<String, dynamic>) {
|
||||
// Extract the actual source information from nested structure
|
||||
final sourceMap = Map<String, dynamic>.from(sourceData);
|
||||
|
||||
// Add additional metadata from the outer structure if available
|
||||
if (map.containsKey('document') && map['document'] is List) {
|
||||
final documents = map['document'] as List;
|
||||
if (documents.isNotEmpty) {
|
||||
sourceMap['snippet'] = documents.first?.toString();
|
||||
final sources = parseOpenWebUISourceList([map]);
|
||||
if (sources.isNotEmpty) {
|
||||
final targetId = _resolveTargetMessageId(
|
||||
messageId,
|
||||
getMessages,
|
||||
);
|
||||
if (targetId != null) {
|
||||
for (final source in sources) {
|
||||
appendSourceReference(targetId, source);
|
||||
}
|
||||
}
|
||||
|
||||
if (map.containsKey('metadata') && map['metadata'] is List) {
|
||||
final metadata = map['metadata'] as List;
|
||||
if (metadata.isNotEmpty && metadata.first is Map) {
|
||||
sourceMap['metadata'] = metadata.first;
|
||||
}
|
||||
}
|
||||
|
||||
source = ChatSourceReference.fromJson(sourceMap);
|
||||
} else {
|
||||
// Fallback: treat the entire map as a source (for backward compatibility)
|
||||
source = ChatSourceReference.fromJson(map);
|
||||
}
|
||||
|
||||
final targetId = _resolveTargetMessageId(messageId, getMessages);
|
||||
if (targetId != null) {
|
||||
appendSourceReference(targetId, source);
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user