Skip to content
Merged
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
4 changes: 2 additions & 2 deletions site/src/pages/AgentsPage/components/AgentChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ type AgentChatInputProps = {
userPromptHistory?: readonly string[];

// Optional context-usage summary shown to the left of the send button.
// Pass `null` to render fallback values (e.g. when limit is unknown).
// Fields the provider has not reported render as fallback values.
// Omit entirely to hide the indicator.
contextUsage?: AgentContextUsage | null;
contextUsage?: AgentContextUsage;
// Re-pins the chat to the workspace's latest context snapshot,
// surfaced by the context indicator when the pinned context has
// drifted.
Expand Down
13 changes: 5 additions & 8 deletions site/src/pages/AgentsPage/components/ChatPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,11 @@ export const ChatPageInput: React.FC<ChatPageInputProps> = ({
messages,
modelOptions.find((option) => option.id === selectedModel)?.contextLimit,
);
const latestContextUsage =
rawUsage || chatContext
? {
...(rawUsage ?? {}),
compressionThreshold,
context: chatContext,
}
: rawUsage;
const latestContextUsage = {
...rawUsage,
compressionThreshold,
context: chatContext,
};
const queryClient = useQueryClient();
const refreshContextMutation = useMutation(
refreshChatContext(queryClient, chatId ?? ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const Dirty: Story = {
// so the popover explains when the numbers will appear.
export const NoUsage: Story = {
args: {
usage: null,
usage: {},
},
play: async ({ canvasElement }) => {
const button = within(canvasElement).getByRole("button");
Expand Down
32 changes: 15 additions & 17 deletions site/src/pages/AgentsPage/components/ContextUsageIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const ContextDirLabel: React.FC<{ dir: string }> = ({ dir }) => (
);

export const ContextUsageIndicator: React.FC<{
usage: AgentContextUsage | null;
usage: AgentContextUsage;
onRefreshContext?: () => void;
isRefreshingContext?: boolean;
}> = ({ usage, onRefreshContext, isRefreshingContext }) => {
Expand All @@ -249,10 +249,10 @@ export const ContextUsageIndicator: React.FC<{
setOpen(true);
};

const usedTokens = hasFiniteTokenValue(usage?.usedTokens)
const usedTokens = hasFiniteTokenValue(usage.usedTokens)
? usage.usedTokens
: undefined;
const contextLimitTokens = hasFiniteTokenValue(usage?.contextLimitTokens)
const contextLimitTokens = hasFiniteTokenValue(usage.contextLimitTokens)
? usage.contextLimitTokens
: undefined;
const percentUsed =
Expand All @@ -265,21 +265,21 @@ export const ContextUsageIndicator: React.FC<{
// Providers may report usage without token counts. Only a chat with no
// reported usage at all should promise numbers after the next message.
const hasReportedUsage = [
usage?.usedTokens,
usage?.contextLimitTokens,
usage?.inputTokens,
usage?.outputTokens,
usage?.cacheReadTokens,
usage?.cacheCreationTokens,
usage?.reasoningTokens,
usage.usedTokens,
usage.contextLimitTokens,
usage.inputTokens,
usage.outputTokens,
usage.cacheReadTokens,
usage.cacheCreationTokens,
usage.reasoningTokens,
].some(hasFiniteTokenValue);
const percentLabel =
percentUsed === null ? "--" : `${Math.round(percentUsed)}%`;
const clampedPercent = hasPercent
? Math.min(Math.max(percentUsed, 0), 100)
: 0;

const context = usage?.context;
const context = usage.context;
const isDirty = context?.dirty ?? false;
const contextError = context?.error ?? "";
const hasContextError = contextError !== "";
Expand Down Expand Up @@ -386,17 +386,15 @@ export const ContextUsageIndicator: React.FC<{
const statusNote = statusNotes.length > 0 ? ` ${statusNotes.join(" ")}` : "";
let ariaLabel = "Context usage";
if (hasPercent) {
const label = usage?.estimated
? "Estimated context usage"
: "Context usage";
const label = usage.estimated ? "Estimated context usage" : "Context usage";
ariaLabel = `${label} ${percentLabel}. ${formatTokenCount(usedTokens)} of ${formatTokenCount(contextLimitTokens)} tokens used.${statusNote}`;
} else if (statusNote !== "") {
ariaLabel = `Context usage.${statusNote}`;
}

let usageLabel = "Context usage will appear after sending a message.";
if (hasPercent) {
const prefix = usage?.estimated ? "Estimated: " : "";
const prefix = usage.estimated ? "Estimated: " : "";
usageLabel = `${prefix}${percentLabel} - ${formatTokenCountCompact(usedTokens)} / ${formatTokenCountCompact(contextLimitTokens)} context used`;
} else if (hasReportedUsage) {
usageLabel = "Context usage unavailable";
Expand All @@ -405,14 +403,14 @@ export const ContextUsageIndicator: React.FC<{
const panelContent = (
<div className="text-xs text-content-primary">
{usageLabel}
{hasPercent && usage?.estimated && (
{hasPercent && usage.estimated && (
<div className="mt-1 max-w-64 text-content-secondary">
Based on the compacted summary only, excluding other prompt content
and tools. Replaced by measured usage after the next response.
</div>
)}
{hasPercent &&
usage?.compressionThreshold !== undefined &&
usage.compressionThreshold !== undefined &&
usage.compressionThreshold > 0 && (
<div className="mt-1 text-content-secondary">
{`Compacts at ${usage.compressionThreshold}%`}
Expand Down
Loading