Skip to content

Commit cd45e1c

Browse files
committed
Added Hard coded Assistant ID,check out the .env.example file for more details
1 parent 825c126 commit cd45e1c

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

.env.example

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
#API KEY HERE#
1+
# This is the API key provided by OpenAI. You can get this key from your OpenAI account.
2+
# It is used to authenticate your application's requests to the OpenAI API.
3+
OPENAI_API_KEY=
24

3-
4-
OPENAI_API_KEY=
5+
# This is the ID of the assistant that your application will use.
6+
# You can get this ID from your OpenAI account.
7+
# If this value is set, the application will use this assistant for the chat.
8+
# If this value is not set, the application will prompt the user to enter the assistant details.
9+
REACT_APP_ASSISTANT_ID=

app/page.tsx

+22-7
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ export default function Chat() {
2424
inputRef,
2525
formRef,
2626
initialThreadMessage,
27-
setInitialThreadMessage
27+
setInitialThreadMessage,
28+
setChatStarted,
29+
chatStarted: chatHasStarted,
30+
2831

2932
} = useChatState();
3033

3134
// Initialize ChatManager only once using useEffect
32-
const [chatManager, setChatManager] = useState<ChatManager | null>(null);
33-
34-
// Add a state to track loading status of message sending
35-
const [isMessageLoading, setIsMessageLoading] = useState(false);
35+
// Initialize ChatManager only once using useEffect
36+
const [chatManager, setChatManager] = useState<ChatManager | null>(null);
37+
38+
// Set assistantId from environment variable
39+
const [assistantId, setAssistantId] = useState<string | null>(process.env.REACT_APP_ASSISTANT_ID || '');
40+
41+
// Add a state to track loading status of message sending
42+
const [isMessageLoading, setIsMessageLoading] = useState(false);
3643

3744
useEffect(() => {
3845
const chatManagerInstance = ChatManager.getInstance(setChatMessages, setStatusMessage);
@@ -41,7 +48,14 @@ export default function Chat() {
4148
setIsMessageLoading(chatManagerInstance.getChatState().isLoading);
4249
}, [setChatMessages, setStatusMessage]);
4350

44-
// Update chat state and handle assistant response reception
51+
useEffect(() => {
52+
if (assistantId && chatManager) {
53+
console.log('Assistant ID found:', assistantId);
54+
chatManager.startAssistantWithId(assistantId, initialThreadMessage);
55+
} else {
56+
console.warn('Assistant ID not found');
57+
}
58+
}, [assistantId, chatManager, initialThreadMessage]);
4559

4660

4761
const startChatAssistant = async () => {
@@ -51,6 +65,7 @@ export default function Chat() {
5165
try {
5266
await chatManager.startAssistant({ assistantName, assistantModel, assistantDescription }, file, initialThreadMessage);
5367
console.log('Assistant started:', chatManager.getChatState());
68+
setChatStarted(true);
5469
} catch (error) {
5570
console.error('Error starting assistant:', error);
5671
if (error instanceof Error) setStatusMessage(`Error: ${error.message}`);
@@ -89,7 +104,7 @@ export default function Chat() {
89104
return (
90105
<main className="flex flex-col items-center justify-between pb-40 bg-space-grey-light">
91106
<LinkBar />
92-
{chatMessages.length > 0 ? (
107+
{chatHasStarted || assistantId ? (
93108
<MessageList chatMessages={chatMessages} />
94109
) : (
95110
<WelcomeForm {...{assistantName, setAssistantName, assistantDescription, setAssistantDescription, assistantModel, setAssistantModel, file, handleFileChange, startChatAssistant, isButtonDisabled, isStartLoading, statusMessage}} />

app/services/ChatManager.ts

+50
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,55 @@ class ChatManager {
136136
}
137137
}
138138

139+
async startAssistantWithId(assistantId: string, initialMessage: string): Promise<void> {
140+
try {
141+
this.state.setStatusMessage('Creating thread...');
142+
this.state.assistantId = assistantId;
143+
const threadId = await createChatThread(initialMessage);
144+
if (threadId === null) {
145+
throw new Error('ThreadId is null');
146+
}
147+
this.state.setStatusMessage('Received thread_ID...');
148+
149+
this.state.setStatusMessage('Running assistant...');
150+
this.state.threadId = threadId;
151+
const runId = await runChatAssistant(this.state.assistantId, this.state.threadId);
152+
if (runId === null) {
153+
throw new Error('RunId is null');
154+
}
155+
156+
this.state.runId = runId;
157+
this.state.setStatusMessage('Received Run_ID..');
158+
159+
if (this.state.runId && this.state.threadId) {
160+
const runId = this.state.runId as string;
161+
const threadId = this.state.threadId as string;
162+
this.state.setStatusMessage('checking status...');
163+
const assistantResponse = await fetchAssistantResponse(runId, threadId);
164+
165+
this.state.setStatusMessage('Run complete...');
166+
this.state.assistantResponseReceived = true;
167+
this.state.setStatusMessage('Received messages...');
168+
169+
const newMessage = { role: 'assistant', content: assistantResponse };
170+
this.state.setStatusMessage('Adding messages to chat...');
171+
172+
this.state.messages = [...this.state.messages, newMessage];
173+
this.state.setChatMessages(this.state.messages);
174+
175+
} else {
176+
console.error('RunId or ThreadId is null. Current state:', this.state);
177+
}
178+
} catch (error) {
179+
this.state.setStatusMessage('Error!');
180+
this.state.error = error as Error;
181+
console.error('Error in starting assistant:', error);
182+
} finally {
183+
this.state.setStatusMessage('Done');
184+
this.state.isLoading = false;
185+
}
186+
}
187+
139188

140189
async sendMessage(input: string): Promise<void> {
141190
this.state.isSending = true;
@@ -175,6 +224,7 @@ class ChatManager {
175224
}
176225
}
177226

227+
178228
getChatState(): ChatState {
179229
console.log('Getting chat state');
180230
return this.state;

next.config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ const nextConfig = {
1414
},
1515
];
1616
},
17-
18-
17+
env: {
18+
REACT_APP_ASSISTANT_ID: process.env.REACT_APP_ASSISTANT_ID,
19+
},
1920
};
2021

21-
module.exports = nextConfig;
22+
module.exports = nextConfig;

0 commit comments

Comments
 (0)