Skip to content

Commit c26aec8

Browse files
committed
Merge branch 'main' into dev
Conflicts: refact-agent/gui/src/components/ChatForm/ChatForm.tsx refact-agent/gui/src/hooks/index.ts It looks like you may be committing a merge. If this is not correct, please run git update-ref -d MERGE_HEAD and try again. On branch dev Your branch is up to date with 'origin/dev'. All conflicts fixed but you are still merging. Changes to be committed: modified: src/components/ChatForm/ChatForm.tsx modified: src/hooks/useSendChatRequest.ts new file: src/hooks/useTotalTokenUsage.ts Changes not staged for commit: modified: src/components/ChatForm/ChatForm.tsx
2 parents 0045bc7 + 9a4baf9 commit c26aec8

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

refact-agent/gui/src/components/ChatForm/ChatForm.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { useInputValue } from "./useInputValue";
3434
import {
3535
clearInformation,
3636
getInformationMessage,
37+
setInformation,
3738
} from "../../features/Errors/informationSlice";
3839
import { InformationCallout } from "../Callout/Callout";
3940
import { ToolConfirmation } from "./ToolConfirmation";

refact-agent/gui/src/hooks/useSendChatRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export function useAutoSend() {
355355
const sendImmediately = useAppSelector(selectSendImmediately);
356356
const wasInteracted = useAppSelector(getToolsInteractionStatus); // shows if tool confirmation popup was interacted by user
357357
const areToolsConfirmed = useAppSelector(getToolsConfirmationStatus);
358+
358359
const { sendMessages, abort, messagesWithSystemPrompt } =
359360
useSendChatRequest();
360361
// TODO: make a selector for this, or show tool formation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { useMemo } from "react";
2+
import { useAppSelector } from "./useAppSelector";
3+
import { isAssistantMessage } from "../events";
4+
import { selectMessages } from "../features/Chat";
5+
import { type Usage } from "../services/refact/chat";
6+
import {
7+
calculateUsageInputTokens,
8+
mergeUsages,
9+
} from "../utils/calculateUsageInputTokens";
10+
11+
const TOKEN_LIMIT = 200_000;
12+
export function useTotalTokenUsage() {
13+
const messages = useAppSelector(selectMessages);
14+
15+
const summedUsages = useMemo(() => {
16+
const usages = messages.reduce<Usage[]>((acc, message) => {
17+
if (isAssistantMessage(message) && message.usage) {
18+
return [...acc, message.usage];
19+
}
20+
return acc;
21+
}, []);
22+
return mergeUsages(usages);
23+
}, [messages]);
24+
25+
const tokens = useMemo(() => {
26+
if (!summedUsages) return 0;
27+
return calculateUsageInputTokens({
28+
keys: [
29+
"prompt_tokens",
30+
"cache_creation_input_tokens",
31+
"cache_read_input_tokens",
32+
],
33+
usage: summedUsages,
34+
});
35+
}, [summedUsages]);
36+
37+
const limitReached = useMemo(() => {
38+
return tokens >= TOKEN_LIMIT;
39+
}, [tokens]);
40+
41+
return {
42+
summedUsages,
43+
tokens,
44+
limitReached,
45+
limit: TOKEN_LIMIT,
46+
};
47+
}

0 commit comments

Comments
 (0)