hermes_service.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. """Yaovi front-end bridge over Hermes's own provider/auth/model management.
  2. One long-lived process per Hermes home (system root or one Bot's runtime home).
  3. Requests and replies are NDJSON on stdin/stdout:
  4. -> {"id": "1", "op": "oauth.start", "args": {...}}
  5. <- {"id": "1", "ok": true, "result": {...}} | {"id": "1", "ok": false, "status": 400, "error": "..."}
  6. Every op dispatches to the same functions Hermes's dashboard uses
  7. (hermes_cli/web_routers/{oauth,config_env,models}.py, agent/credential_pool.py,
  8. agent/auxiliary_client.py), so storage stays Hermes-native: config.yaml, .env, auth.json.
  9. """
  10. import asyncio
  11. import contextlib
  12. import json
  13. import math
  14. import os
  15. import sys
  16. import threading
  17. import time
  18. import types
  19. from argparse import Namespace
  20. from pathlib import Path
  21. ROOT = Path(__file__).resolve().parent
  22. sys.path.insert(0, str(ROOT / "hermes"))
  23. sys.path.insert(0, str(ROOT))
  24. WIRE = sys.stdout
  25. sys.stdout = sys.stderr # Hermes helpers print; keep the wire clean.
  26. _wire_lock = threading.Lock()
  27. # Native sections compiled into Bot runs; client reads are always redacted.
  28. DASHBOARD_ROUTES = [] # (method, path template, handler) recorded by the FastAPI shim
  29. CONFIG_KEYS = ("model", "providers", "custom_providers", "fallback_model", "auxiliary", "credential_pool_strategies", "image_gen", "video_gen", "web")
  30. class ServiceError(Exception):
  31. def __init__(self, status=None, detail="", *, status_code=None, headers=None):
  32. super().__init__(detail)
  33. self.status_code = status_code if status_code is not None else status
  34. self.detail = detail
  35. def _install_fastapi_shim():
  36. """The dashboard routers import FastAPI only for decorators/exceptions; Yaovi has no ASGI app."""
  37. if "fastapi" in sys.modules:
  38. return
  39. fa = types.ModuleType("fastapi")
  40. fa.__path__ = [] # a package, so ``fastapi.responses`` style imports resolve to the stubs below
  41. class APIRouter:
  42. """Records routes so ``dashboard.call`` can dispatch to the native handler functions."""
  43. def __init__(self, *a, **k):
  44. pass
  45. def _route(self, method):
  46. def register(path, *a, **k):
  47. def decorate(fn):
  48. DASHBOARD_ROUTES.append((method, path, fn))
  49. return fn
  50. return decorate
  51. return register
  52. def __getattr__(self, name):
  53. if name in ("get", "post", "put", "patch", "delete"):
  54. return self._route(name.upper())
  55. return lambda *a, **k: (lambda fn: fn)
  56. class Request:
  57. headers: dict = {}
  58. def _noop(*a, **k):
  59. return None
  60. fa.HTTPException = ServiceError
  61. fa.APIRouter = APIRouter
  62. fa.Request = Request
  63. def _default(default=None, *a, **k):
  64. # Query(20, ge=0) keeps its default so handlers called directly see native defaults.
  65. return None if default is Ellipsis else default
  66. for name in ("Depends", "WebSocket", "WebSocketDisconnect", "UploadFile"):
  67. setattr(fa, name, _noop)
  68. for name in ("Query", "Body", "Header", "File", "Form", "Path"):
  69. setattr(fa, name, _default)
  70. sys.modules["fastapi"] = fa
  71. responses = types.ModuleType("fastapi.responses")
  72. def _capture(self, *a, **k):
  73. self.args, self.kwargs = a, k
  74. for name in ("JSONResponse", "FileResponse", "HTMLResponse", "Response", "StreamingResponse", "PlainTextResponse", "RedirectResponse"):
  75. setattr(responses, name, type(name, (), {"__init__": _capture}))
  76. sys.modules["fastapi.responses"] = responses
  77. fa.responses = responses
  78. encoders = types.ModuleType("fastapi.encoders")
  79. def jsonable_encoder(value, *a, **k):
  80. if hasattr(value, "model_dump"):
  81. return value.model_dump(mode="json")
  82. if isinstance(value, dict):
  83. return {str(key): jsonable_encoder(item) for key, item in value.items()}
  84. if isinstance(value, (list, tuple, set)):
  85. return [jsonable_encoder(item) for item in value]
  86. return value
  87. encoders.jsonable_encoder = jsonable_encoder
  88. sys.modules["fastapi.encoders"] = encoders
  89. fa.encoders = encoders
  90. concurrency = types.ModuleType("fastapi.concurrency")
  91. async def run_in_threadpool(fn, *a, **k):
  92. return await asyncio.to_thread(fn, *a, **k)
  93. concurrency.run_in_threadpool = run_in_threadpool
  94. sys.modules["fastapi.concurrency"] = concurrency
  95. # Only stub starlette when it is genuinely absent: the mcp SDK imports the real package.
  96. try:
  97. import starlette.concurrency # noqa: F401
  98. except ImportError:
  99. starlette = types.ModuleType("starlette")
  100. sconc = types.ModuleType("starlette.concurrency")
  101. sconc.run_in_threadpool = run_in_threadpool
  102. sys.modules.setdefault("starlette", starlette)
  103. sys.modules.setdefault("starlette.concurrency", sconc)
  104. def _bootstrap():
  105. # In the embedded product, connecting an identity and choosing the default
  106. # model are separate user actions. Native Nous login otherwise moves a former
  107. # guest's welcome route to a new provider/model automatically after OAuth.
  108. from hermes_cli import anon_auth
  109. def keep_model_selection(_account_state):
  110. from hermes_cli.config import read_raw_config
  111. try:
  112. model = read_raw_config().get("model")
  113. except Exception:
  114. return {"model": "", "changed": False}
  115. selected = model.get("default", "") if isinstance(model, dict) else model or ""
  116. return {"model": str(selected), "changed": False}
  117. anon_auth.settle_after_upgrade = keep_model_selection
  118. # Both native Nous OAuth completion paths also save credentials through this
  119. # helper. Merge the new credential under Hermes's existing auth-store lock
  120. # without changing active_provider. Never restore an earlier snapshot: that
  121. # would race a user's explicit model selection in another request/process.
  122. # Explicit model selection uses _update_config_for_provider and remains intact.
  123. from hermes_cli import auth
  124. save_active_provider = auth._save_active_provider_state
  125. def save_identity_without_selecting_model(provider_id, state):
  126. if provider_id == "nous":
  127. return auth._persist_provider_state_to_store(
  128. provider_id, state, auth._auth_file_path(), set_active=False)
  129. return save_active_provider(provider_id, state)
  130. auth._save_active_provider_state = save_identity_without_selecting_model
  131. _install_fastapi_shim()
  132. # One process == one home: the dashboard's per-request profile scoping is unnecessary
  133. # (and depends on the TUI gateway, which the embedded runtime does not ship).
  134. import hermes_cli.web_server_profiles as profiles
  135. profiles._profile_scope = lambda profile=None: contextlib.nullcontext()
  136. profiles._config_profile_scope = lambda profile=None: contextlib.nullcontext()
  137. import hermes_cli.web_deps as deps
  138. original_late = deps.late
  139. def late(name, module="hermes_cli.web_server"):
  140. if name == "_require_token":
  141. return lambda request=None: None # Yaovi authorizes before forwarding.
  142. if name in ("_profile_scope", "_config_profile_scope"):
  143. return lambda profile=None: contextlib.nullcontext()
  144. if name == "_dashboard_code_skew_guard":
  145. return lambda: None
  146. return original_late(name, module)
  147. deps.late = late
  148. if os.environ.get("YAOVI_HERMES_TEST_OFFLINE"):
  149. # Test isolation: only loopback is reachable (local fake servers); every remote fetch
  150. # (models.dev, model catalog, OAuth) fails fast and Hermes falls back to bundled data.
  151. import socket
  152. # macOS system proxies (often a loopback proxy) would otherwise tunnel past the guard.
  153. for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"):
  154. os.environ.pop(key, None)
  155. os.environ["NO_PROXY"] = os.environ["no_proxy"] = "*"
  156. real_connect = socket.socket.connect
  157. def _loopback_only(sock, address):
  158. host = address[0] if isinstance(address, tuple) else address
  159. if isinstance(host, str) and host not in ("127.0.0.1", "::1", "localhost") and sock.family != socket.AF_UNIX:
  160. raise ConnectionRefusedError(f"network disabled in tests: {host}")
  161. return real_connect(sock, address)
  162. socket.socket.connect = _loopback_only
  163. real_getaddrinfo = socket.getaddrinfo
  164. def _no_remote_dns(host, *a, **k):
  165. if host not in (None, "127.0.0.1", "::1", "localhost"):
  166. raise socket.gaierror(f"network disabled in tests: {host}")
  167. return real_getaddrinfo(host, *a, **k)
  168. socket.getaddrinfo = _no_remote_dns
  169. # web_server itself needs real fastapi/uvicorn (it SystemExits without them). The routers
  170. # only reach into it for shared state via LateState, so provide that state in a stub.
  171. if "hermes_cli.web_server" not in sys.modules:
  172. import threading
  173. import types
  174. stub = types.ModuleType("hermes_cli.web_server")
  175. stub._CONFIG_MUTATION_LOCK = threading.RLock()
  176. sys.modules["hermes_cli.web_server"] = stub
  177. def _run(value):
  178. if asyncio.iscoroutine(value):
  179. return asyncio.run(value)
  180. return value
  181. def _model(cls_name, args):
  182. from hermes_cli import web_models
  183. return getattr(web_models, cls_name)(**args)
  184. def op_botmodels_options(args):
  185. """Bot model catalogue of this home (formerly the bridge plugin's /model-options)."""
  186. import hermes_bot_models
  187. return hermes_bot_models.options()
  188. def op_botmodels_resolve(args):
  189. """Validate and resolve one Bot's model settings against this home."""
  190. import hermes_bot_models
  191. try:
  192. return hermes_bot_models.resolve(args.get("settings"))
  193. except hermes_bot_models.ModelSettingsError as exc:
  194. raise ServiceError(400, str(exc)) from None
  195. def op_dashboard_call(args):
  196. """Native dashboard REST handler for this home (sessions, profiles, config, models, files)."""
  197. import hermes_dashboard
  198. return hermes_dashboard.call(DASHBOARD_ROUTES, ServiceError, args)
  199. # ── ops ────────────────────────────────────────────────────────────────────────────────────────
  200. def op_ping(args):
  201. from hermes_constants import get_hermes_home, get_default_hermes_root
  202. return {"home": str(get_hermes_home()), "root": str(get_default_hermes_root())}
  203. def op_network_test(args):
  204. import model_network
  205. return model_network.test(args.get("provider"))
  206. def op_providers_catalog(args):
  207. """Every Hermes provider: overlay metadata + label + aliases (full parity with `hermes model`)."""
  208. from hermes_cli import providers as p
  209. aliases = {}
  210. for alias, canonical in getattr(p, "ALIASES", {}).items():
  211. aliases.setdefault(canonical, []).append(alias)
  212. rows = []
  213. seen = set()
  214. try:
  215. from hermes_cli.provider_catalog import provider_catalog
  216. for d in provider_catalog():
  217. seen.add(d.slug)
  218. ov = p.HERMES_OVERLAYS.get(d.slug)
  219. rows.append({
  220. "id": d.slug, "label": d.label, "tab": getattr(d, "tab", ""),
  221. "authType": ov.auth_type if ov else "api_key", "transport": ov.transport if ov else "openai_chat",
  222. "aggregator": bool(ov and ov.is_aggregator), "signupUrl": getattr(d, "signup_url", "") or "",
  223. "envVars": list(getattr(d, "api_key_env_vars", ()) or ()), "aliases": sorted(aliases.get(d.slug, [])),
  224. })
  225. except Exception:
  226. pass
  227. for pid, ov in p.HERMES_OVERLAYS.items():
  228. if pid in seen:
  229. continue
  230. pdef = p.get_provider(pid, allow_network=False)
  231. rows.append({
  232. "id": pid, "label": p.get_label(pid), "tab": "",
  233. "authType": ov.auth_type, "transport": ov.transport, "aggregator": ov.is_aggregator,
  234. "signupUrl": "", "envVars": list(getattr(pdef, "api_key_env_vars", ()) or ()) + list(ov.extra_env_vars),
  235. "aliases": sorted(aliases.get(pid, [])),
  236. })
  237. return {"providers": rows}
  238. def _oauth():
  239. from hermes_cli.web_routers import oauth
  240. return oauth
  241. def op_oauth_list(args):
  242. import hermes_external_auth as ext
  243. return ext.decorate(_run(_oauth().list_oauth_providers()))
  244. def _ext_call(fn, *a):
  245. import hermes_external_auth as ext
  246. try:
  247. return fn(*a)
  248. except ext.AuthFlowError as e:
  249. raise ServiceError(e.status, str(e))
  250. def op_oauth_start(args):
  251. oauth = _oauth()
  252. provider = args["provider"]
  253. entry = next((p for p in oauth._OAUTH_PROVIDER_CATALOG if p["id"] == provider), None)
  254. import hermes_external_auth as ext
  255. if provider in ext.WEB_FLOWS:
  256. return _ext_call(ext.start, provider)
  257. if entry is None:
  258. raise ServiceError(400, f"Unknown provider {provider}")
  259. if entry["flow"] == "external":
  260. raise ServiceError(400, f"{provider} uses an external CLI; run `{entry['cli_command']}` on the server")
  261. if entry["flow"] != "device_code":
  262. raise ServiceError(400, "Unsupported flow")
  263. oauth._gc_oauth_sessions()
  264. return _run(oauth._start_device_code_flow(provider, profile=None))
  265. def op_oauth_poll(args):
  266. import hermes_external_auth as ext
  267. if ext.owns(args["sessionId"]):
  268. return _ext_call(ext.poll, args["provider"], args["sessionId"])
  269. return _run(_oauth().poll_oauth_session(args["provider"], args["sessionId"]))
  270. def op_oauth_cancel(args):
  271. import hermes_external_auth as ext
  272. if ext.owns(args["sessionId"]):
  273. return ext.cancel(args["sessionId"])
  274. return _run(_oauth().cancel_oauth_session(args["sessionId"], None))
  275. def op_oauth_submit(args):
  276. """Paste-token sign-in (Anthropic API key / `claude setup-token` token) into Hermes's .env."""
  277. import hermes_external_auth as ext
  278. return _ext_call(ext.submit_token, args["provider"], args.get("token", ""))
  279. def op_oauth_disconnect(args):
  280. if args["provider"] == "copilot":
  281. import hermes_external_auth as ext
  282. return ext.disconnect("copilot")
  283. return _run(_oauth().disconnect_oauth_provider(args["provider"], None))
  284. def _config_env():
  285. from hermes_cli.web_routers import config_env
  286. return config_env
  287. def _managed_env(key):
  288. return key.startswith("YAOVI_") or key in {"HOME", "PATH", "HERMES_HOME", "HERMES_PYTHON_SRC_ROOT", "PYTHONPATH", "PYTHONHOME"}
  289. def _env_writable(args):
  290. if _managed_env(args["key"]):
  291. raise ServiceError(400, "此变量由服务器运行环境管理,不能在 Hermes 环境变量中修改。")
  292. def op_env_list(args):
  293. return {key: {**row, "readonly": _managed_env(key)} for key, row in _run(_config_env().get_env_vars()).items()}
  294. def op_env_set(args):
  295. _env_writable(args)
  296. return _run(_config_env().set_env_var(_model("EnvVarUpdate", args)))
  297. def op_env_delete(args):
  298. _env_writable(args)
  299. return _run(_config_env().remove_env_var(_model("EnvVarDelete", args)))
  300. def _web_call(name):
  301. def run(args):
  302. import hermes_web
  303. try:
  304. return hermes_web.config() if name == "config" else getattr(hermes_web, name)(args)
  305. except ValueError as error:
  306. raise ServiceError(400, str(error))
  307. return run
  308. op_web_config = _web_call("config")
  309. op_web_provider = _web_call("provider")
  310. op_web_env = _web_call("env")
  311. op_web_search = _web_call("search")
  312. op_web_extract = _web_call("extract")
  313. op_web_test = _web_call("test")
  314. def op_providers_validate(args):
  315. return _run(_config_env().validate_provider_credential(_model("EnvVarUpdate", args), None))
  316. def op_endpoints_list(args):
  317. return _run(_config_env().list_custom_endpoints())
  318. def op_endpoints_upsert(args):
  319. return _run(_config_env().upsert_custom_endpoint(_model("CustomEndpointUpdate", args)))
  320. def op_endpoints_delete(args):
  321. return _run(_config_env().delete_custom_endpoint(args["id"]))
  322. def op_endpoints_validate(args):
  323. return _run(_config_env().validate_custom_endpoint(_model("CustomEndpointUpdate", args)))
  324. def _models():
  325. from hermes_cli.web_routers import models
  326. return models
  327. def op_model_options(args):
  328. result = _run(_models().get_model_options(refresh=bool(args.get("refresh")),
  329. include_unconfigured=bool(args.get("includeUnconfigured")),
  330. explicit_only=bool(args.get("explicitOnly"))))
  331. # The Hermes picker deliberately exposes a broad dial. Yaovi Auto needs the
  332. # native transport's actual vocabulary so its recorded effort matches the wire.
  333. from agent.reasoning_effort import codex_supported_efforts, grok_supported_efforts
  334. from providers import get_provider_profile
  335. for provider in result.get("providers", []):
  336. slug = provider.get("slug")
  337. capabilities = provider.setdefault("capabilities", {})
  338. for model in provider.get("models", []):
  339. caps = capabilities.setdefault(model, {})
  340. if caps.get("reasoning") is False:
  341. continue
  342. if slug == "openai-codex":
  343. levels = codex_supported_efforts(model)
  344. elif slug in ("xai", "xai-oauth"):
  345. levels = grok_supported_efforts(model)
  346. else:
  347. # A boolean reasoning flag is not a declaration of selectable levels.
  348. # Preserve explicit catalog metadata; only supplement profile-declared sets.
  349. if "supported_reasoning_efforts" in caps or "supported_reasoning_levels" in caps:
  350. continue
  351. profile = get_provider_profile(slug) if slug else None
  352. levels = profile.supported_reasoning_efforts(model) if profile else None
  353. if levels is not None:
  354. caps["supported_reasoning_efforts"] = list(levels)
  355. caps["reasoning"] = bool(levels)
  356. if levels and slug in ("openai-codex", "xai", "xai-oauth"):
  357. caps["default_reasoning_effort"] = "medium"
  358. return result
  359. def op_model_info(args):
  360. return _run(_models().get_model_info())
  361. def op_model_recommended(args):
  362. return _run(_models().get_recommended_default_model(provider=args.get("provider", "")))
  363. def op_model_auxiliary(args):
  364. return _run(_models().get_auxiliary_models())
  365. def op_model_set(args):
  366. return _run(_models().set_model_assignment(_model("ModelAssignment", args)))
  367. def _media_toolset(args):
  368. name = args.get("name")
  369. if name not in ("image_gen", "video_gen"):
  370. raise ServiceError(400, "Unknown media toolset")
  371. return name
  372. def op_media_config(args):
  373. from hermes_cli.web_routers import tools
  374. return _run(tools.get_toolset_config(_media_toolset(args)))
  375. def op_media_models(args):
  376. from hermes_cli.web_routers import tools
  377. return _run(tools.get_toolset_models(_media_toolset(args), provider=args.get("provider")))
  378. def op_media_provider(args):
  379. from hermes_cli.web_routers import tools
  380. return _run(tools.select_toolset_provider(_media_toolset(args), _model("ToolsetProviderSelect", {"provider": args["provider"]})))
  381. def op_media_model(args):
  382. from hermes_cli.web_routers import tools
  383. return _run(tools.select_toolset_model(_media_toolset(args), _model("ToolsetModelSelect", {"model": args["model"], "provider": args.get("provider")})))
  384. def op_media_env(args):
  385. from hermes_cli.web_routers import tools
  386. return _run(tools.save_toolset_env(_media_toolset(args), _model("ToolsetEnvUpdate", {"env": args["env"]})))
  387. def op_media_generate(args):
  388. """Use the native media dispatcher; Yaovi only publishes its resulting file as an attachment."""
  389. import base64
  390. import mimetypes
  391. from hermes_constants import get_hermes_home
  392. name = _media_toolset(args)
  393. # The service owns this temporary run overlay; never persist a Bot selection to config.yaml.
  394. from agent import runtime_context
  395. with _aux_lock:
  396. previous = runtime_context._context
  397. selection = args.get("selection")
  398. if name == "image_gen" and isinstance(selection, dict):
  399. from hermes_cli.config import load_config
  400. import copy
  401. config = copy.deepcopy(load_config())
  402. provider, model = selection.get("provider"), selection.get("model")
  403. if not isinstance(provider, str) or not isinstance(model, str) or not provider or not model:
  404. raise ServiceError(400, "Invalid image model selection")
  405. section = config.setdefault("image_gen", {})
  406. section.update({"provider": provider, "model": model, "allow_provider_fallback": False})
  407. section.setdefault(provider, {})["model"] = model
  408. runtime_context._context = {"config": config}
  409. try:
  410. if name == "image_gen":
  411. from tools.image_generation_tool import _handle_image_generate
  412. from agent.image_gen_provider import save_url_image
  413. arguments = args.get("arguments") or {}
  414. if arguments.get("image_url") or arguments.get("reference_image_urls"):
  415. from tools.image_generation_tool import _active_image_capabilities
  416. capabilities = _active_image_capabilities()
  417. if "image" not in capabilities.get("modalities", []) or capabilities.get("source_image_role") == "style":
  418. raise ServiceError(400, "Selected image model does not support source image editing; no generation was requested")
  419. result = _handle_image_generate(arguments)
  420. asset_key, save_url = "image", save_url_image
  421. else:
  422. from tools.video_generation_tool import _handle_video_generate
  423. from agent.video_gen_provider import save_url_video
  424. result = _handle_video_generate(args.get("arguments") or {})
  425. asset_key, save_url = "video", save_url_video
  426. finally:
  427. runtime_context._context = previous
  428. result = json.loads(result) if isinstance(result, str) else result
  429. if not isinstance(result, dict) or not result.get("success"):
  430. raise ServiceError(502, str((result.get("error") if isinstance(result, dict) else None) or "Hermes media generation failed"))
  431. asset = result.get(asset_key)
  432. if not isinstance(asset, str) or not asset:
  433. raise ServiceError(502, "Hermes returned no media file")
  434. path = save_url(asset) if asset.startswith(("https://", "http://")) else Path(asset)
  435. path = path.resolve()
  436. cache = (get_hermes_home() / "cache").resolve()
  437. if not path.is_relative_to(cache) or not path.is_file():
  438. raise ServiceError(502, "Hermes returned an invalid media cache path")
  439. limit = 32 * 1024 * 1024 if name == "image_gen" else 64 * 1024 * 1024
  440. with path.open("rb") as source:
  441. data = source.read(limit + 1)
  442. if not data or len(data) > limit:
  443. raise ServiceError(502, "Generated media is empty or exceeds the attachment limit")
  444. mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
  445. if not mime.startswith("image/" if name == "image_gen" else "video/"):
  446. raise ServiceError(502, "Hermes returned an invalid media type")
  447. return {"name": path.name, "mimeType": mime, "data": base64.b64encode(data).decode("ascii"),
  448. "provider": result.get("provider"), "model": result.get("model")}
  449. def op_config_get(args):
  450. """Raw (non-default-merged) provider sections of this home's config.yaml, secrets redacted."""
  451. from hermes_cli.config import read_raw_config
  452. raw = read_raw_config() or {}
  453. return {"config": _redact({k: raw[k] for k in CONFIG_KEYS if k in raw})}
  454. def op_config_compiled(args):
  455. """Unredacted provider sections, for compiling into a run config. Never forwarded to a client."""
  456. from hermes_cli.config import read_raw_config
  457. raw = read_raw_config() or {}
  458. return {"config": {k: raw[k] for k in CONFIG_KEYS if k in raw}}
  459. def op_config_patch(args):
  460. """Replace whole provider sections (null deletes) after Hermes's own structural validation."""
  461. from hermes_cli.config import read_raw_config, save_config, validate_config_structure
  462. patch = args.get("config") or {}
  463. bad = [k for k in patch if k not in CONFIG_KEYS]
  464. if bad:
  465. raise ServiceError(400, f"Unsupported config keys: {', '.join(bad)}")
  466. raw = read_raw_config() or {}
  467. for key, value in patch.items():
  468. if value is None:
  469. raw.pop(key, None)
  470. else:
  471. raw[key] = value
  472. issues = [i for i in validate_config_structure(raw) if getattr(i, "severity", "") == "error"]
  473. if issues:
  474. raise ServiceError(400, "; ".join(getattr(i, "message", str(i)) for i in issues))
  475. save_config(raw)
  476. return op_config_get({})
  477. def _redact(value):
  478. if isinstance(value, dict):
  479. return {k: ("***" if k in ("api_key", "apiKey", "token") and v else _redact(v)) for k, v in value.items()}
  480. if isinstance(value, list):
  481. return [_redact(v) for v in value]
  482. return value
  483. def _entry_view(index, entry):
  484. token = entry.access_token or ""
  485. return {
  486. "index": index, "id": entry.id, "label": entry.label, "authType": entry.auth_type, "source": entry.source,
  487. "priority": entry.priority, "baseUrl": entry.base_url, "lastStatus": entry.last_status,
  488. "lastError": entry.last_error_message, "tokenPreview": ("…" + token[-4:]) if len(token) > 8 else "",
  489. }
  490. def op_pool_list(args):
  491. from agent.credential_pool import load_pool
  492. from hermes_cli.config import read_raw_config
  493. pool = load_pool(args["provider"])
  494. strategies = (read_raw_config() or {}).get("credential_pool_strategies") or {}
  495. return {"provider": args["provider"], "strategy": strategies.get(args["provider"], "fill_first"),
  496. "entries": [_entry_view(i + 1, e) for i, e in enumerate(pool.entries())]}
  497. def op_pool_add(args):
  498. from agent.credential_pool import load_pool
  499. from hermes_cli.auth_commands import _add_api_key_credential
  500. if not (args.get("apiKey") or "").strip():
  501. raise ServiceError(400, "apiKey required")
  502. pool = load_pool(args["provider"])
  503. entry = _add_api_key_credential(Namespace(api_key=args["apiKey"], label=args.get("label") or ""), args["provider"], pool)
  504. return {"entry": _entry_view(len(pool.entries()), entry)}
  505. def op_pool_remove(args):
  506. from agent.credential_pool import load_pool
  507. pool = load_pool(args["provider"])
  508. removed = pool.remove_index(int(args["index"]))
  509. if removed is None:
  510. raise ServiceError(404, "credential not found")
  511. return {"ok": True}
  512. def op_pool_priority(args):
  513. from agent.credential_pool import load_pool
  514. pool = load_pool(args["provider"])
  515. moved = pool.move_entry(args["id"], int(args["priority"]))
  516. if moved is None:
  517. raise ServiceError(404, "credential not found")
  518. return {"ok": True}
  519. def op_auth_import_codex(args):
  520. """One-time import of a Yaovi-held Codex OAuth grant into Hermes's auth.json (no network)."""
  521. from hermes_cli.auth_codex import _save_codex_tokens
  522. tokens = {"access_token": args["accessToken"], "refresh_token": args["refreshToken"]}
  523. if args.get("accountId"):
  524. tokens["account_id"] = args["accountId"]
  525. _save_codex_tokens(tokens, set_active=False)
  526. return {"ok": True}
  527. _aux_lock = threading.RLock()
  528. def _aux_timeout(args):
  529. """A Host classification deadline includes time spent waiting for this shared process's lock."""
  530. deadline = args.get("deadlineAt")
  531. if deadline is None:
  532. return args.get("timeout")
  533. if isinstance(deadline, bool) or not isinstance(deadline, (int, float)) or not math.isfinite(deadline):
  534. raise ServiceError(400, "Invalid auxiliary deadline")
  535. remaining = (deadline - time.time() * 1000) / 1000
  536. if remaining <= 0:
  537. raise ServiceError(504, "Auxiliary classification deadline expired")
  538. timeout = args.get("timeout")
  539. return min(timeout, remaining) if isinstance(timeout, (int, float)) and timeout > 0 else remaining
  540. def op_aux_complete(args):
  541. """One auxiliary call through Hermes's own task routing (auxiliary.<task> → main model → fallback).
  542. ``config`` is the compiled run config (system layer + Bot overlay); it is installed as the
  543. embedded configuration for the duration of the call, exactly as a Bot run sees it."""
  544. from agent import runtime_context
  545. from agent.auxiliary_client import call_llm
  546. content = args["prompt"]
  547. images = args.get("images") or []
  548. if images:
  549. content = [{"type": "text", "text": args["prompt"]}] + [
  550. {"type": "image_url", "image_url": {"url": f"data:{i['mimeType']};base64,{i['data']}"}} for i in images]
  551. kwargs = {"task": args.get("task") or None, "messages": [{"role": "user", "content": content}],
  552. "max_tokens": args.get("maxTokens") or 1200, "timeout": args.get("timeout")}
  553. if args.get("provider"):
  554. kwargs["provider"] = args["provider"]
  555. if args.get("model"):
  556. kwargs["model"] = args["model"]
  557. with _aux_lock:
  558. kwargs["timeout"] = _aux_timeout(args)
  559. previous = runtime_context._context
  560. usage_observations = []
  561. restore_usage = lambda: None
  562. if args.get('usageObservation'):
  563. import dialogue_usage
  564. restore_usage = dialogue_usage.install({**args['usageObservation'], 'role': 'auxiliary', 'provider': args.get('provider', 'unknown')},
  565. lambda kind, **event: usage_observations.append(event['observation']))
  566. if isinstance(args.get("config"), dict):
  567. runtime_context._context = {"config": args["config"]}
  568. try:
  569. if not args.get("provider"):
  570. # Same main-route handoff a running agent gives its auxiliary calls ("auto" → main).
  571. with contextlib.suppress(Exception):
  572. from hermes_cli.config import load_config
  573. from hermes_cli.runtime_provider import resolve_runtime_provider
  574. main = (load_config().get("model") or {})
  575. if main.get("provider"):
  576. rt = resolve_runtime_provider(requested=main["provider"], target_model=main.get("default") or None)
  577. kwargs["main_runtime"] = {**rt, "model": main.get("default") or rt.get("model")}
  578. # Provider preparation also consumes the same deadline; never restart its budget here.
  579. kwargs["timeout"] = _aux_timeout(args)
  580. response = call_llm(**kwargs)
  581. finally:
  582. runtime_context._context = previous
  583. restore_usage()
  584. try:
  585. text = response.choices[0].message.content or ""
  586. except Exception:
  587. text = str(getattr(response, "output_text", "") or "")
  588. return {"text": text, **({'usageObservations': usage_observations} if args.get('usageObservation') else {})}
  589. def op_provider_runtime(args):
  590. """Resolved credentials for one Hermes provider (default: the main model's provider).
  591. Used only by Yaovi's media tools (image/STT/TTS), which call the OpenAI-shaped media
  592. endpoints directly; chat and auxiliary traffic never leaves Hermes."""
  593. from hermes_cli.config import load_config
  594. from hermes_cli.runtime_provider import resolve_runtime_provider
  595. main = load_config().get("model") or {}
  596. provider = args.get("provider") or main.get("provider") or None
  597. model = args.get("model") or (main.get("default") if provider == main.get("provider") else None)
  598. rt = resolve_runtime_provider(requested=provider, target_model=model or None)
  599. return {"provider": rt.get("provider") or provider or "", "base_url": rt.get("base_url") or "",
  600. "api_key": rt.get("api_key") or "", "api_mode": rt.get("api_mode") or "",
  601. "model": model or rt.get("model") or ""}
  602. OPS = {name[3:].replace("_", ".", 1): fn for name, fn in globals().items() if name.startswith("op_")}
  603. def emit(value):
  604. with _wire_lock:
  605. WIRE.write(json.dumps(value, ensure_ascii=False, default=str) + "\n")
  606. WIRE.flush()
  607. def handle(message):
  608. rid = message.get("id")
  609. fn = OPS.get(message.get("op", ""))
  610. if fn is None:
  611. emit({"id": rid, "ok": False, "status": 404, "error": f"unknown op {message.get('op')}"})
  612. return
  613. try:
  614. # Embedded-config readers and writers share the lock so a request cannot
  615. # observe another Bot's temporary media/auxiliary overlay. These two ops
  616. # only read persisted config.yaml via Hermes's atomic-write/cache path;
  617. # they never consult the overlay and must not queue behind slow I/O.
  618. import model_network
  619. args = message.get("args") or {}
  620. op = message.get("op", "")
  621. with (contextlib.nullcontext() if op in ('config.get', 'config.compiled') else _aux_lock), model_network.scope(message.get("network"), args.get("provider") if op.startswith("oauth.") or op == "network.test" else None,
  622. login=op in ("oauth.start", "oauth.submit", "oauth.poll"), model_operation=not op.startswith("web.")):
  623. result = fn(args)
  624. emit({"id": rid, "ok": True, "result": result})
  625. except ServiceError as e:
  626. emit({"id": rid, "ok": False, "status": e.status_code, "error": public_error(e.detail, message.get("network"))})
  627. except BaseException as e: # noqa: BLE001 — every failure (even SystemExit) must reach the caller
  628. status = getattr(e, "status_code", 500)
  629. emit({"id": rid, "ok": False, "status": status if isinstance(status, int) else 500,
  630. "error": public_error(str(getattr(e, "detail", "") or e) or type(e).__name__, message.get("network"))})
  631. def public_error(error, snapshot):
  632. from urllib.parse import urlsplit, unquote
  633. text = str(error)
  634. if snapshot:
  635. values = [snapshot.get("default"), *snapshot.get("connections", {}).values(), *snapshot.get("botConnections", {}).values()]
  636. for value in values:
  637. if not isinstance(value, str) or value == "environment":
  638. continue
  639. parsed = urlsplit(value)
  640. text = text.replace(value, "[代理地址]")
  641. if parsed.password:
  642. text = text.replace(parsed.password, "[已隐藏]").replace(unquote(parsed.password), "[已隐藏]")
  643. return text
  644. def main():
  645. os.environ.setdefault("HERMES_SKIP_UPDATE_CHECK", "1")
  646. os.environ["HERMES_PYTHON_SRC_ROOT"] = str(ROOT / "hermes")
  647. _bootstrap()
  648. import model_network
  649. model_network.install()
  650. import signal
  651. signal.signal(signal.SIGTERM,lambda *_:sys.exit(0))
  652. emit({"type": "ready"})
  653. for line in sys.stdin:
  654. line = line.strip()
  655. if not line:
  656. continue
  657. try:
  658. message = json.loads(line)
  659. except ValueError:
  660. continue
  661. threading.Thread(target=handle, args=(message,), daemon=True).start()
  662. if __name__ == "__main__":
  663. main()