Skip to content

Commit 8078fad

Browse files
authored
fix: Dont create assistants per user (#391)
* fix: Dont create assistants per user * cr
1 parent 83a1470 commit 8078fad

File tree

4 files changed

+4
-40
lines changed

4 files changed

+4
-40
lines changed

Diff for: frontend/app/components/ThreadHistory.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ interface ThreadHistoryProps {
1616
userId: string | undefined;
1717
createThread: (id: string) => Promise<any>;
1818
clearMessages: () => void;
19-
assistantId: string | undefined;
2019
switchSelectedThread: (thread: ThreadActual) => void;
2120
getUserThreads: (id: string) => Promise<void>;
2221
deleteThread: (id: string) => Promise<void>;

Diff for: frontend/app/hooks/useGraph.tsx

+3-33
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { parsePartialJson } from "@langchain/core/output_parsers";
2-
import { useEffect, useState } from "react";
2+
import { useState } from "react";
33
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";
44
import { useToast } from "./use-toast";
55
import { v4 as uuidv4 } from "uuid";
66

77
import { Client } from "@langchain/langgraph-sdk";
8-
import { getCookie, setCookie } from "../utils/cookies";
98
import { ThreadActual, useThreads } from "./useThreads";
109
import { ModelOptions } from "../types";
1110
import { useRuns } from "./useRuns";
12-
import { ASSISTANT_ID_COOKIE_NAME } from "../utils/constants";
1311

1412
export const createClient = () => {
1513
const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:3000/api";
@@ -61,32 +59,12 @@ interface UseGraphInput {
6159

6260
export function useGraph(inputArgs: UseGraphInput) {
6361
const { toast } = useToast();
64-
const { getThreadById, setThreadId } = useThreads(inputArgs.userId);
62+
const { setThreadId } = useThreads(inputArgs.userId);
6563
const { shareRun } = useRuns();
6664
const [messages, setMessages] = useState<BaseMessage[]>([]);
67-
const [assistantId, setAssistantId] = useState<string>();
6865
const [selectedModel, setSelectedModel] =
6966
useState<ModelOptions>("openai/gpt-4o-mini");
7067

71-
useEffect(() => {
72-
if (assistantId || typeof window === "undefined") return;
73-
getOrCreateAssistant();
74-
}, []);
75-
76-
const getOrCreateAssistant = async () => {
77-
const assistantIdCookie = getCookie(ASSISTANT_ID_COOKIE_NAME);
78-
if (assistantIdCookie) {
79-
setAssistantId(assistantIdCookie);
80-
return;
81-
}
82-
const client = createClient();
83-
const assistant = await client.assistants.create({
84-
graphId: "chat",
85-
});
86-
setAssistantId(assistant.assistant_id);
87-
setCookie(ASSISTANT_ID_COOKIE_NAME, assistant.assistant_id);
88-
};
89-
9068
const streamMessage = async (params: GraphInput) => {
9169
if (!inputArgs.threadId) {
9270
toast({
@@ -95,13 +73,6 @@ export function useGraph(inputArgs: UseGraphInput) {
9573
});
9674
return undefined;
9775
}
98-
if (!assistantId) {
99-
toast({
100-
title: "Error",
101-
description: "Assistant ID not found",
102-
});
103-
return undefined;
104-
}
10576

10677
const client = createClient();
10778

@@ -122,7 +93,7 @@ export function useGraph(inputArgs: UseGraphInput) {
12293
}),
12394
};
12495

125-
const stream = client.runs.stream(inputArgs.threadId, assistantId, {
96+
const stream = client.runs.stream(inputArgs.threadId, "chat", {
12697
input,
12798
streamMode: "events",
12899
config: {
@@ -682,7 +653,6 @@ export function useGraph(inputArgs: UseGraphInput) {
682653

683654
return {
684655
messages,
685-
assistantId,
686656
selectedModel,
687657
setSelectedModel,
688658
setMessages,

Diff for: frontend/app/page.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default function ContentComposerChatInterface(): React.ReactElement {
3737
messages,
3838
setMessages,
3939
streamMessage,
40-
assistantId,
4140
switchSelectedThread,
4241
selectedModel,
4342
setSelectedModel,
@@ -47,15 +46,13 @@ export default function ContentComposerChatInterface(): React.ReactElement {
4746
});
4847
const [isRunning, setIsRunning] = useState(false);
4948

50-
const isSubmitDisabled = !userId || !assistantId || !currentThread;
49+
const isSubmitDisabled = !userId || !currentThread;
5150

5251
async function onNew(message: AppendMessage): Promise<void> {
5352
if (isSubmitDisabled) {
5453
let description = "";
5554
if (!userId) {
5655
description = "Unable to find user ID. Please try again later.";
57-
} else if (!assistantId) {
58-
description = "Unable to find assistant ID. Please try again later.";
5956
} else if (!currentThread) {
6057
description =
6158
"Unable to find current thread ID. Please try again later.";
@@ -121,7 +118,6 @@ export default function ContentComposerChatInterface(): React.ReactElement {
121118
userThreads={userThreads}
122119
userId={userId}
123120
createThread={createThread}
124-
assistantId={assistantId}
125121
deleteThread={(id) => deleteThread(id, () => setMessages([]))}
126122
clearMessages={() => setMessages([])}
127123
/>

Diff for: frontend/app/utils/constants.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export const RESPONSE_FEEDBACK_KEY = "user_score";
22
export const SOURCE_CLICK_KEY = "user_click";
3-
export const ASSISTANT_ID_COOKIE_NAME = "clc_assistant_id_v3";
43
export const THREAD_ID_COOKIE_NAME = "clc_thread_id_v3";
54
export const USER_ID_COOKIE_NAME = "clc_user_id_v3";

0 commit comments

Comments
 (0)