From 95d4db8002bf9ffa71027d47c3ffb30abfeea3f8 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Thu, 30 Jan 2025 23:14:06 -0600 Subject: [PATCH 1/3] Long chat warning --- frontend/src/components/ChatBox.tsx | 207 ++++++++++++++------- frontend/src/routes/_auth.chat.$chatId.tsx | 2 +- 2 files changed, 137 insertions(+), 72 deletions(-) diff --git a/frontend/src/components/ChatBox.tsx b/frontend/src/components/ChatBox.tsx index bf77cc1e5..24f8f17d5 100644 --- a/frontend/src/components/ChatBox.tsx +++ b/frontend/src/components/ChatBox.tsx @@ -1,5 +1,4 @@ import { Blend, CornerRightUp } from "lucide-react"; - import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { useEffect, useRef, useState } from "react"; @@ -8,13 +7,76 @@ import { cn } from "@/utils/utils"; import { useQuery } from "@tanstack/react-query"; import { getBillingService } from "@/billing/billingService"; import { Route as ChatRoute } from "@/routes/_auth.chat.$chatId"; +import { ChatMessage } from "@/state/LocalStateContext"; +import { useNavigate } from "@tanstack/react-router"; + +// Rough token estimation function +function estimateTokenCount(text: string): number { + // A very rough estimation: ~4 characters per token on average + return Math.ceil(text.length / 4); +} + +function TokenWarning({ + messages, + currentInput, + className +}: { + messages: ChatMessage[]; + currentInput: string; + className?: string; +}) { + const totalTokens = + messages.reduce((acc, msg) => acc + estimateTokenCount(msg.content), 0) + + (currentInput ? estimateTokenCount(currentInput) : 0); + + const navigate = useNavigate(); + + if (totalTokens < 10000) return null; + + const handleNewChat = async (e: React.MouseEvent) => { + e.preventDefault(); + try { + await navigate({ to: "/" }); + // Ensure element is available after navigation + setTimeout(() => document.getElementById("message")?.focus(), 0); + } catch (error) { + console.error("Navigation failed:", error); + } + }; + + return ( +
+
+ Tip: + Long chats cause you to reach your usage limits faster. +
+ +
+ ); +} export default function Component({ onSubmit, - startTall + startTall, + messages = [] }: { onSubmit: (input: string) => void; startTall?: boolean; + messages?: ChatMessage[]; }) { const [inputValue, setInputValue] = useState(""); const { @@ -152,78 +214,81 @@ export default function Component({ })(); return ( -
{ - if (isDisabled) { - e.preventDefault(); - return; - } - if (!isFocused) { - inputRef.current?.focus(); - } - }} - > - -