Skip to content

Commit

Permalink
Refactor chat route.
Browse files Browse the repository at this point in the history
  • Loading branch information
bLopata committed Dec 20, 2024
1 parent a4bf4a3 commit 9913c9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
43 changes: 30 additions & 13 deletions www/app/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ export default function Chat({

const firstChat = useMemo(() => {
// Check if there are no conversations or only one conversation with no messages
return !initialConversations?.length ||
(initialConversations.length === 1 && !initialMessages?.length) ||
initialChatAccess.freeMessages === 50;
return !initialConversations?.length ||
(initialConversations.length === 1 && !initialMessages?.length) ||
initialChatAccess.freeMessages === 50;
}, [initialConversations?.length, initialMessages?.length, initialChatAccess.freeMessages]);
const defaultMessage: Message = {
content:
`${firstChat ? 'I\'m Bloom, your Aristotelian learning companion,' : 'Welcome back! I\'m'} here to guide your intellectual journey.
`${firstChat ? 'I\'m Bloom, your Aristotelian learning companion,' : 'Welcome back! I\'m'} here to guide your intellectual journey.
The more we chat, the more I learn about you as a person. That helps me adapt to your interests and needs.
Expand Down Expand Up @@ -365,9 +365,21 @@ What\'s on your mind? Let\'s dive in. 🌱`,
conversationId!,
thoughtText
);
const honchoContent = (await new Response(
honchoResponse
).json()) as HonchoResponse;

let honchoContent: HonchoResponse;
if (honchoResponse instanceof Response) {
honchoContent = await honchoResponse.json();
} else {
// Convert ReadableStream to text first
const reader = honchoResponse.getReader();
let result = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += new TextDecoder().decode(value);
}
honchoContent = JSON.parse(result);
}

const pureThought = thoughtText;

Expand All @@ -386,10 +398,16 @@ What\'s on your mind? Let\'s dive in. 🌱`,
honchoContent.content
);
if (!responseStream) throw new Error('Failed to get response stream');
const stream = responseStream instanceof Response
? responseStream.body
: responseStream;

responseReader = responseStream.getReader();
if (!stream) throw new Error('Failed to get response stream');
responseReader = stream.getReader();
let currentModelOutput = '';

if (!responseReader) throw new Error('Failed to get stream reader');

// Process response stream
while (true) {
const { done, value } = await responseReader.read();
Expand Down Expand Up @@ -559,11 +577,10 @@ What\'s on your mind? Let\'s dive in. 🌱`,
placeholder={
canUseApp ? 'Type a message...' : 'Subscribe to send messages'
}
className={`flex-1 px-3 py-1 lg:px-5 lg:py-3 bg-accent text-gray-400 rounded-2xl border-2 resize-none outline-none focus:outline-none ${
canSend && canUseApp
? 'border-green-200 focus:border-green-200'
: 'border-red-200 focus:border-red-200 opacity-50'
}`}
className={`flex-1 px-3 py-1 lg:px-5 lg:py-3 bg-accent text-gray-400 rounded-2xl border-2 resize-none outline-none focus:outline-none ${canSend && canUseApp
? 'border-green-200 focus:border-green-200'
: 'border-red-200 focus:border-red-200 opacity-50'
}`}
rows={1}
disabled={!canUseApp}
onKeyDown={(e) => {
Expand Down
4 changes: 2 additions & 2 deletions www/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export async function POST(req: NextRequest) {
}

const { canChat } = await getChatAccess(supabase, user.id);
if (canChat) {
if (!canChat) {
return new NextResponse('Subscription required', { status: 402 });
}

Expand Down Expand Up @@ -217,7 +217,7 @@ export async function POST(req: NextRequest) {

console.log('Got the Stream');

return new NextResponse(stream.body, {
return new NextResponse(stream, {
status: 200,
headers: {
'Content-Type': 'text/event-stream',
Expand Down

0 comments on commit 9913c9b

Please sign in to comment.