useCopilotContext()

useCopilotContext()

The useCopilotContext() hook is a convenient way to access the current copilot context where you need it. This hook provides the client context data and helper methods to set the client context.

You can read more about the context data and helper methods in the setting client context overview.

Usage

To use this hook your component must be a descendant of <CopilotProvider /> .

Get copilot context

The following will get the current copilot context.

CurrentContext.tsx
import { useCopilotContext } from "@continual/react";
 
export default function CurrentContext() {
  const [copilotContext] = useCopilotContext();
 
  return (
    <div>
      Current client context:
      <pre>{JSON.stringify(copilotContext, null, 2)}</pre>
    </div>
  );
}

Set copilot context

The following will set the current copilot context to encourage the copilot to respond in Spanish, but you can use a similar pattern to set the context to anything you want.

SpanishButton.tsx
import { useCopilotContext } from "@continual/react";
 
export default function SpanishButton() {
  const [_, setCopilotContext] = useCopilotContext();
 
  const switchToSpanish = () => {
    setCopilotContext("Always respond in Spanish.");
  };
 
  return <button onClick={switchToSpanish}>Speak in Spanish</button>;
}

Return value

The useCopilotContext() hook returns an array with two values:

VariableDescription
copilotContextA JSON serializable object representing the current copilot context.
setCopilotContectA function that accepts a JSON serializable object and sets the current copilot context.