useAskAI

Hook for controlling AskAI state and actions

Overview

Access and control the AskAI state from any component inside AskAI, including surfaces like AskAI.Dialog or AskAI.Chat.

Demo

This demo uses useAskAI to open AskAI.Dialog and send a predefined question when the button is clicked.

Example

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

Use AskAIProvider in a layout and call useAskAI from a page or nested component, then render AskAI to provide the surfaces (like AskAI.Trigger and AskAI.Dialog).

// 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>
  );
}
// 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

ValueTypeDescription
openbooleanCurrent open state.
setOpen(open: boolean) => voidSet open state.
toggleOpen() => voidToggle open state.
inputstringCurrent input text.
setInput(value: string, options?: { open?: boolean }) => voidUpdate input text and optionally open the surface.
messagesUIMessage[]Current chat messages.
statusChatStatusStreaming status.
errorError | undefinedTransport or streaming error.
isLoadingbooleanLoading persisted history.
sendMessage({ text: string }, options?: { open?: boolean }) => voidSend a message and optionally open the surface.
handleSubmit(message: PromptInputMessage) => voidSubmit handler used by AskAI.Input.
regenerate(options?: { messageId?: string }) => voidRegenerate a response.
clearMessages() => void | Promise<void>Clear message history.