lailaelkoussy commited on
Commit
7141c1f
·
1 Parent(s): 93ad5fa

add filter used_in_repo in list_all_entities

Browse files
Files changed (1) hide show
  1. gradio_mcp_space.py +42 -14
gradio_mcp_space.py CHANGED
@@ -247,9 +247,9 @@ Incoming Edges ({len(incoming)}):
247
  @observe(as_type="tool")
248
  def search_nodes(query: str, limit: int = 10) -> str:
249
  """
250
- Search for nodes in the knowledge graph by query string.
251
 
252
- Uses semantic and keyword search via the code index.
253
 
254
  Args:
255
  query: The search string to match against code index
@@ -440,12 +440,12 @@ def get_neighbors(node_id: str) -> str:
440
  @observe(as_type="tool")
441
  def go_to_definition(entity_name: str) -> str:
442
  """
443
- Find where an entity is declared or defined in the codebase.
444
 
445
- Locates the declaration point for functions, classes, variables, etc.
446
 
447
  Args:
448
- entity_name: The name of the entity to find the definition for
449
 
450
  Returns:
451
  str: A formatted string with definition locations
@@ -489,12 +489,12 @@ def go_to_definition(entity_name: str) -> str:
489
  @observe(as_type="tool")
490
  def find_usages(entity_name: str, limit: int = 20) -> str:
491
  """
492
- Find all usages or calls of an entity in the codebase.
493
 
494
  Shows where functions, classes, variables, etc. are used.
495
 
496
  Args:
497
- entity_name: The name of the entity to find usages for
498
  limit: Maximum number of usages to return (default: 20)
499
 
500
  Returns:
@@ -597,12 +597,12 @@ def get_file_structure(file_path: str) -> str:
597
  @observe(as_type="tool")
598
  def get_related_chunks(chunk_id: str, relation_type: str = "calls") -> str:
599
  """
600
- Get chunks related to a given chunk by a specific relationship.
601
 
602
- Find chunks connected via relationships like 'calls', 'contains', etc.
603
 
604
  Args:
605
- chunk_id: The ID of the chunk to find related chunks for
606
  relation_type: The type of relationship to filter by (default: 'calls')
607
 
608
  Returns:
@@ -651,7 +651,8 @@ def list_all_entities(
651
  limit: int = 50,
652
  page: int = 1,
653
  entity_type: Optional[str] = None,
654
- declared_in_repo: Optional[bool] = None
 
655
  ) -> str:
656
  """
657
  List all entities tracked in the knowledge graph with filtering and pagination options.
@@ -663,6 +664,7 @@ def list_all_entities(
663
  page: Page number for pagination, 1-indexed (default: 1)
664
  entity_type: Filter by entity type ('class', 'function', 'method', 'variable', 'parameter', 'function_call', 'method_call')
665
  declared_in_repo: If True, only return entities with declarations. If False, only entities without declarations. If None, return all.
 
666
 
667
  Returns:
668
  str: A formatted string with all entities for the requested page
@@ -700,6 +702,15 @@ def list_all_entities(
700
  declared_in_repo = False
701
  elif declared_in_repo.lower() in ("none", "null", "all", ""):
702
  declared_in_repo = None
 
 
 
 
 
 
 
 
 
703
 
704
  if not knowledge_graph.entities:
705
  return "No entities found in the knowledge graph."
@@ -721,6 +732,14 @@ def list_all_entities(
721
  if not declared_in_repo and has_declaration:
722
  continue
723
 
 
 
 
 
 
 
 
 
724
  filtered_entities[entity_name] = info
725
 
726
  # Build the response with filtered entities
@@ -730,6 +749,8 @@ def list_all_entities(
730
  filter_desc.append(f"type={entity_type}")
731
  if declared_in_repo is not None:
732
  filter_desc.append(f"declared_in_repo={declared_in_repo}")
 
 
733
  filter_text = f" (filtered by {', '.join(filter_desc)})" if filter_desc else ""
734
  return f"No entities found{filter_text}."
735
 
@@ -769,6 +790,8 @@ def list_all_entities(
769
  result += f"\n(Filtered by type={entity_type})\n"
770
  if declared_in_repo is not None:
771
  result += f"(Filtered by declared_in_repo={declared_in_repo})\n"
 
 
772
 
773
  return result
774
  except Exception as e:
@@ -1758,8 +1781,8 @@ def create_gradio_app():
1758
  node_output = gr.Textbox(label="Node Information", lines=20, max_lines=30)
1759
  node_info_btn.click(fn=get_node_info, inputs=node_id_input, outputs=node_output)
1760
  node_edges_btn.click(fn=get_node_edges, inputs=node_id_input, outputs=node_output)
1761
- gr.Markdown(_tool_doc_md(get_node_info))
1762
- gr.Markdown(_tool_doc_md(get_node_edges))
1763
 
1764
  with gr.Tab("🏗️ Structure"):
1765
  gr.Markdown("### Repository Tree")
@@ -1799,12 +1822,17 @@ def create_gradio_app():
1799
  label="Declared in Repo (optional)",
1800
  value=""
1801
  )
 
 
 
 
 
1802
  list_entities_btn = gr.Button("List Entities", variant="primary")
1803
  with gr.Column():
1804
  list_entities_output = gr.Textbox(label="Entities", lines=20, max_lines=30)
1805
  list_entities_btn.click(
1806
  fn=list_all_entities,
1807
- inputs=[entity_limit, entity_page, entity_type_filter, declared_in_repo],
1808
  outputs=list_entities_output,
1809
  )
1810
  gr.Markdown(_tool_doc_md(list_all_entities))
 
247
  @observe(as_type="tool")
248
  def search_nodes(query: str, limit: int = 10) -> str:
249
  """
250
+ Search for chunk nodes in the knowledge graph by query string.
251
 
252
+ Uses keyword search via the code index.
253
 
254
  Args:
255
  query: The search string to match against code index
 
440
  @observe(as_type="tool")
441
  def go_to_definition(entity_name: str) -> str:
442
  """
443
+ Retrieve where an entity is declared or defined in the codebase.
444
 
445
+ Locates and retrieves the declaration point for functions, classes, variables, etc.
446
 
447
  Args:
448
+ entity_name: The name of the entity to retrieve the definition for
449
 
450
  Returns:
451
  str: A formatted string with definition locations
 
489
  @observe(as_type="tool")
490
  def find_usages(entity_name: str, limit: int = 20) -> str:
491
  """
492
+ Retrieve all usages or calls of an entity in the codebase.
493
 
494
  Shows where functions, classes, variables, etc. are used.
495
 
496
  Args:
497
+ entity_name: The name of the entity to retrieve usages for
498
  limit: Maximum number of usages to return (default: 20)
499
 
500
  Returns:
 
597
  @observe(as_type="tool")
598
  def get_related_chunks(chunk_id: str, relation_type: str = "calls") -> str:
599
  """
600
+ Retrieve chunks related to a given chunk by a specific relationship.
601
 
602
+ Retrieve chunks connected via relationships like 'calls', 'contains', etc.
603
 
604
  Args:
605
+ chunk_id: The ID of the chunk to retrieve related chunks for
606
  relation_type: The type of relationship to filter by (default: 'calls')
607
 
608
  Returns:
 
651
  limit: int = 50,
652
  page: int = 1,
653
  entity_type: Optional[str] = None,
654
+ declared_in_repo: Optional[bool] = None,
655
+ called_in_repo: Optional[bool] = None
656
  ) -> str:
657
  """
658
  List all entities tracked in the knowledge graph with filtering and pagination options.
 
664
  page: Page number for pagination, 1-indexed (default: 1)
665
  entity_type: Filter by entity type ('class', 'function', 'method', 'variable', 'parameter', 'function_call', 'method_call')
666
  declared_in_repo: If True, only return entities with declarations. If False, only entities without declarations. If None, return all.
667
+ called_in_repo: If True, only return entities that have usages/calls in the repo. If False, only entities without usages. If None, return all.
668
 
669
  Returns:
670
  str: A formatted string with all entities for the requested page
 
702
  declared_in_repo = False
703
  elif declared_in_repo.lower() in ("none", "null", "all", ""):
704
  declared_in_repo = None
705
+
706
+ # Handle called_in_repo - convert string to bool if needed
707
+ if isinstance(called_in_repo, str):
708
+ if called_in_repo.lower() in ("true", "1", "yes"):
709
+ called_in_repo = True
710
+ elif called_in_repo.lower() in ("false", "0", "no"):
711
+ called_in_repo = False
712
+ elif called_in_repo.lower() in ("none", "null", "all", ""):
713
+ called_in_repo = None
714
 
715
  if not knowledge_graph.entities:
716
  return "No entities found in the knowledge graph."
 
732
  if not declared_in_repo and has_declaration:
733
  continue
734
 
735
+ # Filter by called_in_repo (usages) if specified
736
+ if called_in_repo is not None:
737
+ has_calls = len(info.get('calling_chunk_ids', [])) > 0
738
+ if called_in_repo and not has_calls:
739
+ continue
740
+ if not called_in_repo and has_calls:
741
+ continue
742
+
743
  filtered_entities[entity_name] = info
744
 
745
  # Build the response with filtered entities
 
749
  filter_desc.append(f"type={entity_type}")
750
  if declared_in_repo is not None:
751
  filter_desc.append(f"declared_in_repo={declared_in_repo}")
752
+ if called_in_repo is not None:
753
+ filter_desc.append(f"called_in_repo={called_in_repo}")
754
  filter_text = f" (filtered by {', '.join(filter_desc)})" if filter_desc else ""
755
  return f"No entities found{filter_text}."
756
 
 
790
  result += f"\n(Filtered by type={entity_type})\n"
791
  if declared_in_repo is not None:
792
  result += f"(Filtered by declared_in_repo={declared_in_repo})\n"
793
+ if called_in_repo is not None:
794
+ result += f"(Filtered by called_in_repo={called_in_repo})\n"
795
 
796
  return result
797
  except Exception as e:
 
1781
  node_output = gr.Textbox(label="Node Information", lines=20, max_lines=30)
1782
  node_info_btn.click(fn=get_node_info, inputs=node_id_input, outputs=node_output)
1783
  node_edges_btn.click(fn=get_node_edges, inputs=node_id_input, outputs=node_output)
1784
+ gr.Markdown("#Get Node Info:" + _tool_doc_md(get_node_info))
1785
+ gr.Markdown("#Get Node Edges:" + _tool_doc_md(get_node_edges))
1786
 
1787
  with gr.Tab("🏗️ Structure"):
1788
  gr.Markdown("### Repository Tree")
 
1822
  label="Declared in Repo (optional)",
1823
  value=""
1824
  )
1825
+ called_in_repo = gr.Dropdown(
1826
+ choices=["", "true", "false"],
1827
+ label="Called in Repo (optional)",
1828
+ value=""
1829
+ )
1830
  list_entities_btn = gr.Button("List Entities", variant="primary")
1831
  with gr.Column():
1832
  list_entities_output = gr.Textbox(label="Entities", lines=20, max_lines=30)
1833
  list_entities_btn.click(
1834
  fn=list_all_entities,
1835
+ inputs=[entity_limit, entity_page, entity_type_filter, declared_in_repo, called_in_repo],
1836
  outputs=list_entities_output,
1837
  )
1838
  gr.Markdown(_tool_doc_md(list_all_entities))