AI SDK

useChatAccessibility

Create the AI SDK observer and completion callbacks before calling useChat in the same React component.

Complete useChat integration

Call useChatAccessibility first, pass chatCallbacks into useChat, then observe the documented snapshot.

tsx
const accessibility = useChatAccessibility({
  runtime,
  scopeId: "support-thread",
  getToolLabel({ toolName }) {
    return toolName === "lookupOrder" ? "Look up order" : "A tool";
  },
  onFinish: hostOnFinish,
  onError: hostOnError,
});

const chat = useChat({
  id: "support-thread",
  ...accessibility.chatCallbacks,
});

useObserveChatAccessibility({
  integration: accessibility,
  snapshot: chat,
});

How this code works

  1. 01
    Create callbacks first

    useChatAccessibility creates one observer and callback pair for each runtime and scope.

  2. 02
    Pass callbacks to useChat

    Completion and error details reach the adapter and your existing callbacks.

  3. 03
    Observe public state

    useObserveChatAccessibility reads messages, status, and error after useChat returns.

  4. 04
    Let the hook clean up

    Unmounting disposes the observer and supports React Strict Mode remounts.

Options and return values

runtimePick<GenerativeA11yRuntime, 'dispatch'>Required

Receives adapter events while remaining under your app's control.

Default
n/a
scopeIdstringRequired

Stable non-empty namespace for messages, tools, approvals, and citations.

Default
n/a
maxTrackedEntitiesnumberOptional

Bounds adapter identity memory and suppresses new identity after saturation.

Default
1000
getToolLabel(context) => stringOptional

Maps public tool context to short localized copy without exposing arguments.

Default
"A tool"
onFinish / onErrorAI SDK callbacksOptional

Your existing callbacks. chatCallbacks calls them after the adapter records the result.

Default
undefined
returnChatIntegrationReturn

Observer plus onFinish and onError callbacks for useChat.

Default
n/a

useObserveChatAccessibility

useObserveChatAccessibility reads your ChatIntegration without disposing it.

Options and return values

integrationChatIntegrationRequired

Value returned by useChatAccessibility.

Default
n/a
snapshotUseChatSnapshotRequired

Documented messages, status, and error fields from useChat.

Default
n/a
returnvoidReturn

Observation occurs in an effect after the snapshot commits.

Default
n/a