agent-ecosystem/src/renderer/utils/contextMath.ts
iliya db08b0ae9e feat: enhance team provisioning instructions and context handling
- Added clarification on handling review requests in TeamProvisioningService to prevent redundant notifications.
- Introduced a new utility function to compute remaining context in contextMath, improving context management.
- Updated ChatHistory component to display remaining context urgency and percentage.
- Enhanced UserChatGroup with improved expand/collapse functionality and sticky "Show less" button.
- Updated tests to validate new context handling and messaging instructions.
2026-03-10 13:34:10 +02:00

51 lines
1.7 KiB
TypeScript

import { DEFAULT_CONTEXT_WINDOW } from '@shared/utils/modelParser';
import type { ContextInjection } from '@renderer/types/contextInjection';
export function sumContextInjectionTokens(injections: readonly ContextInjection[]): number {
let sum = 0;
for (const inj of injections) {
sum += inj.estimatedTokens ?? 0;
}
return sum;
}
export function computePercentOfTotal(
visibleTokens: number,
totalSessionTokens: number | undefined
): number | null {
if (totalSessionTokens === undefined || totalSessionTokens <= 0) return null;
if (!Number.isFinite(visibleTokens) || visibleTokens <= 0) return 0;
return Math.min((visibleTokens / totalSessionTokens) * 100, 100);
}
export function formatPercentOfTotal(
visibleTokens: number,
totalSessionTokens: number | undefined
): string | null {
const pct = computePercentOfTotal(visibleTokens, totalSessionTokens);
if (pct === null) return null;
return `${pct.toFixed(1)}% of input`;
}
export type ContextUrgency = 'normal' | 'warning' | 'critical';
export interface RemainingContext {
remainingPct: number;
urgency: ContextUrgency;
}
/**
* Compute how much context window remains before compaction.
* Returns null if input data is unavailable.
*/
export function computeRemainingContext(
totalInputTokens: number | undefined,
contextWindow: number = DEFAULT_CONTEXT_WINDOW
): RemainingContext | null {
if (totalInputTokens === undefined || totalInputTokens <= 0) return null;
const remainingPct = Math.max(((contextWindow - totalInputTokens) / contextWindow) * 100, 0);
const urgency: ContextUrgency =
remainingPct < 20 ? 'critical' : remainingPct < 40 ? 'warning' : 'normal';
return { remainingPct, urgency };
}