| 1 | type StyleImageSection = { |
| 2 | basePrompt: string |
| 3 | styleGuidance: string |
| 4 | } |
| 5 | |
| 6 | export const splitStyleImageSection = (styleSkillPrompt: string): StyleImageSection => { |
| 7 | const lines = styleSkillPrompt.trim().split(/\r?\n/) |
| 8 | const start = lines.findIndex((line) => /^##\s*配图\s*$/.test(line.trim())) |
| 9 | if (start < 0) return { basePrompt: styleSkillPrompt.trim(), styleGuidance: '' } |
| 10 | |
| 11 | let end = start + 1 |
| 12 | while (end < lines.length && !/^##\s+/.test(lines[end].trim())) end += 1 |
| 13 | const basePrompt = [...lines.slice(0, start), ...lines.slice(end)] |
| 14 | .join('\n') |
| 15 | .replace(/\n{3,}/g, '\n\n') |
| 16 | .trim() |
| 17 | return { |
| 18 | basePrompt, |
| 19 | styleGuidance: lines.slice(start + 1, end).join('\n').trim() |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | export const buildStyleImageGuidance = (args: { |
| 24 | visualEnabled: boolean |
| 25 | imageGenerationPrompt?: string | null |
| 26 | styleGuidance?: string |
| 27 | }): string => { |
| 28 | const imageDirection = args.imageGenerationPrompt?.trim() |
| 29 | if (!args.visualEnabled || !imageDirection) return '' |
| 30 | |
| 31 | return [ |
| 32 | '## 配图', |
| 33 | args.styleGuidance?.trim() || '' |
| 34 | ] |
| 35 | .filter(Boolean) |
| 36 | .join('\n') |
| 37 | } |
| 38 | |
| 39 | export const appendStyleImageGuidance = ( |
| 40 | styleSkillPrompt: string, |
| 41 | args: { |
| 42 | visualEnabled: boolean |
| 43 | imageGenerationPrompt?: string | null |
| 44 | } |
| 45 | ): string => { |
| 46 | const { basePrompt, styleGuidance } = splitStyleImageSection(styleSkillPrompt) |
| 47 | const guidance = buildStyleImageGuidance({ ...args, styleGuidance }) |
| 48 | return guidance ? `${basePrompt}\n\n${guidance}` : basePrompt |
| 49 | } |
| 50 |