hermes_service.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_openviking_connection(args):
  450. from native_openviking import connection
  451. from hermes_constants import get_hermes_home
  452. return connection(get_hermes_home())
  453. def _openviking_config_call(name, args):
  454. import native_openviking_config as native
  455. from hermes_constants import get_hermes_home
  456. try:
  457. if name == 'read': return native.read_configuration(get_hermes_home())
  458. if name == 'test': return native.test_configuration(get_hermes_home(), args.get('config'))
  459. if name == 'prepare': return native.prepare(get_hermes_home(), args.get('config'))
  460. return native.transaction(args.get('token'), args.get('action'), get_hermes_home())
  461. except native.ConfigurationError as error:
  462. raise ServiceError(error.status, str(error)) from None
  463. def op_openviking_test(args):
  464. return _openviking_config_call('test', args)
  465. def op_openviking_config(args):
  466. return _openviking_config_call('read', args)
  467. def op_openviking_config_prepare(args):
  468. return _openviking_config_call('prepare', args)
  469. def op_openviking_config_transaction(args):
  470. return _openviking_config_call('transaction', args)
  471. def op_openviking_remove_peer(args):
  472. from native_openviking import connection_client
  473. from plugins.memory.openviking import _resolve_user_space
  474. import re
  475. peer = str(args.get("peerId", ""))
  476. if not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,127}", peer):
  477. raise ServiceError(400, "peer 名称无效")
  478. client = connection_client(op_openviking_connection({}), peer)
  479. user = _resolve_user_space(client, timeout=5, raise_on_timeout=True)
  480. if not user or user != args.get("user"):
  481. raise ServiceError(409, "原 OpenViking user 未确认,保留待清理记录")
  482. try:
  483. client.delete("/api/v1/fs", params={"uri": f"viking://user/{user}/peers/{peer}", "recursive": True}, timeout=30)
  484. except Exception as exc:
  485. if getattr(exc, "status_code", None) != 404:
  486. raise ServiceError(502, "peer 清理失败,将保留待重试记录") from None
  487. return {"ok": True}
  488. def op_config_get(args):
  489. """Raw (non-default-merged) provider sections of this home's config.yaml, secrets redacted."""
  490. from hermes_cli.config import read_raw_config
  491. raw = read_raw_config() or {}
  492. return {"config": _redact({k: raw[k] for k in CONFIG_KEYS if k in raw})}
  493. def op_config_compiled(args):
  494. """Unredacted provider sections, for compiling into a run config. Never forwarded to a client."""
  495. from hermes_cli.config import read_raw_config
  496. raw = read_raw_config() or {}
  497. return {"config": {k: raw[k] for k in CONFIG_KEYS if k in raw}}
  498. def op_config_patch(args):
  499. """Replace whole provider sections (null deletes) after Hermes's own structural validation."""
  500. from hermes_cli.config import read_raw_config, save_config, validate_config_structure
  501. patch = args.get("config") or {}
  502. bad = [k for k in patch if k not in CONFIG_KEYS]
  503. if bad:
  504. raise ServiceError(400, f"Unsupported config keys: {', '.join(bad)}")
  505. raw = read_raw_config() or {}
  506. for key, value in patch.items():
  507. if value is None:
  508. raw.pop(key, None)
  509. else:
  510. raw[key] = value
  511. issues = [i for i in validate_config_structure(raw) if getattr(i, "severity", "") == "error"]
  512. if issues:
  513. raise ServiceError(400, "; ".join(getattr(i, "message", str(i)) for i in issues))
  514. save_config(raw)
  515. return op_config_get({})
  516. def _redact(value):
  517. if isinstance(value, dict):
  518. return {k: ("***" if k in ("api_key", "apiKey", "token") and v else _redact(v)) for k, v in value.items()}
  519. if isinstance(value, list):
  520. return [_redact(v) for v in value]
  521. return value
  522. def _entry_view(index, entry):
  523. token = entry.access_token or ""
  524. return {
  525. "index": index, "id": entry.id, "label": entry.label, "authType": entry.auth_type, "source": entry.source,
  526. "priority": entry.priority, "baseUrl": entry.base_url, "lastStatus": entry.last_status,
  527. "lastError": entry.last_error_message, "tokenPreview": ("…" + token[-4:]) if len(token) > 8 else "",
  528. }
  529. def op_pool_list(args):
  530. from agent.credential_pool import load_pool
  531. from hermes_cli.config import read_raw_config
  532. pool = load_pool(args["provider"])
  533. strategies = (read_raw_config() or {}).get("credential_pool_strategies") or {}
  534. return {"provider": args["provider"], "strategy": strategies.get(args["provider"], "fill_first"),
  535. "entries": [_entry_view(i + 1, e) for i, e in enumerate(pool.entries())]}
  536. def op_pool_add(args):
  537. from agent.credential_pool import load_pool
  538. from hermes_cli.auth_commands import _add_api_key_credential
  539. if not (args.get("apiKey") or "").strip():
  540. raise ServiceError(400, "apiKey required")
  541. pool = load_pool(args["provider"])
  542. entry = _add_api_key_credential(Namespace(api_key=args["apiKey"], label=args.get("label") or ""), args["provider"], pool)
  543. return {"entry": _entry_view(len(pool.entries()), entry)}
  544. def op_pool_remove(args):
  545. from agent.credential_pool import load_pool
  546. pool = load_pool(args["provider"])
  547. removed = pool.remove_index(int(args["index"]))
  548. if removed is None:
  549. raise ServiceError(404, "credential not found")
  550. return {"ok": True}
  551. def op_pool_priority(args):
  552. from agent.credential_pool import load_pool
  553. pool = load_pool(args["provider"])
  554. moved = pool.move_entry(args["id"], int(args["priority"]))
  555. if moved is None:
  556. raise ServiceError(404, "credential not found")
  557. return {"ok": True}
  558. def op_auth_import_codex(args):
  559. """One-time import of a Yaovi-held Codex OAuth grant into Hermes's auth.json (no network)."""
  560. from hermes_cli.auth_codex import _save_codex_tokens
  561. tokens = {"access_token": args["accessToken"], "refresh_token": args["refreshToken"]}
  562. if args.get("accountId"):
  563. tokens["account_id"] = args["accountId"]
  564. _save_codex_tokens(tokens, set_active=False)
  565. return {"ok": True}
  566. _aux_lock = threading.RLock()
  567. def _aux_timeout(args):
  568. """A Host classification deadline includes time spent waiting for this shared process's lock."""
  569. deadline = args.get("deadlineAt")
  570. if deadline is None:
  571. return args.get("timeout")
  572. if isinstance(deadline, bool) or not isinstance(deadline, (int, float)) or not math.isfinite(deadline):
  573. raise ServiceError(400, "Invalid auxiliary deadline")
  574. remaining = (deadline - time.time() * 1000) / 1000
  575. if remaining <= 0:
  576. raise ServiceError(504, "Auxiliary classification deadline expired")
  577. timeout = args.get("timeout")
  578. return min(timeout, remaining) if isinstance(timeout, (int, float)) and timeout > 0 else remaining
  579. def op_aux_complete(args):
  580. """One auxiliary call through Hermes's own task routing (auxiliary.<task> → main model → fallback).
  581. ``config`` is the compiled run config (system layer + Bot overlay); it is installed as the
  582. embedded configuration for the duration of the call, exactly as a Bot run sees it."""
  583. from agent import runtime_context
  584. from agent.auxiliary_client import call_llm
  585. content = args["prompt"]
  586. images = args.get("images") or []
  587. if images:
  588. content = [{"type": "text", "text": args["prompt"]}] + [
  589. {"type": "image_url", "image_url": {"url": f"data:{i['mimeType']};base64,{i['data']}"}} for i in images]
  590. kwargs = {"task": args.get("task") or None, "messages": [{"role": "user", "content": content}],
  591. "max_tokens": args.get("maxTokens") or 1200, "timeout": args.get("timeout")}
  592. if args.get("provider"):
  593. kwargs["provider"] = args["provider"]
  594. if args.get("model"):
  595. kwargs["model"] = args["model"]
  596. with _aux_lock:
  597. kwargs["timeout"] = _aux_timeout(args)
  598. previous = runtime_context._context
  599. usage_observations = []
  600. restore_usage = lambda: None
  601. if args.get('usageObservation'):
  602. import dialogue_usage
  603. restore_usage = dialogue_usage.install({**args['usageObservation'], 'role': 'auxiliary', 'provider': args.get('provider', 'unknown')},
  604. lambda kind, **event: usage_observations.append(event['observation']))
  605. if isinstance(args.get("config"), dict):
  606. runtime_context._context = {"config": args["config"]}
  607. try:
  608. if not args.get("provider"):
  609. # Same main-route handoff a running agent gives its auxiliary calls ("auto" → main).
  610. with contextlib.suppress(Exception):
  611. from hermes_cli.config import load_config
  612. from hermes_cli.runtime_provider import resolve_runtime_provider
  613. main = (load_config().get("model") or {})
  614. if main.get("provider"):
  615. rt = resolve_runtime_provider(requested=main["provider"], target_model=main.get("default") or None)
  616. kwargs["main_runtime"] = {**rt, "model": main.get("default") or rt.get("model")}
  617. # Provider preparation also consumes the same deadline; never restart its budget here.
  618. kwargs["timeout"] = _aux_timeout(args)
  619. response = call_llm(**kwargs)
  620. finally:
  621. runtime_context._context = previous
  622. restore_usage()
  623. try:
  624. text = response.choices[0].message.content or ""
  625. except Exception:
  626. text = str(getattr(response, "output_text", "") or "")
  627. return {"text": text, **({'usageObservations': usage_observations} if args.get('usageObservation') else {})}
  628. def op_provider_runtime(args):
  629. """Resolved credentials for one Hermes provider (default: the main model's provider).
  630. Used only by Yaovi's media tools (image/STT/TTS), which call the OpenAI-shaped media
  631. endpoints directly; chat and auxiliary traffic never leaves Hermes."""
  632. from hermes_cli.config import load_config
  633. from hermes_cli.runtime_provider import resolve_runtime_provider
  634. main = load_config().get("model") or {}
  635. provider = args.get("provider") or main.get("provider") or None
  636. model = args.get("model") or (main.get("default") if provider == main.get("provider") else None)
  637. rt = resolve_runtime_provider(requested=provider, target_model=model or None)
  638. return {"provider": rt.get("provider") or provider or "", "base_url": rt.get("base_url") or "",
  639. "api_key": rt.get("api_key") or "", "api_mode": rt.get("api_mode") or "",
  640. "model": model or rt.get("model") or ""}
  641. def _mcp_call(fn):
  642. def run(args):
  643. import hermes_mcp
  644. try:
  645. return getattr(hermes_mcp, fn)(args)
  646. except hermes_mcp.McpError as e:
  647. raise ServiceError(e.status, str(e))
  648. return run
  649. op_mcp_list = _mcp_call("op_list")
  650. op_mcp_raw = _mcp_call("op_raw")
  651. op_mcp_upsert = _mcp_call("op_upsert")
  652. op_mcp_remove = _mcp_call("op_remove")
  653. op_mcp_enabled = _mcp_call("op_enabled")
  654. op_mcp_test = _mcp_call("op_test")
  655. op_mcp_authStart = _mcp_call("op_auth_start")
  656. op_mcp_authStatus = _mcp_call("op_auth_status")
  657. op_mcp_authCancel = _mcp_call("op_auth_cancel")
  658. op_mcp_authCallback = _mcp_call("op_auth_callback")
  659. def _channels_call(fn):
  660. def run(args):
  661. import hermes_channels
  662. try:
  663. return getattr(hermes_channels, fn)(args)
  664. except hermes_channels.ChannelError as e:
  665. raise ServiceError(e.status, str(e))
  666. return run
  667. op_channels_prepare = _channels_call("op_prepare")
  668. op_channels_whatsappStart = _channels_call("op_whatsapp_start")
  669. op_channels_whatsappApply = _channels_call("op_whatsapp_apply")
  670. op_channels_whatsappStatus = _channels_call("op_whatsapp_status")
  671. op_channels_whatsappCancel = _channels_call("op_whatsapp_cancel")
  672. op_channels_list = _channels_call("op_list")
  673. op_channels_enabled = _channels_call("op_enabled")
  674. op_channels_upsert = _channels_call("op_upsert")
  675. op_channels_setEnabled = _channels_call("op_set_enabled")
  676. op_channels_remove = _channels_call("op_remove")
  677. op_channels_test = _channels_call("op_test")
  678. op_channels_pairingApprove = _channels_call("op_pairing_approve")
  679. op_channels_pairingRevoke = _channels_call("op_pairing_revoke")
  680. def op_ecosystem_skills(args):
  681. from hermes_ecosystem import skills_catalog
  682. return skills_catalog()
  683. def op_ecosystem_bundle(args):
  684. from hermes_ecosystem import skill_bundle
  685. return skill_bundle(args['sourcePath'], args.get('fingerprint'))
  686. OPS = {name[3:].replace("_", ".", 1): fn for name, fn in globals().items() if name.startswith("op_")}
  687. def emit(value):
  688. with _wire_lock:
  689. WIRE.write(json.dumps(value, ensure_ascii=False, default=str) + "\n")
  690. WIRE.flush()
  691. def handle(message):
  692. rid = message.get("id")
  693. fn = OPS.get(message.get("op", ""))
  694. if fn is None:
  695. emit({"id": rid, "ok": False, "status": 404, "error": f"unknown op {message.get('op')}"})
  696. return
  697. try:
  698. # Embedded-config readers and writers share the lock so a request cannot
  699. # observe another Bot's temporary media/auxiliary overlay. These two ops
  700. # only read persisted config.yaml via Hermes's atomic-write/cache path;
  701. # they never consult the overlay and must not queue behind slow I/O.
  702. import model_network
  703. args = message.get("args") or {}
  704. op = message.get("op", "")
  705. 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,
  706. login=op in ("oauth.start", "oauth.submit", "oauth.poll"), model_operation=not op.startswith(("mcp.", "channels.", "web.", "ecosystem."))):
  707. result = fn(args)
  708. emit({"id": rid, "ok": True, "result": result})
  709. except ServiceError as e:
  710. emit({"id": rid, "ok": False, "status": e.status_code, "error": public_error(e.detail, message.get("network"))})
  711. except BaseException as e: # noqa: BLE001 — every failure (even SystemExit) must reach the caller
  712. status = getattr(e, "status_code", 500)
  713. emit({"id": rid, "ok": False, "status": status if isinstance(status, int) else 500,
  714. "error": public_error(str(getattr(e, "detail", "") or e) or type(e).__name__, message.get("network"))})
  715. def public_error(error, snapshot):
  716. from urllib.parse import urlsplit, unquote
  717. text = str(error)
  718. if snapshot:
  719. values = [snapshot.get("default"), *snapshot.get("connections", {}).values(), *snapshot.get("botConnections", {}).values()]
  720. for value in values:
  721. if not isinstance(value, str) or value == "environment":
  722. continue
  723. parsed = urlsplit(value)
  724. text = text.replace(value, "[代理地址]")
  725. if parsed.password:
  726. text = text.replace(parsed.password, "[已隐藏]").replace(unquote(parsed.password), "[已隐藏]")
  727. return text
  728. def main():
  729. os.environ.setdefault("HERMES_SKIP_UPDATE_CHECK", "1")
  730. os.environ["HERMES_PYTHON_SRC_ROOT"] = str(ROOT / "hermes")
  731. _bootstrap()
  732. import model_network
  733. model_network.install()
  734. import atexit,signal
  735. def cleanup():
  736. from hermes_pairing import manager
  737. manager.close()
  738. atexit.register(cleanup)
  739. signal.signal(signal.SIGTERM,lambda *_:sys.exit(0))
  740. emit({"type": "ready"})
  741. for line in sys.stdin:
  742. line = line.strip()
  743. if not line:
  744. continue
  745. try:
  746. message = json.loads(line)
  747. except ValueError:
  748. continue
  749. threading.Thread(target=handle, args=(message,), daemon=True).start()
  750. if __name__ == "__main__":
  751. main()