feat(app): new chats are named after folder and harness; the first prompt is optional and remembered per harness and folder
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Whether a new chat starts with a prompt, and which, for one harness and
|
||||
/// directory: the last choice made there.
|
||||
typedef ChatCreatePrompt = ({bool send, String text});
|
||||
|
||||
abstract interface class ChatCreatePromptStore {
|
||||
Future<ChatCreatePrompt?> read(String backend, String directory);
|
||||
|
||||
Future<void> write(String backend, String directory, ChatCreatePrompt value);
|
||||
}
|
||||
|
||||
class SharedPreferencesChatCreatePromptStore implements ChatCreatePromptStore {
|
||||
const SharedPreferencesChatCreatePromptStore();
|
||||
|
||||
static String _key(String backend, String directory) =>
|
||||
'pipelines.chat.createPrompt.v1.$backend|$directory';
|
||||
|
||||
@override
|
||||
Future<ChatCreatePrompt?> read(String backend, String directory) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_key(backend, directory));
|
||||
if (raw == null) return null;
|
||||
final data = jsonDecode(raw);
|
||||
if (data is! Map || data['send'] is! bool || data['text'] is! String) {
|
||||
return null;
|
||||
}
|
||||
return (send: data['send'] as bool, text: data['text'] as String);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> write(
|
||||
String backend,
|
||||
String directory,
|
||||
ChatCreatePrompt value,
|
||||
) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(
|
||||
_key(backend, directory),
|
||||
jsonEncode({'send': value.send, 'text': value.text}),
|
||||
);
|
||||
} catch (_) {
|
||||
// Remembering is a convenience; the chat is still created.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The default chat name: the directory's last folder and the harness, as in
|
||||
/// "Pipelines Codex".
|
||||
String defaultChatName(String directory, String harnessLabel) {
|
||||
final parts = directory
|
||||
.split('/')
|
||||
.where((part) => part.isNotEmpty && part != '~')
|
||||
.toList();
|
||||
final leaf = parts.isEmpty ? 'Home' : parts.last;
|
||||
final folder = leaf.isEmpty
|
||||
? 'Home'
|
||||
: '${leaf[0].toUpperCase()}${leaf.substring(1)}';
|
||||
return '$folder $harnessLabel';
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../data/chat_create_prompt_store.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../domain/chat_commands.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
@@ -39,11 +42,14 @@ Future<JsonMap?> showChatCreateDialog(
|
||||
required String operationId,
|
||||
required Future<JsonMap> Function(ChatDraft draft, String operationId)
|
||||
onSubmit,
|
||||
ChatCreatePromptStore prompts =
|
||||
const SharedPreferencesChatCreatePromptStore(),
|
||||
}) {
|
||||
final dialog = CreateChatDialog(
|
||||
repository: repository,
|
||||
operationId: operationId,
|
||||
onSubmit: onSubmit,
|
||||
prompts: prompts,
|
||||
);
|
||||
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? showCupertinoDialog<JsonMap>(context: context, builder: (_) => dialog)
|
||||
@@ -56,8 +62,12 @@ class CreateChatDialog extends StatefulWidget {
|
||||
required this.repository,
|
||||
required this.operationId,
|
||||
required this.onSubmit,
|
||||
this.prompts = const SharedPreferencesChatCreatePromptStore(),
|
||||
});
|
||||
|
||||
/// 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;
|
||||
@@ -70,6 +80,17 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
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 = true;
|
||||
|
||||
/// The remembered prompt fills in only until the operator types one.
|
||||
bool _promptEdited = false;
|
||||
int _recall = 0;
|
||||
List<String> _directories = const [];
|
||||
List<String> _backends = const [];
|
||||
String? _directory;
|
||||
@@ -90,6 +111,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
void dispose() {
|
||||
_attempt++;
|
||||
_instructions.dispose();
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -118,6 +140,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
: _backends.first;
|
||||
_loading = false;
|
||||
});
|
||||
_choiceChanged();
|
||||
} catch (error) {
|
||||
if (!mounted || attempt != _attempt) return;
|
||||
setState(() {
|
||||
@@ -129,23 +152,52 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
|
||||
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 widget.prompts.read(backend, directory);
|
||||
if (!mounted || recall != _recall) return;
|
||||
if (_promptEdited) return;
|
||||
setState(() {
|
||||
_sendPrompt = saved?.send ?? true;
|
||||
_instructions.text = saved?.text ?? chatDefaultSessionInstructions;
|
||||
});
|
||||
}
|
||||
|
||||
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: chatNewSessionTitle,
|
||||
title: name.isEmpty
|
||||
? defaultChatName(directory, chatBackendLabel(backend))
|
||||
: name,
|
||||
directory: directory,
|
||||
backend: _backend ?? '',
|
||||
instructions: _instructions.text,
|
||||
backend: backend,
|
||||
instructions: _sendPrompt ? _instructions.text : '',
|
||||
),
|
||||
widget.operationId,
|
||||
);
|
||||
// Next time in this folder with this harness, the same choice.
|
||||
unawaited(
|
||||
widget.prompts.write(backend, directory, (
|
||||
send: _sendPrompt,
|
||||
text: _instructions.text,
|
||||
)),
|
||||
);
|
||||
if (!mounted) return;
|
||||
Navigator.pop(context, session);
|
||||
} catch (error) {
|
||||
@@ -173,29 +225,70 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
backends: _backends,
|
||||
directory: _directory,
|
||||
backend: _backend,
|
||||
onDirectory: (value) => setState(() => _directory = value),
|
||||
onBackend: (value) => setState(() => _backend = value),
|
||||
onDirectory: (value) {
|
||||
setState(() => _directory = value);
|
||||
_choiceChanged();
|
||||
},
|
||||
onBackend: (value) {
|
||||
setState(() => _backend = value);
|
||||
_choiceChanged();
|
||||
},
|
||||
onRetry: _loadCatalog,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text('How should it act?'),
|
||||
const Text('Name'),
|
||||
const SizedBox(height: 4),
|
||||
if (cupertino)
|
||||
CupertinoTextField(
|
||||
key: const Key('chat-create-instructions'),
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
key: const Key('chat-create-name'),
|
||||
controller: _name,
|
||||
onChanged: (_) => _nameEdited = true,
|
||||
)
|
||||
else
|
||||
TextField(
|
||||
key: const Key('chat-create-instructions'),
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
key: const Key('chat-create-name'),
|
||||
controller: _name,
|
||||
onChanged: (_) => _nameEdited = true,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(chatCreateInstructionsHelper),
|
||||
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),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_sendPrompt) ...[
|
||||
const SizedBox(height: 4),
|
||||
if (cupertino)
|
||||
CupertinoTextField(
|
||||
key: const Key('chat-create-instructions'),
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
onChanged: (_) => _promptEdited = true,
|
||||
)
|
||||
else
|
||||
TextField(
|
||||
key: const Key('chat-create-instructions'),
|
||||
controller: _instructions,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
onChanged: (_) => _promptEdited = true,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(chatCreateInstructionsHelper),
|
||||
],
|
||||
if (_catalogReady) ...[
|
||||
const SizedBox(height: 8),
|
||||
const Text(chatCreateModelNote),
|
||||
|
||||
@@ -66,7 +66,8 @@ void main() {
|
||||
|
||||
final fields =
|
||||
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
|
||||
expect(fields['title'], 'New chat');
|
||||
// Named after the folder and harness by default.
|
||||
expect(fields['title'], 'Home OpenCode');
|
||||
expect(fields['directory'], '~');
|
||||
expect(fields.containsKey('backend'), isFalse);
|
||||
expect(fields['instructions'], 'Keep me');
|
||||
@@ -169,7 +170,7 @@ void main() {
|
||||
|
||||
final fields =
|
||||
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
|
||||
expect(fields['title'], 'New chat');
|
||||
expect(fields['title'], 'Archive Codex');
|
||||
expect(fields['directory'], '/archive');
|
||||
expect(fields['backend'], 'codex');
|
||||
expect(fields['instructions'], 'Be careful');
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_create_prompt_store.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/create_chat_dialog.dart';
|
||||
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
class _MemoryPrompts implements ChatCreatePromptStore {
|
||||
final Map<String, ChatCreatePrompt> saved = {};
|
||||
|
||||
@override
|
||||
Future<ChatCreatePrompt?> read(String backend, String directory) async =>
|
||||
saved['$backend|$directory'];
|
||||
|
||||
@override
|
||||
Future<void> write(
|
||||
String backend,
|
||||
String directory,
|
||||
ChatCreatePrompt value,
|
||||
) async => saved['$backend|$directory'] = value;
|
||||
}
|
||||
|
||||
Future<ChatDraft?> _create(
|
||||
WidgetTester tester,
|
||||
_MemoryPrompts prompts, {
|
||||
bool turnOff = false,
|
||||
String? prompt,
|
||||
}) async {
|
||||
ChatDraft? sent;
|
||||
final gateway = FakeChatGateway()
|
||||
..directoriesHandler = () async => {
|
||||
'chat_directories': ['/work/pipelines'],
|
||||
};
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CreateChatDialog(
|
||||
repository: ChatRepository(gateway),
|
||||
operationId: 'op',
|
||||
prompts: prompts,
|
||||
onSubmit: (draft, _) async {
|
||||
sent = draft;
|
||||
return {'id': 'new'};
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
if (prompt != null) {
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-create-instructions')),
|
||||
prompt,
|
||||
);
|
||||
}
|
||||
if (turnOff) {
|
||||
await tester.tap(find.byKey(const Key('chat-create-send-prompt')));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
await tester.ensureVisible(find.byKey(const Key('chat-create-confirm')));
|
||||
await tester.tap(find.byKey(const Key('chat-create-confirm')));
|
||||
await tester.pumpAndSettle();
|
||||
return sent;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('the default name is the folder and the harness', () {
|
||||
expect(
|
||||
defaultChatName('/Users/me/repos/pipelines', 'Codex'),
|
||||
'Pipelines Codex',
|
||||
);
|
||||
expect(defaultChatName('~', 'OpenCode'), 'Home OpenCode');
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'a custom prompt is sent and comes back for that harness and folder',
|
||||
(tester) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
final first = await _create(tester, prompts, prompt: 'Review only.');
|
||||
expect(first!.instructions, 'Review only.');
|
||||
expect(prompts.saved.values.single, (send: true, text: 'Review only.'));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
final second = await _create(tester, prompts);
|
||||
expect(second!.instructions, 'Review only.');
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a chat can start without a prompt, and that is remembered', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
final draft = await _create(tester, prompts, turnOff: true);
|
||||
expect(draft!.instructions, isEmpty);
|
||||
expect(prompts.saved.values.single.send, isFalse);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await _create(tester, prompts);
|
||||
expect(prompts.saved.values.single.send, isFalse);
|
||||
});
|
||||
}
|
||||
@@ -46,7 +46,7 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(bodies, hasLength(2));
|
||||
expect(bodies[1]['operation_id'], bodies[0]['operation_id']);
|
||||
expect(bodies[1]['title'], 'New chat');
|
||||
expect(bodies[1]['title'], 'Home OpenCode');
|
||||
expect(find.byKey(const Key('chat-create-instructions')), findsNothing);
|
||||
expect(opened?['id'], 'created');
|
||||
expect(gateway.count('chat_sessions'), greaterThanOrEqualTo(2));
|
||||
|
||||
Reference in New Issue
Block a user