Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Make custom answers mutually exclusive in agent single-choice questions

Daniel825ce664241f
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scss
index 1bdce9700a..ebc368d88e 100644
--- a/app/src/assets/scss/business/_ai_agent.scss
+++ b/app/src/assets/scss/business/_ai_agent.scss
@@ -1043,6 +1043,19 @@
line-height: 1.5;
}
+.agent-chat__question-custom-option {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 0 8px;
+ margin-top: 8px;
+
+ .agent-chat__question-custom {
+ min-width: 0;
+ margin-top: 0;
+ }
+}
+
.agent-chat__question-custom {
width: 100%;
box-sizing: border-box;
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.ts
index c8ba4b7d02..f7cacee598 100644
--- a/app/src/layout/dock/agent/AgentChat.ts
+++ b/app/src/layout/dock/agent/AgentChat.ts
@@ -2099,14 +2099,19 @@ export class AgentChat extends Model {
});
// 恢复已提交的选中状态(answers 中存的是选项 value)。
if (entry.answers && entry.answers.length > 0) {
- el.querySelectorAll("input[type=radio], input[type=checkbox]").forEach((inp) => {
+ el.querySelectorAll(".agent-chat__question-option input").forEach((inp) => {
(inp as HTMLInputElement).checked = entry.answers!.includes((inp as HTMLInputElement).value);
});
const customInput = el.querySelector(".agent-chat__question-custom") as HTMLInputElement | null;
if (customInput) {
- const customAnswer = entry.answers.find(a => el.querySelector('input[value="' + a + '"]') === null);
+ const optionInputs = Array.from(el.querySelectorAll(".agent-chat__question-option input")) as HTMLInputElement[];
+ const customAnswer = entry.answers.find(a => !optionInputs.some(input => input.value === a));
if (customAnswer) {
customInput.value = customAnswer;
+ const customRadio = customInput.parentElement.querySelector(".agent-chat__question-custom-radio") as HTMLInputElement;
+ if (customRadio) {
+ customRadio.checked = true;
+ }
}
}
}
@@ -4168,21 +4173,18 @@ export class AgentChat extends Model {
status: "pending",
});
- el.querySelectorAll(".agent-chat__question-option").forEach((option) => {
- const input = option.querySelector("input") as HTMLInputElement;
- if (!input) return;
- let wasChecked = false;
- option.addEventListener("mousedown", () => {
- wasChecked = input.checked;
- });
- option.addEventListener("click", (e) => {
- if (el.classList.contains("agent-chat__msg--confirmed")) {
- return;
- }
- if (input.type === "radio" && wasChecked) {
- e.preventDefault();
- input.checked = false;
+ el.querySelectorAll(".agent-chat__question-custom-option").forEach((option) => {
+ const radio = option.querySelector(".agent-chat__question-custom-radio") as HTMLInputElement;
+ const input = option.querySelector(".agent-chat__question-custom") as HTMLInputElement;
+ const selectCustom = () => {
+ if (!input.disabled) {
+ radio.checked = true;
}
+ };
+ input.addEventListener("focus", selectCustom);
+ input.addEventListener("input", selectCustom);
+ radio.addEventListener("click", () => {
+ input.focus();
});
});
@@ -4193,13 +4195,14 @@ export class AgentChat extends Model {
for (let qi = 0; qi < rawQuestions.length; qi++) {
const optEl = el.querySelector('.agent-chat__question-options[data-qi="' + qi + '"]');
if (optEl) {
- const selected = optEl.querySelectorAll("input:checked") as NodeListOf<HTMLInputElement>;
+ const selected = optEl.querySelectorAll(".agent-chat__question-option input:checked") as NodeListOf<HTMLInputElement>;
for (let si = 0; si < selected.length; si++) {
answers.push(selected[si].value);
}
}
const customInput = el.querySelector('.agent-chat__question-custom[data-qi="' + qi + '"]') as HTMLInputElement;
- if (customInput && customInput.value.trim()) {
+ const customRadio = optEl?.querySelector(".agent-chat__question-custom-radio") as HTMLInputElement;
+ if (customInput && customInput.value.trim() && (!customRadio || customRadio.checked)) {
answers.push(customInput.value.trim());
}
}
diff --git a/app/src/layout/dock/agent/AgentMessageRenderer.ts b/app/src/layout/dock/agent/AgentMessageRenderer.ts
index 6035098221..05afd6ffaa 100644
--- a/app/src/layout/dock/agent/AgentMessageRenderer.ts
+++ b/app/src/layout/dock/agent/AgentMessageRenderer.ts
@@ -109,7 +109,15 @@ export const renderQuestionCardHTML = (rawQuestions: Array<Record<string, unknow
html += "</label>";
}
if (custom) {
- html += '<input class="agent-chat__question-custom" placeholder="' + (L.agentQuestionCustom || "Type your own answer...") + '" data-qi="' + qi + '">';
+ const customLabel = escapeHtml(L.agentQuestionCustom || "Type your own answer...");
+ if (!multiple) {
+ html += '<div class="agent-chat__question-custom-option">' +
+ '<input type="radio" name="' + inputName + '" class="agent-chat__question-custom-radio" aria-label="' + customLabel + '">';
+ }
+ html += '<input class="agent-chat__question-custom" aria-label="' + customLabel + '" placeholder="' + customLabel + '" data-qi="' + qi + '">';
+ if (!multiple) {
+ html += "</div>";
+ }
}
html += "</div></div>";
}