The ResearchProvider component manages the global research state, including synchronization between the CopilotKit useCoAgent state and local storage. To access and manipulate this state within your application, wrap your component tree with ResearchProvider and use the useResearch hook.
Context Properties:
state: The current ResearchState object.setResearchState: A function to update the research state. It accepts either a new state object or a functional updater.sourcesModalOpen: A boolean indicating if the sources modal is currently visible.setSourcesModalOpen: A function to toggle the visibility of the sources modal.runAgent: A function to trigger the execution of the research agent.
Note: useResearch will throw an error if it is called outside of a ResearchProvider.
import { ResearchProvider, useResearch } from '@/components/research-context';
function MyComponent() {
return (
<ResearchProvider>
<ResearchContent />
</ResearchProvider>
);
}
function ResearchContent() {
const { state, runAgent, setSourcesModalOpen } = useResearch();
return (
<div>
<pre>{JSON.stringify(state, null, 2)}</pre>
<button onClick={runAgent}>Run Agent</button>
<button onClick={() => setSourcesModalOpen(true)}>Show Sources</button>
</div>
);
}