|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd. |
4 | 4 |
|
| 5 | +import 'dart:math' as math; |
| 6 | + |
| 7 | +import 'package:devtools_app_shared/ui.dart'; |
5 | 8 | import 'package:flutter/material.dart'; |
| 9 | +import 'package:flutter/services.dart'; |
6 | 10 |
|
7 | 11 | import '../../../framework/scaffold/bottom_pane.dart'; |
8 | 12 | import '../../ui/tab.dart'; |
| 13 | +import '../../utils/utils.dart'; |
| 14 | +import '../ai_controller.dart'; |
| 15 | +import '../ai_message_types.dart'; |
9 | 16 |
|
10 | | -class AiAssistantPane extends StatelessWidget implements TabbedPane { |
| 17 | +class AiAssistantPane extends StatefulWidget implements TabbedPane { |
11 | 18 | const AiAssistantPane({super.key}); |
12 | 19 |
|
13 | 20 | @override |
14 | | - DevToolsTab get tab => |
15 | | - DevToolsTab.create(tabName: _tabName, gaPrefix: _gaPrefix); |
| 21 | + DevToolsTab get tab => DevToolsTab.create( |
| 22 | + tabName: AiAssistantPane._tabName, |
| 23 | + gaPrefix: AiAssistantPane._gaPrefix, |
| 24 | + ); |
16 | 25 |
|
17 | 26 | static const _tabName = 'AI Assistant'; |
18 | | - |
19 | 27 | static const _gaPrefix = 'aiAssistant'; |
20 | 28 |
|
| 29 | + @override |
| 30 | + State<AiAssistantPane> createState() => _AiAssistantPaneState(); |
| 31 | +} |
| 32 | + |
| 33 | +class _AiAssistantPaneState extends State<AiAssistantPane> { |
| 34 | + static const _baseOverscrollPadding = 125.0; |
| 35 | + static const _spinnerHeight = 50.0; |
| 36 | + static const _scrollDuration = Duration(milliseconds: 250); |
| 37 | + |
| 38 | + final _textController = TextEditingController(); |
| 39 | + final _messages = <ChatMessage>[]; |
| 40 | + final _scrollController = ScrollController(); |
| 41 | + final _aiController = AiController(); |
| 42 | + late final FocusNode _focusNode; |
| 43 | + |
| 44 | + bool _isThinking = false; |
| 45 | + double _overscrollPadding = _baseOverscrollPadding; |
| 46 | + |
| 47 | + @override |
| 48 | + void initState() { |
| 49 | + super.initState(); |
| 50 | + _focusNode = FocusNode(onKeyEvent: _handleEnterKey); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + void dispose() { |
| 55 | + _focusNode.dispose(); |
| 56 | + _textController.dispose(); |
| 57 | + super.dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + KeyEventResult _handleEnterKey(FocusNode node, KeyEvent event) { |
| 61 | + final isEnterKey = |
| 62 | + event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.enter; |
| 63 | + |
| 64 | + if (isEnterKey && !HardwareKeyboard.instance.isShiftPressed) { |
| 65 | + if (!_isThinking) { |
| 66 | + safeUnawaited(_sendMessage()); |
| 67 | + } |
| 68 | + return KeyEventResult.handled; |
| 69 | + } |
| 70 | + |
| 71 | + return KeyEventResult.ignored; |
| 72 | + } |
| 73 | + |
| 74 | + Future<void> _sendMessage() async { |
| 75 | + final messageText = _textController.text; |
| 76 | + if (messageText.isEmpty) return; |
| 77 | + _textController.clear(); |
| 78 | + |
| 79 | + final userMessage = ChatMessage(text: messageText, isUser: true); |
| 80 | + setState(() { |
| 81 | + _overscrollPadding = _calculateOverscrollPadding(userMessage); |
| 82 | + _isThinking = true; |
| 83 | + _messages.add(userMessage); |
| 84 | + }); |
| 85 | + _scrollToBottom(); |
| 86 | + |
| 87 | + final aiResponse = await _aiController.sendMessage(userMessage); |
| 88 | + setState(() { |
| 89 | + _isThinking = false; |
| 90 | + _overscrollPadding = _calculateOverscrollPadding(aiResponse); |
| 91 | + _messages.add(aiResponse); |
| 92 | + }); |
| 93 | + _scrollToBottom(); |
| 94 | + } |
| 95 | + |
| 96 | + void _scrollToBottom() { |
| 97 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 98 | + if (_scrollController.hasClients) { |
| 99 | + safeUnawaited( |
| 100 | + _scrollController.animateTo( |
| 101 | + _scrollController.position.maxScrollExtent, |
| 102 | + duration: _scrollDuration, |
| 103 | + curve: Curves.ease, |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + double _calculateOverscrollPadding(ChatMessage message) { |
| 111 | + final messageHeight = |
| 112 | + message.text.split('\n').length * (defaultFontSize + densePadding); |
| 113 | + final overscrollPadding = _baseOverscrollPadding + messageHeight; |
| 114 | + return message.isUser |
| 115 | + ? overscrollPadding + _spinnerHeight |
| 116 | + : overscrollPadding; |
| 117 | + } |
| 118 | + |
| 119 | + @override |
| 120 | + Widget build(BuildContext context) { |
| 121 | + return LayoutBuilder( |
| 122 | + builder: (context, constraints) { |
| 123 | + return Column( |
| 124 | + children: [ |
| 125 | + Expanded( |
| 126 | + child: ListView.builder( |
| 127 | + padding: EdgeInsets.only( |
| 128 | + bottom: math.max( |
| 129 | + 0, |
| 130 | + constraints.maxHeight - _overscrollPadding, |
| 131 | + ), |
| 132 | + ), |
| 133 | + controller: _scrollController, |
| 134 | + itemCount: _isThinking |
| 135 | + ? _messages.length + 1 |
| 136 | + : _messages.length, |
| 137 | + itemBuilder: (context, index) { |
| 138 | + if (_isThinking && index == _messages.length) { |
| 139 | + return const _ThinkingSpinner(); |
| 140 | + } |
| 141 | + return _ChatMessageBubble(message: _messages[index]); |
| 142 | + }, |
| 143 | + ), |
| 144 | + ), |
| 145 | + ConstrainedBox( |
| 146 | + constraints: BoxConstraints(maxHeight: constraints.maxHeight), |
| 147 | + child: Padding( |
| 148 | + padding: const EdgeInsets.all(denseSpacing), |
| 149 | + child: RoundedOutlinedBorder( |
| 150 | + child: Padding( |
| 151 | + // ignore: prefer-correct-edge-insets-constructor, false positive. |
| 152 | + padding: const EdgeInsets.fromLTRB( |
| 153 | + defaultSpacing, |
| 154 | + noPadding, |
| 155 | + defaultSpacing, |
| 156 | + densePadding, |
| 157 | + ), |
| 158 | + child: TextField( |
| 159 | + controller: _textController, |
| 160 | + focusNode: _focusNode, |
| 161 | + keyboardType: TextInputType.multiline, |
| 162 | + textAlignVertical: TextAlignVertical.center, |
| 163 | + minLines: 1, |
| 164 | + maxLines: 10, |
| 165 | + decoration: InputDecoration( |
| 166 | + hintText: 'Ask a question...', |
| 167 | + border: InputBorder.none, |
| 168 | + suffixIcon: IconButton( |
| 169 | + icon: const Icon(Icons.send), |
| 170 | + onPressed: _isThinking ? null : _sendMessage, |
| 171 | + ), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ), |
| 175 | + ), |
| 176 | + ), |
| 177 | + ), |
| 178 | + ], |
| 179 | + ); |
| 180 | + }, |
| 181 | + ); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +class _ChatMessageBubble extends StatelessWidget { |
| 186 | + const _ChatMessageBubble({required this.message}); |
| 187 | + |
| 188 | + final ChatMessage message; |
| 189 | + |
| 190 | + @override |
| 191 | + Widget build(BuildContext context) { |
| 192 | + final colorScheme = Theme.of(context).colorScheme; |
| 193 | + return Align( |
| 194 | + alignment: message.isUser ? Alignment.centerRight : Alignment.centerLeft, |
| 195 | + child: Container( |
| 196 | + decoration: BoxDecoration( |
| 197 | + color: message.isUser |
| 198 | + ? colorScheme.primaryContainer |
| 199 | + : colorScheme.secondaryContainer, |
| 200 | + borderRadius: defaultBorderRadius, |
| 201 | + ), |
| 202 | + padding: const EdgeInsets.all(defaultSpacing), |
| 203 | + margin: const EdgeInsets.all(denseSpacing), |
| 204 | + child: Text(message.text), |
| 205 | + ), |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +class _ThinkingSpinner extends StatelessWidget { |
| 211 | + const _ThinkingSpinner(); |
| 212 | + |
21 | 213 | @override |
22 | 214 | Widget build(BuildContext context) { |
23 | | - return const Column( |
24 | | - children: [ |
25 | | - Expanded(child: Center(child: Text('TODO: Implement AI Assistant.'))), |
26 | | - ], |
| 215 | + return const Align( |
| 216 | + alignment: Alignment.centerLeft, |
| 217 | + child: Padding( |
| 218 | + padding: EdgeInsets.symmetric( |
| 219 | + vertical: denseSpacing, |
| 220 | + horizontal: extraLargeSpacing, |
| 221 | + ), |
| 222 | + child: CircularProgressIndicator(), |
| 223 | + ), |
27 | 224 | ); |
28 | 225 | } |
29 | 226 | } |
0 commit comments