feat: Refetch threads when a new thread is created

This commit is contained in:
bracesproul
2025-03-05 15:28:27 -08:00
parent b5ed8d1b10
commit 76bf8b2533
4 changed files with 111 additions and 12 deletions

82
src/providers/Thread.tsx Normal file
View File

@@ -0,0 +1,82 @@
import { validate } from "uuid";
import { getApiKey } from "@/lib/api-key";
import { Client, Thread } from "@langchain/langgraph-sdk";
import { useQueryParam, StringParam } from "use-query-params";
import {
createContext,
useContext,
ReactNode,
useCallback,
useState,
Dispatch,
SetStateAction,
} from "react";
interface ThreadContextType {
getThreads: () => Promise<Thread[]>;
threads: Thread[];
setThreads: Dispatch<SetStateAction<Thread[]>>;
threadsLoading: boolean;
setThreadsLoading: Dispatch<SetStateAction<boolean>>;
}
const ThreadContext = createContext<ThreadContextType | undefined>(undefined);
function createClient(apiUrl: string, apiKey: string | undefined) {
return new Client({
apiKey,
apiUrl,
});
}
function getThreadSearchMetadata(
assistantId: string,
): { graph_id: string } | { assistant_id: string } {
if (validate(assistantId)) {
return { assistant_id: assistantId };
} else {
return { graph_id: assistantId };
}
}
export function ThreadProvider({ children }: { children: ReactNode }) {
const [apiUrl] = useQueryParam("apiUrl", StringParam);
const [assistantId] = useQueryParam("assistantId", StringParam);
const [threads, setThreads] = useState<Thread[]>([]);
const [threadsLoading, setThreadsLoading] = useState(false);
const getThreads = useCallback(async (): Promise<Thread[]> => {
if (!apiUrl || !assistantId) return [];
const client = createClient(apiUrl, getApiKey() ?? undefined);
const threads = await client.threads.search({
metadata: {
...getThreadSearchMetadata(assistantId),
},
limit: 100,
});
return threads;
}, [apiUrl, assistantId]);
const value = {
getThreads,
threads,
setThreads,
threadsLoading,
setThreadsLoading,
};
return (
<ThreadContext.Provider value={value}>{children}</ThreadContext.Provider>
);
}
export function useThreads() {
const context = useContext(ThreadContext);
if (context === undefined) {
throw new Error("useThreads must be used within a ThreadProvider");
}
return context;
}