// Transcript variety for chat scroll captures over the full-app fixture's // `?chats=N` sessions (tool/perf/perf_fixture.dart). Every option defaults to // off, so the bench's short markdown transcripts are unchanged. // // `?msgs=60,3` messages per chat, by chat index (the last count repeats); // `?rich=1` answers carry a wide code line and a wide table, operator // turns an image, and every fifth turn an agent report and a // harness notice; // `?older=1` the first page reports more history; Load older adds 20 // earlier messages; // `?live=N` 3 s after a chat subscribes, N text deltas stream into a // new reply, one every 60 ms. import 'dart:async'; import 'package:pipelines_app/core/api_gateway.dart'; Map _params = const {}; void configureChatScrollFixture(Map params) => _params = params; bool get _rich => _params['rich'] == '1'; bool get _older => _params['older'] == '1'; int get _live => int.tryParse(_params['live'] ?? '') ?? 0; /// The message count for chat [index] (`perf-chat-`). int chatScrollMessageCount(int index) { final counts = [ for (final value in (_params['msgs'] ?? '').split(',')) ?int.tryParse(value.trim()), ]; if (counts.isEmpty) return 20; return counts[index < counts.length ? index : counts.length - 1]; } /// A 96x64 PNG, so an operator turn draws an image thumbnail. const _png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABACAIAAABqVuVZAAAAeklE' 'QVR4nO3QMQ0AIADAMOQgEYnIwgEPBzuaTMDSMdfWpfH9IB4gQIAAAQoHCBAgQIDCAQIECBCg' 'cIAAAQIEKBwgQIAAAQoHCBAgQIDCAQIECBCgcIAAAQIEKBwgQIAAAQoHCBAgQIDCAQIECBCg' 'cIAAAQIEKBwgQG8du9DB8GFqxbUAAAAASUVORK5CYII='; String _answer(int i) { final wide = _rich ? '\n```sh\n${List.filled(14, 'go test ./internal/server/... -run Test$i').join(' && ')}\n```\n\n' '| ${[for (var c = 0; c < 14; c++) 'Column $c'].join(' | ')} |\n' '| ${List.filled(14, '---').join(' | ')} |\n' '| ${[for (var c = 0; c < 14; c++) 'value $i.$c'].join(' | ')} |\n\n' : ''; return '## Answer $i\n\nSome **bold** text with `code`.\n\n' '```dart\n${List.generate(8, (l) => 'final v$l = $i * $l;').join('\n')}' '\n```\n$wide\n- one\n- two\n'; } JsonMap _message(String chat, int i) { final id = '$chat-m$i'; if (_rich && i % 10 == 5) { return { 'id': id, 'role': 'user', 'kind': 'agent_report', 'parts': [ { 'type': 'text', 'name': 'reviewer', 'text': '## Review of change $i\n\nAll checks passed.\n\n${_answer(i)}', }, ], }; } if (_rich && i % 10 == 7) { return { 'id': id, 'role': 'user', 'kind': 'notice', 'parts': [ {'type': 'text', 'text': 'Background task $i finished.'}, ], }; } if (i.isEven) { return { 'id': id, 'role': 'user', 'parts': [ if (_rich && i % 4 == 0) { 'type': 'file', 'mime': 'image/png', 'name': 'shot$i.png', 'uri': _png, }, {'type': 'text', 'text': 'Question $i'}, ], }; } return { 'id': id, 'role': 'assistant', 'parts': [ {'type': 'text', 'text': _answer(i)}, ], }; } /// One history page for chat [index]: the newest messages, or with `?older=1` /// and a cursor, the 20 messages before them. JsonMap chatScrollHistory(String sessionId, int index, String cursor) { final count = chatScrollMessageCount(index); if (_older && cursor == 'older') { return { 'messages': [for (var i = -20; i < 0; i++) _message(sessionId, i)], 'next_cursor': '', 'has_more': false, }; } return { 'messages': [for (var i = 0; i < count; i++) _message(sessionId, i)], 'next_cursor': _older ? 'older' : '', 'has_more': _older, }; } /// Streams the `?live=N` reply into [sessionId] through [emit]. void chatScrollStream(String sessionId, void Function(JsonMap frame) emit) { final deltas = _live; if (deltas == 0) return; JsonMap event(JsonMap body) => { 'type': 'chat_event', 'chat_event': {'session_id': sessionId, ...body}, }; Timer(const Duration(seconds: 3), () { emit( event({ 'kind': 'step', 'step': {'status': 'running', 'turn_id': 'live', 'intermediate': false}, }), ); var sent = 0; Timer.periodic(const Duration(milliseconds: 60), (timer) { emit( event({ 'message_id': '$sessionId-live', 'kind': 'text', 'part': {'id': '$sessionId-live-text', 'type': 'text'}, 'delta': sent % 12 == 11 ? 'word$sent\n\n' : 'word$sent ', }), ); if (++sent >= deltas) timer.cancel(); }); }); }