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_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; /// A live-looking fixture conversation with a deterministic composer. /// /// A successful prompt starts a running turn (so the primary control becomes /// Stop); text containing `queued` additionally returns the queued flag. Text /// containing `refuse` is rejected so the browser can see the restored draft /// and the polite error line. Stop ends the turn through a live step frame. class _ProbeGateway implements ApiGateway { final _frames = StreamController.broadcast(); var _sequence = 0; static const _session = 'probe-composer'; @override Stream get frames => _frames.stream; @override Future call(String type, [JsonMap fields = const {}]) async { switch (type) { case 'chat_subscribe': return {}; case 'chat_messages': return { 'chat_history': { 'messages': [ // A long history scrolls behind the floating header. if (_probeLong) for (var i = 0; i < 12; i++) { 'id': 'probe-history-$i', 'role': i.isEven ? 'user' : 'assistant', 'parts': [ { 'type': 'text', 'text': 'Earlier message ${i + 1}: a line of history so ' 'the transcript is taller than the window.', }, ], }, { 'id': 'probe-user', 'role': 'user', 'parts': [ { 'type': 'text', 'text': 'Show me the composer as an operator would use it.', }, ], }, { 'id': 'probe-assistant', 'role': 'assistant', 'parts': [ { 'type': 'text', 'text': 'The composer sits below the conversation with a ' 'multiline field and one primary control.', }, ], }, ], 'next_cursor': '', 'has_more': false, }, }; case 'chat_prompt': final body = fields['chat_prompt'] as JsonMap? ?? const {}; final text = '${body['text'] ?? ''}'; if (text.toLowerCase().contains('refuse')) { throw StateError('The fixture refused this prompt.'); } final queued = text.toLowerCase().contains('queued'); Timer(const Duration(milliseconds: 150), _running); await Future.delayed(const Duration(milliseconds: 450)); return queued ? {'chat_queued': true} : {}; case 'chat_interrupt': Timer(const Duration(milliseconds: 200), _ended); return {}; } return {}; } void _running() => _emit({ 'kind': 'step', 'step': { 'status': 'running', 'turn_id': 'probe-turn', 'intermediate': false, }, }); void _ended() => _emit({ 'kind': 'step', 'step': {'status': 'ended', 'turn_id': 'probe-turn', 'intermediate': false}, }); void _emit(JsonMap body) => _frames.add({ 'type': 'chat_event', 'chat_event': {'session_id': _session, ...body}, }); String nextOperation() => 'probe-op-${++_sequence}'; } const _probeLong = bool.fromEnvironment('CHAT_COMPOSER_PROBE_LONG'); const _probeModel = String.fromEnvironment( 'CHAT_COMPOSER_PROBE_MODEL', defaultValue: 'claude-opus-5-5', ); /// A phone-width frame inside a wider browser window (headless Chrome crops /// windows narrower than about 500px); zero uses the whole window. const _probeWidth = int.fromEnvironment('CHAT_COMPOSER_PROBE_WIDTH'); Widget _framed(Widget child) => _probeWidth == 0 ? child : Center( child: SizedBox(width: _probeWidth.toDouble(), child: child), ); const _probeStyle = String.fromEnvironment( 'CHAT_COMPOSER_PROBE_STYLE', defaultValue: 'material', ); void main() { final gateway = _ProbeGateway(); final thread = ChatThreadView( gateway: gateway, sessionId: 'probe-composer', title: 'Composer fixture', session: const { 'id': 'probe-composer', 'backend': 'opencode', 'model': _probeModel, 'directory': '/Users/ada/repos/tank', }, onBack: () {}, drafts: MemoryChatDraftStore(), newOperationId: gateway.nextOperation, ); if (_probeStyle == 'cupertino') { runApp( CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold(child: _framed(SafeArea(child: thread))), ), ); return; } runApp( MaterialApp( theme: ThemeData( platform: TargetPlatform.android, brightness: Brightness.light, ), home: Scaffold(body: _framed(SafeArea(child: thread))), ), ); }