Spaces:
Build error
Build error
File size: 7,145 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 177 178 179 |
import 'package:auto_gpt_flutter_client/models/message_type.dart';
import 'package:auto_gpt_flutter_client/viewmodels/settings_viewmodel.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_queue_viewmodel.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
import 'package:auto_gpt_flutter_client/views/chat/agent_message_tile.dart';
import 'package:auto_gpt_flutter_client/views/chat/chat_input_field.dart';
import 'package:auto_gpt_flutter_client/views/chat/loading_indicator.dart';
import 'package:auto_gpt_flutter_client/views/chat/user_message_tile.dart';
import 'package:flutter/material.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;
class ChatView extends StatefulWidget {
final ChatViewModel viewModel;
const ChatView({Key? key, required this.viewModel}) : super(key: key);
@override
_ChatViewState createState() => _ChatViewState();
}
class _ChatViewState extends State<ChatView> {
final ScrollController _scrollController = ScrollController();
bool _isAtBottom = true;
@override
void initState() {
super.initState();
// Listen for scroll events and determine whether the scroll is at the bottom
_scrollController.addListener(() {
if (_scrollController.position.atEdge) {
if (_scrollController.position.pixels == 0) {
_isAtBottom = false;
} else {
_isAtBottom = true;
}
}
});
// Schedule the fetchChatsForTask call for after the initial build
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.viewModel.fetchChatsForTask();
});
}
@override
void dispose() {
// Dispose of the ScrollController when the widget is removed
_scrollController.dispose();
super.dispose();
}
void _scrollToBottom() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
@override
Widget build(BuildContext context) {
// TODO: Do we want to have a reference to task view model in this class?
final taskViewModel = Provider.of<TaskViewModel>(context, listen: true);
return Scaffold(
body: Column(
children: [
// Chat messages list
Expanded(
child: ListView.builder(
controller: _scrollController,
itemCount: widget.viewModel.chats.length,
itemBuilder: (context, index) {
final chat = widget.viewModel.chats[index];
// If the last message has been built and we're at the bottom of the list, scroll down
if (index == widget.viewModel.chats.length - 1 && _isAtBottom) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToBottom();
});
}
if (chat.messageType == MessageType.user) {
return UserMessageTile(message: chat.message);
} else {
return AgentMessageTile(
key: ValueKey(chat.id),
chat: chat,
onArtifactsButtonPressed: () {
// Loop through each artifact and download it using the artifact_id
for (var artifact in chat.artifacts) {
widget.viewModel
.downloadArtifact(chat.taskId, artifact.artifactId);
}
},
);
}
},
),
),
const SizedBox(height: 10),
LoadingIndicator(
isLoading: Provider.of<TaskQueueViewModel>(context, listen: true)
.isBenchmarkRunning ||
widget.viewModel.isWaitingForAgentResponse ||
taskViewModel.isWaitingForAgentResponse),
const SizedBox(height: 10),
// Input area
Padding(
padding: const EdgeInsets.all(8.0),
child: ChatInputField(
onSendPressed: (message) async {
widget.viewModel.addTemporaryMessage(message);
try {
if (widget.viewModel.currentTaskId != null) {
widget.viewModel.sendChatMessage(
message,
continuousModeSteps: Provider.of<SettingsViewModel>(
context,
listen: false)
.continuousModeSteps);
} else {
String newTaskId = await taskViewModel.createTask(message);
widget.viewModel.setCurrentTaskId(newTaskId);
widget.viewModel.sendChatMessage(
message,
continuousModeSteps: Provider.of<SettingsViewModel>(
context,
listen: false)
.continuousModeSteps);
}
} catch (response) {
if (response is http.Response && response.statusCode == 404) {
Fluttertoast.showToast(
msg:
"404 error: Please ensure the correct baseURL for your agent in \nthe settings and that your agent adheres to the agent protocol.",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.TOP,
timeInSecForIosWeb: 5,
backgroundColor: Colors.red,
webPosition: "center",
webBgColor:
"linear-gradient(to right, #dc1c13, #dc1c13)",
textColor: Colors.white,
fontSize: 16.0);
} else if (response is http.Response &&
response.statusCode >= 500 &&
response.statusCode < 600) {
Fluttertoast.showToast(
msg: "500 error: Something went wrong",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.TOP,
timeInSecForIosWeb: 5,
backgroundColor: Colors.red,
webPosition: "center",
webBgColor:
"linear-gradient(to right, #dc1c13, #dc1c13)",
textColor: Colors.white,
fontSize: 16.0);
}
}
},
onContinuousModePressed: () {
widget.viewModel.isContinuousMode =
!widget.viewModel.isContinuousMode;
},
isContinuousMode: widget.viewModel.isContinuousMode,
viewModel: widget.viewModel,
),
),
],
),
);
}
}
|