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
refactor(InteractionPrompt): enhance emptyPage function to initialize state with question options
NickFuryXXX5b9a75e7a0bb
diff --git a/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/InteractionPrompt.tsx b/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/InteractionPrompt.tsxindex 27b3e16a91..cab7327f23 100644--- a/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/InteractionPrompt.tsx+++ b/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/InteractionPrompt.tsx@@ -12,7 +12,7 @@ import remarkGfm from 'remark-gfm';import { FileText, ChevronLeft, ChevronRight } from 'lucide-react';import { useChatStore } from '../../stores';import type { AskUserQuestionPayload, Message, Question, UserAnswer, UserAnswerStatus } from '../../types';-import { buildEmptyAskUserAnswers, resolveAskUserStatus } from './interactionSubmission';+import { buildEmptyAskUserAnswers, isIncompleteAskUserPage, resolveAskUserStatus } from './interactionSubmission';import { buildQaSummaryContent, type QaSummaryData, type QaSummaryItem } from './qaSummary';/** 后端为「有选项的问题」追加的自定义输入占位项。 */@@ -37,15 +37,15 @@ interface PageState {/** 单选场景下是否选中「自定义」项 */customActive: boolean;/**- * 该页是否因「点击跳过按钮且当前无任何选择/输入」而被显式标记为跳过。- * 仅用于 answerFor 判断是否要返回空答案而非套用「默认选第一项」的兜底;+ * 该页是否被用户显式跳过,用于返回空答案并显示跳过摘要;* 一旦用户在该页重新做出任何选择/输入,会被清除。*/skippedNoSelection?: boolean;}-function emptyPage(): PageState {- return { selected: [], custom: '', customActive: false };+function emptyPage(question?: Question): PageState {+ const first = question?.options?.find(option => option.label !== CUSTOM_OPTION_LABEL);+ return { selected: first ? [first.value || first.label] : [], custom: '', customActive: false };}export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps) {@@ -65,7 +65,7 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)const [submitting, setSubmitting] = useState(false);const current = questions[page];- const st = states[page] ?? emptyPage();+ const st = states[page] ?? emptyPage(current);const isLast = page >= total - 1;const hasCustomOption = useMemo(@@ -81,9 +81,9 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)const patch = useCallback((updater: (prev: PageState) => PageState) => {- setStates((prev) => ({ ...prev, [page]: updater(prev[page] ?? emptyPage()) }));+ setStates((prev) => ({ ...prev, [page]: updater(prev[page] ?? emptyPage(current)) }));},- [page],+ [page, current],);const toggleOption = useCallback(@@ -114,14 +114,14 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)[patch],);- /** 当前页是否选了 Other 却未填写自定义内容(禁止前进/提交,避免空答触发 thinking)。 */- const incompleteCustom = st.customActive && !st.custom.trim();+ /** 显式空选或空 Other 不得前进/提交;跳过仍允许空答案。 */+ const incompleteAnswer = current ? isIncompleteAskUserPage(current, st) : false;/** 把某页状态转为一个 UserAnswer。 */const answerFor = useCallback((idx: number, overrides?: Record<number, PageState>): UserAnswer => {const q = questions[idx];- const s = overrides?.[idx] ?? states[idx] ?? emptyPage();+ const s = overrides?.[idx] ?? states[idx] ?? emptyPage(q);const customText = s.custom.trim();// 该页被显式跳过时返回空答案;交互状态由顶层 status 表达,不能再把// 内部提示文本伪装成 custom_input,也不能套用默认第一项的兜底。@@ -134,12 +134,6 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)}const answer: UserAnswer = { question: q?.question, selected_options: [...s.selected] };if (customText) answer.custom_input = customText;- // 兜底:既无选择也无输入、且不是显式跳过时,默认第一个普通选项(避免后端拿到空答案)。- // 该分支对应「点确定/下一步但什么都没选」这种情况,维持原有行为,不在本次改动范围内。- if (answer.selected_options.length === 0 && !customText) {- const first = (q?.options ?? []).find((o) => o.label !== CUSTOM_OPTION_LABEL);- if (first) answer.selected_options = [first.value || first.label];- }return answer;},[questions, states],@@ -154,7 +148,7 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)/** 组装「问题澄清」回显数据(仅展示用户真实作答内容)。 */const buildSummary = useCallback((overrides?: Record<number, PageState>): QaSummaryData => {const items: QaSummaryItem[] = questions.map((q, idx) => {- const s = overrides?.[idx] ?? states[idx] ?? emptyPage();+ const s = overrides?.[idx] ?? states[idx] ?? emptyPage(q);const customText = s.custom.trim();if (s.skippedNoSelection && s.selected.length === 0 && !customText) {return { header: q.header, question: q.question, answers: [t('interactionPrompt.skippedSummary')] };@@ -179,6 +173,16 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)discardAnswers = false,) => {if (submitting) return;+ if (!discardAnswers) {+ const incompletePage = questions.findIndex((q, idx) => {+ const state = overrides?.[idx] ?? states[idx] ?? emptyPage(q);+ return isIncompleteAskUserPage(q, state);+ });+ if (incompletePage !== -1) {+ setPage(incompletePage);+ return;+ }+ }setSubmitting(true);if (withEcho) {const sid = useChatStore.getState().activeSessionId;@@ -200,7 +204,7 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)onSubmit(pending.request_id, answers, pending.source, status);clearPending();},- [submitting, pending, buildSummary, buildAnswers, questions, addMessage, onSubmit, clearPending],+ [submitting, pending, buildSummary, buildAnswers, questions, states, addMessage, onSubmit, clearPending],);const goPrev = useCallback(() => setPage((p) => Math.max(0, p - 1)), []);@@ -213,22 +217,18 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)}, [total]);const handleNextOrConfirm = useCallback(() => {- // Other 空输入:留在当前页提示用户填写,勿提交以免进入黄色 thinking(#2330)。- if (incompleteCustom) return;+ // 空 Other 或多选空答案必须先补充内容(#2330)。+ if (incompleteAnswer) return;if (isLast) submit(true, 'answered');else goNextPage();- }, [incompleteCustom, isLast, submit, goNextPage]);+ }, [incompleteAnswer, isLast, submit, goNextPage]);const handleSkip = useCallback(() => {// 跳过:无论当前页此前是否已经选中过某个选项/填过自定义输入,点击"跳过"// 都必须视为"这道题被跳过了",绝不能把之前的选择原样带出去(bug011)。- // 显式标记为"已跳过",后续 answerFor 会生成空答案并由顶层 status 表达语义,- // 而不是套用"默认选第一项"的兜底逻辑——这一点是本次修复的核心,- // 务必确保 skippedNoSelection 分支在 answerFor 里排在"默认选第一项"兜底之前- // (见 answerFor 实现),不会被兜底逻辑抢先命中。// patch() 触发的 setStates 是异步的,若末页直接 submit 会读到旧值,// 因此显式构造覆盖态传给 submit,避免依赖尚未生效的 state。- const skippedState: PageState = { ...emptyPage(), skippedNoSelection: true };+ const skippedState: PageState = { selected: [], custom: '', customActive: false, skippedNoSelection: true };const overridden = { ...states, [page]: skippedState };patch(() => skippedState);if (isLast) submit(true, 'skipped', overridden);@@ -267,7 +267,7 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)type="button"className="ix-prompt__pager-btn"onClick={goNextPage}- disabled={page >= reached || page >= total - 1}+ disabled={incompleteAnswer || page >= reached || page >= total - 1}aria-label={t('interactionPrompt.next')}><ChevronRight size={16} strokeWidth={2} />@@ -331,6 +331,11 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)/>)}</div>+ {incompleteAnswer && (+ <p role="status" className="text-warn">+ {t(st.customActive ? 'interactionPrompt.customRequired' : 'interactionPrompt.selectionRequired')}+ </p>+ )}</div><div className="ix-prompt__foot">@@ -354,7 +359,7 @@ export function InteractionPrompt({ pending, onSubmit }: InteractionPromptProps)type="button"className="ix-btn ix-btn--primary"onClick={handleNextOrConfirm}- disabled={submitting || incompleteCustom}+ disabled={submitting || incompleteAnswer}>{isLast ? t('interactionPrompt.confirm') : t('interactionPrompt.nextStep')}</button>diff --git a/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/interactionSubmission.ts b/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/interactionSubmission.tsindex 8a762424f1..40a61e6f0d 100644--- a/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/interactionSubmission.ts+++ b/jiuwenswarm/channels/web/frontend/src/components/InteractionSlot/interactionSubmission.ts@@ -2,6 +2,21 @@ import type { Question, UserAnswer, UserAnswerStatus } from '../../types';const CUSTOM_INPUT_OPTION_LABELS = new Set(['Other', '其他']);+/** Validate explicit choices without replacing a cleared selection with a default. */+export function isIncompleteAskUserPage(+ question: Question,+ state: { selected: string[]; custom: string; customActive: boolean; skippedNoSelection?: boolean },+): boolean {+ if (state.skippedNoSelection) return false;+ if (state.customActive) return !state.custom.trim();+ return Boolean(+ question.multi_select &&+ question.options?.some(option => option.label !== 'Other') &&+ state.selected.length === 0 &&+ !state.custom.trim(),+ );+}+/** Return whether a normalized AskUser answer contains actual user input. */export function hasAskUserInput(answer: UserAnswer): boolean {const hasSelectedOption = answer.selected_options.some(option => {diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.jsonindex 2abeeb40a9..cba7884317 100644--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json@@ -2652,6 +2652,8 @@"confirm": "Confirm","customOption": "Custom","customPlaceholder": "No suitable option? Type your answer...",+ "customRequired": "Please enter a custom answer.",+ "selectionRequired": "Select at least one option or enter a custom answer.","skippedSummary": "Skipped"},"qaSummary": {diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.jsonindex a1f94333a8..060980c2d7 100644--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json@@ -2642,6 +2642,8 @@"confirm": "确定","customOption": "自定义","customPlaceholder": "没有合适的选项?输入您的答案...",+ "customRequired": "请填写自定义内容。",+ "selectionRequired": "请至少选择一项,或填写自定义内容。","skippedSummary": "已跳过"},"qaSummary": {diff --git a/jiuwenswarm/channels/web/frontend/tests/askUserSubmission.test.mjs b/jiuwenswarm/channels/web/frontend/tests/askUserSubmission.test.mjsindex cae685e644..9e0a92dea2 100644--- a/jiuwenswarm/channels/web/frontend/tests/askUserSubmission.test.mjs+++ b/jiuwenswarm/channels/web/frontend/tests/askUserSubmission.test.mjs@@ -4,10 +4,47 @@ import test from 'node:test';import {buildEmptyAskUserAnswers,hasAskUserInput,+ isIncompleteAskUserPage,resolveAskUserStatus,shouldUnlockPendingApproval,} from '../node_modules/.cache/ask-user-submission/components/InteractionSlot/interactionSubmission.js';+const choiceQuestion = { question: 'Choose?', options: [{ label: 'A' }, { label: 'B' }, { label: 'Other' }], multi_select: true };+const emptyChoice = { selected: [], custom: '', customActive: false };++test('clearing the default multi-selection requires an answer without restoring the default', () => {+ const state = { ...emptyChoice, selected: ['A'] };+ assert.equal(isIncompleteAskUserPage(choiceQuestion, state), false);+ state.selected = [];+ assert.equal(isIncompleteAskUserPage(choiceQuestion, state), true);+ assert.deepEqual(state.selected, []);+ assert.equal(isIncompleteAskUserPage(choiceQuestion, { ...state, custom: 'My answer' }), false);+ assert.equal(isIncompleteAskUserPage(choiceQuestion, { ...state, custom: ' ' }), true);+});++test('empty or whitespace Other requires input for single-choice questions', () => {+ const question = { ...choiceQuestion, multi_select: false };+ for (const custom of ['', ' ']) {+ assert.equal(isIncompleteAskUserPage(question, { ...emptyChoice, customActive: true, custom }), true);+ }+ assert.equal(isIncompleteAskUserPage(question, { ...emptyChoice, customActive: true, custom: 'Answer' }), false);+});++test('explicit skip permits an empty answer and does not select a default', () => {+ const skipped = { ...emptyChoice, skippedNoSelection: true };+ assert.equal(isIncompleteAskUserPage(choiceQuestion, skipped), false);+ assert.deepEqual(skipped.selected, []);+});++test('submission validation detects an incomplete earlier page', () => {+ const states = [{ ...emptyChoice, customActive: true }, { ...emptyChoice, selected: ['B'] }];+ assert.equal(states.findIndex(state => isIncompleteAskUserPage(choiceQuestion, state)), 0);+});++test('free-input questions retain their existing empty-input behavior', () => {+ assert.equal(isIncompleteAskUserPage({ ...choiceQuestion, options: [] }, emptyChoice), false);+});+test('preserves explicit answered even when answers are empty', () => {assert.equal(resolveAskUserStatus('answered', []), 'answered');});