Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): preserve numeric zero in chat input forms (#43243)

ZengLiangliang4e068f62c072
diff --git a/web/app/components/base/chat/chat-with-history/__tests__/chat-wrapper.spec.tsx b/web/app/components/base/chat/chat-with-history/__tests__/chat-wrapper.spec.tsx
index 9225ec98a4d..c65284461d6 100644
--- a/web/app/components/base/chat/chat-with-history/__tests__/chat-wrapper.spec.tsx
+++ b/web/app/components/base/chat/chat-with-history/__tests__/chat-wrapper.spec.tsx
@@ -384,6 +384,27 @@ describe('ChatWrapper', () => {
expect(container).not.toBeInTheDocument()
})
+ it('should not disable input when a required number field is zero', () => {
+ vi.mocked(useChatWithHistoryContext).mockReturnValue({
+ ...defaultContextValue,
+ inputsForms: [
+ { variable: 'count', label: 'Count', type: InputVarType.number, required: true },
+ ],
+ newConversationInputs: { count: 0 },
+ newConversationInputsRef: {
+ current: { count: 0 },
+ } as ChatWithHistoryContextValue['newConversationInputsRef'],
+ currentConversationId: '',
+ })
+
+ render(<ChatWrapper />)
+ const textboxes = screen.getAllByRole('textbox')
+ const chatInput = textboxes[textboxes.length - 1]
+ const container = getChatInputDisabledSurface(chatInput!)
+
+ expect(container).not.toBeInTheDocument()
+ })
+
it('should disable input when file is uploading', () => {
vi.mocked(useChatWithHistoryContext).mockReturnValue({
...defaultContextValue,
diff --git a/web/app/components/base/chat/chat-with-history/__tests__/hooks.spec.tsx b/web/app/components/base/chat/chat-with-history/__tests__/hooks.spec.tsx
index f47064aca93..a614c42fbc6 100644
--- a/web/app/components/base/chat/chat-with-history/__tests__/hooks.spec.tsx
+++ b/web/app/components/base/chat/chat-with-history/__tests__/hooks.spec.tsx
@@ -1187,6 +1187,36 @@ describe('useChatWithHistory', () => {
expect(callback).not.toHaveBeenCalled()
})
+ it('should invoke callback when a required number input is zero', async () => {
+ mockStoreState.appParams = {
+ user_input_form: [
+ {
+ number: {
+ variable: 'required_number',
+ label: 'Required Number',
+ required: true,
+ default: 0,
+ },
+ },
+ ],
+ } as unknown as ChatConfig
+ mockFetchConversations.mockResolvedValue(createConversationData())
+ mockFetchChatList.mockResolvedValue({ data: [] })
+
+ const { result } = await renderWithClient(() => useChatWithHistory())
+
+ await waitFor(() => {
+ expect(result!.current.newConversationInputsRef.current.required_number).toBe(0)
+ })
+ const callback = vi.fn()
+
+ act(() => {
+ result!.current.handleStartChat(callback)
+ })
+
+ expect(callback).toHaveBeenCalledTimes(1)
+ })
+
it('should invoke callback when allInputsHidden is true regardless of required fields', async () => {
// Arrange
mockStoreState.appParams = {
@@ -1907,6 +1937,37 @@ describe('useChatWithHistory', () => {
})
})
+ describe('inputsForms number numeric zero', () => {
+ it('should preserve zero from URL params and seed it into new conversation inputs', async () => {
+ const { getRawInputsFromUrlParams } = await import('../../utils')
+ vi.mocked(getRawInputsFromUrlParams).mockResolvedValue({ num_var: '0' })
+
+ mockStoreState.appParams = {
+ user_input_form: [
+ {
+ number: {
+ variable: 'num_var',
+ label: 'Number',
+ required: false,
+ default: 10,
+ },
+ },
+ ],
+ } as unknown as ChatConfig
+ mockFetchConversations.mockResolvedValue(createConversationData())
+ mockFetchChatList.mockResolvedValue({ data: [] })
+
+ const { result } = await renderWithClient(() => useChatWithHistory())
+
+ await waitFor(() => {
+ expect(result!.current.inputsForms[0].default).toBe(0)
+ expect(result!.current.newConversationInputsRef.current.num_var).toBe(0)
+ })
+
+ vi.mocked(getRawInputsFromUrlParams).mockResolvedValue({})
+ })
+ })
+
// Scenario: showNewConversationItemInList manual state management.
describe('setShowNewConversationItemInList', () => {
it('should not prepend empty item when showNewConversationItemInList is false', async () => {
diff --git a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx
index 3c91652fd22..7e8cc81c392 100644
--- a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx
+++ b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx
@@ -119,7 +119,9 @@ const ChatWrapper = () => {
if (fileIsUploading) return
- if (!inputsFormValue?.[variable]) hasEmptyInput = label as string
+ const inputValue = inputsFormValue?.[variable]
+ if (inputValue === undefined || inputValue === null || inputValue === '')
+ hasEmptyInput = label as string
if (
(type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
diff --git a/web/app/components/base/chat/chat-with-history/hooks.tsx b/web/app/components/base/chat/chat-with-history/hooks.tsx
index 812dd3155e8..2755b2ed66b 100644
--- a/web/app/components/base/chat/chat-with-history/hooks.tsx
+++ b/web/app/components/base/chat/chat-with-history/hooks.tsx
@@ -288,10 +288,16 @@ export const useChatWithHistory = (installedAppInfo?: InstalledAppResponse) => {
}
}
if (item.number) {
- const convertedNumber = Number(initInputs[item.number.variable])
+ const rawNumber = initInputs[item.number.variable]
+ const convertedNumber =
+ rawNumber === undefined || rawNumber === null || rawNumber === ''
+ ? Number.NaN
+ : Number(rawNumber)
return {
...item.number,
- default: convertedNumber || item.default || item.number.default,
+ default: Number.isNaN(convertedNumber)
+ ? (item.default ?? item.number.default)
+ : convertedNumber,
type: 'number',
}
}
@@ -356,7 +362,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledAppResponse) => {
useEffect(() => {
const conversationInputs: Record<string, any> = {}
inputsForms.forEach((item: any) => {
- conversationInputs[item.variable] = item.default || null
+ conversationInputs[item.variable] = item.default === '' ? null : (item.default ?? null)
})
handleNewConversationInputsChange(conversationInputs)
}, [handleNewConversationInputsChange, inputsForms])
@@ -432,7 +438,8 @@ export const useChatWithHistory = (installedAppInfo?: InstalledAppResponse) => {
requiredVars.forEach(({ variable, label, type }) => {
if (hasEmptyInput) return
if (fileIsUploading) return
- if (!newConversationInputsRef.current[variable] && !silent)
+ const inputValue = newConversationInputsRef.current[variable]
+ if ((inputValue === undefined || inputValue === null || inputValue === '') && !silent)
hasEmptyInput = label as string
if (
(type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
@@ -491,7 +498,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledAppResponse) => {
handleChangeConversation('')
const conversationInputs: Record<string, any> = {}
inputsForms.forEach((item: any) => {
- conversationInputs[item.variable] = item.default || null
+ conversationInputs[item.variable] = item.default === '' ? null : (item.default ?? null)
})
handleNewConversationInputsChange(conversationInputs)
setClearChatList(true)
diff --git a/web/app/components/base/chat/chat-with-history/inputs-form/__tests__/content.spec.tsx b/web/app/components/base/chat/chat-with-history/inputs-form/__tests__/content.spec.tsx
index 0dde75ddb09..94932f32ad7 100644
--- a/web/app/components/base/chat/chat-with-history/inputs-form/__tests__/content.spec.tsx
+++ b/web/app/components/base/chat/chat-with-history/inputs-form/__tests__/content.spec.tsx
@@ -266,6 +266,17 @@ describe('InputsFormContent', () => {
)
})
+ it('renders numeric zero as zero', () => {
+ const context = createMockContext({
+ inputsForms: [{ variable: 'num', type: InputVarType.number, label: 'Num' }],
+ currentConversationInputs: { num: 0 },
+ })
+
+ renderWithContext(<InputsFormContent />, context)
+
+ expect(screen.getByRole('spinbutton', { name: 'Num' })).toHaveValue(0)
+ })
+
it('renders and handles paragraph input updates', async () => {
const user = userEvent.setup()
const context = createMockContext({
diff --git a/web/app/components/base/chat/chat-with-history/inputs-form/content.tsx b/web/app/components/base/chat/chat-with-history/inputs-form/content.tsx
index cd63613b2d5..cb75b738646 100644
--- a/web/app/components/base/chat/chat-with-history/inputs-form/content.tsx
+++ b/web/app/components/base/chat/chat-with-history/inputs-form/content.tsx
@@ -90,7 +90,7 @@ const InputsFormContent = ({ showTip }: Props) => {
<Input
aria-labelledby={`${baseId}-${form.variable}-label`}
type="number"
- value={inputsFormValue?.[form.variable] || ''}
+ value={inputsFormValue?.[form.variable] ?? ''}
onValueChange={(value) => handleFormChange(form.variable, value)}
placeholder={form.label}
/>
diff --git a/web/app/components/base/chat/embedded-chatbot/__tests__/chat-wrapper.spec.tsx b/web/app/components/base/chat/embedded-chatbot/__tests__/chat-wrapper.spec.tsx
index 01f7ab6a23e..c5d053f517e 100644
--- a/web/app/components/base/chat/embedded-chatbot/__tests__/chat-wrapper.spec.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/__tests__/chat-wrapper.spec.tsx
@@ -440,6 +440,21 @@ describe('EmbeddedChatbot chat-wrapper', () => {
expect(screen.getByRole('button', { name: 'send message' })).toBeDisabled()
})
+ it('should not disable sending when a required number input is zero', () => {
+ vi.mocked(useEmbeddedChatbotContext).mockReturnValue(
+ createContextValue({
+ inputsForms: [
+ { variable: 'count', label: 'Count', required: true, type: InputVarType.number },
+ ],
+ newConversationInputsRef: { current: { count: 0 } },
+ }),
+ )
+
+ render(<ChatWrapper />)
+
+ expect(screen.getByRole('button', { name: 'send message' })).not.toBeDisabled()
+ })
+
it('should show the user avatar fallback when avatar data is provided', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(
createContextValue({
diff --git a/web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsx b/web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsx
index e53b51a08f6..a33a74669f3 100644
--- a/web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/__tests__/hooks.spec.tsx
@@ -520,6 +520,23 @@ describe('useEmbeddedChatbot', () => {
expect(forms.find((f: InputForm) => f.variable === 'f1')?.type).toBe('file')
expect(forms.find((f: InputForm) => f.variable === 'j1')?.type).toBe('json_object')
})
+
+ it('should preserve numeric zero supplied via URL params', async () => {
+ mockGetProcessedInputsFromUrlParams.mockResolvedValue({ n1: '0' })
+ mockStoreState.appParams = {
+ user_input_form: [{ number: { variable: 'n1', default: 10 } }],
+ } as unknown as ChatConfig
+
+ const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
+
+ await waitFor(() => {
+ expect(result.current.inputsForms).toHaveLength(1)
+ })
+ expect(result.current.inputsForms[0].default).toBe(0)
+ await waitFor(() => {
+ expect(result.current.newConversationInputsRef.current.n1).toBe(0)
+ })
+ })
})
// Scenario: checkInputsRequired validates empty fields and pending multi-file uploads
@@ -590,6 +607,26 @@ describe('useEmbeddedChatbot', () => {
expect(callback).toHaveBeenCalled()
})
+
+ it('should accept numeric zero for a required number input', async () => {
+ mockStoreState.appParams = {
+ user_input_form: [{ number: { variable: 'n1', required: true, label: 'N1' } }],
+ } as unknown as ChatConfig
+
+ const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
+
+ act(() => {
+ result.current.handleNewConversationInputsChange({ n1: 0 })
+ })
+ const onStart = vi.fn()
+ act(() => {
+ ;(
+ result.current as unknown as { handleStartChat: (cb?: () => void) => void }
+ ).handleStartChat(onStart)
+ })
+
+ expect(onStart).toHaveBeenCalled()
+ })
})
// Scenario: handlers (New Conversation, Change Conversation, Feedback)
diff --git a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx b/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx
index 1a8bf16e50b..7b90bcea322 100644
--- a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx
@@ -121,7 +121,9 @@ const ChatWrapper = () => {
if (fileIsUploading) return
- if (!inputsFormValue?.[variable]) hasEmptyInput = label as string
+ const inputValue = inputsFormValue?.[variable]
+ if (inputValue === undefined || inputValue === null || inputValue === '')
+ hasEmptyInput = label as string
if (
(type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
diff --git a/web/app/components/base/chat/embedded-chatbot/hooks.tsx b/web/app/components/base/chat/embedded-chatbot/hooks.tsx
index 16714b5840b..3a3eb573612 100644
--- a/web/app/components/base/chat/embedded-chatbot/hooks.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/hooks.tsx
@@ -195,10 +195,16 @@ export const useEmbeddedChatbot = (appSourceType: AppSourceType, tryAppId?: stri
}
}
if (item.number) {
- const convertedNumber = Number(initInputs[item.number.variable])
+ const rawNumber = initInputs[item.number.variable]
+ const convertedNumber =
+ rawNumber === undefined || rawNumber === null || rawNumber === ''
+ ? Number.NaN
+ : Number(rawNumber)
return {
...item.number,
- default: convertedNumber || item.default || item.number.default,
+ default: Number.isNaN(convertedNumber)
+ ? (item.default ?? item.number.default)
+ : convertedNumber,
type: 'number',
}
}
@@ -264,7 +270,7 @@ export const useEmbeddedChatbot = (appSourceType: AppSourceType, tryAppId?: stri
useEffect(() => {
const conversationInputs: Record<string, InputValueTypes> = {}
inputsForms.forEach((item) => {
- conversationInputs[item.variable] = item.default || null
+ conversationInputs[item.variable] = item.default === '' ? null : (item.default ?? null)
})
handleNewConversationInputsChange(conversationInputs)
}, [handleNewConversationInputsChange, inputsForms])
@@ -339,7 +345,8 @@ export const useEmbeddedChatbot = (appSourceType: AppSourceType, tryAppId?: stri
requiredVars.forEach(({ variable, label, type }) => {
if (hasEmptyInput) return
if (fileIsUploading) return
- if (!newConversationInputsRef.current[variable] && !silent)
+ const inputValue = newConversationInputsRef.current[variable]
+ if ((inputValue === undefined || inputValue === null || inputValue === '') && !silent)
hasEmptyInput = label as string
if (
(type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
diff --git a/web/app/components/base/chat/embedded-chatbot/inputs-form/__tests__/content.spec.tsx b/web/app/components/base/chat/embedded-chatbot/inputs-form/__tests__/content.spec.tsx
index 70a9b2eb0ec..a4eeee89dc7 100644
--- a/web/app/components/base/chat/embedded-chatbot/inputs-form/__tests__/content.spec.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/inputs-form/__tests__/content.spec.tsx
@@ -281,4 +281,20 @@ describe('InputsFormContent', () => {
render(<InputsFormContent />)
expect(screen.getByDisplayValue('conv value'))!.toBeInTheDocument()
})
+
+ it('should render numeric zero from context as zero', () => {
+ const contextWithZero = {
+ ...mockContextValue,
+ newConversationInputs: {
+ num_var: 0,
+ },
+ }
+
+ vi.mocked(useEmbeddedChatbotContext).mockReturnValue(
+ contextWithZero as unknown as ReturnType<typeof useEmbeddedChatbotContext>,
+ )
+
+ render(<InputsFormContent />)
+ expect(screen.getByRole('spinbutton', { name: 'Number Label' })).toHaveValue(0)
+ })
})
diff --git a/web/app/components/base/chat/embedded-chatbot/inputs-form/content.tsx b/web/app/components/base/chat/embedded-chatbot/inputs-form/content.tsx
index 1a9a45cfe78..9849b448a04 100644
--- a/web/app/components/base/chat/embedded-chatbot/inputs-form/content.tsx
+++ b/web/app/components/base/chat/embedded-chatbot/inputs-form/content.tsx
@@ -94,7 +94,7 @@ const InputsFormContent = ({ showTip }: Props) => {
<Input
aria-labelledby={`${baseId}-${form.variable}-label`}
type="number"
- value={inputsFormValue?.[form.variable] || ''}
+ value={inputsFormValue?.[form.variable] ?? ''}
onValueChange={(value) => handleFormChange(form.variable, value)}
placeholder={form.label}
/>