hermes_service.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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_dashboard_call(args):
  185. """Native dashboard REST handler for this home (sessions, profiles, config, models, files)."""
  186. import hermes_dashboard
  187. return hermes_dashboard.call(DASHBOARD_ROUTES, ServiceError, args)
  188. # ── ops ────────────────────────────────────────────────────────────────────────────────────────
  189. def op_ping(args):
  190. from hermes_constants import get_hermes_home, get_default_hermes_root
  191. return {"home": str(get_hermes_home()), "root": str(get_default_hermes_root())}
  192. def op_network_test(args):
  193. import model_network
  194. return model_network.test(args.get("provider"))
  195. def op_providers_catalog(args):
  196. """Every Hermes provider: overlay metadata + label + aliases (full parity with `hermes model`)."""
  197. from hermes_cli import providers as p
  198. aliases = {}
  199. for alias, canonical in getattr(p, "ALIASES", {}).items():
  200. aliases.setdefault(canonical, []).append(alias)
  201. rows = []
  202. seen = set()
  203. try:
  204. from hermes_cli.provider_catalog import provider_catalog
  205. for d in provider_catalog():
  206. seen.add(d.slug)
  207. ov = p.HERMES_OVERLAYS.get(d.slug)
  208. rows.append({
  209. "id": d.slug, "label": d.label, "tab": getattr(d, "tab", ""),
  210. "authType": ov.auth_type if ov else "api_key", "transport": ov.transport if ov else "openai_chat",
  211. "aggregator": bool(ov and ov.is_aggregator), "signupUrl": getattr(d, "signup_url", "") or "",
  212. "envVars": list(getattr(d, "api_key_env_vars", ()) or ()), "aliases": sorted(aliases.get(d.slug, [])),
  213. })
  214. except Exception:
  215. pass
  216. for pid, ov in p.HERMES_OVERLAYS.items():
  217. if pid in seen:
  218. continue
  219. pdef = p.get_provider(pid, allow_network=False)
  220. rows.append({
  221. "id": pid, "label": p.get_label(pid), "tab": "",
  222. "authType": ov.auth_type, "transport": ov.transport, "aggregator": ov.is_aggregator,
  223. "signupUrl": "", "envVars": list(getattr(pdef, "api_key_env_vars", ()) or ()) + list(ov.extra_env_vars),
  224. "aliases": sorted(aliases.get(pid, [])),
  225. })
  226. return {"providers": rows}
  227. def _oauth():
  228. from hermes_cli.web_routers import oauth
  229. return oauth
  230. def op_oauth_list(args):
  231. import hermes_external_auth as ext
  232. return ext.decorate(_run(_oauth().list_oauth_providers()))
  233. def _ext_call(fn, *a):
  234. import hermes_external_auth as ext
  235. try:
  236. return fn(*a)
  237. except ext.AuthFlowError as e:
  238. raise ServiceError(e.status, str(e))
  239. def op_oauth_start(args):
  240. oauth = _oauth()
  241. provider = args["provider"]
  242. entry = next((p for p in oauth._OAUTH_PROVIDER_CATALOG if p["id"] == provider), None)
  243. import hermes_external_auth as ext
  244. if provider in ext.WEB_FLOWS:
  245. return _ext_call(ext.start, provider)
  246. if entry is None:
  247. raise ServiceError(400, f"Unknown provider {provider}")
  248. if entry["flow"] == "external":
  249. raise ServiceError(400, f"{provider} uses an external CLI; run `{entry['cli_command']}` on the server")
  250. if entry["flow"] != "device_code":
  251. raise ServiceError(400, "Unsupported flow")
  252. oauth._gc_oauth_sessions()
  253. return _run(oauth._start_device_code_flow(provider, profile=None))
  254. def op_oauth_poll(args):
  255. import hermes_external_auth as ext
  256. if ext.owns(args["sessionId"]):
  257. return _ext_call(ext.poll, args["provider"], args["sessionId"])
  258. return _run(_oauth().poll_oauth_session(args["provider"], args["sessionId"]))
  259. def op_oauth_cancel(args):
  260. import hermes_external_auth as ext
  261. if ext.owns(args["sessionId"]):
  262. return ext.cancel(args["sessionId"])
  263. return _run(_oauth().cancel_oauth_session(args["sessionId"], None))
  264. def op_oauth_submit(args):
  265. """Paste-token sign-in (Anthropic API key / `claude setup-token` token) into Hermes's .env."""
  266. import hermes_external_auth as ext
  267. return _ext_call(ext.submit_token, args["provider"], args.get("token", ""))
  268. def op_oauth_disconnect(args):
  269. if args["provider"] == "copilot":
  270. import hermes_external_auth as ext
  271. return ext.disconnect("copilot")
  272. return _run(_oauth().disconnect_oauth_provider(args["provider"], None))
  273. def _config_env():
  274. from hermes_cli.web_routers import config_env
  275. return config_env
  276. def _managed_env(key):
  277. return key.startswith("YAOVI_") or key in {"HOME", "PATH", "HERMES_HOME", "HERMES_PYTHON_SRC_ROOT", "PYTHONPATH", "PYTHONHOME"}
  278. def _env_writable(args):
  279. if _managed_env(args["key"]):
  280. raise ServiceError(400, "此变量由服务器运行环境管理,不能在 Hermes 环境变量中修改。")
  281. def op_env_list(args):
  282. return {key: {**row, "readonly": _managed_env(key)} for key, row in _run(_config_env().get_env_vars()).items()}
  283. def op_env_set(args):
  284. _env_writable(args)
  285. return _run(_config_env().set_env_var(_model("EnvVarUpdate", args)))
  286. def op_env_delete(args):
  287. _env_writable(args)
  288. return _run(_config_env().remove_env_var(_model("EnvVarDelete", args)))
  289. def _web_call(name):
  290. def run(args):
  291. import hermes_web
  292. try:
  293. return hermes_web.config() if name == "config" else getattr(hermes_web, name)(args)
  294. except ValueError as error:
  295. raise ServiceError(400, str(error))
  296. return run
  297. op_web_config = _web_call("config")
  298. op_web_provider = _web_call("provider")
  299. op_web_env = _web_call("env")
  300. op_web_search = _web_call("search")
  301. op_web_extract = _web_call("extract")
  302. op_web_test = _web_call("test")
  303. def op_providers_validate(args):
  304. return _run(_config_env().validate_provider_credential(_model("EnvVarUpdate", args), None))
  305. def op_endpoints_list(args):
  306. return _run(_config_env().list_custom_endpoints())
  307. def op_endpoints_upsert(args):
  308. return _run(_config_env().upsert_custom_endpoint(_model("CustomEndpointUpdate", args)))
  309. def op_endpoints_delete(args):
  310. return _run(_config_env().delete_custom_endpoint(args["id"]))
  311. def op_endpoints_validate(args):
  312. return _run(_config_env().validate_custom_endpoint(_model("CustomEndpointUpdate", args)))
  313. def _models():
  314. from hermes_cli.web_routers import models
  315. return models
  316. def op_model_options(args):
  317. result = _run(_models().get_model_options(refresh=bool(args.get("refresh")),
  318. include_unconfigured=bool(args.get("includeUnconfigured")),
  319. explicit_only=bool(args.get("explicitOnly"))))
  320. # The Hermes picker deliberately exposes a broad dial. Yaovi Auto needs the
  321. # native transport's actual vocabulary so its recorded effort matches the wire.
  322. from agent.reasoning_effort import codex_supported_efforts, grok_supported_efforts
  323. from providers import get_provider_profile
  324. for provider in result.get("providers", []):
  325. slug = provider.get("slug")
  326. capabilities = provider.setdefault("capabilities", {})
  327. for model in provider.get("models", []):
  328. caps = capabilities.setdefault(model, {})
  329. if caps.get("reasoning") is False:
  330. continue
  331. if slug == "openai-codex":
  332. levels = codex_supported_efforts(model)
  333. elif slug in ("xai", "xai-oauth"):
  334. levels = grok_supported_efforts(model)
  335. else:
  336. # A boolean reasoning flag is not a declaration of selectable levels.
  337. # Preserve explicit catalog metadata; only supplement profile-declared sets.
  338. if "supported_reasoning_efforts" in caps or "supported_reasoning_levels" in caps:
  339. continue
  340. profile = get_provider_profile(slug) if slug else None
  341. levels = profile.supported_reasoning_efforts(model) if profile else None
  342. if levels is not None:
  343. caps["supported_reasoning_efforts"] = list(levels)
  344. caps["reasoning"] = bool(levels)
  345. if levels and slug in ("openai-codex", "xai", "xai-oauth"):
  346. caps["default_reasoning_effort"] = "medium"
  347. return result
  348. def op_model_info(args):
  349. return _run(_models().get_model_info())
  350. def op_model_recommended(args):
  351. return _run(_models().get_recommended_default_model(provider=args.get("provider", "")))
  352. def op_model_auxiliary(args):
  353. return _run(_models().get_auxiliary_models())
  354. def op_model_set(args):
  355. return _run(_models().set_model_assignment(_model("ModelAssignment", args)))
  356. def _media_toolset(args):
  357. name = args.get("name")
  358. if name not in ("image_gen", "video_gen"):
  359. raise ServiceError(400, "Unknown media toolset")
  360. return name
  361. def op_media_config(args):
  362. from hermes_cli.web_routers import tools
  363. return _run(tools.get_toolset_config(_media_toolset(args)))
  364. def op_media_models(args):
  365. from hermes_cli.web_routers import tools
  366. return _run(tools.get_toolset_models(_media_toolset(args), provider=args.get("provider")))
  367. def op_media_provider(args):
  368. from hermes_cli.web_routers import tools
  369. return _run(tools.select_toolset_provider(_media_toolset(args), _model("ToolsetProviderSelect", {"provider": args["provider"]})))
  370. def op_media_model(args):
  371. from hermes_cli.web_routers import tools
  372. return _run(tools.select_toolset_model(_media_toolset(args), _model("ToolsetModelSelect", {"model": args["model"], "provider": args.get("provider")})))
  373. def op_media_env(args):
  374. from hermes_cli.web_routers import tools
  375. return _run(tools.save_toolset_env(_media_toolset(args), _model("ToolsetEnvUpdate", {"env": args["env"]})))
  376. def op_media_generate(args):
  377. """Use the native media dispatcher; Yaovi only publishes its resulting file as an attachment."""
  378. import base64
  379. import mimetypes
  380. from hermes_constants import get_hermes_home
  381. name = _media_toolset(args)
  382. # The service owns this temporary run overlay; never persist a Bot selection to config.yaml.
  383. from agent import runtime_context
  384. with _aux_lock:
  385. previous = runtime_context._context
  386. selection = args.get("selection")
  387. if name == "image_gen" and isinstance(selection, dict):
  388. from hermes_cli.config import load_config
  389. import copy
  390. config = copy.deepcopy(load_config())
  391. provider, model = selection.get("provider"), selection.get("model")
  392. if not isinstance(provider, str) or not isinstance(model, str) or not provider or not model:
  393. raise ServiceError(400, "Invalid image model selection")
  394. section = config.setdefault("image_gen", {})
  395. section.update({"provider": provider, "model": model, "allow_provider_fallback": False})
  396. section.setdefault(provider, {})["model"] = model
  397. runtime_context._context = {"config": config}
  398. try:
  399. if name == "image_gen":
  400. from tools.image_generation_tool import _handle_image_generate
  401. from agent.image_gen_provider import save_url_image
  402. arguments = args.get("arguments") or {}
  403. if arguments.get("image_url") or arguments.get("reference_image_urls"):
  404. from tools.image_generation_tool import _active_image_capabilities
  405. capabilities = _active_image_capabilities()
  406. if "image" not in capabilities.get("modalities", []) or capabilities.get("source_image_role") == "style":
  407. raise ServiceError(400, "Selected image model does not support source image editing; no generation was requested")
  408. result = _handle_image_generate(arguments)
  409. asset_key, save_url = "image", save_url_image
  410. else:
  411. from tools.video_generation_tool import _handle_video_generate
  412. from agent.video_gen_provider import save_url_video
  413. result = _handle_video_generate(args.get("arguments") or {})
  414. asset_key, save_url = "video", save_url_video
  415. finally:
  416. runtime_context._context = previous
  417. result = json.loads(result) if isinstance(result, str) else result
  418. if not isinstance(result, dict) or not result.get("success"):
  419. raise ServiceError(502, str((result.get("error") if isinstance(result, dict) else None) or "Hermes media generation failed"))
  420. asset = result.get(asset_key)
  421. if not isinstance(asset, str) or not asset:
  422. raise ServiceError(502, "Hermes returned no media file")
  423. path = save_url(asset) if asset.startswith(("https://", "http://")) else Path(asset)
  424. path = path.resolve()
  425. cache = (get_hermes_home() / "cache").resolve()
  426. if not path.is_relative_to(cache) or not path.is_file():
  427. raise ServiceError(502, "Hermes returned an invalid media cache path")
  428. limit = 32 * 1024 * 1024 if name == "image_gen" else 64 * 1024 * 1024
  429. with path.open("rb") as source:
  430. data = source.read(limit + 1)
  431. if not data or len(data) > limit:
  432. raise ServiceError(502, "Generated media is empty or exceeds the attachment limit")
  433. mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
  434. if not mime.startswith("image/" if name == "image_gen" else "video/"):
  435. raise ServiceError(502, "Hermes returned an invalid media type")
  436. return {"name": path.name, "mimeType": mime, "data": base64.b64encode(data).decode("ascii"),
  437. "provider": result.get("provider"), "model": result.get("model")}
  438. def op_openviking_connection(args):
  439. from native_openviking import connection
  440. from hermes_constants import get_hermes_home
  441. return connection(get_hermes_home())
  442. def _openviking_config_call(name, args):
  443. import native_openviking_config as native
  444. from hermes_constants import get_hermes_home
  445. try:
  446. if name == 'read': return native.read_configuration(get_hermes_home())
  447. if name == 'test': return native.test_configuration(get_hermes_home(), args.get('config'))
  448. if name == 'prepare': return native.prepare(get_hermes_home(), args.get('config'))
  449. return native.transaction(args.get('token'), args.get('action'), get_hermes_home())
  450. except native.ConfigurationError as error:
  451. raise ServiceError(error.status, str(error)) from None
  452. def op_openviking_test(args):
  453. return _openviking_config_call('test', args)
  454. def op_openviking_config(args):
  455. return _openviking_config_call('read', args)
  456. def op_openviking_config_prepare(args):
  457. return _openviking_config_call('prepare', args)
  458. def op_openviking_config_transaction(args):
  459. return _openviking_config_call('transaction', args)
  460. def op_openviking_remove_peer(args):
  461. from native_openviking import connection_client
  462. from plugins.memory.openviking import _resolve_user_space
  463. import re
  464. peer = str(args.get("peerId", ""))
  465. if not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,127}", peer):
  466. raise ServiceError(400, "peer 名称无效")
  467. client = connection_client(op_openviking_connection({}), peer)
  468. user = _resolve_user_space(client, timeout=5, raise_on_timeout=True)
  469. if not user or user != args.get("user"):
  470. raise ServiceError(409, "原 OpenViking user 未确认,保留待清理记录")
  471. try:
  472. client.delete("/api/v1/fs", params={"uri": f"viking://user/{user}/peers/{peer}", "recursive": True}, timeout=30)
  473. except Exception as exc:
  474. if getattr(exc, "status_code", None) != 404:
  475. raise ServiceError(502, "peer 清理失败,将保留待重试记录") from None
  476. return {"ok": True}
  477. def op_config_get(args):
  478. """Raw (non-default-merged) provider sections of this home's config.yaml, secrets redacted."""
  479. from hermes_cli.config import read_raw_config
  480. raw = read_raw_config() or {}
  481. return {"config": _redact({k: raw[k] for k in CONFIG_KEYS if k in raw})}
  482. def op_config_compiled(args):
  483. """Unredacted provider sections, for compiling into a run config. Never forwarded to a client."""
  484. from hermes_cli.config import read_raw_config
  485. raw = read_raw_config() or {}
  486. return {"config": {k: raw[k] for k in CONFIG_KEYS if k in raw}}
  487. def op_config_patch(args):
  488. """Replace whole provider sections (null deletes) after Hermes's own structural validation."""
  489. from hermes_cli.config import read_raw_config, save_config, validate_config_structure
  490. patch = args.get("config") or {}
  491. bad = [k for k in patch if k not in CONFIG_KEYS]
  492. if bad:
  493. raise ServiceError(400, f"Unsupported config keys: {', '.join(bad)}")
  494. raw = read_raw_config() or {}
  495. for key, value in patch.items():
  496. if value is None:
  497. raw.pop(key, None)
  498. else:
  499. raw[key] = value
  500. issues = [i for i in validate_config_structure(raw) if getattr(i, "severity", "") == "error"]
  501. if issues:
  502. raise ServiceError(400, "; ".join(getattr(i, "message", str(i)) for i in issues))
  503. save_config(raw)
  504. return op_config_get({})
  505. def _redact(value):
  506. if isinstance(value, dict):
  507. return {k: ("***" if k in ("api_key", "apiKey", "token") and v else _redact(v)) for k, v in value.items()}
  508. if isinstance(value, list):
  509. return [_redact(v) for v in value]
  510. return value
  511. def _entry_view(index, entry):
  512. token = entry.access_token or ""
  513. return {
  514. "index": index, "id": entry.id, "label": entry.label, "authType": entry.auth_type, "source": entry.source,
  515. "priority": entry.priority, "baseUrl": entry.base_url, "lastStatus": entry.last_status,
  516. "lastError": entry.last_error_message, "tokenPreview": ("…" + token[-4:]) if len(token) > 8 else "",
  517. }
  518. def op_pool_list(args):
  519. from agent.credential_pool import load_pool
  520. from hermes_cli.config import read_raw_config
  521. pool = load_pool(args["provider"])
  522. strategies = (read_raw_config() or {}).get("credential_pool_strategies") or {}
  523. return {"provider": args["provider"], "strategy": strategies.get(args["provider"], "fill_first"),
  524. "entries": [_entry_view(i + 1, e) for i, e in enumerate(pool.entries())]}
  525. def op_pool_add(args):
  526. from agent.credential_pool import load_pool
  527. from hermes_cli.auth_commands import _add_api_key_credential
  528. if not (args.get("apiKey") or "").strip():
  529. raise ServiceError(400, "apiKey required")
  530. pool = load_pool(args["provider"])
  531. entry = _add_api_key_credential(Namespace(api_key=args["apiKey"], label=args.get("label") or ""), args["provider"], pool)
  532. return {"entry": _entry_view(len(pool.entries()), entry)}
  533. def op_pool_remove(args):
  534. from agent.credential_pool import load_pool
  535. pool = load_pool(args["provider"])
  536. removed = pool.remove_index(int(args["index"]))
  537. if removed is None:
  538. raise ServiceError(404, "credential not found")
  539. return {"ok": True}
  540. def op_pool_priority(args):
  541. from agent.credential_pool import load_pool
  542. pool = load_pool(args["provider"])
  543. moved = pool.move_entry(args["id"], int(args["priority"]))
  544. if moved is None:
  545. raise ServiceError(404, "credential not found")
  546. return {"ok": True}
  547. def op_auth_import_codex(args):
  548. """One-time import of a Yaovi-held Codex OAuth grant into Hermes's auth.json (no network)."""
  549. from hermes_cli.auth_codex import _save_codex_tokens
  550. tokens = {"access_token": args["accessToken"], "refresh_token": args["refreshToken"]}
  551. if args.get("accountId"):
  552. tokens["account_id"] = args["accountId"]
  553. _save_codex_tokens(tokens, set_active=False)
  554. return {"ok": True}
  555. _aux_lock = threading.RLock()
  556. def _aux_timeout(args):
  557. """A Host classification deadline includes time spent waiting for this shared process's lock."""
  558. deadline = args.get("deadlineAt")
  559. if deadline is None:
  560. return args.get("timeout")
  561. if isinstance(deadline, bool) or not isinstance(deadline, (int, float)) or not math.isfinite(deadline):
  562. raise ServiceError(400, "Invalid auxiliary deadline")
  563. remaining = (deadline - time.time() * 1000) / 1000
  564. if remaining <= 0:
  565. raise ServiceError(504, "Auxiliary classification deadline expired")
  566. timeout = args.get("timeout")
  567. return min(timeout, remaining) if isinstance(timeout, (int, float)) and timeout > 0 else remaining
  568. def op_aux_complete(args):
  569. """One auxiliary call through Hermes's own task routing (auxiliary.<task> → main model → fallback).
  570. ``config`` is the compiled run config (system layer + Bot overlay); it is installed as the
  571. embedded configuration for the duration of the call, exactly as a Bot run sees it."""
  572. from agent import runtime_context
  573. from agent.auxiliary_client import call_llm
  574. content = args["prompt"]
  575. images = args.get("images") or []
  576. if images:
  577. content = [{"type": "text", "text": args["prompt"]}] + [
  578. {"type": "image_url", "image_url": {"url": f"data:{i['mimeType']};base64,{i['data']}"}} for i in images]
  579. kwargs = {"task": args.get("task") or None, "messages": [{"role": "user", "content": content}],
  580. "max_tokens": args.get("maxTokens") or 1200, "timeout": args.get("timeout")}
  581. if args.get("provider"):
  582. kwargs["provider"] = args["provider"]
  583. if args.get("model"):
  584. kwargs["model"] = args["model"]
  585. with _aux_lock:
  586. kwargs["timeout"] = _aux_timeout(args)
  587. previous = runtime_context._context
  588. usage_observations = []
  589. restore_usage = lambda: None
  590. if args.get('usageObservation'):
  591. import dialogue_usage
  592. restore_usage = dialogue_usage.install({**args['usageObservation'], 'role': 'auxiliary', 'provider': args.get('provider', 'unknown')},
  593. lambda kind, **event: usage_observations.append(event['observation']))
  594. if isinstance(args.get("config"), dict):
  595. runtime_context._context = {"config": args["config"]}
  596. try:
  597. if not args.get("provider"):
  598. # Same main-route handoff a running agent gives its auxiliary calls ("auto" → main).
  599. with contextlib.suppress(Exception):
  600. from hermes_cli.config import load_config
  601. from hermes_cli.runtime_provider import resolve_runtime_provider
  602. main = (load_config().get("model") or {})
  603. if main.get("provider"):
  604. rt = resolve_runtime_provider(requested=main["provider"], target_model=main.get("default") or None)
  605. kwargs["main_runtime"] = {**rt, "model": main.get("default") or rt.get("model")}
  606. # Provider preparation also consumes the same deadline; never restart its budget here.
  607. kwargs["timeout"] = _aux_timeout(args)
  608. response = call_llm(**kwargs)
  609. finally:
  610. runtime_context._context = previous
  611. restore_usage()
  612. try:
  613. text = response.choices[0].message.content or ""
  614. except Exception:
  615. text = str(getattr(response, "output_text", "") or "")
  616. return {"text": text, **({'usageObservations': usage_observations} if args.get('usageObservation') else {})}
  617. def op_provider_runtime(args):
  618. """Resolved credentials for one Hermes provider (default: the main model's provider).
  619. Used only by Yaovi's media tools (image/STT/TTS), which call the OpenAI-shaped media
  620. endpoints directly; chat and auxiliary traffic never leaves Hermes."""
  621. from hermes_cli.config import load_config
  622. from hermes_cli.runtime_provider import resolve_runtime_provider
  623. main = load_config().get("model") or {}
  624. provider = args.get("provider") or main.get("provider") or None
  625. model = args.get("model") or (main.get("default") if provider == main.get("provider") else None)
  626. rt = resolve_runtime_provider(requested=provider, target_model=model or None)
  627. return {"provider": rt.get("provider") or provider or "", "base_url": rt.get("base_url") or "",
  628. "api_key": rt.get("api_key") or "", "api_mode": rt.get("api_mode") or "",
  629. "model": model or rt.get("model") or ""}
  630. def _mcp_call(fn):
  631. def run(args):
  632. import hermes_mcp
  633. try:
  634. return getattr(hermes_mcp, fn)(args)
  635. except hermes_mcp.McpError as e:
  636. raise ServiceError(e.status, str(e))
  637. return run
  638. op_mcp_list = _mcp_call("op_list")
  639. op_mcp_raw = _mcp_call("op_raw")
  640. op_mcp_upsert = _mcp_call("op_upsert")
  641. op_mcp_remove = _mcp_call("op_remove")
  642. op_mcp_enabled = _mcp_call("op_enabled")
  643. op_mcp_test = _mcp_call("op_test")
  644. op_mcp_authStart = _mcp_call("op_auth_start")
  645. op_mcp_authStatus = _mcp_call("op_auth_status")
  646. op_mcp_authCancel = _mcp_call("op_auth_cancel")
  647. op_mcp_authCallback = _mcp_call("op_auth_callback")
  648. def _channels_call(fn):
  649. def run(args):
  650. import hermes_channels
  651. try:
  652. return getattr(hermes_channels, fn)(args)
  653. except hermes_channels.ChannelError as e:
  654. raise ServiceError(e.status, str(e))
  655. return run
  656. op_channels_prepare = _channels_call("op_prepare")
  657. op_channels_whatsappStart = _channels_call("op_whatsapp_start")
  658. op_channels_whatsappApply = _channels_call("op_whatsapp_apply")
  659. op_channels_whatsappStatus = _channels_call("op_whatsapp_status")
  660. op_channels_whatsappCancel = _channels_call("op_whatsapp_cancel")
  661. op_channels_list = _channels_call("op_list")
  662. op_channels_enabled = _channels_call("op_enabled")
  663. op_channels_upsert = _channels_call("op_upsert")
  664. op_channels_setEnabled = _channels_call("op_set_enabled")
  665. op_channels_remove = _channels_call("op_remove")
  666. op_channels_test = _channels_call("op_test")
  667. op_channels_pairingApprove = _channels_call("op_pairing_approve")
  668. op_channels_pairingRevoke = _channels_call("op_pairing_revoke")
  669. def op_ecosystem_skills(args):
  670. from hermes_ecosystem import skills_catalog
  671. return skills_catalog()
  672. def op_ecosystem_bundle(args):
  673. from hermes_ecosystem import skill_bundle
  674. return skill_bundle(args['sourcePath'], args.get('fingerprint'))
  675. OPS = {name[3:].replace("_", ".", 1): fn for name, fn in globals().items() if name.startswith("op_")}
  676. def emit(value):
  677. with _wire_lock:
  678. WIRE.write(json.dumps(value, ensure_ascii=False, default=str) + "\n")
  679. WIRE.flush()
  680. def handle(message):
  681. rid = message.get("id")
  682. fn = OPS.get(message.get("op", ""))
  683. if fn is None:
  684. emit({"id": rid, "ok": False, "status": 404, "error": f"unknown op {message.get('op')}"})
  685. return
  686. try:
  687. # Embedded-config readers and writers share the lock so a request cannot
  688. # observe another Bot's temporary media/auxiliary overlay. These two ops
  689. # only read persisted config.yaml via Hermes's atomic-write/cache path;
  690. # they never consult the overlay and must not queue behind slow I/O.
  691. import model_network
  692. args = message.get("args") or {}
  693. op = message.get("op", "")
  694. 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,
  695. login=op in ("oauth.start", "oauth.submit", "oauth.poll"), model_operation=not op.startswith(("mcp.", "channels.", "web.", "ecosystem."))):
  696. result = fn(args)
  697. emit({"id": rid, "ok": True, "result": result})
  698. except ServiceError as e:
  699. emit({"id": rid, "ok": False, "status": e.status_code, "error": public_error(e.detail, message.get("network"))})
  700. except BaseException as e: # noqa: BLE001 — every failure (even SystemExit) must reach the caller
  701. status = getattr(e, "status_code", 500)
  702. emit({"id": rid, "ok": False, "status": status if isinstance(status, int) else 500,
  703. "error": public_error(str(getattr(e, "detail", "") or e) or type(e).__name__, message.get("network"))})
  704. def public_error(error, snapshot):
  705. from urllib.parse import urlsplit, unquote
  706. text = str(error)
  707. if snapshot:
  708. values = [snapshot.get("default"), *snapshot.get("connections", {}).values(), *snapshot.get("botConnections", {}).values()]
  709. for value in values:
  710. if not isinstance(value, str) or value == "environment":
  711. continue
  712. parsed = urlsplit(value)
  713. text = text.replace(value, "[代理地址]")
  714. if parsed.password:
  715. text = text.replace(parsed.password, "[已隐藏]").replace(unquote(parsed.password), "[已隐藏]")
  716. return text
  717. def main():
  718. os.environ.setdefault("HERMES_SKIP_UPDATE_CHECK", "1")
  719. os.environ["HERMES_PYTHON_SRC_ROOT"] = str(ROOT / "hermes")
  720. _bootstrap()
  721. import model_network
  722. model_network.install()
  723. import atexit,signal
  724. def cleanup():
  725. from hermes_pairing import manager
  726. manager.close()
  727. atexit.register(cleanup)
  728. signal.signal(signal.SIGTERM,lambda *_:sys.exit(0))
  729. emit({"type": "ready"})
  730. for line in sys.stdin:
  731. line = line.strip()
  732. if not line:
  733. continue
  734. try:
  735. message = json.loads(line)
  736. except ValueError:
  737. continue
  738. threading.Thread(target=handle, args=(message,), daemon=True).start()
  739. if __name__ == "__main__":
  740. main()