REFERENCE
開発者リファレンス
認証、API、SDK、Flow Agent のプロセッサ、パラメータ解決のリファレンスです。
React SDK (@asgard-js/react)
@asgard-js/react React SDK リファレンス
@asgard-js/react は @asgard-js/core の上に、すぐ使える React 製のチャット UI とフックを提供します。React アプリケーションに Asgard のチャット機能を素早く組み込めます。
統合パターンの選択、詳細な設定、よくある問題は フロントエンド SDK ガイドにまとまっています。 各機能の実際に動かせる例は SDK Demo にあります。
インストール
@asgard-js/reactは@asgard-js/coreを peer dependency とするため、両方をインストールしてください。npm install @asgard-js/react @asgard-js/core<Chatbot>コンポーネントimport { Chatbot } from "@asgard-js/react";
import "@asgard-js/react/style";
export default function App() {
return (
<div style={{ height: "100vh" }}>
<Chatbot
title="マイ AI アシスタント"
customChannelId="user-123"
config={{
botProviderEndpoint:
"https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}",
}}
/>
</div>
);
}必須の Props
Prop 型 説明 config.botProviderEndpointstringBot Provider のエンドポイント URL customChannelIdstring対話チャネルの識別子。通常はユーザー ID かセッション ID props の一覧は Props Reference にあります。
SSR フレームワーク(Next.js)
@asgard-js/reactはブラウザ API(window、SpeechRecognition、ResizeObserverなど)に依存するため、サーバー側では実行できません。Next.js App Router
"use client";
import dynamic from "next/dynamic";
import "@asgard-js/react/style";
const Chatbot = dynamic(
() => import("@asgard-js/react").then((m) => m.Chatbot),
{ ssr: false },
);
export default function ChatPage() {
return (
<div style={{ height: "100vh" }}>
<Chatbot
title="マイ AI アシスタント"
customChannelId="user-123"
config={{
botProviderEndpoint:
"https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}",
}}
/>
</div>
);
}Next.js Pages Router
import dynamic from "next/dynamic";
const Chatbot = dynamic(
() => import("@asgard-js/react").then((m) => m.Chatbot),
{ ssr: false },
);Docusaurus / Gatsby
import BrowserOnly from "@docusaurus/BrowserOnly";
export default function MyPage() {
return (
<BrowserOnly>
{() => {
const { Chatbot } = require("@asgard-js/react");
require("@asgard-js/react/style");
return (
<Chatbot
title="..."
customChannelId="..."
config={{ botProviderEndpoint: "..." }}
/>
);
}}
</BrowserOnly>
);
}UI のカスタマイズ(useAsgardContext)
renderHeaderやrenderFooterなどのカスタム描画 props の中では、useAsgardContextで対話の状態にアクセスできます。import { useAsgardContext } from "@asgard-js/react";
function CustomHeader() {
const { resetChannel, isConnecting } = useAsgardContext();
return (
<div>
<span>My Bot</span>
<button onClick={resetChannel} disabled={isConnecting}>
対話をリセット
</button>
</div>
);
}Context と Provider
@asgard-js/reactは 4 組の context を公開しており、それぞれに Provider と対応するフックがあります。<Chatbot>は内部で接続済みなので、UI を自前で組む場合にだけ直接使います。Context Provider Hook 提供するもの AsgardServiceContextAsgardServiceContextProvideruseAsgardContext対話の状態と操作( resetChannel、isConnectingなど)AsgardThemeContextAsgardThemeContextProvideruseAsgardThemeContextテーマ設定。既定値は defaultAsgardThemeContextValueAsgardTemplateContextAsgardTemplateContextProvideruseAsgardTemplateContextメッセージテンプレートの描画設定 AsgardAppInitializationContextAsgardAppInitializationContextProvideruseAsgardAppInitializationContextアプリケーションの初期化状態 このほか
FileDropContextProviderとuseFileDropContextがファイルのドラッグ&ドロップを扱います。ThemeScopeはテーマを部分木に適用し、DEFAULT_CONTENT_MAX_WIDTHはコンテンツ領域の既定の最大幅です。Headless での統合
<Chatbot>を使わず自前で画面を作る場合は、次の 2 つのフックから始めます。import { useAsgardServiceClient, useChannel } from '@asgard-js/react';
const client = useAsgardServiceClient({
config: { botProviderEndpoint, apiKey },
keepConnectionOnUnmount: false,
});
const channel = useChannel({ client, customChannelId, initMessages });useAsgardServiceClientはAsgardServiceClientの生成とライフサイクル管理を行い、AsgardServiceClient | nullを返します。keepConnectionOnUnmountはコンポーネントのアンマウント時に接続を保つかどうかを決めます。useChannelはUseChannelProps(client、customChannelId、initMessages、autoResetChannel、onSseMessage、onAuthErrorなど)を受け取り、UseChannelReturnを返します。ファイルエクスプローラーと Sandbox
Sandbox のファイルシステムを表示するためのコンポーネントとフックです。
公開 API 用途 FileExplorer/FileExplorerPanelエクスプローラー本体とパネルのコンテナ FileExplorerProvider/useFileExplorerエクスプローラーの context と、それを読むフック useFileExplorerControllerエクスプローラーのコントローラーを構築します( UseFileExplorerControllerOptions)FileView単一ファイルのビューア createSandboxFsProvidersAsgardServiceClientのsandboxFs*メソッドを、エクスプローラーが求める provider の形にまとめますuseSandboxLaunch/useLaunchedSandboxesSandbox の起動と、起動済み一覧の取得 sandboxAsSource/sandboxesAsSourcesSandbox をエクスプローラーのソースに変換します SourceSetFileExplorerSourceSet 用のエクスプローラー。 @asgard-js/coreのAsgardSourceSetClientと組み合わせますEMPTY_SOURCE_VIEWはソースがないときの空状態の値です。その他のコンポーネント
ChatHeaderは対話のタイトルバーで、ChatHeaderActionでアクションボタンを追加できます。TaskListとSubagentListはタスクとサブエージェントを表示し、データは@asgard-js/coreの派生状態から得ます。汎用の React ユーティリティ
以下のフックには Asgard 固有の意味はありません。パッケージが内部で使うために公開されている汎用のユーティリティで、利用しても構いませんが統合インターフェースの一部ではありません。
useDebounce、useDeepCompareMemo、useResizeObserver、useVisualViewport、useIsAtBottom、useSyncedSpin、usePromptSuggestion。tは i18n の翻訳関数、Localeはロケールの型です。Sandbox ブラウザーパネル
SandboxBrowserPanelは Sandbox のブラウザー画面をレイアウト内に描画し、接続・入力の転送・カーソルの処理を引き受けます。useSandboxBrowserController({ open?, activeSandboxName? })は開閉状態と対象の Sandbox を管理します。open、activeSandboxName、requestedBrowserと、openBrowser()、closeBrowser()、toggle()、selectSandbox(name)、requestBrowser(name, { reveal })を返します。requestBrowserは既定でパネルも開きます。reveal: falseを渡すと、開かずに対象だけ切り替えられます。返るオブジェクトは memo 化されており、identity は状態にのみ従って変わります。パネルの接続 effect がこのオブジェクトに依存しているため、毎レンダーで新しいオブジェクトを返すと再接続が繰り返されます。
toRemoteCoords(video, clientX, clientY)はパネル上のポインター位置をリモート画面の座標に変換し、fromRemoteCoords(video, x, y)はその逆です。どちらも映像のレターボックスを考慮し、計算できない場合は値をでっち上げずにnullを返します。独自のポインターオーバーレイや注釈を作るときだけ直接呼びます。関連ドキュメント
- フロントエンド SDK ガイド — パターンの選択、props、よくある問題
- JavaScript SDK リファレンス — 低レベルの
@asgard-js/coreAPI