Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(ask_user): 选择 Other 时展开自定义输入框,避免输入丢失 (#1983)

stevenchenhanwen20064656d46f53e2
diff --git a/jiuwenswarm/channels/web/frontend/src/components/ChatPanel/InlineQuestionCard.tsx b/jiuwenswarm/channels/web/frontend/src/components/ChatPanel/InlineQuestionCard.tsx
index 0513ac3672..8fea4d2b3a 100644
--- a/jiuwenswarm/channels/web/frontend/src/components/ChatPanel/InlineQuestionCard.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/components/ChatPanel/InlineQuestionCard.tsx
@@ -19,6 +19,12 @@ interface InlineQuestionCardProps {
onSubmit: (requestId: string, answers: UserAnswer[], source?: string) => void;
}
+// 后端会给带选项的问题末尾追加一个「自定义输入」选项(interrupt_helpers._build_multi_questions)。
+// 兼容两种形态:带哨兵值 __other__(新)或仅有英文文案 Other 且无 value(旧)。
+const OTHER_VALUE = '__other__';
+const isOtherOption = (option: QuestionOption): boolean =>
+ option.value === OTHER_VALUE || (!option.value && option.label === 'Other');
+
function ApprovalQuestionContent({ question }: { question: Question }) {
return (
<ReactMarkdown remarkPlugins={[remarkGfm]}>
@@ -31,11 +37,13 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
const { t } = useTranslation();
const { pendingQuestion, setPendingQuestion } = useChatStore();
const [selections, setSelections] = useState<Map<number, string>>(new Map());
+ const [customInputs, setCustomInputs] = useState<Map<number, string>>(new Map());
const [submitted, setSubmitted] = useState(false);
const requestId = pendingQuestion?.request_id;
useEffect(() => {
setSelections(new Map());
+ setCustomInputs(new Map());
setSubmitted(false);
}, [requestId]);
@@ -43,19 +51,31 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
const allAnswered = useMemo(() => {
if (!pendingQuestion) return false;
- return pendingQuestion.questions.every((_, idx) => selections.has(idx));
- }, [pendingQuestion, selections]);
+ return pendingQuestion.questions.every((_, idx) => {
+ if (!selections.has(idx)) return false;
+ // 选了 Other 必须填了内容才算完成
+ if (selections.get(idx) === OTHER_VALUE) {
+ return (customInputs.get(idx) || '').trim().length > 0;
+ }
+ return true;
+ });
+ }, [pendingQuestion, selections, customInputs]);
const buildAnswers = useCallback(
(selMap: Map<number, string>): UserAnswer[] => {
return (pendingQuestion?.questions ?? []).map((q, idx) => {
const sel = selMap.get(idx);
+ // Other:清空 selected_options,把用户输入放进 custom_input,
+ // 命中后端 interface.py 的 `elif custom_input` 分支。
+ if (sel === OTHER_VALUE) {
+ return { selected_options: [], custom_input: (customInputs.get(idx) || '').trim() };
+ }
if (sel) return { selected_options: [sel] };
const firstOption = q.options[0];
return { selected_options: firstOption ? [firstOption.value || firstOption.label] : [] };
});
},
- [pendingQuestion]
+ [pendingQuestion, customInputs]
);
const doSubmit = useCallback(
@@ -72,11 +92,15 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
(questionIndex: number, option: QuestionOption) => {
if (submitted) return;
- const value = option.value || option.label;
+ const isOther = isOtherOption(option);
+ const value = isOther ? OTHER_VALUE : (option.value || option.label);
const next = new Map(selections);
next.set(questionIndex, value);
setSelections(next);
+ // Other:不立即提交,展开输入框,等用户填完再手动提交。
+ if (isOther) return;
+
if (!isBatch) {
doSubmit(next);
}
@@ -84,6 +108,23 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
[submitted, selections, isBatch, doSubmit]
);
+ const handleCustomInputChange = useCallback(
+ (questionIndex: number, text: string) => {
+ setCustomInputs((prev) => new Map(prev).set(questionIndex, text));
+ },
+ []
+ );
+
+ // 单问题模式下,Other 输入完成后由此提交
+ const handleSubmitOther = useCallback(
+ (questionIndex: number) => {
+ if (submitted) return;
+ if ((customInputs.get(questionIndex) || '').trim().length === 0) return;
+ doSubmit(selections);
+ },
+ [submitted, customInputs, selections, doSubmit]
+ );
+
const handleAcceptAll = useCallback(() => {
if (!pendingQuestion || submitted) return;
const all = new Map<number, string>();
@@ -223,7 +264,7 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
{/* 选项按钮 */}
<div className="px-4 pb-3 flex flex-col gap-2">
{question.options.map((option) => {
- const optionValue = option.value || option.label;
+ const optionValue = isOtherOption(option) ? OTHER_VALUE : (option.value || option.label);
const isAccept = option.label === t('chatUi.inlineQuestion.accept')
|| option.label === t('chatUi.inlineQuestion.allowOnce')
|| option.label === '本次允许';
@@ -307,6 +348,53 @@ export function InlineQuestionCard({ onSubmit }: InlineQuestionCardProps) {
</button>
);
})}
+
+ {/* 选中 Other 后展开自定义输入框:填完再提交 */}
+ {selectedValue === OTHER_VALUE && (
+ <div className="mt-1 flex flex-col gap-2">
+ <textarea
+ autoFocus
+ value={customInputs.get(qIndex) || ''}
+ onChange={(e) => handleCustomInputChange(qIndex, e.target.value)}
+ placeholder={t('userQuestion.customPlaceholder')}
+ disabled={submitted}
+ rows={2}
+ className="w-full px-3 py-2 text-sm rounded-lg resize-none focus:outline-none"
+ style={{
+ backgroundColor: 'var(--bg-elevated)',
+ border: '1px solid var(--border)',
+ color: 'var(--text)',
+ }}
+ onFocus={(e) => { e.currentTarget.style.borderColor = 'var(--accent)'; }}
+ onBlur={(e) => { e.currentTarget.style.borderColor = 'var(--border)'; }}
+ onKeyDown={(e) => {
+ if (!isBatch && e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
+ e.preventDefault();
+ handleSubmitOther(qIndex);
+ }
+ }}
+ />
+ {!isBatch && (() => {
+ const hasText = (customInputs.get(qIndex) || '').trim().length > 0;
+ return (
+ <button
+ onClick={() => handleSubmitOther(qIndex)}
+ disabled={submitted || !hasText}
+ className="self-end px-4 py-1.5 text-xs font-medium text-white rounded-lg transition-opacity"
+ style={{
+ background: hasText
+ ? 'linear-gradient(135deg, var(--accent), var(--accent-2))'
+ : 'var(--border)',
+ opacity: hasText ? 1 : 0.5,
+ cursor: hasText ? 'pointer' : 'not-allowed',
+ }}
+ >
+ {t('chatUi.inlineQuestion.submit')}
+ </button>
+ );
+ })()}
+ </div>
+ )}
</div>
</div>
);