# useAskAI



import { Preview } from '@/components/preview';

# Overview

Access and control the AskAI state from any component inside [`AskAI`](/docs/components/ask-ai), including surfaces like [`AskAI.Dialog`](/docs/components/ask-ai-dialog) or [`AskAI.Chat`](/docs/components/ask-ai-chat).

## Demo

This demo uses `useAskAI` to open [`AskAI.Dialog`](/docs/components/ask-ai-dialog) and send a predefined question when the button is clicked.

<Preview path="use-ask-ai" />

## Example

```tsx
import { AskAI, useAskAI } from 'peam/client';

function UseAskAIButton() {
  const { sendMessage } = useAskAI();

  return (
    <button
      type="button"
      onClick={() => {
        sendMessage({ text: 'How can I install Peam?' });
      }}
    >
      Ask how to install Peam
    </button>
  );
}

export default function Page() {
  return (
    <AskAI>
      <AskAI.Trigger />
      <AskAI.Dialog />
      <UseAskAIButton />
    </AskAI>
  );
}
```

## Example with [`AskAIProvider`](/docs/components/ask-ai-provider)

Use [`AskAIProvider`](/docs/components/ask-ai-provider) in a layout and call `useAskAI` from a page or nested component, then render [`AskAI`](/docs/components/ask-ai) to provide the surfaces (like [`AskAI.Trigger`](/docs/components/ask-ai-trigger) and [`AskAI.Dialog`](/docs/components/ask-ai-dialog)).

```tsx
// app/layout.tsx
import { AskAIProvider, AskAI } from 'peam/client';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <AskAIProvider>
          {children}
          <AskAI />
        </AskAIProvider>
      </body>
    </html>
  );
}
```

```tsx
// app/page.tsx
import { useAskAI } from 'peam/client';

function AskAICTA() {
  const { sendMessage } = useAskAI();

  return (
    <button
      type="button"
      onClick={() => {
        sendMessage({ text: 'Summarize this page' });
      }}
    >
      Ask AI about this page
    </button>
  );
}

export default function Page() {
  return (
    <main>
      <AskAICTA />
    </main>
  );
}
```

## Returns

| Value           | Type                                                       | Description                                                            |
| --------------- | ---------------------------------------------------------- | ---------------------------------------------------------------------- |
| `open`          | `boolean`                                                  | Current open state.                                                    |
| `setOpen`       | `(open: boolean) => void`                                  | Set open state.                                                        |
| `toggleOpen`    | `() => void`                                               | Toggle open state.                                                     |
| `input`         | `string`                                                   | Current input text.                                                    |
| `setInput`      | `(value: string, options?: { open?: boolean }) => void`    | Update input text and optionally open the surface.                     |
| `messages`      | `UIMessage[]`                                              | Current chat messages.                                                 |
| `status`        | `ChatStatus`                                               | Streaming status.                                                      |
| `error`         | `Error \| undefined`                                       | Transport or streaming error.                                          |
| `isLoading`     | `boolean`                                                  | Loading persisted history.                                             |
| `sendMessage`   | `({ text: string }, options?: { open?: boolean }) => void` | Send a message and optionally open the surface.                        |
| `handleSubmit`  | `(message: PromptInputMessage) => void`                    | Submit handler used by [`AskAI.Input`](/docs/components/ask-ai-input). |
| `regenerate`    | `(options?: { messageId?: string }) => void`               | Regenerate a response.                                                 |
| `clearMessages` | `() => void \| Promise<void>`                              | Clear message history.                                                 |


## Sitemap
[Overview of all docs pages](/sitemap.md)
