跳至主要内容

<Chatbot> Props Reference

以下列出 @asgard-js/react<Chatbot> 元件所有可用 props。

必填

Prop型別說明
configClientConfigClient 設定,至少要有 botProviderEndpoint。詳見下方
customChannelIdstring唯一識別對話頻道的 ID(通常用 user id / session id)

基本顯示

Prop型別預設說明
titlestringHeader 標題
avatarstringBot 頭像 URL
botTypingPlaceholderstring正在輸入訊息Bot 正在輸入時的 placeholder
inputPlaceholderstring輸入框 placeholder
fullScreenbooleanfalse是否以全螢幕模式渲染
classNamestring外層 className
styleCSSProperties外層 inline style
loadingComponentReactNode自訂載入中畫面

功能開關

Prop型別說明
enableUploadboolean啟用圖片上傳按鈕
enableDocumentUploadboolean啟用文件上傳按鈕
enableExportboolean啟用對話匯出
enableLoadConfigFromServiceboolean從 bot provider 讀取遠端設定(例如 embedConfig
maintainConnectionWhenClosedboolean關閉視窗時保留 SSE 連線
autoResetChannelboolean預設 true;設為 false 可保留 initMessages 歷史

詳見 功能開關 Demo

初始訊息與事件

Prop型別說明
initMessagesConversationMessage[]初始化時就載入的對話紀錄
onSseMessage(response, { conversation }) => void每一個 SSE 事件的 callback
onSseError(error: unknown) => voidSSE 錯誤 callback
onBeforeSendMessage(params) => params送出前修改 message params。見 before-send-message
onMessageSent() => void訊息送出後 callback
onReset() => voidReset channel 時觸發
onClose() => voidChatbot 關閉時觸發

客製渲染

Prop型別說明
themePartial<AsgardThemeContextValue>主題設定,見 主題 Demo
renderHeader() => ReactNode完全取代預設 header,見 Custom Header
renderMenu() => ReactNode在 body 和 footer 之間插入選單,見 Render Menu
renderMessageContent(props) => ReactNode完全取代訊息內容渲染,見 Custom Renderer
errorMessageRenderer(message) => ReactNode自訂錯誤訊息呈現
customActionsReactNode[]塞到 header 右側的自訂按鈕
messageActions(message) => MessageActionConfig[]每一則 bot 訊息下方的 action 按鈕
onMessageAction(actionId, message) => void點擊 message action 時的 callback
onTemplateBtnClick(payload, eventName, raw) => voidButton template 的 emit action callback
onErrorClick(message) => void點擊錯誤訊息時的 callback
defaultLinkTarget'_blank' | '_self' | '_parent' | '_top'訊息中連結的預設 target

認證

Prop型別說明
authState'authenticated' | 'requireAuth' | 'requireApiKey' | ...目前的驗證狀態,見 Auth Demo
onApiKeySubmit(apiKey: string) => Promise<void>使用者送出 API key 時觸發
onAuthError(error: { isAuthError, isBotProviderError, errorDetail }) => void認證錯誤 callback

ClientConfig 欄位

config prop 的型別是 ClientConfig

欄位型別說明
botProviderEndpointstring必填。Bot provider base URL
endpointstring@deprecated。改用 botProviderEndpoint
apiKeystring靜態 API key
customHeadersRecord<string, string>自訂 HTTP headers(例如 Authorization: Bearer ...
transformSsePayload(payload) => payload送出前變更 SSE payload
debugModeboolean開啟 debug log
onRunInit / onMessage / onToolCall / onProcess / onRunDone / onRunErrorEventHandler對應各 SSE event 的 handler

botProviderEndpoint 設為字串 "skip" 可以進入 preview mode: 不會建立實際連線,只渲染 initMessages,本站許多 demo 都使用這個模式。

也看看