11import './App.css' ;
2- import Chat , { Bubble , useMessages } from '@chatui/core' ;
2+ import Chat , { Bubble , useMessages , Progress } from '@chatui/core' ;
33import '@chatui/core/dist/index.css' ;
4- import React , { useEffect } from 'react'
4+ import '@chatui/core/es/styles/index.less' ;
5+ import React , { useEffect , useState } from 'react' ;
6+ import './chatui-theme.css' ;
57
68const defaultQuickReplies = [
79 {
@@ -48,19 +50,20 @@ const initialMessages = [
4850
4951function App ( ) {
5052 const { messages, appendMsg, setTyping } = useMessages ( initialMessages ) ;
53+ const [ percentage , setPercentage ] = useState ( 0 ) ;
5154
5255 function handleSend ( type , val ) {
5356 if ( type === 'text' && val . trim ( ) ) {
5457 appendMsg ( {
5558 type : 'text' ,
5659 content : { text : val } ,
57- position : 'right ' ,
60+ position : 'left ' ,
5861 user : { avatar : '//gitclone.com/download1/user.png' } ,
5962 } ) ;
6063
6164 setTyping ( true ) ;
62-
63- onGenCode ( val ) ;
65+ setPercentage ( 10 ) ;
66+ onGenCode ( val , val , 0 ) ;
6467 }
6568 }
6669
@@ -90,7 +93,7 @@ function App() {
9093 } else if ( item . name === "Java" ) {
9194 content = "int add(int x,int y){" ;
9295 } else if ( item . name === "javascript" ) {
93- content = "function Add(x,y,z ){" ;
96+ content = "function Add(x,y){" ;
9497 } else if ( item . name === "golang" ) {
9598 content = "func IsBlacklist(bl []string,url string) bool{" ;
9699 } else {
@@ -99,26 +102,57 @@ function App() {
99102 handleSend ( 'text' , content ) ;
100103 }
101104
102- function onGenCode ( context ) {
103- var sl = context . trim ( ) . split ( "\n" ) ;
104- context = sl [ sl . length - 1 ] ;
105- if ( context . trim ( ) === "" ) {
106- alert ( "输入不能为空!" )
105+ function onGenCode ( context_en , context_ch , count ) {
106+ if ( count >= 5 ) {
107+ setPercentage ( 0 ) ;
107108 return ;
108109 }
109110 let xhr = new XMLHttpRequest ( ) ;
110- xhr . open ( 'post' , 'https://gitclone.com/aiit/codegen ' ) ;
111+ xhr . open ( 'post' , 'https://gitclone.com/aiit/codegen_stream ' ) ;
111112 xhr . setRequestHeader ( 'Content-Type' , 'application/json' ) ;
112113 xhr . onload = function ( ) {
113114 var json = JSON . parse ( xhr . response ) ;
114- context = context + "\n" + json . result ;
115- appendMsg ( {
116- type : 'text' ,
117- content : { text : context } ,
118- user : { avatar : '//gitclone.com/download1/gitclone.png' } ,
119- } ) ;
115+ if ( count === 0 ) {
116+ context_en = context_en + "\n" + json . result_en ;
117+ context_ch = context_ch + "\n" + json . result_ch ;
118+ appendMsg ( {
119+ type : 'text' ,
120+ content : { text : context_ch } ,
121+ user : { avatar : '//gitclone.com/download1/gitclone.png' } ,
122+ } ) ;
123+ } else {
124+ if ( ( "" === json . result_en . trim ( ) ) || json . result_en . trim ( ) . startsWith ( "A:" ) || json . result_en . trim ( ) . endsWith ( "A:" ) ) {
125+ setPercentage ( 0 ) ;
126+ return ;
127+ }
128+ context_en = context_en + json . result_en ;
129+ context_ch = context_ch + json . result_ch ;
130+ if ( context_ch === context_en ) {
131+ updateMsg ( context_en ) ;
132+ } else {
133+ updateMsg ( context_ch + "\n" + context_en ) ;
134+ }
135+
136+ }
137+ count ++ ;
138+ setPercentage ( count * 20 ) ;
139+ onGenCode ( context_en , context_ch , count ) ;
140+ }
141+ xhr . send ( JSON . stringify ( {
142+ "context" : context_en ,
143+ "maxlength" : 16 ,
144+ "modelname" : "codegen"
145+ } ) ) ;
146+
147+ function updateMsg ( context_ch ) {
148+ var oUl = document . getElementById ( 'root' ) ;
149+ var aBox = getByClass ( oUl , 'Bubble text' ) ;
150+ if ( aBox . length > 0 ) {
151+ aBox [ aBox . length - 1 ] . innerHTML = "<p>" + context_ch + "</p>" ;
152+ var msgList = getByClass ( oUl , "PullToRefresh" ) [ 0 ] ;
153+ msgList . scrollTo ( 0 , msgList . scrollHeight ) ;
154+ }
120155 }
121- xhr . send ( '{"context":"' + context + '","maxlength":32}' ) ;
122156 }
123157
124158 function findInArr ( arr , n ) {
@@ -169,14 +203,15 @@ function App() {
169203 title : 'More' ,
170204 } ,
171205 ] ,
172- title : '基于Salesforce codegen和GPTJ、GPT-neo的AI代码生成 ' ,
206+ title : '基于Salesforce codegen和GPTJ的AI代码生成 ' ,
173207 } }
174208 messages = { messages }
175209 renderMessageContent = { renderMessageContent }
176210 quickReplies = { defaultQuickReplies }
177211 onQuickReplyClick = { handleQuickReplyClick }
178212 onSend = { handleSend }
179213 />
214+ < Progress value = { percentage } />
180215 </ div >
181216 ) ;
182217}
0 commit comments