Files

90 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

// Agent optimize page: one action select, fields per action, JSON out.
(function () {
"use strict";
var ACTIONS = {
score: { help: "Local autorater scores a whole conversation.",
fields: [["metric", "select:task_success,safety"], ["transcript", "area:user: ...\nagent: ..."]] },
simulate: { help: "A simulated user talks to an agent built from your instruction, then both are scored.",
fields: [["instruction", "area"], ["starting_prompt", "text"], ["plan", "area"], ["max_turns", "text:5"]] },
cluster: { help: "Group failures (one per line) by root cause.",
fields: [["failures", "area"], ["max_clusters", "text:5"]] },
suggest: { help: "Cluster failures and suggest a refined system instruction.",
fields: [["instruction", "area"], ["failures", "area"]] },
evaluate: { help: "Managed evaluateInstances (safety, exact_match, trajectory).",
fields: [["kind", "select:safety,exact_match,trajectory"], ["prediction", "area"], ["reference", "text"],
["predicted", "text:tool names, comma separated"]] },
scenarios: { help: "Generate simulated user scenarios.",
fields: [["agent_instruction", "area"], ["instruction", "text"], ["count", "text:5"]] },
runs: { help: "List evaluation runs (needs OAuth).", fields: [["limit", "text:10"]] },
traces: { help: "Cloud Trace summary (needs OAuth).", fields: [["limit", "text:20"], ["filter", "text"]] },
synthetic: { help: "generateSyntheticData: paste the request JSON (needs OAuth).",
fields: [["request", "area:{ ... }"]] },
"operations/list": { help: "Operations of an online evaluator (needs OAuth).",
fields: [["evaluator", "text:projects/P/locations/L/onlineEvaluators/ID"], ["limit", "text:20"]] },
"operations/get": { help: "One online evaluator operation.", fields: [["name", "text"]] },
"operations/wait": { help: "Server-side wait, 1..60 seconds.",
fields: [["name", "text"], ["timeout_seconds", "text:30"]] },
"logs/write": { help: "Write one log entry. Dry run unless confirm is write.",
fields: [["log_id", "text:agent-check"], ["text", "text"], ["confirm", "text"]] },
logs: { help: "Cloud Logging entries (needs OAuth).", fields: [["limit", "text:20"], ["trace_id", "text"], ["filter", "text"]] }
};
var $ = function (id) { return document.getElementById(id); };
var sel = $("o-action"), fields = $("o-fields"), out = $("o-out"), err = $("o-error"), help = $("o-help");
var MAX_REPLY_CHARS = 100000;
Object.keys(ACTIONS).forEach(function (k) {
var o = document.createElement("option");
o.value = k; o.textContent = k; sel.appendChild(o);
});
function build() {
var a = ACTIONS[sel.value];
help.textContent = a.help;
fields.innerHTML = "";
a.fields.forEach(function (f) {
var label = document.createElement("label");
label.textContent = f[0];
var kind = f[1].split(":")[0], arg = f[1].slice(kind.length + 1), el;
if (kind === "select") {
el = document.createElement("select");
arg.split(",").forEach(function (v) {
var o = document.createElement("option"); o.value = v; o.textContent = v; el.appendChild(o);
});
} else if (kind === "area") {
el = document.createElement("textarea"); el.rows = 5; el.placeholder = arg;
} else {
el = document.createElement("input"); el.type = "text";
if (/^\d+$/.test(arg)) el.value = arg; else el.placeholder = arg;
}
el.id = "of-" + f[0]; el.style.width = "100%"; el.style.boxSizing = "border-box";
label.appendChild(el);
fields.appendChild(label);
});
}
function send() {
err.textContent = ""; out.textContent = "";
var body = {};
ACTIONS[sel.value].fields.forEach(function (f) {
var v = $("of-" + f[0]).value;
if (v !== "") body[f[0]] = v;
});
$("o-send").disabled = true;
fetch("/agent/optimize/" + sel.value, {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body)
}).then(function (r) {
return r.text().then(function (t) { return { ok: r.ok, text: t }; });
}).then(function (r) {
var text = r.text.slice(0, MAX_REPLY_CHARS);
if (!r.ok) { err.textContent = text; return; }
try { text = JSON.stringify(JSON.parse(text), null, 2); } catch (e) { /* show raw */ }
out.textContent = text;
}).catch(function (e) { err.textContent = String(e); })
.then(function () { $("o-send").disabled = false; });
}
sel.addEventListener("change", build);
$("o-send").addEventListener("click", send);
build();
})();