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): keep generated variables when applying the prompt generator (#45274)

Haoyang Lid930c42f9e38
diff --git a/web/app/components/app/configuration/__tests__/configuration-flow.spec.tsx b/web/app/components/app/configuration/__tests__/configuration-flow.spec.tsx
index 560074d2cce..6dc4ee22098 100644
--- a/web/app/components/app/configuration/__tests__/configuration-flow.spec.tsx
+++ b/web/app/components/app/configuration/__tests__/configuration-flow.spec.tsx
@@ -5,8 +5,11 @@ import { zProviderWithModelsResponse } from '@dify/contracts/api/console/workspa
import { act, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import Cookies from 'js-cookie'
+import * as reactI18next from 'react-i18next'
+import { onTestFinished } from 'vite-plus/test'
import CommonLayoutError from '@/app/(commonLayout)/error'
import ErrorBoundary from '@/app/components/base/error-boundary'
+import { replacePromptEditorText } from '@/app/components/base/prompt-editor/plugins/__tests__/test-helpers'
import { DETAIL_SIDEBAR_COOKIE_NAME } from '@/app/components/detail-sidebar/cookie'
import { PermissionType } from '@/app/components/plugins/types'
import { useStore as usePluginDependenciesStore } from '@/app/components/workflow/plugin-dependency/store'
@@ -64,6 +67,7 @@ const api = vi.hoisted(() => ({
install: vi.fn<typeof consoleClient.workspaces.current.plugin.install.marketplace.post>(),
models: vi.fn<typeof consoleClient.workspaces.current.models.modelTypes.byModelType.get>(),
modelSummary: vi.fn<typeof consoleClient.workspaces.current.modelProviders.summary.get>(),
+ generate: vi.fn<typeof consoleClient.ruleGenerate.post>(),
}))
vi.mock('@/service/console', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/service/console')>()
@@ -86,6 +90,7 @@ vi.mock('@/service/console', async (importOriginal) => {
api.rules(input),
}),
consoleClient: withConsoleClientResponses(actual.consoleClient, {
+ 'ruleGenerate.post': (input) => api.generate(input),
'workspaces.current.plugin.permission.fetch.get': async () => ({
install_permission: PermissionType.everyone,
debug_permission: PermissionType.everyone,
@@ -695,6 +700,56 @@ it('adds a prompt variable, edits its label, and publishes the actual form schem
})
})
+it('keeps generated variables when the render after Apply waits for a translation namespace', async () => {
+ const namespace = createDeferred()
+ api.generate.mockResolvedValue({
+ prompt: 'Plan a trip to {{city}}.',
+ variables: ['city'],
+ opening_statement: 'Where to?',
+ error: '',
+ })
+ const { user } = setup()
+ await waitForPage()
+ await user.click(screen.getByRole('button', { name: 'appDebug.operation.automatic' }))
+ const dialog = await screen.findByRole('dialog', { name: 'appGeneration.generate.title' })
+ await replacePromptEditorText(
+ within(dialog).getByRole('textbox', { name: 'appGeneration.generate.instruction' }),
+ 'Help plan a city trip',
+ )
+ await user.click(within(dialog).getByRole('button', { name: 'appGeneration.generate.generate' }))
+ await user.click(
+ await within(dialog).findByRole('button', { name: 'appGeneration.generate.apply' }),
+ )
+ // Namespaces load on demand, so the render that first needs this one suspends without committing.
+ const { useTranslation } = reactI18next
+ const namespaceLoading = vi.spyOn(reactI18next, 'useTranslation').mockImplementation(((
+ ...args: Parameters<typeof useTranslation>
+ ) => {
+ if (args[0]?.includes('appAnnotation')) throw namespace.promise
+ return useTranslation(...args)
+ }) as typeof useTranslation)
+ onTestFinished(() => namespaceLoading.mockRestore())
+ await user.click(
+ within(screen.getByRole('alertdialog')).getByRole('button', {
+ name: 'common.operation.confirm',
+ }),
+ )
+ expect(screen.getByRole('alertdialog')).toBeInTheDocument()
+ await act(async () => {
+ namespaceLoading.mockRestore()
+ namespace.resolve()
+ })
+ expect(await screen.findByTitle('city · city')).toBeInTheDocument()
+ expect(screen.queryByText('appDebug.formattingChangedTitle')).not.toBeInTheDocument()
+ await publish(user)
+ expect(api.publish.mock.lastCall![0]).toMatchObject({
+ body: {
+ pre_prompt: 'Plan a trip to {{city}}.',
+ user_input_form: [{ 'text-input': { variable: 'city' } }],
+ },
+ })
+})
+
it('edits advanced completion history prefixes through the page and publishes them', async () => {
const app = createApp({
model_config: createAppModelConfigFixture({
diff --git a/web/app/components/base/prompt-editor/plugins/update-block.tsx b/web/app/components/base/prompt-editor/plugins/update-block.tsx
index 9d93e5a6bb9..f19d6d012c6 100644
--- a/web/app/components/base/prompt-editor/plugins/update-block.tsx
+++ b/web/app/components/base/prompt-editor/plugins/update-block.tsx
@@ -19,13 +19,18 @@ const UpdateBlock = ({ instanceId }: UpdateBlockProps) => {
if (v.type === PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER && v.instanceId === instanceId) {
const editorState = editor.parseEditorState(textToEditorState(v.payload))
editor.setEditorState(editorState)
- editor.update(() => {
- $getRoot()
- .getAllTextNodes()
- .forEach((node) => {
- if (node instanceof CustomTextNode) node.markDirty()
- })
- })
+ // Commit synchronously so both onChange reports fire before the emitter's caller writes its
+ // own state. A deferred commit could report after that write from a stale render and undo it.
+ editor.update(
+ () => {
+ $getRoot()
+ .getAllTextNodes()
+ .forEach((node) => {
+ if (node instanceof CustomTextNode) node.markDirty()
+ })
+ },
+ { discrete: true },
+ )
}
})