2025-08-20 17:01:46 +05:30
|
|
|
import 'dart:async';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-09-30 16:02:34 +05:30
|
|
|
import 'package:gpt_markdown/gpt_markdown.dart';
|
2025-08-20 17:01:46 +05:30
|
|
|
import 'package:conduit/shared/widgets/markdown/markdown_config.dart';
|
|
|
|
|
|
|
|
|
|
class StreamingMarkdownWidget extends StatefulWidget {
|
|
|
|
|
final Stream<String>? contentStream;
|
|
|
|
|
final String? staticContent;
|
|
|
|
|
final bool isStreaming;
|
|
|
|
|
|
|
|
|
|
const StreamingMarkdownWidget({
|
|
|
|
|
super.key,
|
|
|
|
|
this.contentStream,
|
|
|
|
|
this.staticContent,
|
|
|
|
|
required this.isStreaming,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
2025-09-24 12:00:49 +05:30
|
|
|
State<StreamingMarkdownWidget> createState() =>
|
|
|
|
|
_StreamingMarkdownWidgetState();
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _StreamingMarkdownWidgetState extends State<StreamingMarkdownWidget> {
|
|
|
|
|
final _buffer = StringBuffer();
|
|
|
|
|
Timer? _debounceTimer;
|
|
|
|
|
String _renderedContent = '';
|
|
|
|
|
StreamSubscription<String>? _streamSubscription;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
if (widget.contentStream != null) {
|
|
|
|
|
_streamSubscription = widget.contentStream!.listen(_handleChunk);
|
|
|
|
|
} else if (widget.staticContent != null) {
|
|
|
|
|
_renderedContent = widget.staticContent!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleChunk(String chunk) {
|
|
|
|
|
_buffer.write(chunk);
|
|
|
|
|
|
|
|
|
|
// Debounce rendering for performance
|
|
|
|
|
_debounceTimer?.cancel();
|
|
|
|
|
_debounceTimer = Timer(const Duration(milliseconds: 50), () {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_renderedContent = _fixIncompleteMarkdown(_buffer.toString());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _fixIncompleteMarkdown(String content) {
|
|
|
|
|
// Auto-close unclosed code blocks for valid markdown during streaming
|
|
|
|
|
final fenceCount = '```'.allMatches(content).length;
|
|
|
|
|
if (fenceCount % 2 != 0) {
|
|
|
|
|
content += '\n```';
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
// Fix incomplete bold/italic markers
|
|
|
|
|
final boldCount = RegExp(r'\*\*').allMatches(content).length;
|
|
|
|
|
if (boldCount % 2 != 0) {
|
|
|
|
|
content += '**';
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
final italicCount = RegExp(r'(?<!\*)\*(?!\*)').allMatches(content).length;
|
|
|
|
|
if (italicCount % 2 != 0) {
|
|
|
|
|
content += '*';
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
// Fix incomplete link brackets
|
|
|
|
|
final openBrackets = '['.allMatches(content).length;
|
|
|
|
|
final closeBrackets = ']'.allMatches(content).length;
|
|
|
|
|
if (openBrackets > closeBrackets) {
|
|
|
|
|
content += ']' * (openBrackets - closeBrackets);
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
final openParens = '('.allMatches(content).length;
|
|
|
|
|
final closeParens = ')'.allMatches(content).length;
|
|
|
|
|
if (openParens > closeParens) {
|
|
|
|
|
content += ')' * (openParens - closeParens);
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didUpdateWidget(StreamingMarkdownWidget oldWidget) {
|
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
// Handle stream changes
|
|
|
|
|
if (widget.contentStream != oldWidget.contentStream) {
|
|
|
|
|
_streamSubscription?.cancel();
|
|
|
|
|
if (widget.contentStream != null) {
|
|
|
|
|
_streamSubscription = widget.contentStream!.listen(_handleChunk);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
// Handle static content changes
|
|
|
|
|
if (widget.staticContent != oldWidget.staticContent) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_renderedContent = widget.staticContent ?? '';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 16:02:34 +05:30
|
|
|
final config = ConduitMarkdownConfig.getStyleConfig(context: context);
|
2025-08-20 17:01:46 +05:30
|
|
|
|
|
|
|
|
if (_renderedContent.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 16:02:34 +05:30
|
|
|
// GptMarkdown handles both streaming and static content elegantly
|
|
|
|
|
return GptMarkdown(_renderedContent, style: config.textStyle);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_debounceTimer?.cancel();
|
|
|
|
|
_streamSubscription?.cancel();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extension to provide easy access to streaming markdown
|
|
|
|
|
extension StreamingMarkdownExtension on String {
|
2025-09-30 16:02:34 +05:30
|
|
|
Widget toMarkdown({required BuildContext context, bool isStreaming = false}) {
|
2025-08-20 17:01:46 +05:30
|
|
|
return StreamingMarkdownWidget(
|
|
|
|
|
staticContent: this,
|
|
|
|
|
isStreaming: isStreaming,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper widget for displaying markdown with loading state
|
|
|
|
|
class MarkdownWithLoading extends StatelessWidget {
|
|
|
|
|
final String? content;
|
|
|
|
|
final bool isLoading;
|
|
|
|
|
|
2025-09-30 16:02:34 +05:30
|
|
|
const MarkdownWithLoading({super.key, this.content, required this.isLoading});
|
2025-08-20 17:01:46 +05:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
if (isLoading && (content == null || content!.isEmpty)) {
|
2025-09-30 16:02:34 +05:30
|
|
|
return const Center(child: CircularProgressIndicator());
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return StreamingMarkdownWidget(
|
|
|
|
|
staticContent: content ?? '',
|
|
|
|
|
isStreaming: isLoading,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
}
|