Chat optimizations
This commit is contained in:
@@ -1714,12 +1714,17 @@ async def chat_create_page(request: Request):
|
||||
|
||||
profile_picture = await get_user_avatar(session, user_id)
|
||||
|
||||
# Load all saved posts for sidebar (exclude scheduled and published)
|
||||
all_posts = await db.get_generated_posts(user_id)
|
||||
saved_posts = [post for post in all_posts if post.status not in ['scheduled', 'published']]
|
||||
|
||||
return templates.TemplateResponse("chat_create.html", {
|
||||
"request": request,
|
||||
"page": "chat-create",
|
||||
"session": session,
|
||||
"post_types": post_types,
|
||||
"profile_picture": profile_picture
|
||||
"profile_picture": profile_picture,
|
||||
"saved_posts": saved_posts
|
||||
})
|
||||
|
||||
|
||||
@@ -3550,6 +3555,139 @@ async def chat_save_post(request: Request):
|
||||
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
|
||||
|
||||
|
||||
@user_router.get("/api/employee/chat/history/{post_id}")
|
||||
async def get_chat_history(request: Request, post_id: str):
|
||||
"""Get chat history for a saved post."""
|
||||
session = require_user_session(request)
|
||||
if not session:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
try:
|
||||
user_id = UUID(session.user_id)
|
||||
post_uuid = UUID(post_id)
|
||||
|
||||
# Fetch post
|
||||
post = await db.get_generated_post(post_uuid)
|
||||
if not post:
|
||||
return JSONResponse({"success": False, "error": "Post nicht gefunden"}, status_code=404)
|
||||
|
||||
# Verify ownership
|
||||
if post.user_id != user_id:
|
||||
return JSONResponse({"success": False, "error": "Nicht autorisiert"}, status_code=403)
|
||||
|
||||
# Reconstruct chat history from writer_versions and critic_feedback
|
||||
chat_history = []
|
||||
|
||||
# First version: AI generates initial post (no user message)
|
||||
if post.writer_versions and len(post.writer_versions) > 0:
|
||||
first_explanation = "Hier ist dein erster Entwurf:"
|
||||
# Check if first critic feedback has explanation (from AI)
|
||||
if post.critic_feedback and len(post.critic_feedback) > 0:
|
||||
first_explanation = post.critic_feedback[0].get('explanation', first_explanation)
|
||||
|
||||
chat_history.append({
|
||||
"user": "", # No user message for first generation
|
||||
"ai": post.writer_versions[0],
|
||||
"explanation": first_explanation
|
||||
})
|
||||
|
||||
# Subsequent versions: User feedback → AI refined version
|
||||
for i in range(1, len(post.writer_versions)):
|
||||
user_message = ""
|
||||
explanation = "Hier ist die überarbeitete Version:"
|
||||
|
||||
# Get user feedback from critic_feedback (offset by 1, since first is for initial)
|
||||
if i <= len(post.critic_feedback):
|
||||
feedback_item = post.critic_feedback[i - 1]
|
||||
user_message = feedback_item.get('feedback', '')
|
||||
explanation = feedback_item.get('explanation', explanation)
|
||||
|
||||
chat_history.append({
|
||||
"user": user_message,
|
||||
"ai": post.writer_versions[i],
|
||||
"explanation": explanation
|
||||
})
|
||||
|
||||
return JSONResponse({
|
||||
"success": True,
|
||||
"chat_history": chat_history,
|
||||
"post": post.post_content,
|
||||
"post_type_id": str(post.post_type_id) if post.post_type_id else None,
|
||||
"topic_title": post.topic_title
|
||||
})
|
||||
|
||||
except ValueError:
|
||||
return JSONResponse({"success": False, "error": "Ungültige Post-ID"}, status_code=400)
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching chat history: {e}")
|
||||
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
|
||||
|
||||
|
||||
@user_router.put("/api/employee/chat/update/{post_id}")
|
||||
async def update_chat_post(request: Request, post_id: str):
|
||||
"""Update an existing post with new chat conversation."""
|
||||
session = require_user_session(request)
|
||||
if not session:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
try:
|
||||
user_id = UUID(session.user_id)
|
||||
post_uuid = UUID(post_id)
|
||||
|
||||
data = await request.json()
|
||||
post_content = data.get("post_content", "").strip()
|
||||
chat_history = data.get("chat_history", [])
|
||||
|
||||
if not post_content:
|
||||
return JSONResponse({"success": False, "error": "Post-Inhalt erforderlich"})
|
||||
|
||||
# Fetch existing post
|
||||
post = await db.get_generated_post(post_uuid)
|
||||
if not post:
|
||||
return JSONResponse({"success": False, "error": "Post nicht gefunden"}, status_code=404)
|
||||
|
||||
# Verify ownership
|
||||
if post.user_id != user_id:
|
||||
return JSONResponse({"success": False, "error": "Nicht autorisiert"}, status_code=403)
|
||||
|
||||
# Extract all AI-generated versions from chat history
|
||||
writer_versions = []
|
||||
critic_feedback_list = []
|
||||
|
||||
for item in chat_history:
|
||||
if 'ai' in item and item['ai']:
|
||||
writer_versions.append(item['ai'])
|
||||
# Store user feedback as "critic feedback"
|
||||
if 'user' in item and item['user']:
|
||||
critic_feedback_list.append({
|
||||
'feedback': item['user'],
|
||||
'explanation': item.get('explanation', '')
|
||||
})
|
||||
|
||||
# Prepare update data
|
||||
updates = {
|
||||
'post_content': post_content,
|
||||
'writer_versions': writer_versions,
|
||||
'critic_feedback': critic_feedback_list,
|
||||
'iterations': len(writer_versions)
|
||||
}
|
||||
|
||||
# Update the post using the correct method
|
||||
updated_post = await db.update_generated_post(post_uuid, updates)
|
||||
|
||||
return JSONResponse({
|
||||
"success": True,
|
||||
"post_id": str(updated_post.id),
|
||||
"message": "Post erfolgreich aktualisiert"
|
||||
})
|
||||
|
||||
except ValueError:
|
||||
return JSONResponse({"success": False, "error": "Ungültige Post-ID"}, status_code=400)
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating chat post: {e}")
|
||||
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
|
||||
|
||||
|
||||
@user_router.post("/api/company/invite")
|
||||
async def send_company_invitation(request: Request):
|
||||
"""Send invitation to a new employee."""
|
||||
|
||||
Reference in New Issue
Block a user