2
2
3
3
from __future__ import annotations
4
4
5
- from functools import partial
6
5
from pathlib import Path
7
- from typing import TYPE_CHECKING , cast
6
+ from typing import cast
8
7
9
8
from gitingest .clone import clone_repo
10
9
from gitingest .ingestion import ingest_query
11
10
from gitingest .query_parser import IngestionQuery , parse_query
12
11
from gitingest .utils .git_utils import validate_github_token
12
+ from server .models import IngestErrorResponse , IngestResponse , IngestSuccessResponse
13
13
from server .server_config import (
14
- DEFAULT_FILE_SIZE_KB ,
15
- EXAMPLE_REPOS ,
14
+ DEFAULT_MAX_FILE_SIZE_KB ,
16
15
MAX_DISPLAY_SIZE ,
17
- templates ,
18
16
)
19
17
from server .server_utils import Colors , log_slider_to_size
20
18
21
- if TYPE_CHECKING :
22
- from fastapi import Request
23
- from starlette .templating import _TemplateResponse
24
-
25
19
26
20
async def process_query (
27
- request : Request ,
28
- * ,
29
21
input_text : str ,
30
22
slider_position : int ,
31
23
pattern_type : str = "exclude" ,
32
24
pattern : str = "" ,
33
- is_index : bool = False ,
34
25
token : str | None = None ,
35
- ) -> _TemplateResponse :
26
+ ) -> IngestResponse :
36
27
"""Process a query by parsing input, cloning a repository, and generating a summary.
37
28
38
29
Handle user input, process Git repository data, and prepare
39
30
a response for rendering a template with the processed results or an error message.
40
31
41
32
Parameters
42
33
----------
43
- request : Request
44
- The HTTP request object.
45
34
input_text : str
46
35
Input text provided by the user, typically a Git repository URL or slug.
47
36
slider_position : int
@@ -50,15 +39,13 @@ async def process_query(
50
39
Type of pattern to use (either "include" or "exclude") (default: ``"exclude"``).
51
40
pattern : str
52
41
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``).
55
42
token : str | None
56
43
GitHub personal access token (PAT) for accessing private repositories.
57
44
58
45
Returns
59
46
-------
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
62
49
63
50
Raises
64
51
------
@@ -79,21 +66,10 @@ async def process_query(
79
66
if token :
80
67
validate_github_token (token )
81
68
82
- template = "index.jinja" if is_index else "git.jinja"
83
- template_response = partial (templates .TemplateResponse , name = template )
84
69
max_file_size = log_slider_to_size (slider_position )
85
70
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
-
96
71
query : IngestionQuery | None = None
72
+ short_repo_url = ""
97
73
98
74
try :
99
75
query = await parse_query (
@@ -107,7 +83,7 @@ async def process_query(
107
83
query .ensure_url ()
108
84
109
85
# 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 } "
111
87
112
88
clone_config = query .extract_clone_config ()
113
89
await clone_repo (clone_config , token = token )
@@ -126,10 +102,10 @@ async def process_query(
126
102
print (f"{ Colors .BROWN } WARN{ Colors .END } : { Colors .RED } <- { Colors .END } " , end = "" )
127
103
print (f"{ Colors .RED } { exc } { Colors .END } " )
128
104
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
+ )
133
109
134
110
if len (content ) > MAX_DISPLAY_SIZE :
135
111
content = (
@@ -148,18 +124,17 @@ async def process_query(
148
124
summary = summary ,
149
125
)
150
126
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 ,
159
136
)
160
137
161
- return template_response (context = context )
162
-
163
138
164
139
def _print_query (url : str , max_file_size : int , pattern_type : str , pattern : str ) -> None :
165
140
"""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)
177
152
178
153
"""
179
154
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 :
181
156
print (
182
157
f" | { Colors .YELLOW } Size: { int (max_file_size / 1024 )} kb{ Colors .END } " ,
183
158
end = "" ,
0 commit comments