feat: add a project from the picker, and keep the context ring

- the project picker has an "Add project" action: a folder browser starts
  at the server user's home, navigates (typed path, up, new folder) and
  adds the chosen folder through POST /api/openchamber/directory; the new
  project becomes the draft's target at once
- refreshProjects merges settings-only projects (newest added first), so a
  project added before its first session still appears in the picker; the
  first session moves it into /api/project as usual
- usageReading no longer hides the composer's context ring when the
  session's model has left the server catalog: a 200K window is assumed,
  the percentage is prefixed with "~", and the usage sheet says the window
  is estimated. A failed model fetch at connect is retried when a chat
  opens, so a normal session shows the real percentage
- tests: the add-project browser flow, settings-only projects, and the
  estimated/known context readings
This commit is contained in:
blumia committed 2026-11-27 09:26:14 +08:00
1 parent bd8afeb686
commit c6b5a870c6
11 files changed
+708 -22

No files matched your search

+72 -11
View File
@@ -141,6 +141,9 @@ class AppState extends ChangeNotifier {
String? chatsRoot;
List<String> chatRoots = [];
/// The server user's home directory, where the add-project browser starts.
String homeDirectory = '';
/// The unsent new-session draft, kept in memory. Null when there is none.
SessionDraft? draft;
bool sendingDraft = false;
@@ -532,6 +535,7 @@ class AppState extends ChangeNotifier {
projects = [];
chatsRoot = null;
chatRoots = [];
homeDirectory = '';
allowPromptingSubagentSessions = false;
_childrenLoaded.clear();
_childrenLoading.clear();
@@ -598,6 +602,7 @@ class AppState extends ChangeNotifier {
// 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>>{};
final settingsRows = <Map<String, dynamic>>[];
try {
final settings = await cl.settings();
final rows = settings['projects'];
@@ -610,10 +615,36 @@ class AppState extends ChangeNotifier {
final key = slashPath(path);
byPath[key] = meta;
byPath[key.toLowerCase()] = meta;
settingsRows.add(meta);
}
}
} catch (_) {}
// OpenCode only lists a checkout once it has a session. A project the
// user added in the client has no session yet, so its settings row is
// the only record; keep those rows in the picker (newest first) until
// the first session moves them into /api/project.
final known = <String>{};
for (final project in list) {
known.add(slashPath(project.canonical));
known.add(slashPath(project.canonical).toLowerCase());
}
final extras = <({int added, ProjectInfo project})>[];
for (final meta in settingsRows) {
final path = meta['path'];
if (path is! String || path.trim().isEmpty) continue;
final key = slashPath(path);
if (known.contains(key) || known.contains(key.toLowerCase())) continue;
final added = meta['addedAt'];
extras.add((
added: added is num ? added.toInt() : 0,
project: ProjectInfo(id: '', canonical: path).withMeta(meta),
));
}
extras.sort((a, b) => b.added.compareTo(a.added));
projects = [
for (final extra in extras) extra.project,
for (final project in list)
project.withMeta(byPath[slashPath(project.canonical)] ??
byPath[slashPath(project.canonical).toLowerCase()]),
@@ -622,6 +653,18 @@ class AppState extends ChangeNotifier {
} catch (_) {}
}
/// Adds a folder to the server's project list, then refreshes the list so
/// its settings row decorates it. Returns the resolved project; the caller
/// may select it for a new session. Throws when the server refuses the path.
Future<ProjectInfo?> addProjectDirectory(String path) async {
final cl = client;
if (cl == null) return null;
final resolved = await cl.addProjectDirectory(path);
await refreshProjects();
return projectFor(resolved) ??
ProjectInfo(id: '', canonical: resolved, name: pathLeaf(resolved));
}
final Map<String, Future<Uint8List?>> _projectIcons = {};
/// A project's icon image, fetched once per theme/colour/version. Null when
@@ -818,6 +861,7 @@ class AppState extends ChangeNotifier {
add(info.canonicalLegacyChatsRoot);
chatsRoot = info.chatsRoot.isNotEmpty ? info.chatsRoot : null;
chatRoots = roots.toList();
if (info.home.isNotEmpty) homeDirectory = info.home;
notifyListeners();
} catch (_) {}
}
@@ -1343,6 +1387,10 @@ class AppState extends ChangeNotifier {
// Opening a chat whose turn is already under way: the events that started
// it came while the chat was closed, so ask the server once.
final actives = refreshActive();
// The catalog is read once per connection; a request that raced the
// server's startup leaves it empty and the context ring without the
// model's window. Opening a chat is where that window is first needed.
if (modelList.isEmpty) unawaited(refreshModels());
await chat!.load();
final known = await actives;
if (chat != null && chat!.session.id == s.id) {
@@ -2351,7 +2399,18 @@ class ChatState extends ChangeNotifier {
// ------------------------------------------------------------ usage
(int, int, int, double, bool, double) usageReading() {
/// The context window assumed when the session's model is not in the
/// server's catalog: old sessions can name a model its provider has since
/// dropped, and hiding the ring then reads as a regression. The value is
/// the common 200K window; the usage sheet marks it as estimated.
static const estimatedContextWindow = 200000;
/// The composer's context reading: used tokens, the model's context window,
/// its reserved output, the percentage of the usable window, whether there
/// is a reading at all, and whether the window was estimated because the
/// model is unknown. The last two are false before the first assistant
/// message with token usage.
(int, int, int, double, bool, bool) usageReading() {
Message? last;
for (var i = msgs.length - 1; i >= 0; i--) {
if (msgs[i].type == 'assistant' && msgs[i].tokens != null) {
@@ -2359,25 +2418,27 @@ class ChatState extends ChangeNotifier {
break;
}
}
if (last == null) return (0, 0, 0, 0, false, 0);
if (last == null) return (0, 0, 0, 0, false, false);
ModelRef? model = last.model;
if (model == null) {
for (var i = msgs.length - 1; i >= 0 && model == null; i--) {
model = msgs[i].model;
}
}
if (model == null) return (0, 0, 0, 0, false, 0);
final used = last.tokens!.used;
final lim = app.modelLimits['${model.providerId}/${model.id}'] ??
app.modelLimits[model.id] ??
(0, 0);
final context = lim.$1;
var output = lim.$2;
if (context <= 0 || used <= 0) return (used, context, output, 0, false, 0);
var reserve = output > 32000 ? 32000 : output;
if (used <= 0) return (used, 0, 0, 0, false, false);
final lim = model == null
? (0, 0)
: (app.modelLimits['${model.providerId}/${model.id}'] ??
app.modelLimits[model.id] ??
(0, 0));
final known = lim.$1 > 0;
final context = known ? lim.$1 : estimatedContextWindow;
final output = known ? lim.$2 : 0;
final reserve = output > 32000 ? 32000 : output;
final threshold = (context - reserve) < 1 ? 1 : context - reserve;
final pct = used / threshold * 100;
return (used, context, output, pct, true, 0);
return (used, context, output, pct, true, !known);
}
double get turnCost {