logoAnt Design X

useXChat

Work with agent hook for data management.
Import
import{ useXChat }from"@ant-design/x";

When To Use

Use Agent to manage conversation data and produce data for page rendering.

Examples

Basic usage.

codepen icon
External Link Icon
expand codeexpand code

Update content with stream output.

codepen icon
External Link Icon
expand codeexpand code

Interrupt the ongoing streaming output.

codepen icon
External Link Icon
expand codeexpand code
Hello, what can I do for you?

Return multiple recommended contents by customizing the ability.

codepen icon
External Link Icon
expand codeexpand code

API

API

type useXChat<AgentMessage, ParsedMessage = AgentMessage> = (
config: XChatConfig<AgentMessage, ParsedMessage>,
) => XChatConfigReturnType;

XChatConfig

PropertyDescriptionTypeDefaultVersion
agentagent parameter is required when using the onRequest method in an agent generated by useXAgent.XAgent-
defaultMessagesdefault messages{ status, message }[]-
parserConvert AgentMessage to ParsedMessage for consumption. If not set, AgentMessage will be consumed directly. Supports converting one AgentMessage to multiple ParsedMessages(message: AgentMessage) => BubbleMessage | BubbleMessage[]-
requestFallbackFallback when request fails, not provided will not be displayedAgentMessage | () => AgentMessage-
requestPlaceholderShow the placeholder information when requesting, not provided will not be displayedAgentMessage | () => AgentMessage-

XChatConfigReturnType

PropertyDescriptionTypeVersion
messagesCurrent managed messages contentAgentMessages[]
parsedMessagesParsed messages by parser apiParsedMessages[]
onRequestCreate a message and trigger a request(message) => void
setMessagesModify messages directly without triggering requests(messages: { message, status }[]) => void
Ask me