407 lines
13 KiB
Dart
407 lines
13 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../data/chat_repository.dart';
|
|
import '../../domain/chat_sessions.dart';
|
|
|
|
/// Browses server-owned sessions. Opening a row is read-only at this layer.
|
|
class ChatSessionsScreen extends StatefulWidget {
|
|
const ChatSessionsScreen({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.onOpen,
|
|
});
|
|
final ApiGateway gateway;
|
|
final void Function(JsonMap session) onOpen;
|
|
|
|
@override
|
|
State<ChatSessionsScreen> createState() => _ChatSessionsScreenState();
|
|
}
|
|
|
|
class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
|
late ChatRepository _repository;
|
|
final _search = TextEditingController();
|
|
List<JsonMap> _sessions = [];
|
|
String? _error;
|
|
bool _loading = true;
|
|
int _generation = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_repository = ChatRepository(widget.gateway);
|
|
_refresh();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatSessionsScreen oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.gateway != widget.gateway) {
|
|
_repository = ChatRepository(widget.gateway);
|
|
_refresh();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_generation++;
|
|
_search.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _refresh() async {
|
|
final generation = ++_generation;
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
final sessions = await _repository.sessions();
|
|
if (!mounted || generation != _generation) return;
|
|
setState(() {
|
|
_sessions = sessions;
|
|
_loading = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted || generation != _generation) return;
|
|
setState(() {
|
|
_error = '$error';
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _mutate(
|
|
Future<JsonMap> Function(String) action,
|
|
String id,
|
|
) async {
|
|
try {
|
|
await action(id);
|
|
await _refresh();
|
|
} catch (error) {
|
|
if (mounted) setState(() => _error = '$error');
|
|
}
|
|
}
|
|
|
|
Future<void> _rename(JsonMap session) async {
|
|
final controller = TextEditingController(text: chatSessionTitle(session));
|
|
final title = await showDialog<String>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Rename chat'),
|
|
content: TextField(
|
|
controller: controller,
|
|
autofocus: true,
|
|
decoration: const InputDecoration(labelText: 'Chat name'),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(context, controller.text.trim()),
|
|
child: const Text('Save'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
controller.dispose();
|
|
if (title == null || title.trim().isEmpty) return;
|
|
try {
|
|
await _repository.rename('${session['id']}', title);
|
|
await _refresh();
|
|
} catch (error) {
|
|
if (mounted) setState(() => _error = '$error');
|
|
}
|
|
}
|
|
|
|
Future<void> _newChat() async {
|
|
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 backend,
|
|
String instructions,
|
|
})
|
|
>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('New chat'),
|
|
content: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: title,
|
|
decoration: const InputDecoration(labelText: 'Name'),
|
|
),
|
|
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(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(context, (
|
|
title: title.text.trim(),
|
|
directory: dirs.value,
|
|
backend: backend.value,
|
|
instructions: instructions.text,
|
|
)),
|
|
child: const Text('Create'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
title.dispose();
|
|
dirs.dispose();
|
|
backend.dispose();
|
|
instructions.dispose();
|
|
if (result == null) return;
|
|
if (result.title.isEmpty || result.directory.isEmpty) {
|
|
setState(() => _error = 'Enter a name and server directory.');
|
|
return;
|
|
}
|
|
try {
|
|
final reply = await _repository.create(
|
|
title: result.title,
|
|
directory: result.directory,
|
|
backend: result.backend,
|
|
operationId: 'native-chat-${DateTime.now().microsecondsSinceEpoch}',
|
|
instructions: result.instructions,
|
|
);
|
|
final session = reply['chat_session'];
|
|
if (session is Map) widget.onOpen(Map<String, dynamic>.from(session));
|
|
await _refresh();
|
|
} catch (error) {
|
|
if (mounted) setState(() => _error = '$error');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final visible = filterChatSessions(_sessions, _search.text);
|
|
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();
|
|
final closed = visible.where((s) => s['closed_at'] != null).toList();
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Chats'),
|
|
actions: [
|
|
IconButton(
|
|
onPressed: _newChat,
|
|
tooltip: 'New chat',
|
|
icon: const Icon(Icons.add),
|
|
),
|
|
IconButton(
|
|
onPressed: _refresh,
|
|
tooltip: 'Refresh',
|
|
icon: const Icon(Icons.refresh),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: TextField(
|
|
controller: _search,
|
|
onChanged: (_) => setState(() {}),
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.search),
|
|
labelText: 'Search chats',
|
|
),
|
|
),
|
|
),
|
|
if (_error != null)
|
|
MaterialBanner(
|
|
content: Text(_error!),
|
|
actions: [
|
|
TextButton(onPressed: _refresh, child: const Text('Retry')),
|
|
],
|
|
),
|
|
Expanded(
|
|
child: _loading && _sessions.isEmpty
|
|
? const Center(child: CircularProgressIndicator())
|
|
: ListView(
|
|
children: [
|
|
if (starred.isNotEmpty) _section('Starred', starred),
|
|
if (open.isNotEmpty) _section('Chats', open),
|
|
if (closed.isNotEmpty) _section('Closed', closed),
|
|
if (visible.isEmpty && !_loading)
|
|
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'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
onPressed: _newChat,
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('New chat'),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _section(String title, List<JsonMap> sessions) => ExpansionTile(
|
|
title: Text(title),
|
|
initiallyExpanded: true,
|
|
children: [
|
|
for (final group in groupChatSessions(sessions)) ...[
|
|
if (groupChatSessions(sessions).length > 1)
|
|
ListTile(
|
|
dense: true,
|
|
leading: const Icon(Icons.folder_outlined),
|
|
title: Text(
|
|
group.directory.isEmpty ? 'No directory' : group.directory,
|
|
),
|
|
),
|
|
for (final session in group.sessions) _row(session),
|
|
],
|
|
],
|
|
);
|
|
|
|
Widget _row(JsonMap session) {
|
|
final id = '${session['id'] ?? ''}';
|
|
final isClosed = session['closed_at'] != null;
|
|
final starred = session['starred'] == true;
|
|
return ListTile(
|
|
leading: Icon(
|
|
session['running'] == true
|
|
? Icons.sync
|
|
: starred
|
|
? Icons.star
|
|
: Icons.chat_bubble_outline,
|
|
),
|
|
title: Text(chatSessionTitle(session)),
|
|
subtitle: Text(
|
|
'${session['directory'] ?? ''}${session['running'] == true
|
|
? ' · Streaming'
|
|
: session['unseen'] == true
|
|
? ' · New'
|
|
: ''}',
|
|
),
|
|
onTap: () => widget.onOpen(session),
|
|
trailing: PopupMenuButton<String>(
|
|
tooltip: 'Chat actions',
|
|
onSelected: (value) {
|
|
if (value == 'rename') {
|
|
_rename(session);
|
|
}
|
|
if (value == 'star') {
|
|
_mutate((id) => _repository.star(id, !starred), id);
|
|
}
|
|
if (value == 'close') {
|
|
_mutate(_repository.close, id);
|
|
}
|
|
if (value == 'reopen') {
|
|
_mutate(_repository.reopen, id);
|
|
}
|
|
},
|
|
itemBuilder: (_) => [
|
|
const PopupMenuItem(value: 'rename', child: Text('Rename')),
|
|
PopupMenuItem(
|
|
value: 'star',
|
|
child: Text(starred ? 'Remove from Starred' : 'Star'),
|
|
),
|
|
if (isClosed)
|
|
const PopupMenuItem(value: 'reopen', child: Text('Reopen'))
|
|
else
|
|
const PopupMenuItem(value: 'close', child: Text('Close')),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|