Agent Zero AI framework
feat: validate min tokens for compaction
Nicolas Leão140869eb9ac0
diff --git a/plugins/_chat_compaction/api/compact_chat.py b/plugins/_chat_compaction/api/compact_chat.pyindex 98738a38f..2c2e7b047 100644--- a/plugins/_chat_compaction/api/compact_chat.py+++ b/plugins/_chat_compaction/api/compact_chat.py@@ -2,6 +2,7 @@from helpers.api import ApiHandler, Input, Output, Request, Responsefrom agent import AgentContextfrom plugins._chat_compaction.helpers.compactor import (+ MIN_COMPACTION_TOKENS,get_compaction_stats,run_compaction,)@@ -28,8 +29,15 @@ class CompactChat(ApiHandler):if visible_count <= 1:return Response("Not enough messages to compact", 400)+ # Gate both stats and compact — no point opening the modal for tiny chats+ stats = await get_compaction_stats(context)+ if stats["token_count"] < MIN_COMPACTION_TOKENS:+ return {+ "ok": False,+ "message": f"Not enough content to compact (minimum {MIN_COMPACTION_TOKENS:,} tokens)",+ }+if action == "stats":- stats = await get_compaction_stats(context)return {"ok": True, "stats": stats}elif action == "compact":diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.pyindex e7220048c..011711582 100644--- a/plugins/_chat_compaction/helpers/compactor.py+++ b/plugins/_chat_compaction/helpers/compactor.py@@ -13,6 +13,8 @@ from helpers.persist_chat import (remove_msg_files,)from helpers.state_monitor_integration import mark_dirty_all++MIN_COMPACTION_TOKENS = 1000from plugins._model_config.helpers.model_config import (get_chat_model_config,get_utility_model_config,diff --git a/plugins/_chat_compaction/plugin.yaml b/plugins/_chat_compaction/plugin.yamlindex 9425c5678..902032fef 100644--- a/plugins/_chat_compaction/plugin.yaml+++ b/plugins/_chat_compaction/plugin.yaml@@ -2,7 +2,3 @@ name: _chat_compactiontitle: Chat Compactiondescription: Compact entire chat history into a single optimized summary message.version: 1.0.0-settings_sections:- - agent-per_project_config: false-per_agent_config: falsediff --git a/plugins/_chat_compaction/webui/compact-modal.html b/plugins/_chat_compaction/webui/compact-modal.htmlindex 070e12aee..665da605b 100644--- a/plugins/_chat_compaction/webui/compact-modal.html+++ b/plugins/_chat_compaction/webui/compact-modal.html@@ -61,6 +61,13 @@<div class="model-name" x-text="$store.compactStore.selectedModelDisplay"></div></div>+ <template x-if="!$store.compactStore?.canCompact">+ <div class="stats-info">+ <span class="material-symbols-outlined">info</span>+ <span>Conversation is too short to compact (minimum 1,000 tokens).</span>+ </div>+ </template>+<div class="stats-warning"><span class="material-symbols-outlined">warning</span><span>This action cannot be undone. The original conversation will be replaced with a summary.</span>@@ -83,7 +90,7 @@<buttonclass="button primary danger"@click="$store.compactStore?.compact()"- :disabled="$store.compactStore?.compacting || !$store.compactStore?.stats"+ :disabled="$store.compactStore?.compacting || !$store.compactStore?.canCompact"><template x-if="$store.compactStore?.compacting"><span class="loading-spinner"></span>@@ -265,6 +272,24 @@text-overflow: ellipsis;}+ .stats-info {+ display: flex;+ align-items: flex-start;+ gap: var(--spacing-sm);+ padding: var(--spacing-md);+ margin-bottom: var(--spacing-md);+ background: var(--color-info-bg, rgba(59, 130, 246, 0.1));+ border: 1px solid var(--color-info-border, rgba(59, 130, 246, 0.3));+ border-radius: var(--border-radius-md);+ color: var(--color-info-text, #1e40af);+ font-size: 0.85rem;+ }++ .stats-info .material-symbols-outlined {+ font-size: 1.2rem;+ flex-shrink: 0;+ }+.stats-warning {display: flex;align-items: flex-start;diff --git a/plugins/_chat_compaction/webui/compact-store.js b/plugins/_chat_compaction/webui/compact-store.jsindex a70236dff..25973ec82 100644--- a/plugins/_chat_compaction/webui/compact-store.js+++ b/plugins/_chat_compaction/webui/compact-store.js@@ -13,6 +13,12 @@ export const store = createStore("compactStore", {selectedPresetName: "",useChatModel: true,+ minTokens: 1000,++ get canCompact() {+ return this.stats && this.stats.token_count >= this.minTokens;+ },+get selectedModelDisplay() {if (this.selectedPresetName) {const preset = this.presets.find(diff --git a/plugins/_chat_compaction/webui/config.html b/plugins/_chat_compaction/webui/config.htmldeleted file mode 100644index 3590e2d10..000000000--- a/plugins/_chat_compaction/webui/config.html+++ /dev/null@@ -1,104 +0,0 @@-<html>-<head>- <title>Compaction Plugin Settings</title>-</head>-<body>- <div x-data>- <template x-if="config">- <div>- <div class="section-title">Compaction Configuration</div>- <div class="section-description">- Configure how the chat compaction feature works.- </div>-- <div class="field">- <div class="field-label">- <div class="field-title">Use chat model</div>- <div class="field-description">- When enabled, uses the currently selected chat model for compaction.- When disabled, uses the utility model (usually faster and cheaper).- </div>- </div>- <div class="field-control">- <label class="toggle">- <input type="checkbox" x-model="config.use_chat_model" />- <span class="toggler"></span>- </label>- </div>- </div>- </div>- </template>- </div>-- <style>- .section-title {- font-size: 1rem;- font-weight: 600;- margin-bottom: 8px;- color: var(--color-text, #e5e5e5);- }- .section-description {- font-size: 0.85rem;- color: var(--color-text-secondary, #999);- margin-bottom: 20px;- line-height: 1.5;- }- .field {- margin-bottom: 20px;- }- .field-label {- margin-bottom: 8px;- }- .field-title {- font-weight: 500;- color: var(--color-text, #e5e5e5);- margin-bottom: 4px;- }- .field-description {- font-size: 0.8rem;- color: var(--color-text-secondary, #999);- line-height: 1.4;- }- .field-control {- margin-top: 8px;- }- .toggle {- display: inline-flex;- align-items: center;- cursor: pointer;- }- .toggle input {- display: none;- }- .toggler {- width: 44px;- height: 24px;- background: var(--color-border, #444);- border-radius: 12px;- position: relative;- transition: background 0.2s;- }- .toggler::after {- content: '';- position: absolute;- width: 20px;- height: 20px;- background: white;- border-radius: 50%;- top: 2px;- left: 2px;- transition: transform 0.2s;- }- .toggle input:checked + .toggler {- background: var(--color-primary, #3b82f6);- }- .toggle input:checked + .toggler::after {- transform: translateX(20px);- }- </style>-- <script type="module">- import { store } from "/components/plugins/plugin-settings-store.js";- </script>-</body>-</html>