feat: Add proper thread handling in content generation

- Detect threads by template name and skip platform truncation
- Parse thread content into individual posts with numbering
- Add thread_posts array to API response with post details
- Evaluate quality on first post (hook) for threads
- Add is_thread and thread_posts fields to GenerateV2Response

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-28 21:23:18 +00:00
parent e32885afc5
commit 9e857961f9
4 changed files with 143 additions and 13 deletions

View File

@@ -164,12 +164,37 @@ class ContentGeneratorV2:
content = response.choices[0].message.content.strip()
# 6. Adaptar a plataforma
# 6. Detectar si es un hilo (no adaptar, devolver posts separados)
is_thread = template_name == "thread"
if is_thread:
# Parsear posts del hilo
thread_posts = self._parse_thread_posts(content)
return {
"content": content,
"adapted_content": content, # No truncar hilos
"is_thread": True,
"thread_posts": thread_posts,
"truncated": False,
"metadata": {
"template": template_name,
"personality": rendered["personality"],
"platform": platform,
"temperature": temperature,
"tokens_used": response.usage.total_tokens if response.usage else None,
"changes_made": [],
"num_posts": len(thread_posts),
}
}
# 7. Adaptar a plataforma (solo para contenido normal)
adapted = self.plt_adapter.adapt(content, platform)
return {
"content": content,
"adapted_content": adapted.content,
"is_thread": False,
"truncated": adapted.truncated,
"metadata": {
"template": template_name,
@@ -181,6 +206,55 @@ class ContentGeneratorV2:
}
}
def _parse_thread_posts(self, content: str) -> List[Dict[str, Any]]:
"""
Parsear el contenido de un hilo en posts individuales.
Args:
content: Contenido completo del hilo
Returns:
Lista de dicts con cada post y sus metadatos
"""
import re
posts = []
# Intentar separar por patrones de numeración: "1/", "2/", "1)", "2)"
# Buscar patrones como "\n1/" o inicio con "1/"
numbered_pattern = r'(?:^|\n)(\d+)[/\)]\s*'
# Encontrar todas las posiciones de inicio de posts numerados
matches = list(re.finditer(numbered_pattern, content))
if matches and len(matches) >= 2:
# Extraer posts basándose en las posiciones
for i, match in enumerate(matches):
start = match.start()
# El fin es el inicio del siguiente match o el final del contenido
end = matches[i + 1].start() if i + 1 < len(matches) else len(content)
post_content = content[start:end].strip()
post_num = int(match.group(1))
posts.append({
"number": post_num,
"content": post_content,
"char_count": len(post_content)
})
else:
# Fallback: separar por doble salto de línea
raw_posts = [p.strip() for p in content.split("\n\n") if p.strip()]
for i, post_content in enumerate(raw_posts, 1):
posts.append({
"number": i,
"content": post_content,
"char_count": len(post_content)
})
return posts
# === Métodos de Conveniencia ===
async def generate_tip(

View File

@@ -630,6 +630,7 @@ Responde SOLO con el contenido adaptado."""
attempt = 0
temperature = 0.7
is_thread = template_name == "thread"
while attempt < max_attempts:
attempt += 1
@@ -643,36 +644,59 @@ Responde SOLO con el contenido adaptado."""
temperature_override=temperature
)
content = result["adapted_content"]
# Para hilos, usar contenido original (no truncado)
if is_thread:
content = result["content"]
else:
content = result["adapted_content"]
# Evaluar calidad
quality = await self._validator.evaluate(content, platform)
# Evaluar calidad (para hilos, evaluar solo el primer post como muestra)
content_to_evaluate = content
if is_thread and result.get("thread_posts"):
# Evaluar el hook (primer post) como indicador de calidad
first_post = result["thread_posts"][0]["content"]
content_to_evaluate = first_post
quality = await self._validator.evaluate(content_to_evaluate, platform)
# Si pasa, retornar
if quality.final_decision == "accept":
return {
response = {
"content": content,
"quality_score": quality.scoring.total_score if quality.scoring else None,
"score_breakdown": quality.scoring.breakdown if quality.scoring else None,
"is_top_performer": quality.scoring.is_top_performer if quality.scoring else False,
"attempts": attempt,
"metadata": result["metadata"]
"metadata": result["metadata"],
"is_thread": is_thread,
}
# Agregar posts del hilo si aplica
if is_thread:
response["thread_posts"] = result.get("thread_posts", [])
return response
# Si debe regenerar, aumentar temperature
temperature = min(1.0, temperature + 0.1)
# Si llegamos aquí, usar el último intento aunque no sea ideal
return {
response = {
"content": content,
"quality_score": quality.scoring.total_score if quality.scoring else None,
"score_breakdown": quality.scoring.breakdown if quality.scoring else None,
"is_top_performer": False,
"attempts": attempt,
"metadata": result["metadata"],
"warning": "Contenido aceptado después de máximos intentos"
"warning": "Contenido aceptado después de máximos intentos",
"is_thread": is_thread,
}
if is_thread:
response["thread_posts"] = result.get("thread_posts", [])
return response
async def save_to_memory(
self,
db: Session,