Skip to content

Commit f94367f

Browse files
committed
fix: support dynamic headers function prop for auth token refresh (CopilotKit#2779)
1 parent 9686a71 commit f94367f

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

packages/react-core/src/components/copilot-provider/copilotkit-props.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff 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.

packages/react-core/src/components/copilot-provider/copilotkit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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,

packages/react-core/src/v2/providers/CopilotKitProvider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const useLicenseContext = (): LicenseContextValue =>
112112
export 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>(
263263
export 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;

0 commit comments

Comments
 (0)