REFERENCE
SDK 範例
@asgard-js/core 與 @asgard-js/react 的用法與可以直接操作的範例。
Chatbot Props
以下列出 @asgard-js/react 的 <Chatbot> 元件所有可用 props。
必填
Prop 型別 說明 configClientConfigClient 設定,至少要有 botProviderEndpoint。詳見下方customChannelIdstring唯一識別對話頻道的 ID(通常用 user id / session id) 基本顯示
Prop 型別 預設 說明 titlestring— Header 標題(Bot 名稱列,與 channelTitle不同)avatarstring— Bot 頭像 URL botTypingPlaceholderstring— 已棄用(0.3.x 起),不再渲染任何內容。執行中提示改為自動顯示、與連線狀態綁定的 running indicator(無需設定) locale'en-US' | 'ja-JP' | 'zh-TW''en-US'內建文案語系:工具呼叫標籤、群組摘要、任務 / 子代理面板。見 多語系 channelTitlestring | null— 對話頂端標題列的初始值(通常取自 channelMetadata()),會隨後端title.update動態更新。見 對話標題inputPlaceholderstring— 輸入框 placeholder fullScreenbooleanfalse是否以全螢幕模式渲染 classNamestring— 外層 classNamestyleCSSProperties— 外層 inline style loadingComponentReactNode— 自訂載入中畫面 功能開關
Prop 型別 說明 enableUploadboolean啟用圖片上傳按鈕 enableDocumentUploadboolean啟用文件上傳按鈕 allowedImageMimeTypesstring[]限制 enableUpload可上傳的圖片 MIME 類型;給值即完全覆蓋預設清單。見 檔案上傳allowedDocumentMimeTypesstring[]限制 enableDocumentUpload可上傳的文件 MIME 類型;給值即完全覆蓋預設清單。見 檔案上傳enableExportboolean啟用對話匯出 enableLoadConfigFromServiceboolean從 bot provider 讀取遠端設定(例如 embedConfig)maintainConnectionWhenClosedboolean關閉視窗時保留 SSE 連線 keepConnectionOnUnmountboolean預設 false;元件 unmount 時保留進行中的 SSE run 讓它在後端跑完(與maintainConnectionWhenClosed的「關閉視窗」情境不同)autoResetChannelboolean預設 true。0.3.x 起 mount 會先查 channel metadata:後端已存在的 channel 一律會被還原(重播歷史),不受此設定影響;只有 channel 尚不存在時,false才會略過RESET_CHANNEL以保留initMessages。見 自動重置 ChanneluserIdentityHintstring使用者身分識別提示。設定後所有請求(SSE、上傳)會帶上 X-ASGARD-USER-IDENTITY-HINTheader詳見 功能開關 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 onChannelReady() => voidChannel 就緒可收訊息時觸發(此時 ref 已更新,可在 callback 內用 ref 送開場訊息); resetChannel後會再次觸發onReset() => voidReset channel 時觸發 onClose() => voidChatbot 關閉時觸發 客製渲染
Prop 型別 說明 themePartial<AsgardThemeContextValue>主題設定,見 主題 Demo renderHeader() => ReactNode完全取代預設 header,見 Custom Header renderFooter() => ReactNode完全取代預設 footer(輸入框區),見 Custom Footer renderMenu() => ReactNode在 body 和 footer 之間插入選單,見 Render Menu renderTitle(props) => ReactNode完全取代對話頂端標題列( props為{ title, renderDefault },回傳null隱藏)。搭配untitledLabel(未命名時的字,預設新對話)、channelTitleHidden(直接隱藏)。見 對話標題renderMessageContent(props) => ReactNode完全取代訊息內容渲染,見 Custom Renderer renderToolCallGroup(props) => ReactNode自訂或隱藏 Tool Call Group,見 工具呼叫 errorMessageRenderer(message) => ReactNode自訂錯誤訊息呈現 customActionsReactNode[]塞到 header 右側的自訂按鈕 footerEndActionsReactNode[]塞到 footer input row 最右邊(send / mic 之後)的自訂節點,見 Footer 尾巴按鈕 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 hideRunChromeboolean不渲染內建的停靠 Task / 子代理面板,改由你自行擺放,見 自行停靠面板 Sandbox
Prop 型別 說明 fileExplorer'builtin' | 'off'內建 File Explorer 側欄,預設 'builtin';設'off'改由你擺放<FileExplorerPanel>。見 File ExplorerautoRevealOnOpenFileCardbooleanopen-file卡片抵達時是否自動展開側欄,預設true;檔案有未存檔的修改時會抑制fileExplorerBasePathstring覆寫檔案樹根目錄的絕對路徑,取代 sandbox 的 workingDirectoryonSandboxOpenBrowser(sandboxName) => void接手 sandbox://<name>/open-browser卡片;有給就以你的為準。見 交接卡onSandboxOpenFile(sandboxName, absolutePath) => void接手 sandbox://<name>/open-file卡片。注意這是相加不是取代——內建側欄仍會被拉開,除非同時設fileExplorer="off"sandboxBrowserOpenTarget'_blank' | '_self' | '_parent' | '_top'預設 open-browser 處理器開分頁的目標,預設 '_blank'認證
Prop 型別 說明 authStateAuthState目前的驗證狀態,見下方說明與 Auth Demo onApiKeySubmit(apiKey: string) => Promise<void>使用者送出 API key 時觸發 onAuthError(error: { isAuthError, isBotProviderError, errorDetail }) => void認證錯誤 callback AuthState的可能值:'loading'|'needApiKey'|'authenticated'|'error'|'invalidApiKey'|'subscriptionExpired'|'botNotFound'ClientConfig欄位configprop 的型別是ClientConfig:欄位 型別 說明 botProviderEndpointstring必填。Bot provider base URL endpointstring@deprecated。改用 botProviderEndpointapiKeystring靜態 API key customHeadersRecord<string, string>自訂 HTTP headers(例如 Authorization: Bearer ...)userIdentityHintstring使用者身分識別提示,自動帶上 X-ASGARD-USER-IDENTITY-HINTheader(同頂層 prop)transformSsePayload(payload) => payload送出前變更 SSE payload debugModeboolean開啟 debug log onRunInit/onMessage/onToolCall/onProcess/onRunDone/onRunErrorEventHandler對應各 SSE event 的 handler 將
botProviderEndpoint設為字串"skip"可以進入 preview mode: 不會建立實際連線,只渲染initMessages,本站許多 demo 都使用這個模式。也看看
- useChannel hook — 不使用
<Chatbot>自己組 UI ConversationMessage類型 — 訊息的 union type 結構
- useChannel hook — 不使用