Improve chat session catalog creation
This commit is contained in:
@@ -39,17 +39,27 @@ class ChatRepository {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
}))['chat_shells'],
|
||||
);
|
||||
Future<List<String>> directories() async =>
|
||||
((await gateway.call('chat_directories'))['chat_directories'] as List? ??
|
||||
const [])
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
Future<List<String>> directories() async {
|
||||
final value = (await gateway.call('chat_directories'))['chat_directories'];
|
||||
if (value is! List)
|
||||
throw StateError('Server did not return chat directories.');
|
||||
return value
|
||||
.whereType<String>()
|
||||
.where((entry) => entry.trim().isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<List<String>> backends() async =>
|
||||
((await gateway.call('chat_backends'))['chat_backends'] as List? ??
|
||||
const [])
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
Future<List<String>> backends() async {
|
||||
final value = (await gateway.call('chat_backends'))['chat_backends'];
|
||||
if (value is! List)
|
||||
throw StateError('Server did not return chat backends.');
|
||||
final result = value
|
||||
.whereType<String>()
|
||||
.where((entry) => entry.trim().isNotEmpty)
|
||||
.toList();
|
||||
if (result.isEmpty) throw StateError('Server returned no chat backends.');
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<JsonMap> shellOutput(
|
||||
String sessionId,
|
||||
|
||||
@@ -117,34 +117,95 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
}
|
||||
|
||||
Future<void> _newChat() async {
|
||||
final dirs = TextEditingController();
|
||||
List<String> directories;
|
||||
List<String> backends;
|
||||
try {
|
||||
final catalog = await Future.wait([
|
||||
_repository.directories(),
|
||||
_repository.backends(),
|
||||
]);
|
||||
directories = catalog[0];
|
||||
backends = catalog[1];
|
||||
} catch (error) {
|
||||
if (mounted)
|
||||
setState(() => _error = 'Unable to load chat options: $error');
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (directories.isEmpty || backends.isEmpty) {
|
||||
setState(
|
||||
() => _error =
|
||||
'The server has no chat directories or backends available.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
final dirs = ValueNotifier<String>(directories.first);
|
||||
final backend = ValueNotifier<String>(backends.first);
|
||||
final title = TextEditingController(text: 'New chat');
|
||||
final instructions = TextEditingController();
|
||||
final result =
|
||||
await showDialog<
|
||||
({String title, String directory, String instructions})
|
||||
({
|
||||
String title,
|
||||
String directory,
|
||||
String backend,
|
||||
String instructions,
|
||||
})
|
||||
>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('New chat'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: title,
|
||||
decoration: const InputDecoration(labelText: 'Name'),
|
||||
),
|
||||
TextField(
|
||||
controller: dirs,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server directory',
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: title,
|
||||
decoration: const InputDecoration(labelText: 'Name'),
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: instructions,
|
||||
decoration: const InputDecoration(labelText: 'Instructions'),
|
||||
),
|
||||
],
|
||||
ValueListenableBuilder<String>(
|
||||
valueListenable: dirs,
|
||||
builder: (context, value, _) =>
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: value,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server directory',
|
||||
),
|
||||
items: [
|
||||
for (final item in directories)
|
||||
DropdownMenuItem(value: item, child: Text(item)),
|
||||
],
|
||||
onChanged: (item) {
|
||||
if (item != null) dirs.value = item;
|
||||
},
|
||||
),
|
||||
),
|
||||
ValueListenableBuilder<String>(
|
||||
valueListenable: backend,
|
||||
builder: (context, value, _) =>
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: value,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Backend',
|
||||
),
|
||||
items: [
|
||||
for (final item in backends)
|
||||
DropdownMenuItem(value: item, child: Text(item)),
|
||||
],
|
||||
onChanged: (item) {
|
||||
if (item != null) backend.value = item;
|
||||
},
|
||||
),
|
||||
),
|
||||
const Text('New chats use the server default model.'),
|
||||
TextField(
|
||||
controller: instructions,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Instructions',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
@@ -154,7 +215,8 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, (
|
||||
title: title.text.trim(),
|
||||
directory: dirs.text.trim(),
|
||||
directory: dirs.value,
|
||||
backend: backend.value,
|
||||
instructions: instructions.text,
|
||||
)),
|
||||
child: const Text('Create'),
|
||||
@@ -164,6 +226,7 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
);
|
||||
title.dispose();
|
||||
dirs.dispose();
|
||||
backend.dispose();
|
||||
instructions.dispose();
|
||||
if (result == null) return;
|
||||
if (result.title.isEmpty || result.directory.isEmpty) {
|
||||
@@ -174,6 +237,7 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
final reply = await _repository.create(
|
||||
title: result.title,
|
||||
directory: result.directory,
|
||||
backend: result.backend,
|
||||
operationId: 'native-chat-${DateTime.now().microsecondsSinceEpoch}',
|
||||
instructions: result.instructions,
|
||||
);
|
||||
@@ -188,7 +252,9 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final visible = filterChatSessions(_sessions, _search.text);
|
||||
final starred = visible.where((s) => s['starred'] == true).toList();
|
||||
final starred = visible
|
||||
.where((s) => s['starred'] == true && s['closed_at'] == null)
|
||||
.toList();
|
||||
final open = visible
|
||||
.where((s) => s['starred'] != true && s['closed_at'] == null)
|
||||
.toList();
|
||||
@@ -238,7 +304,22 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
if (open.isNotEmpty) _section('Chats', open),
|
||||
if (closed.isNotEmpty) _section('Closed', closed),
|
||||
if (visible.isEmpty && !_loading)
|
||||
const ListTile(title: Text('No chats found.')),
|
||||
ListTile(
|
||||
title: Text(
|
||||
_sessions.isEmpty
|
||||
? 'No chats yet.'
|
||||
: 'No chats match "${_search.text.trim()}".',
|
||||
),
|
||||
trailing: _search.text.isEmpty
|
||||
? null
|
||||
: TextButton(
|
||||
onPressed: () {
|
||||
_search.clear();
|
||||
setState(() {});
|
||||
},
|
||||
child: const Text('Clear search'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -257,11 +338,13 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
initiallyExpanded: true,
|
||||
children: [
|
||||
for (final group in groupChatSessions(sessions)) ...[
|
||||
if (group.directory.isNotEmpty)
|
||||
if (groupChatSessions(sessions).length > 1)
|
||||
ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(group.directory),
|
||||
title: Text(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
),
|
||||
),
|
||||
for (final session in group.sessions) _row(session),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user