240 lines
6.6 KiB
Dart
240 lines
6.6 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../data/chat_repository.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'chat_create_dialog_frame.dart';
|
|
import 'chat_create_fields.dart';
|
|
|
|
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<JsonMap?> showChatCreateDialog(
|
|
BuildContext context, {
|
|
required ChatRepository repository,
|
|
required String operationId,
|
|
required Future<JsonMap> Function(ChatDraft draft, String operationId)
|
|
onSubmit,
|
|
}) {
|
|
final dialog = CreateChatDialog(
|
|
repository: repository,
|
|
operationId: operationId,
|
|
onSubmit: onSubmit,
|
|
);
|
|
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? showCupertinoDialog<JsonMap>(context: context, builder: (_) => dialog)
|
|
: showDialog<JsonMap>(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<JsonMap> Function(ChatDraft draft, String operationId) onSubmit;
|
|
|
|
@override
|
|
State<CreateChatDialog> createState() => _CreateChatDialogState();
|
|
}
|
|
|
|
class _CreateChatDialogState extends State<CreateChatDialog> {
|
|
final _name = TextEditingController(text: 'New chat');
|
|
final _instructions = TextEditingController();
|
|
List<String> _directories = const [];
|
|
List<String> _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++;
|
|
_name.dispose();
|
|
_instructions.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _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 = _directories.contains(_directory)
|
|
? _directory
|
|
: _directories.isEmpty
|
|
? null
|
|
: _directories.first;
|
|
_backend = _backends.contains(_backend)
|
|
? _backend
|
|
: _backends.isEmpty
|
|
? null
|
|
: _backends.first;
|
|
_loading = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted || attempt != _attempt) return;
|
|
setState(() {
|
|
_catalogError = '$error';
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
bool get _catalogReady =>
|
|
!_loading &&
|
|
_catalogError == null &&
|
|
_directories.isNotEmpty &&
|
|
_backends.isNotEmpty;
|
|
|
|
Future<void> _submit() async {
|
|
if (!_catalogReady || _submitting) return;
|
|
final title = _name.text.trim();
|
|
final directory = _directory ?? '';
|
|
if (title.isEmpty || directory.isEmpty) {
|
|
setState(() => _formError = 'Enter a name and server directory.');
|
|
return;
|
|
}
|
|
setState(() {
|
|
_submitting = true;
|
|
_formError = null;
|
|
});
|
|
try {
|
|
final session = await widget.onSubmit(
|
|
ChatDraft(
|
|
title: title,
|
|
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: [
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
placeholder: 'Name',
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
decoration: const InputDecoration(labelText: 'Name'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
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,
|
|
),
|
|
if (_catalogReady) ...[
|
|
const SizedBox(height: 8),
|
|
const Text('New chats use the server default model.'),
|
|
],
|
|
const SizedBox(height: 12),
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
placeholder: 'Instructions',
|
|
minLines: 1,
|
|
maxLines: 3,
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
decoration: const InputDecoration(labelText: 'Instructions'),
|
|
minLines: 1,
|
|
maxLines: 3,
|
|
),
|
|
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,
|
|
);
|
|
}
|
|
}
|