/ src / components / TokenWarning.tsx
TokenWarning.tsx
 1  import { Box, Text } from 'ink'
 2  import * as React from 'react'
 3  import { getTheme } from '../utils/theme.js'
 4  
 5  type Props = {
 6    tokenUsage: number
 7  }
 8  
 9  const MAX_TOKENS = 190_000 // leave wiggle room for /compact
10  export const WARNING_THRESHOLD = MAX_TOKENS * 0.6 // 60%
11  const ERROR_THRESHOLD = MAX_TOKENS * 0.8 // 80%
12  
13  export function TokenWarning({ tokenUsage }: Props): React.ReactNode {
14    const theme = getTheme()
15  
16    if (tokenUsage < WARNING_THRESHOLD) {
17      return null
18    }
19  
20    const isError = tokenUsage >= ERROR_THRESHOLD
21  
22    return (
23      <Box flexDirection="row">
24        <Text color={isError ? theme.error : theme.warning}>
25          Context low (
26          {Math.max(0, 100 - Math.round((tokenUsage / MAX_TOKENS) * 100))}%
27          remaining) &middot; Run /compact to compact & continue
28        </Text>
29      </Box>
30    )
31  }