feat(app): add native chat composer with persisted drafts and stop
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
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<JsonMap>.broadcast();
|
||||
var _sequence = 0;
|
||||
|
||||
static const _session = 'probe-composer';
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => _frames.stream;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
switch (type) {
|
||||
case 'chat_subscribe':
|
||||
return <String, dynamic>{};
|
||||
case 'chat_messages':
|
||||
return {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
{
|
||||
'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<void>.delayed(const Duration(milliseconds: 450));
|
||||
return queued ? {'chat_queued': true} : <String, dynamic>{};
|
||||
case 'chat_interrupt':
|
||||
Timer(const Duration(milliseconds: 200), _ended);
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
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 _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',
|
||||
onBack: () {},
|
||||
drafts: MemoryChatDraftStore(),
|
||||
newOperationId: gateway.nextOperation,
|
||||
);
|
||||
if (_probeStyle == 'cupertino') {
|
||||
runApp(
|
||||
CupertinoApp(
|
||||
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
||||
home: CupertinoPageScaffold(child: SafeArea(child: thread)),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
runApp(
|
||||
MaterialApp(
|
||||
theme: ThemeData(
|
||||
platform: TargetPlatform.android,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
home: Scaffold(body: SafeArea(child: thread)),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user