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
@@ -0,0 +1,81 @@
import 'dart:convert';
import 'dart:typed_data';
import '../../../../core/transport/server_url.dart';
import '../../domain/chat_attachment_draft.dart';
import '../../domain/chat_attachment_wire.dart';
import 'chat_upload_stub.dart'
if (dart.library.io) 'chat_upload_io.dart'
if (dart.library.js_interop) 'chat_upload_web.dart'
as platform;
/// The server's reply to one stored chat file.
typedef ChatUploadReply = ({int status, String body});
/// Posts one file to the server's `/chat-uploads`: the bytes travel as their
/// own HTTP request, outside the websocket frame, and the reply names the
/// stored file a prompt then references.
Future<ChatUploadReply> postChatUpload(
Uri uri, {
required String name,
required String mime,
required Uint8List bytes,
}) => platform.postChatUpload(uri, name: name, mime: mime, bytes: bytes);
/// The header that carries the file name; the server refuses a post without it.
const chatUploadNameHeader = 'X-Pipelines-Upload-Name';
/// Uploads [bytes] for [attachment] to the server behind [endpoint] and
/// returns the stored name a prompt references. Every failure is a
/// [ChatAttachmentWireException] in words the composer shows as they are.
Future<String> uploadChatAttachment(
String endpoint,
ChatAttachment attachment,
Uint8List bytes,
) async {
final uri = ServerUrl.resolveArtifactUrl(endpoint, '/chat-uploads');
if (uri == null) {
throw const ChatAttachmentWireException(
'No server is configured to upload files to.',
);
}
final ChatUploadReply reply;
try {
reply = await postChatUpload(
uri,
name: attachment.name.trim().isEmpty ? 'attachment' : attachment.name,
mime: attachment.mime.trim().isEmpty
? 'application/octet-stream'
: attachment.mime,
bytes: bytes,
);
} catch (error) {
throw ChatAttachmentWireException(
'"${attachment.name}" could not be uploaded: $error',
);
}
if (reply.status == 401) {
throw const ChatAttachmentWireException(
'Sign in to upload files (Settings → Sign in).',
);
}
if (reply.status != 200) {
final reason = reply.body.trim();
throw ChatAttachmentWireException(
reason.isEmpty
? '"${attachment.name}" could not be uploaded (HTTP ${reply.status}).'
: '"${attachment.name}" could not be uploaded: $reason',
);
}
try {
final decoded = jsonDecode(reply.body);
final upload = decoded is Map ? decoded['upload'] : null;
if (upload is String && upload.isNotEmpty) return upload;
} on FormatException {
// Reported below.
}
throw ChatAttachmentWireException(
'"${attachment.name}" upload returned no file name.',
);
}
@@ -0,0 +1,31 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import '../../../../core/session/server_session.dart';
import 'chat_upload.dart';
Future<ChatUploadReply> postChatUpload(
Uri uri, {
required String name,
required String mime,
required Uint8List bytes,
}) async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 15);
try {
final request = await client.postUrl(uri);
request.followRedirects = false;
// A phone behind the web login carries its kept session.
ServerSession.headers.forEach(request.headers.set);
request.headers
..set(chatUploadNameHeader, Uri.encodeComponent(name))
..set(HttpHeaders.contentTypeHeader, mime)
..contentLength = bytes.length;
request.add(bytes);
final response = await request.close();
final body = await response.transform(utf8.decoder).join();
return (status: response.statusCode, body: body);
} finally {
client.close(force: true);
}
}
@@ -0,0 +1,10 @@
import 'dart:typed_data';
import 'chat_upload.dart';
Future<ChatUploadReply> postChatUpload(
Uri uri, {
required String name,
required String mime,
required Uint8List bytes,
}) => throw UnsupportedError('Uploads are not supported on this platform.');
@@ -0,0 +1,46 @@
import 'dart:js_interop';
import 'dart:typed_data';
import 'chat_upload.dart';
@JS('fetch')
external JSPromise<_Response> _fetch(JSString input, _Init init);
extension type _Init._(JSObject _) implements JSObject {
external factory _Init({
JSString method,
JSAny headers,
JSAny body,
JSString credentials,
});
}
extension type _Response._(JSObject _) implements JSObject {
external int get status;
external JSPromise<JSString> text();
}
/// The browser keeps the login cookie; `same-origin` sends it only to the
/// page's own server.
Future<ChatUploadReply> postChatUpload(
Uri uri, {
required String name,
required String mime,
required Uint8List bytes,
}) async {
final response = await _fetch(
uri.toString().toJS,
_Init(
method: 'POST'.toJS,
headers: {
chatUploadNameHeader: Uri.encodeComponent(name),
'Content-Type': mime,
}.jsify()!,
body: bytes.toJS,
credentials: 'same-origin'.toJS,
),
).toDart;
final body = (await response.text().toDart).toDart;
return (status: response.status, body: body);
}