87 lines
2.8 KiB
Dart
87 lines
2.8 KiB
Dart
// An agent question for captures over the full-app fixture's `?chats=N`
|
|
// sessions (tool/perf/perf_fixture.dart). Off by default.
|
|
//
|
|
// `?question=opencode` 1.5 s after a chat subscribes, a pending question
|
|
// form arrives; chat_answer is acknowledged and then
|
|
// the server's `form.replied` resolution frame follows
|
|
// (OpenCode);
|
|
// `?question=claude` the same, but only the reply ack settles it (Claude
|
|
// Code's AskUserQuestion: the resolution comes later
|
|
// from the server's readback);
|
|
// `?question=fail` chat_answer is refused, so the card must stay.
|
|
import 'dart:async';
|
|
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
|
|
Map<String, String> _params = const {};
|
|
|
|
void configureChatQuestionFixture(Map<String, String> params) =>
|
|
_params = params;
|
|
|
|
String get _mode => _params['question'] ?? '';
|
|
|
|
JsonMap _event(String sessionId, JsonMap body) => {
|
|
'type': 'chat_event',
|
|
'chat_event': {'session_id': sessionId, ...body},
|
|
};
|
|
|
|
/// Pushes the pending question into [sessionId] through [emit].
|
|
void chatQuestionStream(String sessionId, void Function(JsonMap frame) emit) {
|
|
if (_mode.isEmpty) return;
|
|
Timer(const Duration(milliseconds: 1500), () {
|
|
emit(
|
|
_event(sessionId, {
|
|
'kind': 'form',
|
|
'form': {
|
|
'id': '$sessionId-question',
|
|
'session_id': sessionId,
|
|
'kind': 'question',
|
|
'title': 'Database',
|
|
'fields': [
|
|
{
|
|
'key': 'q0',
|
|
'title': 'Which database should the release use?',
|
|
'type': 'string',
|
|
'required': true,
|
|
'options': [
|
|
{'value': 'Postgres', 'label': 'Postgres'},
|
|
{'value': 'SQLite', 'label': 'SQLite'},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
}
|
|
|
|
/// The chat_answer reply, or null when `?question=` is off.
|
|
JsonMap? chatQuestionReply(JsonMap request) {
|
|
if (_mode.isEmpty || request['type'] != 'chat_answer') return null;
|
|
final id = request['id'];
|
|
if (_mode == 'fail') {
|
|
return {
|
|
'id': id,
|
|
'type': 'error',
|
|
'error': 'the question is no longer awaiting an answer',
|
|
};
|
|
}
|
|
return {'id': id, 'type': 'chat_answer'};
|
|
}
|
|
|
|
/// The OpenCode resolution frame that follows an accepted answer.
|
|
List<JsonMap> chatQuestionFollowUps(JsonMap request) {
|
|
if (_mode != 'opencode' || request['type'] != 'chat_answer') {
|
|
return const [];
|
|
}
|
|
final body = request['chat_answer'];
|
|
if (body is! Map) return const [];
|
|
final session = '${body['session_id'] ?? ''}';
|
|
return [
|
|
_event(session, {
|
|
'kind': 'form',
|
|
'form': {'id': body['form_id'], 'session_id': session, 'resolved': true},
|
|
}),
|
|
];
|
|
}
|