Agent Zero AI framework
feat: allow model selection in compaction modal
Nicolas Leão32eb5f8fba52
diff --git a/plugins/_chat_compaction/api/compact_chat.py b/plugins/_chat_compaction/api/compact_chat.pyindex 94569fb30..98738a38f 100644--- a/plugins/_chat_compaction/api/compact_chat.py+++ b/plugins/_chat_compaction/api/compact_chat.py@@ -33,12 +33,12 @@ class CompactChat(ApiHandler):return {"ok": True, "stats": stats}elif action == "compact":- from helpers.plugins import get_plugin_config- agent = context.agent0- plugin_config = get_plugin_config("_chat_compaction", agent=agent) or {}- use_chat_model = plugin_config.get("use_chat_model", True)+ use_chat_model = input.get("use_chat_model", True)+ preset_name = input.get("preset_name") or None- context.run_task(_run_compaction_task, context, use_chat_model)+ context.run_task(+ _run_compaction_task, context, use_chat_model, preset_name+ )return {"ok": True, "message": "Compaction started"}@@ -46,10 +46,10 @@ class CompactChat(ApiHandler):return Response(f"Unknown action: {action}", 400)-async def _run_compaction_task(context, use_chat_model: bool):+async def _run_compaction_task(context, use_chat_model: bool, preset_name: str | None):"""Wrapper to run compaction and handle errors."""try:- await run_compaction(context, use_chat_model)+ await run_compaction(context, use_chat_model, preset_name)except Exception as e:context.log.log(type="error",diff --git a/plugins/_chat_compaction/extensions/webui/chat-input-bottom-actions-start/compact-button.html b/plugins/_chat_compaction/extensions/webui/chat-input-bottom-actions-start/compact-button.htmlindex 0e145d339..51d8ea923 100644--- a/plugins/_chat_compaction/extensions/webui/chat-input-bottom-actions-start/compact-button.html+++ b/plugins/_chat_compaction/extensions/webui/chat-input-bottom-actions-start/compact-button.html@@ -58,15 +58,35 @@<span class="cmpct-stat-label">Tokens</span><span class="cmpct-stat-value" x-text="$store.compactStore?.stats?.token_count?.toLocaleString()"></span></div>- <div class="cmpct-stat">- <span class="cmpct-stat-label">Model</span>- <span class="cmpct-stat-value" x-text="$store.compactStore?.stats?.model_name"></span>+ </div>++ <!-- Model selection -->+ <div class="cmpct-model-section">+ <div class="cmpct-model-label">Model</div>++ <div class="cmpct-model-controls">+ <select class="cmpct-select"+ x-model="$store.compactStore.selectedPresetName">+ <option value="">Current</option>+ <template x-for="preset in $store.compactStore.presets" :key="preset.name">+ <option :value="preset.name" x-text="preset.name"></option>+ </template>+ </select>++ <div class="cmpct-toggle">+ <button :class="{ active: $store.compactStore.useChatModel }"+ @click="$store.compactStore.useChatModel = true">Chat</button>+ <button :class="{ active: !$store.compactStore.useChatModel }"+ @click="$store.compactStore.useChatModel = false">Utility</button>+ </div></div>++ <div class="cmpct-model-name" x-text="$store.compactStore.selectedModelDisplay"></div></div>- <div class="cmpct-warning">- <span class="material-symbols-outlined">warning</span>- <span>This action cannot be undone. The original conversation will be replaced with a summary.</span>+ <div class="cmpct-info">+ <span class="material-symbols-outlined">check_circle</span>+ <span>The context will be replaced with a compacted summary. The original conversation will be backed up.</span></div></div></template>@@ -115,7 +135,7 @@border-radius: 12px;box-shadow: 0 4px 23px rgba(0, 0, 0, 0.3);width: 90%;- max-width: 420px;+ max-width: 560px;overflow: hidden;}@@ -160,7 +180,7 @@.cmpct-stats {display: grid;- grid-template-columns: repeat(3, 1fr);+ grid-template-columns: repeat(2, 1fr);gap: 12px;margin-bottom: 16px;}@@ -188,19 +208,96 @@color: var(--color-text, #e5e5e5);}- .cmpct-warning {+ /* Model selection section */+ .cmpct-model-section {+ margin-bottom: 16px;+ }++ .cmpct-model-label {+ font-size: 0.7rem;+ color: var(--color-text-secondary, #999);+ text-transform: uppercase;+ letter-spacing: 0.05em;+ margin-bottom: 8px;+ }++ .cmpct-model-controls {+ display: flex;+ gap: 8px;+ margin-bottom: 6px;+ }++ .cmpct-select {+ flex: 1;+ min-width: 0;+ padding: 6px 10px;+ border-radius: 6px;+ border: 1px solid var(--color-border, #333);+ background: var(--color-background-muted, #252525);+ color: var(--color-text, #e5e5e5);+ font-size: 0.85rem;+ cursor: pointer;+ }++ .cmpct-select:focus {+ outline: none;+ border-color: var(--color-primary, #3b82f6);+ }++ .cmpct-toggle {+ display: flex;+ border-radius: 6px;+ border: 1px solid var(--color-border, #333);+ overflow: hidden;+ flex-shrink: 0;+ }++ .cmpct-toggle button {+ padding: 6px 14px;+ border: none;+ background: var(--color-background-muted, #252525);+ color: var(--color-text-secondary, #999);+ font-size: 0.8rem;+ font-weight: 500;+ cursor: pointer;+ transition: all 0.15s;+ }++ .cmpct-toggle button:first-child {+ border-right: 1px solid var(--color-border, #333);+ }++ .cmpct-toggle button.active {+ background: var(--color-primary, #3b82f6);+ color: white;+ }++ .cmpct-toggle button:hover:not(.active) {+ background: var(--color-background-hover, #444);+ }++ .cmpct-model-name {+ font-size: 0.85rem;+ color: var(--color-text, #e5e5e5);+ opacity: 0.7;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+ }++ .cmpct-info {display: flex;align-items: flex-start;gap: 8px;padding: 10px 12px;- background: rgba(245, 158, 11, 0.1);- border: 1px solid rgba(245, 158, 11, 0.3);+ background: rgba(34, 197, 94, 0.1);+ border: 1px solid rgba(34, 197, 94, 0.25);border-radius: 8px;- color: #f59e0b;+ color: #4ade80;font-size: 0.8rem;}- .cmpct-warning .material-symbols-outlined {+ .cmpct-info .material-symbols-outlined {font-size: 1.1rem;flex-shrink: 0;}diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.pyindex 1df021f74..e7220048c 100644--- a/plugins/_chat_compaction/helpers/compactor.py+++ b/plugins/_chat_compaction/helpers/compactor.py@@ -1,13 +1,11 @@"""Core compaction logic for the compaction plugin."""-import asyncioimport osfrom datetime import datetime-from typing import Callable+import models as models_modulefrom agent import Agent-from helpers import files, history, tokens+from helpers import tokensfrom helpers.history import History, output_text-from helpers.log import Logfrom helpers.persist_chat import (export_json_chat,get_chat_folder_path,@@ -15,7 +13,14 @@ from helpers.persist_chat import (remove_msg_files,)from helpers.state_monitor_integration import mark_dirty_all-from plugins._model_config.helpers.model_config import get_chat_model_config+from plugins._model_config.helpers.model_config import (+ get_chat_model_config,+ get_utility_model_config,+ get_preset_by_name,+ build_model_config,+ build_chat_model,+ build_utility_model,+)def _save_pre_compaction_backup(context, full_text: str) -> dict[str, str]:@@ -40,7 +45,36 @@ def _save_pre_compaction_backup(context, full_text: str) -> dict[str, str]:return {"json": json_path, "txt": txt_path}-async def run_compaction(context, use_chat_model: bool = True) -> None:+def _build_model(use_chat_model: bool, preset_name: str | None, agent):+ """Build the LLM model for compaction based on user selection.++ If preset_name is given, builds from that preset's config.+ Otherwise falls back to the agent's currently configured model.+ """+ if preset_name:+ preset = get_preset_by_name(preset_name)+ if preset:+ model_key = "chat" if use_chat_model else "utility"+ cfg = preset.get(model_key, {})+ if cfg.get("provider") or cfg.get("name"):+ mc = build_model_config(cfg, models_module.ModelType.CHAT)+ return cfg, models_module.get_chat_model(+ mc.provider, mc.name, model_config=mc, **mc.build_kwargs()+ )++ if use_chat_model:+ cfg = get_chat_model_config(agent)+ return cfg, build_chat_model(agent)+ else:+ cfg = get_utility_model_config(agent)+ return cfg, build_utility_model(agent)+++async def run_compaction(+ context,+ use_chat_model: bool = True,+ preset_name: str | None = None,+) -> None:"""Compact the chat history into a single summarized message.@@ -66,17 +100,11 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:if not full_text.strip():raise ValueError("No conversation content to compact")- # Step 2: Estimate tokens and get model config+ # Step 2: Estimate tokens, resolve model, and compute context budgettoken_count = tokens.approximate_tokens(full_text)-- model_config = get_chat_model_config() if use_chat_model else None- if model_config is None:- # Fallback: use default context length- ctx_length = 128000- else:- ctx_length = int(model_config.get("ctx_length", 128000))-- # Leave some buffer for the prompt and response++ resolved_cfg, model = _build_model(use_chat_model, preset_name, agent)+ ctx_length = int(resolved_cfg.get("ctx_length", 128000)) if resolved_cfg else 128000max_input_tokens = int(ctx_length * 0.7)# Step 3: Create progress log item (count user-visible messages only)@@ -91,12 +119,11 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:# Step 4: Handle large histories by chunking if necessaryif token_count > max_input_tokens:summary = await _compact_large_history(- agent, full_text, token_count, max_input_tokens, log_item, use_chat_model+ agent, full_text, token_count, max_input_tokens, log_item, model)else:- # Single-pass compactionsummary = await _compact_single_pass(- agent, full_text, log_item, use_chat_model+ agent, full_text, log_item, model)if not summary or not summary.strip():@@ -149,130 +176,69 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:raise-async def _compact_single_pass(- agent,- full_text: str,- log_item,- use_chat_model: bool-) -> str:- """Compact history in a single LLM call."""-+async def _compact_single_pass(agent, full_text: str, log_item, model) -> str:+ """Compact history in a single LLM call using the provided model."""system_prompt = agent.read_prompt("compact.sys.md")user_prompt = agent.read_prompt("compact.msg.md", conversation=full_text)-- if use_chat_model:- from langchain_core.messages import HumanMessage, SystemMessage- messages = [- SystemMessage(content=system_prompt),- HumanMessage(content=user_prompt)- ]-- async def chat_stream_cb(chunk: str, total: str):- if chunk:- log_item.stream(content=chunk)-- summary, _ = await agent.call_chat_model(- messages=messages,- response_callback=chat_stream_cb,- )- else:- async def util_stream_cb(chunk: str):- if chunk:- log_item.stream(content=chunk)-- summary = await agent.call_utility_model(- system=system_prompt,- message=user_prompt,- callback=util_stream_cb,- )-++ async def stream_cb(chunk: str, total: str):+ if chunk:+ log_item.stream(content=chunk)++ summary, _ = await model.unified_call(+ system_message=system_prompt,+ user_message=user_prompt,+ response_callback=stream_cb,+ )return summaryasync def _compact_large_history(- agent,- full_text: str,- token_count: int,- max_input_tokens: int,- log_item,- use_chat_model: bool+ agent, full_text: str, token_count: int, max_input_tokens: int, log_item, model) -> str:- """- Handle large histories by splitting into chunks and summarizing iteratively.- """+ """Handle large histories by splitting into chunks and summarizing iteratively."""log_item.update(content=f"History is large (~{token_count} tokens). Splitting into chunks...",)-- # Split conversation into roughly equal halves+lines = full_text.split('\n')mid = len(lines) // 2-- chunks = [- '\n'.join(lines[:mid]),- '\n'.join(lines[mid:])- ]-+ chunks = ['\n'.join(lines[:mid]), '\n'.join(lines[mid:])]+summaries = []for i, chunk in enumerate(chunks, 1):- log_item.update(- content=f"Summarizing part {i}/{len(chunks)}...",- )-+ log_item.update(content=f"Summarizing part {i}/{len(chunks)}...")+system_prompt = agent.read_prompt("compact.sys.md")user_prompt = agent.read_prompt("compact.msg.md", conversation=chunk)-- if use_chat_model:- from langchain_core.messages import HumanMessage, SystemMessage- messages = [- SystemMessage(content=system_prompt),- HumanMessage(content=user_prompt)- ]- chunk_summary, _ = await agent.call_chat_model(- messages=messages,- response_callback=None, # No streaming for chunks- )- else:- chunk_summary = await agent.call_utility_model(- system=system_prompt,- message=user_prompt,- callback=None,- )-++ chunk_summary, _ = await model.unified_call(+ system_message=system_prompt,+ user_message=user_prompt,+ )summaries.append(chunk_summary)-- # Combine summaries+combined = "\n\n---\n\n".join(summaries)-- log_item.update(- content="Creating final summary from parts...",- )-- # Final compaction of combined summaries+ log_item.update(content="Creating final summary from parts...")+final_prompt = agent.read_prompt("compact.sys.md")final_user = agent.read_prompt(- "compact.msg.md",- conversation=f"This is a multi-part conversation. Here are summaries of each part:\n\n{combined}"+ "compact.msg.md",+ conversation=f"This is a multi-part conversation. Here are summaries of each part:\n\n{combined}",+ )++ async def stream_cb(chunk: str, total: str):+ if chunk:+ log_item.stream(content=chunk)++ final_summary, _ = await model.unified_call(+ system_message=final_prompt,+ user_message=final_user,+ response_callback=stream_cb,)-- if use_chat_model:- from langchain_core.messages import HumanMessage, SystemMessage- messages = [- SystemMessage(content=final_prompt),- HumanMessage(content=final_user)- ]- final_summary, _ = await agent.call_chat_model(- messages=messages,- response_callback=lambda chunk, total: log_item.stream(content=chunk),- )- else:- final_summary = await agent.call_utility_model(- system=final_prompt,- message=final_user,- callback=lambda chunk: log_item.stream(content=chunk),- )-return final_summary++async def get_compaction_stats(context) -> dict:"""Get statistics about the current chat for the confirmation modal.@@ -296,12 +262,16 @@ async def get_compaction_stats(context) -> dict:full_text = output_text(history_output, ai_label="assistant", human_label="user")token_count = tokens.approximate_tokens(full_text) if full_text else 0- # Get model name- model_config = get_chat_model_config()- model_name = model_config.get("name", "Default Model") if model_config else "Utility Model"+ # Get model names for both chat and utility+ chat_cfg = get_chat_model_config(agent)+ utility_cfg = get_utility_model_config(agent)+ chat_model_name = chat_cfg.get("name", "Default") if chat_cfg else "Default"+ utility_model_name = utility_cfg.get("name", "Default") if utility_cfg else "Default"return {"message_count": message_count,"token_count": token_count,- "model_name": model_name,+ "model_name": chat_model_name,+ "chat_model_name": chat_model_name,+ "utility_model_name": utility_model_name,}diff --git a/plugins/_chat_compaction/webui/compact-modal.html b/plugins/_chat_compaction/webui/compact-modal.htmlindex 840b6db04..070e12aee 100644--- a/plugins/_chat_compaction/webui/compact-modal.html+++ b/plugins/_chat_compaction/webui/compact-modal.html@@ -29,10 +29,36 @@<span class="stat-label">Tokens</span><span class="stat-value" x-text="$store.compactStore?.stats?.token_count?.toLocaleString()"></span></div>- <div class="stat-item">- <span class="stat-label">Model</span>- <span class="stat-value" x-text="$store.compactStore?.stats?.model_name"></span>+ </div>++ <!-- Model selection -->+ <div class="model-section">+ <div class="model-section-label">Model</div>++ <div class="model-controls">+ <!-- Preset dropdown -->+ <select class="compact-select"+ x-model="$store.compactStore.selectedPresetName">+ <option value="">Current</option>+ <template x-for="preset in $store.compactStore.presets" :key="preset.name">+ <option :value="preset.name" x-text="preset.name"></option>+ </template>+ </select>++ <!-- Chat / Utility toggle -->+ <div class="model-type-toggle">+ <button :class="{ active: $store.compactStore.useChatModel }"+ @click="$store.compactStore.useChatModel = true">+ Chat+ </button>+ <button :class="{ active: !$store.compactStore.useChatModel }"+ @click="$store.compactStore.useChatModel = false">+ Utility+ </button>+ </div></div>++ <div class="model-name" x-text="$store.compactStore.selectedModelDisplay"></div></div><div class="stats-warning">@@ -88,7 +114,7 @@border-radius: var(--border-radius-md);box-shadow: var(--shadow-lg);width: 90%;- max-width: 500px;+ max-width: 640px;max-height: 90vh;overflow-y: auto;}@@ -133,7 +159,7 @@.stats-grid {display: grid;- grid-template-columns: repeat(3, 1fr);+ grid-template-columns: repeat(2, 1fr);gap: var(--spacing-md);margin-bottom: var(--spacing-lg);}@@ -160,6 +186,84 @@font-weight: 600;color: var(--color-text);}++ /* ── Model selection ── */+ .model-section {+ margin-bottom: var(--spacing-lg);+ }++ .model-section-label {+ font-size: 0.75rem;+ color: var(--color-text-secondary);+ text-transform: uppercase;+ letter-spacing: 0.05em;+ margin-bottom: var(--spacing-sm);+ }++ .model-controls {+ display: flex;+ gap: var(--spacing-sm);+ margin-bottom: var(--spacing-sm);+ }++ .compact-select {+ flex: 1;+ min-width: 0;+ padding: 6px 10px;+ border-radius: var(--border-radius-md);+ border: 1px solid var(--color-border);+ background: var(--color-background-muted);+ color: var(--color-text);+ font-size: 0.85rem;+ cursor: pointer;+ appearance: auto;+ }++ .compact-select:focus {+ outline: none;+ border-color: var(--color-primary, #3b82f6);+ }++ .model-type-toggle {+ display: flex;+ border-radius: var(--border-radius-md);+ border: 1px solid var(--color-border);+ overflow: hidden;+ flex-shrink: 0;+ }++ .model-type-toggle button {+ padding: 6px 14px;+ border: none;+ background: var(--color-background-muted);+ color: var(--color-text-secondary);+ font-size: 0.8rem;+ font-weight: 500;+ cursor: pointer;+ transition: all 0.15s;+ }++ .model-type-toggle button:not(:last-child) {+ border-right: 1px solid var(--color-border);+ }++ .model-type-toggle button.active {+ background: var(--color-primary, #3b82f6);+ color: white;+ }++ .model-type-toggle button:hover:not(.active) {+ background: var(--color-background-hover);+ }++ .model-name {+ font-size: 0.85rem;+ color: var(--color-text);+ opacity: 0.7;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+ }.stats-warning {display: flex;diff --git a/plugins/_chat_compaction/webui/compact-store.js b/plugins/_chat_compaction/webui/compact-store.jsindex 2a059ffa0..a70236dff 100644--- a/plugins/_chat_compaction/webui/compact-store.js+++ b/plugins/_chat_compaction/webui/compact-store.js@@ -9,6 +9,24 @@ export const store = createStore("compactStore", {compacting: false,stats: null,showModal: false,+ presets: [],+ selectedPresetName: "",+ useChatModel: true,++ get selectedModelDisplay() {+ if (this.selectedPresetName) {+ const preset = this.presets.find(+ (p) => p.name === this.selectedPresetName+ );+ if (preset) {+ const cfg = this.useChatModel ? preset.chat : preset.utility;+ if (cfg?.name) return cfg.name;+ }+ }+ return this.useChatModel+ ? this.stats?.chat_model_name || "Chat Model"+ : this.stats?.utility_model_name || "Utility Model";+ },async fetchStats() {try {@@ -18,16 +36,22 @@ export const store = createStore("compactStore", {return;}- const res = await callJsonApi("/plugins/_chat_compaction/compact_chat", {- context: ctxid,- action: "stats",- });+ const [statsRes, presetsRes] = await Promise.all([+ callJsonApi("/plugins/_chat_compaction/compact_chat", {+ context: ctxid,+ action: "stats",+ }),+ callJsonApi("/plugins/_model_config/model_presets", {+ action: "get",+ }),+ ]);- if (!res?.ok) {- throw new Error(res?.message || "Failed to fetch stats");+ if (!statsRes?.ok) {+ throw new Error(statsRes?.message || "Failed to fetch stats");}- this.stats = res.stats;+ this.stats = statsRes.stats;+ this.presets = presetsRes?.ok ? presetsRes.presets || [] : [];this.showModal = true;} catch (e) {toastFrontendError(e.message, "Compaction");@@ -45,6 +69,8 @@ export const store = createStore("compactStore", {const res = await callJsonApi("/plugins/_chat_compaction/compact_chat", {context: ctxid,action: "compact",+ use_chat_model: this.useChatModel,+ preset_name: this.selectedPresetName || null,});if (!res?.ok) {@@ -63,5 +89,8 @@ export const store = createStore("compactStore", {closeModal() {this.showModal = false;this.stats = null;+ this.presets = [];+ this.selectedPresetName = "";+ this.useChatModel = true;},});