Chat: answered questions collapse into their turn like tool calls
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
// 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},
|
||||
}),
|
||||
];
|
||||
}
|
||||
@@ -12,11 +12,13 @@
|
||||
// them, and keeps colours picked with chat_color;
|
||||
// `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for
|
||||
// scroll captures (tool/perf/chat_scroll_fixture.dart).
|
||||
// `?question=` pushes an agent question (tool/perf/chat_question_fixture.dart).
|
||||
// `/new` in a fixture chat moves it to a fresh, empty session
|
||||
// (tool/perf/chat_new_session_fixture.dart).
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
import 'chat_new_session_fixture.dart';
|
||||
import 'chat_question_fixture.dart';
|
||||
import 'chat_scroll_fixture.dart';
|
||||
|
||||
Map<String, String> _params = const {};
|
||||
@@ -25,6 +27,7 @@ Map<String, String> _params = const {};
|
||||
void configurePerfFixture(Map<String, String> params) {
|
||||
_params = params;
|
||||
configureChatScrollFixture(params);
|
||||
configureChatQuestionFixture(params);
|
||||
}
|
||||
|
||||
int get _hits => int.tryParse(_params['hits'] ?? '') ?? 0;
|
||||
@@ -91,6 +94,8 @@ bool get _scrollVariety => ['msgs', 'rich', 'older'].any(_params.containsKey);
|
||||
|
||||
/// Unsolicited envelopes that follow a reply: the `?quota=` usage frame.
|
||||
List<JsonMap> perfChatFollowUps(JsonMap request) {
|
||||
final answered = chatQuestionFollowUps(request);
|
||||
if (answered.isNotEmpty) return answered;
|
||||
final quota = _params['quota'];
|
||||
if (_chats == 0 || quota == null || request['type'] != 'chat_subscribe') {
|
||||
return const [];
|
||||
@@ -189,6 +194,8 @@ String _answer(int i) =>
|
||||
/// Chat replies for `?chats=N`, or null to fall through to the fixture.
|
||||
JsonMap? perfChatReply(JsonMap request) {
|
||||
if (_chats == 0) return null;
|
||||
final question = chatQuestionReply(request);
|
||||
if (question != null) return question;
|
||||
final id = request['id'];
|
||||
switch ('${request['type']}') {
|
||||
case 'chat_sessions':
|
||||
|
||||
@@ -28,6 +28,7 @@ import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import 'fixture_host/fixture_host.dart';
|
||||
import 'perf/chat_search_fixture.dart';
|
||||
import 'perf/chat_question_fixture.dart';
|
||||
import 'perf/chat_scroll_fixture.dart';
|
||||
import 'perf/perf_fixture.dart';
|
||||
import 'probe_folder_fixture.dart';
|
||||
@@ -527,9 +528,12 @@ class _FixtureChannel implements WebSocketChannel {
|
||||
final followUps = perfChatFollowUps(decoded);
|
||||
final subscribe = decoded['chat_subscribe'];
|
||||
if (subscribe is Map) {
|
||||
chatScrollStream('${subscribe['session_id'] ?? ''}', (frame) {
|
||||
void emit(JsonMap frame) {
|
||||
if (!_incoming.isClosed) _incoming.add(jsonEncode(frame));
|
||||
});
|
||||
}
|
||||
|
||||
chatScrollStream('${subscribe['session_id'] ?? ''}', emit);
|
||||
chatQuestionStream('${subscribe['session_id'] ?? ''}', emit);
|
||||
}
|
||||
if (reply == null && followUps.isEmpty) return;
|
||||
Future<void>.delayed(const Duration(milliseconds: 120), () {
|
||||
|
||||
Reference in New Issue
Block a user