stopGeneration method

Stream stopGeneration({
  1. required String baseDataUrl,
  2. required String mentor,
  3. required String generation_id,
  4. required String token,
  5. required String username,
  6. String tenant = 'main',
})

Implementation

Stream<dynamic> stopGeneration({
  required String baseDataUrl,
  required String mentor,
  required String generation_id,
  required String token,
  required String username,
  String tenant = 'main', // Optional prompt
}) {
  var channel = WebSocketChannel.connect(
    Uri.parse('wss://$baseDataUrl/ws/langflow-stop-generation/'),
  );

  // Initial data without the prompt
  final dataWithoutPrompt = {
    "generation_id": generation_id,
    "name": mentor,
    "tenant": tenant,
    "username": username,
    "token": token
  };

  // Send initial data without prompt
  channel.sink.add(json.encode(dataWithoutPrompt));

  // Return a stream that handles incoming messages
  return channel.stream.map((message) {
    var partialResponse = jsonDecode(message);
    if ((partialResponse.containsKey('error')) ||
        (partialResponse.containsKey('eos') &&
            partialResponse['eos'] == true)) {
      channel.sink.close();
    }
    return partialResponse;
  }).handleError((error) {
    print('Error connecting to server: $error');
    channel.sink.close();
  });
}