bridge.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. from __future__ import annotations
  2. import logging
  3. from dataclasses import dataclass, field, replace as dataclass_replace
  4. from typing import Any, Callable, Optional, Sequence
  5. from tools.connectors.gateway.config import connectors_available, guest_identity_pending
  6. from tools.connectors.gateway.errors import GatewayAuthError, GatewayUnavailable, ToolGatewayError
  7. from tools.connectors.gateway.files import FileInputError, model_parameters, save_outputs, stage_inputs
  8. from tools.connectors.gateway.merge import fill_remote_failure, splice_remote_results
  9. from tools.connectors.gateway.names import parse_connector_name, vendor_slug_candidates
  10. from tools.managed_gateway_identity import CallRef
  11. logger = logging.getLogger(__name__)
  12. SIGN_IN_EXPIRED = "sign_in_expired"
  13. UNREACHABLE = "unreachable"
  14. # No Nous identity yet: the first hosted manage_connections action creates one.
  15. NOT_SET_UP = "not_set_up"
  16. NOT_SET_UP_MESSAGE = ("Hosted connectors are not set up for this user yet. "
  17. "Call manage_connections to connect the app first; that sets them up.")
  18. __all__ = [
  19. "NOT_SET_UP",
  20. "NOT_SET_UP_MESSAGE",
  21. "SIGN_IN_EXPIRED",
  22. "UNREACHABLE",
  23. "ConnectorLeg",
  24. "connector_describe",
  25. "connector_search_hits",
  26. "run_remote",
  27. ]
  28. @dataclass(frozen=True)
  29. class ConnectorLeg:
  30. payload: dict[str, Any] = field(default_factory=dict)
  31. failure: Optional[str] = None
  32. _TOKEN_REJECTED_CODES = frozenset({"UNAUTHORIZED", "INVALID_TOKEN", "TOKEN_EXPIRED"})
  33. def _leg_failure(exc: Exception) -> Optional[str]:
  34. if isinstance(exc, GatewayAuthError) and exc.code == NOT_SET_UP:
  35. return NOT_SET_UP
  36. if isinstance(exc, GatewayAuthError):
  37. if exc.status == 401 or str(exc.code).upper() in _TOKEN_REJECTED_CODES:
  38. return SIGN_IN_EXPIRED
  39. return None
  40. return UNREACHABLE
  41. def _default_client_factory(call: Optional[CallRef] = None):
  42. """The real client for the model's read and execute legs. These never create an identity."""
  43. from tools.connectors.gateway.client import ConnectorClient
  44. if guest_identity_pending():
  45. raise GatewayAuthError(NOT_SET_UP_MESSAGE, code=NOT_SET_UP)
  46. return ConnectorClient(call=call)
  47. def connector_search_hits(
  48. queries: Sequence[dict[str, Any]],
  49. *,
  50. availability: Optional[Callable[[], bool]] = None,
  51. client_factory: Optional[Callable[[], Any]] = None,
  52. call: Optional[CallRef] = None,
  53. ) -> ConnectorLeg:
  54. try:
  55. available = (availability or connectors_available)()
  56. if not available or not queries:
  57. return ConnectorLeg()
  58. client = client_factory() if client_factory else _default_client_factory(call)
  59. return ConnectorLeg(payload=client.search(list(queries)) or {})
  60. except GatewayUnavailable:
  61. logger.debug("Connector search skipped: gateway dark")
  62. return ConnectorLeg()
  63. except Exception as exc:
  64. logger.warning("Connector search failed (D32): %s", exc)
  65. return ConnectorLeg(failure=_leg_failure(exc))
  66. def connector_describe(
  67. names: Sequence[str],
  68. *,
  69. availability: Optional[Callable[[], bool]] = None,
  70. client_factory: Optional[Callable[[], Any]] = None,
  71. call: Optional[CallRef] = None,
  72. ) -> ConnectorLeg:
  73. try:
  74. available = (availability or connectors_available)()
  75. if not available:
  76. return ConnectorLeg()
  77. # Resolve each name independently: candidates can overlap across composed names.
  78. wanted: dict[str, tuple[str, ...]] = {}
  79. request_slugs: list[str] = []
  80. for name in names:
  81. parsed = parse_connector_name(name)
  82. if parsed is None or parsed.raw in wanted:
  83. continue
  84. candidates = vendor_slug_candidates(parsed.connector, parsed.tool)
  85. wanted[parsed.raw] = candidates
  86. for slug in candidates:
  87. if slug not in request_slugs:
  88. request_slugs.append(slug)
  89. if not wanted:
  90. return ConnectorLeg()
  91. client = client_factory() if client_factory else _default_client_factory(call)
  92. response = client.schemas(request_slugs) or {}
  93. schemas = response.get("schemas") if isinstance(response.get("schemas"), dict) else {}
  94. tools: dict[str, Any] = {}
  95. for composed, candidates in wanted.items():
  96. schema = next(
  97. (schemas[slug] for slug in candidates
  98. if isinstance(schemas.get(slug), dict)),
  99. None,
  100. )
  101. if schema is None:
  102. continue
  103. tools[composed] = {
  104. "description": str(schema.get("description") or ""),
  105. "parameters": model_parameters(composed, schema.get("input_schema") or {}),
  106. }
  107. return ConnectorLeg(payload={"tools": tools})
  108. except GatewayUnavailable:
  109. logger.debug("Connector describe skipped: gateway dark")
  110. return ConnectorLeg()
  111. except Exception as exc:
  112. logger.warning("Connector describe failed (D32): %s", exc)
  113. return ConnectorLeg(failure=_leg_failure(exc))
  114. def run_remote(
  115. planned,
  116. dispatch_id: Optional[str],
  117. *,
  118. availability: Optional[Callable[[], bool]],
  119. client_factory: Optional[Callable[[], Any]],
  120. turn_id: Optional[str] = None,
  121. task_id: Optional[str] = None,
  122. ) -> list[dict[str, Any]]:
  123. try:
  124. available = (availability or connectors_available)()
  125. except Exception:
  126. available = False
  127. if not available:
  128. return fill_remote_failure(
  129. planned,
  130. "Unknown tool: connectors are not available in this session.",
  131. code="TOOL_NOT_FOUND",
  132. )
  133. # Try the conventional prefix first; only confirmed misses get a literal retry.
  134. wire_planned = [
  135. dataclass_replace(
  136. plan,
  137. tool=vendor_slug_candidates(plan.connector, plan.tool)[0],
  138. )
  139. for plan in planned
  140. ]
  141. try:
  142. client = client_factory() if client_factory else _default_client_factory(
  143. CallRef(turn_id=turn_id, tool_call_id=dispatch_id))
  144. entries = splice_remote_results(planned, _execute_with_files(client, wire_planned, task_id))
  145. except ToolGatewayError as exc:
  146. logger.warning(
  147. "Connector execute for dispatch %s failed (%s): %s",
  148. dispatch_id,
  149. exc.code,
  150. exc,
  151. )
  152. return fill_remote_failure(
  153. planned, f"The connector gateway request failed: {exc}"
  154. )
  155. except Exception as exc:
  156. logger.warning(
  157. "Connector execute for dispatch %s failed unexpectedly: %s",
  158. dispatch_id,
  159. exc,
  160. )
  161. return fill_remote_failure(
  162. planned, "The connector gateway request failed unexpectedly."
  163. )
  164. fallback_slots: list[int] = []
  165. fallback_planned = []
  166. for slot, (plan, entry) in enumerate(zip(planned, entries)):
  167. primary, literal = vendor_slug_candidates(plan.connector, plan.tool)
  168. error = entry.get("error") if isinstance(entry, dict) else None
  169. if (
  170. primary != literal
  171. and isinstance(error, dict)
  172. and error.get("code") == "TOOL_NOT_FOUND"
  173. ):
  174. fallback_slots.append(slot)
  175. fallback_planned.append(dataclass_replace(plan, tool=literal))
  176. if not fallback_planned:
  177. return entries
  178. # Retry confirmed misses once without disturbing successful sibling slots.
  179. try:
  180. fallback_entries = splice_remote_results(
  181. fallback_planned, _execute_with_files(client, fallback_planned, task_id))
  182. except ToolGatewayError as exc:
  183. logger.warning(
  184. "Connector execute fallback for dispatch %s failed (%s): %s",
  185. dispatch_id,
  186. exc.code,
  187. exc,
  188. )
  189. fallback_entries = fill_remote_failure(
  190. fallback_planned, f"The connector gateway request failed: {exc}"
  191. )
  192. except Exception as exc:
  193. logger.warning(
  194. "Connector execute fallback for dispatch %s failed unexpectedly: %s",
  195. dispatch_id,
  196. exc,
  197. )
  198. fallback_entries = fill_remote_failure(
  199. fallback_planned, "The connector gateway request failed unexpectedly."
  200. )
  201. for slot, fallback_entry in zip(fallback_slots, fallback_entries):
  202. entries[slot] = fallback_entry
  203. return entries
  204. def _execute_with_files(client: Any, planned: Sequence[Any], task_id: Optional[str]) -> list[dict[str, Any]]:
  205. """Execute ``planned`` with every file input staged first and every returned file saved.
  206. A call whose file input is refused or fails to stage gets that error as its result and is never
  207. executed; its siblings still run."""
  208. from tools.connectors.gateway.client import return_to_args
  209. results: list[Optional[dict[str, Any]]] = [None] * len(planned)
  210. runnable: list[tuple[int, Any]] = []
  211. for slot, plan in enumerate(planned):
  212. try:
  213. runnable.append((slot, stage_inputs(client, plan, task_id=task_id)))
  214. except FileInputError as exc:
  215. logger.warning("Connector %s file input refused (%s): %s", plan.name, exc.code, exc)
  216. results[slot] = exc.result()
  217. if runnable:
  218. # A CONNECTION_REQUIRED link minted by this call is the user's next click, so the call names
  219. # the surface that browser should come back to.
  220. remote = client.execute([plan for _, plan in runnable], **return_to_args())
  221. for (slot, _), result in zip(runnable, remote or ()):
  222. results[slot] = save_outputs(client, result)
  223. return results