1
秦芳睿
9 天以前 e70a362606b78a822e93d5117a9013e8f9086faf
JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/core/SMTAIServerRequest.java
@@ -98,6 +98,7 @@
   private List<AIAttachFile>      _listAttchFile = null;
   private Json               _jsonAttachTables = null;
   private Json               _jsonAttachMetrics = null;
   private long               _lastSendChunkedStreamBlockTick = 0;
   private boolean               _sendStarStream = false; // 将输出文字变成*
@@ -127,6 +128,8 @@
   public boolean setSendStarStream(boolean isSetStartStream)
   {
      boolean org = _sendStarStream;
      if("true".equals(System.getProperty("show_ai_stream")))
         isSetStartStream = false;
      _sendStarStream = isSetStartStream; 
      return org;
   }
@@ -184,6 +187,40 @@
            list.add(sbText.toString());
         }
      }
      if(_jsonAttachMetrics != null)
      {
         for(Json jsonAttachTable : _jsonAttachMetrics.asJsonList())
         {
            StringBuilder sbText = new StringBuilder();
            sbText.append("现有表格:" + jsonAttachTable.getJson("title").asString() + "\n");
            List<Json> jsonColumns = jsonAttachTable.getJson("columns").asJsonList();
            for(int i = 0; i < jsonColumns.size(); i ++)
            {
               if(i > 0)
                  sbText.append(",");
               sbText.append(jsonColumns.get(i).asString());
            }
            sbText.append("\n");
            for(Json jsonRecord : jsonAttachTable.getJson("values").asJsonList())
            {
               List<Json> listRec = jsonRecord.asJsonList();
               for(int i = 0; i < listRec.size(); i ++)
               {
                  if(i > 0)
                     sbText.append(",");
                  if(listRec.get(i).isNull())
                     sbText.append("");
                  else
                     sbText.append(listRec.get(i).asString());
               }
               sbText.append("\n");
            }
            sbText.append("\n");
            list.add(sbText.toString());
         }
      }
      
      return list.size() == 0 ? null : list.toArray(new String[list.size()]);
   }
@@ -528,6 +565,11 @@
   {
      if(_sendStarStream)
      {
         if((System.currentTimeMillis() - _lastSendChunkedStreamBlockTick) < 2000)
            return;
         _lastSendChunkedStreamBlockTick = System.currentTimeMillis();
         stream = "#";
         if(stream.indexOf("\n") >= 0)
            stream += "\n";
@@ -537,9 +579,6 @@
   
   public void sendChunkedBlock(String mode, Object value) throws Exception
   {
      if(!_isChunked)
         return;
      Json jsonResult;
      
      if(value == null)
@@ -569,15 +608,23 @@
            _listChatExecProcess = new ArrayList<>();
         _listChatExecProcess.add(jsonResult);
      }
      _logger.info("sendChunkedBlock:" + jsonResult.toString());
      String text = URLEncoder.encode(jsonResult.toString(), "UTF-8").replace("+", "%20") + "\n";
      this.getResponse().getOutputStream().write(text.getBytes("UTF-8"));
      
//      this.getResponse().getWriter().print(String.format(
//         "%d\r\n%s\r\n", text.length(), text
//         ));
      this.getResponse().flushBuffer();
      if(!_isChunked)
      {
         _logger.info("NO sendChunkedBlock:" + jsonResult.toString());
      }
      else
      {
         _logger.info("sendChunkedBlock:" + jsonResult.toString());
         String text = URLEncoder.encode(jsonResult.toString(), "UTF-8").replace("+", "%20") + "\n";
         this.getResponse().getOutputStream().write(text.getBytes("UTF-8"));
   //      this.getResponse().getWriter().print(String.format(
   //         "%d\r\n%s\r\n", text.length(), text
   //         ));
         this.getResponse().flushBuffer();
      }
   }
   
   public String getClientAddr()