mirror of
https://github.com/linshenkx/prompt-optimizer.git
synced 2026-05-06 21:50:27 +08:00
fix(ui): clear content before Prompt Garden import
This commit is contained in:
@@ -21,7 +21,6 @@ import type {
|
|||||||
} from '@prompt-optimizer/core'
|
} from '@prompt-optimizer/core'
|
||||||
|
|
||||||
import { useToast } from '../ui/useToast'
|
import { useToast } from '../ui/useToast'
|
||||||
import { createDefaultEvaluationResults } from '../../types/evaluation'
|
|
||||||
import { isValidVariableName } from '../../types/variable'
|
import { isValidVariableName } from '../../types/variable'
|
||||||
import { i18n } from '../../plugins/i18n'
|
import { i18n } from '../../plugins/i18n'
|
||||||
import type { BasicSystemSessionApi } from '../../stores/session/useBasicSystemSession'
|
import type { BasicSystemSessionApi } from '../../stores/session/useBasicSystemSession'
|
||||||
@@ -1072,7 +1071,7 @@ const pickImportedExample = (
|
|||||||
return examples[0] || null
|
return examples[0] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
const clearSessionForExternalImport = (targetKey: SupportedSubModeKey, api: {
|
const clearSessionContentForExternalImport = (targetKey: SupportedSubModeKey, api: {
|
||||||
basicSystemSession: BasicSystemSessionApi
|
basicSystemSession: BasicSystemSessionApi
|
||||||
basicUserSession: BasicUserSessionApi
|
basicUserSession: BasicUserSessionApi
|
||||||
proVariableSession: ProVariableSessionApi
|
proVariableSession: ProVariableSessionApi
|
||||||
@@ -1080,82 +1079,39 @@ const clearSessionForExternalImport = (targetKey: SupportedSubModeKey, api: {
|
|||||||
imageImage2ImageSession: ImageImage2ImageSessionApi
|
imageImage2ImageSession: ImageImage2ImageSessionApi
|
||||||
imageMultiImageSession: ImageMultiImageSessionApi
|
imageMultiImageSession: ImageMultiImageSessionApi
|
||||||
optimizerCurrentVersions: Ref<PromptRecordChain['versions']>
|
optimizerCurrentVersions: Ref<PromptRecordChain['versions']>
|
||||||
}, 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') {
|
if (targetKey === 'basic-system') {
|
||||||
api.basicSystemSession.updatePrompt(content)
|
api.basicSystemSession.clearContent({ persist: false })
|
||||||
resetCommon(api.basicSystemSession)
|
|
||||||
api.basicSystemSession.updateTestContent('')
|
|
||||||
api.basicSystemSession.resetTestVariantState()
|
|
||||||
api.optimizerCurrentVersions.value = []
|
api.optimizerCurrentVersions.value = []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetKey === 'basic-user') {
|
if (targetKey === 'basic-user') {
|
||||||
api.basicUserSession.updatePrompt(content)
|
api.basicUserSession.clearContent({ persist: false })
|
||||||
resetCommon(api.basicUserSession)
|
|
||||||
api.basicUserSession.updateTestContent('')
|
|
||||||
api.basicUserSession.resetTestVariantState()
|
|
||||||
api.optimizerCurrentVersions.value = []
|
api.optimizerCurrentVersions.value = []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetKey === 'pro-variable') {
|
if (targetKey === 'pro-variable') {
|
||||||
api.proVariableSession.updatePrompt(content)
|
api.proVariableSession.clearContent({ persist: false })
|
||||||
resetCommon(api.proVariableSession)
|
|
||||||
api.proVariableSession.updateTestContent('')
|
|
||||||
api.proVariableSession.resetTestVariantState()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetKey === 'pro-multi') {
|
if (targetKey === 'pro-multi') {
|
||||||
// Conversation mode uses a different state tree (messages snapshot + selection).
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetKey === 'image-text2image') {
|
if (targetKey === 'image-text2image') {
|
||||||
api.imageText2ImageSession.updatePrompt(content)
|
api.imageText2ImageSession.clearContent({ persist: false })
|
||||||
resetCommon(api.imageText2ImageSession)
|
|
||||||
api.imageText2ImageSession.updateOriginalImageResult(null)
|
|
||||||
api.imageText2ImageSession.updateOptimizedImageResult(null)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetKey === 'image-multiimage') {
|
if (targetKey === 'image-multiimage') {
|
||||||
api.imageMultiImageSession.updatePrompt(content)
|
api.imageMultiImageSession.clearContent({ persist: false })
|
||||||
resetCommon(api.imageMultiImageSession)
|
|
||||||
api.imageMultiImageSession.replaceInputImages([])
|
|
||||||
api.imageMultiImageSession.updateOriginalImageResult(null)
|
|
||||||
api.imageMultiImageSession.updateOptimizedImageResult(null)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// image-image2image
|
api.imageImage2ImageSession.clearContent({ persist: false })
|
||||||
api.imageImage2ImageSession.updatePrompt(content)
|
|
||||||
resetCommon(api.imageImage2ImageSession)
|
|
||||||
api.imageImage2ImageSession.updateInputImage(null)
|
|
||||||
api.imageImage2ImageSession.updateOriginalImageResult(null)
|
|
||||||
api.imageImage2ImageSession.updateOptimizedImageResult(null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SaveFavoriteDialogPayload = {
|
type SaveFavoriteDialogPayload = {
|
||||||
@@ -1273,19 +1229,9 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions)
|
|||||||
throw new Error('Empty conversation content')
|
throw new Error('Empty conversation content')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
proMultiMessageSession.clearContent({ persist: false })
|
||||||
proMultiMessageSession.updateConversationMessages(messages)
|
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.
|
// Auto-select latest system/user message for convenience.
|
||||||
let selectedId = ''
|
let selectedId = ''
|
||||||
for (let i = messages.length - 1; i >= 0; i--) {
|
for (let i = messages.length - 1; i >= 0; i--) {
|
||||||
@@ -1303,7 +1249,7 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions)
|
|||||||
throw new Error('Empty prompt content')
|
throw new Error('Empty prompt content')
|
||||||
}
|
}
|
||||||
|
|
||||||
clearSessionForExternalImport(
|
clearSessionContentForExternalImport(
|
||||||
targetKey,
|
targetKey,
|
||||||
{
|
{
|
||||||
basicSystemSession,
|
basicSystemSession,
|
||||||
@@ -1313,9 +1259,22 @@ export function useAppPromptGardenImport(options: AppPromptGardenImportOptions)
|
|||||||
imageImage2ImageSession,
|
imageImage2ImageSession,
|
||||||
imageMultiImageSession,
|
imageMultiImageSession,
|
||||||
optimizerCurrentVersions,
|
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.
|
// Import variables into submode-scoped temporary variables.
|
||||||
|
|||||||
@@ -426,8 +426,8 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
expect(proMultiMessageSession.chainId).toBe('')
|
expect(proMultiMessageSession.chainId).toBe('')
|
||||||
expect(proMultiMessageSession.versionId).toBe('')
|
expect(proMultiMessageSession.versionId).toBe('')
|
||||||
|
|
||||||
// Variables injected from schema; existing values preserved.
|
// Variables are re-seeded from the import payload after clear-content runs.
|
||||||
expect(proMultiMessageSession.getTemporaryVariable('topic')).toBe('pizza')
|
expect(proMultiMessageSession.getTemporaryVariable('topic')).toBe('ice cream')
|
||||||
expect(proMultiMessageSession.getTemporaryVariable('format')).toBe('markdown')
|
expect(proMultiMessageSession.getTemporaryVariable('format')).toBe('markdown')
|
||||||
expect(proMultiMessageSession.getTemporaryVariable('tone')).toBe('')
|
expect(proMultiMessageSession.getTemporaryVariable('tone')).toBe('')
|
||||||
expect(proMultiMessageSession.getTemporaryVariable('obsolete')).toBeUndefined()
|
expect(proMultiMessageSession.getTemporaryVariable('obsolete')).toBeUndefined()
|
||||||
@@ -581,8 +581,8 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
expect(proVariableSession.testVariantResults.a).toEqual({ result: '', reasoning: '' })
|
expect(proVariableSession.testVariantResults.a).toEqual({ result: '', reasoning: '' })
|
||||||
expect(proVariableSession.testVariantResults.b).toEqual({ result: '', reasoning: '' })
|
expect(proVariableSession.testVariantResults.b).toEqual({ result: '', reasoning: '' })
|
||||||
|
|
||||||
// Variables injected from schema; existing values preserved.
|
// Variables are re-seeded from the import payload after clear-content runs.
|
||||||
expect(proVariableSession.getTemporaryVariable('name')).toBe('Bob')
|
expect(proVariableSession.getTemporaryVariable('name')).toBe('Alice')
|
||||||
expect(proVariableSession.getTemporaryVariable('tone')).toBe('')
|
expect(proVariableSession.getTemporaryVariable('tone')).toBe('')
|
||||||
expect(proVariableSession.getTemporaryVariable('obsolete')).toBeUndefined()
|
expect(proVariableSession.getTemporaryVariable('obsolete')).toBeUndefined()
|
||||||
|
|
||||||
@@ -1148,6 +1148,18 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
const imageText2ImageSession = useImageText2ImageSession(pinia)
|
const imageText2ImageSession = useImageText2ImageSession(pinia)
|
||||||
const imageImage2ImageSession = useImageImage2ImageSession(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.
|
// Existing values should be preserved.
|
||||||
imageText2ImageSession.setTemporaryVariable('season', 'winter')
|
imageText2ImageSession.setTemporaryVariable('season', 'winter')
|
||||||
imageText2ImageSession.setTemporaryVariable('obsolete', 'should-delete')
|
imageText2ImageSession.setTemporaryVariable('obsolete', 'should-delete')
|
||||||
@@ -1235,9 +1247,15 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
|
|
||||||
// Prompt imported into image session.
|
// Prompt imported into image session.
|
||||||
expect(imageText2ImageSession.originalPrompt).toBe('Draw a {{season}} {{style}} landscape')
|
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.
|
// Variables are re-seeded from the import payload after clear-content runs.
|
||||||
expect(imageText2ImageSession.getTemporaryVariable('season')).toBe('winter')
|
expect(imageText2ImageSession.getTemporaryVariable('season')).toBe('')
|
||||||
|
|
||||||
// Missing variable names are injected as empty strings.
|
// Missing variable names are injected as empty strings.
|
||||||
expect(imageText2ImageSession.getTemporaryVariable('style')).toBe('')
|
expect(imageText2ImageSession.getTemporaryVariable('style')).toBe('')
|
||||||
@@ -1277,6 +1295,25 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
const imageText2ImageSession = useImageText2ImageSession(pinia)
|
const imageText2ImageSession = useImageText2ImageSession(pinia)
|
||||||
const imageImage2ImageSession = useImageImage2ImageSession(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<PromptRecordChain['versions']>([makeDummyRecord()])
|
const optimizerCurrentVersions = ref<PromptRecordChain['versions']>([makeDummyRecord()])
|
||||||
const hasRestoredInitialState = ref(false)
|
const hasRestoredInitialState = ref(false)
|
||||||
const isLoadingExternalData = ref(false)
|
const isLoadingExternalData = ref(false)
|
||||||
@@ -1374,6 +1411,13 @@ describe('useAppPromptGardenImport', () => {
|
|||||||
|
|
||||||
expect(currentRoute.value.path).toBe('/image/image2image')
|
expect(currentRoute.value.path).toBe('/image/image2image')
|
||||||
expect(imageImage2ImageSession.originalPrompt).toBe('Transform the image')
|
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==
|
// [0,1,2,3] -> AAECAw==
|
||||||
expect(imageImage2ImageSession.inputImageB64).toBe('AAECAw==')
|
expect(imageImage2ImageSession.inputImageB64).toBe('AAECAw==')
|
||||||
|
|||||||
Reference in New Issue
Block a user