Agent Zero AI framework
handling for plugins init script
Alessandro8ddd8b3ae763
diff --git a/python/api/plugins.py b/python/api/plugins.pyindex 103ca198f..48914d765 100644--- a/python/api/plugins.py+++ b/python/api/plugins.py@@ -1,4 +1,8 @@+import jsonimport os+import subprocess+import sys+from datetime import datetime, timezonefrom python.helpers.api import ApiHandler, Request, Responsefrom python.helpers import plugins, files@@ -206,4 +210,62 @@ class Plugins(ApiHandler):return {"ok": True, "content": files.read_file(file_path), "filename": filename}- return Response(status=400, response=f"Unknown action: {action}")+ if action == "run_init_script":+ plugin_name = input.get("plugin_name", "")+ if not plugin_name:+ return Response(status=400, response="Missing plugin_name")++ plugin_dir = plugins.find_plugin_dir(plugin_name)+ if not plugin_dir:+ return Response(status=404, response="Plugin not found")++ init_script = files.get_abs_path(plugin_dir, "initialize.py")+ if not files.exists(init_script):+ return Response(status=404, response="initialize.py not found")++ executed_at = datetime.now(timezone.utc).isoformat()+ try:+ result = subprocess.run(+ [sys.executable, init_script],+ stdout=subprocess.PIPE,+ stderr=subprocess.STDOUT,+ text=True,+ cwd=plugin_dir,+ timeout=120,+ )+ exit_code = result.returncode+ output = result.stdout or ""+ except subprocess.TimeoutExpired:+ exit_code = -1+ output = "Error: script timed out after 120 seconds"+ except Exception as e:+ exit_code = -1+ output = f"Error: {str(e)}"++ exec_record = {"executed_at": executed_at, "exit_code": exit_code}+ exec_path = plugins.determine_plugin_asset_path(plugin_name, "", "", "init_exec.json")+ if exec_path:+ files.write_file(exec_path, json.dumps(exec_record))++ return {+ "ok": exit_code == 0,+ "output": output,+ "exit_code": exit_code,+ "executed_at": executed_at,+ }++ if action == "get_init_exec":+ plugin_name = input.get("plugin_name", "")+ if not plugin_name:+ return Response(status=400, response="Missing plugin_name")++ exec_path = plugins.determine_plugin_asset_path(plugin_name, "", "", "init_exec.json")+ if exec_path and files.exists(exec_path):+ try:+ data = json.loads(files.read_file(exec_path))+ return {"ok": True, "data": data}+ except Exception:+ pass+ return {"ok": True, "data": None}++ return Response(status=400, response=f"Unknown action: {action}")\ No newline at end of filediff --git a/python/helpers/plugins.py b/python/helpers/plugins.pyindex 5d010e25a..a6393214e 100644--- a/python/helpers/plugins.py+++ b/python/helpers/plugins.py@@ -136,6 +136,7 @@ def get_enhanced_plugins_list(has_config_screen=has_config_screen,has_readme=has_readme,has_license=has_license,+ has_init_script=has_init_script,toggle_state=toggle_state,))diff --git a/webui/components/plugins/list/plugin-init-modal.html b/webui/components/plugins/list/plugin-init-modal.htmlnew file mode 100644index 000000000..182f7b7a9--- /dev/null+++ b/webui/components/plugins/list/plugin-init-modal.html@@ -0,0 +1,173 @@+<html>+<head>+ <title>Plugin Init</title>+ <script type="module">+ import { store } from "/components/plugins/list/plugin-init-store.js";+ </script>+</head>+<body>+ <div x-data>+ <template x-if="$store.pluginInitStore">+ <div x-destroy="$store.pluginInitStore.cleanup()"+ x-create="$el.closest('.modal')?.querySelector('.modal-title') && ($el.closest('.modal').querySelector('.modal-title').textContent = 'Init: ' + $store.pluginInitStore.pluginDisplayName)"+ class="plugin-init-root">++ <div class="plugin-init-idle"+ x-show="!$store.pluginInitStore.running && $store.pluginInitStore.exitCode === null && !$store.pluginInitStore.output">+ <span class="material-symbols-outlined idle-icon">terminal</span>+ <div class="idle-text">+ <span class="idle-headline">Press <strong>Run</strong> to initialize <strong x-text="$store.pluginInitStore.pluginDisplayName"></strong></span>+ <span class="idle-sub">This will execute the plugin's <code>initialize.py</code> setup script.</span>+ <template x-if="$store.pluginInitStore.lastExec">+ <span class="last-exec-info">+ Last run:+ <span x-text="new Date($store.pluginInitStore.lastExec.executed_at).toLocaleString()"></span>+ — + <span :class="$store.pluginInitStore.lastExec.exit_code === 0 ? 'exit-ok' : 'exit-err'"+ x-text="$store.pluginInitStore.lastExec.exit_code === 0 ? 'succeeded' : 'failed (exit ' + $store.pluginInitStore.lastExec.exit_code + ')'">+ </span>+ </span>+ </template>+ <template x-if="!$store.pluginInitStore.lastExec">+ <span class="last-exec-info never-run">Never initialized</span>+ </template>+ </div>+ </div>++ <div class="plugin-init-status" x-show="$store.pluginInitStore.running">+ <span class="material-symbols-outlined spin">progress_activity</span>+ <span>Running initialize.py...</span>+ </div>++ <div class="plugin-init-exit" x-show="!$store.pluginInitStore.running && $store.pluginInitStore.exitCode !== null">+ <span class="material-symbols-outlined"+ :class="$store.pluginInitStore.exitCode === 0 ? 'exit-ok' : 'exit-err'"+ x-text="$store.pluginInitStore.exitCode === 0 ? 'check_circle' : 'error'">+ </span>+ <span x-text="$store.pluginInitStore.exitCode === 0 ? 'Completed successfully (exit 0)' : 'Exited with code ' + $store.pluginInitStore.exitCode"></span>+ </div>++ <pre class="plugin-init-output"+ x-show="$store.pluginInitStore.output || $store.pluginInitStore.running"+ x-text="$store.pluginInitStore.output || ''"></pre>+ </div>+ </template>+ </div>++ <div class="modal-footer" data-modal-footer>+ <button class="btn btn-action footer-btn"+ x-data+ :disabled="$store.pluginInitStore?.running"+ @click="$store.pluginInitStore?.run()">+ <span class="icon material-symbols-outlined">play_arrow</span>+ <span x-text="$store.pluginInitStore?.exitCode !== null || $store.pluginInitStore?.output ? 'Rerun' : 'Run'">Run</span>+ </button>+ <button class="btn btn-cancel footer-btn" @click="window.closeModal?.()">Close</button>+ </div>++ <style>+ .plugin-init-root {+ display: flex;+ flex-direction: column;+ width: 100%;+ }++ .plugin-init-idle {+ display: flex;+ align-items: flex-start;+ gap: 1rem;+ padding: 2rem 1.5rem;+ color: var(--color-text-secondary);+ }++ .idle-icon {+ font-size: 2.75rem;+ opacity: 0.35;+ flex-shrink: 0;+ margin-top: 0.15rem;+ }++ .idle-text {+ display: flex;+ flex-direction: column;+ gap: 0.4rem;+ }++ .idle-headline {+ font-size: 1rem;+ color: var(--color-text);+ line-height: 1.4;+ }++ .idle-sub {+ font-size: 0.82rem;+ opacity: 0.65;+ }++ .last-exec-info {+ font-size: 0.8rem;+ opacity: 0.7;+ margin-top: 0.25rem;+ }++ .never-run {+ font-style: italic;+ }++ .plugin-init-status {+ display: flex;+ align-items: center;+ gap: 0.5rem;+ padding: 0.75rem 1.5rem;+ color: var(--color-text-secondary);+ font-size: var(--font-size-small);+ }++ .plugin-init-exit {+ display: flex;+ align-items: center;+ gap: 0.5rem;+ padding: 0.5rem 1.5rem;+ font-size: var(--font-size-small);+ }++ .exit-ok {+ color: var(--color-success, #27ae60);+ }++ .exit-err {+ color: var(--color-error, #e74c3c);+ }++ .plugin-init-output {+ font-family: "Courier New", Courier, monospace;+ font-size: 0.82rem;+ background: var(--color-bg-secondary, #1e1e1e);+ color: var(--color-text-primary);+ padding: 1rem 1.5rem;+ margin: 0;+ overflow-y: auto;+ max-height: 60vh;+ white-space: pre-wrap;+ word-break: break-word;+ border-radius: 6px;+ }++ @keyframes spin {+ from { transform: rotate(0deg); }+ to { transform: rotate(360deg); }+ }++ .spin {+ animation: spin 1s linear infinite;+ }++ .footer-btn {+ padding: 0.5rem 1.5rem;+ font-size: 0.875rem;+ font-weight: 500;+ border-radius: 0.25rem;+ }+ </style>+</body>+</html>diff --git a/webui/components/plugins/list/plugin-init-store.js b/webui/components/plugins/list/plugin-init-store.jsnew file mode 100644index 000000000..17797c898--- /dev/null+++ b/webui/components/plugins/list/plugin-init-store.js@@ -0,0 +1,80 @@+import { createStore } from "/js/AlpineStore.js";+import * as api from "/js/api.js";+import {+ store as notificationStore,+ defaultPriority,+} from "/components/notifications/notification-store.js";++const model = {+ pluginName: "",+ pluginDisplayName: "",+ output: "",+ running: false,+ exitCode: null,+ lastExec: null,++ async open(plugin) {+ if (!plugin?.name) return;+ this.pluginName = plugin.name;+ this.pluginDisplayName = plugin.display_name || plugin.name;+ this.output = "";+ this.exitCode = null;+ this.lastExec = null;+ window.openModal?.("components/plugins/list/plugin-init-modal.html");+ await this.fetchLastExec();+ },++ async fetchLastExec() {+ try {+ const response = await api.callJsonApi("plugins", {+ action: "get_init_exec",+ plugin_name: this.pluginName,+ });+ this.lastExec = response.data || null;+ } catch (e) {+ this.lastExec = null;+ }+ },++ async run() {+ if (!this.pluginName) return;+ this.output = "";+ this.exitCode = null;+ this.running = true;+ try {+ const response = await api.callJsonApi("plugins", {+ action: "run_init_script",+ plugin_name: this.pluginName,+ });+ this.output = response.output || "";+ this.exitCode = response.exit_code ?? null;+ if (response.executed_at) {+ this.lastExec = { executed_at: response.executed_at, exit_code: response.exit_code ?? null };+ }+ } catch (e) {+ this.output = e.message || String(e);+ this.exitCode = -1;+ notificationStore.frontendError(+ e.message || String(e),+ "Failed to run init script",+ 3,+ "pluginInit",+ defaultPriority,+ true,+ );+ } finally {+ this.running = false;+ }+ },++ cleanup() {+ this.pluginName = "";+ this.pluginDisplayName = "";+ this.output = "";+ this.running = false;+ this.exitCode = null;+ this.lastExec = null;+ },+};++export const store = createStore("pluginInitStore", model);diff --git a/webui/components/plugins/list/plugin-list.html b/webui/components/plugins/list/plugin-list.htmlindex d57d11aa4..da7b137bc 100644--- a/webui/components/plugins/list/plugin-list.html+++ b/webui/components/plugins/list/plugin-list.html@@ -97,6 +97,14 @@<span class="icon material-symbols-outlined">gavel</span> License</button></template>+ <template x-if="plugin.has_init_script">+ <button type="button"+ class="button"+ title="Run initializer script"+ @click="$store.pluginInitStore.open(plugin)">+ <span class="icon material-symbols-outlined">terminal</span> Init+ </button>+ </template><button type="button"class="button"title="Info"diff --git a/webui/components/plugins/list/pluginListStore.js b/webui/components/plugins/list/pluginListStore.jsindex 0a1e5dfd6..85a1d9f51 100644--- a/webui/components/plugins/list/pluginListStore.js+++ b/webui/components/plugins/list/pluginListStore.js@@ -2,6 +2,7 @@ import { createStore } from "/js/AlpineStore.js";import * as api from "/js/api.js";import "/components/plugins/plugin-settings-store.js";import "/components/plugins/toggle/plugin-toggle-store.js";+import "/components/plugins/list/plugin-init-store.js";import "/components/modals/markdown/markdown-store.js";import {store as notificationStore,