323 lines
9.5 KiB
Dart
323 lines
9.5 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 {
|
|
final dirs = TextEditingController();
|
|
final title = TextEditingController(text: 'New chat');
|
|
final instructions = TextEditingController();
|
|
final result =
|
|
await showDialog<
|
|
({String title, String directory, 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',
|
|
),
|
|
),
|
|
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.text.trim(),
|
|
instructions: instructions.text,
|
|
)),
|
|
child: const Text('Create'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
title.dispose();
|
|
dirs.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,
|
|
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).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)
|
|
const ListTile(title: Text('No chats found.')),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
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 (group.directory.isNotEmpty)
|
|
ListTile(
|
|
dense: true,
|
|
leading: const Icon(Icons.folder_outlined),
|
|
title: Text(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')),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|