Files
aigen/utils/promptParts.ts
T
TowstyandCursor f788fd4bb4 Split prompts into pre/prompt/post and tidy the AIGen video form.
Keep wrappers off the library mid-prompt so Use as input and Extend restore the same split, send stills to xAIGen as hidden inputs, and stop the two-column library from growing with the list.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-01 18:39:02 -05:00

43 lines
1.4 KiB
TypeScript

export type PromptParts = {
pre: string
prompt: string
post: string
}
export function sanitizePromptPart(raw: unknown) {
return String(raw || '').replace(/\r\n/g, '\n').replace(/\r/g, '')
}
export function composePromptParts(pre: string, prompt: string, post: string) {
return [pre, prompt, post]
.map(part => sanitizePromptPart(part).trim())
.filter(Boolean)
.join('\n\n')
}
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 {
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 || ''
}
}