-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interrupt streaming output demo
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## zh-CN | ||
|
||
打断正在流式输出的内容。 | ||
|
||
## en-US | ||
|
||
Interrupt the ongoing streaming output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { UserOutlined } from '@ant-design/icons'; | ||
import { Bubble, Sender, useXAgent, useXChat, XStream } from '@ant-design/x'; | ||
import { Flex, type GetProp } from 'antd'; | ||
import React, { useRef } from 'react'; | ||
|
||
const roles: GetProp<typeof Bubble.List, 'roles'> = { | ||
ai: { | ||
placement: 'start', | ||
avatar: { icon: <UserOutlined />, style: { background: '#fde3cf' } }, | ||
}, | ||
local: { | ||
placement: 'end', | ||
avatar: { icon: <UserOutlined />, style: { background: '#87d068' } }, | ||
}, | ||
}; | ||
|
||
const contentChunks = [ | ||
'He', | ||
'llo', | ||
', w', | ||
'or', | ||
'ld!', | ||
' Ant', | ||
' Design', | ||
' X', | ||
' is', | ||
' the', | ||
' best', | ||
'!', | ||
]; | ||
|
||
function mockReadableStream() { | ||
const sseChunks: string[] = []; | ||
|
||
for (let i = 0; i < contentChunks.length; i++) { | ||
const sseEventPart = `event: message\ndata: {"id":"${i}","content":"${contentChunks[i]}"}\n\n`; | ||
sseChunks.push(sseEventPart); | ||
} | ||
|
||
return new ReadableStream({ | ||
async start(controller) { | ||
for (const chunk of sseChunks) { | ||
await new Promise((resolve) => setTimeout(resolve, 300)); | ||
controller.enqueue(new TextEncoder().encode(chunk)); | ||
} | ||
controller.close(); | ||
}, | ||
}); | ||
} | ||
|
||
const App = () => { | ||
const [content, setContent] = React.useState(''); | ||
|
||
const abortRef = useRef(() => {}); | ||
|
||
// Agent for request | ||
const [agent] = useXAgent({ | ||
request: async ({}, { onSuccess, onUpdate }) => { | ||
const stream = XStream({ | ||
readableStream: mockReadableStream(), | ||
}); | ||
|
||
const reader = stream.getReader(); | ||
abortRef.current = () => { | ||
reader?.cancel(); | ||
}; | ||
|
||
let current = ''; | ||
while (reader) { | ||
const { value, done } = await reader.read(); | ||
if (done) { | ||
onSuccess(current); | ||
break; | ||
} | ||
if (!value) continue; | ||
const data = JSON.parse(value.data); | ||
current += data.content || ''; | ||
onUpdate(current); | ||
} | ||
}, | ||
}); | ||
|
||
// Chat messages | ||
const { onRequest, messages } = useXChat({ | ||
agent, | ||
}); | ||
|
||
return ( | ||
<Flex vertical gap="middle"> | ||
<Bubble.List | ||
roles={roles} | ||
style={{ maxHeight: 300 }} | ||
items={messages.map(({ id, message, status }) => ({ | ||
key: id, | ||
role: status === 'local' ? 'local' : 'ai', | ||
content: message, | ||
}))} | ||
/> | ||
<Sender | ||
loading={agent.isRequesting()} | ||
value={content} | ||
onChange={setContent} | ||
onSubmit={(nextContent) => { | ||
onRequest(nextContent); | ||
setContent(''); | ||
}} | ||
onCancel={() => abortRef.current()} | ||
/> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters