Skip to content

The Open Standard for Generative UI

3.5 KB
Raw Blame
---
title: "assistant-ui"
---

<Steps>

<Step title="Register the OpenUI toolkit" source="https://www.openui.com/docs/api-reference/assistant-ui">

Inside AssistantRuntimeProvider, add openuiIntegration.toolkit through AuiConfig and pass openuiIntegration.instructions to useAssistantInstructions.

```tsx title="src/components/openui-config-provider.tsx"
"use client";

import type { ReactNode } from "react";
import {
  AuiConfig,
  AuiProvider,
  Tools,
  useAui,
  useAssistantInstructions,
} from "@assistant-ui/react";
import { openuiIntegration } from "@openuidev/assistant-ui";

function OpenUIModelInstructions() {
  useAssistantInstructions(openuiIntegration.instructions);
  return null;
}

// Render this inside AssistantRuntimeProvider (next step).
export function OpenUIConfigProvider({ children }: { children: ReactNode }) {
  const aui = useAui();
  const config = AuiConfig({
    tools: Tools({ toolkit: openuiIntegration.toolkit }),
  });

  return (
    <AuiProvider extends={aui} config={config}>
      <OpenUIModelInstructions />
      {children}
    </AuiProvider>
  );
}
```

</Step>

<Step title="Let the model call the OpenUI tools" source="https://www.openui.com/docs/api-reference/assistant-ui">

present_openui renders complete cards, tables, and charts. prompt_openui renders forms and choices, and completes only when the user submits a result. With an AI SDK runtime, shouldContinueAfterOpenUIPrompt starts the next model step only after a prompt_openui result.

```tsx title="src/components/openui-runtime-provider.tsx"
"use client";

import type { ReactNode } from "react";
import { AssistantRuntimeProvider } from "@assistant-ui/react";
import { useChatRuntime } from "@assistant-ui/react-ai-sdk";
import { shouldContinueAfterOpenUIPrompt } from "@openuidev/assistant-ui/ai-sdk";
// The provider from step 1.
import { OpenUIConfigProvider } from "./openui-config-provider";

export function OpenUIRuntimeProvider({ children }: { children: ReactNode }) {
  // The runtime's chat transport forwards both OpenUI tools to your backend.
  // Continue once prompt_openui has the user's answer; a finished
  // present_openui call is the final response and starts no new step.
  const runtime = useChatRuntime({
    sendAutomaticallyWhen: shouldContinueAfterOpenUIPrompt,
  });

  return (
    <AssistantRuntimeProvider runtime={runtime}>
      <OpenUIConfigProvider>{children}</OpenUIConfigProvider>
    </AssistantRuntimeProvider>
  );
}
```

</Step>

<Step title="Bring your own components" source="https://www.openui.com/docs/api-reference/assistant-ui">

Call createOpenUIIntegration with your own library so the tool schemas, model instructions, and renderers share one component vocabulary.

```ts title="src/lib/openui-integration.ts"
import { createOpenUIIntegration } from "@openuidev/assistant-ui";
import { createShouldContinueAfterOpenUIPrompt } from "@openuidev/assistant-ui/ai-sdk";
// Your component library, made with createLibrary from @openuidev/react-lang.
import { library } from "@/lib/openui";

// Use openui.toolkit and openui.instructions in place of openuiIntegration.
// The tool names are optional; these replace present_openui and prompt_openui.
export const openui = createOpenUIIntegration({
  library,
  presentToolName: "show_panel",
  promptToolName: "ask_panel",
});

// Pass this to useChatRuntime as sendAutomaticallyWhen when you rename the tools.
export const shouldContinue = createShouldContinueAfterOpenUIPrompt({
  promptToolName: openui.toolNames.prompt,
});
```

</Step>

</Steps>