File tree Expand file tree Collapse file tree
components/copilot-provider Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -68,15 +68,19 @@ export interface CopilotKitProps extends Omit<
6868
6969 /**
7070 * Additional headers to be sent with the request.
71+ * Can be a static object or a function that returns headers dynamically
72+ * (useful for refreshing auth tokens).
7173 *
7274 * For example:
73- * ```json
74- * {
75- * "Authorization": "Bearer X"
76- * }
75+ * ```tsx
76+ * // Static headers
77+ * headers={{ "Authorization": "Bearer X" }}
78+ *
79+ * // Dynamic headers (re-evaluated on each render)
80+ * headers={() => ({ "Authorization": `Bearer ${getToken()}` }) }
7781 * ```
7882 */
79- headers ?: Record < string , string > ;
83+ headers ?: Record < string , string > | ( ( ) => Record < string , string > ) ;
8084
8185 /**
8286 * The children to be rendered within the CopilotKit.
Original file line number Diff line number Diff line change @@ -311,7 +311,7 @@ export function CopilotKitInternal(cpkProps: CopilotKitProps) {
311311 publicApiKey : publicApiKey ,
312312 ...( cloud ? { cloud } : { } ) ,
313313 chatApiEndpoint : chatApiEndpoint ,
314- headers : props . headers || { } ,
314+ headers : typeof props . headers === "function" ? props . headers ( ) : ( props . headers || { } ) ,
315315 properties : props . properties || { } ,
316316 transcribeAudioUrl : props . transcribeAudioUrl ,
317317 textToSpeechUrl : props . textToSpeechUrl ,
Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ export const useLicenseContext = (): LicenseContextValue =>
112112export interface CopilotKitProviderProps {
113113 children : ReactNode ;
114114 runtimeUrl ?: string ;
115- headers ?: Record < string , string > ;
115+ headers ?: Record < string , string > | ( ( ) => Record < string , string > ) ;
116116 /**
117117 * Credentials mode for fetch requests (e.g., "include" for HTTP-only cookies in cross-origin requests).
118118 */
@@ -263,7 +263,7 @@ function useStableArrayProp<T>(
263263export const CopilotKitProvider : React . FC < CopilotKitProviderProps > = ( {
264264 children,
265265 runtimeUrl,
266- headers = { } ,
266+ headers : headersProp = { } ,
267267 credentials,
268268 publicApiKey,
269269 publicLicenseKey,
@@ -389,6 +389,9 @@ export const CopilotKitProvider: React.FC<CopilotKitProviderProps> = ({
389389 ) ;
390390 const hasLocalAgents = mergedAgents && Object . keys ( mergedAgents ) . length > 0 ;
391391
392+ // Resolve headers from function or static object
393+ const headers = typeof headersProp === "function" ? headersProp ( ) : headersProp ;
394+
392395 // Merge a provided publicApiKey into headers (without overwriting an explicit header).
393396 const mergedHeaders = useMemo ( ( ) => {
394397 if ( ! resolvedPublicKey ) return headers ;
You can’t perform that action at this time.
0 commit comments