feat(gateway): expose context usage and warning events

This commit is contained in:
William Valentin
2026-02-16 15:44:09 -08:00
parent 8758ea8f1c
commit fee8be1de0
11 changed files with 645 additions and 334 deletions
+49
View File
@@ -200,6 +200,47 @@ export class SessionBridge {
return results;
}
/** Get estimated context budget for all active sessions. */
getAllContextUsage(): Array<{
sessionId: string;
budget: {
estimatedTokens: number;
contextWindow: number;
remainingTokens: number;
usagePct: number;
thresholdPct: number;
thresholdTokens: number;
shouldCompact: boolean;
};
}> {
const results: Array<{
sessionId: string;
budget: {
estimatedTokens: number;
contextWindow: number;
remainingTokens: number;
usagePct: number;
thresholdPct: number;
thresholdTokens: number;
shouldCompact: boolean;
};
}> = [];
const seen = new Set<string>();
for (const client of this.clients.values()) {
if (seen.has(client.sessionId)) {
continue;
}
seen.add(client.sessionId);
results.push({
sessionId: client.sessionId,
budget: client.agent.getContextBudget(),
});
}
return results;
}
private getOrCreateAgent(sessionId: string): AgentOrchestrator {
let agent = this.agents.get(sessionId);
if (!agent) {
@@ -233,6 +274,14 @@ export class SessionBridge {
keepTurns: config.compaction.keep_turns,
summaryMaxTokens: config.compaction.summary_max_tokens,
importanceThreshold: config.compaction.importance_threshold,
proactive: {
enabled: config.compaction.proactive.enabled,
warnPct: config.compaction.proactive.warn_pct,
checkpointPct: config.compaction.proactive.checkpoint_pct,
autoCompactPct: config.compaction.proactive.auto_compact_pct,
checkpointCooldownMs: config.compaction.proactive.checkpoint_cooldown_ms,
memoryNamespace: config.compaction.proactive.memory_namespace,
},
} : undefined,
modelName: config?.models.default.model,
contextWindow: config?.models.default.context_window,