feat: a new-session draft and a sidebar grouped by project
- Starting a session opens NewSessionScreen instead of a title sheet: a project chip (GET /api/project, one row per canonical), a model/agent chip, attachments and the composer. Nothing is created on the server until the first send, which posts the session with its directory, model and agent and then sends that message. - The draft lives in AppState (memory only): leaving it keeps it; the sidebar shows a draft row with its first line and a discard button; a failed create stays on the draft with its text. - The session list groups by project (chat folders under 聊天), dates the rows (今天/昨天/本周早些时候/更早), pins through session.metadata.openchamber.pinned, archives on demand, pages with "载入更多", and swipes to pin/archive/delete (flutter_slidable). - Child sessions are hidden; a parent's row shows its children's activity. - The chat app bar follows the live session list, so the title the server generates from the first message appears at once. - Attachments and the model picker move to shared widgets, used by the chat and the draft. - 0.3.13 (313).
This commit is contained in:
1 parent
5a371e4faf
commit
fff6562cbc
14 files changed
+1775
-366
No files matched your search
+335
-27
@@ -49,6 +49,28 @@ const _eventTypes = [
|
||||
'permission.replied',
|
||||
];
|
||||
|
||||
/// A new session the user started but has not sent yet. Nothing exists on the
|
||||
/// server until the first message creates the session.
|
||||
class SessionDraft {
|
||||
String directory;
|
||||
ModelRef? model;
|
||||
String agent;
|
||||
String text = '';
|
||||
int attachments = 0;
|
||||
String? error;
|
||||
|
||||
SessionDraft({required this.directory, this.model, this.agent = ''});
|
||||
|
||||
bool get isEmpty => text.trim().isEmpty && attachments == 0;
|
||||
|
||||
String get firstLine {
|
||||
for (final line in text.split('\n')) {
|
||||
if (line.trim().isNotEmpty) return line.trim();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
class AppState extends ChangeNotifier {
|
||||
final Storage storage = Storage();
|
||||
|
||||
@@ -68,6 +90,27 @@ class AppState extends ChangeNotifier {
|
||||
|
||||
ChatState? chat;
|
||||
|
||||
/// Projects the server knows, for the new-session picker and the sidebar's
|
||||
/// group headers.
|
||||
List<ProjectInfo> projects = [];
|
||||
|
||||
/// The unsent new-session draft, kept in memory. Null when there is none.
|
||||
SessionDraft? draft;
|
||||
bool sendingDraft = false;
|
||||
|
||||
/// Whether the shell is showing the draft. A draft can exist while another
|
||||
/// session is open (its row waits in the sidebar).
|
||||
bool draftOpen = false;
|
||||
|
||||
/// Archived sessions, fetched only when the sidebar asks for them.
|
||||
List<Session> archivedSessions = [];
|
||||
bool archivedLoading = false;
|
||||
bool archivedLoaded = false;
|
||||
|
||||
/// Where the next page of the session list starts.
|
||||
String? _sessionsNext;
|
||||
bool loadingMoreSessions = false;
|
||||
|
||||
/// Terminals by workspace directory: switching sessions or tabs keeps the
|
||||
/// shell alive, and each directory gets its own.
|
||||
final Map<String, TerminalSessionState> terminals = {};
|
||||
@@ -179,6 +222,7 @@ class AppState extends ChangeNotifier {
|
||||
storage.saveConnections(list);
|
||||
_startStream();
|
||||
refreshSessions();
|
||||
refreshProjects();
|
||||
refreshActive();
|
||||
refreshPerms();
|
||||
refreshModels();
|
||||
@@ -197,7 +241,14 @@ class AppState extends ChangeNotifier {
|
||||
client = null;
|
||||
conn = null;
|
||||
chat = null;
|
||||
draft = null;
|
||||
draftOpen = false;
|
||||
sendingDraft = false;
|
||||
sessions = [];
|
||||
projects = [];
|
||||
archivedSessions = [];
|
||||
archivedLoaded = false;
|
||||
_sessionsNext = null;
|
||||
perms = [];
|
||||
error = null;
|
||||
notifyListeners();
|
||||
@@ -220,8 +271,9 @@ class AppState extends ChangeNotifier {
|
||||
sessionsLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
final page = await cl.listSessions();
|
||||
final page = await cl.listSessions(archived: false);
|
||||
sessions = page.data;
|
||||
_sessionsNext = page.next;
|
||||
sessionsLoading = false;
|
||||
} catch (e) {
|
||||
sessionsLoading = false;
|
||||
@@ -236,8 +288,11 @@ class AppState extends ChangeNotifier {
|
||||
sessionsLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
final page = await cl.listSessions(search: q);
|
||||
final page = await cl.listSessions(search: q, archived: false);
|
||||
sessions = page.data;
|
||||
// Search results are one page; "load more" would append unsearched
|
||||
// sessions.
|
||||
_sessionsNext = null;
|
||||
sessionsLoading = false;
|
||||
} catch (_) {
|
||||
sessionsLoading = false;
|
||||
@@ -245,6 +300,248 @@ class AppState extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> refreshProjects() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
try {
|
||||
projects = await cl.listProjects();
|
||||
notifyListeners();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// Loads the next (older) page of the session list.
|
||||
Future<void> loadMoreSessions() async {
|
||||
final cl = client;
|
||||
final cursor = _sessionsNext;
|
||||
if (cl == null || cursor == null || loadingMoreSessions) return;
|
||||
loadingMoreSessions = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
final page = await cl.listSessions(cursor: cursor, archived: false);
|
||||
final known = sessions.map((s) => s.id).toSet();
|
||||
sessions = [
|
||||
...sessions,
|
||||
...page.data.where((s) => !known.contains(s.id)),
|
||||
];
|
||||
_sessionsNext = page.next;
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
} finally {
|
||||
loadingMoreSessions = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshArchived() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
archivedLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
final page = await cl.listSessions(archived: true, limit: 100);
|
||||
archivedSessions = page.data;
|
||||
archivedLoaded = true;
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
} finally {
|
||||
archivedLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
bool get hasMoreSessions => _sessionsNext != null;
|
||||
|
||||
String projectLabelFor(String directory) {
|
||||
if (directory.isEmpty) return '';
|
||||
for (final project in projects) {
|
||||
if (project.canonical == directory) return project.label;
|
||||
}
|
||||
final trimmed = directory.replaceAll(RegExp(r'/+$'), '');
|
||||
final index = trimmed.lastIndexOf('/');
|
||||
return index >= 0 ? trimmed.substring(index + 1) : trimmed;
|
||||
}
|
||||
|
||||
ProjectInfo? projectFor(String directory) {
|
||||
for (final project in projects) {
|
||||
if (project.canonical == directory) return project;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Pins live in the session's metadata, so every client sees them.
|
||||
Future<void> togglePin(Session session) async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
final metadata = Map<String, dynamic>.from(session.metadata);
|
||||
final openchamber = metadata['openchamber'] is Map
|
||||
? Map<String, dynamic>.from(metadata['openchamber'] as Map)
|
||||
: <String, dynamic>{};
|
||||
if (session.isPinned) {
|
||||
openchamber.remove('pinned');
|
||||
} else {
|
||||
openchamber['pinned'] = {
|
||||
'at': DateTime.now().millisecondsSinceEpoch,
|
||||
};
|
||||
}
|
||||
if (openchamber.isEmpty) {
|
||||
metadata.remove('openchamber');
|
||||
} else {
|
||||
metadata['openchamber'] = openchamber;
|
||||
}
|
||||
try {
|
||||
await cl.updateSession(session.id, metadata: metadata);
|
||||
_patchSession(session.id, (s) => s.copyWith(metadata: metadata));
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setArchived(Session session, bool archived) async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
try {
|
||||
await cl.updateSession(session.id, archived: archived);
|
||||
if (archived) {
|
||||
sessions.removeWhere((s) => s.id == session.id);
|
||||
if (archivedLoaded) {
|
||||
archivedSessions.insert(
|
||||
0,
|
||||
session.copyWith(
|
||||
archived: DateTime.now().millisecondsSinceEpoch));
|
||||
}
|
||||
} else {
|
||||
archivedSessions.removeWhere((s) => s.id == session.id);
|
||||
final restored = session.copyWith(archived: 0);
|
||||
if (!sessions.any((s) => s.id == restored.id)) {
|
||||
sessions.insert(0, restored);
|
||||
}
|
||||
_sortSessions();
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void _sortSessions() =>
|
||||
sessions.sort((a, b) => b.updated.compareTo(a.updated));
|
||||
|
||||
// ------------------------------------------------------------------ draft
|
||||
|
||||
/// Opens (or reopens) the single in-memory draft. A non-empty draft wins
|
||||
/// over the directory a caller asks for; an empty one takes the directory.
|
||||
SessionDraft openDraft({String? directory}) {
|
||||
final current = draft;
|
||||
if (current != null) {
|
||||
if (current.isEmpty &&
|
||||
directory != null &&
|
||||
directory.isNotEmpty &&
|
||||
directory != current.directory) {
|
||||
current.directory = directory;
|
||||
}
|
||||
notifyListeners();
|
||||
return current;
|
||||
}
|
||||
final fallback = directory ??
|
||||
chat?.session.directory ??
|
||||
(sessions.isNotEmpty ? sessions.first.directory : '');
|
||||
final latest = sessions.isNotEmpty ? sessions.first : null;
|
||||
final dir = fallback.isNotEmpty
|
||||
? fallback
|
||||
: (projects.isNotEmpty ? projects.first.canonical : '');
|
||||
final created = SessionDraft(
|
||||
directory: dir,
|
||||
model: chat?.selectedModel ?? latest?.model,
|
||||
agent: (chat != null && chat!.selectedAgent.isNotEmpty)
|
||||
? chat!.selectedAgent
|
||||
: (latest?.agent ?? ''),
|
||||
);
|
||||
draft = created;
|
||||
draftOpen = true;
|
||||
notifyListeners();
|
||||
return created;
|
||||
}
|
||||
|
||||
/// Brings the draft back on screen without touching what it holds.
|
||||
void showDraft() {
|
||||
if (draft == null) return;
|
||||
draftOpen = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void discardDraft() {
|
||||
if (draft == null) return;
|
||||
draft = null;
|
||||
draftOpen = false;
|
||||
sendingDraft = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateDraftText(String text) {
|
||||
final current = draft;
|
||||
if (current == null) return;
|
||||
current.text = text;
|
||||
// The sidebar's draft row shows the first line; a rebuild per keystroke is
|
||||
// cheap at this size.
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateDraftTarget(String directory) {
|
||||
final current = draft;
|
||||
if (current == null) return;
|
||||
current.directory = directory;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateDraftModel(ModelRef? model, {String? agent}) {
|
||||
final current = draft;
|
||||
if (current == null) return;
|
||||
if (model != null) current.model = model;
|
||||
if (agent != null) current.agent = agent;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// The first send is what creates the session: until then nothing exists on
|
||||
/// the server, so an untouched draft never leaves an empty session behind.
|
||||
Future<void> sendDraft({
|
||||
required String text,
|
||||
List<Map<String, String>>? files,
|
||||
List<String> images = const [],
|
||||
}) async {
|
||||
final current = draft;
|
||||
final cl = client;
|
||||
if (current == null || cl == null || sendingDraft) return;
|
||||
current.error = null;
|
||||
sendingDraft = true;
|
||||
notifyListeners();
|
||||
final Session created;
|
||||
try {
|
||||
created = await cl.createSession(
|
||||
directory: current.directory,
|
||||
model: current.model,
|
||||
agent: current.agent,
|
||||
);
|
||||
} catch (e) {
|
||||
sendingDraft = false;
|
||||
current.error = e is ApiError ? e.message : '$e';
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
draft = null;
|
||||
sendingDraft = false;
|
||||
if (!sessions.any((s) => s.id == created.id)) {
|
||||
sessions.insert(0, created);
|
||||
}
|
||||
await openChat(created);
|
||||
final id = await chat?.send(text: text, files: files);
|
||||
if (id != null && id.isNotEmpty && images.isNotEmpty) {
|
||||
chat!.imagePaths[id] = images;
|
||||
chat!.touch();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshActive() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
@@ -383,14 +680,7 @@ class AppState extends ChangeNotifier {
|
||||
final id = data['sessionID'] as String?;
|
||||
final title = data['title'] as String?;
|
||||
if (id == null || title == null) return;
|
||||
_patchSession(id, (s) => Session(
|
||||
id: s.id,
|
||||
title: title,
|
||||
agent: s.agent,
|
||||
model: s.model,
|
||||
location: s.location,
|
||||
created: s.created,
|
||||
updated: s.updated));
|
||||
_patchSession(id, (s) => s.copyWith(title: title));
|
||||
break;
|
||||
case 'session.model.selected':
|
||||
final id = data['sessionID'] as String?;
|
||||
@@ -398,34 +688,51 @@ class AppState extends ChangeNotifier {
|
||||
? null
|
||||
: ModelRef.fromJson(data['model'] as Map<String, dynamic>);
|
||||
if (id == null) return;
|
||||
_patchSession(id, (s) => Session(
|
||||
id: s.id,
|
||||
title: s.title,
|
||||
agent: s.agent,
|
||||
model: model,
|
||||
location: s.location,
|
||||
created: s.created,
|
||||
updated: s.updated));
|
||||
_patchSession(id, (s) => s.copyWith(model: model));
|
||||
break;
|
||||
case 'session.agent.selected':
|
||||
final id = data['sessionID'] as String?;
|
||||
final agent = data['agent'] as String?;
|
||||
if (id == null || agent == null) return;
|
||||
_patchSession(id, (s) => Session(
|
||||
id: s.id,
|
||||
title: s.title,
|
||||
agent: agent,
|
||||
model: s.model,
|
||||
location: s.location,
|
||||
created: s.created,
|
||||
updated: s.updated));
|
||||
_patchSession(id, (s) => s.copyWith(agent: agent));
|
||||
break;
|
||||
case 'session.metadata.updated':
|
||||
final id = data['sessionID'] as String?;
|
||||
final metadata = data['metadata'];
|
||||
if (id != null && metadata is Map<String, dynamic>) {
|
||||
_patchSession(
|
||||
id,
|
||||
(s) => s.copyWith(
|
||||
metadata: Map<String, dynamic>.from(metadata)));
|
||||
notifyListeners();
|
||||
}
|
||||
break;
|
||||
case 'session.archived':
|
||||
final id = data['sessionID'] as String?;
|
||||
if (id != null) {
|
||||
final gone = sessions.firstWhere((s) => s.id == id,
|
||||
orElse: () => Session(id: id, title: ''));
|
||||
sessions.removeWhere((s) => s.id == id);
|
||||
if (archivedLoaded && gone.title.isNotEmpty) {
|
||||
archivedSessions.insert(
|
||||
0,
|
||||
gone.copyWith(
|
||||
archived: DateTime.now().millisecondsSinceEpoch));
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
break;
|
||||
case 'session.deleted':
|
||||
final id = data['sessionID'] as String?;
|
||||
if (id != null) sessions.removeWhere((s) => s.id == id);
|
||||
if (id != null) {
|
||||
sessions.removeWhere((s) => s.id == id);
|
||||
archivedSessions.removeWhere((s) => s.id == id);
|
||||
}
|
||||
break;
|
||||
case 'session.unarchived':
|
||||
refreshSessions();
|
||||
if (archivedLoaded) refreshArchived();
|
||||
return;
|
||||
case 'session.moved':
|
||||
refreshSessions();
|
||||
return;
|
||||
@@ -492,6 +799,7 @@ class AppState extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> openChat(Session s) async {
|
||||
draftOpen = false;
|
||||
chat = ChatState(this, s);
|
||||
// The shell shows the new session at once; its messages follow.
|
||||
notifyListeners();
|
||||
|
||||
Reference in new issue
Block a user