REFERENCE
SDK 範例
@asgard-js/core 與 @asgard-js/react 的用法與可以直接操作的範例。
私有機器人
連線到一個需要 API key 的私有 bot provider。SDK 會自動處理
未認證 → needApiKey → authenticated 的狀態轉換。
程式範例
import { useState } from "react";
import { Chatbot } from "@asgard-js/react";
import type { AuthState } from "@asgard-js/core";
export function PrivateBot() {
const [authState, setAuthState] = useState<AuthState>("needApiKey");
return (
<Chatbot
title="私有機器人"
config={{
botProviderEndpoint: "https://...",
apiKey: authState === "authenticated" ? storedKey : undefined,
}}
customChannelId="private-channel"
authState={authState}
onApiKeySubmit={async (key) => {
// 驗證 key 後切換狀態
setAuthState("authenticated");
}}
onAuthError={(error) => {
if (error.isAuthError) setAuthState("needApiKey");
}}
/>
);
}