feat(support-chat): add admin smart chat settings

This commit is contained in:
devops committed 2026-10-14 19:44:24 +00:00
1 parent fa4f1124e6
commit b6735b5a9c
14 files changed
+285 -25

No files matched your search

+52
View File
@@ -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'])
+19 -12
View File
@@ -15,16 +15,24 @@ from policy import validate_message, safe_answer, research_query, safe_intent
from hermes_cli.config import load_config
from hermes_cli.runtime_provider import resolve_runtime_provider
from run_agent import AIAgent
from provider_catalog import configured_catalog, selected_runtime
async def catalog(request):
key = os.environ.get('SUPPORT_INTERNAL_KEY', '')
if len(key) < 32 or not hmac.compare_digest(request.headers.get('Authorization', ''), 'Bearer '+key):
return web.json_response({'answer': UNAVAILABLE}, status=401)
try:
return web.json_response({'providers': await asyncio.to_thread(configured_catalog)}, headers={'Cache-Control': 'no-store'})
except Exception:
return web.json_response({'answer': UNAVAILABLE}, status=503)
UNAVAILABLE = 'پشتیبان هوشمند فعلاً در دسترس نیست. لطفاً کمی بعد دوباره تلاش کنید یا تیکت پشتیبانی ثبت کنید.'
DOMAIN = 'فقط درباره محصولات و سلامت حیوانات خانگی و خدمات کنینا پاسخ می‌دهم. پیوند دلخواه یا موضوع خارج از این حوزه پذیرفته نمی‌شود.'
LIMIT = asyncio.Semaphore(2)
logging.disable(logging.CRITICAL)
def answer_once(message, catalog):
config = load_config()
model = config.get('model', {})
runtime = resolve_runtime_provider(requested=model.get('provider'), target_model=model.get('default'))
def answer_once(message, catalog, selection):
model, runtime = selected_runtime(selection)
research = None
query = research_query(message)
if query:
@@ -53,10 +61,8 @@ def answer_once(message, catalog):
finally:
with contextlib.suppress(Exception): agent.close()
def classify_once(message):
config = load_config()
model = config.get('model', {})
runtime = resolve_runtime_provider(requested=model.get('provider'), target_model=model.get('default'))
def classify_once(message, selection):
model, runtime = selected_runtime(selection)
agent = AIAgent(model=model.get('default'), provider=runtime.get('provider'), base_url=runtime.get('base_url'), api_key=runtime.get('api_key'), api_mode=runtime.get('api_mode'), enabled_toolsets=[], disabled_toolsets=['all'], skip_memory=True, skip_context_files=True, skip_background_review=True, load_soul_identity=False, session_id=str(uuid.uuid4()), session_db=None, save_trajectories=False, quiet_mode=True, max_iterations=2, max_tokens=1800, run_budget_seconds=35, checkpoints_enabled=False)
agent._persist_disabled = True
try:
@@ -77,14 +83,14 @@ async def classify(request):
return web.json_response({'answer': UNAVAILABLE}, status=401)
try:
data = await request.json()
if not isinstance(data, dict) or set(data) != {'message'}: raise ValueError('payload')
if not isinstance(data, dict) or set(data) != {'message', 'selection'}: raise ValueError('payload')
message = validate_message(data['message'])
except Exception:
return web.json_response({'answer': DOMAIN}, status=400)
if LIMIT.locked(): return web.json_response({'answer': UNAVAILABLE}, status=503)
try:
async with LIMIT:
intent = await asyncio.to_thread(classify_once, message)
intent = await asyncio.to_thread(classify_once, message, data['selection'])
return web.json_response({'intent': intent}, headers={'Cache-Control': 'no-store'})
except Exception:
return web.json_response({'answer': UNAVAILABLE}, status=503)
@@ -95,19 +101,20 @@ async def ask(request):
return web.json_response({'answer': UNAVAILABLE}, status=401)
try:
data = await request.json()
if set(data) != {'message','catalog'} or not isinstance(data['catalog'], list) or len(data['catalog']) > 80: raise ValueError('payload')
if set(data) != {'message','catalog', 'selection'} or not isinstance(data['catalog'], list) or len(data['catalog']) > 80: raise ValueError('payload')
message = validate_message(data['message'])
except Exception:
return web.json_response({'answer': DOMAIN}, status=400)
if LIMIT.locked(): return web.json_response({'answer': UNAVAILABLE}, status=503)
try:
async with LIMIT:
answer = await asyncio.to_thread(answer_once, message, data['catalog'])
answer = await asyncio.to_thread(answer_once, message, data['catalog'], data['selection'])
return web.json_response({'answer': answer}, headers={'Cache-Control':'no-store'})
except Exception:
return web.json_response({'answer': UNAVAILABLE}, status=503)
app = web.Application(client_max_size=100000)
app.router.add_get('/support/catalog', catalog)
app.router.add_post('/support/ask', ask)
app.router.add_post('/support/classify', classify)
if __name__ == '__main__':