feat(support-chat): add admin smart chat settings
This commit is contained in:
1 parent
fa4f1124e6
commit
b6735b5a9c
14 files changed
+285
-25
No files matched your search
@@ -0,0 +1,52 @@
|
||||
"""Server-owned configured provider catalog. Never accepts endpoint URLs."""
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
import urllib.request
|
||||
from hermes_cli.config import load_config
|
||||
from hermes_cli.runtime_provider import resolve_runtime_provider
|
||||
|
||||
_cache = (0, [])
|
||||
_SAFE = re.compile(r'^[\w .:/@+\[\]()-]{1,160}$', re.ASCII)
|
||||
|
||||
def configured_catalog():
|
||||
global _cache
|
||||
if time.monotonic() - _cache[0] < 60:
|
||||
return _cache[1]
|
||||
result = []
|
||||
config = load_config()
|
||||
for provider in config.get('custom_providers', []):
|
||||
name = provider.get('name', '')
|
||||
if not _SAFE.fullmatch(name) or '://' in name:
|
||||
continue
|
||||
pid = 'configured-' + hashlib.sha256(name.encode()).hexdigest()[:16]
|
||||
try:
|
||||
runtime = resolve_runtime_provider(requested='custom:' + name)
|
||||
endpoint = runtime['base_url'].rstrip('/') + '/models'
|
||||
req = urllib.request.Request(endpoint, headers={'Authorization': 'Bearer ' + runtime['api_key']})
|
||||
# Endpoint comes exclusively from server configuration, never request data.
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
def redirect_request(self, *args, **kwargs):
|
||||
return None
|
||||
with urllib.request.build_opener(NoRedirect).open(req, timeout=8) as response:
|
||||
raw = response.read(2000000)
|
||||
live = {m.get('id') for m in json.loads(raw).get('data', []) if isinstance(m, dict)}
|
||||
models = [m for m in provider.get('models', []) if isinstance(m, str) and _SAFE.fullmatch(m) and '://' not in m and m in live]
|
||||
if models:
|
||||
result.append({'id': pid, 'label': name, 'models': sorted(set(models))})
|
||||
except Exception:
|
||||
continue
|
||||
if not result:
|
||||
raise ValueError('catalog unavailable')
|
||||
_cache = (time.monotonic(), result)
|
||||
return result
|
||||
|
||||
def selected_runtime(selection):
|
||||
if not isinstance(selection, dict) or set(selection) != {'provider', 'model'}:
|
||||
raise ValueError('selection')
|
||||
catalog = configured_catalog()
|
||||
item = next((p for p in catalog if p['id'] == selection['provider'] and selection['model'] in p['models']), None)
|
||||
if item is None:
|
||||
raise ValueError('selection')
|
||||
return {'default': selection['model']}, resolve_runtime_provider(requested='custom:' + item['label'], target_model=selection['model'])
|
||||
Reference in new issue
Block a user