Files
pipelines/app/tool/chat_perf_fixture.dart
T
alan 20bf6202b3 Rebuild only the live chat turn while replies stream
Reuse unchanged transcript rows, the composer bar and the header across
streamed deltas, and share parsed markdown by text so rows scrolled back
into view are not parsed again. Adds a rebuild/parse harness and a
release-web frame-timing probe with a long-transcript fixture.
2026-11-01 23:59:40 +00:00

83 lines
2.8 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:pipelines_app/core/api_gateway.dart';
/// Long-transcript performance fixture for `tool/chat_composer_probe_entry.dart`.
///
/// `?perf=<n>` opens a transcript of n messages whose answers carry headings,
/// inline markdown, a 30-line code block and a list. `?stream=<n>` then streams
/// n text deltas into a new reply, one every 40 ms, starting 6 s after load.
/// With either, every frame's build and raster durations are printed to the
/// console as `PERF_FRAME <build us> <raster us> <total us>` for
/// `tool/chat_perf_probe.mjs`.
int get chatPerfMessages =>
int.tryParse(Uri.base.queryParameters['perf'] ?? '') ?? 0;
int get chatPerfDeltas =>
int.tryParse(Uri.base.queryParameters['stream'] ?? '') ?? 0;
String _answer(int index) =>
'## Answer $index\n\nSome **bold** text with `code` and a '
'[link](https://example.com/$index).\n\n'
'```dart\n'
'${List.generate(30, (line) => 'final v$line = $index * $line;').join('\n')}'
'\n```\n\n'
'- one\n- two\n- three\n';
/// The fixture history; the newest message is the operator's, so a streamed
/// reply opens a new turn.
List<JsonMap> chatPerfTranscript(int count) => [
for (var index = 0; index < count; index++)
(count - index).isOdd
? {
'id': 'perf-u$index',
'role': 'user',
'parts': [
{'type': 'text', 'text': 'Question $index'},
],
}
: {
'id': 'perf-a$index',
'role': 'assistant',
'parts': [
{'type': 'text', 'id': 'perf-t$index', 'text': _answer(index)},
],
},
];
/// Starts the frame report and, when asked, the streamed reply through [emit].
void startChatPerf(void Function(JsonMap body) emit) {
if (chatPerfMessages == 0 && chatPerfDeltas == 0) return;
WidgetsFlutterBinding.ensureInitialized().addTimingsCallback((timings) {
for (final timing in timings) {
// ignore: avoid_print
print(
'PERF_FRAME ${timing.buildDuration.inMicroseconds} '
'${timing.rasterDuration.inMicroseconds} '
'${timing.totalSpan.inMicroseconds}',
);
}
});
final deltas = chatPerfDeltas;
if (deltas == 0) return;
Timer(const Duration(milliseconds: 6000), () {
// ignore: avoid_print
print('PERF_STREAM_START');
var sent = 0;
Timer.periodic(const Duration(milliseconds: 40), (timer) {
emit({
'message_id': 'perf-live',
'kind': 'text',
'part': {'id': 'perf-live-text', 'type': 'text'},
'delta': sent == 0 ? 'Live reply ' : 'word$sent ',
});
if (++sent >= deltas) {
timer.cancel();
// ignore: avoid_print
print('PERF_STREAM_END');
}
});
});
}