Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/components/PromptInput/PromptInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ import { useMaybeTruncateInput } from './useMaybeTruncateInput.js';
import { usePromptInputPlaceholder } from './usePromptInputPlaceholder.js';
import { useShowFastIconHint } from './useShowFastIconHint.js';
import { useSwarmBanner } from './useSwarmBanner.js';
import { expandPastedTextRefs, parseReferences } from '../../history.js';
import { roughTokenCountEstimation } from '../../services/tokenEstimation.js';
import { isNonSpacePrintable, isVimModeEnabled } from './utils.js';
type Props = {
debug: boolean;
Expand Down Expand Up @@ -250,6 +252,63 @@ function PromptInput({
show: false
});
const [cursorOffset, setCursorOffset] = useState<number>(input.length);

const calculateTotalTokens = useCallback((text: string, contents: Record<number, PastedContent>): number => {
const expandedText = expandPastedTextRefs(text, contents);
let tokens = roughTokenCountEstimation(expandedText);

const refs = parseReferences(text);
for (const ref of refs) {
const content = contents[ref.id];
if (content?.type === 'image') {
tokens += 2000;
}
}

return tokens;
}, []);

const [tokenCount, setTokenCount] = useState<number>(
input.length === 0 ? 0 : calculateTotalTokens(input, pastedContents)
);
const [isCalculatingTokens, setIsCalculatingTokens] = useState(false);
const tokenDebounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);

useEffect(() => {
if (tokenDebounceTimerRef.current) {
clearTimeout(tokenDebounceTimerRef.current);
tokenDebounceTimerRef.current = null;
}

if (input.length === 0) {
setTokenCount(0);
setIsCalculatingTokens(false);
return;
}

setIsCalculatingTokens(true);

tokenDebounceTimerRef.current = setTimeout(() => {
const tokens = calculateTotalTokens(input, pastedContents);
setTokenCount(tokens);
setIsCalculatingTokens(false);
tokenDebounceTimerRef.current = null;
}, 300);

return () => {
if (tokenDebounceTimerRef.current) {
clearTimeout(tokenDebounceTimerRef.current);
}
};
}, [input, pastedContents, calculateTotalTokens]);

useEffect(() => {
return () => {
if (tokenDebounceTimerRef.current) {
clearTimeout(tokenDebounceTimerRef.current);
}
};
}, []);
// Track the last input value set via internal handlers so we can detect
// external input changes (e.g. speech-to-text injection) and move cursor to end.
const lastInternalInputRef = React.useRef(input);
Expand Down Expand Up @@ -2271,7 +2330,7 @@ function PromptInput({
{textInputElement}
</Box>
</Box>}
<PromptInputFooter apiKeyStatus={apiKeyStatus} debug={debug} exitMessage={exitMessage} vimMode={isVimModeEnabled() ? vimMode : undefined} mode={mode} autoUpdaterResult={autoUpdaterResult} isAutoUpdating={isAutoUpdating} verbose={verbose} onAutoUpdaterResult={onAutoUpdaterResult} onChangeIsUpdating={setIsAutoUpdating} suggestions={suggestions} selectedSuggestion={selectedSuggestion} maxColumnWidth={maxColumnWidth} toolPermissionContext={effectiveToolPermissionContext} helpOpen={helpOpen} suppressHint={input.length > 0} isLoading={isLoading} tasksSelected={tasksSelected} teamsSelected={teamsSelected} bridgeSelected={bridgeSelected} tmuxSelected={tmuxSelected} teammateFooterIndex={teammateFooterIndex} ideSelection={ideSelection} mcpClients={mcpClients} isPasting={isPasting} isInputWrapped={isInputWrapped} messages={messages} isSearching={isSearchingHistory} historyQuery={historyQuery} setHistoryQuery={setHistoryQuery} historyFailedMatch={historyFailedMatch} onOpenTasksDialog={isFullscreenEnvEnabled() ? handleOpenTasksDialog : undefined} />
<PromptInputFooter apiKeyStatus={apiKeyStatus} debug={debug} exitMessage={exitMessage} vimMode={isVimModeEnabled() ? vimMode : undefined} mode={mode} autoUpdaterResult={autoUpdaterResult} isAutoUpdating={isAutoUpdating} verbose={verbose} onAutoUpdaterResult={onAutoUpdaterResult} onChangeIsUpdating={setIsAutoUpdating} suggestions={suggestions} selectedSuggestion={selectedSuggestion} maxColumnWidth={maxColumnWidth} toolPermissionContext={effectiveToolPermissionContext} helpOpen={helpOpen} suppressHint={input.length > 0} isLoading={isLoading} tasksSelected={tasksSelected} teamsSelected={teamsSelected} bridgeSelected={bridgeSelected} tmuxSelected={tmuxSelected} teammateFooterIndex={teammateFooterIndex} ideSelection={ideSelection} mcpClients={mcpClients} isPasting={isPasting} isInputWrapped={isInputWrapped} messages={messages} isSearching={isSearchingHistory} historyQuery={historyQuery} setHistoryQuery={setHistoryQuery} historyFailedMatch={historyFailedMatch} onOpenTasksDialog={isFullscreenEnvEnabled() ? handleOpenTasksDialog : undefined} tokenCount={tokenCount} isCalculatingTokens={isCalculatingTokens} />
{isFullscreenEnvEnabled() ? null : autoModeOptInDialog}
{isFullscreenEnvEnabled() ?
// position=absolute takes zero layout height so the spinner
Expand Down
9 changes: 8 additions & 1 deletion src/components/PromptInput/PromptInputFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Props = {
setHistoryQuery: (query: string) => void;
historyFailedMatch: boolean;
onOpenTasksDialog?: (taskId?: string) => void;
tokenCount: number;
isCalculatingTokens: boolean;
};
function PromptInputFooter({
apiKeyStatus,
Expand Down Expand Up @@ -92,7 +94,9 @@ function PromptInputFooter({
historyQuery,
setHistoryQuery,
historyFailedMatch,
onOpenTasksDialog
onOpenTasksDialog,
tokenCount,
isCalculatingTokens
}: Props): ReactNode {
const settings = useSettings();
const {
Expand Down Expand Up @@ -142,6 +146,9 @@ function PromptInputFooter({
<PromptInputFooterLeftSide exitMessage={exitMessage} vimMode={vimMode} mode={mode} toolPermissionContext={toolPermissionContext} suppressHint={suppressHint} isLoading={isLoading} tasksSelected={pillSelected} teamsSelected={teamsSelected} teammateFooterIndex={teammateFooterIndex} tmuxSelected={tmuxSelected} isPasting={isPasting} isSearching={isSearching} historyQuery={historyQuery} setHistoryQuery={setHistoryQuery} historyFailedMatch={historyFailedMatch} onOpenTasksDialog={onOpenTasksDialog} />
</Box>
<Box flexShrink={1} gap={1}>
<Text dimColor>
{isCalculatingTokens ? '...' : `${tokenCount} tokens`}
</Text>
{isFullscreen ? null : <Notifications apiKeyStatus={apiKeyStatus} autoUpdaterResult={autoUpdaterResult} debug={debug} isAutoUpdating={isAutoUpdating} verbose={verbose} messages={messages} onAutoUpdaterResult={onAutoUpdaterResult} onChangeIsUpdating={onChangeIsUpdating} ideSelection={ideSelection} mcpClients={mcpClients} isInputWrapped={isInputWrapped} isNarrow={isNarrow} />}
{"external" === 'ant' && isUndercover() && <Text dimColor>undercover</Text>}
<BridgeStatusIndicator bridgeSelected={bridgeSelected} />
Expand Down