import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../../core/api_gateway.dart'; import '../../data/chat_repository.dart'; import '../../domain/chat_commands.dart'; import 'chat_adaptive.dart'; import 'chat_create_dialog_frame.dart'; import 'chat_create_fields.dart'; /// The instructions field's helper and the model note, verbatim from the web. const String chatCreateInstructionsHelper = "Preloaded as this chat's session instructions. The agent still reads the " "repository's AGENTS.md and skills."; const String chatCreateModelNote = "This server has no request for choosing a chat's model, so a new chat " "uses the server's default."; class ChatDraft { const ChatDraft({ required this.title, required this.directory, required this.backend, required this.instructions, }); final String title; final String directory; final String backend; final String instructions; } /// The dialog sends the create itself so a refusal can keep the operator's /// draft and the server's words in place. [operationId] is minted once per /// create intent and reused across retries, so a retry cannot create twice. Future showChatCreateDialog( BuildContext context, { required ChatRepository repository, required String operationId, required Future Function(ChatDraft draft, String operationId) onSubmit, }) { final dialog = CreateChatDialog( repository: repository, operationId: operationId, onSubmit: onSubmit, ); return chatControlsStyleOf(context) == ChatControlsStyle.cupertino ? showCupertinoDialog(context: context, builder: (_) => dialog) : showDialog(context: context, builder: (_) => dialog); } class CreateChatDialog extends StatefulWidget { const CreateChatDialog({ super.key, required this.repository, required this.operationId, required this.onSubmit, }); final ChatRepository repository; final String operationId; final Future Function(ChatDraft draft, String operationId) onSubmit; @override State createState() => _CreateChatDialogState(); } class _CreateChatDialogState extends State { final _instructions = TextEditingController( text: chatDefaultSessionInstructions, ); List _directories = const []; List _backends = const []; String? _directory; String? _backend; String? _catalogError; String? _formError; bool _loading = true; bool _submitting = false; int _attempt = 0; @override void initState() { super.initState(); _loadCatalog(); } @override void dispose() { _attempt++; _instructions.dispose(); super.dispose(); } Future _loadCatalog() async { final attempt = ++_attempt; setState(() { _loading = true; _catalogError = null; }); try { final values = await Future.wait([ widget.repository.directories(), widget.repository.backends(), ]); if (!mounted || attempt != _attempt) return; setState(() { _directories = values[0]; _backends = values[1]; _directory = chatDirectoryChoices(_directories).contains(_directory) ? _directory : chatHomeDirectory; _backend = _backends.contains(_backend) ? _backend : _backends.contains('opencode') ? 'opencode' : _backends.first; _loading = false; }); } catch (error) { if (!mounted || attempt != _attempt) return; setState(() { _catalogError = '$error'; _loading = false; }); } } bool get _catalogReady => !_loading && _catalogError == null; Future _submit() async { if (!_catalogReady || _submitting) return; final directory = _directory ?? chatHomeDirectory; setState(() { _submitting = true; _formError = null; }); try { final session = await widget.onSubmit( ChatDraft( title: chatNewSessionTitle, directory: directory, backend: _backend ?? '', instructions: _instructions.text, ), widget.operationId, ); if (!mounted) return; Navigator.pop(context, session); } catch (error) { if (!mounted) return; setState(() { _submitting = false; _formError = '$error'; }); } } @override Widget build(BuildContext context) { final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; final content = SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ChatCatalogFields( loading: _loading, error: _catalogError, directories: _directories, backends: _backends, directory: _directory, backend: _backend, onDirectory: (value) => setState(() => _directory = value), onBackend: (value) => setState(() => _backend = value), onRetry: _loadCatalog, ), const SizedBox(height: 12), const Text('How should it act?'), const SizedBox(height: 4), if (cupertino) CupertinoTextField( key: const Key('chat-create-instructions'), controller: _instructions, minLines: 2, maxLines: 4, ) else TextField( key: const Key('chat-create-instructions'), controller: _instructions, minLines: 2, maxLines: 4, ), const SizedBox(height: 4), const Text(chatCreateInstructionsHelper), if (_catalogReady) ...[ const SizedBox(height: 8), const Text(chatCreateModelNote), const SizedBox(height: 8), Text( 'Start in ${_directory ?? chatHomeDirectory}', key: const Key('chat-create-start-in'), ), ], if (_formError != null) ...[ const SizedBox(height: 8), Text( _formError!, style: TextStyle( color: cupertino ? CupertinoColors.systemRed.resolveFrom(context) : Theme.of(context).colorScheme.error, ), ), ], ], ), ); return ChatCreateDialogFrame( content: content, canSubmit: _catalogReady, submitting: _submitting, onCancel: () => Navigator.pop(context), onSubmit: _submit, ); } }