Skip to content

useChatContext Hook

Deprecated

The useChatContext hook has been replaced by the useCyborg hook, which provides a more powerful and flexible API.

Please see the useCyborg Hook documentation for the current API.

Migration Guide

The useCyborg hook replaces useChatContext with an improved API:

Before (useChatContext)

tsx
import { useChatContext } from '@cyborg-sdk/react'

function ProductPage() {
  useChatContext({
    pageData: {
      currentPage: '/products/123',
      description: 'Product detail page',
      productId: '123'
    },
    sessionData: {
      userId: 'user123',
      subscriptionTier: 'premium'
    }
  })

  return <YourComponent />
}

After (useCyborg)

tsx
import { useCyborg } from '@cyborg-sdk/react'

function ProductPage() {
  useCyborg({
    // Context replaces pageData and sessionData
    context: {
      page: 'product',
      productId: '123',
      userId: 'user123',
      subscriptionTier: 'premium'
    },
    // NEW: AI behavior instructions
    instructions: 'Help users understand this product.',
    // NEW: Register tools
    tools: [{
      name: 'addToCart',
      description: 'Add product to cart',
      parameters: { type: 'object', properties: { quantity: { type: 'number' } } },
      handler: async ({ quantity }) => {
        // Your implementation
        return { success: true }
      }
    }],
    // NEW: Suggested prompts
    suggestedPrompts: ['What are the features?', 'How does pricing work?']
  })

  return <YourComponent />
}

Key Changes

useChatContextuseCyborg
pageDatacontext
sessionDatacontext (merged)
-instructions (NEW)
-tools (NEW)
-suggestedPrompts (NEW)

Benefits of useCyborg

  1. Unified Context: No longer need to separate page and session data
  2. AI Instructions: Guide the AI's behavior per-component
  3. Tool Registration: Enable AI to execute functions in your app
  4. Suggested Prompts: Show context-aware prompts to users
  5. Dynamic Updates: Update context, tools, and prompts at runtime

Learn More

Built with VitePress