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

@@ -164,6 +164,18 @@ class ChatMessagesNotifier extends StateNotifier<List<ChatMessage>> {
]; ];
} }
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"');
@@ -779,6 +791,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"');
// 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); 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,11 +559,37 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
webSearchEnabledProvider.select((enabled) => enabled), webSearchEnabledProvider.select((enabled) => enabled),
); );
return GestureDetector( return AnimatedContainer(
duration: AnimationDuration.fast,
curve: Curves.easeInOut,
child: GestureDetector(
onTap: widget.enabled onTap: widget.enabled
? () { ? () {
// Toggle web search with haptic feedback
HapticFeedback.lightImpact();
ref.read(webSearchEnabledProvider.notifier).state = ref.read(webSearchEnabledProvider.notifier).state =
!webSearchEnabled; !webSearchEnabled;
// Show a snackbar to confirm the toggle
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
webSearchEnabled
? 'Web search disabled'
: 'Web search enabled - I can now search the internet',
style: TextStyle(
color: context.conduitTheme.textPrimary,
),
),
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, : null,
child: Container( child: Container(
@@ -572,7 +599,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
), ),
decoration: BoxDecoration( decoration: BoxDecoration(
color: webSearchEnabled color: webSearchEnabled
? context.conduitTheme.textPrimary.withValues( ? context.conduitTheme.info.withValues(
alpha: Alpha.buttonHover, alpha: Alpha.buttonHover,
) )
: context.conduitTheme.surfaceBackground.withValues( : context.conduitTheme.surfaceBackground.withValues(
@@ -581,9 +608,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
borderRadius: BorderRadius.circular(AppBorderRadius.xl), borderRadius: BorderRadius.circular(AppBorderRadius.xl),
border: Border.all( border: Border.all(
color: webSearchEnabled color: webSearchEnabled
? context.conduitTheme.textPrimary.withValues( ? context.conduitTheme.info
alpha: Alpha.buttonHover + Alpha.subtle,
)
: context.conduitTheme.textPrimary.withValues( : context.conduitTheme.textPrimary.withValues(
alpha: Alpha.subtle, alpha: Alpha.subtle,
), ),
@@ -593,12 +618,17 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
child: Row( child: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Icon( AnimatedSwitcher(
Platform.isIOS ? CupertinoIcons.search : Icons.travel_explore, duration: AnimationDuration.fast,
child: Icon(
webSearchEnabled
? (Platform.isIOS ? CupertinoIcons.globe : Icons.public)
: (Platform.isIOS ? CupertinoIcons.search : Icons.search),
key: ValueKey(webSearchEnabled),
size: IconSize.small, 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,
)) ))
@@ -606,16 +636,17 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
alpha: Alpha.disabled, alpha: Alpha.disabled,
), ),
), ),
),
const SizedBox(width: Spacing.sm), const SizedBox(width: Spacing.sm),
Flexible( Flexible(
child: Text( child: Text(
'Search', webSearchEnabled ? 'Web' : 'Search',
style: TextStyle( style: TextStyle(
fontSize: AppTypography.bodySmall, fontSize: AppTypography.bodySmall,
fontWeight: FontWeight.w500, fontWeight: webSearchEnabled ? FontWeight.w600 : FontWeight.w500,
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,
)) ))
@@ -628,6 +659,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
], ],
), ),
), ),
),
); );
} }
@@ -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,
), ),
), ),
], ],