session_complete_path.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """``complete.path`` on the shared gateway: the sidecar's path / ``@`` listing, rooted in one session's folder.
  2. The Ink TUI asks its canonical connection for composer completions. The listing is the sidecar's own
  3. (``tui_gateway/methods_complete.py``); its root is the session's frozen policy cwd, never a cwd the
  4. client sends or the daemon's own folder.
  5. """
  6. import asyncio
  7. import json
  8. from pathlib import Path
  9. from gateway.session_busy_controls import authorize
  10. from hermes_state_runtime import RuntimeStoreError
  11. async def complete_path(connection, ref, params):
  12. if (set(params) - {'session_id', 'profile', 'word', 'cwd'}
  13. or any(not isinstance(value, str) for value in params.values())):
  14. raise RuntimeStoreError('invalid_params')
  15. word = params.get('word', '')
  16. if not word or not ref.session_id:
  17. return {'items': []}
  18. authority = connection.authority
  19. if ref.session_id not in authority.sessions:
  20. raise RuntimeStoreError('not_found')
  21. authorize(connection, ref, params, 'session:read')
  22. from gateway.session_policy import policy_for_source, terminal_uses_host_cwd
  23. policy = policy_for_source(authority.runner, authority.sessions[ref.session_id].source)
  24. if policy is None or not terminal_uses_host_cwd(json.loads(policy.terminal_json)):
  25. # A remote or sandboxed backend's tree is not this host's; completion is advisory, so nothing.
  26. return {'items': []}
  27. # The listing helpers are bound onto the sidecar module's globals (method_ctx.bind_module).
  28. from tui_gateway import server
  29. def listing():
  30. from gateway.run import _profile_runtime_scope
  31. with _profile_runtime_scope(Path(authority.profile_id)):
  32. return server._path_completion_items(word, policy.cwd, None, True)
  33. return {'items': await asyncio.to_thread(listing)}