File size: 7,965 Bytes
7d4338a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { marked } from "/vendor/marked/marked.esm.js";
import { createStore } from "/js/AlpineStore.js";
import * as api from "/js/api.js";
import { openModal } from "/js/modals.js";
import { toastFrontendError } from "/components/notifications/notification-store.js";

const BASE = "/plugins/_plugin_scan/webui";

/** @type {{ ratings: Record<string, {icon:string,label:string}>, checks: Record<string, {label:string,detail:string,criteria:Record<string,string>}> } | null} */
let _config = null;
/** @type {string|null} */
let _templateCache = null;

async function fetchText(url, label) {
  const response = await fetch(url);
  if (!response.ok) {
    const body = await response.text().catch(() => "");
    throw new Error(`Failed to load ${label}: ${response.status} ${response.statusText}${body ? ` - ${body}` : ""}`);
  }
  return response.text();
}

async function fetchJson(url, label) {
  const response = await fetch(url);
  if (!response.ok) {
    const body = await response.text().catch(() => "");
    throw new Error(`Failed to load ${label}: ${response.status} ${response.statusText}${body ? ` - ${body}` : ""}`);
  }
  return response.json();
}

async function loadConfig() {
  if (_config) return _config;
  try {
    _config = await fetchJson(`${BASE}/plugin-scan-checks.json`, "scan checks");
    return _config;
  } catch (error) {
    _config = null;
    throw error;
  }
}

async function loadTemplate() {
  if (_templateCache) return _templateCache;
  try {
    _templateCache = await fetchText(`${BASE}/plugin-scan-prompt.md`, "scan prompt template");
    return _templateCache;
  } catch (error) {
    _templateCache = null;
    throw error;
  }
}

function formatCriteria(ratings, criteria) {
  return Object.entries(criteria)
    .map(([level, desc]) => `- ${ratings[level].icon} ${desc}`)
    .join("\n");
}

function formatStatusLegend(ratings) {
  return Object.entries(ratings)
    .map(([, r]) => `- ${r.icon} **${r.label}**`)
    .join("\n");
}

function formatRatingIcons(ratings) {
  return Object.values(ratings).map((r) => r.icon).join("/");
}
let _pollGen = 0;
const POLL_INTERVAL = 2000;
const MAX_POLL_MS = 10 * 60 * 1000;
const SCAN_TITLE = "Plugin Scanner";

function formatErrorMessage(error) {
  return error instanceof Error ? error.message : String(error);
}

export const store = createStore("pluginScan", {
  gitUrl: "",
  checks: {},
  checksMeta: {},
  prompt: "",
  output: "",
  scanning: false,
  scanCtxId: "",

  get renderedOutput() {
    return this.output ? marked.parse(this.output, { breaks: true }) : "";
  },

  async init() {
    const cfg = await loadConfig();
    if (!cfg) return;
    this.checksMeta = cfg.checks;
    const initial = {};
    for (const key of Object.keys(cfg.checks)) initial[key] = true;
    this.checks = initial;
  },

  async onOpen(url) {
    this.output = "";
    this.scanning = false;
    if (url) this.gitUrl = url;
    const cfg = await loadConfig();
    if (cfg && Object.keys(this.checks).length === 0) {
      this.checksMeta = cfg.checks;
      const initial = {};
      for (const key of Object.keys(cfg.checks)) initial[key] = true;
      this.checks = initial;
    }
    this.buildPrompt();
  },

  cleanup() {
    _pollGen++;
  },

  async openModal(url) {
    this.gitUrl = url || "";
    await openModal("/plugins/_plugin_scan/webui/plugin-scan.html");
  },

  async buildPrompt() {
    try {
      const [cfg, template] = await Promise.all([loadConfig(), loadTemplate()]);
      if (!cfg) return;
      const { ratings, checks } = cfg;

      let text = template;
      text = text.replace(/\{\{GIT_URL\}\}/g, this.gitUrl || "<paste git URL here>");

      const selected = Object.entries(this.checks)
        .filter(([, v]) => v)
        .map(([k]) => checks[k])
        .filter(Boolean);

      text = text.replace(
        /\{\{SELECTED_CHECKS\}\}/g,
        selected.length ? selected.map((c) => `- ${c.label}`).join("\n") : "- (no checks selected)",
      );
      text = text.replace(
        /\{\{CHECK_DETAILS\}\}/g,
        selected.length
          ? selected.map((c) => `**${c.label}**: ${c.detail}\n${formatCriteria(ratings, c.criteria)}`).join("\n\n")
          : "(no checks selected)",
      );
      text = text.replace(/\{\{STATUS_LEGEND\}\}/g, formatStatusLegend(ratings));
      text = text.replace(/\{\{RATING_ICONS\}\}/g, formatRatingIcons(ratings));
      text = text.replace(/\{\{RATING_PASS\}\}/g, ratings.pass.icon);
      text = text.replace(/\{\{RATING_WARNING\}\}/g, ratings.warning.icon);
      text = text.replace(/\{\{RATING_FAIL\}\}/g, ratings.fail.icon);

      this.prompt = text;
    } catch (/** @type {any} */ e) {
      console.error("Failed to build prompt:", e);
      void toastFrontendError(`Failed to build prompt: ${formatErrorMessage(e)}`, SCAN_TITLE);
    }
  },

  async copyPrompt() {
    try {
      await navigator.clipboard.writeText(this.prompt);
    } catch {
      void toastFrontendError("Failed to copy the scan prompt", SCAN_TITLE);
    }
  },

  /** Create a fresh context, log the prompt into it, and start the scan immediately. */
  async runScan() {
    if (!this.gitUrl.trim()) {
      void toastFrontendError("Please enter a Git URL", SCAN_TITLE);
      return;
    }

    await this.buildPrompt();
    const capturedPrompt = this.prompt;
    const gen = ++_pollGen;
    this.output = "";

    let ctxId;
    try {
      const resp = await api.callJsonApi("/chat_create", {});
      if (!resp.ok) throw new Error("Failed to create chat context");
      ctxId = resp.ctxid;
    } catch (/** @type {any} */ e) {
      void toastFrontendError(`Scan failed: ${formatErrorMessage(e)}`, SCAN_TITLE);
      return;
    }
    this.scanCtxId = ctxId;

    try {
      await api.callJsonApi("/plugins/_plugin_scan/plugin_scan_queue", { context: ctxId, text: capturedPrompt });
    } catch { /* best-effort */ }
    this.scanning = true;
    this._runNext(gen, ctxId, capturedPrompt);
  },

  /** @param {number} gen  @param {string} ctxId  @param {string} prompt */
  async _runNext(gen, ctxId, prompt) {
    try {
      await api.callJsonApi("/plugins/_plugin_scan/plugin_scan_start", { text: prompt, context: ctxId });
      await this._pollLoop(gen, ctxId);
    } catch (/** @type {any} */ e) {
      if (gen === _pollGen) {
        void toastFrontendError(`Scan failed: ${formatErrorMessage(e)}`, SCAN_TITLE);
        this.scanning = false;
      }
    }
  },

  /** @param {number} gen  @param {string} ctxId */
  async _pollLoop(gen, ctxId) {
    let started = false;
    const deadline = Date.now() + MAX_POLL_MS;
    while (true) {
      if (Date.now() >= deadline) {
        if (gen === _pollGen) {
          this.scanning = false;
          void toastFrontendError("Scan timed out while waiting for the agent response", SCAN_TITLE);
          console.error(`Scan poll timed out for context ${ctxId}`);
        }
        return;
      }
      await new Promise((r) => setTimeout(r, POLL_INTERVAL));
      try {
        const snap = await api.callJsonApi("/poll", {
          context: ctxId, log_from: 0, notifications_from: 0,
          timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
        });

        if (gen === _pollGen && snap.logs?.length) {
          const last = snap.logs.filter((/** @type {any} */ l) => l.type === "response" && l.no > 0).pop();
          if (last) this.output = last.content || "";
        }

        if (snap.log_progress_active) started = true;
        if (started && !snap.log_progress_active) {
          if (gen === _pollGen) this.scanning = false;
          return;
        }
        if (snap.deselect_chat) return;
      } catch (/** @type {any} */ e) {
        if (gen === _pollGen) console.error("Poll error:", e);
      }
    }
  },

  openChatInNewWindow() {
    if (!this.scanCtxId) return;
    const url = new URL(window.location.href);
    url.searchParams.set("ctxid", this.scanCtxId);
    window.open(url.toString(), "_blank");
  },
});