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:
@@ -90,11 +90,22 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
/// Another device changed a chat's shared colour, star or title: the row
|
||||
/// takes the server's values at once, with no re-list. Each field the
|
||||
/// registry row omits is cleared, since omitted means unset on the wire.
|
||||
///
|
||||
/// A row naming `previous_id` is a chat another device moved to a fresh
|
||||
/// session (`/new`): it replaces that row in place and [onSessionReplaced]
|
||||
/// lets the destination follow it if that chat is open here.
|
||||
void _sessionChanged(JsonMap frame) {
|
||||
final changed = frame['chat_session'];
|
||||
if (changed is! Map) return;
|
||||
final id = '${changed['id'] ?? ''}';
|
||||
if (id.isEmpty || _disposed) return;
|
||||
final previousId = '${changed['previous_id'] ?? ''}'.trim();
|
||||
if (previousId.isNotEmpty && previousId != id) {
|
||||
final row = Map<String, dynamic>.from(changed)..remove('previous_id');
|
||||
replaceSession(previousId, row);
|
||||
onSessionReplaced?.call(previousId, row);
|
||||
return;
|
||||
}
|
||||
var found = false;
|
||||
_sessions = [
|
||||
for (final row in _sessions)
|
||||
@@ -338,17 +349,32 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
return Map<String, dynamic>.from(session);
|
||||
}
|
||||
|
||||
/// Lists a session the server just created at the head of the loaded rows,
|
||||
/// so the row is present before its conversation opens; the caller re-reads
|
||||
/// the list from the server afterwards. A row with the same id is replaced,
|
||||
/// never duplicated.
|
||||
void insertCreated(JsonMap session) {
|
||||
/// Called when another device moved a listed chat to a fresh session, with
|
||||
/// the id it left and its row under the new id.
|
||||
void Function(String previousId, JsonMap session)? onSessionReplaced;
|
||||
|
||||
/// `/new` moved the chat [previousId] to a fresh native session: [session]
|
||||
/// takes that row's place in the list (it is the same chat), so it is
|
||||
/// present before its conversation opens; the caller re-reads the list from
|
||||
/// the server afterwards. A row unknown here is listed at the head, and a
|
||||
/// row already under the new id is never duplicated.
|
||||
void replaceSession(String previousId, JsonMap session) {
|
||||
if (_disposed) return;
|
||||
final id = '${session['id'] ?? ''}';
|
||||
_sessions = [
|
||||
session,
|
||||
for (final row in _sessions)
|
||||
if ('${row['id'] ?? ''}' != id) row,
|
||||
];
|
||||
if (id.isEmpty) return;
|
||||
var placed = false;
|
||||
final next = <JsonMap>[];
|
||||
for (final row in _sessions) {
|
||||
final rowId = '${row['id'] ?? ''}';
|
||||
if (rowId == previousId) {
|
||||
if (!placed) next.add(session);
|
||||
placed = true;
|
||||
} else if (rowId != id) {
|
||||
next.add(row);
|
||||
}
|
||||
}
|
||||
_sessions = placed ? next : [session, ...next];
|
||||
if (activeSessionId == previousId) activeSessionId = id;
|
||||
_seedLocal();
|
||||
_notify();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user