357 lines
11 KiB
Dart
357 lines
11 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../../../core/servers/server_scope.dart';
|
|
import '../../data/chat_create_prompt_store.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';
|
|
import 'chat_prompt_chooser.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<JsonMap?> showChatCreateDialog(
|
|
BuildContext context, {
|
|
required ChatRepository repository,
|
|
required String operationId,
|
|
required Future<JsonMap> Function(ChatDraft draft, String operationId)
|
|
onSubmit,
|
|
ChatCreatePromptStore? prompts,
|
|
}) {
|
|
final dialog = CreateChatDialog(
|
|
repository: repository,
|
|
operationId: operationId,
|
|
onSubmit: onSubmit,
|
|
prompts: prompts,
|
|
);
|
|
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,
|
|
this.prompts,
|
|
});
|
|
|
|
/// The last first-prompt choice per harness and directory.
|
|
final ChatCreatePromptStore? prompts;
|
|
|
|
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> {
|
|
late final ChatCreatePromptStore _prompts =
|
|
widget.prompts ??
|
|
SharedPreferencesChatCreatePromptStore(
|
|
serverId: serverStorageScope(serverIdentity(widget.repository.gateway)),
|
|
);
|
|
final _instructions = TextEditingController(
|
|
text: chatDefaultSessionInstructions,
|
|
);
|
|
final _name = TextEditingController();
|
|
|
|
/// The name follows the folder and harness until the operator edits it.
|
|
bool _nameEdited = false;
|
|
|
|
/// Whether the new chat starts with the prompt below.
|
|
bool _sendPrompt = false;
|
|
|
|
/// A user's choice or edit wins over any pending saved-prompt read.
|
|
bool _promptTouched = false;
|
|
String _promptChoice = ChatPromptChooser.custom;
|
|
ChatCreatePrompt? _savedPrompt;
|
|
int _recall = 0;
|
|
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++;
|
|
_instructions.dispose();
|
|
_name.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 = chatDirectoryChoices(_directories).contains(_directory)
|
|
? _directory
|
|
: chatHomeDirectory;
|
|
_backend = _backends.contains(_backend)
|
|
? _backend
|
|
: _backends.contains('opencode')
|
|
? 'opencode'
|
|
: _backends.first;
|
|
_loading = false;
|
|
});
|
|
_choiceChanged();
|
|
} catch (error) {
|
|
if (!mounted || attempt != _attempt) return;
|
|
setState(() {
|
|
_catalogError = '$error';
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
bool get _catalogReady => !_loading && _catalogError == null;
|
|
|
|
/// A folder or harness pick: the default name follows it, and the prompt
|
|
/// choice last made there comes back.
|
|
Future<void> _choiceChanged() async {
|
|
final backend = _backend ?? '';
|
|
final directory = _directory ?? chatHomeDirectory;
|
|
if (!_nameEdited) {
|
|
_name.text = defaultChatName(directory, chatBackendLabel(backend));
|
|
}
|
|
final recall = ++_recall;
|
|
final saved = await _prompts.read(backend, directory);
|
|
if (!mounted || recall != _recall) return;
|
|
if (_promptTouched) return;
|
|
setState(() {
|
|
// Prompt text can be recalled, but sending it always requires an
|
|
// explicit choice in this new-chat dialog.
|
|
_sendPrompt = false;
|
|
_instructions.text = saved?.text ?? chatDefaultSessionInstructions;
|
|
_savedPrompt = saved;
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
});
|
|
}
|
|
|
|
void _selectPrompt(String choice) {
|
|
setState(() {
|
|
_promptTouched = true;
|
|
_promptChoice = choice;
|
|
if (choice == ChatPromptChooser.careful) {
|
|
_instructions.text = chatDefaultSessionInstructions;
|
|
_sendPrompt = true;
|
|
} else if (choice == ChatPromptChooser.lastUsed) {
|
|
_instructions.text = _savedPrompt!.text;
|
|
_sendPrompt = true;
|
|
} else {
|
|
_sendPrompt = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_catalogReady || _submitting) return;
|
|
final directory = _directory ?? chatHomeDirectory;
|
|
final backend = _backend ?? '';
|
|
setState(() {
|
|
_submitting = true;
|
|
_formError = null;
|
|
});
|
|
try {
|
|
final name = _name.text.trim();
|
|
final session = await widget.onSubmit(
|
|
ChatDraft(
|
|
title: name.isEmpty
|
|
? defaultChatName(directory, chatBackendLabel(backend))
|
|
: name,
|
|
directory: directory,
|
|
backend: backend,
|
|
instructions: _sendPrompt ? _instructions.text : '',
|
|
),
|
|
widget.operationId,
|
|
);
|
|
// Next time in this folder with this harness, the same choice.
|
|
unawaited(
|
|
_prompts.write(backend, directory, (
|
|
send: _sendPrompt,
|
|
text: _instructions.text,
|
|
)),
|
|
);
|
|
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);
|
|
_choiceChanged();
|
|
},
|
|
onBackend: (value) {
|
|
setState(() => _backend = value);
|
|
_choiceChanged();
|
|
},
|
|
onRetry: _loadCatalog,
|
|
),
|
|
const SizedBox(height: 12),
|
|
const Text('Name'),
|
|
const SizedBox(height: 4),
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
onChanged: (_) => _nameEdited = true,
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
onChanged: (_) => _nameEdited = true,
|
|
),
|
|
const SizedBox(height: 12),
|
|
ChatPromptChooser(
|
|
cupertino: cupertino,
|
|
enabled: _sendPrompt,
|
|
choice: _promptChoice,
|
|
hasLastUsed: _savedPrompt?.text.trim().isNotEmpty == true,
|
|
onEnabled: (value) => setState(() {
|
|
_promptTouched = true;
|
|
_sendPrompt = value;
|
|
}),
|
|
onChoice: _selectPrompt,
|
|
),
|
|
if (_sendPrompt) ...[
|
|
const SizedBox(height: 4),
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
minLines: 2,
|
|
maxLines: 4,
|
|
onChanged: (_) {
|
|
setState(() {
|
|
_promptTouched = true;
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
});
|
|
},
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
minLines: 2,
|
|
maxLines: 4,
|
|
onChanged: (_) {
|
|
setState(() {
|
|
_promptTouched = true;
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
});
|
|
},
|
|
),
|
|
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,
|
|
);
|
|
}
|
|
}
|