minhvtt commited on
Commit
5506ecb
·
verified ·
1 Parent(s): a554f90

Upload 9 files

Browse files
Files changed (2) hide show
  1. core.py +21 -1
  2. services_legacy.py +26 -6
core.py CHANGED
@@ -8,7 +8,7 @@ from fastapi.encoders import ENCODERS_BY_TYPE
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.staticfiles import StaticFiles
10
  from google import genai
11
- from pymongo import MongoClient
12
 
13
  app = FastAPI(title="Nomus AI Agent Calendar V3 – Streaming + Proposals")
14
 
@@ -41,6 +41,26 @@ team_chat_collection = db.team_chat_history
41
  team_docs_collection = db.team_documents
42
  team_doc_chunks_collection = db.team_document_chunks
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  UPLOAD_DIR = os.environ.get("UPLOAD_DIR", os.path.join(os.path.dirname(__file__), "uploads"))
45
  os.makedirs(UPLOAD_DIR, exist_ok=True)
46
  app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
 
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.staticfiles import StaticFiles
10
  from google import genai
11
+ from pymongo import ASCENDING, MongoClient
12
 
13
  app = FastAPI(title="Nomus AI Agent Calendar V3 – Streaming + Proposals")
14
 
 
41
  team_docs_collection = db.team_documents
42
  team_doc_chunks_collection = db.team_document_chunks
43
 
44
+
45
+ def _ensure_indexes() -> None:
46
+ try:
47
+ team_doc_chunks_collection.create_index(
48
+ [("doc_id", ASCENDING), ("chunk_index", ASCENDING)],
49
+ name="idx_team_doc_chunks_doc_id_chunk",
50
+ background=True,
51
+ )
52
+ team_docs_collection.create_index(
53
+ [("team_id", ASCENDING), ("project_id", ASCENDING), ("updated_at", ASCENDING)],
54
+ name="idx_team_docs_team_project_updated",
55
+ background=True,
56
+ )
57
+ except Exception:
58
+ # Index creation failure should not block app startup.
59
+ pass
60
+
61
+
62
+ _ensure_indexes()
63
+
64
  UPLOAD_DIR = os.environ.get("UPLOAD_DIR", os.path.join(os.path.dirname(__file__), "uploads"))
65
  os.makedirs(UPLOAD_DIR, exist_ok=True)
66
  app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
services_legacy.py CHANGED
@@ -23,6 +23,7 @@ import whisper
23
  from fastapi import HTTPException
24
  from google.genai import types
25
  from PIL import Image, ImageOps
 
26
  from transformers import AutoTokenizer, VitsModel
27
 
28
  from core import (
@@ -736,12 +737,31 @@ def _store_team_document_chunks(doc_id: str, nodes: List[Dict[str, Any]], create
736
 
737
 
738
  def _load_team_document_nodes(doc_id: str) -> List[Dict[str, Any]]:
739
- rows = list(
740
- team_doc_chunks_collection.find(
741
- {"doc_id": doc_id},
742
- {"_id": 0, "nodes": 1},
743
- ).sort("chunk_index", 1)
744
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
  nodes: List[Dict[str, Any]] = []
746
  for row in rows:
747
  chunk_nodes = row.get("nodes")
 
23
  from fastapi import HTTPException
24
  from google.genai import types
25
  from PIL import Image, ImageOps
26
+ from pymongo.errors import OperationFailure
27
  from transformers import AutoTokenizer, VitsModel
28
 
29
  from core import (
 
737
 
738
 
739
  def _load_team_document_nodes(doc_id: str) -> List[Dict[str, Any]]:
740
+ normalized_doc_id = str(doc_id or "").strip()
741
+ if not normalized_doc_id:
742
+ return []
743
+
744
+ projection = {"_id": 0, "chunk_index": 1, "nodes": 1}
745
+ try:
746
+ # Prefer index-backed sort. If index is not ready yet, fallback below.
747
+ cursor = team_doc_chunks_collection.find(
748
+ {"doc_id": normalized_doc_id},
749
+ projection,
750
+ ).hint("idx_team_doc_chunks_doc_id_chunk").sort("chunk_index", 1)
751
+
752
+ if hasattr(cursor, "allow_disk_use"):
753
+ cursor = cursor.allow_disk_use(True)
754
+
755
+ rows = list(cursor)
756
+ except (OperationFailure, ValueError):
757
+ rows = list(
758
+ team_doc_chunks_collection.find(
759
+ {"doc_id": normalized_doc_id},
760
+ projection,
761
+ )
762
+ )
763
+ rows.sort(key=lambda row: int(row.get("chunk_index", 0)))
764
+
765
  nodes: List[Dict[str, Any]] = []
766
  for row in rows:
767
  chunk_nodes = row.get("nodes")