|
|
@@ -22,10 +22,9 @@ let elapsedTimer = 0;
|
|
|
let hasBackendRoundStats = false;
|
|
|
let pendingEnabledTools = null;
|
|
|
let availableTools = [];
|
|
|
-let generatedChatPrompt = "";
|
|
|
|
|
|
bindClick("#add-system-prompt", () => {
|
|
|
- systemPrompts.append(createSystemPrompt(""));
|
|
|
+ systemPrompts.append(createSystemPrompt({ content: "" }));
|
|
|
});
|
|
|
|
|
|
bindClick("#add-pre-message", () => {
|
|
|
@@ -55,6 +54,7 @@ chatForm.addEventListener("submit", (event) => {
|
|
|
runDebugSession();
|
|
|
});
|
|
|
|
|
|
+initializePromptList();
|
|
|
refreshPromptSetSelector();
|
|
|
loadTools();
|
|
|
|
|
|
@@ -91,7 +91,7 @@ function renderTools(tools) {
|
|
|
if (!tools.length) {
|
|
|
toolList.textContent = "No tools available";
|
|
|
updateToolCount();
|
|
|
- updateChatSystemPromptDefault(tools);
|
|
|
+ updateEventPromptItem(tools);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -110,7 +110,7 @@ function renderTools(tools) {
|
|
|
checkbox.checked = true;
|
|
|
checkbox.addEventListener("change", () => {
|
|
|
updateToolCount();
|
|
|
- updateChatSystemPromptDefault(availableTools);
|
|
|
+ updateEventPromptItem(availableTools);
|
|
|
});
|
|
|
|
|
|
label.append(checkbox, ` ${tool.name}`);
|
|
|
@@ -132,7 +132,7 @@ function renderTools(tools) {
|
|
|
pendingEnabledTools = null;
|
|
|
}
|
|
|
updateToolCount();
|
|
|
- updateChatSystemPromptDefault(tools);
|
|
|
+ updateEventPromptItem(tools);
|
|
|
}
|
|
|
|
|
|
function formatRequiredParameters(tool) {
|
|
|
@@ -147,7 +147,7 @@ function setAllTools(checked) {
|
|
|
checkbox.checked = checked;
|
|
|
});
|
|
|
updateToolCount();
|
|
|
- updateChatSystemPromptDefault(availableTools);
|
|
|
+ updateEventPromptItem(availableTools);
|
|
|
}
|
|
|
|
|
|
function updateToolCount() {
|
|
|
@@ -208,8 +208,9 @@ function deletePromptSet() {
|
|
|
|
|
|
function buildPromptSet() {
|
|
|
return {
|
|
|
- system_prompts: [...document.querySelectorAll(".system-prompt")]
|
|
|
- .map((input) => input.value.trim())
|
|
|
+ prompt_items: buildPromptItems(),
|
|
|
+ system_prompts: buildCustomPromptItems()
|
|
|
+ .map((item) => item.content)
|
|
|
.filter(Boolean),
|
|
|
pre_messages: [...document.querySelectorAll(".pre-message")]
|
|
|
.map((row) => ({
|
|
|
@@ -221,7 +222,7 @@ function buildPromptSet() {
|
|
|
model: document.querySelector("#model").value.trim(),
|
|
|
temperature: Number(document.querySelector("#temperature").value),
|
|
|
max_tokens: Number(document.querySelector("#max-tokens").value),
|
|
|
- system_prompt: document.querySelector("#chat-system-prompt").value.trim(),
|
|
|
+ system_prompt: "",
|
|
|
extra_body: buildExtraBody("chat"),
|
|
|
},
|
|
|
event_agent: {
|
|
|
@@ -237,14 +238,13 @@ function buildPromptSet() {
|
|
|
}
|
|
|
|
|
|
function restorePromptSet(promptSet) {
|
|
|
- restoreSystemPrompts(promptSet.system_prompts || []);
|
|
|
+ restoreSystemPrompts(promptSet.prompt_items || promptSet.system_prompts || []);
|
|
|
restorePreMessages(promptSet.pre_messages || []);
|
|
|
|
|
|
const chatAgent = promptSet.chat_agent || {};
|
|
|
document.querySelector("#model").value = chatAgent.model || "";
|
|
|
document.querySelector("#temperature").value = chatAgent.temperature ?? 0.2;
|
|
|
document.querySelector("#max-tokens").value = chatAgent.max_tokens ?? 800;
|
|
|
- document.querySelector("#chat-system-prompt").value = chatAgent.system_prompt || "";
|
|
|
restoreExtraBody("chat", chatAgent.extra_body);
|
|
|
|
|
|
const eventAgent = promptSet.event_agent || {};
|
|
|
@@ -256,7 +256,7 @@ function restorePromptSet(promptSet) {
|
|
|
document.querySelector("#max-event-loops").value = eventAgent.max_event_loops ?? 1;
|
|
|
const enabledTools = eventAgent.enabled_tools || [];
|
|
|
pendingEnabledTools = applySelectedTools(enabledTools) ? null : enabledTools;
|
|
|
- updateChatSystemPromptDefault(availableTools);
|
|
|
+ updateEventPromptItem(availableTools);
|
|
|
}
|
|
|
|
|
|
function buildExtraBody(prefix) {
|
|
|
@@ -286,10 +286,11 @@ function restoreExtraBody(prefix, extraBody = {}) {
|
|
|
|
|
|
function restoreSystemPrompts(prompts) {
|
|
|
systemPrompts.textContent = "";
|
|
|
- const values = prompts.length ? prompts : [""];
|
|
|
- values.forEach((prompt) => {
|
|
|
+ const promptItems = normalizePromptItems(prompts);
|
|
|
+ promptItems.forEach((prompt) => {
|
|
|
systemPrompts.append(createSystemPrompt(prompt));
|
|
|
});
|
|
|
+ ensureEventPromptItem();
|
|
|
}
|
|
|
|
|
|
function restorePreMessages(messages) {
|
|
|
@@ -299,12 +300,59 @@ function restorePreMessages(messages) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-function createSystemPrompt(value) {
|
|
|
+function createSystemPrompt(prompt) {
|
|
|
+ const promptItem = normalizePromptItem(prompt);
|
|
|
+ const item = document.createElement("div");
|
|
|
+ item.className = promptItem.prompt_kind === "event_agent_system"
|
|
|
+ ? "prompt-item event-prompt"
|
|
|
+ : "prompt-item";
|
|
|
+ item.dataset.promptKind = promptItem.prompt_kind;
|
|
|
+ item.draggable = true;
|
|
|
+
|
|
|
+ const head = document.createElement("div");
|
|
|
+ head.className = "prompt-item-head";
|
|
|
+
|
|
|
+ const title = document.createElement("span");
|
|
|
+ title.textContent = promptItem.prompt_kind === "event_agent_system"
|
|
|
+ ? "EventAgent System Prompt"
|
|
|
+ : "Custom Prompt";
|
|
|
+
|
|
|
+ const actions = document.createElement("div");
|
|
|
+ actions.className = "prompt-actions";
|
|
|
+
|
|
|
+ const moveUp = document.createElement("button");
|
|
|
+ moveUp.className = "move-prompt-up";
|
|
|
+ moveUp.type = "button";
|
|
|
+ moveUp.textContent = "Up";
|
|
|
+ moveUp.addEventListener("click", () => movePromptItem(item, -1));
|
|
|
+
|
|
|
+ const moveDown = document.createElement("button");
|
|
|
+ moveDown.className = "move-prompt-down";
|
|
|
+ moveDown.type = "button";
|
|
|
+ moveDown.textContent = "Down";
|
|
|
+ moveDown.addEventListener("click", () => movePromptItem(item, 1));
|
|
|
+
|
|
|
+ actions.append(moveUp, moveDown);
|
|
|
+ if (promptItem.prompt_kind !== "event_agent_system") {
|
|
|
+ const deleteButton = document.createElement("button");
|
|
|
+ deleteButton.className = "delete-system-prompt";
|
|
|
+ deleteButton.type = "button";
|
|
|
+ deleteButton.textContent = "Delete";
|
|
|
+ deleteButton.addEventListener("click", () => item.remove());
|
|
|
+ actions.append(deleteButton);
|
|
|
+ }
|
|
|
+
|
|
|
+ head.append(title, actions);
|
|
|
+
|
|
|
const textarea = document.createElement("textarea");
|
|
|
textarea.className = "system-prompt";
|
|
|
- textarea.rows = 4;
|
|
|
- textarea.value = value;
|
|
|
- return textarea;
|
|
|
+ textarea.rows = promptItem.prompt_kind === "event_agent_system" ? 7 : 4;
|
|
|
+ textarea.value = promptItem.content;
|
|
|
+ textarea.readOnly = promptItem.prompt_kind === "event_agent_system";
|
|
|
+
|
|
|
+ item.append(head, textarea);
|
|
|
+ bindPromptDrag(item);
|
|
|
+ return item;
|
|
|
}
|
|
|
|
|
|
function createPreMessage(message) {
|
|
|
@@ -325,10 +373,66 @@ function applySelectedTools(toolNames) {
|
|
|
checkbox.checked = selected.has(checkbox.value);
|
|
|
});
|
|
|
updateToolCount();
|
|
|
- updateChatSystemPromptDefault(availableTools);
|
|
|
+ updateEventPromptItem(availableTools);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+function initializePromptList() {
|
|
|
+ [...systemPrompts.querySelectorAll(".prompt-item")].forEach((item) => {
|
|
|
+ bindPromptItemControls(item);
|
|
|
+ bindPromptDrag(item);
|
|
|
+ });
|
|
|
+ ensureEventPromptItem();
|
|
|
+}
|
|
|
+
|
|
|
+function buildPromptItems() {
|
|
|
+ return [...systemPrompts.querySelectorAll(".prompt-item")]
|
|
|
+ .map((item) => ({
|
|
|
+ prompt_kind: item.dataset.promptKind || "custom",
|
|
|
+ content: item.querySelector(".system-prompt").value.trim(),
|
|
|
+ }))
|
|
|
+ .filter((item) => item.prompt_kind === "event_agent_system" || item.content);
|
|
|
+}
|
|
|
+
|
|
|
+function buildCustomPromptItems() {
|
|
|
+ return buildPromptItems().filter((item) => item.prompt_kind !== "event_agent_system");
|
|
|
+}
|
|
|
+
|
|
|
+function collectSystemPrompts() {
|
|
|
+ return buildPromptItems()
|
|
|
+ .map((item) => item.content)
|
|
|
+ .filter(Boolean);
|
|
|
+}
|
|
|
+
|
|
|
+function normalizePromptItems(prompts) {
|
|
|
+ const items = Array.isArray(prompts) ? prompts : [];
|
|
|
+ const normalized = items
|
|
|
+ .map((prompt) => normalizePromptItem(prompt))
|
|
|
+ .filter((prompt) => prompt.prompt_kind === "event_agent_system" || prompt.content);
|
|
|
+ if (!normalized.some((prompt) => prompt.prompt_kind === "event_agent_system")) {
|
|
|
+ normalized.push({ prompt_kind: "event_agent_system", content: "" });
|
|
|
+ }
|
|
|
+ if (!normalized.some((prompt) => prompt.prompt_kind === "custom")) {
|
|
|
+ normalized.unshift({ prompt_kind: "custom", content: "" });
|
|
|
+ }
|
|
|
+ return normalized;
|
|
|
+}
|
|
|
+
|
|
|
+function normalizePromptItem(prompt) {
|
|
|
+ if (prompt && typeof prompt === "object" && !Array.isArray(prompt)) {
|
|
|
+ return {
|
|
|
+ prompt_kind: prompt.prompt_kind === "event_agent_system"
|
|
|
+ ? "event_agent_system"
|
|
|
+ : "custom",
|
|
|
+ content: prompt.content || "",
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ prompt_kind: "custom",
|
|
|
+ content: prompt || "",
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
function buildAvailableEventsPrompt(tools) {
|
|
|
const eventLines = tools.map((tool) => `- ${tool.name}: ${tool.description || "No description"}`);
|
|
|
if (!eventLines.length) {
|
|
|
@@ -346,24 +450,70 @@ function buildAvailableEventsPrompt(tools) {
|
|
|
].join("\n");
|
|
|
}
|
|
|
|
|
|
-function updateChatSystemPromptDefault(tools) {
|
|
|
+function updateEventPromptItem(tools) {
|
|
|
+ const eventPrompt = ensureEventPromptItem();
|
|
|
const selected = new Set(selectedTools());
|
|
|
const prompt = buildAvailableEventsPrompt(
|
|
|
tools.filter((tool) => selected.has(tool.name)),
|
|
|
);
|
|
|
- const systemPrompt = document.querySelector("#chat-system-prompt");
|
|
|
- const preview = document.querySelector("#chat-event-prompt-preview");
|
|
|
- if (preview) {
|
|
|
- preview.value = prompt;
|
|
|
+ eventPrompt.querySelector(".system-prompt").value = prompt;
|
|
|
+}
|
|
|
+
|
|
|
+function ensureEventPromptItem() {
|
|
|
+ const existing = systemPrompts.querySelector('[data-prompt-kind="event_agent_system"]');
|
|
|
+ if (existing) {
|
|
|
+ return existing;
|
|
|
}
|
|
|
- if (!systemPrompt) {
|
|
|
- generatedChatPrompt = prompt;
|
|
|
- return;
|
|
|
+
|
|
|
+ const item = createSystemPrompt({
|
|
|
+ prompt_kind: "event_agent_system",
|
|
|
+ content: "",
|
|
|
+ });
|
|
|
+ systemPrompts.append(item);
|
|
|
+ return item;
|
|
|
+}
|
|
|
+
|
|
|
+function bindPromptItemControls(item) {
|
|
|
+ const moveUp = item.querySelector(".move-prompt-up");
|
|
|
+ if (moveUp) {
|
|
|
+ moveUp.addEventListener("click", () => movePromptItem(item, -1));
|
|
|
+ }
|
|
|
+ const moveDown = item.querySelector(".move-prompt-down");
|
|
|
+ if (moveDown) {
|
|
|
+ moveDown.addEventListener("click", () => movePromptItem(item, 1));
|
|
|
+ }
|
|
|
+ const deleteButton = item.querySelector(".delete-system-prompt");
|
|
|
+ if (deleteButton) {
|
|
|
+ deleteButton.addEventListener("click", () => item.remove());
|
|
|
}
|
|
|
- if (!systemPrompt.value.trim() || systemPrompt.value === generatedChatPrompt) {
|
|
|
- systemPrompt.value = prompt;
|
|
|
+}
|
|
|
+
|
|
|
+function movePromptItem(item, direction) {
|
|
|
+ if (direction < 0 && item.previousElementSibling) {
|
|
|
+ systemPrompts.insertBefore(item, item.previousElementSibling);
|
|
|
+ }
|
|
|
+ if (direction > 0 && item.nextElementSibling) {
|
|
|
+ systemPrompts.insertBefore(item.nextElementSibling, item);
|
|
|
}
|
|
|
- generatedChatPrompt = prompt;
|
|
|
+}
|
|
|
+
|
|
|
+function bindPromptDrag(item) {
|
|
|
+ item.addEventListener("dragstart", () => {
|
|
|
+ item.classList.add("dragging");
|
|
|
+ });
|
|
|
+ item.addEventListener("dragend", () => {
|
|
|
+ item.classList.remove("dragging");
|
|
|
+ });
|
|
|
+ item.addEventListener("dragover", (event) => {
|
|
|
+ event.preventDefault();
|
|
|
+ const dragging = systemPrompts.querySelector(".prompt-item.dragging");
|
|
|
+ if (!dragging || dragging === item) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const rect = item.getBoundingClientRect();
|
|
|
+ const afterMidpoint = event.clientY > rect.top + rect.height / 2;
|
|
|
+ systemPrompts.insertBefore(dragging, afterMidpoint ? item.nextSibling : item);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
function refreshPromptSetSelector(selectedName = "") {
|
|
|
@@ -453,9 +603,7 @@ function runDebugSession() {
|
|
|
function buildRequest(submittedMessage = userMessage.value) {
|
|
|
return {
|
|
|
user_message: submittedMessage,
|
|
|
- system_prompts: [...document.querySelectorAll(".system-prompt")]
|
|
|
- .map((input) => input.value.trim())
|
|
|
- .filter(Boolean),
|
|
|
+ system_prompts: collectSystemPrompts(),
|
|
|
pre_messages: [...document.querySelectorAll(".pre-message")]
|
|
|
.map((row) => ({
|
|
|
role: row.querySelector(".pre-role").value,
|
|
|
@@ -466,7 +614,7 @@ function buildRequest(submittedMessage = userMessage.value) {
|
|
|
model: document.querySelector("#model").value.trim(),
|
|
|
temperature: Number(document.querySelector("#temperature").value),
|
|
|
max_tokens: Number(document.querySelector("#max-tokens").value),
|
|
|
- system_prompt: document.querySelector("#chat-system-prompt").value.trim(),
|
|
|
+ system_prompt: "",
|
|
|
extra_body: buildExtraBody("chat"),
|
|
|
},
|
|
|
event_agent: {
|