File size: 7,713 Bytes
430354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { createStore } from "/js/AlpineStore.js";
import * as api from "/js/api.js";
import { renderSafeMarkdown } from "/js/safe-markdown.js";
import { store as pluginSettingsStore } from "/components/plugins/plugin-settings-store.js";
import { store as pluginExecuteStore } from "/components/plugins/list/plugin-execute-store.js";
import { store as fileBrowserStore } from "/components/modals/file-browser/file-browser-store.js";
import { store as markdownModalStore } from "/components/modals/markdown/markdown-store.js";
import { callJsExtensions } from "/js/extensions.js";
import {
  store as notificationStore,
  defaultPriority,
} from "/components/notifications/notification-store.js";

const MODAL_PATH = "components/plugins/list/plugin-list.html";

// define the model object holding data and functions
const model = {
  loading: false,
  plugins: [],
  selectedPlugin: null,
  activeTab: "custom",
  readmeContent: "",
  readmeLoading: false,
  readmeError: "",

  async open(tab = "custom") {
    await this.setTab(tab);
    window.openModal?.(MODAL_PATH);
  },

  async init() {
    this.loading = false;
    // If a tab is already selected (e.g. via open()), use it. 
    // Otherwise default to custom -> builtin fallback.
    if (this.activeTab && this.activeTab !== "custom") {
      await this.setTab(this.activeTab);
    } else {
      await this.setTab("custom");
      if (this.plugins.length === 0) {
        await this.setTab("builtin");
      }
    }
  },

  async loadPluginList(filter) {
    this.loading = true;
    this.selectedPlugin = null;
    try {
      const response = await api.callJsonApi("plugins_list", { filter });
      this.plugins = Array.isArray(response.plugins) ? response.plugins : [];
      void callJsExtensions("plugins_list_after_load", {
        filter: filter ? { ...filter } : null,
        plugins: this.plugins,
        store: this,
      });
    } catch (e) {
      this.plugins = [];
      showErrorNotification(e, "Failed to load plugins list");
    } finally {
      this.loading = false;
    }
  },

  async setTab(tab) {
    if (tab === "pluginHub") {
      this.activeTab = "pluginHub";
      this.loading = false;
      return;
    }

    this.activeTab = tab === "builtin" ? "builtin" : "custom";
    const filter =
      this.activeTab === "builtin"
        ? { builtin: true, custom: false, search: "" }
        : { builtin: false, custom: true, search: "" };
    await this.loadPluginList(filter);
  },

  async refresh() {
    if (this.activeTab === "pluginHub") {
      return;
    }
    await this.setTab(this.activeTab);
  },

  openPlugin(plugin) {
    if (!plugin?.name || !plugin?.has_main_screen) return;
    window.openModal?.(`/plugins/${plugin.name}/webui/main.html`);
  },

  openPluginExecute(plugin) {
    if (!plugin?.name || !plugin?.has_execute_script) return;
    pluginExecuteStore.open(plugin);
  },

  canOpenPluginConfig(plugin) {
    return !!(
      plugin?.has_config_screen ||
      plugin?.per_project_config ||
      plugin?.per_agent_config
    );
  },

  async openPluginConfig(pluginOrName) {
    const pluginName =
      typeof pluginOrName === "string" ? pluginOrName : pluginOrName?.name;
    if (!pluginName) return;

    if (
      typeof pluginOrName === "object" &&
      !this.canOpenPluginConfig(pluginOrName)
    )
      return;

    try {
      if (!pluginSettingsStore?.openConfig) {
        throw new Error("Plugin settings store is unavailable.");
      }
      await pluginSettingsStore.openConfig(pluginName);
    } catch (e) {
      showErrorNotification(e, "Failed to open plugin config");
    }
  },

  isPluginEnabled(plugin) {
    if (plugin?.always_enabled) return true;
    return plugin?.toggle_state === "enabled";
  },

  toggleStatusLabel(plugin) {
    return this.isPluginEnabled(plugin) ? "ON" : "OFF";
  },

  async updateToggle(plugin, enabled) {
    if (!plugin?.name) return;
    if (plugin.always_enabled) return;

    const nextEnabled = !!enabled;
    const previousState = plugin.toggle_state;
    plugin.toggle_state = nextEnabled ? "enabled" : "disabled";

    this.loading = true;
    try {
        const response = await api.callJsonApi("plugins", {
            action: "toggle_plugin",
            plugin_name: plugin.name,
            enabled: nextEnabled,
            project_name: "",
            agent_profile: "",
            clear_overrides: false,
        });
        if (response?.error) throw new Error(response.error);
        await this.refresh();
    } catch (e) {
        plugin.toggle_state = previousState;
        showErrorNotification(e, "Failed to toggle plugin");
        this.loading = false;
    }
  },

  async openPluginDoc(plugin, doc) {
    try {
      const response = await api.callJsonApi("plugins", {
        action: "get_doc",
        plugin_name: plugin.name,
        doc,
      });
      if (response?.error) throw new Error(response.error);
      if (!markdownModalStore?.open) throw new Error("Markdown modal store unavailable.");
      markdownModalStore.open(response.filename, response.content);
      window.openModal?.("components/modals/markdown/markdown-modal.html");
    } catch (e) {
      showErrorNotification(e, "Failed to open document");
    }
  },

  async loadPluginReadme(plugin) {
    this.readmeLoading = true;
    this.readmeContent = "";
    this.readmeError = "";
    try {
      const response = await api.callJsonApi("plugins", {
        action: "get_doc",
        plugin_name: plugin.name,
        doc: "readme",
      });
      if (response?.error) throw new Error(response.error);
      this.readmeContent = renderSafeMarkdown(response.content || "");
    } catch (e) {
      const error = e instanceof Error ? e : new Error(String(e));
      this.readmeError = error.message || "Failed to load README";
    } finally {
      this.readmeLoading = false;
    }
  },

  openPluginInfo(plugin) {
    if (!plugin) return;
    this.selectedPlugin = plugin;
    this.readmeContent = "";
    this.readmeLoading = false;
    this.readmeError = "";
    if (plugin.has_readme) {
      void this.loadPluginReadme(plugin);
    }
    window.openModal?.("components/plugins/plugin-info.html");
  },

  async openPluginFolder(plugin) {
    if (!plugin?.path) return;
    await fileBrowserStore.open(plugin.path);
  },

  async openPluginHub(plugin) {
    const pluginKey = (plugin?.pluginHub?.key || "").trim();
    if (!pluginKey) return;
    const { store: pluginInstallStore } = await import(
      "/plugins/_plugin_installer/webui/pluginInstallStore.js"
    );
    await pluginInstallStore.openPluginHubDetailByKey(pluginKey);
  },

  async deletePlugin(plugin) {
    if (!plugin?.name) return;

    if (!plugin.is_custom) {
      showErrorNotification(
        new Error("Only custom plugins can be deleted from this modal."),
        "Delete blocked",
      );
      return;
    }

    try {
      const response = await api.callJsonApi("plugins", {
        action: "delete_plugin",
        plugin_name: plugin.name,
      });
      if (response?.error) {
        throw new Error(response.error);
      }
      if (window.toastFrontendSuccess) {
        window.toastFrontendSuccess("Plugin deleted", "Plugins");
      }
      await this.refresh();
    } catch (e) {
      showErrorNotification(e, "Failed to delete plugin");
    }
  },
};

function showErrorNotification(error, heading) {
    const text = error.message || error.text || JSON.stringify(error);
  notificationStore.frontendError(
    text,
    heading,
    3,
    "pluginsList",
    defaultPriority,
    true,
  );
}

// convert it to alpine store
const store = createStore("pluginListStore", model);

// export for use in other files
export { store };