Buckets:

download
raw
17.4 kB
# backend/tasks/discussion_tasks.py
import shutil
import json
import re
from datetime import datetime
from typing import Any, Dict, Optional
from ascii_colors import trace_exception, ASCIIColors
from backend.db.models.user import User as DBUser, UserMessageGrade, UserStarredDiscussion
from backend.db.models.memory import UserMemory
from backend.discussion import get_user_discussion, get_user_discussion_manager
from backend.session import get_user_discussion_assets_path, get_user_lollms_client
from backend.task_manager import Task
from backend.ws_manager import manager
def _process_data_zone_task(task: Task, username: str, discussion_id: str, contextual_prompt: Optional[str]):
task.log("Starting data zone processing task...")
discussion = get_user_discussion(username, discussion_id)
if not discussion:
raise ValueError("Discussion not found.")
with task.db_session_factory() as db:
db_user = db.query(DBUser).filter(DBUser.username == username).first()
if not db_user:
raise Exception(f"User '{username}' not found.")
user_data_zone = db_user.data_zone or ""
now = datetime.now()
replacements = {
"{{date}}": now.strftime("%Y-%m-%d"),
"{{time}}": now.strftime("%H:%M:%S"),
"{{datetime}}": now.strftime("%Y-%m-%d %H:%M:%S"),
"{{user_name}}": username,
}
processed_user_data_zone = user_data_zone
for placeholder, value in replacements.items():
processed_user_data_zone = processed_user_data_zone.replace(placeholder, value)
all_images_info = discussion.get_discussion_images()
if (not discussion.discussion_data_zone or not discussion.discussion_data_zone.strip()) and not all_images_info and (not contextual_prompt or not contextual_prompt.strip()):
task.log("Data zone and prompt are empty, nothing to process.", "WARNING")
return {"discussion_id": discussion_id, "new_content": ""}
def summary_callback(message: str, msg_type: Any, params: Optional[Dict] = None):
"""Callback to update the task in real-time."""
if task.cancellation_event.is_set():
task.log("Cancellation requested, stopping generation.", level="WARNING")
return False
task.log(message)
task.set_description(message)
if params and 'progress' in params:
task.set_progress(int(params['progress']))
return True
prompt_to_use = contextual_prompt
if (not discussion.discussion_data_zone or not discussion.discussion_data_zone.strip()) and not prompt_to_use and all_images_info:
prompt_to_use = "Describe the attached image(s) in detail."
task.log(f"No text found. Using default prompt: '{prompt_to_use}'")
discussion_images_b64 = [img_info['data'] for img_info in all_images_info if img_info.get('active', True)]
lc = get_user_lollms_client(username)
summary = lc.long_context_processing(
text_to_process = discussion.discussion_data_zone,
images=discussion_images_b64,
contextual_prompt=prompt_to_use,
system_prompt=processed_user_data_zone,
context_fill_percentage = 1,
overlap_tokens= 0,
expected_generation_tokens = int(lc.llm.default_ctx_size/6),
max_scratchpad_tokens = int(lc.llm.default_ctx_size/4),
scratchpad_compression_threshold = int(60*lc.llm.default_ctx_size/64),
streaming_callback=summary_callback
)
if isinstance(summary, dict) and 'error' in summary:
error_message = f"Failed to process data zone: {summary['error']}"
task.log(error_message, "ERROR")
raise Exception(error_message)
if not isinstance(summary, str):
task.log(f"Unexpected non-string result from processing: {type(summary)}. Converting to string.", "WARNING")
summary = str(summary)
discussion.discussion_data_zone = summary
discussion.commit()
all_images_info = discussion.get_discussion_images()
task.set_progress(100)
task.set_description("Processing complete and saved.")
return {
"discussion_id": discussion_id,
"new_content": summary,
"zone": "discussion",
"discussion_images": [img_info['data'] for img_info in all_images_info],
"active_discussion_images": [img_info['active'] for img_info in all_images_info]
}
def _memorize_ltm_task(task: Task, username: str, discussion_id: str):
task.log("Starting long-term memory memorization task...")
try:
with task.db_session_factory() as db:
db_user = db.query(DBUser).filter(DBUser.username == username).first()
if not db_user:
raise Exception("User not found.")
discussion = get_user_discussion(username, discussion_id)
if not discussion:
raise ValueError("Discussion not found.")
# Explicitly load messages
task.set_progress(10)
task.log("Loading discussion history...")
if hasattr(discussion, 'load_messages'):
discussion.load_messages()
messages = discussion.messages
if not messages:
task.log("No messages in discussion to memorize.", "WARNING")
return {"discussion_id": discussion_id, "zone": "memory"}
# Format conversation for LLM
conversation_text = ""
for msg in messages:
sender = msg.sender or "Unknown"
content = msg.content or ""
conversation_text += f"{sender}: {content}\n\n"
task.set_progress(30)
task.log("Analyzing conversation for important facts...")
lc = get_user_lollms_client(username)
system_prompt = """You are a Memory Manager for an AI assistant. Your goal is to extract STRICTLY USEFUL long-term information.
CRITERIA FOR SAVING:
1. User Details: Name, age, profession, specific preferences, or strong dislikes.
2. Solved Problems: If a complex technical problem was solved, summarize the solution logic (not the whole conversation) so it can be recalled later.
3. Project Context: Ongoing project details that will be needed in future sessions.
DO NOT SAVE:
- Casual conversation ("how are you", "thanks", "hello").
- Temporary context (e.g., "rewrite this paragraph", "what is 2+2").
- General knowledge or facts the AI already knows.
- Vague statements.
If no important information is found based on these strict criteria, return {"memories": []}.
"""
template = """{
"memories": [
{"title": "Concise Title", "content": "Specific detail or solution summary"}
]
}"""
# Truncate conversation if too long (simple heuristic)
max_chars = 12000
if len(conversation_text) > max_chars:
conversation_text = conversation_text[-max_chars:]
# Use generate_code to handle structure enforcement and thinking block removal
code_content = lc.generate_code(
conversation_text,
system_prompt=system_prompt,
template=template,
language="json",
n_predict=1024,
temperature=0.1
)
task.set_progress(70)
task.log("Parsing extracted memories...")
if not code_content:
task.log("AI failed to generate memory structure.", "WARNING")
return {"discussion_id": discussion_id, "zone": "memory"}
try:
# Clean any potential leading/trailing whitespace before parsing
data = json.loads(code_content.strip())
extracted_memories = data.get("memories", [])
except json.JSONDecodeError as e:
task.log(f"Failed to parse JSON from AI response: {e}. Content: {code_content[:100]}...", "ERROR")
extracted_memories = []
count = 0
if extracted_memories:
from backend.routers.memories import get_user_memory_manager
mm = get_user_memory_manager(username)
for mem in extracted_memories:
title = mem.get("title")
content = mem.get("content")
if title and content:
combined_content = f"{title}: {content}"
mm.add(content=combined_content, importance=0.85, level=3)
count += 1
task.log(f"Extracted: {title}")
if count > 0:
task.log(f"Successfully saved {count} new memories.")
# Push notification manually to ensure immediate UI update via WS
from backend.ws_manager import manager
manager.send_personal_message_sync({
"type": "data_zone_processed",
"data": {"discussion_id": discussion_id, "zone": "memory"}
}, db_user.id)
else:
task.log("No relevant information found to save.", "INFO")
else:
task.log("No important memories found in this conversation.", "INFO")
task.set_progress(100)
task.log("Memorization task finished.")
return {"discussion_id": discussion_id, "zone": "memory"}
except Exception as e:
task.log(f"Memorization failed: {str(e)}", "ERROR")
trace_exception(e)
raise e
def _clean_discussion_data_zone_task(task: Task, username: str, discussion_id: str):
task.log("Running Pure Artefact migration on Data Zone...")
discussion = get_user_discussion(username, discussion_id)
if not discussion:
raise ValueError("Discussion not found.")
content = discussion.discussion_data_zone or ""
if not content.strip():
task.log("Data zone is empty.")
return {"discussion_id": discussion_id, "new_content": ""}
# Nuclear Cleanup: Remove ANY block delineated by '---' to ensure the Data Zone
# is only used for manual user input, leaving Artefacts to the layered manager.
block_pattern = re.compile(
r"--- .*? ---[\s\r\n]+([\s\S]*?)[\s\r\n]+--- End .*? ---",
re.MULTILINE | re.IGNORECASE
)
final_content = block_pattern.sub("", content).strip()
# Secondary pass to remove orphaned headers/footers
final_content = re.sub(r"--- (Document|Skill|Note|Artefact): .*? ---\s*", "", final_content, flags=re.I)
final_content = re.sub(r"--- End (Document|Skill|Note|Artefact)(?:: .*?)? ---\s*", "", final_content, flags=re.I)
discussion.discussion_data_zone = final_content.strip()
discussion.commit()
task.set_progress(100)
task.log("Data zone cleaned and re-formatted.")
return {"discussion_id": discussion_id, "new_content": final_content, "zone": "discussion"}
def _generate_message_audio_task(task: Task, username: str, discussion_id: str, message_id: str):
task.log(f"Starting background audio generation for message: {message_id}")
task.set_progress(10)
try:
from backend.session import get_user_data_root, build_lollms_client_from_params
from backend.discussion import get_user_discussion
# 1. Fetch the message content
discussion = get_user_discussion(username, discussion_id)
if not discussion:
raise ValueError("Discussion not found.")
message = discussion.get_message(message_id)
if not message:
raise ValueError("Message not found.")
# Clean text: remove markdown artifacts and thinking blocks
text = message.content
# Remove thinking blocks if present
text = re.sub(r'<think>[\s\S]*?<\/think>', '', text)
# Strip common markdown
clean_text = text.replace('#', '').replace('*', '').replace('```', '').strip()
if not clean_text:
raise ValueError("Message content is empty after cleaning.")
# 2. Init Client and generate audio
lc = build_lollms_client_from_params(username=username, load_llm=False, load_tts=True)
if not lc.tts:
raise Exception("TTS Service is not configured or available.")
task.set_progress(30)
task.log("Communicating with TTS Engine...")
audio_bytes = lc.tts.generate_audio(clean_text)
task.set_progress(80)
task.log("Generation complete. Saving file...")
# 3. Save to user's generated media folder
output_dir = get_user_data_root(username) / "generated_audio"
output_dir.mkdir(parents=True, exist_ok=True)
filename = f"msg_{message_id}_{datetime.now().strftime('%H%M%S')}.wav"
file_path = output_dir / filename
file_path.write_bytes(audio_bytes)
# 4. Update Message Metadata in DB so it persists
audio_url = f"/api/files/generated_audio/{filename}"
message.set_metadata_item('audio_url', audio_url, discussion)
discussion.commit()
task.set_progress(100)
task.log(f"Success. Audio linked to message.")
return {
"status": "ready",
"message_id": message_id,
"audio_url": audio_url
}
except Exception as e:
task.log(f"Message audio generation failed: {str(e)}", "ERROR")
trace_exception(e)
raise e
def _prune_empty_discussions_task(task: Task, username: str):
task.log("Starting prune of empty and single-message discussions.")
dm = get_user_discussion_manager(username)
all_discs_infos = dm.list_discussions()
total_discussions = len(all_discs_infos)
discussions_to_delete = []
task.log(f"Scanning {total_discussions} discussions for user '{username}'.")
discussion_db_session = dm.get_session()
try:
for i, disc_info in enumerate(all_discs_infos):
if task.cancellation_event.is_set():
task.log("Scan for prunable discussions cancelled.", level="WARNING")
break
discussion_id = disc_info['id']
try:
message_count = discussion_db_session.query(dm.MessageModel).filter(dm.MessageModel.discussion_id == discussion_id).count()
if message_count <= 1:
discussions_to_delete.append(discussion_id)
except Exception as e:
task.log(f"Could not process discussion {discussion_id} for pruning: {e}", level="WARNING")
progress = int(50 * (i + 1) / total_discussions) if total_discussions > 0 else 50
task.set_progress(progress)
finally:
discussion_db_session.close()
if task.cancellation_event.is_set():
return {"message": "Pruning task cancelled during scan.", "deleted_count": 0}
if not discussions_to_delete:
task.log("No empty discussions found to prune.")
task.set_progress(100)
return {"message": "Pruning complete. No empty discussions found.", "deleted_count": 0}
task.log(f"Found {len(discussions_to_delete)} discussions to delete. Starting deletion process...")
task.set_progress(50)
deleted_count = 0
failed_count = 0
with task.db_session_factory() as db:
try:
db_user = db.query(DBUser).filter(DBUser.username == username).one()
for discussion_id in discussions_to_delete:
if task.cancellation_event.is_set():
break
try:
dm.delete_discussion(discussion_id)
assets_path = get_user_discussion_assets_path(username) / discussion_id
if assets_path.exists() and assets_path.is_dir():
shutil.rmtree(assets_path, ignore_errors=True)
db.query(UserStarredDiscussion).filter_by(user_id=db_user.id, discussion_id=discussion_id).delete(synchronize_session=False)
db.query(UserMessageGrade).filter_by(user_id=db_user.id, discussion_id=discussion_id).delete(synchronize_session=False)
deleted_count += 1
except Exception as e:
print(f"ERROR: Failed to delete discussion {discussion_id} during prune: {e}")
failed_count += 1
db.commit()
task.set_progress(100)
if deleted_count > 0:
task.log(f"Successfully pruned {deleted_count} discussions.")
if failed_count > 0:
task.log(f"Failed to prune {failed_count} discussions. Check server console for details.", level="ERROR")
if task.cancellation_event.is_set():
task.log(f"Pruning cancelled after deleting {deleted_count} discussions.", level="WARNING")
return {"message": f"Pruning complete. Deleted: {deleted_count}, Failed: {failed_count}.", "deleted_count": deleted_count, "failed_count": failed_count}
except Exception as e:
db.rollback()
task.log(f"A critical database error occurred during the commit phase: {e}", level="CRITICAL")
raise e

Xet Storage Details

Size:
17.4 kB
·
Xet hash:
eaf8e9b9026c25787feea68b7ef2167223d22f6c3ceb8f9a10c3c7f5e911cf25

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.