Charles Grandjean commited on
Commit
e0a9bd6
Β·
1 Parent(s): c38da96

fix create draft

Browse files
Files changed (1) hide show
  1. utils/tools.py +24 -22
utils/tools.py CHANGED
@@ -13,6 +13,8 @@ from agents.lawyer_messenger import LawyerMessengerAgent
13
  from utils.lightrag_client import LightRAGClient, get_lightrag_client, validate_jurisdiction, get_available_jurisdictions
14
  import resend
15
  import logging
 
 
16
  from bs4 import BeautifulSoup
17
 
18
  logger = logging.getLogger(__name__)
@@ -119,7 +121,6 @@ async def search_web(query: str) -> str:
119
  raise ValueError("TavilySearch not initialized in agent_api.py")
120
 
121
  result = await tavily_search.ainvoke({"query": query})
122
- import json
123
  data = json.loads(result) if isinstance(result, str) else result
124
 
125
  output = ["🌐 WEB SEARCH RESULTS", "=" * 80]
@@ -297,8 +298,6 @@ async def _retrieve_lawyer_document(
297
  Document content including extracted_text, summary, actors, and key_details
298
  """
299
  try:
300
- import httpx
301
-
302
  # Check configuration from environment
303
  base_url = os.getenv("SUPABASE_BASE_URL")
304
  cyberlgl_api_key = os.getenv("CYBERLGL_API_KEY")
@@ -349,7 +348,7 @@ async def _retrieve_lawyer_document(
349
 
350
  return "\n".join(output)
351
 
352
- except httpx.TimeoutError:
353
  return "Error: Timeout while retrieving document"
354
  except httpx.RequestError as e:
355
  return f"Error: Failed to connect to document server: {str(e)}"
@@ -454,9 +453,8 @@ async def _create_draft_document(
454
 
455
  msg, document_id = await _create_draft_document_impl(
456
  user_id=user_id,
457
- title=title,
458
  content=empty_html,
459
- path=path
460
  )
461
 
462
  if not document_id:
@@ -534,7 +532,6 @@ async def _create_draft_document(
534
 
535
  async def _create_draft_document_impl(
536
  user_id: str,
537
- title: str,
538
  content: str,
539
  path: str
540
  ) -> tuple[str, str]:
@@ -543,31 +540,36 @@ async def _create_draft_document_impl(
543
 
544
  Args:
545
  user_id: User UUID
546
- title: Document title
547
  content: Document HTML content
548
- path: Folder path
549
 
550
  Returns:
551
  tuple: (message, document_id) - Success/failure message and document_id (or None on error)
552
  """
553
  try:
554
- import httpx
555
-
556
  # Check configuration from environment
557
  base_url = os.getenv("SUPABASE_BASE_URL")
558
  cyberlgl_api_key = os.getenv("CYBERLGL_API_KEY")
559
 
560
- if path:
561
- # Remove leading ./ if present
562
- if path.startswith('./'):
563
- path = path[2:]
564
- # Ensure trailing /
565
- if not path.endswith('/'):
566
- path += '/'
 
 
 
 
 
 
 
 
 
 
567
  else:
568
- path = ''
569
-
570
- full_path = f"./{path}{title}.pdf"
571
 
572
  # URL already has /functions/v1/ (correct)
573
  endpoint_url = f"{base_url}/functions/v1/create-document-from-html"
@@ -604,7 +606,7 @@ async def _create_draft_document_impl(
604
  else:
605
  return f"❌ Error: HTTP {response.status_code} - {response.text}", None
606
 
607
- except httpx.TimeoutError:
608
  return "❌ Error: Timeout while saving document", None
609
  except httpx.RequestError as e:
610
  return f"❌ Error: Failed to connect to document server: {str(e)}", None
 
13
  from utils.lightrag_client import LightRAGClient, get_lightrag_client, validate_jurisdiction, get_available_jurisdictions
14
  import resend
15
  import logging
16
+
17
+ import httpx
18
  from bs4 import BeautifulSoup
19
 
20
  logger = logging.getLogger(__name__)
 
121
  raise ValueError("TavilySearch not initialized in agent_api.py")
122
 
123
  result = await tavily_search.ainvoke({"query": query})
 
124
  data = json.loads(result) if isinstance(result, str) else result
125
 
126
  output = ["🌐 WEB SEARCH RESULTS", "=" * 80]
 
298
  Document content including extracted_text, summary, actors, and key_details
299
  """
300
  try:
 
 
301
  # Check configuration from environment
302
  base_url = os.getenv("SUPABASE_BASE_URL")
303
  cyberlgl_api_key = os.getenv("CYBERLGL_API_KEY")
 
348
 
349
  return "\n".join(output)
350
 
351
+ except httpx.TimeoutException:
352
  return "Error: Timeout while retrieving document"
353
  except httpx.RequestError as e:
354
  return f"Error: Failed to connect to document server: {str(e)}"
 
453
 
454
  msg, document_id = await _create_draft_document_impl(
455
  user_id=user_id,
 
456
  content=empty_html,
457
+ path=path_with_filename
458
  )
459
 
460
  if not document_id:
 
532
 
533
  async def _create_draft_document_impl(
534
  user_id: str,
 
535
  content: str,
536
  path: str
537
  ) -> tuple[str, str]:
 
540
 
541
  Args:
542
  user_id: User UUID
 
543
  content: Document HTML content
544
+ path: Full path with filename (with or without extension)
545
 
546
  Returns:
547
  tuple: (message, document_id) - Success/failure message and document_id (or None on error)
548
  """
549
  try:
 
 
550
  # Check configuration from environment
551
  base_url = os.getenv("SUPABASE_BASE_URL")
552
  cyberlgl_api_key = os.getenv("CYBERLGL_API_KEY")
553
 
554
+ # Extract filename (last part) and folder (everything before)
555
+ filename = os.path.basename(path)
556
+ folder = os.path.dirname(path)
557
+
558
+ # Remove extension if present
559
+ if '.' in filename:
560
+ filename = filename.rsplit('.', 1)[0]
561
+
562
+ # Build folder path with ./ prefix if needed
563
+ if folder and not folder.startswith('./'):
564
+ folder = f"./{folder}"
565
+ elif not folder:
566
+ folder = '.'
567
+
568
+ # Build full path
569
+ if folder == '.':
570
+ full_path = f"./{filename}.pdf"
571
  else:
572
+ full_path = f"{folder}/{filename}.pdf"
 
 
573
 
574
  # URL already has /functions/v1/ (correct)
575
  endpoint_url = f"{base_url}/functions/v1/create-document-from-html"
 
606
  else:
607
  return f"❌ Error: HTTP {response.status_code} - {response.text}", None
608
 
609
+ except httpx.TimeoutException:
610
  return "❌ Error: Timeout while saving document", None
611
  except httpx.RequestError as e:
612
  return f"❌ Error: Failed to connect to document server: {str(e)}", None