fix: web search

This commit is contained in:
cogwheel0
2025-08-19 13:09:40 +05:30
parent 5da31b60b4
commit 8c6a4dd2a2
3 changed files with 168 additions and 55 deletions

View File

@@ -2470,6 +2470,19 @@ class ApiService {
data['chat_id'] = conversationId; data['chat_id'] = conversationId;
} }
// Add web search flag if enabled
if (enableWebSearch) {
data['web_search'] = true;
// Also add it in features for compatibility
data['features'] = {
'web_search': true,
'image_generation': false,
'code_interpreter': false,
'memory': false,
};
debugPrint('DEBUG: Web search enabled in SSE request');
}
// Don't add session_id or id - they break SSE streaming! // Don't add session_id or id - they break SSE streaming!
// The server falls back to task-based async when these are present // The server falls back to task-based async when these are present

View File

@@ -163,6 +163,18 @@ class ChatMessagesNotifier extends StateNotifier<List<ChatMessage>> {
lastMessage.copyWith(content: content), lastMessage.copyWith(content: content),
]; ];
} }
void updateLastMessageWithFunction(ChatMessage Function(ChatMessage) updater) {
if (state.isEmpty) return;
final lastMessage = state.last;
if (lastMessage.role != 'assistant') return;
state = [
...state.sublist(0, state.length - 1),
updater(lastMessage),
];
}
void appendToLastMessage(String content) { void appendToLastMessage(String content) {
debugPrint('DEBUG: appendToLastMessage called with: "$content"'); debugPrint('DEBUG: appendToLastMessage called with: "$content"');
@@ -778,6 +790,9 @@ Future<void> _sendMessageInternal(
// Check if web search is enabled for API // Check if web search is enabled for API
final webSearchEnabled = ref.read(webSearchEnabledProvider); final webSearchEnabled = ref.read(webSearchEnabledProvider);
// Debug log to track web search state
debugPrint('DEBUG: Web search toggle state: $webSearchEnabled');
// No need for function calling tools since we're using retrieval directly // No need for function calling tools since we're using retrieval directly
final tools = <Map<String, dynamic>>[]; final tools = <Map<String, dynamic>>[];
@@ -979,10 +994,49 @@ Future<void> _sendMessageInternal(
}, },
); );
// Track web search status
bool isSearching = false;
final streamSubscription = persistentController.stream.listen( final streamSubscription = persistentController.stream.listen(
(chunk) { (chunk) {
debugPrint('DEBUG: Received stream chunk: "$chunk"'); debugPrint('DEBUG: Received stream chunk: "$chunk"');
ref.read(chatMessagesProvider.notifier).appendToLastMessage(chunk);
// Check for web search indicators in the stream
if (webSearchEnabled && !isSearching) {
// Check if this is the start of web search
if (chunk.contains('[SEARCHING]') ||
chunk.contains('Searching the web') ||
chunk.contains('web search')) {
isSearching = true;
// Update the message to show search status
ref.read(chatMessagesProvider.notifier).updateLastMessageWithFunction(
(message) => message.copyWith(
content: '🔍 Searching the web...',
metadata: {'webSearchActive': true},
),
);
return; // Don't append this chunk
}
}
// Check if web search is complete
if (isSearching && (chunk.contains('[/SEARCHING]') ||
chunk.contains('Search complete'))) {
isSearching = false;
// Clear the search status message
ref.read(chatMessagesProvider.notifier).updateLastMessageWithFunction(
(message) => message.copyWith(
content: '',
metadata: {'webSearchActive': false},
),
);
return; // Don't append this chunk
}
// Regular content - append to message
if (!chunk.contains('[SEARCHING]') && !chunk.contains('[/SEARCHING]')) {
ref.read(chatMessagesProvider.notifier).appendToLastMessage(chunk);
}
}, },
onDone: () async { onDone: () async {

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import '../../../shared/theme/theme_extensions.dart'; import '../../../shared/theme/theme_extensions.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -558,64 +559,76 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
webSearchEnabledProvider.select((enabled) => enabled), webSearchEnabledProvider.select((enabled) => enabled),
); );
return GestureDetector( return AnimatedContainer(
onTap: widget.enabled duration: AnimationDuration.fast,
? () { curve: Curves.easeInOut,
ref.read(webSearchEnabledProvider.notifier).state = child: GestureDetector(
!webSearchEnabled; onTap: widget.enabled
} ? () {
: null, // Toggle web search with haptic feedback
child: Container( HapticFeedback.lightImpact();
padding: const EdgeInsets.symmetric( ref.read(webSearchEnabledProvider.notifier).state =
horizontal: Spacing.md, !webSearchEnabled;
vertical: Spacing.sm,
), // Show a snackbar to confirm the toggle
decoration: BoxDecoration( ScaffoldMessenger.of(context).showSnackBar(
color: webSearchEnabled SnackBar(
? context.conduitTheme.textPrimary.withValues( content: Text(
alpha: Alpha.buttonHover, webSearchEnabled
) ? 'Web search disabled'
: context.conduitTheme.surfaceBackground.withValues( : 'Web search enabled - I can now search the internet',
alpha: Alpha.subtle, style: TextStyle(
), color: context.conduitTheme.textPrimary,
borderRadius: BorderRadius.circular(AppBorderRadius.xl), ),
border: Border.all( ),
backgroundColor: context.conduitTheme.surfaceBackground,
duration: const Duration(seconds: 2),
behavior: SnackBarBehavior.floating,
margin: const EdgeInsets.all(Spacing.md),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppBorderRadius.md),
),
),
);
}
: null,
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: Spacing.md,
vertical: Spacing.sm,
),
decoration: BoxDecoration(
color: webSearchEnabled color: webSearchEnabled
? context.conduitTheme.textPrimary.withValues( ? context.conduitTheme.info.withValues(
alpha: Alpha.buttonHover + Alpha.subtle, alpha: Alpha.buttonHover,
) )
: context.conduitTheme.textPrimary.withValues( : context.conduitTheme.surfaceBackground.withValues(
alpha: Alpha.subtle, alpha: Alpha.subtle,
), ),
width: BorderWidth.regular, borderRadius: BorderRadius.circular(AppBorderRadius.xl),
), border: Border.all(
), color: webSearchEnabled
child: Row( ? context.conduitTheme.info
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Platform.isIOS ? CupertinoIcons.search : Icons.travel_explore,
size: IconSize.small,
color: widget.enabled
? (webSearchEnabled
? context.conduitTheme.textPrimary
: context.conduitTheme.textPrimary.withValues(
alpha: Alpha.strong,
))
: context.conduitTheme.textPrimary.withValues( : context.conduitTheme.textPrimary.withValues(
alpha: Alpha.disabled, alpha: Alpha.subtle,
), ),
width: BorderWidth.regular,
), ),
const SizedBox(width: Spacing.sm), ),
Flexible( child: Row(
child: Text( mainAxisSize: MainAxisSize.min,
'Search', children: [
style: TextStyle( AnimatedSwitcher(
fontSize: AppTypography.bodySmall, duration: AnimationDuration.fast,
fontWeight: FontWeight.w500, child: Icon(
webSearchEnabled
? (Platform.isIOS ? CupertinoIcons.globe : Icons.public)
: (Platform.isIOS ? CupertinoIcons.search : Icons.search),
key: ValueKey(webSearchEnabled),
size: IconSize.small,
color: widget.enabled color: widget.enabled
? (webSearchEnabled ? (webSearchEnabled
? context.conduitTheme.textPrimary ? Colors.white
: context.conduitTheme.textPrimary.withValues( : context.conduitTheme.textPrimary.withValues(
alpha: Alpha.strong, alpha: Alpha.strong,
)) ))
@@ -624,8 +637,27 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
), ),
), ),
), ),
), const SizedBox(width: Spacing.sm),
], Flexible(
child: Text(
webSearchEnabled ? 'Web' : 'Search',
style: TextStyle(
fontSize: AppTypography.bodySmall,
fontWeight: webSearchEnabled ? FontWeight.w600 : FontWeight.w500,
color: widget.enabled
? (webSearchEnabled
? Colors.white
: context.conduitTheme.textPrimary.withValues(
alpha: Alpha.strong,
))
: context.conduitTheme.textPrimary.withValues(
alpha: Alpha.disabled,
),
),
),
),
],
),
), ),
), ),
); );
@@ -638,7 +670,9 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
if (!webSearchEnabled) return const SizedBox.shrink(); if (!webSearchEnabled) return const SizedBox.shrink();
return Container( return AnimatedContainer(
duration: AnimationDuration.fast,
curve: Curves.easeInOut,
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: Spacing.md, horizontal: Spacing.md,
vertical: Spacing.xs, vertical: Spacing.xs,
@@ -662,15 +696,27 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Icon( Icon(
Platform.isIOS ? CupertinoIcons.search : Icons.travel_explore, Platform.isIOS ? CupertinoIcons.globe : Icons.travel_explore,
size: IconSize.small, size: IconSize.small,
color: context.conduitTheme.info, color: context.conduitTheme.info,
), ),
const SizedBox(width: Spacing.xs), const SizedBox(width: Spacing.xs),
Text( Text(
'Web search on', 'Web search enabled',
style: AppTypography.captionStyle.copyWith( style: AppTypography.captionStyle.copyWith(
color: context.conduitTheme.info, color: context.conduitTheme.info,
fontWeight: FontWeight.w500,
),
),
const SizedBox(width: Spacing.xs),
GestureDetector(
onTap: () {
ref.read(webSearchEnabledProvider.notifier).state = false;
},
child: Icon(
Icons.close,
size: 12,
color: context.conduitTheme.info,
), ),
), ),
], ],