Skip to content

Commit d492337

Browse files
authored
Merge pull request #21 from OpenPecha/search-changes
Search: content of search fetch from backend
2 parents 32307e9 + 65cc56d commit d492337

File tree

3 files changed

+30
-109
lines changed

3 files changed

+30
-109
lines changed

api/Assistant/assistant_service.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from api.db.pg_database import SessionLocal
33
from api.Assistant.assistant_repository import get_all_assistants, get_assistant_by_id_repository, delete_assistant_repository, update_assistant_repository
44
from api.Assistant.assistant_response_model import AssistantRequest, AssistantResponse, AssistantInfoResponse, AssistantListItemResponse, ContextResponse, UpdateAssistantRequest
5+
from api.search.search_service import get_search_texts_details
56
from api.Assistant.assistant_repository import create_assistant_repository
67
from typing import List
78
from api.Assistant.assistant_model import Assistant, Context
@@ -75,10 +76,14 @@ async def create_assistant_service(token: str, assistant_request: AssistantReque
7576
current_user_email = validate_and_extract_user_email(token=token)
7677
contexts_list = []
7778
for ctx in assistant_request.contexts:
79+
if ctx.pecha_text_id:
80+
search_details = await get_search_texts_details(ctx.pecha_text_id)
81+
content = "\n\n".join([detail.content for detail in search_details]) if search_details else ""
82+
else:
83+
content = ctx.content
7884
contexts_list.append(
79-
Context(content=ctx.content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id)
85+
Context(content=content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id)
8086
)
81-
8287
if files:
8388
for file in files:
8489
if file.filename:
@@ -159,10 +164,17 @@ async def update_assistant_service(assistant_id: UUID, update_request: UpdateAss
159164
if update_request.contexts is not None:
160165
for context in assistant.contexts:
161166
db_session.delete(context)
162-
assistant.contexts = [
163-
Context(content=ctx.content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id)
164-
for ctx in update_request.contexts
165-
]
167+
new_contexts = []
168+
for ctx in update_request.contexts:
169+
if ctx.pecha_text_id:
170+
search_details = await get_search_texts_details(ctx.pecha_text_id)
171+
content = "\n\n".join([detail.content for detail in search_details]) if search_details else ""
172+
else:
173+
content = ctx.content
174+
new_contexts.append(
175+
Context(content=content, pecha_title=ctx.pecha_title, pecha_text_id=ctx.pecha_text_id)
176+
)
177+
assistant.contexts = new_contexts
166178

167179
assistant.updated_at = datetime.now(timezone.utc)
168180

api/search/search_view.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
tags=["search"]
1111
)
1212

13-
1413
@search_router.get("/{text_id}", status_code=status.HTTP_200_OK, response_model=list[SearchTextsDetailsResponse])
1514
async def read_texts_details(
1615
text_id: str

api/ui.py

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -927,20 +927,6 @@ async def serve_ui():
927927
renderFileField(fieldArea, '');
928928
} else if (selectedType === 'search') {
929929
renderSearchField(fieldArea, data.pecha_title || '', data.pecha_text_id || '');
930-
// If editing and has content, create a mock search result with the content
931-
if (data.content && data.pecha_title && data.pecha_text_id) {
932-
const contentDataScript = document.createElement('script');
933-
contentDataScript.type = 'application/json';
934-
contentDataScript.className = 'ctx-pecha-content-data';
935-
// Create a single item array with the content
936-
contentDataScript.textContent = JSON.stringify([{
937-
id: data.pecha_text_id,
938-
content: data.content,
939-
type: 'text',
940-
source: 'pecha'
941-
}]);
942-
fieldArea.appendChild(contentDataScript);
943-
}
944930
}
945931
}
946932
@@ -1090,74 +1076,27 @@ async def serve_ui():
10901076
const titleText = el.textContent.trim();
10911077
const displayTitle = titleText.length > 80 ? titleText.slice(0,80) + '...' : titleText;
10921078
1093-
// Show loading state
1094-
el.innerHTML = '<span class="spinner" style="width:12px;height:12px;border-width:2px"></span> Loading content...';
1095-
el.style.pointerEvents = 'none';
1079+
titleInput.value = displayTitle;
1080+
idInput.value = id;
10961081
1097-
try {
1098-
// Call the search endpoint to fetch content
1099-
const r = await fetch(API_BASE + '/search/' + encodeURIComponent(id));
1100-
if (!r.ok) throw new Error('Failed to fetch content');
1101-
const searchData = await r.json();
1102-
1103-
// Store both the title and the fetched content
1104-
titleInput.value = displayTitle;
1105-
idInput.value = id;
1106-
1107-
// Store content data as JSON in a script tag to avoid HTML escaping issues
1108-
let contentScript = fieldArea.querySelector('.ctx-pecha-content-data');
1109-
if (!contentScript) {
1110-
contentScript = document.createElement('script');
1111-
contentScript.type = 'application/json';
1112-
contentScript.className = 'ctx-pecha-content-data';
1113-
fieldArea.appendChild(contentScript);
1114-
}
1115-
contentScript.textContent = JSON.stringify(searchData || []);
1116-
1117-
resultsDiv.style.display = 'none';
1118-
tagsDiv.innerHTML = '';
1119-
addPechaTag(tagsDiv, fieldArea, displayTitle, id, searchData);
1120-
1121-
toast('Content loaded successfully! (' + (searchData?.length || 0) + ' items)', 'success');
1122-
} catch(e) {
1123-
toast('Error loading content: ' + e.message, 'error');
1124-
el.innerHTML = displayTitle;
1125-
el.style.pointerEvents = '';
1126-
}
1082+
resultsDiv.style.display = 'none';
1083+
tagsDiv.innerHTML = '';
1084+
addPechaTag(tagsDiv, fieldArea, displayTitle, id, null);
1085+
1086+
toast('Pecha text selected!', 'success');
11271087
}
11281088
11291089
function addPechaTag(tagsDiv, fieldArea, title, id, searchData) {
11301090
const tag = document.createElement('div');
11311091
tag.style.cssText = 'margin-top:8px';
11321092
1133-
// Add main tag
11341093
tag.innerHTML = `
11351094
<div class="pecha-tag">
11361095
<span class="pecha-tag-text">${esc(title)}</span>
11371096
<button class="pecha-tag-remove" onclick="removePechaTag(this)">&times;</button>
11381097
</div>
11391098
`;
11401099
1141-
// If we have search data, show the content items
1142-
if (searchData && searchData.length > 0) {
1143-
const contentList = document.createElement('div');
1144-
contentList.style.cssText = 'margin-top:8px;display:flex;flex-direction:column;gap:8px';
1145-
1146-
searchData.forEach((item, idx) => {
1147-
const contentPreview = (item.content || '').substring(0, 150);
1148-
const itemDiv = document.createElement('div');
1149-
itemDiv.style.cssText = 'padding:8px 10px;background:var(--bg-input);border:1px solid var(--border);border-radius:var(--radius-xs);font-size:12px';
1150-
itemDiv.innerHTML = `
1151-
<div style="color:var(--text-secondary);line-height:1.5">
1152-
${esc(contentPreview)}${contentPreview.length >= 150 ? '...' : ''}
1153-
</div>
1154-
`;
1155-
contentList.appendChild(itemDiv);
1156-
});
1157-
1158-
tag.appendChild(contentList);
1159-
}
1160-
11611100
tagsDiv.appendChild(tag);
11621101
}
11631102
@@ -1167,12 +1106,6 @@ async def serve_ui():
11671106
fieldArea.querySelector('.ctx-pecha-title').value = '';
11681107
fieldArea.querySelector('.ctx-pecha-text-id').value = '';
11691108
1170-
// Remove the content data script if it exists
1171-
const contentDataScript = fieldArea.querySelector('.ctx-pecha-content-data');
1172-
if (contentDataScript) {
1173-
contentDataScript.remove();
1174-
}
1175-
11761109
// Clear the pecha tags div
11771110
const tagsDiv = fieldArea.querySelector('.pecha-tags');
11781111
if (tagsDiv) {
@@ -1191,36 +1124,13 @@ async def serve_ui():
11911124
} else if (type === 'search') {
11921125
const pecha_title = e.querySelector('.ctx-pecha-title')?.value.trim();
11931126
const pecha_text_id = e.querySelector('.ctx-pecha-text-id')?.value.trim();
1194-
const contentDataScript = e.querySelector('.ctx-pecha-content-data');
11951127
11961128
if (pecha_title && pecha_text_id) {
1197-
// Parse the stored JSON data
1198-
let searchData = [];
1199-
if (contentDataScript) {
1200-
try {
1201-
searchData = JSON.parse(contentDataScript.textContent || '[]');
1202-
} catch(err) {
1203-
console.error('Failed to parse search data', err);
1204-
}
1205-
}
1206-
1207-
// Create separate context entries for each search result
1208-
if (searchData.length > 0) {
1209-
searchData.forEach(item => {
1210-
contexts.push({
1211-
content: item.content || null,
1212-
pecha_title: pecha_title,
1213-
pecha_text_id: pecha_text_id
1214-
});
1215-
});
1216-
} else {
1217-
// No content loaded yet, just store the metadata
1218-
contexts.push({
1219-
content: null,
1220-
pecha_title: pecha_title,
1221-
pecha_text_id: pecha_text_id
1222-
});
1223-
}
1129+
contexts.push({
1130+
content: null,
1131+
pecha_title: pecha_title,
1132+
pecha_text_id: pecha_text_id
1133+
});
12241134
}
12251135
}
12261136
// Note: 'file' type contexts are handled separately via getFilesFromForm()

0 commit comments

Comments
 (0)