Update tokenlens to canary with server side fetching (#1196)

Co-authored-by: josh <josh@afterima.ge>
This commit is contained in:
Nicklas Scharpff 2025-09-15 11:32:28 +02:00 committed by GitHub
parent 445d63e620
commit 5ab695262f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 196 additions and 286 deletions

View file

@ -1,6 +1,6 @@
'use client';
import { DefaultChatTransport, type LanguageModelUsage } from 'ai';
import { DefaultChatTransport } from 'ai';
import { useChat } from '@ai-sdk/react';
import { useEffect, useState } from 'react';
import useSWR, { useSWRConfig } from 'swr';
@ -21,6 +21,7 @@ import { useChatVisibility } from '@/hooks/use-chat-visibility';
import { useAutoResume } from '@/hooks/use-auto-resume';
import { ChatSDKError } from '@/lib/errors';
import type { Attachment, ChatMessage } from '@/lib/types';
import type { AppUsage } from '@/lib/usage';
import { useDataStream } from './data-stream-provider';
import {
AlertDialog,
@ -50,7 +51,7 @@ export function Chat({
isReadonly: boolean;
session: Session;
autoResume: boolean;
initialLastContext?: LanguageModelUsage;
initialLastContext?: AppUsage;
}) {
const { visibilityType } = useChatVisibility({
chatId: id,
@ -61,9 +62,7 @@ export function Chat({
const { setDataStream } = useDataStream();
const [input, setInput] = useState<string>('');
const [usage, setUsage] = useState<LanguageModelUsage | undefined>(
initialLastContext,
);
const [usage, setUsage] = useState<AppUsage | undefined>(initialLastContext);
const [showCreditCardAlert, setShowCreditCardAlert] = useState(false);
const {
@ -96,9 +95,7 @@ export function Chat({
}),
onData: (dataPart) => {
setDataStream((ds) => (ds ? [...ds, dataPart] : []));
if (dataPart.type === 'data-usage') {
setUsage(dataPart.data);
}
if (dataPart.type === 'data-usage') setUsage(dataPart.data);
},
onFinish: () => {
mutate(unstable_serialize(getChatHistoryPaginationKey));