feat(chat): files upload over HTTP before the prompt, up to 100 MiB each

This commit is contained in:
2026-10-23 21:25:41 +01:00
parent 39a68a00b3
commit e507f554c3
17 changed files with 634 additions and 74 deletions
@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../core/api_gateway.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/upload/chat_upload.dart';
import '../domain/chat_attachment_draft.dart';
import '../domain/chat_attachment_picker.dart';
import '../domain/chat_attachment_wire.dart';
@@ -101,6 +103,13 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
final List<Uint8List> _bytes = [];
String? _attachmentError;
/// True while the draft's files upload ahead of the prompt.
bool _uploading = false;
/// Upload names by attachment instance, so a retried send reuses the files
/// already stored instead of uploading them again.
final Expando<String> _uploaded = Expando();
/// The draft text whose palette the operator closed (Escape) or completed
/// (a row or Enter). The palette stays hidden while the draft still equals
/// it, so any further typing offers the commands again.
@@ -392,14 +401,39 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
/// accepted prompt clears them.
Future<void> _send() async {
final composer = widget.composer;
if (composer.sending || composer.draft.trim().isEmpty) return;
List<JsonMap> parts;
try {
parts = chatAttachmentParts(_attachments, read: _readAttachment);
} on ChatAttachmentWireException catch (error) {
setState(() => _attachmentError = error.message);
if (_uploading || composer.sending || composer.draft.trim().isEmpty) {
return;
}
List<JsonMap> parts;
final endpoint = configuredGatewayEndpoint(composer.gateway);
try {
if (endpoint.isEmpty || _attachments.isEmpty) {
parts = chatAttachmentParts(_attachments, read: _readAttachment);
} else {
// Each file goes to the server as its own request first; the prompt
// then names the stored files.
setState(() {
_uploading = true;
_attachmentError = null;
});
parts = await chatAttachmentUploadParts(
_attachments,
read: _readAttachment,
upload: (attachment, bytes) async => _uploaded[attachment] ??=
await uploadChatAttachment(endpoint, attachment, bytes),
);
}
} on ChatAttachmentWireException catch (error) {
if (mounted) {
setState(() {
_uploading = false;
_attachmentError = error.message;
});
}
return;
}
if (!mounted) return;
if (_uploading) setState(() => _uploading = false);
// Keep the caret in the field while the prompt is in flight, as before.
_focus.requestFocus();
final accepted = await composer.send(attachments: parts);
@@ -432,7 +466,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
/// Adds each file through the landed attachment draft. A refusal keeps every
/// attachment already in the draft and shows the draft's own words; the
/// refused file's bytes are never retained. Picking and pasting share this
/// path so both apply the same name and 1 MiB decisions.
/// path so both apply the same name and size decisions.
void _addFiles(List<ChatPickedFile> files) {
if (!mounted) return;
setState(() {
@@ -527,6 +561,15 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
),
),
if (!_attachments.isEmpty) _attachmentStrip(context, cupertino),
if (_uploading)
_notice(
context,
cupertino,
key: const Key('chat-composer-uploading'),
text: _attachments.attachments.length == 1
? 'Uploading 1 file…'
: 'Uploading ${_attachments.attachments.length} files…',
),
if (_attachmentError != null)
_notice(
context,
@@ -861,7 +904,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
final stop = widget.running;
final enabled = stop
? !composer.stopping
: !composer.sending && composer.draft.trim().isNotEmpty;
: !_uploading && !composer.sending && composer.draft.trim().isNotEmpty;
void press() {
if (stop) {
unawaited(composer.stop());