{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-@l10n/ja/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["platform-badges","admonition"]},"type":"markdown"},"seo":{"title":"Vercel AI SDK でチャットアプリを作る","description":"Treasure AI Studio のエージェントを自社アプリに組み込む方法。サーバーからチャットを作成し、Vercel AI SDK でストリーミングし、エージェントの質問に答える実装まで解説します。","siteUrl":"https://docs.treasure.ai","image":"/assets/treasure-ai-social-preview.e6fefb35676f6da91c2d1bc783bd39e618e7af2b1ad3aa52a96ae69d72e700fe.b1bff462.png","lang":"en-US","jsonLd":{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.treasure.ai/","name":"Treasure AI","url":"https://www.treasure.ai/","logo":"https://www.treasure.ai/hubfs/assets/images/logos/primary-logo.svg"},{"@type":"WebSite","@id":"https://docs.treasure.ai/#website","name":"Treasure AI Documentation","url":"https://docs.treasure.ai/","inLanguage":["en","ja"],"publisher":{"@id":"https://www.treasure.ai/"}}]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"vercel-ai-sdk-でチャットアプリを作る","__idx":0},"children":["Vercel AI SDK でチャットアプリを作る"]},{"$$mdtype":"Tag","name":"PlatformBadges","attributes":{"platforms":["Web","Desktop"]},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Agent Gateway は ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://ai-sdk.dev/"},"children":["Vercel AI SDK"]}," の UI Message Stream プロトコルに対応しているため、Treasure AI Studio のチャットは OSS のクライアント — ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["@ai-sdk/react"]}," の ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["useChat"]}," — に手を加えることなくそのまま扱えます。公式ドキュメントに書かれている使い方がそのまま通用します。Treasure AI 側が追加しているのは、チャットを作成するための小さな REST インターフェースと、アプリケーションが実装する必要のあるツール1つだけです。インストールすべき Treasure AI 製のクライアントライブラリはなく、必要もありません。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["このページは、",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/sdk-apps"},"children":["SDK App"]}," から受け取った資格情報のための統合ガイドです。サーバーが何をするか、クライアントが何をするか、そして間違えると静かに壊れる数カ所を説明します。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"前提条件","__idx":1},"children":["前提条件"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/sdk-apps"},"children":["SDK App"]}," とその ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Gateway Token"]},"（",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["tasgwt_…"]},"）、そしてアカウントの Agent Gateway URL。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["自分で管理するサーバー。Gateway Token をブラウザに到達させてはいけないため、この構成には必ずサーバーが存在します。たとえチャットを作成するだけの役割でも必要です。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Vercel AI SDK の npm パッケージ ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["ai"]}," v7 以降と ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["@ai-sdk/react"]}," v4 以降。ゲートウェイが実装しているのは UI Message Stream の ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["v1"]}," で、これらのバージョンが対応するものです。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"全体の構成","__idx":2},"children":["全体の構成"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["アプリケーションが呼ぶ API は2種類あり、それぞれ使う資格情報が異なります。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"24%","data-label":"API"},"children":["API "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"22%","data-label":"呼び出し元"},"children":["呼び出し元 "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"22%","data-label":"資格情報"},"children":["資格情報 "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"32%","data-label":"用途"},"children":["用途 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["コントロール API"]},"（",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/v1/chats/…"]},"）"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["自社サーバーのみ"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Gateway Token"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["チャットの作成、トークンの再発行・失効、状態の確認、アーカイブ"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["チャットメッセージング API"]},"（",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/v1/messaging/…"]},"）"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["自社サーバー、またはクライアント"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Chat Access Token"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["ターンの実行、履歴の取得、生成の停止"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["チャットを作成すると、Chat Access Token とそのチャット用の",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["接続先 URL"]}," 一式が返ります。この URL は返ってきた値のまま保存して使ってください。完全な URL であり、中身を解釈する必要はありません。メッセージング用の URL を文字列連結で組み立てないでください。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]}," に無いものは公開されていないものであり、URL を手で組むと、メッセージングの提供先が将来変わったときに統合が壊れます。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"トークンをどこに置くか決める","__idx":3},"children":["トークンをどこに置くか決める"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["コードを書く前に、サーバーをストリーミングの経路に置くかどうかを決めます。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"20%","data-label":"観点"},"children":["観点 "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"40%","data-label":"サーバーで中継する（推奨）"},"children":["サーバーで中継する（推奨） "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"40%","data-label":"ブラウザがゲートウェイを直接呼ぶ"},"children":["ブラウザがゲートウェイを直接呼ぶ "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Chat Access Token"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["サーバーに留まる"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["ブラウザが保持する"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["CORS"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["設定するものがない — ブラウザは自分のオリジンに留まる"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["SDK App の",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["ブラウザアクセス"]},"をオンにする必要がある"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["自社の統制が効く範囲"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["自社の認証、監査ログ、レート制限、プロンプト検査がすべてのターンで効く"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["何もない — ゲートウェイが見るのは自社サーバーではなくブラウザ"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["コスト"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["サーバーがストリーミング接続を保持し続ける"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["サーバーはストリーミングの経路から外れる"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["中継する構成が推奨です。"]}," ページ上で動くスクリプト — 侵害された依存パッケージが持ち込んだものや XSS — は、ブラウザが保持している Chat Access Token を読み取ってゲートウェイを直接呼べるため、サーバーで行っていることすべてを迂回できます。",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["ブラウザアクセス"]},"がオフなら、その呼び出しは拒否されます。ブラウザは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Origin"]}," ヘッダーを抑制できないので、ブラウザ内で動くものはゲートウェイに一切到達しません。オンにすることは、ページ内のコードを信頼するという判断です。",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/tokens#%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9"},"children":["ブラウザアクセス"]},"を参照してください。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["以降は中継する構成を前提に説明します。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"チャットを作成する","__idx":4},"children":["チャットを作成する"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Gateway Token を使うのはサーバー側だけです。この呼び出しと、後述の",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%AB-api-%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9"},"children":["リファレンス"]},"にあるコントロール API の呼び出しで使います。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"ts","header":{"controls":{"copy":{}}},"source":"const created = await fetch(`${AGENT_GATEWAY_URL}/v1/chats`, {\n  method: \"POST\",\n  headers: { authorization: `Bearer ${GATEWAY_TOKEN}` },\n});\n\nconst chat = await created.json();\n// {\n//   chat_id: \"…\",\n//   token: { id: \"…\", secret: \"tascat_…\", expires_at: \"2026-10-21T00:00:00Z\" },\n//   endpoints: { ai_sdk: \"https://…\", ai_sdk_history: \"https://…\", stop: \"https://…\" }\n// }\n","lang":"ts"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["chat_id"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["token.secret"]},"、そして ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]}," オブジェクト全体を、自社のエンドユーザーに紐づけてデータベースに保存してください。クライアントが知る必要があるのは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["chat_id"]}," だけです。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["ページを読み込むたびに新しいチャットを作るのではなく、チャットを保持してください。履歴と再開はチャットに紐づくため、毎回新規に作るアプリケーションはそのどちらも持てません。"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info","name":"トークンが期限切れになってもチャットは残ります"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Chat Access Token は未使用のまま24時間、絶対上限は30日です。切れたときは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["POST /v1/chats/{chat_id}/access_tokens"]}," で同じチャットに対して新しいトークンを発行すれば、会話は続きます。トークンだけでなくチャット ID を保存しておくのはこのためです。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"メッセージング-api-を中継する","__idx":5},"children":["メッセージング API を中継する"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["サーバーはメッセージングのルート（ターン、履歴、停止、再開）を保存済みの ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]}," に転送し、それぞれのレスポンスをそのまま返します。自分で正しく実装する必要があるのは3点で、いずれも別の問題のように見える壊れ方をします。"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["切断を伝播させる。"]}," ブラウザが切断したら上流のリクエストを中断してください。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["close"]}," を監視する対象は",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["レスポンス"]},"オブジェクトで、リクエストではありません — リクエストはボディを読み終えた時点で閉じ、それは回答が始まる前なので、そこで中断するとすべてのターンが直ちに打ち切られます。ただし、これは生成を止めるものではありません。ターンを止めるのは stop エンドポイントだけです。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pipe"]}," ではなく ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pipeline"]}," を使う。"]}," 上記の中断で上流のストリームが ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["error"]}," を発しますが、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pipe"]}," はそれを転送せず、処理されない ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["error"]}," イベントはプロセスを終了させます。ブラウザがターンの途中で離脱するのは普通のことなので、通常の運用中にクラッシュするということです。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["バッファリングさせない。"]}," 圧縮ミドルウェアやリバースプロキシは Server-Sent Events のストリームを終了まで保持してしまい、ストリーミングが「最後にまとめて1回」に変わります。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["x-accel-buffering: no"]}," を設定し、ヘッダーを直ちにフラッシュしてください。"]}]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"ts","header":{"controls":{"copy":{}}},"source":"import { Readable } from \"node:stream\";\nimport { pipeline } from \"node:stream/promises\";\n\nasync function forward(req, res, session, url, body) {\n  const controller = new AbortController();\n  res.on(\"close\", () => {\n    if (!res.writableEnded) controller.abort();\n  });\n\n  // ヘッダーは明示的に指定する。ブラウザのヘッダーを再送してはいけない（下の警告を参照）\n  const upstream = await fetch(url, {\n    method: req.method,\n    headers: {\n      authorization: `Bearer ${session.accessToken}`,\n      ...(body === undefined ? {} : { \"content-type\": \"application/json\" }),\n    },\n    ...(body === undefined ? {} : { body: JSON.stringify(body) }),\n    signal: controller.signal,\n  });\n\n  res.status(upstream.status);\n  for (const header of [\"content-type\", \"cache-control\", \"x-vercel-ai-ui-message-stream\"]) {\n    const value = upstream.headers.get(header);\n    if (value) res.setHeader(header, value);\n  }\n  res.setHeader(\"x-accel-buffering\", \"no\");\n  res.flushHeaders();\n\n  if (!upstream.body) return void res.end();\n\n  try {\n    await pipeline(Readable.fromWeb(upstream.body), res);\n  } catch {\n    res.destroy();\n  }\n}\n","lang":"ts"},"children":[]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"warning","name":"ブラウザの Origin ヘッダーを転送しないでください"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["上流リクエストのヘッダーは、ブラウザから受け取ったものを流用せず、明示的に指定してください。ブラウザのヘッダーを再送すると、ゲートウェイが「この呼び出しはブラウザから来た」と解釈する ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Origin"]}," が送られ、",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["ブラウザアクセス"]},"がオフなら ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["「This chat cannot be called from a browser.」"]}," という内容の ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["403"]}," が返ります。実装のつまずきとして最も多いパターンです。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"中継エンドポイントを-csrf-から守る","__idx":6},"children":["中継エンドポイントを CSRF から守る"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["この問題はログインを追加した時点で生まれます。見落としやすいため、個別に説明します。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/api/chats/:id/ai-sdk"]}," をセッション Cookie で認可するようにすると、別オリジンのページからユーザーのブラウザに POST させることができ、サーバーは Chat Access Token を付けて忠実にプロンプトを転送します。ゲートウェイ側では防げません — ゲートウェイから見えるのは、正しい資格情報を持った自社サーバーです。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["対策は一般的なものです。"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["セッション Cookie に ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SameSite=Lax"]}," か ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Strict"]}," を付ける。現在のブラウザはクロスサイトの POST で Cookie を送らないため、これだけでも止まります。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Sec-Fetch-Site"]}," が ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["same-origin"]}," でないリクエストを拒否する。ブラウザが設定する値で、スクリプトから偽装できません。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["古いブラウザまで守る必要があれば CSRF トークンを使う。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Content-Type: application/json"]}," を必須にし、それ以外はパースしない。リクエストが preflight の対象になります。"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["これらはいずれも Gateway Token には関係しません。Gateway Token はプロセスの外に出ないためです。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"クライアントを実装する","__idx":7},"children":["クライアントを実装する"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["ブラウザ側のトランスポートに Treasure AI 固有のものはありません。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"tsx","header":{"controls":{"copy":{}}},"source":"import { useChat } from \"@ai-sdk/react\";\nimport { DefaultChatTransport, lastAssistantMessageIsCompleteWithToolCalls } from \"ai\";\n\nconst { messages, sendMessage, status, addToolOutput } = useChat({\n  transport: new DefaultChatTransport({ api: `/api/chats/${chatId}/ai-sdk` }),\n  messages: history,              // 終了済みのターン。描画前に取得しておく\n  resume: true,                   // 実行中のターンに接続する\n  sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,\n});\n","lang":"tsx"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["以降の描画は SDK の仕事です。知っておく価値があるパートの型は2つあります。"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["dynamic-tool"]}]}," — エージェントが実行するすべてのツール。実行済みの状態で届き、ツール名は ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["part.toolName"]}," に入っています。ゲートウェイはこれらのチャンクに ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["providerExecuted"]}," を付けるので、SDK 側で ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["onToolCall"]}," は抑制されます。実装は必要ありません。つまりこのパートは表示専用です。ワーカーのツールセットは事前に宣言できる一覧ではないので、dynamic になっています。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["tool-AskUserQuestion"]}]}," — 名前付きで送られます。アプリケーションが答える必要がある唯一のツールだからです。"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["接続が開いた後のこと — テキストのストリーミング、順序、ツール呼び出しの進行 — はすべて SDK 側の担当で、",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://ai-sdk.dev/docs/ai-sdk-ui/chatbot"},"children":["SDK のドキュメント"]},"がそのまま参照先になります。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"実装が必要な唯一のツール","__idx":8},"children":["実装が必要な唯一のツール"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["エージェントは人間の判断が必要になると、処理をいったん止めて質問します。その答えはアプリケーションからしか来ないので、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["AskUserQuestion"]}," という名前の通常のツール呼び出しとして届き、出力を記録するまで ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["input-available"]}," の状態に留まります。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["質問して終わったターンは、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["finishReason"]}," が ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["\"stop\""]}," ではなく ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["\"tool-calls\""]}," になります。答えると新しいターンが始まり、その出力は同じアシスタントメッセージに合流するため、会話は「途中に質問が挟まった1つの回答」として読めます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["input.questions"]}," の各質問が持つフィールドは次のとおりです。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"22%","data-label":"フィールド"},"children":["フィールド "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"78%","data-label":"意味"},"children":["意味 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["question"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["質問の文面。",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["これが質問の識別子であり、答えのキーです"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["header"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["カード用の短いラベル。答えのキーには決してなりません"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiSelect"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["複数選択できるかどうか"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["options[].label"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["選択肢。選ばれた label が答えの値になります"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["答えは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["addToolOutput"]}," で記録します。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"tsx","header":{"controls":{"copy":{}}},"source":"addToolOutput({\n  tool: \"AskUserQuestion\",\n  toolCallId: part.toolCallId,\n  output: { answers: { \"Which colour do you prefer?\": \"Blue\" } },\n});\n","lang":"tsx"},"children":[]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"danger","name":"answers のキーは question です。header ではありません"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["header"]}," をキーにすると静かに失敗します。リクエストは受理され、ターンはストリーミングされて完了し、エージェントには答えが見えないため、その旨を回答します。捕捉できるエラーは発生しません。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["answers"]}," オブジェクトは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["questions[].question"]}," から組み立ててください。"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["同様に、その他のケースの値の形式も示します。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"30%","data-label":"ケース"},"children":["ケース "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"70%","data-label":"値"},"children":["値 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["単一選択"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["選ばれた ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["options[].label"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiSelect"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["選ばれた label を ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["\", \""]}," で連結した文字列"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["未回答"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["空文字列"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["答えを実際に送信するのは ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls"]}," です。これがないと答えはブラウザに留まり、エージェントは5分のタイムアウトまで待ちます。このツールをまったく実装しない場合、質問が出た時点で会話はタイムアウトまで止まります。実装されているかどうかをゲートウェイ側から知る手段はありません。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"クライアントが持っていない会話を表示する","__idx":9},"children":["クライアントが持っていない会話を表示する"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["開いたばかりのクライアントは、会話の内容を何も持っていません。これを復元する方法は2つあり、メッセージの境界で分かれているので、両方に問い合わせても二重に表示されることはありません。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"34%","data-label":"ルート"},"children":["ルート "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"66%","data-label":"返るもの"},"children":["返るもの "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints.ai_sdk_history"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["終了済みのターン。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["useChat"]}," の ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["messages"]}," の形式"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["${endpoints.ai_sdk}/{chat_id}/stream"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["終了していないターン — 開始時点からリプレイし、その後ライブに繋がります。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204"]}," は実行中のものがないことを意味します"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["前者は描画前に取得して ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["useChat({ messages })"]}," に渡し、後者は ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["useChat({ resume: true })"]}," に任せます。後者は初回レンダリング時に一度だけ問い合わせます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["resume は、保存済みの URL ではなく自分で URL を組む唯一の箇所です。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["useChat"]}," は自身の ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["api"]}," から ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["${api}/${chatId}/stream"]}," という形で導出します。AI SDK のプロトコルがその形を固定しているためです。中継用のルートも同じ形で ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["ai_sdk"]}," から組んでください。組み立てに使うチャット ID は",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["セッションが保持しているもの"]},"で、ブラウザのリクエストに入っていたものではありません。ブラウザから送られてきた値を、サーバーが呼ぶ URL に埋め込むべきではありません。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["切断してもターンは止まらないため、再接続できるターンが残ります。ターンを止めたい場合は ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints.stop"]}," に POST してください。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"コントロール-api-リファレンス","__idx":10},"children":["コントロール API リファレンス"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["ベース URL はアカウントの Agent Gateway URL です。どの呼び出しにも ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Authorization: Bearer tasgwt_…"]}," が必要です。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"42%","data-label":"メソッドとパス"},"children":["メソッドとパス "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"58%","data-label":"返り値"},"children":["返り値 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["POST /v1/chats"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["201"]}," と ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["chat_id"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["token"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]},"。チャネルのフォルダにチャットを作成します"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["POST /v1/chats/{chat_id}/access_tokens"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["201"]}," と新しい ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["token"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]},"。前のトークンが期限切れになったときに使います"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["DELETE /v1/chats/{chat_id}/access_tokens"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204"]},"。そのチャットのすべての Chat Access Token を失効させます。トークン ID を管理せずにサインアウトを実装できます"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["DELETE /v1/chats/{chat_id}/access_tokens/{token_id}"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204"]},"。1本だけ失効させます"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["GET /v1/chats/{chat_id}"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["200"]}," と ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["chat_id"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["archived"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["created_at"]},"、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["DELETE /v1/chats/{chat_id}"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204"]},"。チャットをアーカイブします。配下のトークンも一緒に止まります"]}]}]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"メッセージング-api","__idx":11},"children":["メッセージング API"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Authorization: Bearer tascat_…"]}," を付けて、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints"]}," の URL に対して呼び出します。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"42%","data-label":"エンドポイント"},"children":["エンドポイント "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"58%","data-label":"返り値"},"children":["返り値 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints.ai_sdk"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Server-Sent Events、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["x-vercel-ai-ui-message-stream: v1"]},"。1回の POST が1回のターンです"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints.ai_sdk_history"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["完了済みメッセージの JSON 配列"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["endpoints.stop"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["204"]},"。実行中のターンを終了します"]}]}]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"エラー","__idx":12},"children":["エラー"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["どのエラーも ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["{\"errors\":[{\"message\":\"…\"}]}"]}," の形です。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["401"]},"・",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["404"]},"・",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["502"]}," の文面は固定で、内容を明かしません。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["403"]}," と ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["422"]}," の文面は、何を直せばよいかを示します。AI SDK は 2xx 以外のボディをそのままクライアント側の例外のメッセージに使うため、開発者が目にするのはこの文字列です。"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"width":"14%","data-label":"ステータス"},"children":["ステータス "]},{"$$mdtype":"Tag","name":"th","attributes":{"width":"86%","data-label":"意味"},"children":["意味 "]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["401"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["資格情報が無い、失効済み、期限切れ、またはチャネルが接続を受け付けていません。別のチャットに対して Chat Access Token を使った場合も ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["401"]}," になります。チャットの存在を探れないようにするためです"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["403"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Origin"]}," ヘッダーが付いた呼び出しで、ブラウザアクセスがオフです。メッセージは、ブラウザの ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Origin"]}," ヘッダーを転送しないよう案内します"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["404"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["そのチャットは存在しません"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["413"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["リクエストボディが大きすぎます"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["422"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["リクエストをパースできなかったか、パースはできたもののゲートウェイが対応していない操作を求めています（回答の再生成や、期限切れになった質問への回答など）。どちらかはメッセージで分かります"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["429"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["レート制限。",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Retry-After"]}," ヘッダーに従ってください"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["500"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["内部エラー"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["502"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["一時的に利用できません。バックオフして再試行してください"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["エラー処理はステータスに対して組み、メッセージには依存しないでください。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"サポートされないもの","__idx":13},"children":["サポートされないもの"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["履歴の編集・分岐・再実行。"]}," AI SDK は毎回の会話全体を送り直しますが、セッションを保持しているのはエージェント側なので、実際に処理されるのは末尾のユーザーメッセージだけです。それより前のメッセージを変更しても無視されます。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["モデルの選択、セッションオプションの変更。"]}," これらの操作は、ゲートウェイが対応するプロトコルに含まれていません。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["WebSocket トランスポート。"]}," Server-Sent Events のみです。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["AG-UI と A2A。"]}," 現在公開されているプロトコルは UI Message Stream だけです。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["クライアント側でのツール出力。"]}," クライアントにはツールが動いたことと引数が見えますが、何を返したかは見えません。",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/channel-chats#%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%8C%E5%8F%97%E3%81%91%E5%8F%96%E3%82%8B%E3%82%82%E3%81%AE"},"children":["エージェントチャネルのチャットが到達できる範囲"]},"を参照してください。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"次のステップ","__idx":14},"children":["次のステップ"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/tokens"},"children":["Gateway Token と Chat Access Token"]}," — 有効期間、ローテーション、失効"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/channel-chats"},"children":["エージェントチャネルのチャットが到達できる範囲"]}," — アプリケーションが動く境界と、ぶつかる制限値"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/products/ai-studio/agent-channels/sdk-apps"},"children":["SDK App の作成と管理"]}," — 代替トークンの発行"]}]}]},"headings":[{"value":"Vercel AI SDK でチャットアプリを作る","id":"vercel-ai-sdk-でチャットアプリを作る","depth":1},{"value":"前提条件","id":"前提条件","depth":2},{"value":"全体の構成","id":"全体の構成","depth":2},{"value":"トークンをどこに置くか決める","id":"トークンをどこに置くか決める","depth":2},{"value":"チャットを作成する","id":"チャットを作成する","depth":2},{"value":"メッセージング API を中継する","id":"メッセージング-api-を中継する","depth":2},{"value":"中継エンドポイントを CSRF から守る","id":"中継エンドポイントを-csrf-から守る","depth":3},{"value":"クライアントを実装する","id":"クライアントを実装する","depth":2},{"value":"実装が必要な唯一のツール","id":"実装が必要な唯一のツール","depth":2},{"value":"クライアントが持っていない会話を表示する","id":"クライアントが持っていない会話を表示する","depth":2},{"value":"コントロール API リファレンス","id":"コントロール-api-リファレンス","depth":2},{"value":"メッセージング API","id":"メッセージング-api","depth":3},{"value":"エラー","id":"エラー","depth":3},{"value":"サポートされないもの","id":"サポートされないもの","depth":2},{"value":"次のステップ","id":"次のステップ","depth":2}],"frontmatter":{"seo":{"title":"Vercel AI SDK でチャットアプリを作る","description":"Treasure AI Studio のエージェントを自社アプリに組み込む方法。サーバーからチャットを作成し、Vercel AI SDK でストリーミングし、エージェントの質問に答える実装まで解説します。"},"platforms":["Web","Desktop"]},"lastModified":"2026-10-16T04:48:46.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/ja/products/ai-studio/agent-channels/building-an-app","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}