Files
moloapp/example/assets/agent_armor.js
T

88 lines
3.3 KiB
JavaScript
Raw Normal View History

// Agent security page: one action selector, one form, JSON output.
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var action = $("a-action"), tmpl = $("a-template"), text = $("a-text");
var scope = $("a-scope"), severity = $("a-severity"), run = $("a-run");
var errorEl = $("a-error"), out = $("a-out"), config = $("a-config");
// Which inputs each action uses.
var uses = {
"sanitize/prompt": ["a-template", "a-text"],
"sanitize/response": ["a-template", "a-text"],
"guard": ["a-template", "a-text"],
"templates/list": [],
"templates/get": ["a-template"],
"templates/create": ["a-template"],
"templates/delete": ["a-template"],
"floor/get": ["a-scope"],
"findings": ["a-scope", "a-severity", "a-family"],
"dashboard": ["a-scope", "a-severity", "a-family"],
"scc/availability": ["a-scope"],
"scc/configs/list": ["a-scope", "a-kind"]
};
function sync() {
var shown = uses[action.value] || [];
["a-template", "a-text", "a-scope", "a-severity", "a-family", "a-kind"].forEach(function (id) {
$(id).parentNode.style.display = shown.indexOf(id) >= 0 ? "" : "none";
});
}
function body() {
var b = {};
var a = action.value;
if (tmpl.value) b.template_id = tmpl.value;
if (text.value) b.text = text.value;
if (scope.value) b.scope = scope.value;
if (severity.value) b.severity = severity.value;
if ($("a-family").value) b.family = $("a-family").value;
if (a === "scc/configs/list") { b.kind = $("a-kind").value; b.parent = scope.value; delete b.scope; }
if (a === "templates/create") {
b.pi = "MEDIUM_AND_ABOVE";
b.malicious_uri = "true";
b.rai = "HATE_SPEECH:HIGH,DANGEROUS:HIGH,HARASSMENT:HIGH,SEXUALLY_EXPLICIT:HIGH";
}
if (a === "templates/delete") {
if (!window.confirm("Delete template " + tmpl.value + "?")) return null;
b.confirm = "delete";
}
return b;
}
run.addEventListener("click", function () {
var b = body();
if (b === null) return;
errorEl.textContent = "";
out.textContent = "";
run.disabled = true;
fetch("/agent/armor/" + action.value, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(b)
}).then(function (r) {
return r.text().then(function (t) { return { ok: r.ok, text: t }; });
}).then(function (r) {
if (!r.ok) {
var msg = r.text;
try { var e = JSON.parse(r.text); msg = e.message || e.error || r.text; } catch (x) {}
errorEl.textContent = msg;
return;
}
try { out.textContent = JSON.stringify(JSON.parse(r.text), null, 2); }
catch (x) { out.textContent = r.text; }
}).catch(function (e) {
errorEl.textContent = String(e);
}).then(function () { run.disabled = false; });
});
action.addEventListener("change", sync);
sync();
fetch("/agent/armor/status").then(function (r) { return r.json(); }).then(function (s) {
config.textContent = (s.configured ? "Credential present. " : "No Google credential. ") +
"Model Armor needs an OAuth access token (GOOGLE_ACCESS_TOKEN); API keys are refused." +
(s.project_set ? "" : " Set GOOGLE_CLOUD_PROJECT.");
}).catch(function () { config.textContent = "Status unavailable."; });
})();