diff --git a/api/Assistant/assistant_service.py b/api/Assistant/assistant_service.py index 914bd99..41b2649 100644 --- a/api/Assistant/assistant_service.py +++ b/api/Assistant/assistant_service.py @@ -2,6 +2,7 @@ from api.db.pg_database import SessionLocal from api.Assistant.assistant_repository import get_all_assistants, get_assistant_by_id_repository, delete_assistant_repository, update_assistant_repository from api.Assistant.assistant_response_model import AssistantRequest, AssistantResponse, AssistantInfoResponse, AssistantListItemResponse, ContextResponse, UpdateAssistantRequest +from api.search.search_service import get_search_texts_details from api.Assistant.assistant_repository import create_assistant_repository from typing import List from api.Assistant.assistant_model import Assistant, Context @@ -75,10 +76,14 @@ async def create_assistant_service(token: str, assistant_request: AssistantReque current_user_email = validate_and_extract_user_email(token=token) contexts_list = [] for ctx in assistant_request.contexts: + if ctx.pecha_text_id: + search_details = await get_search_texts_details(ctx.pecha_text_id) + content = "\n\n".join([detail.content for detail in search_details]) if search_details else "" + else: + content = ctx.content contexts_list.append( - Context(content=ctx.content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id) + Context(content=content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id) ) - if files: for file in files: if file.filename: @@ -159,10 +164,17 @@ async def update_assistant_service(assistant_id: UUID, update_request: UpdateAss if update_request.contexts is not None: for context in assistant.contexts: db_session.delete(context) - assistant.contexts = [ - Context(content=ctx.content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id) - for ctx in update_request.contexts - ] + new_contexts = [] + for ctx in update_request.contexts: + if ctx.pecha_text_id: + search_details = await get_search_texts_details(ctx.pecha_text_id) + content = "\n\n".join([detail.content for detail in search_details]) if search_details else "" + else: + content = ctx.content + new_contexts.append( + Context(content=content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id) + ) + assistant.contexts = new_contexts assistant.updated_at = datetime.now(timezone.utc) diff --git a/api/search/search_view.py b/api/search/search_view.py index 0b39984..3805c7a 100644 --- a/api/search/search_view.py +++ b/api/search/search_view.py @@ -10,7 +10,6 @@ tags=["search"] ) - @search_router.get("/{text_id}", status_code=status.HTTP_200_OK, response_model=list[SearchTextsDetailsResponse]) async def read_texts_details( text_id: str diff --git a/api/ui.py b/api/ui.py index 1a8131d..5609ceb 100644 --- a/api/ui.py +++ b/api/ui.py @@ -927,20 +927,6 @@ async def serve_ui(): renderFileField(fieldArea, ''); } else if (selectedType === 'search') { renderSearchField(fieldArea, data.pecha_title || '', data.pecha_text_id || ''); - // If editing and has content, create a mock search result with the content - if (data.content && data.pecha_title && data.pecha_text_id) { - const contentDataScript = document.createElement('script'); - contentDataScript.type = 'application/json'; - contentDataScript.className = 'ctx-pecha-content-data'; - // Create a single item array with the content - contentDataScript.textContent = JSON.stringify([{ - id: data.pecha_text_id, - content: data.content, - type: 'text', - source: 'pecha' - }]); - fieldArea.appendChild(contentDataScript); - } } } @@ -1090,47 +1076,20 @@ async def serve_ui(): const titleText = el.textContent.trim(); const displayTitle = titleText.length > 80 ? titleText.slice(0,80) + '...' : titleText; - // Show loading state - el.innerHTML = ' Loading content...'; - el.style.pointerEvents = 'none'; + titleInput.value = displayTitle; + idInput.value = id; - try { - // Call the search endpoint to fetch content - const r = await fetch(API_BASE + '/search/' + encodeURIComponent(id)); - if (!r.ok) throw new Error('Failed to fetch content'); - const searchData = await r.json(); - - // Store both the title and the fetched content - titleInput.value = displayTitle; - idInput.value = id; - - // Store content data as JSON in a script tag to avoid HTML escaping issues - let contentScript = fieldArea.querySelector('.ctx-pecha-content-data'); - if (!contentScript) { - contentScript = document.createElement('script'); - contentScript.type = 'application/json'; - contentScript.className = 'ctx-pecha-content-data'; - fieldArea.appendChild(contentScript); - } - contentScript.textContent = JSON.stringify(searchData || []); - - resultsDiv.style.display = 'none'; - tagsDiv.innerHTML = ''; - addPechaTag(tagsDiv, fieldArea, displayTitle, id, searchData); - - toast('Content loaded successfully! (' + (searchData?.length || 0) + ' items)', 'success'); - } catch(e) { - toast('Error loading content: ' + e.message, 'error'); - el.innerHTML = displayTitle; - el.style.pointerEvents = ''; - } + resultsDiv.style.display = 'none'; + tagsDiv.innerHTML = ''; + addPechaTag(tagsDiv, fieldArea, displayTitle, id, null); + + toast('Pecha text selected!', 'success'); } function addPechaTag(tagsDiv, fieldArea, title, id, searchData) { const tag = document.createElement('div'); tag.style.cssText = 'margin-top:8px'; - // Add main tag tag.innerHTML = `