model-utils.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
  2. /**
  3. * Result of token distribution calculation
  4. */
  5. export interface TokenDistributionResult {
  6. /**
  7. * Percentage of context window used by current tokens (0-100)
  8. */
  9. currentPercent: number
  10. /**
  11. * Percentage of context window reserved for model output (0-100)
  12. */
  13. reservedPercent: number
  14. /**
  15. * Percentage of context window still available (0-100)
  16. */
  17. availablePercent: number
  18. /**
  19. * Number of tokens reserved for model output
  20. */
  21. reservedForOutput: number
  22. /**
  23. * Number of tokens still available in the context window
  24. */
  25. availableSize: number
  26. }
  27. /**
  28. * Calculates distribution of tokens within the context window
  29. * This is used for visualizing the token distribution in the UI
  30. *
  31. * @param contextWindow The total size of the context window
  32. * @param contextTokens The number of tokens currently used
  33. * @param maxTokens Optional override for tokens reserved for model output (otherwise uses 8192)
  34. * @returns Distribution of tokens with percentages and raw numbers
  35. */
  36. export const calculateTokenDistribution = (
  37. contextWindow: number,
  38. contextTokens: number,
  39. maxTokens?: number,
  40. ): TokenDistributionResult => {
  41. // Handle potential invalid inputs with positive fallbacks
  42. const safeContextWindow = Math.max(0, contextWindow)
  43. const safeContextTokens = Math.max(0, contextTokens)
  44. // Get the actual max tokens value from the model
  45. // If maxTokens is valid (positive and not equal to context window), use it, otherwise reserve 8192 tokens as a default
  46. const reservedForOutput =
  47. maxTokens && maxTokens > 0 && maxTokens !== safeContextWindow ? maxTokens : ANTHROPIC_DEFAULT_MAX_TOKENS
  48. // Calculate sizes directly without buffer display
  49. const availableSize = Math.max(0, safeContextWindow - safeContextTokens - reservedForOutput)
  50. // Calculate percentages - ensure they sum to exactly 100%
  51. // Use the ratio of each part to the total context window
  52. const total = safeContextTokens + reservedForOutput + availableSize
  53. // Safeguard against division by zero
  54. if (total <= 0) {
  55. return {
  56. currentPercent: 0,
  57. reservedPercent: 0,
  58. availablePercent: 0,
  59. reservedForOutput,
  60. availableSize,
  61. }
  62. }
  63. return {
  64. currentPercent: (safeContextTokens / total) * 100,
  65. reservedPercent: (reservedForOutput / total) * 100,
  66. availablePercent: (availableSize / total) * 100,
  67. reservedForOutput,
  68. availableSize,
  69. }
  70. }