22
33from __future__ import annotations
44
5- from functools import partial
65from pathlib import Path
7- from typing import TYPE_CHECKING , cast
6+ from typing import cast
87
98from gitingest .clone import clone_repo
109from gitingest .ingestion import ingest_query
1110from gitingest .query_parser import IngestionQuery , parse_query
1211from gitingest .utils .git_utils import validate_github_token
12+ from server .models import IngestErrorResponse , IngestResponse , IngestSuccessResponse
1313from server .server_config import (
14- DEFAULT_FILE_SIZE_KB ,
15- EXAMPLE_REPOS ,
14+ DEFAULT_MAX_FILE_SIZE_KB ,
1615 MAX_DISPLAY_SIZE ,
17- templates ,
1816)
1917from server .server_utils import Colors , log_slider_to_size
2018
21- if TYPE_CHECKING :
22- from fastapi import Request
23- from starlette .templating import _TemplateResponse
24-
2519
2620async def process_query (
27- request : Request ,
28- * ,
2921 input_text : str ,
3022 slider_position : int ,
3123 pattern_type : str = "exclude" ,
3224 pattern : str = "" ,
33- is_index : bool = False ,
3425 token : str | None = None ,
35- ) -> _TemplateResponse :
26+ ) -> IngestResponse :
3627 """Process a query by parsing input, cloning a repository, and generating a summary.
3728
3829 Handle user input, process Git repository data, and prepare
3930 a response for rendering a template with the processed results or an error message.
4031
4132 Parameters
4233 ----------
43- request : Request
44- The HTTP request object.
4534 input_text : str
4635 Input text provided by the user, typically a Git repository URL or slug.
4736 slider_position : int
@@ -50,15 +39,13 @@ async def process_query(
5039 Type of pattern to use (either "include" or "exclude") (default: ``"exclude"``).
5140 pattern : str
5241 Pattern to include or exclude in the query, depending on the pattern type.
53- is_index : bool
54- Flag indicating whether the request is for the index page (default: ``False``).
5542 token : str | None
5643 GitHub personal access token (PAT) for accessing private repositories.
5744
5845 Returns
5946 -------
60- _TemplateResponse
61- Rendered template response containing the processed results or an error message.
47+ IngestResponse
48+ A union type, corresponding to IngestErrorResponse or IngestSuccessResponse
6249
6350 Raises
6451 ------
@@ -79,21 +66,10 @@ async def process_query(
7966 if token :
8067 validate_github_token (token )
8168
82- template = "index.jinja" if is_index else "git.jinja"
83- template_response = partial (templates .TemplateResponse , name = template )
8469 max_file_size = log_slider_to_size (slider_position )
8570
86- context = {
87- "request" : request ,
88- "repo_url" : input_text ,
89- "examples" : EXAMPLE_REPOS if is_index else [],
90- "default_file_size" : slider_position ,
91- "pattern_type" : pattern_type ,
92- "pattern" : pattern ,
93- "token" : token ,
94- }
95-
9671 query : IngestionQuery | None = None
72+ short_repo_url = ""
9773
9874 try :
9975 query = await parse_query (
@@ -107,7 +83,7 @@ async def process_query(
10783 query .ensure_url ()
10884
10985 # Sets the "<user>/<repo>" for the page title
110- context [ " short_repo_url" ] = f"{ query .user_name } /{ query .repo_name } "
86+ short_repo_url = f"{ query .user_name } /{ query .repo_name } "
11187
11288 clone_config = query .extract_clone_config ()
11389 await clone_repo (clone_config , token = token )
@@ -126,10 +102,10 @@ async def process_query(
126102 print (f"{ Colors .BROWN } WARN{ Colors .END } : { Colors .RED } <- { Colors .END } " , end = "" )
127103 print (f"{ Colors .RED } { exc } { Colors .END } " )
128104
129- context [ "error_message" ] = f"Error: { exc } "
130- if "405" in str (exc ):
131- context [ "error_message" ] = "Repository not found. Please make sure it is public."
132- return template_response ( context = context )
105+ return IngestErrorResponse (
106+ error = "Repository not found. Please make sure it is public." if "405" in str (exc ) else "" ,
107+ repo_url = short_repo_url ,
108+ )
133109
134110 if len (content ) > MAX_DISPLAY_SIZE :
135111 content = (
@@ -148,18 +124,17 @@ async def process_query(
148124 summary = summary ,
149125 )
150126
151- context .update (
152- {
153- "result" : True ,
154- "summary" : summary ,
155- "tree" : tree ,
156- "content" : content ,
157- "ingest_id" : query .id ,
158- },
127+ return IngestSuccessResponse (
128+ repo_url = input_text ,
129+ short_repo_url = short_repo_url ,
130+ summary = summary ,
131+ tree = tree ,
132+ content = content ,
133+ default_max_file_size = slider_position ,
134+ pattern_type = pattern_type ,
135+ pattern = pattern ,
159136 )
160137
161- return template_response (context = context )
162-
163138
164139def _print_query (url : str , max_file_size : int , pattern_type : str , pattern : str ) -> None :
165140 """Print a formatted summary of the query details for debugging.
@@ -177,7 +152,7 @@ def _print_query(url: str, max_file_size: int, pattern_type: str, pattern: str)
177152
178153 """
179154 print (f"{ Colors .WHITE } { url :<20} { Colors .END } " , end = "" )
180- if int (max_file_size / 1024 ) != DEFAULT_FILE_SIZE_KB :
155+ if int (max_file_size / 1024 ) != DEFAULT_MAX_FILE_SIZE_KB :
181156 print (
182157 f" | { Colors .YELLOW } Size: { int (max_file_size / 1024 )} kb{ Colors .END } " ,
183158 end = "" ,
0 commit comments