feat: a transport seam, image previews, native forms, a phone drawer
- lib/api/transport.dart: TransportRequest/TransportResponse/Transport, with DirectTransport holding the old behaviour (HTTP CONNECT, SOCKS5/socks5h, self-signed and pinned certificates). Client goes through it, so a relay tunnel only adds an implementation. - User rows decode the server's inline attachment data and open the shared image preview on tap; the local path stays the fallback. - The agent's native forms: models from @opencode/client, the form list, replies and cancels, form.* events, and a dock above the composer with when-rules and required-field validation (a switch must be on). - HomeScreen: the session list is the phone's left drawer and a persistent column from 840 px, the chat beside it; the newest session opens on start. - 0.3.10 (310).
This commit is contained in:
1 parent
9b53338908
commit
60d02083d3
11 files changed
+1272
-320
No files matched your search
+479
-14
@@ -18,7 +18,16 @@ import 'files_panel.dart';
|
||||
class ChatScreen extends StatefulWidget {
|
||||
final AppState app;
|
||||
final Session session;
|
||||
const ChatScreen({super.key, required this.app, required this.session});
|
||||
|
||||
/// Opens the session drawer. Null where the list is always on screen (a
|
||||
/// wide layout), so no menu button is shown there.
|
||||
final VoidCallback? onOpenSessions;
|
||||
const ChatScreen({
|
||||
super.key,
|
||||
required this.app,
|
||||
required this.session,
|
||||
this.onOpenSessions,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ChatScreen> createState() => _ChatScreenState();
|
||||
@@ -36,6 +45,9 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
List<_Attach> _attachments = [];
|
||||
bool _attaching = false;
|
||||
|
||||
/// Decoded user attachments, so a rebuild does not base64-decode again.
|
||||
final Map<String, Uint8List> _userImages = {};
|
||||
|
||||
/// `/` palette: the highlighted row, and the draft Esc closed it at.
|
||||
int _slashIndex = 0;
|
||||
String? _slashDismissed;
|
||||
@@ -90,6 +102,13 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
final theme = Theme.of(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: widget.onOpenSessions == null
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.menu),
|
||||
tooltip: '会话',
|
||||
onPressed: widget.onOpenSessions,
|
||||
),
|
||||
title: Text(widget.session.title.isEmpty ? '会话' : widget.session.title,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
actions: [
|
||||
@@ -131,6 +150,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
),
|
||||
Expanded(child: _messageList()),
|
||||
_permissions(),
|
||||
_forms(),
|
||||
_composer(),
|
||||
],
|
||||
),
|
||||
@@ -376,8 +396,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
Widget _userRow(String id) {
|
||||
final m = chat.byId[id]!;
|
||||
final images = chat.imagePaths[id] ?? [];
|
||||
final theme = Theme.of(context);
|
||||
// The server keeps attached images in the message; the paths only cover
|
||||
// the moment between sending and the next refresh.
|
||||
final attachedImages = [
|
||||
for (var i = 0; i < m.files.length; i++)
|
||||
if (m.files[i].isImage) i,
|
||||
];
|
||||
final localImages = attachedImages.isEmpty ? chat.imagePaths[id] ?? [] : const <String>[];
|
||||
final otherFiles = m.files.where((f) => !f.isImage).toList();
|
||||
return Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Container(
|
||||
@@ -393,20 +420,43 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
children: [
|
||||
if (m.text.isNotEmpty)
|
||||
Text(m.text, style: TextStyle(color: theme.colorScheme.onPrimary)),
|
||||
for (final p in images)
|
||||
for (final i in attachedImages)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Image.file(File(p), width: 200, height: 150, fit: BoxFit.cover),
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: GestureDetector(
|
||||
onTap: () => _previewAttachment(m.files[i]),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: _attachmentImage(id, i, m.files[i]),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (m.files.isNotEmpty)
|
||||
for (final p in localImages)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
m.files.map((f) => '📎 ${f.name}').join('\n'),
|
||||
style: TextStyle(color: theme.colorScheme.onPrimary, fontSize: 13),
|
||||
child: GestureDetector(
|
||||
onTap: () => _openPreview(_ImageRef(p, p.split('/').last)),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Image.file(File(p),
|
||||
width: 200, height: 150, fit: BoxFit.cover),
|
||||
),
|
||||
),
|
||||
),
|
||||
for (final f in otherFiles)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.onPrimary.withValues(alpha: 0.14),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text('📎 ${f.name.isEmpty ? '附件' : f.name}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onPrimary, fontSize: 12.5)),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -415,6 +465,28 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _attachmentImage(String messageId, int index, FileAttachment f) {
|
||||
final key = '$messageId/$index';
|
||||
Uint8List? bytes = _userImages[key];
|
||||
if (bytes == null) {
|
||||
try {
|
||||
bytes = base64Decode(f.data);
|
||||
_userImages[key] = bytes;
|
||||
} catch (_) {
|
||||
return const SizedBox(
|
||||
width: 200, height: 60, child: Center(child: Text('图片无法显示')));
|
||||
}
|
||||
}
|
||||
return Image.memory(bytes, width: 200, height: 150, fit: BoxFit.cover);
|
||||
}
|
||||
|
||||
void _previewAttachment(FileAttachment f) {
|
||||
_showImageDialog(
|
||||
f.name.isEmpty ? '图片' : f.name,
|
||||
source: 'data:${f.mime};base64,${f.data}',
|
||||
);
|
||||
}
|
||||
|
||||
Widget _userActions(String id) {
|
||||
final m = chat.byId[id]!;
|
||||
final stamp = m.created > 0
|
||||
@@ -1747,6 +1819,14 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
// ---------------------------------------------------------- permissions
|
||||
|
||||
/// The agent's blocking questions sit just above the composer, like the
|
||||
/// permissions dock: answering one lets the turn continue.
|
||||
Widget _forms() {
|
||||
if (chat.forms.isEmpty) return const SizedBox.shrink();
|
||||
final form = chat.forms.first;
|
||||
return _FormDock(key: ValueKey(form.id), chat: chat, form: form);
|
||||
}
|
||||
|
||||
Widget _permissions() {
|
||||
final perms = widget.app.perms
|
||||
.where((p) => p.sessionId.isEmpty || p.sessionId == chat.id)
|
||||
@@ -1922,12 +2002,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
// ------------------------------------------------------------ previews
|
||||
|
||||
Future<void> _openPreview(_ImageRef ref) async {
|
||||
Future<void> _openPreview(_ImageRef ref) =>
|
||||
_showImageDialog(ref.label, source: ref.source);
|
||||
|
||||
Future<void> _showImageDialog(String title, {required String source}) async {
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (_) => _ImagePreviewDialog(
|
||||
title: ref.label,
|
||||
source: ref.source,
|
||||
title: title,
|
||||
source: source,
|
||||
directory: chat.dir,
|
||||
client: widget.app.client!,
|
||||
),
|
||||
@@ -2300,3 +2383,385 @@ class _ImagePreviewDialogState extends State<_ImagePreviewDialog> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The native form/question card: every visible field at once, one answer.
|
||||
/// Field visibility follows the form's `when` clauses, and required fields
|
||||
/// must be filled before it can be sent.
|
||||
class _FormDock extends StatefulWidget {
|
||||
final ChatState chat;
|
||||
final FormInfo form;
|
||||
const _FormDock({super.key, required this.chat, required this.form});
|
||||
|
||||
@override
|
||||
State<_FormDock> createState() => _FormDockState();
|
||||
}
|
||||
|
||||
class _FormDockState extends State<_FormDock> {
|
||||
final Map<String, Object?> _values = {};
|
||||
final Map<String, TextEditingController> _controllers = {};
|
||||
String? _error;
|
||||
bool _sending = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
for (final f in widget.form.fields) {
|
||||
switch (f.type) {
|
||||
case 'boolean':
|
||||
_values[f.key] = f.defaultValue is bool ? f.defaultValue : false;
|
||||
break;
|
||||
case 'multiselect':
|
||||
_values[f.key] = f.defaultValue is List
|
||||
? List<String>.from((f.defaultValue as List).whereType<String>())
|
||||
: <String>[];
|
||||
break;
|
||||
case 'external':
|
||||
_values[f.key] = true;
|
||||
break;
|
||||
default:
|
||||
if (f.defaultValue != null) {
|
||||
_values[f.key] = f.defaultValue;
|
||||
_controllers[f.key] =
|
||||
TextEditingController(text: '${f.defaultValue}');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final c in _controllers.values) {
|
||||
c.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool _visible(AgentFormField f) {
|
||||
if (f.hidden) return false;
|
||||
for (final w in f.when) {
|
||||
final v = _values[w.key];
|
||||
final eq = v == w.value ||
|
||||
(v is num && w.value is num && v == w.value) ||
|
||||
(v is bool && w.value is bool && v == w.value);
|
||||
if (w.op == 'neq' ? eq : !eq) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TextEditingController _controller(AgentFormField f) =>
|
||||
_controllers.putIfAbsent(f.key, () => TextEditingController());
|
||||
|
||||
void _set(AgentFormField f, Object? value) {
|
||||
setState(() {
|
||||
_values[f.key] = value;
|
||||
_error = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
final visible = widget.form.fields.where(_visible).toList();
|
||||
final missing = <String>[];
|
||||
for (final f in visible) {
|
||||
if (!f.required || f.type == 'external') continue;
|
||||
final v = _values[f.key];
|
||||
// A required switch or acknowledgement has to be on, not merely set.
|
||||
final empty = v == null ||
|
||||
v == false ||
|
||||
(v is String && v.trim().isEmpty) ||
|
||||
(v is List && v.isEmpty);
|
||||
if (empty) missing.add(f.label);
|
||||
}
|
||||
if (missing.isNotEmpty) {
|
||||
setState(() => _error = '请填写:${missing.join('、')}');
|
||||
return;
|
||||
}
|
||||
final answer = <String, Object?>{};
|
||||
for (final f in visible) {
|
||||
if (f.type == 'external') {
|
||||
answer[f.key] = true;
|
||||
continue;
|
||||
}
|
||||
final v = _values[f.key];
|
||||
if (v == null) continue;
|
||||
if (v is String && v.trim().isEmpty) continue;
|
||||
if (v is List && v.isEmpty) continue;
|
||||
answer[f.key] = v;
|
||||
}
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
});
|
||||
await widget.chat.replyForm(widget.form, answer);
|
||||
if (mounted) setState(() => _sending = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final scheme = theme.colorScheme;
|
||||
final fields = widget.form.fields.where(_visible).toList();
|
||||
return Card(
|
||||
margin: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
||||
color: scheme.surfaceContainerHigh,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 10, 12, 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.help_outline, size: 18, color: scheme.primary),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
widget.form.title.isEmpty ? '请选择' : widget.form.title,
|
||||
style: const TextStyle(
|
||||
fontSize: 14, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
minimumSize: const Size(0, 32),
|
||||
),
|
||||
onPressed: _sending
|
||||
? null
|
||||
: () => widget.chat.cancelForm(widget.form),
|
||||
child: const Text('取消', style: TextStyle(fontSize: 12.5)),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (fields.isNotEmpty)
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 360),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
for (final f in fields) _field(f, theme),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(_error!,
|
||||
style: TextStyle(fontSize: 12, color: scheme.error)),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: _sending ? null : _submit,
|
||||
child: _sending
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Text('发送'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _field(AgentFormField f, ThemeData theme) {
|
||||
final scheme = theme.colorScheme;
|
||||
final title = Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(f.label,
|
||||
style:
|
||||
const TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
if (f.required)
|
||||
Text(' *', style: TextStyle(color: scheme.error, fontSize: 13)),
|
||||
],
|
||||
);
|
||||
Widget hint() => f.description == null || f.description!.isEmpty
|
||||
? const SizedBox.shrink()
|
||||
: Padding(
|
||||
padding: const EdgeInsets.only(top: 2, bottom: 2),
|
||||
child: Text(f.description!,
|
||||
style: TextStyle(fontSize: 12, color: theme.hintColor)),
|
||||
);
|
||||
switch (f.type) {
|
||||
case 'external':
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
if (f.url != null && f.url!.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: Text(f.url!,
|
||||
style: TextStyle(
|
||||
fontSize: 12, color: scheme.primary),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'boolean':
|
||||
return SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
title: title,
|
||||
subtitle: hint(),
|
||||
value: _values[f.key] == true,
|
||||
onChanged: (v) => _set(f, v),
|
||||
);
|
||||
case 'multiselect':
|
||||
final selected = List<String>.from(
|
||||
(_values[f.key] as List?)?.whereType<String>() ?? const []);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: -6,
|
||||
children: [
|
||||
for (final o in f.options)
|
||||
FilterChip(
|
||||
label: Text(o.label, style: const TextStyle(fontSize: 12.5)),
|
||||
selected: selected.contains(o.value),
|
||||
onSelected: (on) {
|
||||
final next = [...selected];
|
||||
if (on) {
|
||||
next.add(o.value);
|
||||
} else {
|
||||
next.remove(o.value);
|
||||
}
|
||||
_set(f, next);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'string':
|
||||
if (f.options.isNotEmpty) {
|
||||
final current = _values[f.key]?.toString();
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: -6,
|
||||
children: [
|
||||
for (final o in f.options)
|
||||
ChoiceChip(
|
||||
label: Text(o.label,
|
||||
style: const TextStyle(fontSize: 12.5)),
|
||||
selected: current == o.value,
|
||||
onSelected: (_) => _set(f, o.value),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (f.custom)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: TextField(
|
||||
controller: _controller(f),
|
||||
onChanged: (v) => _set(f, v),
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: '其他…',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
final numeric = f.format == 'number';
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
const SizedBox(height: 4),
|
||||
TextField(
|
||||
controller: _controller(f),
|
||||
onChanged: (v) => _set(f, v),
|
||||
keyboardType: f.format == 'email'
|
||||
? TextInputType.emailAddress
|
||||
: f.format == 'uri'
|
||||
? TextInputType.url
|
||||
: numeric
|
||||
? TextInputType.number
|
||||
: TextInputType.text,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: f.placeholder ??
|
||||
(f.format == 'date' ? 'YYYY-MM-DD' : null),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
const SizedBox(height: 4),
|
||||
TextField(
|
||||
controller: _controller(f),
|
||||
keyboardType:
|
||||
TextInputType.numberWithOptions(decimal: f.type == 'number'),
|
||||
onChanged: (v) {
|
||||
final n = f.type == 'integer'
|
||||
? int.tryParse(v)
|
||||
: num.tryParse(v);
|
||||
_set(f, n ?? (v.isEmpty ? null : v));
|
||||
},
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
default:
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text('${f.label}(${f.type})',
|
||||
style: const TextStyle(fontSize: 13)),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user