Skip to content

Commit fb57650

Browse files
authored
Merge pull request #15 from zhujian0805/main
refactor the copilot-api fetch module
2 parents a4355b6 + 1f33373 commit fb57650

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

code_assistant_manager/copilot_models.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,46 @@ def copilot_headers(
6262
return h
6363

6464

65-
def fetch_models(copilot_token: str, account_type: str = "individual"):
66-
url = f"{copilot_base_url(account_type)}/models"
65+
def fetch_models(copilot_token: str, base_url: str = "https://192.168.1.100:5000"):
66+
url = f"{base_url}/v1/models"
6767
# Security: Add timeout to prevent hanging connections
68-
r = requests.get(url, headers=copilot_headers(copilot_token), timeout=30)
68+
69+
# Determine if SSL verification should be enabled
70+
# For internal/private IPs, skip verification as they often use self-signed certificates
71+
import ipaddress
72+
from urllib.parse import urlparse
73+
74+
verify_ssl = True
75+
try:
76+
parsed = urlparse(base_url)
77+
if parsed.hostname:
78+
try:
79+
ip = ipaddress.ip_address(parsed.hostname)
80+
# Skip verification for private IPs (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8)
81+
if ip.is_private or ip.is_loopback:
82+
verify_ssl = False
83+
except ValueError:
84+
# Not an IP address, keep verification enabled
85+
pass
86+
except Exception:
87+
# If parsing fails, keep verification enabled
88+
pass
89+
90+
# Check if we're using a configured endpoint with API key authentication
91+
# (instead of direct GitHub Copilot API access)
92+
api_key = os.environ.get("api_key")
93+
if api_key:
94+
# Use OpenAI-compatible API key authentication for proxy endpoints
95+
headers = {
96+
"Authorization": f"Bearer {api_key}",
97+
"Content-Type": "application/json",
98+
}
99+
else:
100+
# Use GitHub Copilot token authentication for direct API access
101+
headers = copilot_headers(copilot_token)
102+
103+
# Make the request
104+
r = requests.get(url, headers=headers, timeout=30, verify=verify_ssl)
69105
r.raise_for_status()
70106
return r.json()
71107

@@ -111,8 +147,21 @@ def list_models():
111147
copilot_token = info["token"]
112148
state["copilot_token"] = copilot_token
113149

150+
# Get base URL from environment (set by EndpointManager) or from config
151+
base_url = os.environ.get("endpoint")
152+
if not base_url:
153+
# Try to get from config
154+
try:
155+
from .config import ConfigManager
156+
config = ConfigManager()
157+
copilot_config = config.get_endpoint_config("copilot-api")
158+
base_url = copilot_config.get("endpoint", "https://192.168.1.100:5000")
159+
except Exception as e:
160+
logger.debug(f"Could not load config, using default URL: {e}")
161+
base_url = "https://192.168.1.100:5000"
162+
114163
logger.debug("Fetching Copilot models")
115-
models = fetch_models(copilot_token)
164+
models = fetch_models(copilot_token, base_url)
116165
model_count = len(models.get("data", []))
117166
logger.debug(f"Found {model_count} models")
118167

tests/test_copilot_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ def test_fetch_models_url(self, mock_get):
173173
mock_response.json.return_value = {"data": []}
174174
mock_get.return_value = mock_response
175175

176-
fetch_models("test-copilot-token", account_type="individual")
176+
fetch_models("test-copilot-token", "https://api.githubcopilot.com")
177177

178178
call_args = mock_get.call_args
179-
assert call_args[0][0] == "https://api.githubcopilot.com/models"
179+
assert call_args[0][0] == "https://api.githubcopilot.com/v1/models"
180180

181181
@patch("code_assistant_manager.copilot_models.requests.get")
182182
def test_fetch_models_url_organization(self, mock_get):
@@ -185,10 +185,10 @@ def test_fetch_models_url_organization(self, mock_get):
185185
mock_response.json.return_value = {"data": []}
186186
mock_get.return_value = mock_response
187187

188-
fetch_models("test-copilot-token", account_type="myorg")
188+
fetch_models("test-copilot-token", "https://api.myorg.githubcopilot.com")
189189

190190
call_args = mock_get.call_args
191-
assert call_args[0][0] == "https://api.myorg.githubcopilot.com/models"
191+
assert call_args[0][0] == "https://api.myorg.githubcopilot.com/v1/models"
192192

193193
@patch("code_assistant_manager.copilot_models.requests.get")
194194
def test_fetch_models_headers(self, mock_get):

0 commit comments

Comments
 (0)