Files
pipelines/app/tool/chat_approvals_probe.dart
T

168 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
/// Browser fixture for the chat approval and question cards.
///
/// The real [ChatThreadView] drives the real controller against this scripted
/// gateway. The gateway replays one pending permission and one question on
/// subscribe, and echoes the recorded decision/answer back as the server would,
/// so the cards' answered states are produced by the reply path, not hardcoded.
class _ApprovalsGateway implements ApiGateway {
final _frames = StreamController<JsonMap>.broadcast();
bool _sent = false;
@override
Stream<JsonMap> get frames => _frames.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
switch (type) {
case 'chat_subscribe':
if (!_sent) {
_sent = true;
Timer(const Duration(milliseconds: 400), _replayPending);
}
return {};
case 'chat_messages':
return {
'chat_history': {
'messages': [
{
'id': 'probe-user',
'role': 'user',
'parts': [
{
'type': 'text',
'text': 'Show me the recorded approval and question cards.',
},
],
},
],
'next_cursor': '',
'has_more': false,
},
};
case 'chat_reply':
final body = fields['chat_reply'] as Map? ?? const {};
final requestId = '${body['request_id'] ?? ''}';
final decision = '${body['decision'] ?? ''}';
_frames.add({
'type': 'chat_permission',
'chat_permission': {
..._permission,
'resolved': true,
'decision': decision,
'message': 'Recorded by the probe fixture after $decision.',
'request_id': requestId,
},
});
return {};
case 'chat_answer':
final body = fields['chat_answer'] as Map? ?? const {};
_frames.add({
'type': 'chat_event',
'chat_event': {
'session_id': 'probe-session',
'kind': 'form',
'form': {
..._question,
'resolved': true,
'answers': body['answers'] ?? const <String, dynamic>{},
},
},
});
return {};
}
return {};
}
void _replayPending() {
if (_frames.isClosed) return;
_frames.add({'type': 'chat_permission', 'chat_permission': _permission});
_frames.add({
'type': 'chat_event',
'chat_event': {
'session_id': 'probe-session',
'kind': 'form',
'form': _question,
},
});
}
static const _permission = <String, dynamic>{
'session_id': 'probe-session',
'request_id': 'probe-permission',
'action': 'bash',
'resources': [
'command: rm -rf build',
'cwd: /workspace/demo',
],
'message': 'This deletes the build output.',
'source': {'type': 'tool'},
};
static const _question = <String, dynamic>{
'id': 'probe-question',
'session_id': 'probe-session',
'kind': 'question',
'title': 'A demo question',
'actions': ['accept'],
'fields': [
{
'key': 'colour',
'title': 'Colour',
'type': 'string',
'required': true,
'options': [
{'value': 'red', 'label': 'Red'},
{'value': 'blue', 'label': 'Blue'},
],
},
{
'key': 'note',
'title': 'Notes',
'type': 'string',
'placeholder': 'Optional note',
},
],
};
}
const _style = String.fromEnvironment(
'CHAT_APPROVALS_PROBE_STYLE',
defaultValue: 'material',
);
void main() {
final gateway = _ApprovalsGateway();
final thread = ChatThreadView(
gateway: gateway,
sessionId: 'probe-session',
title: 'Probe conversation',
onBack: () {},
);
if (_style == 'cupertino') {
runApp(
CupertinoApp(
theme: const CupertinoThemeData(brightness: Brightness.dark),
home: CupertinoPageScaffold(child: SafeArea(child: thread)),
),
);
return;
}
runApp(
MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
platform: TargetPlatform.android,
),
home: Scaffold(body: SafeArea(child: thread)),
),
);
}