proxyagent 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {proxyagent-0.2.0 → proxyagent-0.3.0}/PKG-INFO +30 -1
- {proxyagent-0.2.0 → proxyagent-0.3.0}/README.md +29 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/__init__.py +1 -1
- proxyagent-0.3.0/proxyagent/aliases.py +49 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/cli.py +40 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/config.py +19 -11
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/pricing.py +2 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/providers.py +68 -12
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/server.py +43 -21
- {proxyagent-0.2.0 → proxyagent-0.3.0}/pyproject.toml +1 -1
- {proxyagent-0.2.0 → proxyagent-0.3.0}/tests/test_proxy.py +59 -1
- {proxyagent-0.2.0 → proxyagent-0.3.0}/.gitignore +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/crypto.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/db.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/harness.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/security.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/store.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/tools.py +0 -0
- {proxyagent-0.2.0 → proxyagent-0.3.0}/proxyagent/ui/index.html +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: proxyagent
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Run any agent (Claude, Codex, custom) on any machine — with no API key on the machine. A secure, self-hosted proxy for models and tools.
|
|
5
5
|
Project-URL: Homepage, https://github.com/teddyoweh/proxyagent
|
|
6
6
|
Author-email: Spawn Labs <teddy@spawnlabs.ai>
|
|
@@ -61,6 +61,15 @@ proxy and use the **machine token** as the "api key." The proxy authenticates th
|
|
|
61
61
|
checks its scope, **swaps in the real key**, forwards upstream, and logs the call. The
|
|
62
62
|
machine never sees a real credential.
|
|
63
63
|
|
|
64
|
+
## Try it with zero keys (local)
|
|
65
|
+
```bash
|
|
66
|
+
pip install proxyagent && proxyagent serve # prints an admin token
|
|
67
|
+
proxyagent token new local --admin pa_admin_… # mint a token
|
|
68
|
+
# call the built-in `mock` model — full pipeline (auth, scope, usage, cost, log), no real key:
|
|
69
|
+
curl -s localhost:8080/anthropic/v1/messages -H "x-api-key: pa_…" \
|
|
70
|
+
-d '{"model":"mock","max_tokens":50,"messages":[{"role":"user","content":"hi"}]}'
|
|
71
|
+
```
|
|
72
|
+
|
|
64
73
|
## Quickstart
|
|
65
74
|
|
|
66
75
|
**1. Run the proxy** (on a box you control — it holds the real keys):
|
|
@@ -153,6 +162,26 @@ proxyagent.run("claude-code", goal="build the app",
|
|
|
153
162
|
proxy="https://proxy.you.com", token=token)
|
|
154
163
|
```
|
|
155
164
|
|
|
165
|
+
## Supported providers
|
|
166
|
+
`anthropic` · `openai` · `gemini` · `groq` · `openrouter` · `mistral` · `deepseek` ·
|
|
167
|
+
`xai` · `together` — Anthropic uses its Messages API; the rest are OpenAI-compatible.
|
|
168
|
+
Point a harness/agent at `https://proxy.you.com/<provider>/v1` and it routes there.
|
|
169
|
+
Add or override any endpoint with `PROXYAGENT_<NAME>_ENDPOINT`.
|
|
170
|
+
|
|
171
|
+
## Model remap — rename or reroute models
|
|
172
|
+
Rewrite the requested model before forwarding — rename it, or reroute it to a totally
|
|
173
|
+
different provider:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
proxyagent alias set gpt-4o anthropic:claude-sonnet-4-5 # send "gpt-4o" calls to Claude
|
|
177
|
+
proxyagent alias set '*' mock # force EVERYTHING offline (no keys)
|
|
178
|
+
proxyagent alias ls
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The `'*' → mock` trick is the **offline harness** unlock: point `claude-code` at the
|
|
182
|
+
proxy, map everything to `mock`, and it runs end-to-end with zero keys and zero spend —
|
|
183
|
+
perfect for local dev, demos, and CI.
|
|
184
|
+
|
|
156
185
|
## Supported harnesses
|
|
157
186
|
`claude-code`, `codex`, and any **custom** command (`--command "my-agent {goal}"`). Adding one
|
|
158
187
|
is a few lines — it just needs to respect `*_BASE_URL`.
|
|
@@ -29,6 +29,15 @@ proxy and use the **machine token** as the "api key." The proxy authenticates th
|
|
|
29
29
|
checks its scope, **swaps in the real key**, forwards upstream, and logs the call. The
|
|
30
30
|
machine never sees a real credential.
|
|
31
31
|
|
|
32
|
+
## Try it with zero keys (local)
|
|
33
|
+
```bash
|
|
34
|
+
pip install proxyagent && proxyagent serve # prints an admin token
|
|
35
|
+
proxyagent token new local --admin pa_admin_… # mint a token
|
|
36
|
+
# call the built-in `mock` model — full pipeline (auth, scope, usage, cost, log), no real key:
|
|
37
|
+
curl -s localhost:8080/anthropic/v1/messages -H "x-api-key: pa_…" \
|
|
38
|
+
-d '{"model":"mock","max_tokens":50,"messages":[{"role":"user","content":"hi"}]}'
|
|
39
|
+
```
|
|
40
|
+
|
|
32
41
|
## Quickstart
|
|
33
42
|
|
|
34
43
|
**1. Run the proxy** (on a box you control — it holds the real keys):
|
|
@@ -121,6 +130,26 @@ proxyagent.run("claude-code", goal="build the app",
|
|
|
121
130
|
proxy="https://proxy.you.com", token=token)
|
|
122
131
|
```
|
|
123
132
|
|
|
133
|
+
## Supported providers
|
|
134
|
+
`anthropic` · `openai` · `gemini` · `groq` · `openrouter` · `mistral` · `deepseek` ·
|
|
135
|
+
`xai` · `together` — Anthropic uses its Messages API; the rest are OpenAI-compatible.
|
|
136
|
+
Point a harness/agent at `https://proxy.you.com/<provider>/v1` and it routes there.
|
|
137
|
+
Add or override any endpoint with `PROXYAGENT_<NAME>_ENDPOINT`.
|
|
138
|
+
|
|
139
|
+
## Model remap — rename or reroute models
|
|
140
|
+
Rewrite the requested model before forwarding — rename it, or reroute it to a totally
|
|
141
|
+
different provider:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
proxyagent alias set gpt-4o anthropic:claude-sonnet-4-5 # send "gpt-4o" calls to Claude
|
|
145
|
+
proxyagent alias set '*' mock # force EVERYTHING offline (no keys)
|
|
146
|
+
proxyagent alias ls
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The `'*' → mock` trick is the **offline harness** unlock: point `claude-code` at the
|
|
150
|
+
proxy, map everything to `mock`, and it runs end-to-end with zero keys and zero spend —
|
|
151
|
+
perfect for local dev, demos, and CI.
|
|
152
|
+
|
|
124
153
|
## Supported harnesses
|
|
125
154
|
`claude-code`, `codex`, and any **custom** command (`--command "my-agent {goal}"`). Adding one
|
|
126
155
|
is a few lines — it just needs to respect `*_BASE_URL`.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Model remapping — rewrite the requested model (and optionally re-route to another
|
|
2
|
+
provider) before forwarding.
|
|
3
|
+
|
|
4
|
+
A map entry's value is either a model name (rename) or "provider:model" (reroute):
|
|
5
|
+
|
|
6
|
+
PROXYAGENT_MODEL_MAP='{"*": "mock"}' # force everything offline
|
|
7
|
+
PROXYAGENT_MODEL_MAP='{"gpt-4o": "anthropic:claude-sonnet-4-5"}' # reroute to Claude
|
|
8
|
+
|
|
9
|
+
Lookup order: exact "provider:model" → exact "model" → wildcard "*".
|
|
10
|
+
Runtime overrides (set via the admin API) win over the env map.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
|
|
18
|
+
_RUNTIME: dict[str, str] = {}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _env_map() -> dict[str, str]:
|
|
22
|
+
raw = os.environ.get("PROXYAGENT_MODEL_MAP")
|
|
23
|
+
if not raw:
|
|
24
|
+
return {}
|
|
25
|
+
try:
|
|
26
|
+
return {str(k): str(v) for k, v in json.loads(raw).items()}
|
|
27
|
+
except Exception:
|
|
28
|
+
return {}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def get_map() -> dict[str, str]:
|
|
32
|
+
return {**_env_map(), **_RUNTIME}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def set_map(m: dict) -> None:
|
|
36
|
+
_RUNTIME.clear()
|
|
37
|
+
_RUNTIME.update({str(k): str(v) for k, v in (m or {}).items()})
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def remap(provider: str, model: str) -> tuple[str, str]:
|
|
41
|
+
"""Return the (provider, model) to actually use."""
|
|
42
|
+
m = get_map()
|
|
43
|
+
target = m.get(f"{provider}:{model}") or m.get(model) or m.get("*")
|
|
44
|
+
if not target:
|
|
45
|
+
return provider, model
|
|
46
|
+
if ":" in target:
|
|
47
|
+
p, mm = target.split(":", 1)
|
|
48
|
+
return p, mm
|
|
49
|
+
return provider, target
|
|
@@ -70,6 +70,46 @@ token_app = typer.Typer(help="Mint / list / revoke machine tokens.")
|
|
|
70
70
|
app.add_typer(token_app, name="token")
|
|
71
71
|
provider_app = typer.Typer(help="Add / list / remove provider credentials (stored, encrypted).")
|
|
72
72
|
app.add_typer(provider_app, name="provider")
|
|
73
|
+
alias_app = typer.Typer(help="Model remap — rename or reroute models (e.g. force everything to mock).")
|
|
74
|
+
app.add_typer(alias_app, name="alias")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@alias_app.command("ls")
|
|
78
|
+
def alias_ls(proxy: str = typer.Option("http://127.0.0.1:8080", "--proxy"),
|
|
79
|
+
admin: str = typer.Option(None, "--admin")):
|
|
80
|
+
"""Show the current model map."""
|
|
81
|
+
with _admin_client(proxy, admin) as c:
|
|
82
|
+
m = c.get("/admin/aliases").json()["map"]
|
|
83
|
+
if not m:
|
|
84
|
+
console.print("[dim]No aliases. e.g. `proxyagent alias set '*' mock`[/dim]"); return
|
|
85
|
+
t = Table(title="Model aliases")
|
|
86
|
+
t.add_column("Match"); t.add_column("→ Target")
|
|
87
|
+
for k, v in m.items():
|
|
88
|
+
t.add_row(k, v)
|
|
89
|
+
console.print(t)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@alias_app.command("set")
|
|
93
|
+
def alias_set(match: str, target: str,
|
|
94
|
+
proxy: str = typer.Option("http://127.0.0.1:8080", "--proxy"),
|
|
95
|
+
admin: str = typer.Option(None, "--admin")):
|
|
96
|
+
"""Map a model → a model (rename) or 'provider:model' (reroute). Use '*' to catch all."""
|
|
97
|
+
with _admin_client(proxy, admin) as c:
|
|
98
|
+
m = c.get("/admin/aliases").json()["map"]
|
|
99
|
+
m[match] = target
|
|
100
|
+
c.put("/admin/aliases", json={"map": m})
|
|
101
|
+
console.print(f"[green]✓[/green] [cyan]{match}[/cyan] → {target}")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@alias_app.command("rm")
|
|
105
|
+
def alias_rm(match: str, proxy: str = typer.Option("http://127.0.0.1:8080", "--proxy"),
|
|
106
|
+
admin: str = typer.Option(None, "--admin")):
|
|
107
|
+
"""Remove an alias."""
|
|
108
|
+
with _admin_client(proxy, admin) as c:
|
|
109
|
+
m = c.get("/admin/aliases").json()["map"]
|
|
110
|
+
m.pop(match, None)
|
|
111
|
+
c.put("/admin/aliases", json={"map": m})
|
|
112
|
+
console.print(f"[green]✓[/green] removed {match}")
|
|
73
113
|
|
|
74
114
|
|
|
75
115
|
@provider_app.command("add")
|
|
@@ -18,9 +18,10 @@ HOME = Path(os.environ.get("PROXYAGENT_HOME", Path.home() / ".proxyagent"))
|
|
|
18
18
|
@dataclass
|
|
19
19
|
class Provider:
|
|
20
20
|
name: str
|
|
21
|
-
|
|
21
|
+
endpoint: str # full upstream URL (e.g. …/v1/chat/completions)
|
|
22
22
|
key_env: str # env var holding the REAL key
|
|
23
|
-
auth_style: str # "bearer"
|
|
23
|
+
auth_style: str # "bearer" | "x-api-key"
|
|
24
|
+
shape: str # "openai" | "anthropic" (request + usage format)
|
|
24
25
|
extra_headers: dict = field(default_factory=dict)
|
|
25
26
|
|
|
26
27
|
@property
|
|
@@ -36,18 +37,25 @@ class Provider:
|
|
|
36
37
|
return {"Authorization": f"Bearer {key}", **self.extra_headers}
|
|
37
38
|
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return Provider(name, base.rstrip("/"), key_env, style, extra or {})
|
|
40
|
+
def _p(name, endpoint, key_env, *, shape="openai", style="bearer", extra=None) -> Provider:
|
|
41
|
+
endpoint = os.environ.get(f"PROXYAGENT_{name.upper()}_ENDPOINT", endpoint)
|
|
42
|
+
return Provider(name, endpoint, key_env, style, shape, extra or {})
|
|
43
43
|
|
|
44
44
|
|
|
45
|
+
# Built-in upstreams. Anthropic uses its Messages API; the rest are OpenAI-compatible.
|
|
46
|
+
# Add your own / override endpoints via PROXYAGENT_<NAME>_ENDPOINT.
|
|
45
47
|
PROVIDERS: dict[str, Provider] = {
|
|
46
|
-
"anthropic":
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
),
|
|
50
|
-
"
|
|
48
|
+
"anthropic": _p("anthropic", "https://api.anthropic.com/v1/messages", "ANTHROPIC_API_KEY",
|
|
49
|
+
shape="anthropic", style="x-api-key",
|
|
50
|
+
extra={"anthropic-version": os.environ.get("ANTHROPIC_VERSION", "2023-06-01")}),
|
|
51
|
+
"openai": _p("openai", "https://api.openai.com/v1/chat/completions", "OPENAI_API_KEY"),
|
|
52
|
+
"gemini": _p("gemini", "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", "GEMINI_API_KEY"),
|
|
53
|
+
"groq": _p("groq", "https://api.groq.com/openai/v1/chat/completions", "GROQ_API_KEY"),
|
|
54
|
+
"openrouter": _p("openrouter", "https://openrouter.ai/api/v1/chat/completions", "OPENROUTER_API_KEY"),
|
|
55
|
+
"mistral": _p("mistral", "https://api.mistral.ai/v1/chat/completions", "MISTRAL_API_KEY"),
|
|
56
|
+
"deepseek": _p("deepseek", "https://api.deepseek.com/v1/chat/completions", "DEEPSEEK_API_KEY"),
|
|
57
|
+
"xai": _p("xai", "https://api.x.ai/v1/chat/completions", "XAI_API_KEY"),
|
|
58
|
+
"together": _p("together", "https://api.together.xyz/v1/chat/completions", "TOGETHER_API_KEY"),
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
|
|
@@ -16,13 +16,6 @@ from . import pricing
|
|
|
16
16
|
from .config import Config, PROVIDERS
|
|
17
17
|
from .store import Store, now_ms
|
|
18
18
|
|
|
19
|
-
# Map our public path → (provider, upstream path).
|
|
20
|
-
ROUTES = {
|
|
21
|
-
"anthropic": ("anthropic", "/v1/messages"),
|
|
22
|
-
"openai": ("openai", "/v1/chat/completions"),
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
19
|
def resolve_auth(provider, store: Store | None) -> tuple[dict, bool]:
|
|
27
20
|
"""Auth headers for an upstream call. A stored credential (proxy_agent_keys) wins
|
|
28
21
|
over the env key; returns ({}, False) when nothing is configured."""
|
|
@@ -55,20 +48,35 @@ def _extract_usage(provider: str, payload: dict) -> tuple[int | None, int | None
|
|
|
55
48
|
|
|
56
49
|
|
|
57
50
|
async def forward(
|
|
58
|
-
config: Config, provider_name: str,
|
|
51
|
+
config: Config, provider_name: str, body: dict,
|
|
59
52
|
*, streaming: bool, token: dict, store: Store, tools_used: list[str] | None = None,
|
|
60
53
|
):
|
|
61
54
|
"""Forward a request upstream. Returns (status, headers, body_iter_or_dict, log_after)."""
|
|
62
55
|
provider = PROVIDERS[provider_name]
|
|
56
|
+
model = body.get("model", "")
|
|
57
|
+
t0 = now_ms()
|
|
58
|
+
|
|
59
|
+
# Offline mock — exercise the full pipeline (auth, scope, log, cost) with NO real
|
|
60
|
+
# key. Use model "mock" (or "mock-…") anywhere a real model would go.
|
|
61
|
+
if model.startswith("mock"):
|
|
62
|
+
payload, (ptok, ctok) = _mock_payload(provider.shape, body)
|
|
63
|
+
store.log_request(
|
|
64
|
+
token_id=token["id"], token_label=token.get("label"), provider=provider_name,
|
|
65
|
+
model=model, status=200, prompt_tokens=ptok, completion_tokens=ctok,
|
|
66
|
+
latency_ms=now_ms() - t0, streamed=1 if streaming else 0,
|
|
67
|
+
tools_used=json.dumps(tools_used or []), cost_usd=pricing.cost_usd(model, ptok, ctok),
|
|
68
|
+
error=None)
|
|
69
|
+
if streaming:
|
|
70
|
+
return 200, {"content-type": "text/event-stream"}, _mock_stream(provider.shape, payload), None
|
|
71
|
+
return 200, {"content-type": "application/json"}, payload, None
|
|
72
|
+
|
|
63
73
|
auth, ok = resolve_auth(provider, store)
|
|
64
74
|
if not ok:
|
|
65
75
|
return 502, {}, {"error": f"provider '{provider_name}' not configured on the proxy "
|
|
66
76
|
f"(set {provider.key_env} or `proxyagent provider add {provider_name}`)"}, None
|
|
67
77
|
|
|
68
|
-
url = provider.
|
|
78
|
+
url = provider.endpoint
|
|
69
79
|
headers = {"content-type": "application/json", **auth}
|
|
70
|
-
model = body.get("model", "")
|
|
71
|
-
t0 = now_ms()
|
|
72
80
|
|
|
73
81
|
def _log(status, ptok, ctok, err=None):
|
|
74
82
|
store.log_request(
|
|
@@ -112,6 +120,54 @@ async def forward(
|
|
|
112
120
|
payload = resp.json()
|
|
113
121
|
except Exception:
|
|
114
122
|
payload = {"error": resp.text}
|
|
115
|
-
ptok, ctok = _extract_usage(
|
|
123
|
+
ptok, ctok = _extract_usage(provider.shape, payload if isinstance(payload, dict) else {})
|
|
116
124
|
_log(resp.status_code, ptok, ctok, None if resp.is_success else str(payload)[:300])
|
|
117
125
|
return resp.status_code, {"content-type": "application/json"}, payload, None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# ------------------------------------------------------------------ #
|
|
129
|
+
# Offline mock — provider-shaped canned responses for local testing.
|
|
130
|
+
# ------------------------------------------------------------------ #
|
|
131
|
+
|
|
132
|
+
def _last_user_text(body: dict) -> str:
|
|
133
|
+
for m in reversed(body.get("messages", [])):
|
|
134
|
+
if m.get("role") == "user":
|
|
135
|
+
c = m.get("content")
|
|
136
|
+
if isinstance(c, str):
|
|
137
|
+
return c
|
|
138
|
+
if isinstance(c, list):
|
|
139
|
+
return " ".join(p.get("text", "") for p in c if isinstance(p, dict))
|
|
140
|
+
return ""
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _mock_payload(provider: str, body: dict):
|
|
144
|
+
prompt = _last_user_text(body)[:200]
|
|
145
|
+
text = f"[proxyagent mock] received: {prompt!r}. No real key used — the pipeline works."
|
|
146
|
+
ptok, ctok = max(1, len(prompt) // 4), max(1, len(text) // 4)
|
|
147
|
+
if provider == "anthropic":
|
|
148
|
+
return ({
|
|
149
|
+
"id": "msg_mock", "type": "message", "role": "assistant", "model": body.get("model"),
|
|
150
|
+
"content": [{"type": "text", "text": text}], "stop_reason": "end_turn",
|
|
151
|
+
"usage": {"input_tokens": ptok, "output_tokens": ctok},
|
|
152
|
+
}, (ptok, ctok))
|
|
153
|
+
return ({
|
|
154
|
+
"id": "chatcmpl-mock", "object": "chat.completion", "model": body.get("model"),
|
|
155
|
+
"choices": [{"index": 0, "message": {"role": "assistant", "content": text},
|
|
156
|
+
"finish_reason": "stop"}],
|
|
157
|
+
"usage": {"prompt_tokens": ptok, "completion_tokens": ctok, "total_tokens": ptok + ctok},
|
|
158
|
+
}, (ptok, ctok))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
async def _mock_stream(provider: str, payload: dict):
|
|
162
|
+
import json as _j
|
|
163
|
+
if provider == "anthropic":
|
|
164
|
+
text = payload["content"][0]["text"]
|
|
165
|
+
yield f"event: message_start\ndata: {_j.dumps({'type':'message_start','message':payload})}\n\n".encode()
|
|
166
|
+
yield (f"event: content_block_delta\ndata: "
|
|
167
|
+
f"{_j.dumps({'type':'content_block_delta','delta':{'type':'text_delta','text':text}})}\n\n").encode()
|
|
168
|
+
yield b"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
|
|
169
|
+
else:
|
|
170
|
+
text = payload["choices"][0]["message"]["content"]
|
|
171
|
+
chunk = {"choices": [{"delta": {"content": text}, "index": 0}]}
|
|
172
|
+
yield f"data: {_j.dumps(chunk)}\n\n".encode()
|
|
173
|
+
yield b"data: [DONE]\n\n"
|
|
@@ -14,9 +14,9 @@ from fastapi import FastAPI, Header, HTTPException, Request
|
|
|
14
14
|
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
|
|
15
15
|
from pydantic import BaseModel
|
|
16
16
|
|
|
17
|
-
from . import crypto
|
|
17
|
+
from . import aliases, crypto
|
|
18
18
|
from .config import Config, PROVIDERS
|
|
19
|
-
from .providers import
|
|
19
|
+
from .providers import forward, scope_allows
|
|
20
20
|
from .security import token_matches
|
|
21
21
|
from .store import Store, now_ms
|
|
22
22
|
from .tools import ToolRegistry
|
|
@@ -83,38 +83,44 @@ def create_app(config: Config | None = None) -> FastAPI:
|
|
|
83
83
|
# ------------------------------------------------------------------ #
|
|
84
84
|
# Provider proxy endpoints
|
|
85
85
|
# ------------------------------------------------------------------ #
|
|
86
|
-
async def _proxy(
|
|
86
|
+
async def _proxy(provider: str, request: Request, authorization, x_api_key):
|
|
87
87
|
token = auth_machine(authorization, x_api_key)
|
|
88
|
-
|
|
88
|
+
if provider not in PROVIDERS:
|
|
89
|
+
raise HTTPException(404, f"unknown provider '{provider}' (known: {list(PROVIDERS)})")
|
|
89
90
|
body = await request.json()
|
|
90
|
-
model
|
|
91
|
+
# model remap — may rename the model and/or reroute to another provider
|
|
92
|
+
provider, model = aliases.remap(provider, body.get("model", ""))
|
|
93
|
+
if provider not in PROVIDERS:
|
|
94
|
+
raise HTTPException(400, f"alias target provider '{provider}' is unknown")
|
|
95
|
+
body["model"] = model
|
|
91
96
|
scope = _json.loads(token["scope_json"])
|
|
92
|
-
if not scope_allows(scope,
|
|
93
|
-
raise HTTPException(403, f"token scope does not allow {
|
|
97
|
+
if not scope_allows(scope, provider, model):
|
|
98
|
+
raise HTTPException(403, f"token scope does not allow {provider}:{model}")
|
|
94
99
|
|
|
95
100
|
used_tools: list[str] = []
|
|
96
101
|
if request.headers.get("x-proxyagent-tools", "").lower() in ("1", "on", "true"):
|
|
97
|
-
body = tools.inject(body,
|
|
102
|
+
body = tools.inject(body, PROVIDERS[provider].shape)
|
|
98
103
|
used_tools = tools.names()
|
|
99
104
|
|
|
100
105
|
streaming = bool(body.get("stream"))
|
|
101
106
|
status, headers, payload, _ = await forward(
|
|
102
|
-
config,
|
|
103
|
-
|
|
104
|
-
)
|
|
107
|
+
config, provider, body, streaming=streaming, token=token, store=store,
|
|
108
|
+
tools_used=used_tools)
|
|
105
109
|
if streaming:
|
|
106
110
|
return StreamingResponse(payload, media_type="text/event-stream")
|
|
107
111
|
return JSONResponse(payload, status_code=status)
|
|
108
112
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
# OpenAI-compatible providers hit /<provider>/v1/chat/completions; Anthropic-style
|
|
114
|
+
# hit /<provider>/v1/messages. The provider segment selects the upstream.
|
|
115
|
+
@app.post("/{provider}/v1/chat/completions")
|
|
116
|
+
async def chat(provider: str, request: Request, authorization: str | None = Header(None),
|
|
117
|
+
x_api_key: str | None = Header(None)):
|
|
118
|
+
return await _proxy(provider, request, authorization, x_api_key)
|
|
113
119
|
|
|
114
|
-
@app.post("/
|
|
115
|
-
async def
|
|
116
|
-
|
|
117
|
-
return await _proxy(
|
|
120
|
+
@app.post("/{provider}/v1/messages")
|
|
121
|
+
async def messages(provider: str, request: Request, authorization: str | None = Header(None),
|
|
122
|
+
x_api_key: str | None = Header(None)):
|
|
123
|
+
return await _proxy(provider, request, authorization, x_api_key)
|
|
118
124
|
|
|
119
125
|
# ------------------------------------------------------------------ #
|
|
120
126
|
# Tools — execute a proxied tool (creds stay here)
|
|
@@ -212,10 +218,26 @@ def create_app(config: Config | None = None) -> FastAPI:
|
|
|
212
218
|
raise HTTPException(404, "no such credential")
|
|
213
219
|
return {"ok": True}
|
|
214
220
|
|
|
221
|
+
# -- model aliases / remap -------------------------------------------- #
|
|
222
|
+
@app.get("/admin/aliases")
|
|
223
|
+
async def get_aliases(authorization: str | None = Header(None),
|
|
224
|
+
x_admin_token: str | None = Header(None)):
|
|
225
|
+
require_admin(authorization, x_admin_token)
|
|
226
|
+
return {"map": aliases.get_map()}
|
|
227
|
+
|
|
228
|
+
@app.put("/admin/aliases")
|
|
229
|
+
async def set_aliases(request: Request, authorization: str | None = Header(None),
|
|
230
|
+
x_admin_token: str | None = Header(None)):
|
|
231
|
+
require_admin(authorization, x_admin_token)
|
|
232
|
+
body = await request.json()
|
|
233
|
+
aliases.set_map(body.get("map", body))
|
|
234
|
+
return {"map": aliases.get_map()}
|
|
235
|
+
|
|
215
236
|
@app.get("/healthz")
|
|
216
237
|
async def healthz():
|
|
217
|
-
return {"ok": True, "providers": _configured(), "
|
|
218
|
-
"backend": store.backend
|
|
238
|
+
return {"ok": True, "providers": _configured(), "available": sorted(PROVIDERS),
|
|
239
|
+
"tools": tools.names(), "backend": store.backend,
|
|
240
|
+
"aliases": len(aliases.get_map())}
|
|
219
241
|
|
|
220
242
|
# ------------------------------------------------------------------ #
|
|
221
243
|
# Dashboard
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "proxyagent"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.3.0"
|
|
8
8
|
description = "Run any agent (Claude, Codex, custom) on any machine — with no API key on the machine. A secure, self-hosted proxy for models and tools."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -5,9 +5,18 @@ import os
|
|
|
5
5
|
os.environ.setdefault("PROXYAGENT_HOME", "/tmp/proxyagent_test_home")
|
|
6
6
|
os.environ["PROXYAGENT_ADMIN_TOKEN"] = "pa_admin_test"
|
|
7
7
|
|
|
8
|
+
import pytest # noqa: E402
|
|
8
9
|
from fastapi.testclient import TestClient # noqa: E402
|
|
9
10
|
|
|
10
|
-
from proxyagent
|
|
11
|
+
from proxyagent import aliases as _aliases # noqa: E402
|
|
12
|
+
from proxyagent.config import Config, PROVIDERS # noqa: E402
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture(autouse=True)
|
|
16
|
+
def _reset_aliases():
|
|
17
|
+
_aliases.set_map({})
|
|
18
|
+
yield
|
|
19
|
+
_aliases.set_map({})
|
|
11
20
|
from proxyagent.providers import scope_allows # noqa: E402
|
|
12
21
|
from proxyagent.security import hash_token, token_matches, new_token # noqa: E402
|
|
13
22
|
from proxyagent.server import create_app # noqa: E402
|
|
@@ -98,6 +107,25 @@ def test_credential_storage_and_resolution():
|
|
|
98
107
|
assert s.remove_credential(cid)
|
|
99
108
|
|
|
100
109
|
|
|
110
|
+
def test_mock_provider_offline():
|
|
111
|
+
"""Full pipeline with no real key: mint → call model 'mock' → response + usage + log."""
|
|
112
|
+
c = _client()
|
|
113
|
+
tok = c.post("/admin/tokens", headers=ADMIN, json={"label": "m", "scope": ["*"]}).json()["token"]
|
|
114
|
+
r = c.post("/anthropic/v1/messages", headers={"x-api-key": tok},
|
|
115
|
+
json={"model": "mock", "max_tokens": 50, "messages": [{"role": "user", "content": "hello"}]})
|
|
116
|
+
assert r.status_code == 200
|
|
117
|
+
body = r.json()
|
|
118
|
+
assert body["content"][0]["text"].startswith("[proxyagent mock]")
|
|
119
|
+
assert body["usage"]["input_tokens"] >= 1
|
|
120
|
+
# it was logged (with $0 cost)
|
|
121
|
+
logs = c.get("/admin/logs", headers=ADMIN).json()["logs"]
|
|
122
|
+
assert logs[0]["model"] == "mock" and logs[0]["status"] == 200
|
|
123
|
+
# openai shape too
|
|
124
|
+
r2 = c.post("/openai/v1/chat/completions", headers={"authorization": f"Bearer {tok}"},
|
|
125
|
+
json={"model": "mock", "messages": [{"role": "user", "content": "hi"}]})
|
|
126
|
+
assert r2.json()["choices"][0]["message"]["content"].startswith("[proxyagent mock]")
|
|
127
|
+
|
|
128
|
+
|
|
101
129
|
def test_provider_admin_endpoints():
|
|
102
130
|
c = _client()
|
|
103
131
|
r = c.post("/admin/providers", headers=ADMIN, json={"provider": "anthropic", "secret": "sk-ant-x"})
|
|
@@ -107,3 +135,33 @@ def test_provider_admin_endpoints():
|
|
|
107
135
|
# unknown provider rejected
|
|
108
136
|
assert c.post("/admin/providers", headers=ADMIN,
|
|
109
137
|
json={"provider": "nope", "secret": "x"}).status_code == 400
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_more_providers_route():
|
|
141
|
+
# new providers are routable; mock works on any of them with no key
|
|
142
|
+
assert "groq" in PROVIDERS and "gemini" in PROVIDERS and "openrouter" in PROVIDERS
|
|
143
|
+
c = _client()
|
|
144
|
+
tok = c.post("/admin/tokens", headers=ADMIN, json={"scope": ["*"]}).json()["token"]
|
|
145
|
+
r = c.post("/groq/v1/chat/completions", headers={"authorization": f"Bearer {tok}"},
|
|
146
|
+
json={"model": "mock", "messages": [{"role": "user", "content": "hi"}]})
|
|
147
|
+
assert r.status_code == 200 and r.json()["choices"][0]["message"]["content"].startswith("[proxyagent mock]")
|
|
148
|
+
# unknown provider → 404
|
|
149
|
+
assert c.post("/nope/v1/chat/completions", headers={"authorization": f"Bearer {tok}"},
|
|
150
|
+
json={"model": "mock", "messages": []}).status_code == 404
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_model_remap_forces_mock_offline():
|
|
154
|
+
c = _client()
|
|
155
|
+
tok = c.post("/admin/tokens", headers=ADMIN, json={"scope": ["*"]}).json()["token"]
|
|
156
|
+
# map everything to mock → a "real" model call runs offline, no key
|
|
157
|
+
c.put("/admin/aliases", headers=ADMIN, json={"map": {"*": "mock"}})
|
|
158
|
+
r = c.post("/openai/v1/chat/completions", headers={"authorization": f"Bearer {tok}"},
|
|
159
|
+
json={"model": "gpt-4o", "messages": [{"role": "user", "content": "hi"}]})
|
|
160
|
+
assert r.status_code == 200 and "[proxyagent mock]" in r.json()["choices"][0]["message"]["content"]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_model_remap_reroutes_provider():
|
|
164
|
+
from proxyagent.aliases import remap
|
|
165
|
+
_aliases.set_map({"gpt-4o": "anthropic:mock"})
|
|
166
|
+
assert remap("openai", "gpt-4o") == ("anthropic", "mock")
|
|
167
|
+
assert remap("openai", "gpt-4o-mini") == ("openai", "gpt-4o-mini") # no match
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|