Workspace - Source Excerpt 08
Summary
This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/spiralist-workspace/assets/js/workspace.js so readers can inspect the evidence without opening the full source file.
**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/assets/js/workspace.js
runAction(async () => {
const response = await apiFetch("prompts/" + promptId + "/improve", {
method: "POST",
body: JSON.stringify({}),
});
const suggestion = response.suggestion || {};
const structured = suggestion.structured_prompt || {};
const lines = [];
if (suggestion.title) {
lines.push(t("fieldTitle", "Title") + ": " + suggestion.title);
}
if (suggestion.summary) {
lines.push(t("fieldSummary", "Summary") + ": " + suggestion.summary);
}
["instruction", "context", "constraints", "examples", "output_format"].forEach((key) => {
if (structured[key]) {
lines.push(key.replace("_", " ") + ":\n" + structured[key]);
}
});
if (suggestion.notes) {
lines.push(t("fieldNotes", "Notes") + ":\n" + suggestion.notes);
}
if (target) {
target.textContent = lines.length ? lines.join("\n\n") : (response.output_text || t("promptImproveNoSuggestion", "No worksheet returned."));
}
if (copyButton) {
copyButton.hidden = !(target && String(target.textContent || "").trim());
}
}, {
context: "prompt-improve",
fallback: t("promptImproveFailed", "Prompt improvement failed."),
onError: (error, message) => {
if (target) {
target.textContent = message;
}
if (copyButton) {
copyButton.hidden = true;
}
},
});
});
});
};
const initTestForms = () => {
document.querySelectorAll("[data-test-form]").forEach((form) => {
form.addEventListener("submit", (event) => {
event.preventDefault();
setResult(form, t("testSaving", "Saving test..."));
runFormAction(form, async () => {
const promptId = parseInt(form.getAttribute("data-prompt-id") || "0", 10);
await apiFetch("prompts/" + promptId + "/tests", {
method: "POST",
body: JSON.stringify(formDataToObject(form)),
});
setResult(form, t("testSaved", "Test saved."));
window.location.reload();
}, t("testSaveFailed", "Test save failed."), "prompt-test-save");
});
});
document.querySelectorAll("[data-run-test]").forEach((button) => {
button.addEventListener("click", () => {
const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
const testId = parseInt(button.getAttribute("data-test-id") || "0", 10);
const target = button.closest("[data-test-item]")?.querySelector("[data-test-result]");
if (target) {
target.textContent = t("testRunning", "Running test...");
}
runAction(async () => {
const response = await apiFetch("prompts/" + promptId + "/tests/" + testId + "/run", {
method: "POST",
body: JSON.stringify({}),
});
const evaluation = response.evaluation || {};
if (target) {
target.textContent = (evaluation.status || t("testFinishedStatus", "finished")) + ": " + (evaluation.details || "");
}
}, {
context: "prompt-test-run",
fallback: t("testRunFailed", "Test run failed."),
onError: (error, message) => {
if (target) {
target.textContent = message;
}
},
});
});
});
};
const initVersionActions = () => {
document.querySelectorAll("[data-rollback-version]").forEach((button) => {
button.addEventListener("click", () => {
const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
const version = parseInt(button.getAttribute("data-version") || "0", 10);
const target = button.closest("[data-version-item]")?.querySelector("[data-version-result]");
if (target) {
target.textContent = t("rollbackRunning", "Rolling back...");
}
runAction(async () => {
await apiFetch("prompts/" + promptId + "/versions/" + version + "/rollback", {
method: "POST",
body: JSON.stringify({}),
});
window.location.reload();
}, {
context: "prompt-version-rollback",
fallback: t("rollbackFailed", "Rollback failed."),
onError: (error, message) => {
if (target) {
target.textContent = message;
}
},
});
});
});
};
const initAdminPromptTests = () => {
const button = document.querySelector("[data-workspace-admin-run-prompt-tests]");
const limitField = document.querySelector("[data-workspace-admin-test-limit]");
const resultField = document.querySelector("[data-workspace-admin-prompt-tests-result]");
if (!button || !resultField) {
return;
}
button.addEventListener("click", () => {
const limit = Math.max(1, Math.min(50, parseInt(limitField?.value || "20", 10) || 20));
resultField.textContent = t("promptTestsRunning", "Running prompt tests...");
button.disabled = true;
runAction(async () => {
const response = await apiFetch("admin/prompt-tests/run", {
method: "POST",
body: JSON.stringify({ limit }),
});
const summary = response.summary || {};
const results = Array.isArray(response.results) ? response.results : [];
resultField.textContent = "";
const summaryLine = document.createElement("strong");
summaryLine.textContent = formatText(
"promptTestsFinished",
"Finished: {passed} passed, {failed} failed, {errors} errors, {total} total.",
{
passed: summary.passed || 0,
failed: summary.failed || 0,
errors: summary.errors || 0,
total: summary.total || 0,
}
);
resultField.appendChild(summaryLine);
if (results.length) {
const list = document.createElement("ul");
list.className = "spiralist-workspace-admin-test-results";
results.slice(0, 10).forEach((item) => {
const row = document.createElement("li");
row.textContent = "[" + (item.status || t("unknownValue", "unknown")) + "] " +
(item.prompt_title || formatText("promptNumber", "Prompt #{id}", { id: item.prompt_id || "" })) +
" " + formatText("testNumber", "test #{id}", { id: item.test_id || "" }) +
(item.details ? ": " + item.details : "");
list.appendChild(row);
});