From 08687bae2ee825fa4fb66fac6bcc05cf934d64da Mon Sep 17 00:00:00 2001 From: linshen <32978552+linshenkx@users.noreply.github.com> Date: Fri, 24 Apr 2026 22:25:24 +0800 Subject: [PATCH] fix(ui): clear content before Prompt Garden import --- .../app/useAppPromptGardenImport.ts | 91 +++++-------------- .../useAppPromptGardenImport.spec.ts | 56 ++++++++++-- 2 files changed, 75 insertions(+), 72 deletions(-) diff --git a/packages/ui/src/composables/app/useAppPromptGardenImport.ts b/packages/ui/src/composables/app/useAppPromptGardenImport.ts index 296d3f82..3c11cffc 100644 --- a/packages/ui/src/composables/app/useAppPromptGardenImport.ts +++ b/packages/ui/src/composables/app/useAppPromptGardenImport.ts @@ -21,7 +21,6 @@ import type { } from '@prompt-optimizer/core' import { useToast } from '../ui/useToast' -import { createDefaultEvaluationResults } from '../../types/evaluation' import { isValidVariableName } from '../../types/variable' import { i18n } from '../../plugins/i18n' import type { BasicSystemSessionApi } from '../../stores/session/useBasicSystemSession' @@ -1072,7 +1071,7 @@ const pickImportedExample = ( return examples[0] || null } -const clearSessionForExternalImport = (targetKey: SupportedSubModeKey, api: { +const clearSessionContentForExternalImport = (targetKey: SupportedSubModeKey, api: { basicSystemSession: BasicSystemSessionApi basicUserSession: BasicUserSessionApi proVariableSession: ProVariableSessionApi @@ -1080,82 +1079,39 @@ const clearSessionForExternalImport = (targetKey: SupportedSubModeKey, api: { imageImage2ImageSession: ImageImage2ImageSessionApi imageMultiImageSession: ImageMultiImageSessionApi optimizerCurrentVersions: Ref -}, content: string) => { - const resetCommon = (session: { - updateOptimizedResult: (payload: { - optimizedPrompt: string - reasoning?: string - chainId: string - versionId: string - }) => void - // Pinia setup stores unwrap refs on the store type, so this is the plain value. - evaluationResults?: unknown - }) => { - session.updateOptimizedResult({ - optimizedPrompt: '', - reasoning: '', - chainId: '', - versionId: '' - }) - if (session.evaluationResults !== undefined) { - session.evaluationResults = createDefaultEvaluationResults() - } - } - +}) => { if (targetKey === 'basic-system') { - api.basicSystemSession.updatePrompt(content) - resetCommon(api.basicSystemSession) - api.basicSystemSession.updateTestContent('') - api.basicSystemSession.resetTestVariantState() + api.basicSystemSession.clearContent({ persist: false }) api.optimizerCurrentVersions.value = [] return } if (targetKey === 'basic-user') { - api.basicUserSession.updatePrompt(content) - resetCommon(api.basicUserSession) - api.basicUserSession.updateTestContent('') - api.basicUserSession.resetTestVariantState() + api.basicUserSession.clearContent({ persist: false }) api.optimizerCurrentVersions.value = [] return } if (targetKey === 'pro-variable') { - api.proVariableSession.updatePrompt(content) - resetCommon(api.proVariableSession) - api.proVariableSession.updateTestContent('') - api.proVariableSession.resetTestVariantState() + api.proVariableSession.clearContent({ persist: false }) return } if (targetKey === 'pro-multi') { - // Conversation mode uses a different state tree (messages snapshot + selection). return } if (targetKey === 'image-text2image') { - api.imageText2ImageSession.updatePrompt(content) - resetCommon(api.imageText2ImageSession) - api.imageText2ImageSession.updateOriginalImageResult(null) - api.imageText2ImageSession.updateOptimizedImageResult(null) + api.imageText2ImageSession.clearContent({ persist: false }) return } if (targetKey === 'image-multiimage') { - api.imageMultiImageSession.updatePrompt(content) - resetCommon(api.imageMultiImageSession) - api.imageMultiImageSession.replaceInputImages([]) - api.imageMultiImageSession.updateOriginalImageResult(null) - api.imageMultiImageSession.updateOptimizedImageResult(null) + api.imageMultiImageSession.clearContent({ persist: false }) return } - // image-image2image - api.imageImage2ImageSession.updatePrompt(content) - resetCommon(api.imageImage2ImageSession) - api.imageImage2ImageSession.updateInputImage(null) - api.imageImage2ImageSession.updateOriginalImageResult(null) - api.imageImage2ImageSession.updateOptimizedImageResult(null) + api.imageImage2ImageSession.clearContent({ persist: false }) } type SaveFavoriteDialogPayload = { @@ -1273,19 +1229,9 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions) throw new Error('Empty conversation content') } + proMultiMessageSession.clearContent({ persist: false }) proMultiMessageSession.updateConversationMessages(messages) - // Reset state that is tied to the previously selected message/chain. - proMultiMessageSession.setMessageChainMap({}) - proMultiMessageSession.resetTestVariantState() - proMultiMessageSession.updateOptimizedResult({ - optimizedPrompt: '', - reasoning: '', - chainId: '', - versionId: '', - }) - proMultiMessageSession.evaluationResults = createDefaultEvaluationResults() - // Auto-select latest system/user message for convenience. let selectedId = '' for (let i = messages.length - 1; i >= 0; i--) { @@ -1303,7 +1249,7 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions) throw new Error('Empty prompt content') } - clearSessionForExternalImport( + clearSessionContentForExternalImport( targetKey, { basicSystemSession, @@ -1313,9 +1259,22 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions) imageImage2ImageSession, imageMultiImageSession, optimizerCurrentVersions, - }, - content + } ) + + if (targetKey === 'basic-system') { + basicSystemSession.updatePrompt(content) + } else if (targetKey === 'basic-user') { + basicUserSession.updatePrompt(content) + } else if (targetKey === 'pro-variable') { + proVariableSession.updatePrompt(content) + } else if (targetKey === 'image-text2image') { + imageText2ImageSession.updatePrompt(content) + } else if (targetKey === 'image-multiimage') { + imageMultiImageSession.updatePrompt(content) + } else { + imageImage2ImageSession.updatePrompt(content) + } } // Import variables into submode-scoped temporary variables. diff --git a/packages/ui/tests/unit/composables/useAppPromptGardenImport.spec.ts b/packages/ui/tests/unit/composables/useAppPromptGardenImport.spec.ts index 9c9262c9..13c29148 100644 --- a/packages/ui/tests/unit/composables/useAppPromptGardenImport.spec.ts +++ b/packages/ui/tests/unit/composables/useAppPromptGardenImport.spec.ts @@ -426,8 +426,8 @@ describe('useAppPromptGardenImport', () => { expect(proMultiMessageSession.chainId).toBe('') expect(proMultiMessageSession.versionId).toBe('') - // Variables injected from schema; existing values preserved. - expect(proMultiMessageSession.getTemporaryVariable('topic')).toBe('pizza') + // Variables are re-seeded from the import payload after clear-content runs. + expect(proMultiMessageSession.getTemporaryVariable('topic')).toBe('ice cream') expect(proMultiMessageSession.getTemporaryVariable('format')).toBe('markdown') expect(proMultiMessageSession.getTemporaryVariable('tone')).toBe('') expect(proMultiMessageSession.getTemporaryVariable('obsolete')).toBeUndefined() @@ -581,8 +581,8 @@ describe('useAppPromptGardenImport', () => { expect(proVariableSession.testVariantResults.a).toEqual({ result: '', reasoning: '' }) expect(proVariableSession.testVariantResults.b).toEqual({ result: '', reasoning: '' }) - // Variables injected from schema; existing values preserved. - expect(proVariableSession.getTemporaryVariable('name')).toBe('Bob') + // Variables are re-seeded from the import payload after clear-content runs. + expect(proVariableSession.getTemporaryVariable('name')).toBe('Alice') expect(proVariableSession.getTemporaryVariable('tone')).toBe('') expect(proVariableSession.getTemporaryVariable('obsolete')).toBeUndefined() @@ -1148,6 +1148,18 @@ describe('useAppPromptGardenImport', () => { const imageText2ImageSession = useImageText2ImageSession(pinia) const imageImage2ImageSession = useImageImage2ImageSession(pinia) + imageText2ImageSession.updateOptimizedResult({ + optimizedPrompt: 'old-opt', + reasoning: 'old-r', + chainId: 'old-chain', + versionId: 'old-version', + }) + imageText2ImageSession.testVariantResults = { + ...imageText2ImageSession.testVariantResults, + a: { result: 'old-a', reasoning: 'old-a-r' }, + b: { result: 'old-b', reasoning: 'old-b-r' }, + } + // Existing values should be preserved. imageText2ImageSession.setTemporaryVariable('season', 'winter') imageText2ImageSession.setTemporaryVariable('obsolete', 'should-delete') @@ -1235,9 +1247,15 @@ describe('useAppPromptGardenImport', () => { // Prompt imported into image session. expect(imageText2ImageSession.originalPrompt).toBe('Draw a {{season}} {{style}} landscape') + expect(imageText2ImageSession.optimizedPrompt).toBe('') + expect(imageText2ImageSession.reasoning).toBe('') + expect(imageText2ImageSession.chainId).toBe('') + expect(imageText2ImageSession.versionId).toBe('') + expect(imageText2ImageSession.testVariantResults.a).toBeNull() + expect(imageText2ImageSession.testVariantResults.b).toBeNull() - // The variable key exists; existing value preserved. - expect(imageText2ImageSession.getTemporaryVariable('season')).toBe('winter') + // Variables are re-seeded from the import payload after clear-content runs. + expect(imageText2ImageSession.getTemporaryVariable('season')).toBe('') // Missing variable names are injected as empty strings. expect(imageText2ImageSession.getTemporaryVariable('style')).toBe('') @@ -1277,6 +1295,25 @@ describe('useAppPromptGardenImport', () => { const imageText2ImageSession = useImageText2ImageSession(pinia) const imageImage2ImageSession = useImageImage2ImageSession(pinia) + imageImage2ImageSession.updatePrompt('old prompt') + imageImage2ImageSession.updateOptimizedResult({ + optimizedPrompt: 'old-opt', + reasoning: 'old-r', + chainId: 'old-chain', + versionId: 'old-version', + }) + imageImage2ImageSession.updateInputImage({ + imageB64: 'old-b64', + imageId: null, + mimeType: 'image/png', + }) + imageImage2ImageSession.testVariantResults = { + ...imageImage2ImageSession.testVariantResults, + a: { result: 'old-a', reasoning: 'old-a-r' }, + b: { result: 'old-b', reasoning: 'old-b-r' }, + } + imageImage2ImageSession.setTemporaryVariable('obsolete', 'should-delete') + const optimizerCurrentVersions = ref([makeDummyRecord()]) const hasRestoredInitialState = ref(false) const isLoadingExternalData = ref(false) @@ -1374,6 +1411,13 @@ describe('useAppPromptGardenImport', () => { expect(currentRoute.value.path).toBe('/image/image2image') expect(imageImage2ImageSession.originalPrompt).toBe('Transform the image') + expect(imageImage2ImageSession.optimizedPrompt).toBe('') + expect(imageImage2ImageSession.reasoning).toBe('') + expect(imageImage2ImageSession.chainId).toBe('') + expect(imageImage2ImageSession.versionId).toBe('') + expect(imageImage2ImageSession.testVariantResults.a).toBeNull() + expect(imageImage2ImageSession.testVariantResults.b).toBeNull() + expect(imageImage2ImageSession.getTemporaryVariable('obsolete')).toBeUndefined() // [0,1,2,3] -> AAECAw== expect(imageImage2ImageSession.inputImageB64).toBe('AAECAw==')