2025-08-20 22:15:26 +05:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2025-08-31 14:02:44 +05:30
|
|
|
import 'dart:convert';
|
2025-08-31 19:07:05 +05:30
|
|
|
import 'dart:async';
|
2025-08-20 22:15:26 +05:30
|
|
|
import 'dart:io' show Platform;
|
|
|
|
|
import '../../../shared/theme/theme_extensions.dart';
|
|
|
|
|
import '../../../shared/widgets/markdown/streaming_markdown_widget.dart';
|
|
|
|
|
import '../../../core/utils/reasoning_parser.dart';
|
2025-09-05 22:44:04 +05:30
|
|
|
import '../../../core/utils/message_segments.dart';
|
2025-08-31 14:02:44 +05:30
|
|
|
import '../../../core/utils/tool_calls_parser.dart';
|
2025-09-20 23:58:18 +05:30
|
|
|
import '../providers/text_to_speech_provider.dart';
|
2025-08-20 22:15:26 +05:30
|
|
|
import 'enhanced_image_attachment.dart';
|
2025-08-23 20:09:43 +05:30
|
|
|
import 'package:conduit/l10n/app_localizations.dart';
|
2025-08-26 13:31:47 +05:30
|
|
|
import 'enhanced_attachment.dart';
|
2025-09-02 20:43:57 +05:30
|
|
|
import 'package:conduit/shared/widgets/chat_action_button.dart';
|
2025-09-20 22:03:55 +05:30
|
|
|
import '../../../shared/widgets/model_avatar.dart';
|
2025-08-20 22:15:26 +05:30
|
|
|
|
|
|
|
|
class AssistantMessageWidget extends ConsumerStatefulWidget {
|
|
|
|
|
final dynamic message;
|
|
|
|
|
final bool isStreaming;
|
|
|
|
|
final String? modelName;
|
2025-09-20 22:03:55 +05:30
|
|
|
final String? modelIconUrl;
|
2025-08-20 22:15:26 +05:30
|
|
|
final VoidCallback? onCopy;
|
|
|
|
|
final VoidCallback? onRegenerate;
|
|
|
|
|
final VoidCallback? onLike;
|
|
|
|
|
final VoidCallback? onDislike;
|
|
|
|
|
|
|
|
|
|
const AssistantMessageWidget({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.message,
|
|
|
|
|
this.isStreaming = false,
|
|
|
|
|
this.modelName,
|
2025-09-20 22:03:55 +05:30
|
|
|
this.modelIconUrl,
|
2025-08-20 22:15:26 +05:30
|
|
|
this.onCopy,
|
|
|
|
|
this.onRegenerate,
|
|
|
|
|
this.onLike,
|
|
|
|
|
this.onDislike,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState<AssistantMessageWidget> createState() =>
|
|
|
|
|
_AssistantMessageWidgetState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AssistantMessageWidgetState extends ConsumerState<AssistantMessageWidget>
|
|
|
|
|
with TickerProviderStateMixin {
|
|
|
|
|
late AnimationController _fadeController;
|
|
|
|
|
late AnimationController _slideController;
|
2025-09-05 22:44:04 +05:30
|
|
|
// Unified content segments (text, tool-calls, reasoning)
|
|
|
|
|
List<MessageSegment> _segments = const [];
|
2025-08-31 14:02:44 +05:30
|
|
|
final Set<String> _expandedToolIds = {};
|
2025-09-05 22:44:04 +05:30
|
|
|
final Set<int> _expandedReasoning = {};
|
2025-08-20 22:15:26 +05:30
|
|
|
Widget? _cachedAvatar;
|
2025-08-31 19:07:05 +05:30
|
|
|
bool _allowTypingIndicator = false;
|
|
|
|
|
Timer? _typingGateTimer;
|
2025-09-20 23:58:18 +05:30
|
|
|
String _ttsPlainText = '';
|
2025-09-02 20:43:57 +05:30
|
|
|
// press state handled by shared ChatActionButton
|
2025-08-20 22:15:26 +05:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_fadeController = AnimationController(
|
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
|
vsync: this,
|
|
|
|
|
);
|
|
|
|
|
_slideController = AnimationController(
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
vsync: this,
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
// Parse reasoning and tool-calls sections
|
|
|
|
|
_reparseSections();
|
2025-08-31 19:07:05 +05:30
|
|
|
_updateTypingIndicatorGate();
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangeDependencies() {
|
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
// Build cached avatar when theme context is available
|
|
|
|
|
_buildCachedAvatar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didUpdateWidget(AssistantMessageWidget oldWidget) {
|
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
// Re-parse sections when message content changes
|
2025-08-20 22:15:26 +05:30
|
|
|
if (oldWidget.message.content != widget.message.content) {
|
2025-08-31 14:02:44 +05:30
|
|
|
_reparseSections();
|
2025-08-31 19:07:05 +05:30
|
|
|
_updateTypingIndicatorGate();
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-20 22:03:55 +05:30
|
|
|
// Rebuild cached avatar if model name or icon changes
|
|
|
|
|
if (oldWidget.modelName != widget.modelName ||
|
|
|
|
|
oldWidget.modelIconUrl != widget.modelIconUrl) {
|
2025-08-20 22:15:26 +05:30
|
|
|
_buildCachedAvatar();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
void _reparseSections() {
|
|
|
|
|
final raw0 = widget.message.content ?? '';
|
|
|
|
|
// Strip any leftover placeholders from content before parsing
|
|
|
|
|
const ti = '[TYPING_INDICATOR]';
|
|
|
|
|
const searchBanner = '🔍 Searching the web...';
|
|
|
|
|
String raw = raw0;
|
|
|
|
|
if (raw.startsWith(ti)) {
|
|
|
|
|
raw = raw.substring(ti.length);
|
|
|
|
|
}
|
|
|
|
|
if (raw.startsWith(searchBanner)) {
|
|
|
|
|
raw = raw.substring(searchBanner.length);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
// Do not truncate content during streaming; segmented parser skips
|
2025-08-31 19:07:05 +05:30
|
|
|
// incomplete details blocks and tiles will render once complete.
|
2025-09-05 22:44:04 +05:30
|
|
|
final rSegs = ReasoningParser.segments(raw);
|
|
|
|
|
|
|
|
|
|
final out = <MessageSegment>[];
|
|
|
|
|
final textBuf = StringBuffer();
|
|
|
|
|
if (rSegs == null || rSegs.isEmpty) {
|
|
|
|
|
final tSegs = ToolCallsParser.segments(raw);
|
|
|
|
|
if (tSegs == null || tSegs.isEmpty) {
|
|
|
|
|
out.add(MessageSegment.text(raw));
|
|
|
|
|
textBuf.write(raw);
|
|
|
|
|
} else {
|
|
|
|
|
for (final s in tSegs) {
|
|
|
|
|
if (s.isToolCall && s.entry != null) {
|
|
|
|
|
out.add(MessageSegment.tool(s.entry!));
|
|
|
|
|
} else if ((s.text ?? '').isNotEmpty) {
|
|
|
|
|
out.add(MessageSegment.text(s.text!));
|
|
|
|
|
textBuf.write(s.text);
|
2025-08-31 19:07:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
} else {
|
|
|
|
|
for (final rs in rSegs) {
|
|
|
|
|
if (rs.isReasoning && rs.entry != null) {
|
|
|
|
|
out.add(MessageSegment.reason(rs.entry!));
|
|
|
|
|
} else if ((rs.text ?? '').isNotEmpty) {
|
|
|
|
|
final t = rs.text!;
|
|
|
|
|
final tSegs = ToolCallsParser.segments(t);
|
|
|
|
|
if (tSegs == null || tSegs.isEmpty) {
|
|
|
|
|
out.add(MessageSegment.text(t));
|
|
|
|
|
textBuf.write(t);
|
|
|
|
|
} else {
|
|
|
|
|
for (final s in tSegs) {
|
|
|
|
|
if (s.isToolCall && s.entry != null) {
|
|
|
|
|
out.add(MessageSegment.tool(s.entry!));
|
|
|
|
|
} else if ((s.text ?? '').isNotEmpty) {
|
|
|
|
|
out.add(MessageSegment.text(s.text!));
|
|
|
|
|
textBuf.write(s.text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 19:07:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 14:02:44 +05:30
|
|
|
|
2025-09-20 23:58:18 +05:30
|
|
|
final segments = out.isEmpty ? [MessageSegment.text(raw)] : out;
|
|
|
|
|
final speechText = _buildTtsPlainText(segments, raw);
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
setState(() {
|
2025-09-20 23:58:18 +05:30
|
|
|
_segments = segments;
|
|
|
|
|
_ttsPlainText = speechText;
|
2025-08-31 14:02:44 +05:30
|
|
|
});
|
2025-08-31 19:07:05 +05:30
|
|
|
_updateTypingIndicatorGate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _updateTypingIndicatorGate() {
|
2025-09-01 16:28:49 +05:30
|
|
|
// Show typing indicator while streaming until we have any renderable segments
|
|
|
|
|
// (tool tiles or actual text). Use a short delay to avoid flicker.
|
2025-08-31 19:07:05 +05:30
|
|
|
_typingGateTimer?.cancel();
|
|
|
|
|
final hasRenderable = _hasRenderableSegments;
|
2025-09-01 16:28:49 +05:30
|
|
|
if (widget.isStreaming && !hasRenderable) {
|
2025-08-31 19:07:05 +05:30
|
|
|
_allowTypingIndicator = false;
|
|
|
|
|
_typingGateTimer = Timer(const Duration(milliseconds: 150), () {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_allowTypingIndicator = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
_allowTypingIndicator = false;
|
|
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-20 23:58:18 +05:30
|
|
|
String get _messageId {
|
|
|
|
|
try {
|
|
|
|
|
final dynamic idValue = widget.message.id;
|
|
|
|
|
if (idValue == null) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return idValue.toString();
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _buildTtsPlainText(List<MessageSegment> segments, String fallback) {
|
|
|
|
|
if (segments.isEmpty) {
|
|
|
|
|
return _sanitizeForSpeech(fallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final buffer = StringBuffer();
|
|
|
|
|
for (final segment in segments) {
|
|
|
|
|
if (!segment.isText) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
final text = segment.text ?? '';
|
|
|
|
|
final sanitized = _sanitizeForSpeech(text);
|
|
|
|
|
if (sanitized.isEmpty) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (buffer.isNotEmpty) {
|
|
|
|
|
buffer.writeln();
|
|
|
|
|
buffer.writeln();
|
|
|
|
|
}
|
|
|
|
|
buffer.write(sanitized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final result = buffer.toString().trim();
|
|
|
|
|
if (result.isEmpty) {
|
|
|
|
|
return _sanitizeForSpeech(fallback);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _sanitizeForSpeech(String input) {
|
|
|
|
|
if (input.isEmpty) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var text = input;
|
|
|
|
|
text = text.replaceAll(RegExp(r'```'), ' ');
|
|
|
|
|
text = text.replaceAll(RegExp(r'`'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'!\[(.*?)\]\((.*?)\)'), r'$1');
|
|
|
|
|
text = text.replaceAll(RegExp(r'\[(.*?)\]\((.*?)\)'), r'$1');
|
|
|
|
|
text = text.replaceAll(RegExp(r'\*\*'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'__'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'\*'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'_'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'~'), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'^[-*+]\s+', multiLine: true), '');
|
|
|
|
|
text = text.replaceAll(RegExp(r'^>\s?', multiLine: true), '');
|
|
|
|
|
text = text.replaceAll(' ', ' ');
|
|
|
|
|
text = text.replaceAll('&', '&');
|
|
|
|
|
text = text.replaceAll('<', '<');
|
|
|
|
|
text = text.replaceAll('>', '>');
|
|
|
|
|
text = text.replaceAll(RegExp(r'[ \t]{2,}'), ' ');
|
|
|
|
|
text = text.replaceAll(RegExp(r'\n{3,}'), '\n\n');
|
|
|
|
|
return text.trim();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
// No streaming-specific markdown fixes needed here; handled by Markdown widget
|
|
|
|
|
|
|
|
|
|
Widget _buildToolCallTile(ToolCallEntry tc) {
|
|
|
|
|
final isExpanded = _expandedToolIds.contains(tc.id);
|
|
|
|
|
final theme = context.conduitTheme;
|
|
|
|
|
|
2025-09-16 18:15:44 +05:30
|
|
|
String pretty(dynamic v, {int max = 1200}) {
|
2025-08-31 14:02:44 +05:30
|
|
|
try {
|
2025-09-16 18:15:44 +05:30
|
|
|
final formatted = const JsonEncoder.withIndent(' ').convert(v);
|
|
|
|
|
return formatted.length > max
|
|
|
|
|
? '${formatted.substring(0, max)}\n…'
|
|
|
|
|
: formatted;
|
2025-08-31 14:02:44 +05:30
|
|
|
} catch (_) {
|
|
|
|
|
final s = v?.toString() ?? '';
|
2025-09-02 00:04:21 +05:30
|
|
|
return s.length > max ? '${s.substring(0, max)}…' : s;
|
2025-08-31 14:02:44 +05:30
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
2025-08-31 14:02:44 +05:30
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.only(bottom: Spacing.xs),
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: () {
|
|
|
|
|
setState(() {
|
|
|
|
|
if (isExpanded) {
|
|
|
|
|
_expandedToolIds.remove(tc.id);
|
|
|
|
|
} else {
|
|
|
|
|
_expandedToolIds.add(tc.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
child: Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.sm,
|
|
|
|
|
vertical: Spacing.xs,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceContainer.withValues(alpha: 0.5),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.dividerColor,
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
isExpanded
|
|
|
|
|
? Icons.expand_less_rounded
|
|
|
|
|
: Icons.expand_more_rounded,
|
|
|
|
|
size: 16,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Icon(
|
2025-09-16 18:15:44 +05:30
|
|
|
tc.done
|
|
|
|
|
? Icons.build_circle_outlined
|
|
|
|
|
: Icons.play_circle_outline,
|
2025-08-31 14:02:44 +05:30
|
|
|
size: 14,
|
|
|
|
|
color: theme.buttonPrimary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Text(
|
|
|
|
|
tc.done
|
|
|
|
|
? 'Tool Executed: ${tc.name}'
|
|
|
|
|
: 'Running tool: ${tc.name}…',
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
AnimatedCrossFade(
|
|
|
|
|
firstChild: const SizedBox.shrink(),
|
|
|
|
|
secondChild: Container(
|
|
|
|
|
margin: const EdgeInsets.only(top: Spacing.sm),
|
|
|
|
|
padding: const EdgeInsets.all(Spacing.sm),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceContainer.withValues(alpha: 0.3),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.dividerColor,
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
if (tc.arguments != null) ...[
|
|
|
|
|
Text(
|
|
|
|
|
'Arguments',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: Spacing.xxs),
|
|
|
|
|
SelectableText(
|
2025-09-16 18:15:44 +05:30
|
|
|
pretty(tc.arguments),
|
2025-08-31 14:02:44 +05:30
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontFamily: 'monospace',
|
|
|
|
|
height: 1.35,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: Spacing.sm),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
if (tc.result != null) ...[
|
|
|
|
|
Text(
|
|
|
|
|
'Result',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: Spacing.xxs),
|
|
|
|
|
SelectableText(
|
2025-09-16 18:15:44 +05:30
|
|
|
pretty(tc.result),
|
2025-08-31 14:02:44 +05:30
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontFamily: 'monospace',
|
|
|
|
|
height: 1.35,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-09-16 18:15:44 +05:30
|
|
|
crossFadeState: isExpanded
|
|
|
|
|
? CrossFadeState.showSecond
|
|
|
|
|
: CrossFadeState.showFirst,
|
2025-08-31 14:02:44 +05:30
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildSegmentedContent() {
|
|
|
|
|
final children = <Widget>[];
|
2025-09-05 23:18:24 +05:30
|
|
|
// Determine if media (attachments or generated images) is rendered above.
|
|
|
|
|
final hasMediaAbove =
|
|
|
|
|
(widget.message.attachmentIds?.isNotEmpty ?? false) ||
|
2025-09-16 18:15:44 +05:30
|
|
|
(widget.message.files?.isNotEmpty ?? false);
|
2025-08-31 19:07:05 +05:30
|
|
|
bool firstToolSpacerAdded = false;
|
2025-09-05 22:44:04 +05:30
|
|
|
int idx = 0;
|
|
|
|
|
for (final seg in _segments) {
|
|
|
|
|
if (seg.isTool && seg.toolCall != null) {
|
2025-08-31 19:07:05 +05:30
|
|
|
// Add top spacing before the first tool block for clarity
|
|
|
|
|
if (!firstToolSpacerAdded) {
|
|
|
|
|
children.add(const SizedBox(height: Spacing.sm));
|
|
|
|
|
firstToolSpacerAdded = true;
|
|
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
children.add(_buildToolCallTile(seg.toolCall!));
|
|
|
|
|
} else if (seg.isReasoning && seg.reasoning != null) {
|
2025-09-05 23:18:24 +05:30
|
|
|
// If a reasoning tile is the very first content and sits at the top,
|
|
|
|
|
// add a small spacer above it for breathing room.
|
|
|
|
|
if (children.isEmpty && !hasMediaAbove) {
|
|
|
|
|
children.add(const SizedBox(height: Spacing.sm));
|
|
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
children.add(_buildReasoningTile(seg.reasoning!, idx));
|
2025-08-31 14:02:44 +05:30
|
|
|
} else if ((seg.text ?? '').trim().isNotEmpty) {
|
2025-09-05 22:44:04 +05:30
|
|
|
children.add(_buildEnhancedMarkdownContent(seg.text!));
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
idx++;
|
2025-08-31 14:02:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (children.isEmpty) return const SizedBox.shrink();
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: children,
|
|
|
|
|
);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
bool get _hasRenderableSegments {
|
2025-09-16 18:15:44 +05:30
|
|
|
bool textRenderable(String t) {
|
2025-09-01 16:28:49 +05:30
|
|
|
String cleaned = t;
|
|
|
|
|
// Hide tool_calls blocks entirely
|
|
|
|
|
cleaned = cleaned.replaceAll(
|
|
|
|
|
RegExp(
|
|
|
|
|
r'<details\s+type="tool_calls"[^>]*>[\s\S]*?<\/details>',
|
|
|
|
|
multiLine: true,
|
|
|
|
|
dotAll: true,
|
|
|
|
|
),
|
|
|
|
|
'',
|
|
|
|
|
);
|
2025-09-05 22:44:04 +05:30
|
|
|
// Hide reasoning blocks as well in text check
|
|
|
|
|
cleaned = cleaned.replaceAll(
|
|
|
|
|
RegExp(
|
|
|
|
|
r'<details\s+type="reasoning"[^>]*>[\s\S]*?<\/details>',
|
|
|
|
|
multiLine: true,
|
|
|
|
|
dotAll: true,
|
|
|
|
|
),
|
|
|
|
|
'',
|
|
|
|
|
);
|
2025-09-01 16:28:49 +05:30
|
|
|
// If last <details> is unclosed, drop tail to avoid rendering raw tag
|
|
|
|
|
final lastOpen = cleaned.lastIndexOf('<details');
|
|
|
|
|
if (lastOpen >= 0) {
|
|
|
|
|
final tail = cleaned.substring(lastOpen);
|
|
|
|
|
if (!tail.contains('</details>')) {
|
|
|
|
|
cleaned = cleaned.substring(0, lastOpen);
|
|
|
|
|
}
|
2025-08-31 14:02:44 +05:30
|
|
|
}
|
2025-09-01 16:28:49 +05:30
|
|
|
return cleaned.trim().isNotEmpty;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 22:44:04 +05:30
|
|
|
for (final seg in _segments) {
|
|
|
|
|
if (seg.isTool && seg.toolCall != null) return true;
|
|
|
|
|
if (seg.isReasoning && seg.reasoning != null) return true;
|
2025-09-01 16:28:49 +05:30
|
|
|
final text = seg.text ?? '';
|
2025-09-16 18:15:44 +05:30
|
|
|
if (textRenderable(text)) return true;
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
2025-08-31 14:02:44 +05:30
|
|
|
return false;
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _buildCachedAvatar() {
|
2025-09-20 22:03:55 +05:30
|
|
|
final theme = context.conduitTheme;
|
|
|
|
|
final iconUrl = widget.modelIconUrl?.trim();
|
|
|
|
|
final hasIcon = iconUrl != null && iconUrl.isNotEmpty;
|
|
|
|
|
|
|
|
|
|
final Widget leading = hasIcon
|
|
|
|
|
? ModelAvatar(size: 20, imageUrl: iconUrl, label: widget.modelName)
|
|
|
|
|
: Container(
|
2025-08-20 22:15:26 +05:30
|
|
|
width: 20,
|
|
|
|
|
height: 20,
|
|
|
|
|
decoration: BoxDecoration(
|
2025-09-20 22:03:55 +05:30
|
|
|
color: theme.buttonPrimary,
|
2025-08-20 22:15:26 +05:30
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.small),
|
|
|
|
|
),
|
|
|
|
|
child: Icon(
|
|
|
|
|
Icons.auto_awesome,
|
2025-09-20 22:03:55 +05:30
|
|
|
color: theme.buttonPrimaryText,
|
2025-08-20 22:15:26 +05:30
|
|
|
size: 12,
|
|
|
|
|
),
|
2025-09-20 22:03:55 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_cachedAvatar = Padding(
|
|
|
|
|
padding: const EdgeInsets.only(bottom: 8),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
leading,
|
2025-08-20 22:15:26 +05:30
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Text(
|
|
|
|
|
widget.modelName ?? 'Assistant',
|
|
|
|
|
style: TextStyle(
|
2025-09-20 22:03:55 +05:30
|
|
|
color: theme.textSecondary,
|
2025-08-20 22:15:26 +05:30
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
letterSpacing: 0.1,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2025-08-31 19:07:05 +05:30
|
|
|
_typingGateTimer?.cancel();
|
2025-08-20 22:15:26 +05:30
|
|
|
_fadeController.dispose();
|
|
|
|
|
_slideController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return _buildDocumentationMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildDocumentationMessage() {
|
|
|
|
|
return Container(
|
|
|
|
|
width: double.infinity,
|
2025-09-20 19:23:37 +05:30
|
|
|
margin: const EdgeInsets.only(
|
|
|
|
|
bottom: 16,
|
|
|
|
|
left: Spacing.xs,
|
|
|
|
|
right: Spacing.xs,
|
|
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
// Cached AI Name and Avatar to prevent flashing
|
|
|
|
|
_cachedAvatar ?? const SizedBox.shrink(),
|
|
|
|
|
|
2025-09-05 22:44:04 +05:30
|
|
|
// Reasoning blocks are now rendered inline where they appear
|
2025-08-20 22:15:26 +05:30
|
|
|
|
|
|
|
|
// Documentation-style content without heavy bubble; premium markdown
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2025-08-26 13:31:47 +05:30
|
|
|
// Display attachments (images use EnhancedImageAttachment; non-images use card)
|
2025-08-20 22:15:26 +05:30
|
|
|
if (widget.message.attachmentIds != null &&
|
|
|
|
|
widget.message.attachmentIds!.isNotEmpty) ...[
|
2025-08-26 13:31:47 +05:30
|
|
|
_buildAttachmentItems(),
|
2025-08-20 22:15:26 +05:30
|
|
|
const SizedBox(height: Spacing.md),
|
|
|
|
|
],
|
|
|
|
|
|
2025-09-05 21:05:58 +05:30
|
|
|
// Display generated images from files property - OUTSIDE AnimatedSwitcher to prevent fade issues
|
2025-08-20 23:42:31 +05:30
|
|
|
if (widget.message.files != null &&
|
|
|
|
|
widget.message.files!.isNotEmpty) ...[
|
|
|
|
|
_buildGeneratedImages(),
|
|
|
|
|
const SizedBox(height: Spacing.md),
|
|
|
|
|
],
|
|
|
|
|
|
2025-08-31 14:02:44 +05:30
|
|
|
// Tool calls are rendered inline via segmented content
|
2025-08-31 19:07:05 +05:30
|
|
|
// Smoothly crossfade between typing indicator and content
|
|
|
|
|
AnimatedSwitcher(
|
|
|
|
|
duration: const Duration(milliseconds: 220),
|
|
|
|
|
switchInCurve: Curves.easeOutCubic,
|
|
|
|
|
switchOutCurve: Curves.easeInCubic,
|
|
|
|
|
transitionBuilder: (child, anim) {
|
|
|
|
|
final fade = CurvedAnimation(
|
|
|
|
|
parent: anim,
|
|
|
|
|
curve: Curves.easeOutCubic,
|
|
|
|
|
reverseCurve: Curves.easeInCubic,
|
|
|
|
|
);
|
|
|
|
|
final size = CurvedAnimation(
|
|
|
|
|
parent: anim,
|
|
|
|
|
curve: Curves.easeOutCubic,
|
|
|
|
|
reverseCurve: Curves.easeInCubic,
|
|
|
|
|
);
|
|
|
|
|
return FadeTransition(
|
|
|
|
|
opacity: fade,
|
|
|
|
|
child: SizeTransition(
|
|
|
|
|
sizeFactor: size,
|
|
|
|
|
axisAlignment: -1.0, // collapse/expand from top
|
|
|
|
|
child: child,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2025-09-16 18:15:44 +05:30
|
|
|
child:
|
|
|
|
|
(widget.isStreaming &&
|
2025-09-05 22:44:04 +05:30
|
|
|
!_hasRenderableSegments &&
|
|
|
|
|
_allowTypingIndicator)
|
2025-08-31 19:07:05 +05:30
|
|
|
? KeyedSubtree(
|
|
|
|
|
key: const ValueKey('typing'),
|
|
|
|
|
child: _buildTypingIndicator(),
|
|
|
|
|
)
|
|
|
|
|
: KeyedSubtree(
|
|
|
|
|
key: const ValueKey('content'),
|
|
|
|
|
child: _buildSegmentedContent(),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
2025-08-21 16:15:27 +05:30
|
|
|
// Action buttons below the message content (only after streaming completes)
|
|
|
|
|
if (!widget.isStreaming) ...[
|
|
|
|
|
const SizedBox(height: Spacing.sm),
|
|
|
|
|
_buildActionButtons(),
|
|
|
|
|
],
|
2025-08-20 22:15:26 +05:30
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.animate()
|
|
|
|
|
.fadeIn(duration: const Duration(milliseconds: 300))
|
|
|
|
|
.slideY(
|
|
|
|
|
begin: 0.1,
|
|
|
|
|
end: 0,
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
curve: Curves.easeOutCubic,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildEnhancedMarkdownContent(String content) {
|
|
|
|
|
if (content.trim().isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 16:28:49 +05:30
|
|
|
// Always hide tool_calls blocks; tiles render them separately.
|
|
|
|
|
String cleaned = content.replaceAll(
|
|
|
|
|
RegExp(
|
|
|
|
|
r'<details\s+type="tool_calls"[^>]*>[\s\S]*?<\/details>',
|
|
|
|
|
multiLine: true,
|
|
|
|
|
dotAll: true,
|
|
|
|
|
),
|
|
|
|
|
'',
|
|
|
|
|
);
|
2025-09-05 22:44:04 +05:30
|
|
|
// Also hide reasoning details blocks if any slipped into text
|
|
|
|
|
cleaned = cleaned.replaceAll(
|
|
|
|
|
RegExp(
|
|
|
|
|
r'<details\s+type="reasoning"[^>]*>[\s\S]*?<\/details>',
|
|
|
|
|
multiLine: true,
|
|
|
|
|
dotAll: true,
|
|
|
|
|
),
|
|
|
|
|
'',
|
|
|
|
|
);
|
|
|
|
|
// Remove raw <think>...</think> or <reasoning>...</reasoning> tags in text
|
|
|
|
|
cleaned = cleaned
|
2025-09-16 18:15:44 +05:30
|
|
|
.replaceAll(
|
|
|
|
|
RegExp(r'<think>[\s\S]*?<\/think>', multiLine: true, dotAll: true),
|
|
|
|
|
'',
|
|
|
|
|
)
|
|
|
|
|
.replaceAll(
|
|
|
|
|
RegExp(
|
|
|
|
|
r'<reasoning>[\s\S]*?<\/reasoning>',
|
|
|
|
|
multiLine: true,
|
|
|
|
|
dotAll: true,
|
|
|
|
|
),
|
|
|
|
|
'',
|
|
|
|
|
);
|
2025-09-01 16:28:49 +05:30
|
|
|
|
|
|
|
|
// If there's an unclosed <details>, drop the tail to avoid raw tags.
|
|
|
|
|
final lastOpen = cleaned.lastIndexOf('<details');
|
|
|
|
|
if (lastOpen >= 0) {
|
|
|
|
|
final tail = cleaned.substring(lastOpen);
|
|
|
|
|
if (!tail.contains('</details>')) {
|
|
|
|
|
cleaned = cleaned.substring(0, lastOpen);
|
2025-08-31 19:07:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process images in the remaining text
|
|
|
|
|
final processedContent = _processContentForImages(cleaned);
|
2025-08-20 22:15:26 +05:30
|
|
|
|
|
|
|
|
return StreamingMarkdownWidget(
|
|
|
|
|
staticContent: processedContent,
|
|
|
|
|
isStreaming: widget.isStreaming,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _processContentForImages(String content) {
|
|
|
|
|
// Check if content contains image markdown or base64 data URLs
|
|
|
|
|
// This ensures images generated by AI are properly formatted
|
|
|
|
|
|
|
|
|
|
// Pattern to detect base64 images that might not be in markdown format
|
|
|
|
|
final base64Pattern = RegExp(r'data:image/[^;]+;base64,[A-Za-z0-9+/]+=*');
|
|
|
|
|
|
|
|
|
|
// If we find base64 images not wrapped in markdown, wrap them
|
|
|
|
|
if (base64Pattern.hasMatch(content) && !content.contains('![')) {
|
|
|
|
|
content = content.replaceAllMapped(base64Pattern, (match) {
|
|
|
|
|
final imageData = match.group(0)!;
|
|
|
|
|
// Check if this image is already in markdown format
|
|
|
|
|
final markdownCheck = RegExp(
|
|
|
|
|
r'!\[.*?\]\(' + RegExp.escape(imageData) + r'\)',
|
|
|
|
|
);
|
|
|
|
|
if (!markdownCheck.hasMatch(content)) {
|
|
|
|
|
return '\n\n';
|
|
|
|
|
}
|
|
|
|
|
return imageData;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 13:31:47 +05:30
|
|
|
Widget _buildAttachmentItems() {
|
2025-08-20 22:15:26 +05:30
|
|
|
if (widget.message.attachmentIds == null ||
|
|
|
|
|
widget.message.attachmentIds!.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final imageCount = widget.message.attachmentIds!.length;
|
|
|
|
|
|
|
|
|
|
// Display images in a clean, modern layout for assistant messages
|
2025-08-21 12:49:41 +05:30
|
|
|
// Use AnimatedSwitcher for smooth transitions when loading
|
|
|
|
|
return AnimatedSwitcher(
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
switchInCurve: Curves.easeInOut,
|
|
|
|
|
child: imageCount == 1
|
|
|
|
|
? Container(
|
2025-08-26 13:31:47 +05:30
|
|
|
key: ValueKey('single_item_${widget.message.attachmentIds![0]}'),
|
|
|
|
|
child: EnhancedAttachment(
|
2025-08-21 12:49:41 +05:30
|
|
|
attachmentId: widget.message.attachmentIds![0],
|
|
|
|
|
isMarkdownFormat: true,
|
2025-08-21 16:15:27 +05:30
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 500,
|
|
|
|
|
maxHeight: 400,
|
|
|
|
|
),
|
2025-08-26 13:31:47 +05:30
|
|
|
disableAnimation: widget.isStreaming,
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
)
|
|
|
|
|
: Wrap(
|
2025-08-21 16:15:27 +05:30
|
|
|
key: ValueKey(
|
2025-08-26 13:31:47 +05:30
|
|
|
'multi_items_${widget.message.attachmentIds!.join('_')}',
|
2025-08-21 16:15:27 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
spacing: Spacing.sm,
|
|
|
|
|
runSpacing: Spacing.sm,
|
2025-08-21 16:15:27 +05:30
|
|
|
children: widget.message.attachmentIds!.map<Widget>((
|
|
|
|
|
attachmentId,
|
|
|
|
|
) {
|
2025-08-26 13:31:47 +05:30
|
|
|
return EnhancedAttachment(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('attachment_$attachmentId'),
|
|
|
|
|
attachmentId: attachmentId,
|
|
|
|
|
isMarkdownFormat: true,
|
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
|
maxWidth: imageCount == 2 ? 245 : 160,
|
|
|
|
|
maxHeight: imageCount == 2 ? 245 : 160,
|
|
|
|
|
),
|
2025-08-26 13:31:47 +05:30
|
|
|
disableAnimation: widget.isStreaming,
|
2025-08-21 12:49:41 +05:30
|
|
|
);
|
|
|
|
|
}).toList(),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
);
|
2025-08-20 23:42:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildGeneratedImages() {
|
|
|
|
|
if (widget.message.files == null || widget.message.files!.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
2025-08-21 16:15:27 +05:30
|
|
|
|
2025-08-20 23:42:31 +05:30
|
|
|
// Filter for image files
|
|
|
|
|
final imageFiles = widget.message.files!
|
|
|
|
|
.where((file) => file['type'] == 'image')
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
if (imageFiles.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final imageCount = imageFiles.length;
|
|
|
|
|
|
|
|
|
|
// Display generated images using EnhancedImageAttachment for consistency
|
2025-08-21 12:49:41 +05:30
|
|
|
// Use AnimatedSwitcher for smooth transitions
|
|
|
|
|
return AnimatedSwitcher(
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
switchInCurve: Curves.easeInOut,
|
|
|
|
|
child: imageCount == 1
|
|
|
|
|
? Container(
|
|
|
|
|
key: ValueKey('gen_single_${imageFiles[0]['url']}'),
|
|
|
|
|
child: Builder(
|
|
|
|
|
builder: (context) {
|
|
|
|
|
final imageUrl = imageFiles[0]['url'] as String?;
|
|
|
|
|
if (imageUrl == null) return const SizedBox.shrink();
|
2025-08-21 16:15:27 +05:30
|
|
|
|
2025-08-21 12:49:41 +05:30
|
|
|
return EnhancedImageAttachment(
|
2025-08-21 16:15:27 +05:30
|
|
|
attachmentId:
|
|
|
|
|
imageUrl, // Pass URL directly as it handles URLs
|
2025-08-21 12:49:41 +05:30
|
|
|
isMarkdownFormat: true,
|
2025-08-21 16:15:27 +05:30
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 500,
|
|
|
|
|
maxHeight: 400,
|
|
|
|
|
),
|
2025-09-16 18:15:44 +05:30
|
|
|
disableAnimation:
|
|
|
|
|
false, // Keep animations enabled to prevent black display
|
2025-08-21 12:49:41 +05:30
|
|
|
);
|
|
|
|
|
},
|
2025-08-20 23:42:31 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
)
|
|
|
|
|
: Wrap(
|
2025-08-21 16:15:27 +05:30
|
|
|
key: ValueKey(
|
|
|
|
|
'gen_multi_${imageFiles.map((f) => f['url']).join('_')}',
|
|
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
spacing: Spacing.sm,
|
|
|
|
|
runSpacing: Spacing.sm,
|
|
|
|
|
children: imageFiles.map<Widget>((file) {
|
|
|
|
|
final imageUrl = file['url'] as String?;
|
|
|
|
|
if (imageUrl == null) return const SizedBox.shrink();
|
2025-08-21 16:15:27 +05:30
|
|
|
|
2025-08-21 12:49:41 +05:30
|
|
|
return EnhancedImageAttachment(
|
|
|
|
|
key: ValueKey('gen_attachment_$imageUrl'),
|
|
|
|
|
attachmentId: imageUrl, // Pass URL directly
|
|
|
|
|
isMarkdownFormat: true,
|
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
|
maxWidth: imageCount == 2 ? 245 : 160,
|
|
|
|
|
maxHeight: imageCount == 2 ? 245 : 160,
|
|
|
|
|
),
|
2025-09-16 18:15:44 +05:30
|
|
|
disableAnimation:
|
|
|
|
|
false, // Keep animations enabled to prevent black display
|
2025-08-21 12:49:41 +05:30
|
|
|
);
|
|
|
|
|
}).toList(),
|
2025-08-20 23:42:31 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTypingIndicator() {
|
|
|
|
|
return Consumer(
|
|
|
|
|
builder: (context, ref, child) {
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2025-08-29 01:04:29 +05:30
|
|
|
// Increase spacing between assistant name and typing indicator
|
|
|
|
|
const SizedBox(height: Spacing.md),
|
2025-09-05 12:47:31 +05:30
|
|
|
// Give the indicator breathing room to avoid any clip from transitions
|
2025-09-01 16:28:49 +05:30
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(left: 4, bottom: 4),
|
|
|
|
|
child: SizedBox(
|
2025-09-08 00:05:13 +05:30
|
|
|
height: 22,
|
|
|
|
|
child: Platform.isIOS
|
|
|
|
|
? _buildTypingPillBubble()
|
|
|
|
|
: _buildTypingEllipsis(),
|
2025-09-01 16:28:49 +05:30
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-08 00:05:13 +05:30
|
|
|
Widget _buildTypingEllipsis() {
|
2025-09-05 12:47:31 +05:30
|
|
|
final min = AnimationValues.typingIndicatorScale;
|
2025-09-08 00:05:13 +05:30
|
|
|
final dotColor = context.conduitTheme.textSecondary.withValues(alpha: 0.75);
|
|
|
|
|
|
|
|
|
|
const double dotSize = 6.0;
|
|
|
|
|
const double gap = Spacing.xs; // 4.0
|
|
|
|
|
final d = AnimationDelay.typingDelay;
|
|
|
|
|
final d2 = Duration(milliseconds: d.inMilliseconds * 2);
|
|
|
|
|
|
|
|
|
|
Widget dot(Duration delay) {
|
|
|
|
|
return Container(
|
2025-09-16 18:15:44 +05:30
|
|
|
width: dotSize,
|
|
|
|
|
height: dotSize,
|
|
|
|
|
decoration: BoxDecoration(color: dotColor, shape: BoxShape.circle),
|
|
|
|
|
)
|
2025-09-08 00:05:13 +05:30
|
|
|
.animate(onPlay: (controller) => controller.repeat())
|
|
|
|
|
.then(delay: delay)
|
|
|
|
|
.scale(
|
|
|
|
|
duration: AnimationDuration.typingIndicator,
|
|
|
|
|
curve: AnimationCurves.typingIndicator,
|
|
|
|
|
begin: Offset(min, min),
|
|
|
|
|
end: const Offset(1, 1),
|
|
|
|
|
)
|
|
|
|
|
.then(delay: AnimationDelay.typingDelay)
|
|
|
|
|
.scale(
|
|
|
|
|
duration: AnimationDuration.typingIndicator,
|
|
|
|
|
curve: AnimationCurves.typingIndicator,
|
|
|
|
|
begin: const Offset(1, 1),
|
|
|
|
|
end: Offset(min, min),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
dot(Duration.zero),
|
|
|
|
|
const SizedBox(width: gap),
|
|
|
|
|
dot(d),
|
|
|
|
|
const SizedBox(width: gap),
|
|
|
|
|
dot(d2),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTypingPillBubble() {
|
|
|
|
|
final min = AnimationValues.typingIndicatorScale;
|
|
|
|
|
|
|
|
|
|
final bubbleColor = context.conduitTheme.surfaceContainerHighest;
|
|
|
|
|
final dotColor = context.conduitTheme.textSecondary.withValues(alpha: 0.75);
|
|
|
|
|
|
|
|
|
|
const double dotSize = 6.0;
|
|
|
|
|
const double gap = Spacing.xs; // 4.0
|
|
|
|
|
const double padV = 6.0;
|
|
|
|
|
const double padH = 10.0;
|
|
|
|
|
|
|
|
|
|
final d = AnimationDelay.typingDelay;
|
|
|
|
|
final d2 = Duration(milliseconds: d.inMilliseconds * 2);
|
|
|
|
|
|
|
|
|
|
Widget dot(Duration delay) {
|
|
|
|
|
return Container(
|
2025-09-16 18:15:44 +05:30
|
|
|
width: dotSize,
|
|
|
|
|
height: dotSize,
|
|
|
|
|
decoration: BoxDecoration(color: dotColor, shape: BoxShape.circle),
|
|
|
|
|
)
|
2025-09-08 00:05:13 +05:30
|
|
|
.animate(onPlay: (controller) => controller.repeat())
|
|
|
|
|
.then(delay: delay)
|
|
|
|
|
.scale(
|
|
|
|
|
duration: AnimationDuration.typingIndicator,
|
|
|
|
|
curve: AnimationCurves.typingIndicator,
|
|
|
|
|
begin: Offset(min, min),
|
|
|
|
|
end: const Offset(1, 1),
|
|
|
|
|
)
|
|
|
|
|
.then(delay: AnimationDelay.typingDelay)
|
|
|
|
|
.scale(
|
|
|
|
|
duration: AnimationDuration.typingIndicator,
|
|
|
|
|
curve: AnimationCurves.typingIndicator,
|
|
|
|
|
begin: const Offset(1, 1),
|
|
|
|
|
end: Offset(min, min),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 22:15:26 +05:30
|
|
|
return Container(
|
2025-09-08 00:05:13 +05:30
|
|
|
padding: const EdgeInsets.symmetric(horizontal: padH, vertical: padV),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: bubbleColor,
|
|
|
|
|
borderRadius: BorderRadius.circular(999),
|
|
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
dot(Duration.zero),
|
|
|
|
|
const SizedBox(width: gap),
|
|
|
|
|
dot(d),
|
|
|
|
|
const SizedBox(width: gap),
|
|
|
|
|
dot(d2),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildActionButtons() {
|
2025-09-20 23:58:18 +05:30
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
|
final ttsState = ref.watch(textToSpeechControllerProvider);
|
|
|
|
|
final messageId = _messageId;
|
|
|
|
|
final hasSpeechText = _ttsPlainText.trim().isNotEmpty;
|
2025-08-20 22:15:26 +05:30
|
|
|
final isErrorMessage =
|
|
|
|
|
widget.message.content.contains('⚠️') ||
|
|
|
|
|
widget.message.content.contains('Error') ||
|
|
|
|
|
widget.message.content.contains('timeout') ||
|
|
|
|
|
widget.message.content.contains('retry options');
|
|
|
|
|
|
2025-09-20 23:58:18 +05:30
|
|
|
final isActiveMessage = ttsState.activeMessageId == messageId;
|
|
|
|
|
final isSpeaking =
|
|
|
|
|
isActiveMessage && ttsState.status == TtsPlaybackStatus.speaking;
|
|
|
|
|
final isPaused =
|
|
|
|
|
isActiveMessage && ttsState.status == TtsPlaybackStatus.paused;
|
|
|
|
|
final isBusy =
|
|
|
|
|
isActiveMessage &&
|
|
|
|
|
(ttsState.status == TtsPlaybackStatus.loading ||
|
|
|
|
|
ttsState.status == TtsPlaybackStatus.initializing);
|
|
|
|
|
final bool disableDueToStreaming = widget.isStreaming && !isActiveMessage;
|
|
|
|
|
final bool ttsAvailable = !ttsState.initialized || ttsState.available;
|
|
|
|
|
final bool showStopState =
|
|
|
|
|
isActiveMessage && (isSpeaking || isPaused || isBusy);
|
|
|
|
|
final bool shouldShowTtsButton = hasSpeechText && messageId.isNotEmpty;
|
|
|
|
|
final bool canStartTts =
|
|
|
|
|
shouldShowTtsButton && !disableDueToStreaming && ttsAvailable;
|
|
|
|
|
|
|
|
|
|
VoidCallback? ttsOnTap;
|
|
|
|
|
if (showStopState || canStartTts) {
|
|
|
|
|
ttsOnTap = () {
|
|
|
|
|
if (messageId.isEmpty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ref
|
|
|
|
|
.read(textToSpeechControllerProvider.notifier)
|
|
|
|
|
.toggleForMessage(messageId: messageId, text: _ttsPlainText);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final IconData listenIcon = Platform.isIOS
|
|
|
|
|
? CupertinoIcons.speaker_2_fill
|
|
|
|
|
: Icons.volume_up;
|
|
|
|
|
final IconData stopIcon = Platform.isIOS
|
|
|
|
|
? CupertinoIcons.stop_fill
|
|
|
|
|
: Icons.stop;
|
|
|
|
|
final IconData ttsIcon = showStopState ? stopIcon : listenIcon;
|
|
|
|
|
final String ttsLabel = showStopState ? l10n.ttsStop : l10n.ttsListen;
|
|
|
|
|
|
2025-08-20 22:15:26 +05:30
|
|
|
return Wrap(
|
|
|
|
|
spacing: 8,
|
|
|
|
|
runSpacing: 8,
|
|
|
|
|
children: [
|
2025-09-20 23:58:18 +05:30
|
|
|
if (shouldShowTtsButton)
|
|
|
|
|
_buildActionButton(icon: ttsIcon, label: ttsLabel, onTap: ttsOnTap),
|
2025-08-20 22:15:26 +05:30
|
|
|
_buildActionButton(
|
|
|
|
|
icon: Platform.isIOS
|
|
|
|
|
? CupertinoIcons.doc_on_clipboard
|
|
|
|
|
: Icons.content_copy,
|
2025-09-20 23:58:18 +05:30
|
|
|
label: l10n.copy,
|
2025-08-20 22:15:26 +05:30
|
|
|
onTap: widget.onCopy,
|
|
|
|
|
),
|
|
|
|
|
if (isErrorMessage) ...[
|
|
|
|
|
_buildActionButton(
|
|
|
|
|
icon: Platform.isIOS
|
|
|
|
|
? CupertinoIcons.arrow_clockwise
|
|
|
|
|
: Icons.refresh,
|
2025-09-20 23:58:18 +05:30
|
|
|
label: l10n.retry,
|
2025-08-20 22:15:26 +05:30
|
|
|
onTap: widget.onRegenerate,
|
|
|
|
|
),
|
|
|
|
|
] else ...[
|
|
|
|
|
_buildActionButton(
|
|
|
|
|
icon: Platform.isIOS ? CupertinoIcons.refresh : Icons.refresh,
|
2025-09-20 23:58:18 +05:30
|
|
|
label: l10n.regenerate,
|
2025-08-20 22:15:26 +05:30
|
|
|
onTap: widget.onRegenerate,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildActionButton({
|
|
|
|
|
required IconData icon,
|
|
|
|
|
required String label,
|
|
|
|
|
VoidCallback? onTap,
|
|
|
|
|
}) {
|
2025-09-02 20:43:57 +05:30
|
|
|
return ChatActionButton(icon: icon, label: label, onTap: onTap);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
2025-09-05 22:44:04 +05:30
|
|
|
|
|
|
|
|
// Reasoning tile rendered inline at the position it appears
|
|
|
|
|
Widget _buildReasoningTile(ReasoningEntry rc, int index) {
|
|
|
|
|
final isExpanded = _expandedReasoning.contains(index);
|
|
|
|
|
final theme = context.conduitTheme;
|
|
|
|
|
|
|
|
|
|
String headerText() {
|
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
|
final hasSummary = rc.summary.isNotEmpty;
|
2025-09-16 18:15:44 +05:30
|
|
|
final isThinkingSummary =
|
|
|
|
|
rc.summary.trim().toLowerCase() == 'thinking…' ||
|
2025-09-05 22:44:04 +05:30
|
|
|
rc.summary.trim().toLowerCase() == 'thinking...';
|
|
|
|
|
if (widget.isStreaming) {
|
|
|
|
|
return hasSummary ? rc.summary : l10n.thinking;
|
|
|
|
|
}
|
|
|
|
|
if (rc.duration > 0) {
|
|
|
|
|
return l10n.thoughtForDuration(rc.formattedDuration);
|
|
|
|
|
}
|
|
|
|
|
if (!hasSummary || isThinkingSummary) {
|
|
|
|
|
return l10n.thoughts;
|
|
|
|
|
}
|
|
|
|
|
return rc.summary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.only(bottom: Spacing.xs),
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: () {
|
|
|
|
|
setState(() {
|
|
|
|
|
if (isExpanded) {
|
|
|
|
|
_expandedReasoning.remove(index);
|
|
|
|
|
} else {
|
|
|
|
|
_expandedReasoning.add(index);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
child: Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.sm,
|
|
|
|
|
vertical: Spacing.xs,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceContainer.withValues(alpha: 0.5),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.dividerColor,
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
isExpanded
|
|
|
|
|
? Icons.expand_less_rounded
|
|
|
|
|
: Icons.expand_more_rounded,
|
|
|
|
|
size: 16,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Icon(
|
|
|
|
|
Icons.psychology_outlined,
|
|
|
|
|
size: 14,
|
|
|
|
|
color: theme.buttonPrimary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Text(
|
|
|
|
|
headerText(),
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
AnimatedCrossFade(
|
|
|
|
|
firstChild: const SizedBox.shrink(),
|
|
|
|
|
secondChild: Container(
|
|
|
|
|
margin: const EdgeInsets.only(top: Spacing.sm),
|
|
|
|
|
padding: const EdgeInsets.all(Spacing.sm),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceContainer.withValues(alpha: 0.3),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.dividerColor,
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: SelectableText(
|
|
|
|
|
rc.cleanedReasoning,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: AppTypography.bodySmall,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontFamily: 'monospace',
|
|
|
|
|
height: 1.4,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-09-16 18:15:44 +05:30
|
|
|
crossFadeState: isExpanded
|
|
|
|
|
? CrossFadeState.showSecond
|
|
|
|
|
: CrossFadeState.showFirst,
|
2025-09-05 22:44:04 +05:30
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|