Skip to content

Agent Zero AI framework

Move model config snapshot sync into store

Alessandrob152030a9517
diff --git a/plugins/_model_config/webui/config.html b/plugins/_model_config/webui/config.html
index 45def5ec5..7c21309ac 100644
--- a/plugins/_model_config/webui/config.html
+++ b/plugins/_model_config/webui/config.html
@@ -7,24 +7,12 @@
</head>
<body>
- <div x-data="{ lastConfig: null }"
- x-effect="
- if (config && config !== lastConfig) {
- lastConfig = config;
- const snapshotBeforeInit = typeof context !== 'undefined'
- ? context._toComparableJson(config)
- : null;
- $store.modelConfig.initConfigFields(config);
- if (typeof context !== 'undefined' && context.settingsSnapshotJson === snapshotBeforeInit) {
- context.settingsSnapshotJson = context._toComparableJson(config);
- }
- }
- "
+ <div x-data
x-init="
await $store.modelConfig.ensureLoaded();
$store.modelConfig.resetApiKeyDrafts();
await $store.modelConfig.refreshApiKeyStatus();
- $store.modelConfig.installSettingsHooks(context, config);
+ $store.modelConfig.installSettingsHooks(context);
">
<template x-if="config && $store.modelConfig._loaded">
<div class="model-config-sections">
diff --git a/plugins/_model_config/webui/model-config-store.js b/plugins/_model_config/webui/model-config-store.js
index f16056828..0468c7569 100644
--- a/plugins/_model_config/webui/model-config-store.js
+++ b/plugins/_model_config/webui/model-config-store.js
@@ -235,6 +235,25 @@ export const store = createStore("modelConfig", {
if (config?.embedding_model) config.embedding_model._kwargs_text = kwargsToText(config.embedding_model.kwargs);
},
+ syncContextConfigFields(context, refreshCleanSnapshot = false) {
+ const config = context?.settings;
+ if (!config || typeof config !== 'object') return;
+
+ const snapshotBeforeInit = refreshCleanSnapshot && typeof context._toComparableJson === 'function'
+ ? context._toComparableJson(config)
+ : null;
+
+ this.initConfigFields(config);
+
+ if (
+ refreshCleanSnapshot &&
+ typeof context._toComparableJson === 'function' &&
+ context.settingsSnapshotJson === snapshotBeforeInit
+ ) {
+ context.settingsSnapshotJson = context._toComparableJson(config);
+ }
+ },
+
// Global presets
async loadGlobalPresets() {
try {
@@ -300,13 +319,25 @@ export const store = createStore("modelConfig", {
},
/**
- * Install save and reset hooks on the plugin settings context.
- * - Save: persists dirty API keys before the normal config save.
- * - Reset: reloads global presets when settings are reset to defaults.
+ * Install hooks on the plugin settings context.
+ * - Load/reset: initialize UI-only model fields when the settings object changes.
+ * - Save: persist dirty API keys before the normal config save.
+ * - Reset: reload global presets when settings are reset to defaults.
*/
- installSettingsHooks(context, config) {
+ installSettingsHooks(context) {
if (!context || context.__modelConfigHooksInstalled) return;
+ this.syncContextConfigFields(context, true);
+
+ const originalLoadSettings = context.loadSettings?.bind(context);
+ if (originalLoadSettings) {
+ context.loadSettings = async (...args) => {
+ const result = await originalLoadSettings(...args);
+ this.syncContextConfigFields(context, true);
+ return result;
+ };
+ }
+
const originalSave = context.save.bind(context);
context.save = async () => {
context.error = null;
@@ -324,6 +355,7 @@ export const store = createStore("modelConfig", {
const before = context.settings;
await originalReset();
if (context.settings !== before) {
+ this.syncContextConfigFields(context);
await this.resetGlobalPresets();
}
};
diff --git a/tests/test_model_config_api_keys.py b/tests/test_model_config_api_keys.py
index d9fe9ec4c..7f20b88c2 100644
--- a/tests/test_model_config_api_keys.py
+++ b/tests/test_model_config_api_keys.py
@@ -120,10 +120,14 @@ def test_model_config_frontend_tracks_inline_api_key_edits():
def test_model_config_snapshot_sync_only_adjusts_clean_loaded_configs():
config_path = PROJECT_ROOT / "plugins" / "_model_config" / "webui" / "config.html"
+ store_path = PROJECT_ROOT / "plugins" / "_model_config" / "webui" / "model-config-store.js"
config_content = config_path.read_text(encoding="utf-8")
+ store_content = store_path.read_text(encoding="utf-8")
- assert "const snapshotBeforeInit" in config_content
- assert "context.settingsSnapshotJson === snapshotBeforeInit" in config_content
+ assert "x-effect" not in config_content
+ assert "syncContextConfigFields(context, true)" in store_content
+ assert "context.loadSettings = async" in store_content
+ assert "context.settingsSnapshotJson === snapshotBeforeInit" in store_content
def test_model_switcher_frontend_renders_custom_overrides():