Hooks

React hooks for chat state and tools

React Hooks

Core hooks for building AI chat experiences.


useYourGPT

Main hook for chat state and actions.

import { useYourGPT } from '@yourgpt/copilot-sdk-react';

function Chat() {
  const {
    // State
    messages,
    isLoading,
    error,
    status,

    // Actions
    sendMessage,
    stop,
    clearMessages,
    regenerate,

    // Tools
    toolExecutions,
    pendingApprovals,
    approveToolExecution,
    rejectToolExecution,
    registeredTools,
  } = useYourGPT();
}

Return Values

PropertyTypeDescription
messagesUIMessage[]All chat messages
isLoadingbooleanWhether AI is responding
errorError | nullCurrent error state
status'ready' | 'streaming' | 'submitted' | 'error'Current status
sendMessage(content: string, attachments?: Attachment[]) => PromiseSend a message
stop() => voidStop current generation
clearMessages() => voidClear all messages
regenerate(messageId?: string) => PromiseRegenerate response
toolExecutionsToolExecution[]All tool executions
pendingApprovalsToolExecution[]Tools awaiting approval
approveToolExecution(id: string) => voidApprove a tool
rejectToolExecution(id: string, reason?: string) => voidReject a tool
registeredToolsToolDefinition[]All registered tools

useTools

Register tools with the AI.

import { useTools } from '@yourgpt/copilot-sdk-react';
import { z } from 'zod';

function MyTools() {
  useTools({
    search_products: {
      description: 'Search the product catalog',
      parameters: z.object({
        query: z.string().describe('Search query'),
        limit: z.number().optional().default(10),
      }),
      handler: async ({ query, limit }) => {
        const results = await searchAPI(query, limit);
        return { products: results };
      },
    },

    add_to_cart: {
      description: 'Add product to cart',
      parameters: z.object({
        productId: z.string(),
        quantity: z.number().default(1),
      }),
      requiresApproval: true,
      handler: async ({ productId, quantity }) => {
        await cart.add(productId, quantity);
        return { success: true };
      },
    },
  });

  return null;
}

Tool Definition

PropertyTypeRequiredDescription
descriptionstringYesWhat the tool does (AI reads this)
parametersZodSchemaYesInput validation schema
handler(params) => Promise<any>YesAsync function to execute
requiresApprovalbooleanNoRequire user approval

useAIContext

Share app state with the AI.

import { useAIContext } from '@yourgpt/copilot-sdk-react';

function ProductPage({ product }) {
  useAIContext({
    key: 'current-product',
    data: {
      name: product.name,
      price: product.price,
      stock: product.stock,
    },
    description: 'The product the user is currently viewing',
  });

  return <ProductDetails product={product} />;
}

Options

PropertyTypeRequiredDescription
keystringYesUnique identifier
dataobjectYesData to share with AI
descriptionstringNoDescribe the context for AI
parentIdstringNoParent context for nesting

useAIContexts

Register multiple contexts at once.

import { useAIContexts } from '@yourgpt/copilot-sdk-react';

function Dashboard() {
  useAIContexts([
    { key: 'user', data: user, description: 'Current user info' },
    { key: 'cart', data: cart, description: 'Shopping cart contents' },
    { key: 'settings', data: settings, description: 'User preferences' },
  ]);
}

On this page