// `/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 _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], }; }