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

@@ -36,6 +36,13 @@ class GenerateV2Request(BaseModel):
max_attempts: int = Field(default=2, ge=1, le=5, description="Máximo intentos de regeneración")
class ThreadPost(BaseModel):
"""Un post individual de un hilo."""
number: int
content: str
char_count: int
class GenerateV2Response(BaseModel):
"""Respuesta de generación v2."""
success: bool
@@ -47,6 +54,9 @@ class GenerateV2Response(BaseModel):
attempts: int = 1
metadata: Optional[Dict[str, Any]] = None
error: Optional[str] = None
# Campos específicos para hilos
is_thread: bool = False
thread_posts: Optional[List[ThreadPost]] = None
class BatchGenerateV2Request(BaseModel):
@@ -142,15 +152,26 @@ async def generate_content_v2(
max_attempts=request.max_attempts
)
# Detectar si es un hilo
is_thread = result.get("is_thread", False)
thread_posts = None
if is_thread and result.get("thread_posts"):
thread_posts = [
ThreadPost(**post) for post in result["thread_posts"]
]
return GenerateV2Response(
success=True,
content=result["content"],
adapted_content=result["content"], # Ya está adaptado
adapted_content=result["content"], # Para hilos, no truncar
quality_score=result.get("quality_score"),
score_breakdown=result.get("score_breakdown"),
is_top_quality=result.get("is_top_performer", False),
attempts=result.get("attempts", 1),
metadata=result.get("metadata")
metadata=result.get("metadata"),
is_thread=is_thread,
thread_posts=thread_posts
)
else:
result = await generator._v2.generate(
@@ -163,11 +184,22 @@ async def generate_content_v2(
personality=request.personality
)
# Detectar si es un hilo
is_thread = result.get("is_thread", False)
thread_posts = None
if is_thread and result.get("thread_posts"):
thread_posts = [
ThreadPost(**post) for post in result["thread_posts"]
]
return GenerateV2Response(
success=True,
content=result["content"],
adapted_content=result["adapted_content"],
metadata=result["metadata"]
metadata=result["metadata"],
is_thread=is_thread,
thread_posts=thread_posts
)
except Exception as e: