Merge commit '661dfc66'
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'chat_create_fields.dart';
|
||||
|
||||
/// Native platform controls for choosing what editable prompt to start with.
|
||||
class ChatPromptChooser extends StatelessWidget {
|
||||
const ChatPromptChooser({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.enabled,
|
||||
required this.choice,
|
||||
required this.hasLastUsed,
|
||||
required this.onEnabled,
|
||||
required this.onChoice,
|
||||
});
|
||||
|
||||
static const custom = 'custom';
|
||||
static const careful = 'careful';
|
||||
static const lastUsed = 'last-used';
|
||||
|
||||
final bool cupertino;
|
||||
final bool enabled;
|
||||
final String choice;
|
||||
final bool hasLastUsed;
|
||||
final ValueChanged<bool> onEnabled;
|
||||
final ValueChanged<String> onChoice;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final choices = <String>[custom, careful, if (hasLastUsed) lastUsed];
|
||||
String label(String value) => switch (value) {
|
||||
custom => 'Custom',
|
||||
careful => 'Careful coding',
|
||||
lastUsed => 'Last used prompt',
|
||||
_ => value,
|
||||
};
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Expanded(child: Text('Start with a prompt')),
|
||||
if (cupertino)
|
||||
CupertinoSwitch(
|
||||
key: const Key('chat-create-send-prompt'),
|
||||
value: enabled,
|
||||
onChanged: onEnabled,
|
||||
)
|
||||
else
|
||||
Switch(
|
||||
key: const Key('chat-create-send-prompt'),
|
||||
value: enabled,
|
||||
onChanged: onEnabled,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (enabled)
|
||||
if (cupertino)
|
||||
CupertinoPickerField(
|
||||
fieldKey: const Key('chat-create-prompt-choice'),
|
||||
label: 'Prompt',
|
||||
value: choices.contains(choice) ? choice : custom,
|
||||
values: choices,
|
||||
labelOf: label,
|
||||
onSelected: onChoice,
|
||||
)
|
||||
else
|
||||
MaterialPickerField(
|
||||
fieldKey: const Key('chat-create-prompt-choice'),
|
||||
label: 'Prompt',
|
||||
value: choices.contains(choice) ? choice : custom,
|
||||
values: choices,
|
||||
labelOf: label,
|
||||
onSelected: onChoice,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ 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 =
|
||||
@@ -91,10 +92,12 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
bool _nameEdited = false;
|
||||
|
||||
/// Whether the new chat starts with the prompt below.
|
||||
bool _sendPrompt = true;
|
||||
bool _sendPrompt = false;
|
||||
|
||||
/// The remembered prompt fills in only until the operator types one.
|
||||
bool _promptEdited = 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 [];
|
||||
@@ -168,10 +171,30 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
final recall = ++_recall;
|
||||
final saved = await _prompts.read(backend, directory);
|
||||
if (!mounted || recall != _recall) return;
|
||||
if (_promptEdited) return;
|
||||
if (_promptTouched) return;
|
||||
setState(() {
|
||||
_sendPrompt = saved?.send ?? true;
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -256,22 +279,16 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
onChanged: (_) => _nameEdited = true,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
const Expanded(child: Text('Start with a prompt')),
|
||||
if (cupertino)
|
||||
CupertinoSwitch(
|
||||
key: const Key('chat-create-send-prompt'),
|
||||
value: _sendPrompt,
|
||||
onChanged: (value) => setState(() => _sendPrompt = value),
|
||||
)
|
||||
else
|
||||
Switch(
|
||||
key: const Key('chat-create-send-prompt'),
|
||||
value: _sendPrompt,
|
||||
onChanged: (value) => setState(() => _sendPrompt = value),
|
||||
),
|
||||
],
|
||||
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),
|
||||
@@ -281,7 +298,12 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
onChanged: (_) => _promptEdited = true,
|
||||
onChanged: (_) {
|
||||
setState(() {
|
||||
_promptTouched = true;
|
||||
_promptChoice = ChatPromptChooser.custom;
|
||||
});
|
||||
},
|
||||
)
|
||||
else
|
||||
TextField(
|
||||
@@ -289,7 +311,12 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
onChanged: (_) => _promptEdited = true,
|
||||
onChanged: (_) {
|
||||
setState(() {
|
||||
_promptTouched = true;
|
||||
_promptChoice = ChatPromptChooser.custom;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(chatCreateInstructionsHelper),
|
||||
|
||||
Reference in New Issue
Block a user