Skip to content

Commit b93c3c5

Browse files
committed
fix: 解决Pod2Post生成中的占位符图片问题
- 添加相对CDN路径处理,将 ../CDN/ 格式转换为 CDN/ - 确保AI生成时使用实际的CDN资源而非占位符URL - 支持Docker和本地开发环境的公共CDN路径检测 - 修复正则表达式以正确处理CSS url()中的路径 问题根因:模板文档中使用 ../CDN/cardplanet_logo_filled.svg 等相对路径, AI无法解析这些路径,因此生成了 https://via.placeholder.com 占位符。 现在这些路径会被替换为 CDN/filename 格式,让AI知道使用CDN目录中的实际文件。
1 parent 7e505cb commit b93c3c5

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test document content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test document content

terminal-backend/src/utils/promptProcessor.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,63 @@ class PromptProcessor {
108108
totalUserPathReplacements += userCardBracketMatches.length
109109
}
110110

111+
// 3. 处理相对CDN路径 (../CDN/xxx)
112+
// 这些路径通常出现在模板文档中,需要替换为实际CDN路径
113+
const relativeCdnMatches = processed.match(/\.\.\/CDN\/[^"'\s<>)]+/g) || []
114+
if (relativeCdnMatches.length > 0) {
115+
console.log(`[PromptProcessor] Found ${relativeCdnMatches.length} relative CDN paths to replace`)
116+
117+
for (const match of relativeCdnMatches) {
118+
const cdnFileName = match.replace('../CDN/', '').replace(/[)]+$/, '') // 移除末尾的括号
119+
let cdnPath = null
120+
121+
// 优先使用任务特定CDN
122+
if (taskId) {
123+
const taskCdnPath = path.join(userTemplateDir, 'tasks', taskId, 'CDN', cdnFileName)
124+
if (await this.pathExists(taskCdnPath)) {
125+
cdnPath = `CDN/${cdnFileName}` // 使用相对路径,让AI知道这是CDN目录
126+
console.log(`[PromptProcessor] Replaced ${match} -> ${cdnPath} (task CDN)`)
127+
}
128+
}
129+
130+
// 如果任务CDN不存在,使用公共CDN
131+
if (!cdnPath) {
132+
// 尝试Docker路径
133+
let publicCdnPath = path.join('/app/data/public_template/pod2post/CDN', cdnFileName)
134+
let publicCdnExists = await this.pathExists(publicCdnPath)
135+
136+
// 如果Docker路径不存在,尝试本地开发路径
137+
if (!publicCdnExists) {
138+
publicCdnPath = path.join(process.cwd(), 'data/public_template/pod2post/CDN', cdnFileName)
139+
publicCdnExists = await this.pathExists(publicCdnPath)
140+
}
141+
142+
if (publicCdnExists) {
143+
cdnPath = `CDN/${cdnFileName}` // 使用相对路径格式
144+
console.log(`[PromptProcessor] Replaced ${match} -> ${cdnPath} (public CDN)`)
145+
} else {
146+
console.warn(`[PromptProcessor] CDN file not found: ${cdnFileName}`)
147+
// 保持原样,让AI知道需要这个文件
148+
cdnPath = `CDN/${cdnFileName}`
149+
}
150+
}
151+
152+
processed = processed.replace(new RegExp(match.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), cdnPath)
153+
}
154+
}
155+
111156
// 如果有未找到的路径,抛出错误
112157
if (notFoundPaths.length > 0) {
113158
const error = new Error(`以下路径未找到: ${notFoundPaths.join(', ')}`)
114159
error.notFoundPaths = notFoundPaths
115160
throw error
116161
}
117-
162+
118163
// 打印处理后的prompt
119164
console.log('[PromptProcessor] ==========================================')
120165
console.log('[PromptProcessor] Processed prompt:')
121166
console.log('[PromptProcessor] ', processed.substring(0, 300) + (processed.length > 300 ? '...' : ''))
122-
console.log('[PromptProcessor] Total replacements:', bracketMatches.length + totalUserPathReplacements)
167+
console.log('[PromptProcessor] Total replacements:', bracketMatches.length + totalUserPathReplacements + relativeCdnMatches.length)
123168
console.log('[PromptProcessor] ==========================================')
124169

125170
return processed

0 commit comments

Comments
 (0)