void _handleReceivedResponse()

in frontend/frontend-flutter/lib/screens/bot.dart [290:424]


  void _handleReceivedResponse(String msg, String type) async {
    String mime = "";
    String id = randomString();
    bool isText = true;
    dynamic dataViz;
    GlobalKey<PaginatedDataTableState> tableKey = GlobalKey();
    imageId = randomString();
    PaginatedDataTable? tableGrid;

    //TextToDocParameter.isTextTodocGlobal = true;
    print('Bot : _handleReceivedResponse(): START ');
    print('Bot : _handleReceivedResponse(): isTextToDoc = ' +
        isTextToDoc.toString());
    print(
        'Bot : _handleReceivedResponse(): TextToDocParameter.isTextTodocGlobal = ${TextToDocParameter.isTextTodocGlobal}');

    switch (type) {
      case "text":
        mime = "application/json";
        break;
      case "pdf":
        mime = "application/pdf";
        break;
      case "png":
        //case "image":
        mime = "image/png";
        break;
      case "jpeg":
        //case "image":
        mime = "image/jpeg";
        break;
      default:
        print('Error, out of range : index = $type ');
    }

    print('Bot : _handleReceivedResponse(): type = ' +
        type +
        ' : mime = ' +
        mime);

    print('Bot : _handleReceivedResponse(): msg = ' + msg);

    if (!TextToDocParameter.isTextTodocGlobal) {
      //NL2SQL request
      print('Bot : _handleReceivedResponse(): USING NL2SQL');

      //Get generated reponse
      var rep = await getChatResponseNew(msg, mime, id, user: _user1);

      print(
          'Bot : _handleReceivedResponse(): back from getChatResponseNew() : rep = $rep');

      //graphConfig = rep![2] as GraphConfig;
      dataViz = rep![2];

      if ((rep![0] as String).length == 0) {
        //knownDB
        rep[0] =
            '[{"response": "The request did not return meaningful information. It could be because the question has not been formulated properly or some context is missing."}]';
      } else {
        //The request has been successful and an entry has been created on ${TextToDocParameter.firestore_database_id}
        //Adding the user_grouping and scenario_name to the entry because as of now it does not contain this data.
        //If in the future user_grouping is added, _updateUserGroupingInSessionLogs() below can be removed
        _updateUserGroupingInSessionLogs();

        tableGrid = createPaginatedTable(rep[0] as String);

        tableKeyList.add(tableKey);
        isTableKeyAdded = true;
        print(
            'Bot : _handleReceivedResponse():  tableKey = $tableKey : isTableKeyAdded = $isTableKeyAdded');
      }
      isText = rep[1] as bool;

      print(
          'Bot : _handleReceivedResponse():  repFinal = ' + (rep[0] as String));
      print(
          'Bot : _handleReceivedResponse():  isText = ' + (rep[1].toString()));

      print(
          'Bot : _handleReceivedResponseNew() : CustomMessage : isText = ${isText}');

      if (isText) {
        print(
            'Bot : _handleReceivedResponseNew() : isText = $isText : dataViz = ${dataViz}');
        imageId = "no_image";
      } else {
        print(
            'Bot : _handleReceivedResponseNew() : CustomMessage : imageId = $imageId');
      }

      final customMessage = types.CustomMessage(
          author: _user1,
          createdAt: DateTime.now().millisecondsSinceEpoch,
          id: randomString(),
          type: types.MessageType.custom,
          metadata: {
            "graph": dataViz,
            "textSummary": rep[3] as String,
            "imageId": imageId,
            "dataSource": tableGrid,
            "tableKey": tableKey.toString(),
          });

      _addMessage(customMessage);
    } else {
      print('Bot : _handleReceivedResponse(): USING TEXT2DOC');

      var rep =
          await getChatResponseTextToDoCStream(msg, mime, id, user: _user1);

      imageId = "no_image";

      final customMessage = types.CustomMessage(
          author: _user1,
          createdAt: DateTime.now().millisecondsSinceEpoch,
          id: randomString(),
          type: types.MessageType.custom,
          metadata: {
            "graph": dataViz,
            "textSummary": rep![3] as String,
            "imageId": imageId,
            "dataSource": null,
            "tableKey": tableKey.toString(),
            "stream": rep![4] as Stream<BaseChunk<Object>> ?? null,
            "stopWatch": rep![5] as Stopwatch ?? null
          });

      _addMessage(customMessage);
    }

    setState(() {
      isProcessing = false;
    });
  }