-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadmin_api.py
More file actions
392 lines (317 loc) · 13 KB
/
Copy pathadmin_api.py
File metadata and controls
392 lines (317 loc) · 13 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
Admin API endpoints for the CodexBridge WebUI.
Provides CRUD for providers, settings, and connection testing.
"""
import json
import os
import time
import httpx
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Any, Dict, List, Optional
router = APIRouter(prefix="/api", tags=["admin"])
# Will be set by proxy.py after initialization
pm = None
CONFIG_PATH = ""
def init_admin(manager, config_path: str):
global pm, CONFIG_PATH
pm = manager
CONFIG_PATH = config_path
def _sync_codex_config():
"""Write ~/.codex/config.toml and auth.json so Codex works immediately."""
active_id = pm.config.get("active_provider", "")
pcfg = pm.config.get("providers", {}).get(active_id, {})
if not pcfg:
return
model = (pcfg.get("models") or ["unknown"])[0]
port = pm.config.get("port", 8787)
codex_dir = os.path.expanduser("~/.codex")
os.makedirs(codex_dir, exist_ok=True)
# auth.json
auth_path = os.path.join(codex_dir, "auth.json")
auth = {"OPENAI_API_KEY": "codexbridge-local"}
with open(auth_path, "w") as f:
json.dump(auth, f, indent=2)
# config.toml — preserve existing non-CodexBridge sections
config_path = os.path.join(codex_dir, "config.toml")
existing = ""
if os.path.exists(config_path):
with open(config_path) as f:
existing = f.read()
# Build CodexBridge provider block
cb_block = (
f'[model_providers.codexbridge]\n'
f'name = "{model}"\n'
f'base_url = "http://127.0.0.1:{port}/v1"\n'
f'wire_api = "responses"\n'
f'requires_openai_auth = true\n'
f'request_max_retries = 1\n'
)
# Parse existing config to preserve non-codexbridge sections
lines = existing.split("\n")
new_lines = []
skip = False
for line in lines:
if line.strip().startswith("[model_providers.codexbridge]"):
skip = True
continue
if skip and line.strip().startswith("["):
skip = False
if skip and not line.strip():
continue
if not skip:
new_lines.append(line)
# Insert global settings at top if not present
content = "\n".join(new_lines).strip()
has_provider = "model_provider" in content
has_model = content.startswith('model =') or '\nmodel =' in content
parts = []
if not has_model:
parts.append(f'model = "{model}"')
if not has_provider:
parts.append('model_provider = "codexbridge"')
if parts:
content = "\n".join(parts) + "\n\n" + content
content = content.strip() + "\n\n" + cb_block
with open(config_path, "w") as f:
f.write(content)
# ------------------------------------------------------------------ #
# Models
# ------------------------------------------------------------------ #
class ProviderCreate(BaseModel):
name: str
api_format: str = "openai"
base_url: str = ""
api_key: str = ""
models: List[str] = []
has_reasoning: bool = False
note: str = ""
website: str = ""
class ProviderUpdate(BaseModel):
name: Optional[str] = None
api_format: Optional[str] = None
base_url: Optional[str] = None
api_key: Optional[str] = None
models: Optional[List[str]] = None
has_reasoning: Optional[bool] = None
note: Optional[str] = None
website: Optional[str] = None
class TestRequest(BaseModel):
base_url: str = ""
api_key: str = ""
api_format: str = "openai"
model: str = ""
class SettingsUpdate(BaseModel):
active_provider: Optional[str] = None
port: Optional[int] = None
# ------------------------------------------------------------------ #
# Provider CRUD
# ------------------------------------------------------------------ #
@router.get("/providers")
async def list_providers():
providers = []
for pid, p in pm.providers.items():
pcfg = pm.config.get("providers", {}).get(pid, {})
providers.append({
"id": pid,
"name": p.name,
"api_format": getattr(p, "api_format", "openai"),
"base_url": p.base_url,
"api_key": p.api_key,
"models": p.models,
"has_reasoning": p.has_reasoning,
"note": pcfg.get("note", ""),
"website": pcfg.get("website", ""),
"is_active": pid == pm.config.get("active_provider"),
})
return {"data": providers, "active_provider": pm.config.get("active_provider")}
@router.post("/providers")
async def create_provider(body: ProviderCreate):
pid = body.name.lower().replace(" ", "-").replace("/", "-")
pid = "".join(c for c in pid if c.isalnum() or c == "-")
if not pid:
pid = f"provider-{int(time.time())}"
if pid in pm.providers:
pid = f"{pid}-{int(time.time())}"
pcfg = {
"name": body.name,
"api_format": body.api_format,
"base_url": body.base_url,
"api_key": body.api_key,
"models": body.models,
"has_reasoning": body.has_reasoning,
}
if body.note:
pcfg["note"] = body.note
if body.website:
pcfg["website"] = body.website
pm.config.setdefault("providers", {})[pid] = pcfg
pm.config["active_provider"] = pid
pm.save_config()
from providers import PROVIDER_CLASSES
cls = PROVIDER_CLASSES.get(body.api_format, PROVIDER_CLASSES["openai"])
pm.providers[pid] = cls(pid, pcfg)
_sync_codex_config()
return {"status": "ok", "provider": {"id": pid, "name": body.name}}
@router.put("/providers/{provider_id}")
async def update_provider(provider_id: str, body: ProviderUpdate):
if provider_id not in pm.providers:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
pcfg = pm.config.get("providers", {}).get(provider_id, {})
updates = body.dict(exclude_none=True)
for k, v in updates.items():
if k in ("name", "api_format", "base_url", "api_key", "models", "has_reasoning", "note", "website"):
pcfg[k] = v
pm.config["providers"][provider_id] = pcfg
pm.save_config()
from providers import PROVIDER_CLASSES
api_format = pcfg.get("api_format", "openai")
cls = PROVIDER_CLASSES.get(api_format, PROVIDER_CLASSES["openai"])
pm.providers[provider_id] = cls(provider_id, pcfg)
_sync_codex_config()
return {"status": "ok"}
@router.delete("/providers/{provider_id}")
async def delete_provider(provider_id: str):
if provider_id not in pm.providers:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
del pm.providers[provider_id]
pm.config.get("providers", {}).pop(provider_id, None)
if pm.config.get("active_provider") == provider_id:
remaining = list(pm.providers.keys())
pm.config["active_provider"] = remaining[0] if remaining else ""
pm.save_config()
return {"status": "ok"}
@router.post("/providers/{provider_id}/enable")
async def enable_provider(provider_id: str):
if provider_id not in pm.providers:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
pm.config["active_provider"] = provider_id
pm.save_config()
_sync_codex_config()
return {"status": "ok", "active_provider": provider_id}
@router.post("/providers/{provider_id}/duplicate")
async def duplicate_provider(provider_id: str):
if provider_id not in pm.providers:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
pcfg = pm.config.get("providers", {}).get(provider_id, {})
new_pcfg = dict(pcfg)
new_name = f"{pcfg.get('name', provider_id)} (Copy)"
new_pcfg["name"] = new_name
new_pid = f"{provider_id}-copy-{int(time.time())}"
pm.config.setdefault("providers", {})[new_pid] = new_pcfg
pm.save_config()
from providers import PROVIDER_CLASSES
cls = PROVIDER_CLASSES.get(new_pcfg.get("api_format", "openai"), PROVIDER_CLASSES["openai"])
pm.providers[new_pid] = cls(new_pid, new_pcfg)
return {"status": "ok", "provider": {"id": new_pid, "name": new_name}}
# ------------------------------------------------------------------ #
# Connection Testing
# ------------------------------------------------------------------ #
@router.post("/test")
async def test_connection(body: TestRequest):
"""Test an API connection by sending a minimal chat completion request."""
base_url = body.base_url.rstrip("/")
api_key = body.api_key
model = body.model
api_format = body.api_format
start = time.time()
try:
if api_format == "anthropic":
# Anthropic: base_url could be https://api.anthropic.com or https://api.anthropic.com/v1
test_url = base_url
if not test_url.endswith("/v1"):
test_url = f"{test_url}/v1"
url = f"{test_url}/messages"
headers = {
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
}
payload = {
"model": model or "claude-sonnet-4-20250514",
"max_tokens": 32,
"messages": [{"role": "user", "content": "Hi, reply with just 'OK'."}],
}
else:
# OpenAI-compatible: ensure base_url ends with /v1 before appending /chat/completions
test_url = base_url
if not test_url.endswith("/v1"):
test_url = f"{test_url}/v1"
url = f"{test_url}/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"model": model or "deepseek-chat",
"max_tokens": 32,
"messages": [{"role": "user", "content": "Hi, reply with just 'OK'."}],
}
async with httpx.AsyncClient(timeout=30, follow_redirects=True, proxy=None) as client:
resp = await client.post(url, json=payload, headers=headers)
elapsed = int((time.time() - start) * 1000)
if resp.status_code == 200:
return {"status": "ok", "latency_ms": elapsed, "message": "连接成功"}
else:
detail = resp.text[:300]
return {"status": "error", "latency_ms": elapsed, "message": f"HTTP {resp.status_code}: {detail}"}
except httpx.TimeoutException:
elapsed = int((time.time() - start) * 1000)
return {"status": "error", "latency_ms": elapsed, "message": "连接超时(30秒)"}
except httpx.ConnectError as e:
elapsed = int((time.time() - start) * 1000)
return {"status": "error", "latency_ms": elapsed, "message": f"无法连接到服务器: {e}"}
except Exception as e:
elapsed = int((time.time() - start) * 1000)
return {"status": "error", "latency_ms": elapsed, "message": str(e)}
@router.post("/fetch-models")
async def fetch_models(body: TestRequest):
"""Fetch available models from an API endpoint."""
base_url = body.base_url.rstrip("/")
api_key = body.api_key
api_format = body.api_format
try:
if api_format == "anthropic":
return {"status": "ok", "models": ["claude-sonnet-4-20250514", "claude-opus-4-20250514", "claude-3-5-haiku-20241022"]}
# Ensure base_url ends with /v1 before appending /models
test_url = base_url
if not test_url.endswith("/v1"):
test_url = f"{test_url}/v1"
url = f"{test_url}/models"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
async with httpx.AsyncClient(timeout=15, follow_redirects=True, proxy=None) as client:
resp = await client.get(url, headers=headers)
if resp.status_code == 200:
data = resp.json()
models = [m.get("id", "") for m in data.get("data", [])]
if not models:
return {"status": "error", "message": "模型列表为空", "models": []}
return {"status": "ok", "models": models}
else:
return {"status": "error", "message": f"HTTP {resp.status_code}: {resp.text[:200]}", "models": []}
except Exception as e:
return {"status": "error", "message": str(e), "models": []}
# ------------------------------------------------------------------ #
# Settings
# ------------------------------------------------------------------ #
@router.get("/settings")
async def get_settings():
return {
"active_provider": pm.config.get("active_provider", ""),
"port": pm.config.get("port", 8787),
}
@router.put("/settings")
async def update_settings(body: SettingsUpdate):
if body.active_provider is not None:
pm.config["active_provider"] = body.active_provider
if body.port is not None:
pm.config["port"] = body.port
pm.save_config()
return {"status": "ok"}
@router.get("/config/raw")
async def get_raw_config():
"""Return the raw config.json for preview."""
return {"config": pm.config}