ae208699d8
Keep the Material-only session UI and narrow its tests to the supported host. Preserve public Cupertino parameters for callers while removing unreachable Cupertino widgets and variants.
398 lines
12 KiB
Dart
398 lines
12 KiB
Dart
import '../../../../core/servers/server_context.dart';
|
|
import 'dart:async';
|
|
|
|
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 '../../data/directory_listing.dart';
|
|
import '../../domain/chat_commands.dart';
|
|
import '../folder_picker/folder_picker_route.dart';
|
|
import 'chat_create_dialog_frame.dart';
|
|
import 'chat_create_fields.dart';
|
|
import 'chat_prompt_chooser.dart';
|
|
|
|
/// The instructions field's helper and the creation-time model behavior.
|
|
const String chatCreateInstructionsHelper =
|
|
"Preloaded as this chat's session instructions. The agent still reads the "
|
|
"repository's AGENTS.md and skills.";
|
|
const String chatCreateModelNote =
|
|
"New chats start with the server's default model.";
|
|
|
|
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 = ServerContext.forServer(
|
|
context,
|
|
serverIdentity(repository.gateway) ?? '',
|
|
CreateChatDialog(
|
|
repository: repository,
|
|
operationId: operationId,
|
|
onSubmit: onSubmit,
|
|
prompts: prompts,
|
|
),
|
|
);
|
|
return 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();
|
|
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;
|
|
String _customPrompt = '';
|
|
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;
|
|
bool get _promptReady => !_sendPrompt || _instructions.text.trim().isNotEmpty;
|
|
|
|
/// 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;
|
|
setState(() {
|
|
_savedPrompt = null;
|
|
if (_promptChoice == ChatPromptChooser.lastUsed) {
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
_customPrompt = _instructions.text;
|
|
}
|
|
});
|
|
final saved = await _prompts.read(backend, directory);
|
|
if (!mounted || recall != _recall) return;
|
|
setState(() {
|
|
_savedPrompt = saved;
|
|
if (!_promptTouched) {
|
|
_sendPrompt = false;
|
|
_instructions.clear();
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
}
|
|
});
|
|
}
|
|
|
|
void _selectPrompt(String choice) {
|
|
if (!mounted ||
|
|
_submitting ||
|
|
(choice == ChatPromptChooser.lastUsed && _savedPrompt == null)) {
|
|
return;
|
|
}
|
|
setState(() {
|
|
if (_promptChoice == ChatPromptChooser.custom) {
|
|
_customPrompt = _instructions.text;
|
|
}
|
|
_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 {
|
|
_instructions.text = _customPrompt;
|
|
_sendPrompt = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Opens the folder picker at the current folder when it is inside home,
|
|
/// with the server's recent chat folders as quick picks, and makes the
|
|
/// chosen folder this chat's directory.
|
|
Future<void> _browse() async {
|
|
final home = _directories.isEmpty ? null : _directories.first;
|
|
final current = _directory;
|
|
final initial =
|
|
home != null &&
|
|
current != null &&
|
|
current != chatHomeDirectory &&
|
|
isInsideHome(home, current)
|
|
? current
|
|
: null;
|
|
final chosen = await showFolderPicker(
|
|
context,
|
|
gateway: widget.repository.gateway,
|
|
initialPath: initial,
|
|
recents: _directories.skip(1).toList(),
|
|
);
|
|
if (!mounted || chosen == null || _submitting) return;
|
|
setState(() {
|
|
if (chosen == home) {
|
|
_directory = chatHomeDirectory;
|
|
} else {
|
|
if (!_directories.contains(chosen)) {
|
|
_directories = [
|
|
..._directories.take(1),
|
|
chosen,
|
|
..._directories.skip(1),
|
|
];
|
|
}
|
|
_directory = chosen;
|
|
}
|
|
});
|
|
_choiceChanged();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_catalogReady || _submitting) return;
|
|
if (!_promptReady) {
|
|
setState(() => _formError = 'Enter a prompt or choose a pre-filled one.');
|
|
return;
|
|
}
|
|
final directory = _directory ?? chatHomeDirectory;
|
|
final backend = _backend ?? '';
|
|
final sendPrompt = _sendPrompt;
|
|
final promptText = _instructions.text;
|
|
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 ? promptText : '',
|
|
),
|
|
widget.operationId,
|
|
);
|
|
// Only an actually sent prompt becomes Last used. Starting a promptless
|
|
// chat must not erase a saved preset (or wait for its pending read).
|
|
if (sendPrompt) {
|
|
unawaited(
|
|
_prompts.write(backend, directory, (
|
|
send: sendPrompt,
|
|
text: promptText,
|
|
)),
|
|
);
|
|
}
|
|
if (!mounted) return;
|
|
Navigator.pop(context, session);
|
|
} catch (error) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_submitting = false;
|
|
_formError = '$error';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
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,
|
|
onBrowse: _submitting ? null : _browse,
|
|
),
|
|
const SizedBox(height: 12),
|
|
const Text('Name'),
|
|
const SizedBox(height: 4),
|
|
TextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
onChanged: (_) => _nameEdited = true,
|
|
),
|
|
const SizedBox(height: 12),
|
|
ChatPromptChooser(
|
|
cupertino: false,
|
|
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),
|
|
TextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
minLines: 2,
|
|
maxLines: 4,
|
|
decoration: const InputDecoration(
|
|
hintText: 'Write your custom prompt',
|
|
),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_promptTouched = true;
|
|
_promptChoice = ChatPromptChooser.custom;
|
|
_customPrompt = value;
|
|
});
|
|
},
|
|
),
|
|
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: Theme.of(context).colorScheme.error),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
|
|
return ChatCreateDialogFrame(
|
|
content: content,
|
|
canSubmit: _catalogReady && _promptReady,
|
|
submitting: _submitting,
|
|
onCancel: () => Navigator.pop(context),
|
|
onSubmit: _submit,
|
|
);
|
|
}
|
|
}
|