阿里 Ant X使用
2026-01-19
发送消息
text
1 const {onRequest, messages, isRequesting, abort, onReload} = useXChat({
2 provider: providerFactory(curConversation), // every conversation has its own provider
3 conversationKey: curConversation,
4 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5 // @ts-expect-error
6 defaultMessages: historyMessageFactory(curConversation),
7 requestPlaceholder: () => {
8 return {
9 content: locale.noData,
10 role: 'assistant',
11 };
12 },
13 requestFallback: (_, {error, errorInfo, messageInfo}) => {
14 if (error.name === 'AbortError') {
15 return {
16 content: messageInfo?.message?.content || locale.requestAborted,
17 role: 'assistant',
18 };
19 }
20 return {
21 content: errorInfo?.error?.message || locale.requestFailed,
22 role: 'assistant',
23 };
24 },
25 });自定义发送消息, 比如使用工具情况,这样会导致添加一个一行空的用户消息。所以要用 reload 属性
text
1 onRequest({
2 chatSpaceId: curConversation,
3 confirmed: true,
4 },
5 {
6 reload: true,
7 updatingId: id,
8 })还有一种简单的用法就是用更新消息方法
text
1 const useToolsRequest = (toolCall: PendingToolCall, id: string) => {
2 onReload(id, {
3 chatSpaceId: curConversation,
4 confirmed: true,
5 })
6 }