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:
@@ -0,0 +1,49 @@
|
||||
// `/new` for the `?chats=N` fixture chats (tool/perf/perf_fixture.dart).
|
||||
//
|
||||
// A `chat_new_session` moves chat i to a fresh native id, as the server
|
||||
// re-keys the registry row: the chat keeps its index (title, directory,
|
||||
// colour, star) and its new id opens an empty transcript.
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
final Map<int, int> _generation = {};
|
||||
|
||||
/// The current native id of fixture chat [index].
|
||||
String perfChatId(int index) {
|
||||
final generation = _generation[index] ?? 0;
|
||||
return generation == 0
|
||||
? 'perf-chat-$index'
|
||||
: 'perf-chat-$index-new$generation';
|
||||
}
|
||||
|
||||
/// The fixture chat index an id (current or replaced) belongs to.
|
||||
int perfChatIndex(String id) =>
|
||||
int.tryParse(id.replaceFirst('perf-chat-', '').split('-').first) ?? 0;
|
||||
|
||||
/// Whether [id] is a fresh session `/new` started, whose transcript is empty.
|
||||
bool perfChatIsFresh(String id) => id.contains('-new');
|
||||
|
||||
/// Answers `chat_new_session` with [row] (the chat under its new id) built
|
||||
/// after the move, or an error for a chat id that is not current.
|
||||
JsonMap perfChatNewSessionReply(
|
||||
JsonMap request,
|
||||
JsonMap Function(int index) row,
|
||||
) {
|
||||
final body = request['chat_new_session'];
|
||||
final previous = body is Map ? '${body['session_id'] ?? ''}' : '';
|
||||
final index = perfChatIndex(previous);
|
||||
if (previous != perfChatId(index)) {
|
||||
return {
|
||||
'id': request['id'],
|
||||
'type': 'error',
|
||||
'error': 'this chat has already started a new session',
|
||||
};
|
||||
}
|
||||
_generation[index] = (_generation[index] ?? 0) + 1;
|
||||
final session = {...row(index), 'previous_id': previous};
|
||||
return {
|
||||
'id': request['id'],
|
||||
'type': 'chat_new_session',
|
||||
'chat_session': session,
|
||||
'chat_sessions': [session],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user