feat: group the server's chats and use OpenChamber's project icons
Sessions under the server's managed chats root (chats/<date>/session-…) were grouped per directory, so each one appeared as its own session-xxxxx band. GET /api/fs/home now supplies the chats root plus its canonical and legacy aliases; every chat folder becomes the single 聊天 group. The new-session picker can target 聊天 too: the first send creates a fresh chat directory before creating the session, and a new session started from an existing chat gets a new one instead of sharing its folder. Projects merge their OpenChamber settings row (label, icon key, colour, iconBackground and uploaded/auto icon image) with /api/project. Group headers, the picker and the draft chip draw the chosen icon in its colour, and custom images are fetched through the client transport (SVG included) with a themed fallback to the glyph. The chat group uses a chat bubble rather than a folder. 0.4.8 (408).
This commit is contained in:
1 parent
91bf5e5b81
commit
79221e57d7
14 files changed
+599
-32
No files matched your search
+137
-3
@@ -5,6 +5,7 @@ library;
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@@ -106,6 +107,15 @@ class AppState extends ChangeNotifier {
|
||||
/// group headers.
|
||||
List<ProjectInfo> projects = [];
|
||||
|
||||
/// The "聊天" target of a new-session draft. It is not a path: the first
|
||||
/// send creates a fresh directory under the server's chats root.
|
||||
static const chatTarget = '\u0000chats';
|
||||
|
||||
/// The server's chats root and the aliases it may appear under (canonical,
|
||||
/// legacy `.config` path). Sessions below these are the sidebar's 聊天 group.
|
||||
String? chatsRoot;
|
||||
List<String> chatRoots = [];
|
||||
|
||||
/// The unsent new-session draft, kept in memory. Null when there is none.
|
||||
SessionDraft? draft;
|
||||
bool sendingDraft = false;
|
||||
@@ -428,6 +438,7 @@ class AppState extends ChangeNotifier {
|
||||
_startStream();
|
||||
refreshSessions();
|
||||
refreshProjects();
|
||||
refreshFsHome();
|
||||
refreshActive();
|
||||
refreshPerms();
|
||||
refreshModels();
|
||||
@@ -452,6 +463,9 @@ class AppState extends ChangeNotifier {
|
||||
sendingDraft = false;
|
||||
sessions = [];
|
||||
projects = [];
|
||||
chatsRoot = null;
|
||||
chatRoots = [];
|
||||
_projectIcons.clear();
|
||||
archivedSessions = [];
|
||||
archivedLoaded = false;
|
||||
_sessionsNext = null;
|
||||
@@ -510,11 +524,57 @@ class AppState extends ChangeNotifier {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
try {
|
||||
projects = await cl.listProjects();
|
||||
final list = await cl.listProjects();
|
||||
// The settings document carries the operator's label, icon key, colour
|
||||
// and icon image; /api/project only knows the checkout itself.
|
||||
final byPath = <String, Map<String, dynamic>>{};
|
||||
try {
|
||||
final settings = await cl.settings();
|
||||
final rows = settings['projects'];
|
||||
if (rows is List) {
|
||||
for (final row in rows) {
|
||||
if (row is! Map) continue;
|
||||
final path = row['path'];
|
||||
if (path is! String || path.trim().isEmpty) continue;
|
||||
final meta = Map<String, dynamic>.from(row);
|
||||
final key = slashPath(path);
|
||||
byPath[key] = meta;
|
||||
byPath[key.toLowerCase()] = meta;
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
projects = [
|
||||
for (final project in list)
|
||||
project.withMeta(byPath[slashPath(project.canonical)] ??
|
||||
byPath[slashPath(project.canonical).toLowerCase()]),
|
||||
];
|
||||
notifyListeners();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
final Map<String, Future<Uint8List?>> _projectIcons = {};
|
||||
|
||||
/// A project's icon image, fetched once per theme/colour/version. Null when
|
||||
/// the project has no image or the server does not serve one.
|
||||
Future<Uint8List?> projectIcon(ProjectInfo project,
|
||||
{required bool dark, required String iconColor}) {
|
||||
if (!project.hasIconImage) return Future.value(null);
|
||||
final key = '${project.metaId}|${project.iconImageUpdatedAt}|'
|
||||
'${dark ? 'dark' : 'light'}|$iconColor';
|
||||
final cached = _projectIcons[key];
|
||||
if (cached != null) return cached;
|
||||
final cl = client;
|
||||
final future = cl == null
|
||||
? Future<Uint8List?>.value(null)
|
||||
: cl.projectIconBytes(project.metaId,
|
||||
theme: dark ? 'dark' : 'light', iconColor: iconColor);
|
||||
_projectIcons[key] = future;
|
||||
future.then((bytes) {
|
||||
if (bytes == null) _projectIcons.remove(key);
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
/// Loads the next (older) page of the session list.
|
||||
Future<void> loadMoreSessions() async {
|
||||
final cl = client;
|
||||
@@ -611,6 +671,7 @@ class AppState extends ChangeNotifier {
|
||||
}
|
||||
|
||||
String projectLabelFor(String directory) {
|
||||
if (directory == chatTarget || isChatDirectory(directory)) return '聊天';
|
||||
if (directory.isEmpty) return '';
|
||||
for (final project in projects) {
|
||||
if (samePath(project.canonical, directory)) return project.label;
|
||||
@@ -618,6 +679,63 @@ class AppState extends ChangeNotifier {
|
||||
return pathLeaf(directory);
|
||||
}
|
||||
|
||||
/// Whether [directory] is one of the server's managed chat folders. The
|
||||
/// chats root can be relocated away from the home config directory, so it
|
||||
/// comes from `/api/fs/home`; before that answer the default layout is still
|
||||
/// recognised.
|
||||
bool isChatDirectory(String directory) {
|
||||
if (directory.isEmpty) return false;
|
||||
if (directory == chatTarget) return true;
|
||||
for (final root in chatRoots) {
|
||||
if (isUnderDirectory(directory, root)) return true;
|
||||
}
|
||||
return slashPath(directory).contains('/.config/openchamber/chats/');
|
||||
}
|
||||
|
||||
Future<void> refreshFsHome() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
try {
|
||||
final info = await cl.fsHome();
|
||||
if (client != cl) return;
|
||||
final roots = <String>{};
|
||||
void add(String? value) {
|
||||
if (value != null && value.trim().isNotEmpty) roots.add(value.trim());
|
||||
}
|
||||
add(info.chatsRoot);
|
||||
add(info.canonicalChatsRoot);
|
||||
if (info.home.isNotEmpty) {
|
||||
final home = info.home.replaceAll(RegExp(r'[/\\]+$'), '');
|
||||
add('$home/.config/openchamber/chats');
|
||||
}
|
||||
add(info.canonicalLegacyChatsRoot);
|
||||
chatsRoot = info.chatsRoot.isNotEmpty ? info.chatsRoot : null;
|
||||
chatRoots = roots.toList();
|
||||
notifyListeners();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// A new directory for a chat session, in the server's chats root. The
|
||||
/// server allows creating folders under that managed root without a
|
||||
/// project scope.
|
||||
Future<String> createChatDirectory() async {
|
||||
final cl = client;
|
||||
if (cl == null) throw StateError('未连接服务器');
|
||||
if (chatsRoot == null || chatsRoot!.isEmpty) await refreshFsHome();
|
||||
final root = chatsRoot;
|
||||
if (root == null || root.isEmpty) {
|
||||
throw StateError('服务器没有提供聊天目录');
|
||||
}
|
||||
final now = DateTime.now();
|
||||
final date = '${now.year.toString().padLeft(4, '0')}-'
|
||||
'${now.month.toString().padLeft(2, '0')}-'
|
||||
'${now.day.toString().padLeft(2, '0')}';
|
||||
final id = now.microsecondsSinceEpoch.toRadixString(36);
|
||||
final dir = '${root.replaceAll(RegExp(r'[/\\]+$'), '')}/$date/session-$id';
|
||||
await cl.fsMkdir(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
ProjectInfo? projectFor(String directory) {
|
||||
for (final project in projects) {
|
||||
if (samePath(project.canonical, directory)) return project;
|
||||
@@ -705,9 +823,12 @@ class AppState extends ChangeNotifier {
|
||||
chat?.session.directory ??
|
||||
(sessions.isNotEmpty ? sessions.first.directory : '');
|
||||
final latest = sessions.isNotEmpty ? sessions.first : null;
|
||||
final dir = fallback.isNotEmpty
|
||||
var dir = fallback.isNotEmpty
|
||||
? fallback
|
||||
: (projects.isNotEmpty ? projects.first.canonical : '');
|
||||
// A new session started from an existing chat gets a chat of its own, not
|
||||
// a second session in the same folder.
|
||||
if (dir.isNotEmpty && isChatDirectory(dir)) dir = chatTarget;
|
||||
final created = SessionDraft(
|
||||
directory: dir,
|
||||
model: chat?.selectedModel ?? latest?.model,
|
||||
@@ -773,10 +894,23 @@ class AppState extends ChangeNotifier {
|
||||
current.error = null;
|
||||
sendingDraft = true;
|
||||
notifyListeners();
|
||||
var target = current.directory;
|
||||
if (target == chatTarget) {
|
||||
try {
|
||||
target = await createChatDirectory();
|
||||
current.directory = target;
|
||||
} catch (e) {
|
||||
sendingDraft = false;
|
||||
current.error =
|
||||
'无法创建聊天目录:${e is ApiError ? e.message : e}';
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
}
|
||||
final Session created;
|
||||
try {
|
||||
created = await cl.createSession(
|
||||
directory: current.directory,
|
||||
directory: target,
|
||||
model: current.model,
|
||||
agent: current.agent,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user