Skip to content

[bug] Link Attachments are not supported #137

@Tofu311

Description

@Tofu311

When trying to implement a custom message sender to allow for the handling of larger file attachments, I saw that LinkAttachments aren't supported in the LlmChatView. I've included a snippet of my customMessageSender method below:

  Stream<String> _customMessageSender(
    String prompt, {
    required Iterable<Attachment> attachments,
  }) async* {
    final storageRef = _storage.ref();
    final attachmentsRef = storageRef.child("attachments");
    final List<Map<String, dynamic>> attachmentInfos = [];
    final List<LinkAttachment> linkAttachmentReplacements = [];

    // For every file, upload to GCS bucket
    for (var attachment in attachments) {
      if (attachment is FileAttachment) {
        try {
          final fileRef = attachmentsRef.child(attachment.name);
          final uploadTask = fileRef.putData(
            attachment.bytes,
            SettableMetadata(contentType: attachment.mimeType),
          );

          final snapshot = await uploadTask;
          final url = await snapshot.ref.getDownloadURL();
          attachmentInfos.add({
            'type': 'file',
            'name': attachment.name,
            'mimeType': attachment.mimeType,
            'data': url,
          });

          linkAttachmentReplacements.add(
            LinkAttachment(name: attachment.name, url: Uri.parse(url))
          );

          yield 'Uploaded file: ${attachment.name}';
        } catch (e) {
          yield 'Failed to upload file ${attachment.name}: $e';
        }
      } else if (attachment is LinkAttachment) {
        attachmentInfos.add({
          'type': 'link',
          'name': attachment.name,
          'mimeType': attachment.mimeType,
          'data': attachment.url.toString(),
        });

        linkAttachmentReplacements.add(
            LinkAttachment(name: attachment.name, url: attachment.url)
        );

        yield 'Saved link: ${attachment.name}';
      } else {
        yield 'Unknown attachment type: ${attachment.name}';
      }
    }

    final historyRef = _firestore
        .collection('users')
        .doc(user!.uid)
        .collection('sessions')
        .doc(_currentChatId)
        .collection('history');

    final snapshot = await historyRef.get();
    final nextIndex = snapshot.size.toString().padLeft(3, '0');

    // Save attachment info to Firestore database in the current user's chat session
    if (attachmentInfos.isNotEmpty && _currentChatId != null && user != null) {
      await historyRef.doc(nextIndex).set({
        'attachments': attachmentInfos,
        'origin': "user",
        'text': prompt,
      }, SetOptions(merge: true));
      yield 'All attachments processed and saved!';
    } else {
      yield 'No attachments processed or user/chat/message not found.';
    }

    final response = _provider!.sendMessageStream(
      prompt,
      attachments: linkAttachmentReplacements,
    );

    final text = await response.join();
    yield text;
  }
Image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions