Restore pre-text and post-text with Use as input and Details.

Library items already stored those fields, but Details showed one blob and Use as input left the form's empty pre/post in place.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-09-02 14:51:35 -05:00
co-authored by Cursor
parent c20229da0e
commit c4e18eeb2c
6 changed files with 58 additions and 37 deletions
+25 -16
View File
@@ -19,24 +19,33 @@ export function applyPromptParts(parts: PromptParts, middle: string) {
return composePromptParts(parts.pre, middle, parts.post)
}
export function restorePromptParts(saved: string, stored?: Partial<PromptParts> | null, current?: Partial<PromptParts> | null): PromptParts {
function peelPromptWrappers(mid: string, pre: string, post: string) {
let next = sanitizePromptPart(mid)
if (pre && next.startsWith(pre)) next = next.slice(pre.length).replace(/^\n+/, '')
if (post && next.endsWith(post)) next = next.slice(0, Math.max(0, next.length - post.length)).replace(/\n+$/, '')
return next.trim()
}
export function restorePromptParts(saved: string, stored?: Partial<PromptParts> | null): PromptParts {
const pre = sanitizePromptPart(stored?.pre ?? '').trim()
const post = sanitizePromptPart(stored?.post ?? '').trim()
if (pre || post) {
return {
pre,
prompt: sanitizePromptPart(stored?.prompt ?? saved).trim() || sanitizePromptPart(saved).trim(),
post
}
}
let mid = sanitizePromptPart(stored?.prompt ?? saved)
const curPre = sanitizePromptPart(current?.pre || '').trim()
const curPost = sanitizePromptPart(current?.post || '').trim()
if (curPre && mid.startsWith(curPre)) mid = mid.slice(curPre.length).replace(/^\n+/, '')
if (curPost && mid.endsWith(curPost)) mid = mid.slice(0, Math.max(0, mid.length - curPost.length)).replace(/\n+$/, '')
return {
pre: current?.pre || '',
prompt: mid.trim(),
post: current?.post || ''
pre,
prompt: peelPromptWrappers(stored?.prompt ?? saved, pre, post),
post
}
}
export function formatPromptParts(
stored?: { prompt?: string; promptPre?: string; promptPost?: string } | null,
midOverride?: string
) {
const pre = sanitizePromptPart(stored?.promptPre || '').trim()
const post = sanitizePromptPart(stored?.promptPost || '').trim()
const prompt = peelPromptWrappers(midOverride ?? stored?.prompt ?? '', pre, post)
const blocks: string[] = []
if (pre) blocks.push(`Pre-text\n${pre}`)
if (prompt) blocks.push(pre || post ? `Prompt\n${prompt}` : prompt)
if (post) blocks.push(`Post-text\n${post}`)
return blocks.join('\n\n')
}