feat(chat): files upload over HTTP before the prompt, up to 100 MiB each
This commit is contained in:
@@ -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.',
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user