1d648c520e
The summary line now carries the host's own disclosure control and, when expanded, the recorded lifecycle, input, output, error, duration and edit diff. Each absent field keeps an exact sentence; no value is inferred from answer text.
195 lines
7.0 KiB
Dart
195 lines
7.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_transcript.dart';
|
|
|
|
/// The exact sentence for a recorded activity field the server did not send.
|
|
/// Input and Output reuse the run detail's own wording, so the same absent fact
|
|
/// never reads differently in this app.
|
|
const String chatActivityLifecycleAbsent = 'No lifecycle recorded.';
|
|
const String chatActivityInputAbsent = 'No input recorded.';
|
|
const String chatActivityOutputAbsent = 'No output recorded.';
|
|
const String chatActivityErrorAbsent = 'No error recorded.';
|
|
const String chatActivityDurationAbsent = 'No duration recorded.';
|
|
const String chatActivityDiffAbsent = 'No edit diff recorded.';
|
|
|
|
/// The recorded detail behind one activity line.
|
|
///
|
|
/// Every field is the value the server recorded on the line's latest record, or
|
|
/// the exact absence sentence above. Nothing is inferred from answer text and an
|
|
/// empty string/map/list is absent rather than a blank block.
|
|
class ChatActivityDetail {
|
|
const ChatActivityDetail({
|
|
required this.lifecycle,
|
|
required this.input,
|
|
required this.output,
|
|
required this.error,
|
|
required this.duration,
|
|
required this.diff,
|
|
});
|
|
|
|
final String lifecycle;
|
|
final String input;
|
|
final String output;
|
|
final String error;
|
|
final String duration;
|
|
final String diff;
|
|
}
|
|
|
|
/// The expanded detail of [record], reading only the fields the parsed activity
|
|
/// record already carries.
|
|
///
|
|
/// Tool-style records keep their payload in `state` (input, content, error and
|
|
/// the invocation's timing envelope), while an activity record may carry the same
|
|
/// names at the top level; the top-level value wins when both are present.
|
|
ChatActivityDetail chatActivityDetail(JsonMap record) {
|
|
final state = record['state'] is Map
|
|
? JsonMap.from(record['state'] as Map)
|
|
: const <String, dynamic>{};
|
|
return ChatActivityDetail(
|
|
lifecycle: _lifecycle(record, state) ?? chatActivityLifecycleAbsent,
|
|
input: _input(record, state) ?? chatActivityInputAbsent,
|
|
output: _output(record, state) ?? chatActivityOutputAbsent,
|
|
error: _error(record, state) ?? chatActivityErrorAbsent,
|
|
duration: _duration(record, state) ?? chatActivityDurationAbsent,
|
|
diff: _diff(record, state) ?? chatActivityDiffAbsent,
|
|
);
|
|
}
|
|
|
|
/// The lifecycle state, including a tool-style `state` map's `status`.
|
|
String? _lifecycle(JsonMap record, JsonMap state) {
|
|
if (record['state'] is! Map) {
|
|
final recorded = chatActivityState(record);
|
|
if (recorded.isNotEmpty) return recorded;
|
|
}
|
|
for (final key in const ['status', 'lifecycle']) {
|
|
final text = _scalar(record[key]);
|
|
if (text != null) return text;
|
|
}
|
|
for (final key in const ['status', 'state', 'lifecycle']) {
|
|
final text = _scalar(state[key]);
|
|
if (text != null) return text;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String? _input(JsonMap record, JsonMap state) =>
|
|
_display(_field(record, state, 'input'));
|
|
|
|
/// The textual output, either an `output` field or a tool-style `content` list
|
|
/// of text parts. A content entry that is not text is not answer output.
|
|
String? _output(JsonMap record, JsonMap state) {
|
|
final direct = _display(_field(record, state, 'output'));
|
|
if (direct != null) return direct;
|
|
final content = _field(record, state, 'content');
|
|
if (content is! List) return null;
|
|
final text = content
|
|
.whereType<Map>()
|
|
.where((item) => item['type'] == 'text')
|
|
.map((item) => '${item['text'] ?? ''}')
|
|
.where((item) => item.trim().isNotEmpty)
|
|
.join('\n');
|
|
return text.isEmpty ? null : text;
|
|
}
|
|
|
|
/// A structured error's message (or type when it has no message), else the
|
|
/// recorded error text.
|
|
String? _error(JsonMap record, JsonMap state) {
|
|
final value = _field(record, state, 'error');
|
|
if (value is Map) {
|
|
return _display(value['message']) ??
|
|
_display(value['type']) ??
|
|
_display(value);
|
|
}
|
|
return _display(value);
|
|
}
|
|
|
|
/// A recorded duration, else one derived from the record's own timing envelope.
|
|
String? _duration(JsonMap record, JsonMap state) {
|
|
final direct = record['duration'] ?? state['duration'];
|
|
if (direct is String && direct.trim().isNotEmpty) return direct;
|
|
if (direct is num) return _formatMillis(direct.round());
|
|
final millis = record['duration_ms'] ?? state['duration_ms'];
|
|
if (millis is num) return _formatMillis(millis.round());
|
|
final time = record['time'] ?? state['time'];
|
|
if (time is! Map) return null;
|
|
final start = _millis(time['ran']) ?? _millis(time['created']);
|
|
final completed = _millis(time['completed']);
|
|
if (start == null || completed == null || completed < start) return null;
|
|
return _formatMillis(completed - start);
|
|
}
|
|
|
|
/// A recorded edit diff, either its text or its line list. A line list keeps the
|
|
/// recorded `+`/`-` gutter; no diff is synthesised from an edit call's strings.
|
|
String? _diff(JsonMap record, JsonMap state) {
|
|
final value = _field(record, state, 'diff');
|
|
if (value is String) return value.trim().isEmpty ? null : value;
|
|
if (value is! Map) return _display(value);
|
|
final lines = value['lines'];
|
|
if (lines is List && lines.isNotEmpty) {
|
|
final rendered = <String>[];
|
|
final file = _display(value['file']);
|
|
if (file != null) rendered.add(file);
|
|
final added = _int(value['added']);
|
|
final removed = _int(value['removed']);
|
|
if (added != null || removed != null) {
|
|
rendered.add('+${added ?? 0} -${removed ?? 0}');
|
|
}
|
|
for (final line in lines) {
|
|
if (line is Map) {
|
|
final text = '${line['text'] ?? ''}';
|
|
switch ('${line['kind'] ?? ''}'.toLowerCase()) {
|
|
case 'add':
|
|
rendered.add('+ $text');
|
|
case 'remove':
|
|
rendered.add('- $text');
|
|
default:
|
|
rendered.add(' $text');
|
|
}
|
|
} else {
|
|
rendered.add('$line');
|
|
}
|
|
}
|
|
return rendered.join('\n');
|
|
}
|
|
return _display(value['text'] ?? value['patch']) ?? _display(value);
|
|
}
|
|
|
|
Object? _field(JsonMap record, JsonMap state, String key) {
|
|
final value = record[key];
|
|
if (!_absent(value)) return value;
|
|
final nested = state[key];
|
|
return _absent(nested) ? null : nested;
|
|
}
|
|
|
|
bool _absent(Object? value) =>
|
|
value == null ||
|
|
(value is String && value.trim().isEmpty) ||
|
|
(value is Map && value.isEmpty) ||
|
|
(value is List && value.isEmpty);
|
|
|
|
/// A scalar's exact text or a map/list as formatted, selectable JSON.
|
|
String? _display(Object? value) {
|
|
if (_absent(value)) return null;
|
|
if (value is String) return value;
|
|
if (value is Map || value is List) {
|
|
return const JsonEncoder.withIndent(' ').convert(value);
|
|
}
|
|
return '$value';
|
|
}
|
|
|
|
/// A scalar's text; a structured value is not a lifecycle state.
|
|
String? _scalar(Object? value) =>
|
|
value is Map || value is List ? null : _display(value);
|
|
|
|
int? _millis(Object? value) => value is num ? value.toInt() : null;
|
|
|
|
int? _int(Object? value) => value is num ? value.toInt() : null;
|
|
|
|
/// The compact duration wording shared with the web's `formatToolDuration`.
|
|
String _formatMillis(int millis) {
|
|
if (millis < 1000) return '${millis}ms';
|
|
if (millis < 60000) return '${(millis / 1000).toStringAsFixed(1)}s';
|
|
return '${millis ~/ 60000}m ${(millis % 60000) ~/ 1000}s';
|
|
}
|