@@ -7,6 +7,7 @@ export const AiChatBackendDemo: React.FC = () => {
7
7
const [ input , setInput ] = React . useState ( '' ) ;
8
8
const [ loading , setLoading ] = React . useState ( false ) ;
9
9
const [ error , setError ] = React . useState ( '' ) ;
10
+ const [ stream , setStream ] = React . useState ( true ) ;
10
11
const abortRef = React . useRef < AbortController | null > ( null ) ;
11
12
12
13
const onSend = async ( ) => {
@@ -21,11 +22,12 @@ export const AiChatBackendDemo: React.FC = () => {
21
22
const assistantDraft : ChatMessage = { role : 'assistant' , content : '' } ;
22
23
setMessages ( prev => [ ...prev , assistantDraft ] ) ;
23
24
try {
24
- await callBackendAi ( {
25
+ const result = await callBackendAi ( {
25
26
model,
26
27
messages : nextMessages ,
27
28
signal : controller . signal ,
28
- onChunk : ( m : { role : 'assistant' ; content : string ; reasoning_content : string ; timestamp : string } ) => {
29
+ stream,
30
+ onChunk : ( m : { role : 'assistant' ; content : string ; reasoning_content : string ; timestamp : string } ) => {
29
31
assistantDraft . content = m . content ;
30
32
setMessages ( prev => {
31
33
const copy = [ ...prev ] ;
@@ -34,6 +36,13 @@ export const AiChatBackendDemo: React.FC = () => {
34
36
} ) ;
35
37
}
36
38
} ) ;
39
+ if ( ! stream ) {
40
+ setMessages ( prev => {
41
+ const copy = [ ...prev ] ;
42
+ copy [ copy . length - 1 ] = { role : 'assistant' , content : result . content } ;
43
+ return copy ;
44
+ } ) ;
45
+ }
37
46
} catch ( e ) {
38
47
setError ( e instanceof Error ? e . message : '调用失败' ) ;
39
48
} finally {
@@ -51,6 +60,10 @@ export const AiChatBackendDemo: React.FC = () => {
51
60
< h2 className = "text-base sm:text-lg font-semibold text-gray-900" > AI 后端代理演示</ h2 >
52
61
< div className = "grid grid-cols-1 sm:grid-cols-3 gap-3" >
53
62
< input className = "px-3 py-2 border rounded col-span-1 sm:col-span-1" placeholder = "Model" value = { model } onChange = { ( e ) => setModel ( e . target . value ) } />
63
+ < label className = "flex items-center gap-2 text-sm col-span-1" >
64
+ < input type = "checkbox" checked = { stream } onChange = { ( e ) => setStream ( e . target . checked ) } />
65
+ < span > 流式</ span >
66
+ </ label >
54
67
</ div >
55
68
< div className = "h-64 border rounded p-3 overflow-auto bg-gray-50" >
56
69
{ messages . map ( ( m , i ) => (
0 commit comments