Skip to content

Commit 94de23f

Browse files
rkuo-danswerRichard Kuo (Onyx)
andauthored
Bugfix/chat images 2 (#4630)
* don't hardcode -1 * extra spaces * fix binary data in blurb * add note to binary handling --------- Co-authored-by: Richard Kuo (Onyx) <rkuo@onyx.app>
1 parent dd242c9 commit 94de23f

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
lines changed

backend/onyx/chat/process_message.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@
9696
from onyx.file_store.models import ChatFileType
9797
from onyx.file_store.models import FileDescriptor
9898
from onyx.file_store.models import InMemoryChatFile
99+
from onyx.file_store.utils import get_user_files
99100
from onyx.file_store.utils import load_all_chat_files
100-
from onyx.file_store.utils import load_all_user_file_files
101-
from onyx.file_store.utils import load_all_user_files
101+
from onyx.file_store.utils import load_in_memory_chat_files
102102
from onyx.file_store.utils import save_files
103103
from onyx.llm.exceptions import GenAIDisabledException
104104
from onyx.llm.factory import get_llms_for_persona
@@ -849,12 +849,12 @@ def stream_chat_message_objects(
849849
user_file_files: list[UserFile] | None = None
850850
if user_file_ids or user_folder_ids:
851851
# Load user files
852-
user_files = load_all_user_files(
852+
user_files = load_in_memory_chat_files(
853853
user_file_ids or [],
854854
user_folder_ids or [],
855855
db_session,
856856
)
857-
user_file_files = load_all_user_file_files(
857+
user_file_files = get_user_files(
858858
user_file_ids or [],
859859
user_folder_ids or [],
860860
db_session,

backend/onyx/chat/prune_and_merge.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def _apply_pruning(
155155

156156
section_idx_token_count: dict[int, int] = {}
157157

158+
ind = 0
158159
final_section_ind = None
159160
total_tokens = 0
160161
for ind, section in enumerate(sections):

backend/onyx/db/chat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,10 @@ def create_search_doc_from_user_file(
870870
content_sample = associated_chat_file.content[:100]
871871
# Remove null bytes which can cause SQL errors
872872
content_sample = content_sample.replace(b"\x00", b"")
873-
blurb = content_sample.decode("utf-8", errors="replace")
873+
874+
# NOTE(rkuo): this used to be "replace" instead of strict, but
875+
# that would bypass the binary handling below
876+
blurb = content_sample.decode("utf-8", errors="strict")
874877
except Exception:
875878
# If decoding fails completely, provide a generic description
876879
blurb = f"[Binary file: {db_user_file.name}]"

backend/onyx/file_store/utils.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,32 @@ def load_user_file(file_id: int, db_session: Session) -> InMemoryChatFile:
157157
)
158158

159159

160-
def load_all_user_files(
160+
def load_in_memory_chat_files(
161161
user_file_ids: list[int],
162162
user_folder_ids: list[int],
163163
db_session: Session,
164164
) -> list[InMemoryChatFile]:
165+
"""
166+
Loads the actual content of user files specified by individual IDs and those
167+
within specified folder IDs into memory.
168+
169+
Args:
170+
user_file_ids: A list of specific UserFile IDs to load.
171+
user_folder_ids: A list of UserFolder IDs. All UserFiles within these folders will be loaded.
172+
db_session: The SQLAlchemy database session.
173+
174+
Returns:
175+
A list of InMemoryChatFile objects, each containing the file content (as bytes),
176+
file ID, file type, and filename. Prioritizes loading plaintext versions if available.
177+
"""
178+
# Use parallel execution to load files concurrently
165179
return cast(
166180
list[InMemoryChatFile],
167181
run_functions_tuples_in_parallel(
182+
# 1. Load files specified by individual IDs
168183
[(load_user_file, (file_id, db_session)) for file_id in user_file_ids]
169184
)
185+
# 2. Load all files within specified folders
170186
+ [
171187
file
172188
for folder_id in user_folder_ids
@@ -175,24 +191,47 @@ def load_all_user_files(
175191
)
176192

177193

178-
def load_all_user_file_files(
194+
def get_user_files(
179195
user_file_ids: list[int],
180196
user_folder_ids: list[int],
181197
db_session: Session,
182198
) -> list[UserFile]:
199+
"""
200+
Fetches UserFile database records based on provided file and folder IDs.
201+
202+
Args:
203+
user_file_ids: A list of specific UserFile IDs to fetch.
204+
user_folder_ids: A list of UserFolder IDs. All UserFiles within these folders will be fetched.
205+
db_session: The SQLAlchemy database session.
206+
207+
Returns:
208+
A list containing UserFile SQLAlchemy model objects corresponding to the
209+
specified file IDs and all files within the specified folder IDs.
210+
It does NOT return the actual file content.
211+
"""
183212
user_files: list[UserFile] = []
213+
214+
# 1. Fetch UserFile records for specific file IDs
184215
for user_file_id in user_file_ids:
216+
# Query the database for a UserFile with the matching ID
185217
user_file = (
186218
db_session.query(UserFile).filter(UserFile.id == user_file_id).first()
187219
)
220+
# If found, add it to the list
188221
if user_file is not None:
189222
user_files.append(user_file)
223+
224+
# 2. Fetch UserFile records for all files within specified folder IDs
190225
for user_folder_id in user_folder_ids:
226+
# Query the database for all UserFiles belonging to the current folder ID
227+
# and extend the list with the results
191228
user_files.extend(
192229
db_session.query(UserFile)
193230
.filter(UserFile.folder_id == user_folder_id)
194231
.all()
195232
)
233+
234+
# 3. Return the combined list of UserFile database objects
196235
return user_files
197236

198237

backend/onyx/server/user_documents/api.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from onyx.server.documents.connector import trigger_indexing_for_cc_pair
4343
from onyx.server.documents.models import ConnectorBase
4444
from onyx.server.documents.models import CredentialBase
45+
from onyx.server.query_and_chat.chat_backend import RECENT_DOCS_FOLDER_ID
4546
from onyx.server.user_documents.models import MessageResponse
4647
from onyx.server.user_documents.models import UserFileSnapshot
4748
from onyx.server.user_documents.models import UserFolderSnapshot
@@ -141,9 +142,6 @@ def get_folder(
141142
return folder_snapshot
142143

143144

144-
RECENT_DOCS_FOLDER_ID = -1
145-
146-
147145
@router.post("/user/file/upload")
148146
def upload_user_files(
149147
files: List[UploadFile] = File(...),
@@ -157,7 +155,7 @@ def upload_user_files(
157155
try:
158156
# Use our consolidated function that handles indexing properly
159157
user_files = upload_files_to_user_files_with_indexing(
160-
files, folder_id or -1, user, db_session
158+
files, folder_id or RECENT_DOCS_FOLDER_ID, user, db_session
161159
)
162160

163161
return [UserFileSnapshot.from_model(user_file) for user_file in user_files]

web/src/components/chat/sources/SourceCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function SeeMoreBlock({
125125
<button
126126
onClick={toggleDocumentSelection}
127127
className={`w-full ${fullWidth ? "w-full" : "max-w-[200px]"}
128-
h-[80px] p-3 border border-[1.5px] border-new-background-light text-left bg-accent-background hover:bg-accent-background-hovered dark:bg-accent-background-hovered dark:hover:bg-neutral-700/80 cursor-pointer rounded-lg flex flex-col justify-between overflow-hidden`}
128+
h-[80px] p-3 border border-[1.5px] border-new-background-light text-left bg-accent-background hover:bg-accent-background-hovered dark:bg-accent-background-hovered dark:hover:bg-neutral-700/80 cursor-pointer rounded-lg flex flex-col justify-between overflow-hidden`}
129129
>
130130
<div className="flex items-center gap-1">
131131
{docs.length > 2 && iconsToRender.map((icon, index) => icon)}
@@ -204,7 +204,7 @@ export function FilesSeeMoreBlock({
204204
<button
205205
onClick={toggleDocumentSelection}
206206
className={`w-full ${fullWidth ? "w-full" : "max-w-[200px]"}
207-
h-[80px] p-3 border border-[1.5px] border-new-background-light text-left bg-accent-background hover:bg-accent-background-hovered dark:bg-accent-background-hovered dark:hover:bg-neutral-700/80 cursor-pointer rounded-lg flex flex-col justify-between overflow-hidden`}
207+
h-[80px] p-3 border border-[1.5px] border-new-background-light text-left bg-accent-background hover:bg-accent-background-hovered dark:bg-accent-background-hovered dark:hover:bg-neutral-700/80 cursor-pointer rounded-lg flex flex-col justify-between overflow-hidden`}
208208
>
209209
<div className="flex items-center gap-1">
210210
{files.length > 2 && iconsToRender.map((icon, index) => icon)}

0 commit comments

Comments
 (0)