|
11 | 11 |
|
12 | 12 | from __future__ import annotations |
13 | 13 |
|
| 14 | +import dataclasses |
14 | 15 | import json |
15 | 16 | import logging |
16 | 17 | import os |
@@ -140,12 +141,35 @@ def check_tool_calls(self, _model: str, model_info: dict) -> bool: |
140 | 141 | } |
141 | 142 |
|
142 | 143 | def get_provider(endpoint: str | None = None) -> APIProvider: |
143 | | - """Return the ``APIProvider`` for the given (or configured) endpoint URL.""" |
| 144 | + """Return the ``APIProvider`` for the given (or configured) endpoint URL. |
| 145 | +
|
| 146 | + When running inside an AWF (Agentic Workflow Firewall) sandbox, the |
| 147 | + ``AWF_COPILOT_PROXY`` env var names the upstream provider whose behaviour |
| 148 | + (headers, model defaults, catalog format) the local proxy mirrors. |
| 149 | + The proxy URL is used as ``base_url`` while all other provider traits |
| 150 | + come from the named upstream. |
| 151 | +
|
| 152 | + ``AWF_COPILOT_PROXY`` accepts either a bare hostname |
| 153 | + (``api.githubcopilot.com``) or a full URL |
| 154 | + (``https://api.githubcopilot.com``). |
| 155 | + """ |
144 | 156 | url = endpoint or get_AI_endpoint() |
145 | 157 | netloc = urlparse(url).netloc |
146 | 158 | provider = _PROVIDERS.get(netloc) |
147 | 159 | if provider is not None: |
148 | 160 | return provider |
| 161 | + |
| 162 | + # AWF proxy support: AWF_COPILOT_PROXY names the upstream provider |
| 163 | + # (e.g. "api.githubcopilot.com") whose behaviour this proxy mirrors. |
| 164 | + awf_upstream = os.getenv("AWF_COPILOT_PROXY", "").strip() |
| 165 | + if awf_upstream: |
| 166 | + # Normalize: accept both bare hostnames and full URLs. |
| 167 | + parsed = urlparse(awf_upstream) |
| 168 | + key = parsed.netloc or parsed.path |
| 169 | + upstream = _PROVIDERS.get(key) |
| 170 | + if upstream: |
| 171 | + return dataclasses.replace(upstream, base_url=url) |
| 172 | + |
149 | 173 | # Unknown endpoint — return a generic provider with the given base URL |
150 | 174 | return APIProvider(name="custom", base_url=url, default_model="please-set-default-model-via-env") |
151 | 175 |
|
|
0 commit comments