Skip to content

Commit 03f310d

Browse files
committed
Persist generated_content in CosmosDB and restore on conversation load
- Added generated_content parameter to save_conversation in cosmos_service.py - Added save_generated_content method to update existing conversations - Updated /api/generate endpoint to save full generated_content object - Updated handleSelectConversation in App.tsx to restore generatedContent from DB
1 parent 312e270 commit 03f310d

3 files changed

Lines changed: 91 additions & 2 deletions

File tree

content-gen/src/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ async def generate():
328328
cosmos_service = await get_cosmos_service()
329329
text_content = response.get("text_content", {})
330330
headline = text_content.get("headline", "") if isinstance(text_content, dict) else ""
331+
332+
# Save the message
331333
await cosmos_service.add_message_to_conversation(
332334
conversation_id=conversation_id,
333335
user_id=user_id,
@@ -338,6 +340,21 @@ async def generate():
338340
"timestamp": datetime.now(timezone.utc).isoformat()
339341
}
340342
)
343+
344+
# Save the full generated content for restoration
345+
generated_content_to_save = {
346+
"text_content": response.get("text_content"),
347+
"image_url": response.get("image_url"),
348+
"image_prompt": response.get("image_prompt"),
349+
"image_revised_prompt": response.get("image_revised_prompt"),
350+
"violations": response.get("violations", []),
351+
"requires_modification": response.get("requires_modification", False)
352+
}
353+
await cosmos_service.save_generated_content(
354+
conversation_id=conversation_id,
355+
user_id=user_id,
356+
generated_content=generated_content_to_save
357+
)
341358
except Exception as e:
342359
logger.warning(f"Failed to save generated content to CosmosDB: {e}")
343360

content-gen/src/backend/services/cosmos_service.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ async def save_conversation(
255255
user_id: str,
256256
messages: List[dict],
257257
brief: Optional[CreativeBrief] = None,
258-
metadata: Optional[dict] = None
258+
metadata: Optional[dict] = None,
259+
generated_content: Optional[dict] = None
259260
) -> dict:
260261
"""
261262
Save or update a conversation.
@@ -266,6 +267,7 @@ async def save_conversation(
266267
messages: List of conversation messages
267268
brief: Associated creative brief
268269
metadata: Additional metadata
270+
generated_content: Generated marketing content
269271
270272
Returns:
271273
The saved conversation document
@@ -278,12 +280,49 @@ async def save_conversation(
278280
"messages": messages,
279281
"brief": brief.model_dump() if brief else None,
280282
"metadata": metadata or {},
283+
"generated_content": generated_content,
281284
"updated_at": datetime.now(timezone.utc).isoformat()
282285
}
283286

284287
result = await self._conversations_container.upsert_item(item)
285288
return result
286289

290+
async def save_generated_content(
291+
self,
292+
conversation_id: str,
293+
user_id: str,
294+
generated_content: dict
295+
) -> dict:
296+
"""
297+
Save generated content to an existing conversation.
298+
299+
Args:
300+
conversation_id: Unique conversation identifier
301+
user_id: User ID for partition key
302+
generated_content: The generated content to save
303+
304+
Returns:
305+
Updated conversation document
306+
"""
307+
await self.initialize()
308+
309+
conversation = await self.get_conversation(conversation_id, user_id)
310+
311+
if conversation:
312+
conversation["generated_content"] = generated_content
313+
conversation["updated_at"] = datetime.now(timezone.utc).isoformat()
314+
else:
315+
conversation = {
316+
"id": conversation_id,
317+
"user_id": user_id,
318+
"messages": [],
319+
"generated_content": generated_content,
320+
"updated_at": datetime.now(timezone.utc).isoformat()
321+
}
322+
323+
result = await self._conversations_container.upsert_item(conversation)
324+
return result
325+
287326
async def add_message_to_conversation(
288327
self,
289328
conversation_id: str,

content-gen/src/frontend/src/App.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,40 @@ function App() {
5252
setMessages(loadedMessages);
5353
setPendingBrief(null);
5454
setConfirmedBrief(data.brief || null);
55-
setGeneratedContent(null);
55+
56+
// Restore generated content if it exists
57+
if (data.generated_content) {
58+
const gc = data.generated_content;
59+
// Parse text_content if it's a string
60+
let textContent = gc.text_content;
61+
if (typeof textContent === 'string') {
62+
try {
63+
textContent = JSON.parse(textContent);
64+
} catch {
65+
// Keep as string if not valid JSON
66+
}
67+
}
68+
69+
const restoredContent: GeneratedContent = {
70+
text_content: typeof textContent === 'object' && textContent ? {
71+
headline: textContent?.headline,
72+
body: textContent?.body,
73+
cta_text: textContent?.cta,
74+
tagline: textContent?.tagline,
75+
} : undefined,
76+
image_content: (gc.image_url || gc.image_prompt) ? {
77+
image_url: gc.image_url,
78+
prompt_used: gc.image_prompt,
79+
alt_text: gc.image_revised_prompt || 'Generated marketing image',
80+
} : undefined,
81+
violations: gc.violations || [],
82+
requires_modification: gc.requires_modification || false,
83+
};
84+
setGeneratedContent(restoredContent);
85+
} else {
86+
setGeneratedContent(null);
87+
}
88+
5689
setSelectedProducts([]);
5790
}
5891
} catch (error) {

0 commit comments

Comments
 (0)