Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions react-sdk/src/providers/tambo-client-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"use client";
import TamboAI, { ClientOptions } from "@tambo-ai/typescript-sdk";
import { QueryClient } from "@tanstack/react-query";
import React, { createContext, PropsWithChildren, useState } from "react";
import React, {
createContext,
PropsWithChildren,
useMemo,
useState,
} from "react";
import packageJson from "../../package.json";
import { useTamboSessionToken } from "./hooks/use-tambo-session-token";

Expand All @@ -27,6 +32,12 @@ export interface TamboClientProviderProps {
* user when calling the Tambo API.
*/
userToken?: string;

/**
* Additional headers to include in all requests to the Tambo API.
* These will be merged with the default headers.
*/
additionalHeaders?: Record<string, string>;
}

export interface TamboClientContextProps {
Expand All @@ -36,6 +47,8 @@ export interface TamboClientContextProps {
queryClient: QueryClient;
/** Whether the session token is currently being updated */
isUpdatingToken: boolean;
/** Additional headers to include in all requests */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name defaultHeaders implies that it is all the existing header or something, rather than extra headers that we are adding to the existing (default) headers.

How about additionalHeaders or extraHeaders

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alecf I addressed both comments

additionalHeaders?: Record<string, string>;
}

export const TamboClientContext = createContext<
Expand All @@ -51,24 +64,43 @@ export const TamboClientContext = createContext<
* @param props.apiKey - The API key for the Tambo API
* @param props.environment - The environment to use for the Tambo API
* @param props.userToken - The oauth access token to use to identify the user in the Tambo API
* @param props.additionalHeaders - Additional headers to include in all requests
* @returns The TamboClientProvider component
*/
export const TamboClientProvider: React.FC<
PropsWithChildren<TamboClientProviderProps>
> = ({ children, tamboUrl, apiKey, environment, userToken }) => {
const tamboConfig: ClientOptions = {
apiKey,
defaultHeaders: {
> = ({
children,
tamboUrl,
apiKey,
environment,
userToken,
additionalHeaders,
}) => {
// Create merged headers with useMemo so it updates when additionalHeaders change
const mergedHeaders = useMemo(() => {
const headers = {
"X-Tambo-React-Version": packageJson.version,
},
};
if (tamboUrl) {
tamboConfig.baseURL = tamboUrl;
}
if (environment) {
tamboConfig.environment = environment;
}
const [client] = useState(() => new TamboAI(tamboConfig));
...additionalHeaders, // Merge custom headers
};
return headers;
}, [additionalHeaders]);

// Create client with useMemo so it updates when mergedHeaders change
const client = useMemo(() => {
const tamboConfig: ClientOptions = {
apiKey,
defaultHeaders: mergedHeaders,
};
if (tamboUrl) {
tamboConfig.baseURL = tamboUrl;
}
if (environment) {
tamboConfig.environment = environment;
}
return new TamboAI(tamboConfig);
}, [apiKey, tamboUrl, environment, mergedHeaders]);

const [queryClient] = useState(() => new QueryClient());

// Keep the session token updated and get the updating state
Expand All @@ -84,6 +116,7 @@ export const TamboClientProvider: React.FC<
client,
queryClient,
isUpdatingToken,
additionalHeaders: mergedHeaders,
}}
>
{children}
Expand Down
Loading