native_openviking.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. """Yaovi identity policy around the unmodified, pinned Hermes OpenViking provider.
  2. The upstream provider owns capture, recall, sessions, commit/recovery and tools.
  3. This adapter pins a workstation connection and enforces Bot visibility at tool admission.
  4. """
  5. import json
  6. import re
  7. from contextvars import ContextVar
  8. from urllib.parse import unquote
  9. from plugins.memory.openviking import (OpenVikingMemoryProvider, _VikingClient,
  10. _load_hermes_openviking_config, _profile_openviking_env, _resolve_connection_settings,
  11. _resolve_user_space, _probe_openviking_identity)
  12. def connection(home):
  13. env = _profile_openviking_env(str(home)) or {}
  14. config = _load_hermes_openviking_config(str(home), env=env)
  15. settings = _resolve_connection_settings(config, env=env, hermes_home=str(home))
  16. settings['agent'] = '' # The server-owned Bot identity always wins over .env / ovcli.
  17. value = {'settings': settings, 'providerConfig': config}
  18. client = connection_client(value)
  19. kind, health = _probe_openviking_identity(client)
  20. version = re.match(r'^v?(\d+)\.(\d+)\.(\d+)', str(health.get('version', ''))) if isinstance(health, dict) else None
  21. if kind != 'modern' or not version or tuple(map(int, version.groups())) < (0, 4, 17):
  22. raise RuntimeError('peer 模式需要支持 peer 命名空间的 OpenViking 0.4.17 或更新服务')
  23. status = client.get('/api/v1/system/status', timeout=5).get('result', {})
  24. user = str(status.get('user') or '').strip()
  25. if status.get('account'):
  26. settings['account'] = str(status['account'])
  27. if not user:
  28. raise RuntimeError('Hermes OpenViking 无法确认当前 user,请检查原生配置')
  29. settings['user'] = user
  30. return value
  31. def connection_client(value, peer=''):
  32. s = value['settings']
  33. return _VikingClient(s['endpoint'], s['api_key'], account=s['account'], user=s['user'], agent=peer, extra_headers=s.get('extra_headers'))
  34. def test_connection(value):
  35. try:
  36. client = connection_client(value)
  37. user = _resolve_user_space(client, timeout=5, raise_on_timeout=True)
  38. if not user:
  39. raise ValueError('服务未确认当前 user 身份,请检查凭据或升级 OpenViking')
  40. # An explicit peers path validates the namespace, without creating a peer.
  41. try:
  42. client.get('/api/v1/fs/ls', params={'uri': f'viking://user/{user}/peers'}, timeout=5)
  43. except Exception as error:
  44. if getattr(error, 'status_code', None) != 404:
  45. raise
  46. return {'ok': True, 'steps': [{'name': 'native', 'ok': True, 'detail': f'Hermes 原生插件已连接,当前 user:{user}'}]}
  47. except Exception:
  48. return {'ok': False, 'steps': [{'name': 'native', 'ok': False, 'detail': 'Hermes 原生 OpenViking 连接或 peer 命名空间不可用;请检查原生配置、凭据与服务版本'}]}
  49. class _BotClient(_VikingClient):
  50. def post(self, path, payload=None, **kwargs):
  51. # Native viking_remember submits a user-only extraction session. Tag it too,
  52. # so an explicit remember cannot accidentally become common user memory.
  53. if self._agent and re.fullmatch(r'/api/v1/sessions/[^/]+/messages(?:/batch)?', path):
  54. payload = dict(payload or {})
  55. if isinstance(payload.get('messages'), list):
  56. payload['messages'] = [{**m, 'peer_id': self._agent} if m.get('role') in ('user', 'assistant') else m for m in payload['messages']]
  57. elif payload.get('role') in ('user', 'assistant'):
  58. payload['peer_id'] = self._agent
  59. return super().post(path, payload, **kwargs)
  60. class YaoviOpenVikingProvider(OpenVikingMemoryProvider):
  61. def __init__(self, identity, *, automatic=True, recall=True):
  62. super().__init__()
  63. self.identity = identity
  64. self.automatic = automatic
  65. self.read_only = False
  66. self.recall_enabled = recall
  67. self.policy = identity.get('policy')
  68. self._explicit_access = ContextVar('yaovi_memory_explicit_access', default=None)
  69. def _profile_config_and_env(self):
  70. # Frozen per run. No fallback to an unrelated Bot profile or global actor.
  71. return {k: v for k, v in self.identity['providerConfig'].items() if k != 'deployment'}, {}
  72. def _resolve_bound_connection_settings(self, config=None, env=None):
  73. return {**self.identity['settings'], 'agent': self.identity['peerId']}
  74. def initialize(self, session_id, **kwargs):
  75. self.read_only = kwargs.get('agent_context') != 'primary'
  76. if not self.automatic or self.read_only or self.policy is not None and not self.policy.get('write'):
  77. kwargs['agent_context'] = 'subagent'
  78. # Keep pending commits in a directory dedicated to this identity. A promotion must
  79. # not replay a former peer's pending session through the actor-less primary client.
  80. from pathlib import Path
  81. kwargs['hermes_home'] = str(Path(kwargs['hermes_home']) / 'openviking-identities' / ('peer-' + self.identity['peerId'] if self.identity['peerId'] else 'primary'))
  82. super().initialize(session_id, **kwargs)
  83. self._user_id = self.identity['peerId']
  84. if self._client is None:
  85. raise RuntimeError('Hermes OpenViking 原生连接不可用;本轮记忆未接入')
  86. def _handle_runtime_openviking_unreachable(self, **kwargs):
  87. # Yaovi connects to an existing server; provider initialization must not start
  88. # or install a separate per-Bot Quick Local service.
  89. self._client = None
  90. def _build_client(self, endpoint=None):
  91. endpoint, key, account, user, peer = self._settings_tuple(endpoint)
  92. return _BotClient(endpoint, key, account=account, user=user, agent=peer, extra_headers=self._extra_headers)
  93. def _new_client(self):
  94. return self._build_client()
  95. def _sender_peer(self, author_id):
  96. return self.identity['peerId']
  97. def _recall_config(self):
  98. cfg = super()._recall_config()
  99. all_peers = self.identity['primary'] and (self.policy is None or self.policy.get('allPeers'))
  100. return {**cfg, 'scope': 'shared' if all_peers else 'peer', 'resources': False, 'compress': 'off'}
  101. def prefetch(self, query, *, session_id=''):
  102. if not self.recall_enabled:
  103. return ''
  104. if self.policy is not None and not self._automatic_read_allowed(self._policy_roots()):
  105. return ''
  106. result = super().prefetch(query, session_id=session_id)
  107. # Do not inject a result whose authority was revoked while remote recall
  108. # was in flight. This read-only check never opens an approval request.
  109. if self.policy is not None and not self._automatic_read_allowed(self._policy_roots()):
  110. return ''
  111. return result
  112. def _automatic_read_allowed(self, roots):
  113. if not roots:
  114. return False
  115. from native_authorization import check_only
  116. scopes = list(dict.fromkeys(self._permission_scope(root) for root in roots))
  117. return check_only('viking_search', {'scope': roots, 'permissionScope': scopes[0] if len(scopes) == 1 else scopes})
  118. def _policy_roots(self, *, resources=False):
  119. root = f'viking://user/{self._user_space(require_confirmed=True)}'
  120. policy = self.policy or {}
  121. roots = []
  122. suffixes = ('/memories', '/resources') if resources else ('/memories',)
  123. if policy.get('shared') or self.identity['primary'] and policy.get('self'):
  124. roots.extend(root + suffix for suffix in suffixes)
  125. if not self.identity['primary'] and policy.get('self'):
  126. roots.extend(root + '/peers/' + self.identity['peerId'] + suffix for suffix in suffixes)
  127. if self.identity['primary'] and policy.get('allPeers'):
  128. roots.append(root + '/peers')
  129. return roots
  130. def _post_prefetch_search(self, client, query, session_id, **kwargs):
  131. if self.policy is not None:
  132. roots = self._policy_roots()
  133. if not self._automatic_read_allowed(roots):
  134. return {'result': {'memories': [], 'resources': []}}
  135. kwargs['target_uri'] = roots
  136. return super()._post_prefetch_search(client, query, session_id, **kwargs)
  137. def _session_start_memory_context(self, session_id):
  138. # Upstream reads the shared user profile directly, before query filtering.
  139. # Suppress that read unless this Bot can read that root.
  140. if self.policy is not None:
  141. root = f'viking://user/{self._user_space(require_confirmed=True)}/memories'
  142. if not self._allowed(root) or not self._automatic_read_allowed([root]):
  143. return ''
  144. return super()._session_start_memory_context(session_id)
  145. def _normalized_uri(self, uri):
  146. if not isinstance(uri, str):
  147. return None
  148. uri = unquote(unquote(uri)).rstrip('/')
  149. if any(p in ('.', '..') for p in uri.split('/')) or any(c in uri for c in ('\\', '?', '#', '%')):
  150. return None
  151. user = self._user_space(require_confirmed=True)
  152. uri = uri.replace('viking://~', f'viking://user/{user}', 1) if uri.startswith('viking://~/') or uri == 'viking://~' else uri
  153. root = f'viking://user/{user}'
  154. if not (uri == root or uri.startswith(root + '/')):
  155. return None
  156. return uri
  157. def _allowed(self, uri, *, write=False):
  158. uri = self._normalized_uri(uri)
  159. if uri is None:
  160. return False
  161. root = f'viking://user/{self._user_space(require_confirmed=True)}'
  162. rest = uri[len(root):]
  163. if rest.startswith('/peers/') or rest == '/peers':
  164. if self.identity['primary']:
  165. identity_allowed = not write
  166. else:
  167. peer = '/peers/' + self.identity['peerId']
  168. identity_allowed = rest == peer or rest.startswith(peer + '/')
  169. else:
  170. identity_allowed = rest == '' and self.identity['primary'] and not write or (not write or self.identity['primary']) and any(rest == p or rest.startswith(p + '/') for p in ('/memories', '/resources'))
  171. if not identity_allowed:
  172. return False
  173. explicit = self._explicit_access.get()
  174. if explicit is not None:
  175. return any(uri == target or recursive and uri.startswith(target + '/') for target, recursive in explicit)
  176. if self.policy is None:
  177. return True
  178. if write and not self.policy.get('write'):
  179. return False
  180. return any(uri == target or uri.startswith(target + '/') for target in self._policy_roots(resources=True))
  181. def _permission_scope(self, uri):
  182. root = f'viking://user/{self._user_space(require_confirmed=True)}'
  183. private = root if self.identity['primary'] else root + '/peers/' + self.identity['peerId']
  184. if any(uri == private + p or uri.startswith(private + p + '/') for p in ('/memories', '/resources')):
  185. return 'self'
  186. if any(uri == root + p or uri.startswith(root + p + '/') for p in ('/memories', '/resources')):
  187. return 'shared'
  188. return 'memory-uri:' + uri
  189. def handle_tool_call(self, tool_name, args, **kwargs):
  190. args = dict(args)
  191. args.pop('permissionScope', None) # Computed below; model input cannot declare its own scope.
  192. if self.read_only and tool_name not in ('viking_search', 'viking_read', 'viking_browse'):
  193. return json.dumps({'error': '子任务和审阅上下文只允许读取记忆'})
  194. access_token = None
  195. try:
  196. if tool_name == 'viking_add_resource':
  197. # Native ingestion can read local files/URLs and write account resources.
  198. # Admit only user-scoped targets; local file admission remains Yaovi-owned.
  199. return json.dumps({'error': '请通过服务器的授权文件入口导入资料'})
  200. key = {'viking_search': 'scope', 'viking_browse': 'path', 'viking_forget': 'uri'}.get(tool_name)
  201. if tool_name == 'viking_search' and not args.get('scope'):
  202. root = f'viking://user/{self._user_space(require_confirmed=True)}'
  203. args['scope'] = self._policy_roots(resources=True) if self.policy is not None else root if self.identity['primary'] else [root + '/memories', root + '/peers/' + self.identity['peerId'] + '/memories']
  204. if not args['scope']:
  205. return json.dumps({'error': '未授权默认记忆范围,请指定需要访问的记忆 URI'})
  206. if tool_name == 'viking_browse' and args.get('path', 'viking://') == 'viking://':
  207. if self.policy is not None:
  208. roots = self._policy_roots(resources=True)
  209. if not roots:
  210. return json.dumps({'error': '未授权默认记忆范围,请指定需要访问的记忆 URI'})
  211. args['path'] = roots[0]
  212. else:
  213. args['path'] = 'viking://~' if self.identity['primary'] else 'viking://~/peers/' + self.identity['peerId']
  214. values = args.get('uris') or args.get('uri') if tool_name == 'viking_read' else args.get(key) if key else None
  215. targets = []
  216. if values is not None:
  217. for value in values if isinstance(values, list) else [values]:
  218. normalized = self._normalized_uri(value)
  219. if normalized is None:
  220. return json.dumps({'error': '此 Bot 无权访问该记忆空间'})
  221. if tool_name == 'viking_read' and args.get('level', 'overview') in ('abstract', 'overview'):
  222. normalized = self._normalize_summary_uri(normalized)
  223. targets.append(normalized)
  224. if tool_name == 'viking_read':
  225. args.pop('uris', None)
  226. args['uri'] = targets if isinstance(values, list) else targets[0]
  227. elif key:
  228. args[key] = targets if isinstance(values, list) else targets[0]
  229. values = targets
  230. from native_authorization import authorize, enabled
  231. if enabled():
  232. scopes = list(dict.fromkeys(self._permission_scope(target) for target in targets)) or ['self']
  233. refusal = authorize(tool_name, {**args, 'permissionScope': scopes[0] if len(scopes) == 1 else scopes}, **kwargs)
  234. if refusal is not None:
  235. return refusal
  236. access_token = self._explicit_access.set([(target, tool_name in ('viking_search', 'viking_browse')) for target in targets])
  237. if values is not None and not all(self._allowed(v, write=tool_name == 'viking_forget') for v in (values if isinstance(values, list) else [values])):
  238. return json.dumps({'error': '此 Bot 无权访问该记忆空间'})
  239. return super().handle_tool_call(tool_name, args, **kwargs)
  240. except Exception:
  241. return json.dumps({'error': 'OpenViking 记忆操作失败,请检查连接与访问范围'})
  242. finally:
  243. if access_token is not None:
  244. self._explicit_access.reset(access_token)
  245. def _build_prefetch_entries(self, client, items, **kwargs):
  246. items = [item for item in items if self._allowed(item.get('uri', ''))]
  247. if self.policy is not None and not self._automatic_read_allowed([self._normalized_uri(item['uri']) for item in items]):
  248. return []
  249. return super()._build_prefetch_entries(client, items, **kwargs)
  250. def _tool_search(self, args):
  251. value = json.loads(super()._tool_search(args))
  252. if 'results' in value:
  253. value['results'] = [item for item in value['results'] if self._allowed(item.get('uri', ''))]
  254. for item in value['results']:
  255. if 'related' in item:
  256. item['related'] = [uri for uri in item['related'] if self._allowed(uri)]
  257. value['total'] = len(value['results'])
  258. return json.dumps(value, ensure_ascii=False)
  259. def _tool_browse(self, args):
  260. value = json.loads(super()._tool_browse(args))
  261. if isinstance(value, dict) and 'entries' in value:
  262. value['entries'] = [item for item in value['entries'] if self._allowed(item.get('uri', ''))]
  263. return json.dumps(value, ensure_ascii=False)
  264. def get_tool_schemas(self):
  265. return [s for s in super().get_tool_schemas() if s['name'] != 'viking_add_resource']