Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
omsant02 authored and PoulavBhowmick03 committed Jan 25, 2025
1 parent d18153f commit 154b9a3
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 101 deletions.
131 changes: 78 additions & 53 deletions client/app/agent/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,23 @@ export default function ChatPage() {

const handleSendMessage = async () => {
if (!inputValue.trim()) return;

const newMessage = {
id: uuidv4(),
role: "user",
content: inputValue,
timestamp: new Date().toLocaleTimeString(),
user: "User",
};

setMessages((prevMessages) => [...prevMessages, newMessage]);
setInputValue("");
setIsLoading(true);
setStreamedResponse("");
setError("");

try {
// Format messages for the API - only include unique user messages
const formattedMessages = Array.from(
new Set(
messages
Expand All @@ -119,81 +120,105 @@ export default function ChatPage() {
sender: "user",
content
}));


// Add the current message if it's not already included
if (!formattedMessages.some(msg => msg.content === inputValue)) {
formattedMessages.push({
sender: "user",
content: inputValue
});
}


const requestBody = {
prompt: inputValue,
address: address || "0x0",
messages: formattedMessages,
stream: true
};

const response = await fetch("/api/ask", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: inputValue,
address: address || "0x0",
messages: formattedMessages,
stream: true
}),
body: JSON.stringify(requestBody),
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.details?.message || 'Failed to get response');
}

const reader = response.body?.getReader();
const decoder = new TextDecoder();

if (!reader) {
throw new Error('No reader available');
}

let accumulatedResponse = '';

try {
while (true) {
const { done, value } = await reader.read();
if (done) break;

const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(Boolean);

for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(5));
if (data.content) {
accumulatedResponse += data.content;
setStreamedResponse(accumulatedResponse);

// Check if the response is a stream (has body readable)
if (response.body) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulatedResponse = '';

try {
while (true) {
const { done, value } = await reader.read();
if (done) break;

const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(Boolean);

for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(5));
if (data.content) {
accumulatedResponse += data.content;
setStreamedResponse(accumulatedResponse);
} else if (data.error) {
throw new Error(data.error);
}
} catch (e) {
console.error('Error parsing JSON:', e);
}
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
}
} finally {
reader.releaseLock();
}
} finally {
reader.releaseLock();
}

// Add final message to chat
if (accumulatedResponse) {
setMessages(prev => [...prev, {
id: uuidv4(),
role: "agent",
content: accumulatedResponse,
timestamp: new Date().toLocaleTimeString(),
user: "Agent",
}]);

// Add final message to chat
if (accumulatedResponse) {
setMessages(prev => [...prev, {
id: uuidv4(),
role: "agent",
content: accumulatedResponse,
timestamp: new Date().toLocaleTimeString(),
user: "Agent",
}]);
setAnswer(accumulatedResponse);
}
} else {
// Fallback to non-streaming response
const data = await response.json();

if (data.error) {
throw new Error(data.error);
}

setMessages((prevMessages) => [
...prevMessages,
{
id: uuidv4(),
role: "agent",
content: data.answer,
timestamp: new Date().toLocaleTimeString(),
user: "Agent",
},
]);
setAnswer(data.answer);
}

} catch (err: any) {
console.error('Chat error:', err);
setError(err.message || "Unable to get response");
setAnswer("");
} finally {
setIsLoading(false);
setStreamedResponse("");
Expand Down
139 changes: 91 additions & 48 deletions client/app/api/ask/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,55 @@ async function queryOpenAI({
brianaiResponse: string,
chatId?: string,
streamCallback?: (chunk: string) => Promise<void>
}) {
const messages = [
await systemMessage.format({ brianai_answer: brianaiResponse }),
{ role: "user", content: userQuery }
];

if (streamCallback) {
let fullResponse = '';
await agent.invoke(messages, {
callbacks: [{
handleLLMNewToken: async (token: string) => {
fullResponse += token;
await streamCallback(token);
}): Promise<string> {
try {
if (streamCallback) {
const messages = [
await systemMessage.format({ brianai_answer: brianaiResponse }),
{ role: "user", content: userQuery }
];

let fullResponse = '';
await agent.invoke(messages, {
callbacks: [{
handleLLMNewToken: async (token: string) => {
fullResponse += token;
await streamCallback(token);
},
}],
});
return fullResponse;
}

const response = await app.invoke(
{
messages: [
await prompt.format({
brianai_answer: brianaiResponse,
user_query: userQuery,
}),
],
},
{
configurable: {
thread_id: chatId || "1",
additional_args: { chatId }
},
}],
});
return fullResponse;
} else {
const response = await agent.invoke(messages);
return response.content;
},
);
return response.messages[response.messages.length-1].content as string;
} catch (error) {
console.error("OpenAI Error:", error);
return "Sorry, I am unable to process your request at the moment.";
}
}


async function queryBrianAI(prompt: string, chatId?: string, streamCallback?: (chunk: string) => Promise<void>) {
async function queryBrianAI(
prompt: string,
chatId?: string,
streamCallback?: (chunk: string) => Promise<void>
): Promise<string> {
try {
const response = await axios.post(
BRIAN_API_URL,
Expand All @@ -236,16 +260,22 @@ async function queryBrianAI(prompt: string, chatId?: string, streamCallback?: (c
},
}
);

const brianaiAnswer = response.data.result.answer;
return await queryOpenAI({
const openaiAnswer = await queryOpenAI({
brianaiResponse: brianaiAnswer,
userQuery: prompt,
chatId,
streamCallback
});

return openaiAnswer;
} catch (error) {
console.error("Brian AI Error:", error);
throw error;
if (streamCallback) {
throw error;
}
return "Sorry, I am unable to process your request at the moment.";
}
}

Expand Down Expand Up @@ -288,18 +318,26 @@ export async function POST(request: Request) {
queryBrianAI(prompt, currentChatId, async (chunk) => {
await writer.write(encoder.encode(`data: ${JSON.stringify({ content: chunk })}\n\n`));
}).then(async (fullResponse) => {
await storeMessage({
content: [{
role: "assistant",
content: fullResponse
}],
chatId: currentChatId,
userId,
});
if (fullResponse) {
await storeMessage({
content: [{
role: "assistant",
content: fullResponse
}],
chatId: currentChatId,
userId,
});
}
await writer.write(encoder.encode('data: [DONE]\n\n'));
await writer.close();
}).catch(async (error) => {
console.error('Streaming error:', error);
await writer.abort(error);
const errorMessage = {
error: 'Unable to process request',
details: error.message
};
await writer.write(encoder.encode(`data: ${JSON.stringify(errorMessage)}\n\n`));
await writer.close();
});

return new Response(stream.readable, {
Expand All @@ -311,33 +349,38 @@ export async function POST(request: Request) {
});
}

// Non-streaming response remains unchanged
// Non-streaming response
const response = await queryBrianAI(prompt, currentChatId);
if (response) {
await storeMessage({
content: [{
role: "assistant",
content: response
}],
chatId: currentChatId,
userId,
});

return NextResponse.json({
answer: response,
chatId: currentChatId
});
if (!response) {
throw new Error("Unexpected API response format");
}

await storeMessage({
content: [{
role: "assistant",
content: response
}],
chatId: currentChatId,
userId,
});

return NextResponse.json({
answer: response,
chatId: currentChatId
});

} catch (error: any) {
console.error('Error:', error);

if (error.code === 'P2003') {
return NextResponse.json(
{ error: 'User authentication required', details: 'Please ensure you are logged in.' },
{ error: 'User authentication required', details: 'Please ensure you are logged in.' },
{ status: 401 }
);
}

return NextResponse.json(
{ error: 'Unable to process request', details: error.message },
{ error: 'Unable to process request', details: error.message },
{ status: 500 }
);
}
Expand Down

0 comments on commit 154b9a3

Please sign in to comment.