Skip to content

Commit

Permalink
chore: better place for calculateInputTokens utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
alashchev17 committed Feb 21, 2025
1 parent 4c44946 commit fc475d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from "react";
import { Card, Flex, HoverCard, Text } from "@radix-ui/themes";
import { ArrowDownIcon, ArrowUpIcon } from "@radix-ui/react-icons";

import { ScrollArea } from "../../ScrollArea";
import { Usage } from "../../../services/refact";
import { calculateUsageInputTokens } from "../../../utils/calculateUsageInputTokens";
import type { Usage } from "../../../services/refact";

import styles from "./UsageCounter.module.css";
import { calculateInputTokens } from "./UsageCounter.utils";

type UsageCounterProps = {
usage: Usage;
Expand All @@ -31,12 +33,12 @@ const TokenDisplay: React.FC<{ label: string; value: number }> = ({
);

export const UsageCounter: React.FC<UsageCounterProps> = ({ usage }) => {
const inputTokens = calculateInputTokens(usage, [
const inputTokens = calculateUsageInputTokens(usage, [
"prompt_tokens",
"cache_creation_input_tokens",
"cache_read_input_tokens",
]);
const outputTokens = calculateInputTokens(usage, ["completion_tokens"]);
const outputTokens = calculateUsageInputTokens(usage, ["completion_tokens"]);

return (
<HoverCard.Root>
Expand Down
4 changes: 2 additions & 2 deletions refact-agent/gui/src/features/Chat/Thread/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
isToolCallMessage,
validateToolCall,
} from "../../../services/refact";
import { calculateInputTokens } from "../../../components/ChatContent/UsageCounter/UsageCounter.utils";
import { calculateUsageInputTokens } from "../../../utils/calculateUsageInputTokens";

const RECOMMENDED_MAXIMUM_PROMPT_TOKENS_AMOUNT = 30000;

Expand Down Expand Up @@ -228,7 +228,7 @@ export const chatReducer = createReducer(initialState, (builder) => {
const { usage } = action.payload;
state.thread.usage = usage;

const inputTokensAmount = calculateInputTokens(usage, [
const inputTokensAmount = calculateUsageInputTokens(usage, [
"prompt_tokens",
"cache_creation_input_tokens",
"cache_read_input_tokens",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Usage } from "../../../services/refact";
import { Usage } from "../services/refact";

export const calculateInputTokens = (
export const calculateUsageInputTokens = (
usage: Usage,
keys: (keyof Usage)[],
): number =>
Expand Down

0 comments on commit fc475d4

Please sign in to comment.