forked from realjuangalt/ce-demo-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
295 lines (260 loc) · 11.5 KB
/
server.py
File metadata and controls
295 lines (260 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import http.server
import socketserver
import json
import os
import logging
from urllib.parse import parse_qs
import requests
from dotenv import load_dotenv
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Load environment variables from .env
load_dotenv()
VENICE_API_KEY = os.getenv("VENICE_API_KEY")
GITHUB_API_KEY = os.getenv("GITHUB_API_KEY")
PORT = 8000
# Base GitHub API URL
BASE_URL = "https://api.github.com"
# Headers for GitHub API authentication
github_headers = {
"Authorization": f"Bearer {GITHUB_API_KEY}",
"Accept": "application/vnd.github+json"
}
# Global variables for repository structure, user exploration path, and cancellation
repo_structures = {} # Dictionary to store repo structures, keyed by repo URL
current_path = ["root"] # Current exploration path, starting at root
current_repo_url = None # Track the current repo URL being explored
scraping_cancelled = False # Flag to cancel ongoing scraping
def fetch_repo_contents(owner, repo, path="", depth=0):
"""
Recursively fetch the folder structure of a GitHub repository (without file contents).
Args:
owner (str): Repository owner (e.g., "bitcoin")
repo (str): Repository name (e.g., "bitcoin")
path (str): Path within the repository (default is root "")
depth (int): Depth for recursion (default 0)
Returns:
list: List of items (files and directories) in schema format
"""
if scraping_cancelled:
logger.info(f"Scraping cancelled for {owner}/{repo} at path: {path}")
return []
url = f"{BASE_URL}/repos/{owner}/{repo}/contents/{path}"
items = []
try:
logger.info(f"Fetching GitHub contents from: {url}")
response = requests.get(url, headers=github_headers)
response.raise_for_status()
contents = response.json()
logger.info(f"Received {len(contents)} items at path: {path}")
for item in contents:
if scraping_cancelled:
logger.info(f"Scraping cancelled for {owner}/{repo} at path: {path}/{item['name']}")
return items
if item["type"] == "dir":
children = fetch_repo_contents(owner, repo, item["path"], depth + 1)
items.append({
"name": item["name"],
"type": "directory",
"quality": 50 + depth * 10,
"children": children
})
else:
items.append({
"name": item["name"],
"type": "file",
"quality": 50 + depth * 10
})
except requests.exceptions.RequestException as e:
if hasattr(e, 'response') and e.response:
logger.error(f"Error fetching contents at {path}: {e.response.status_code} - {e.response.text}")
if e.response.status_code == 404:
raise Exception(f"Repository {owner}/{repo} not found")
elif e.response.status_code == 401:
raise Exception("Invalid GitHub API key")
elif e.response.status_code == 403:
raise Exception("GitHub API rate limit exceeded")
logger.error(f"Error fetching contents at {path}: {e}")
raise Exception(f"Failed to fetch contents: {str(e)}")
return items
def scrape_repo_structure(repo_url):
"""
Scrape the folder structure of a GitHub repository and return it in schema format.
Args:
repo_url (str): URL of the repository (e.g., "https://github.com/bitcoin/bitcoin")
Returns:
dict: Repository structure in schema format
"""
global repo_structures, current_repo_url, current_path
parts = repo_url.split("/")
owner = parts[-2]
repo = parts[-1]
logger.info(f"Scraping folder structure for {owner}/{repo}...")
files = fetch_repo_contents(owner, repo)
repo_structures[repo_url] = {
"files": files,
"path": ["root"]
}
current_repo_url = repo_url
current_path = ["root"]
return repo_structures[repo_url]
def get_current_metadata():
"""
Get the metadata of the user's current exploration location.
Returns:
dict: Metadata of the current location (e.g., directory or file details)
"""
if not current_repo_url or current_repo_url not in repo_structures:
return {"location": "No repository loaded"}
repo_data = repo_structures[current_repo_url]
current = repo_data["files"]
path = current_path[1:]
for name in path:
found = False
for item in current:
if item["name"] == name:
if item["type"] == "directory":
current = item["children"]
found = True
break
if not found:
return {"location": "Path not found", "path": current_path}
if len(path) == 0:
return {
"location": "root",
"contents": [item["name"] for item in current],
"path": current_path
}
else:
last_name = path[-1]
for item in current:
if item["name"] == last_name:
if item["type"] == "directory":
return {
"location": f"Directory: {last_name}",
"contents": [child["name"] for child in item["children"]],
"path": current_path
}
else:
return {
"location": f"File: {last_name}",
"path": current_path
}
return {"location": "Path not found", "path": current_path}
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
if self.path == "/chat":
logger.info("Received chat request")
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode('utf-8')
data = parse_qs(post_data)
message = data.get('message', [''])[0]
logger.info(f"Chat message: {message}")
metadata = get_current_metadata()
prompt = f"User is exploring a GitHub repository. Current location: {json.dumps(metadata)}\nUser message: {message}"
url = "https://api.venice.ai/api/v1/chat/completions"
payload = {
"frequency_penalty": 0,
"n": 1,
"presence_penalty": 0,
"temperature": 0.15,
"top_p": 0.9,
"venice_parameters": {"include_venice_system_prompt": True},
"parallel_tool_calls": True,
"model": "llama-3.3-70b",
"messages": [{"role": "user", "content": prompt}]
}
headers = {
"Authorization": f"Bearer {VENICE_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
bot_message = response.json()['choices'][0]['message']['content']
logger.info(f"Chat response: {bot_message}")
except Exception as e:
bot_message = f"Error: {str(e)}"
logger.error(f"Chat API error: {str(e)}")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(json.dumps({"message": bot_message}).encode('utf-8'))
self.wfile.flush()
elif self.path == "/scrape-repo":
logger.info("Received scrape-repo request")
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode('utf-8')
data = parse_qs(post_data)
repo_url = data.get('repo_url', [''])[0]
logger.info(f"Scraping repo: {repo_url}")
scraping_cancelled = False # Reset cancellation flag
try:
repo_structure = scrape_repo_structure(repo_url)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(json.dumps(repo_structure).encode('utf-8'))
self.wfile.flush()
except Exception as e:
logger.error(f"Error scraping repo: {e}")
self.send_response(500)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps({"error": str(e)}).encode('utf-8'))
self.wfile.flush()
elif self.path == "/update-path":
logger.info("Received update-path request")
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode('utf-8')
data = parse_qs(post_data)
path = json.loads(data.get('path', ['[]'])[0])
logger.info(f"Updating exploration path to: {path}")
global current_path
current_path = path
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(json.dumps({"status": "success"}).encode('utf-8'))
self.wfile.flush()
elif self.path == "/cancel-scrape":
logger.info("Received cancel-scrape request")
scraping_cancelled = True
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(json.dumps({"status": "cancelled"}).encode('utf-8'))
self.wfile.flush()
else:
self.send_response(404)
self.end_headers()
def do_GET(self):
if self.path == "/":
self.path = "/index.html"
logger.info(f"Serving static file: {self.path}")
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
# Set up and start the server
Handler = CustomHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
logger.info(f"Serving at http://localhost:{PORT}")
httpd.serve_forever()