Show the chat mode control as soon as the session names its mode

A created chat's reply carried no mode, and the chat destination kept the
first row it opened for the whole visit, ignoring later list re-reads, so the
mode control only appeared after closing and reopening the chat. The server
now names the mode on a created row, and the destination adopts each listed
row that a re-read changed (excluding the live running flag), so the open
conversation follows new, refreshed and other-device mode changes in place.
This commit is contained in:
2026-11-02 16:37:35 +00:00
parent d78cbe3f66
commit 1a9fc74999
4 changed files with 345 additions and 10 deletions
+39 -9
View File
@@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
import '../../core/api_gateway.dart';
import 'conversation/chat_models.dart';
import 'conversation/chat_row_widgets.dart';
import 'conversation/chat_scroll.dart';
import 'conversation/chat_thread_view.dart';
import 'conversation/chat_transcript_cache.dart';
@@ -64,6 +65,10 @@ class _ChatScreenState extends State<ChatScreen> {
/// (a deep link) resolves the same row without a second list request.
final Map<String, JsonMap> _openedSessions = {};
/// Each row as the list last reported it (without its live `running`), so
/// [_adoptRows] adopts only what a re-read changed.
final Map<String, JsonMap> _listedRows = {};
/// Model switches the server accepted after a row was opened. The row that
/// opened the conversation may still carry the pre-switch model, so these win
/// for the rest of this visit, including when the same chat is reopened.
@@ -132,18 +137,40 @@ class _ChatScreenState extends State<ChatScreen> {
unawaited(_sessions.refresh());
}
/// Adopts the list's loaded rows for the routed session: a deep link opens
/// the conversation before any row has been tapped, and the header's title
/// and model control come from that row. Called after a frame, never during
/// Adopts the list's loaded rows for the routed session and for every row
/// this visit has opened. A deep link opens the conversation before any row
/// has been tapped, and a created or long-open row can predate facts the
/// list has since read (its mode, a mode another device chose, a title).
/// The header's title, model and mode controls come from that row, so the
/// open conversation follows each re-read instead of waiting to be reopened.
/// Only a listed row that changed since the list last reported it is
/// adopted, so an unchanged (or still loading) listing never reverts a mode
/// or model this visit has just accepted. Fields only the opened row carries
/// (a create's instruction status) are kept. The list's live `running` flag
/// is ignored: the conversation tracks its own turn, and following it would
/// rebuild the open thread at every turn. Called after a frame, never during
/// the list's own build.
void _adoptRows(List<JsonMap> rows) {
final id = _selected;
if (id == null || _openedSessions.containsKey(id)) return;
if (!mounted) return;
final selected = _selected;
final next = <String, JsonMap>{};
for (final row in rows) {
if ('${row['id'] ?? ''}' != id) continue;
setState(() => _openedSessions[id] = row);
return;
final id = '${row['id'] ?? ''}';
if (id.isEmpty) continue;
final listed = <String, dynamic>{...row}..remove('running');
final previous = _listedRows[id];
_listedRows[id] = listed;
final opened = _openedSessions[id];
if (opened == null && id != selected) continue;
if (opened != null && chatJsonEquals(previous, listed)) continue;
final adopted = <String, dynamic>{...?opened, ...listed};
if (opened != null && opened.containsKey('running')) {
adopted['running'] = opened['running'];
}
if (!chatJsonEquals(opened, adopted)) next[id] = adopted;
}
if (next.isEmpty) return;
setState(() => _openedSessions.addAll(next));
}
void _modelChanged(String id, ChatModelSelection applied) {
@@ -152,13 +179,16 @@ class _ChatScreenState extends State<ChatScreen> {
}
/// An accepted mode switch updates the opened row, so reopening the chat in
/// this visit shows it before the list re-reads.
/// this visit shows it before the list re-reads. The list then re-reads, so
/// a listing already in flight from before the switch is superseded rather
/// than adopted over the accepted mode.
void _modeChanged(String id, String mode, String name) {
final row = _openedSessions[id];
if (!mounted || row == null) return;
setState(
() => _openedSessions[id] = {...row, 'mode': mode, 'mode_name': name},
);
unawaited(_sessions.refresh());
}
/// The row to open for [id], with an accepted model folded in.