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:
@@ -7,45 +7,41 @@ import '../domain/chat_commands.dart';
|
||||
/// Shows one local line in the conversation the command ran in.
|
||||
typedef ChatCommandNoticeCallback = void Function(String text, {bool failed});
|
||||
|
||||
/// Hands a created session row to the destination with the line the new
|
||||
/// conversation opens on. The destination owns navigation: it lists the row,
|
||||
/// opens it and refreshes the list from the server.
|
||||
typedef ChatCreatedSessionCallback =
|
||||
void Function(JsonMap session, String notice);
|
||||
/// Hands the chat's row under its fresh native session id to the destination,
|
||||
/// with the id it replaces and the line the fresh conversation opens on. The
|
||||
/// destination owns navigation: it swaps the list row in place, opens the new
|
||||
/// id and refreshes the list from the server.
|
||||
typedef ChatNewSessionCallback =
|
||||
void Function(String previousId, JsonMap session, String notice);
|
||||
|
||||
/// Runs the recorded commands that act on the open session: `/new`, `/close`
|
||||
/// and `/rename <title>`.
|
||||
///
|
||||
/// Every outcome is a local notice, never a prompt. The requests are the
|
||||
/// repository's existing create, close and rename calls, exactly as the list's
|
||||
/// New chat dialog, Close chat confirm and Rename dialog send them. `/stop` and
|
||||
/// `/help` are not run here; [runs] says which commands are.
|
||||
/// Every outcome is a local notice, never a prompt. `/new` is the server's
|
||||
/// `chat_new_session` (the same chat on a fresh context, as the harness's own
|
||||
/// `/new` or `/clear`); `/close` and `/rename` are the Close chat confirm's and
|
||||
/// Rename dialog's requests. `/stop` and `/help` are not run here; [runs] says
|
||||
/// which commands are.
|
||||
class ChatCommandActions {
|
||||
ChatCommandActions({
|
||||
required this.repository,
|
||||
required this.sessionId,
|
||||
required this.directory,
|
||||
required this.showNotice,
|
||||
required this.confirmClose,
|
||||
required this.openCreatedSession,
|
||||
required this.startedNewSession,
|
||||
this.onRenamed,
|
||||
this.onClosed,
|
||||
this.newOperationId,
|
||||
});
|
||||
|
||||
final ChatRepository repository;
|
||||
final String sessionId;
|
||||
|
||||
/// The open session's recorded directory, blank when the row is unknown.
|
||||
/// `/new` starts the created session there.
|
||||
final String directory;
|
||||
|
||||
final ChatCommandNoticeCallback showNotice;
|
||||
|
||||
/// Opens the row's `Close chat?` confirm; false sends nothing.
|
||||
final Future<bool> Function() confirmClose;
|
||||
|
||||
final ChatCreatedSessionCallback openCreatedSession;
|
||||
final ChatNewSessionCallback startedNewSession;
|
||||
|
||||
/// The server accepted the shared rename with this trimmed title.
|
||||
final void Function(String title)? onRenamed;
|
||||
@@ -53,9 +49,6 @@ class ChatCommandActions {
|
||||
/// The server accepted the close; the conversation is finished here too.
|
||||
final void Function()? onClosed;
|
||||
|
||||
/// Test/fixture seam; production mints one id per create intent.
|
||||
final String Function()? newOperationId;
|
||||
|
||||
/// Whether [command] is one this runner performs.
|
||||
static bool runs(ChatCommand command) => const {
|
||||
chatCommandNew,
|
||||
@@ -81,42 +74,28 @@ class ChatCommandActions {
|
||||
}
|
||||
}
|
||||
|
||||
/// `/new`: the existing `chat_create` with the web's fixed title and brief,
|
||||
/// in this session's directory, with no backend or model field. The reply's
|
||||
/// row is handed to the destination with the notice the new conversation
|
||||
/// opens on; a refusal stays in this conversation.
|
||||
/// `/new`: `chat_new_session` keeps this chat (row, title, colour, star,
|
||||
/// directory, model and mode) and moves it to a fresh native session, so the
|
||||
/// agent starts from an empty context. The reply's row, under its new id, is
|
||||
/// handed to the destination with the notice the fresh conversation opens
|
||||
/// on; a refusal (a running turn, say) stays in this conversation.
|
||||
Future<void> _startSession() async {
|
||||
if (directory.isEmpty) {
|
||||
showNotice(chatNoDirectoryReport, failed: true);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final reply = await repository.create(
|
||||
title: chatNewSessionTitle,
|
||||
directory: directory,
|
||||
operationId: _operationId(),
|
||||
final reply = await repository.newSession(
|
||||
sessionId,
|
||||
instructions: chatDefaultSessionInstructions,
|
||||
);
|
||||
final session = reply['chat_session'];
|
||||
if (session is! Map) {
|
||||
final id = session is Map ? '${session['id'] ?? ''}'.trim() : '';
|
||||
if (session is! Map || id.isEmpty || id == sessionId) {
|
||||
throw const ProtocolError(
|
||||
'The server did not return the new chat session.',
|
||||
'The server did not return the new session for this chat.',
|
||||
);
|
||||
}
|
||||
final row = Map<String, dynamic>.from(session);
|
||||
final title = '${row['title'] ?? ''}'.trim();
|
||||
openCreatedSession(
|
||||
row,
|
||||
chatSessionStartedNotice(
|
||||
title.isEmpty ? chatNewSessionTitle : title,
|
||||
directory,
|
||||
),
|
||||
);
|
||||
final row = Map<String, dynamic>.from(session)..remove('previous_id');
|
||||
startedNewSession(sessionId, row, chatNewSessionNotice);
|
||||
} catch (error) {
|
||||
showNotice(
|
||||
chatSessionStartFailedNotice(directory, _reason(error)),
|
||||
failed: true,
|
||||
);
|
||||
showNotice(chatNewSessionFailedNotice(_reason(error)), failed: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,10 +127,6 @@ class ChatCommandActions {
|
||||
}
|
||||
}
|
||||
|
||||
String _operationId() =>
|
||||
newOperationId?.call() ??
|
||||
'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
||||
|
||||
/// The server's words without a Dart runtime prefix, as the web prints them.
|
||||
String _reason(Object error) =>
|
||||
describeErrorReason(error, fallback: 'The server gave no reason.');
|
||||
|
||||
@@ -80,7 +80,7 @@ class ChatThreadView extends StatefulWidget {
|
||||
this.scrollPositions,
|
||||
this.transcripts,
|
||||
this.initialNotice,
|
||||
this.onCreatedSession,
|
||||
this.onNewSession,
|
||||
this.onSessionRenamed,
|
||||
this.onSessionClosed,
|
||||
this.fileBridge = const ArtifactFileBridge(),
|
||||
@@ -99,10 +99,10 @@ class ChatThreadView extends StatefulWidget {
|
||||
/// the `/new` notice for a session it just created and opened.
|
||||
final String? initialNotice;
|
||||
|
||||
/// `/new` created this row; the destination lists it, opens it with the
|
||||
/// notice and refreshes the list. Without a destination the notice shows
|
||||
/// here instead, since the session already exists on the server.
|
||||
final ChatCreatedSessionCallback? onCreatedSession;
|
||||
/// `/new` moved this chat to a fresh native session; the destination swaps
|
||||
/// the row in place, opens the new id with the notice and refreshes the
|
||||
/// list. Without a destination the notice shows here instead.
|
||||
final ChatNewSessionCallback? onNewSession;
|
||||
|
||||
/// `/rename` was accepted with this title, so the header can follow it.
|
||||
final ValueChanged<String>? onSessionRenamed;
|
||||
@@ -423,22 +423,21 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
}
|
||||
|
||||
/// The runner for this session's `/new`, `/close` and `/rename`, rebuilt per
|
||||
/// build so an adopted row's directory and title are the current ones.
|
||||
/// build so an adopted row's title is the current one.
|
||||
ChatCommandActions _commandActions() {
|
||||
final session =
|
||||
widget.session ?? {'id': widget.sessionId, 'title': widget.title};
|
||||
final onCreated = widget.onCreatedSession;
|
||||
final onNewSession = widget.onNewSession;
|
||||
return ChatCommandActions(
|
||||
repository: ChatRepository(widget.gateway),
|
||||
sessionId: widget.sessionId,
|
||||
directory: '${session['directory'] ?? ''}'.trim(),
|
||||
showNotice: _showCommandNotice,
|
||||
confirmClose: () => confirmCloseChat(context, session),
|
||||
openCreatedSession:
|
||||
onCreated ?? (created, notice) => _showCommandNotice(notice),
|
||||
startedNewSession:
|
||||
onNewSession ??
|
||||
(previousId, created, notice) => _showCommandNotice(notice),
|
||||
onRenamed: widget.onSessionRenamed,
|
||||
onClosed: _sessionClosed,
|
||||
newOperationId: widget.newOperationId,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -174,6 +174,18 @@ class ChatRepository {
|
||||
});
|
||||
}
|
||||
|
||||
/// `/new`: the same chat continues on a fresh native session with an empty
|
||||
/// context. The reply's `chat_session` is the chat under its new id, with
|
||||
/// `previous_id` naming [sessionId].
|
||||
Future<JsonMap> newSession(String sessionId, {String instructions = ''}) =>
|
||||
gateway.call('chat_new_session', {
|
||||
'chat_new_session': {
|
||||
'session_id': sessionId,
|
||||
if (instructions.trim().isNotEmpty)
|
||||
'instructions': instructions.trim(),
|
||||
},
|
||||
});
|
||||
|
||||
Future<JsonMap> rename(String sessionId, String title) =>
|
||||
gateway.call('chat_rename', {
|
||||
'chat_rename': {'session_id': sessionId, 'title': title.trim()},
|
||||
|
||||
@@ -29,7 +29,7 @@ const String chatCommandHelp = 'help';
|
||||
const List<ChatCommand> chatCommands = <ChatCommand>[
|
||||
ChatCommand(
|
||||
name: chatCommandNew,
|
||||
summary: 'Start a fresh session in this directory',
|
||||
summary: 'Clear the context: continue this chat in a fresh session',
|
||||
usage: '/new',
|
||||
),
|
||||
ChatCommand(
|
||||
@@ -131,8 +131,8 @@ String chatUnknownCommandReport(String word) =>
|
||||
String chatCommandUnavailableReport(String word) =>
|
||||
'“$word” is not available yet, so nothing was sent to the agent.';
|
||||
|
||||
/// The behaviour brief a `/new` session is created with, verbatim from the web
|
||||
/// client's `DEFAULT_CHAT_INSTRUCTIONS`.
|
||||
/// The behaviour brief a new chat, and a `/new` fresh session, starts with,
|
||||
/// verbatim from the web client's `DEFAULT_CHAT_INSTRUCTIONS`.
|
||||
const String chatDefaultSessionInstructions =
|
||||
"Act as a careful coding agent. Read the repository's AGENTS.md and the "
|
||||
'relevant skills before acting. Inspect the current state, keep changes '
|
||||
@@ -140,9 +140,6 @@ const String chatDefaultSessionInstructions =
|
||||
'changed and what was not verified. Ask only about real ambiguity, '
|
||||
'destructive actions or changes outside the request.';
|
||||
|
||||
/// The title every `/new` session is created with, as the web client sends it.
|
||||
const String chatNewSessionTitle = 'New chat';
|
||||
|
||||
/// `/new` and `/close` act on the open session alone, so an argument is a
|
||||
/// usage error the web reports in these exact words.
|
||||
String chatCommandArgumentReport(ChatCommand command) =>
|
||||
@@ -152,18 +149,21 @@ String chatCommandArgumentReport(ChatCommand command) =>
|
||||
const String chatRenameUsageReport =
|
||||
'Usage: /rename <title> — renames this session.';
|
||||
|
||||
/// `/new` needs the open session's directory; without one nothing is sent.
|
||||
const String chatNoDirectoryReport =
|
||||
"Can't start a new session here: the server reported no directory for "
|
||||
'this chat.';
|
||||
/// The line the fresh conversation opens on after `/new`: the chat is the
|
||||
/// same, the agent's context is empty.
|
||||
const String chatNewSessionNotice =
|
||||
'New session started. The agent no longer sees the earlier conversation; '
|
||||
'the previous session can still be resumed in a terminal.';
|
||||
|
||||
/// The notice the created session opens with.
|
||||
String chatSessionStartedNotice(String title, String directory) =>
|
||||
'Started a new session “$title” in $directory.';
|
||||
/// The line a conversation opens on when another device started its new
|
||||
/// session.
|
||||
const String chatNewSessionElsewhereNotice =
|
||||
'Another device started a new session in this chat. The agent no longer '
|
||||
'sees the earlier conversation.';
|
||||
|
||||
/// The failed notice the current session shows when the create is refused.
|
||||
String chatSessionStartFailedNotice(String directory, String reason) =>
|
||||
"Couldn't start a new session in $directory: $reason";
|
||||
/// The failed notice the current session shows when `/new` is refused.
|
||||
String chatNewSessionFailedNotice(String reason) =>
|
||||
"Couldn't start a new session: $reason";
|
||||
|
||||
/// The notice after the server accepted the shared rename.
|
||||
String chatSessionRenamedNotice(String title) =>
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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