diff --git a/plugins/_context_window/extensions/python/message_loop_prompts_after/_99_capture_context_usage.py b/plugins/_context_window/extensions/python/message_loop_prompts_after/_99_capture_context_usage.py new file mode 100644 index 0000000000000000000000000000000000000000..8b64b1071fca8dac04a6f5dceef740a01123fbb6 --- /dev/null +++ b/plugins/_context_window/extensions/python/message_loop_prompts_after/_99_capture_context_usage.py @@ -0,0 +1,8 @@ +from agent import LoopData +from helpers.extension import Extension +from plugins._context_window.helpers.usage import capture_context + + +class CaptureContextUsage(Extension): + def execute(self, loop_data: LoopData | None = None, **kwargs): + capture_context(self.agent, loop_data) diff --git a/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js b/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js new file mode 100644 index 0000000000000000000000000000000000000000..5a21adbd55f5133a771c30d853a15598f96bd3a3 --- /dev/null +++ b/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js @@ -0,0 +1,42 @@ +import { store as contextWindowStore } from "/plugins/_context_window/webui/context-window-store.js"; + +const OVERRIDE_REVISION_KEY = "_model_config_override_revision"; +let lastContextId = ""; +let lastRevision = null; +let lastGenerationKey = ""; + +function latestGenerationKey(logs) { + if (!Array.isArray(logs)) return ""; + for (let index = logs.length - 1; index >= 0; index--) { + const item = logs[index]; + if (item?.type !== "agent" || Number(item.agentno || 0) !== 0) continue; + return `${item.no ?? ""}:${item.id ?? ""}`; + } + return ""; +} + +export default async function refreshContextWindow(ctx) { + const snapshot = ctx?.snapshot; + const contextId = String(snapshot?.context || ""); + if (!contextId) { + lastContextId = ""; + lastRevision = null; + lastGenerationKey = ""; + return; + } + + const contexts = Array.isArray(snapshot?.contexts) ? snapshot.contexts : []; + const active = contexts.find(item => item?.id === contextId) || null; + const revision = active?.[OVERRIDE_REVISION_KEY] || null; + const generationKey = latestGenerationKey(snapshot?.logs); + if ( + contextId === lastContextId + && revision === lastRevision + && (!generationKey || generationKey === lastGenerationKey) + ) return; + + lastContextId = contextId; + lastRevision = revision; + if (generationKey) lastGenerationKey = generationKey; + await contextWindowStore.refresh(contextId); +} diff --git a/plugins/_context_window/extensions/webui/interface-controls-end/context-window.html b/plugins/_context_window/extensions/webui/interface-controls-end/context-window.html new file mode 100644 index 0000000000000000000000000000000000000000..c56aed5e89050ac58b5c6b6187a4b2c324dcaaac --- /dev/null +++ b/plugins/_context_window/extensions/webui/interface-controls-end/context-window.html @@ -0,0 +1,40 @@ + + +
+ Install the CLI Connector when you want A0 to use your host terminal, + local files, and a browser from your computer. +
+curl -LsSf https://cli.agent-zero.ai/install.sh | sh
+ irm https://cli.agent-zero.ai/install.ps1 | iex
+
+ Extend Agent Zero across your favorite platforms.
+