Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I stream messages like chatgpt? #588

Open
ReAlign opened this issue Apr 25, 2024 · 3 comments
Open

How do I stream messages like chatgpt? #588

ReAlign opened this issue Apr 25, 2024 · 3 comments
Labels
feature New feature or request

Comments

@ReAlign
Copy link

ReAlign commented Apr 25, 2024

As the title.

@ReAlign ReAlign added the feature New feature or request label Apr 25, 2024
@demchenkoalex
Copy link
Member

no possible with v1. Will try to do in v2. (but please do not wait for me cause it might take lots of time - find an alternative if possible)

@lumpidu
Copy link

lumpidu commented May 1, 2024

When do you plan to release v2 ?

@bawahakim
Copy link

As a workaround, if you get a stream, you can use something like this when you receive the stream.

  void _updateFirstMessageText(String newText, {bool replace = false}) {
    assert(
      _messages.first is types.TextMessage,
      'First message must be a text message',
    );

    final firstMessage = _messages.first as types.TextMessage;
    final updatedText = replace ? newText : firstMessage.text + newText;
    final updatedMessage = firstMessage.copyWith(
      text: updatedText,
    ) as types.TextMessage;

    setState(() {
      _messages.removeAt(0);
      _addMessage(updatedMessage);
    });
  }

You can also emulate streamed text with something like this

bool _isStreaming = false;
final List<String> _messageQueue = [];

  void _onTextReceived(String newText) {
    _messageQueue.add(newText);
    if (!_isStreaming) {
    setState(() {
      final updatedMessage = _messages.first.copyWith(
        text: _messages.first.text + newText,
      ) as types.TextMessage;
      _messages.removeAt(0);
      _addMessage(updatedMessage);
    });
    }
  }

Future<void> _startStreamingMessages() async {
    _isStreaming = true;
    while (_messageQueue.isNotEmpty) {
      final nextMessage = _messageQueue.removeAt(0);
      await _streamText(nextMessage);
    }
    _isStreaming = false;
  }

  Future<void> _streamText(String message) async {
    var currentIndex = 0;
    final completer = Completer<void>();

    Timer.periodic(const Duration(milliseconds: 15), (timer) { // adjust this for the "speed" of the stream
      if (currentIndex < message.length) {
        setState(() {
          final updatedMessage = _messages.first.copyWith(
            text: _messages.first.text + message[currentIndex],
          ) as types.TextMessage;
          _messages.removeAt(0);
          _addMessage(updatedMessage);
          currentIndex++;
        });
      } else {
        timer.cancel();
        completer.complete();
      }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants