Spaces:
Build error
Build error
File size: 7,280 Bytes
3382f47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import 'dart:convert';
import 'package:auto_gpt_flutter_client/models/chat.dart';
import 'package:auto_gpt_flutter_client/views/chat/json_code_snippet_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
class AgentMessageTile extends StatefulWidget {
final Chat chat;
final VoidCallback onArtifactsButtonPressed;
const AgentMessageTile({
Key? key,
required this.chat,
required this.onArtifactsButtonPressed,
}) : super(key: key);
@override
_AgentMessageTileState createState() => _AgentMessageTileState();
}
class _AgentMessageTileState extends State<AgentMessageTile> {
bool isExpanded = false;
@override
Widget build(BuildContext context) {
String jsonString = jsonEncode(widget.chat.jsonResponse);
int artifactsCount = widget.chat.artifacts.length;
bool containsMarkdown(String text) {
// Regular expression to detect Markdown patterns like headers, bold, links, etc.
final RegExp markdownPattern = RegExp(
r'(?:\*\*|__).*?(?:\*\*|__)|' + // Bold
r'(?:\*|_).*?(?:\*|_)|' + // Italic
r'\[.*?\]\(.*?\)|' + // Links
r'!\[.*?\]\(.*?\)|' + // Images
r'#{1,6}.*|' + // Headers
r'```.*?```', // Fenced code blocks
dotAll: true, // To match across multiple lines
caseSensitive: false,
);
return markdownPattern.hasMatch(text);
}
bool hasMarkdown = containsMarkdown(widget.chat.message);
return LayoutBuilder(
builder: (context, constraints) {
double chatViewWidth = constraints.maxWidth;
double tileWidth = (chatViewWidth >= 1000) ? 900 : chatViewWidth - 40;
return Align(
alignment: Alignment.center,
child: Container(
width: tileWidth,
margin: const EdgeInsets.symmetric(vertical: 8),
padding: const EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.black, width: 0.5),
borderRadius: BorderRadius.circular(4),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
constraints: const BoxConstraints(minHeight: 50),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
"Agent",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(width: 20),
Expanded(
child: Container(
padding: const EdgeInsets.fromLTRB(0, 10, 20, 10),
child: SingleChildScrollView(
child: hasMarkdown
? Markdown(
data: widget.chat.message,
shrinkWrap: true,
styleSheet: MarkdownStyleSheet.fromTheme(
Theme.of(context))
.copyWith(
blockquoteDecoration: BoxDecoration(
color: Colors
.black, // Background color for blockquotes
border: Border(
left: BorderSide(
color: Colors.grey,
width: 4.0,
),
),
),
blockquoteAlign: WrapAlignment.start,
blockquotePadding: const EdgeInsets.all(
10.0), // Padding for blockquotes
codeblockDecoration: BoxDecoration(
color: Colors.grey[
200], // Background color for code blocks
borderRadius:
BorderRadius.circular(4.0),
),
codeblockPadding: const EdgeInsets.all(
10.0), // Padding for code blocks
code: TextStyle(
backgroundColor: Colors.grey[
200], // Background color for inline code
fontFamily: 'monospace',
),
),
)
: SelectableText(widget.chat.message,
maxLines: null),
),
),
),
ElevatedButton(
onPressed: widget.onArtifactsButtonPressed,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
side: const BorderSide(color: Colors.black),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('$artifactsCount Artifacts'),
),
const SizedBox(width: 20),
// Expand/Collapse button
IconButton(
splashRadius: 0.1,
icon: Icon(isExpanded
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down),
onPressed: () {
setState(() {
isExpanded = !isExpanded;
});
},
),
],
),
),
// Expanded view with JSON code snippet and copy button
if (isExpanded) ...[
const Divider(),
ClipRect(
child: SizedBox(
height: 200,
child: Padding(
padding: const EdgeInsets.only(right: 20),
child: JsonCodeSnippetView(
jsonString: jsonString,
),
),
),
),
],
],
),
),
);
},
);
}
}
|