Chat: /new continues the same chat on a fresh session

The composer's /new used to create a separate chat. It now sends
chat_new_session. The server starts a fresh native session on the chat's
own harness (OpenCode, Codex or Claude Code) and re-keys the
operator_sessions row in one transaction. The chat keeps its title,
directory, backend, colour, star, model and mode. The old row is kept
unlisted with replaced_by, and its native session stays resumable.

The reply and the chat_session_changed push carry previous_id, so every
client swaps the row in place and an open conversation follows it to the
fresh, empty transcript with a notice.
This commit is contained in:
2026-11-03 14:06:44 +00:00
parent 251cc4f274
commit ad4233f7b8
21 changed files with 910 additions and 131 deletions
+27 -8
View File
@@ -15,6 +15,7 @@ import 'conversation/chat_thread_view.dart';
import 'conversation/chat_transcript_cache.dart';
import 'conversation/composer_focus_request.dart';
import 'data/chat_repository.dart';
import 'domain/chat_commands.dart';
import 'ui/sessions/chat_sessions_controller.dart';
import 'ui/sessions/chat_sessions_screen.dart';
@@ -59,7 +60,7 @@ class _ChatScreenState extends State<ChatScreen> {
/// conversation's command changes a session.
late final ChatSessionsController _sessions = ChatSessionsController(
ChatRepository(widget.gateway),
);
)..onSessionReplaced = _replacedElsewhere;
/// Rows this visit has opened, used for the native title and the recorded
/// model facts. The mounted list also reports its loaded rows, so a routed id
@@ -122,17 +123,35 @@ class _ChatScreenState extends State<ChatScreen> {
}
}
/// `/new` created [session]: list it at the head before its conversation
/// opens, open it on [notice], then re-read the list from the server.
void _openCreated(JsonMap session, String notice) {
/// `/new` moved the chat [previousId] to a fresh session: [session] takes
/// its list row, the fresh conversation opens on [notice], then the list
/// re-reads from the server.
void _startedNewSession(String previousId, JsonMap session, String notice) {
final id = '${session['id'] ?? ''}';
if (id.isEmpty) return;
_sessions.insertCreated(session);
_startNotice = (id: id, notice: notice);
_open(session);
_sessions.replaceSession(previousId, session);
_followNewSession(previousId, session, notice);
unawaited(_sessions.refresh());
}
/// Another device started a new session in a chat: only an open chat
/// follows it; the list already swapped the row.
void _replacedElsewhere(String previousId, JsonMap session) {
if (!mounted || _selected != previousId) return;
_followNewSession(previousId, session, chatNewSessionElsewhereNotice);
}
/// Opens the chat under its new id on the server's row, which carries every
/// shared field (an omitted one is unset, so the old row is not merged in).
/// A model switch accepted here in this visit moved with the chat.
void _followNewSession(String previousId, JsonMap session, String notice) {
final id = '${session['id'] ?? ''}';
final accepted = _acceptedModels[previousId];
if (accepted != null) _acceptedModels[id] = accepted;
_startNotice = (id: id, notice: notice);
_open(<String, dynamic>{...session}..remove('running'));
}
/// `/rename` was accepted for [id]: the opened row follows the shared title
/// and the list re-reads it.
void _renamed(String id, String title) {
@@ -264,7 +283,7 @@ class _ChatScreenState extends State<ChatScreen> {
onBack: onBack,
onModelChanged: (applied) => _modelChanged(selected, applied),
onModeChanged: (mode, name) => _modeChanged(selected, mode, name),
onCreatedSession: _openCreated,
onNewSession: _startedNewSession,
onSessionRenamed: (title) => _renamed(selected, title),
onSessionClosed: _closed,
onColorSelected: (color) => _sessions.setColor(selected, color),