diff --git a/frontend/src/components/ChatBox.tsx b/frontend/src/components/ChatBox.tsx index bf77cc1e..b51e8fef 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,80 @@ 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, + chatId, + className +}: { + messages: ChatMessage[]; + currentInput: string; + chatId?: 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 ( +