search.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from __future__ import annotations
  2. import functools
  3. import logging
  4. from typing import Any, Dict, Iterable, List, Optional, Tuple
  5. from tools.connectors.gateway.bridge import NOT_SET_UP, NOT_SET_UP_MESSAGE, SIGN_IN_EXPIRED, UNREACHABLE
  6. from tools.connectors.gateway.names import format_connector_name, is_connector_name, vendor_slug_candidates
  7. from tools.tool_search_catalog import CatalogEntry, _fn, _tokenize
  8. logger = logging.getLogger(__name__)
  9. def connections_in_scope(tool_defs: Iterable[dict[str, Any]]) -> bool:
  10. return any(_fn(td).get("name") == "manage_connections" for td in tool_defs)
  11. def connectors_unavailable(failure: str, *, verb: str,
  12. names: Optional[list[str]] = None) -> dict[str, Any]:
  13. if failure == NOT_SET_UP:
  14. from tools.connectors.gateway.config import guest_setup_failure
  15. hint = (f"{NOT_SET_UP_MESSAGE} Hosted apps (Gmail, Calendar, Notion and more) are then searchable. "
  16. "If the request is better served by the tools above, use those instead.")
  17. if last_failure := guest_setup_failure():
  18. hint += f" The last attempt failed, so tell the user before retrying: {last_failure}"
  19. field: dict[str, Any] = {"status": NOT_SET_UP, "hint": hint}
  20. if names:
  21. field["names"] = names
  22. return field
  23. hint = (f"Hosted connector tools could not be {verb} right now. "
  24. "Do not conclude the app is missing.")
  25. if failure == SIGN_IN_EXPIRED:
  26. hint += " The user must sign in to Nous again."
  27. field = {"status": "unavailable", "reason": failure, "hint": hint}
  28. if names:
  29. field["names"] = names
  30. return field
  31. def _connector_entry(name: str, connector: str, slug: str, schema: dict[str, Any]) -> CatalogEntry:
  32. description = str(schema.get("description") or "")
  33. from tools.connectors.gateway.files import model_parameters
  34. parameters = model_parameters(name, schema.get("input_schema"))
  35. tool_def = {"type": "function", "function": {
  36. "name": name, "description": description, "parameters": parameters}}
  37. text = f"{connector} {slug.replace('_', ' ')} {description}"
  38. return CatalogEntry(name=name, description=description, schema=tool_def,
  39. source="connectors", source_name=connector, _tokens=_tokenize(text))
  40. def connector_entries_by_group(
  41. queries: list[str],
  42. connector_search: Optional[Any] = None,
  43. call: Optional[Any] = None,
  44. ) -> tuple[list[list[CatalogEntry]], Optional[str]]:
  45. per_query: list[list[CatalogEntry]] = [[] for _ in queries]
  46. try:
  47. if connector_search is None:
  48. from tools.connectors.gateway.bridge import connector_search_hits
  49. connector_search = functools.partial(connector_search_hits, call=call)
  50. leg = connector_search([{"use_case": q} for q in queries])
  51. if leg.failure:
  52. return per_query, leg.failure
  53. hits = leg.payload or {}
  54. schemas = hits.get("schemas")
  55. groups = hits.get("results")
  56. if not isinstance(schemas, dict) or not isinstance(groups, list):
  57. return per_query, (UNREACHABLE if hits else None)
  58. for position, group in enumerate(groups[: len(queries)]):
  59. if not isinstance(group, dict):
  60. continue
  61. echoed = group.get("use_case")
  62. if isinstance(echoed, str) and echoed and echoed != queries[position]:
  63. continue
  64. slugs = group.get("tools") if isinstance(group.get("tools"), list) else []
  65. picked: dict[str, tuple[str, CatalogEntry]] = {}
  66. for slug in slugs:
  67. schema = schemas.get(slug)
  68. if not isinstance(schema, dict) or not schema.get("connector"):
  69. continue
  70. # Gateway policy matches normalized lowercase connector slugs; tool slugs stay verbatim.
  71. slug = str(slug)
  72. connector = str(schema["connector"]).lower()
  73. name = format_connector_name(connector, slug)
  74. prior = picked.get(name)
  75. if prior is not None and prior[0] != slug:
  76. # Keep the slug this composed name resolves to; its twin would execute differently.
  77. reaches = vendor_slug_candidates(connector, name.split("__", 2)[2])[0]
  78. logger.warning("connector %s: vendor slugs %s and %s both compose to %s, which reaches %s",
  79. connector, prior[0], slug, name, reaches)
  80. if slug != reaches:
  81. continue
  82. elif prior is not None:
  83. continue
  84. picked[name] = (slug, _connector_entry(name, str(schema["connector"]), slug, schema))
  85. per_query[position] = [entry for _, entry in picked.values()]
  86. except Exception:
  87. logger.warning("connector search merge failed (D32)", exc_info=True)
  88. return [[] for _ in queries], UNREACHABLE
  89. return per_query, None
  90. def remote_schemas_for(
  91. names: list[str],
  92. current_tool_defs: list[dict[str, Any]],
  93. connector_describe: Optional[Any] = None,
  94. call: Optional[Any] = None,
  95. ) -> tuple[dict[str, dict[str, Any]], Optional[str]]:
  96. connector_names = [n for n in names if is_connector_name(n)]
  97. if not connector_names or not connections_in_scope(current_tool_defs):
  98. return {}, None
  99. try:
  100. if connector_describe is None:
  101. from tools.connectors.gateway.bridge import connector_describe as describe
  102. connector_describe = functools.partial(describe, call=call)
  103. leg = connector_describe(connector_names)
  104. if leg.failure:
  105. return {}, leg.failure
  106. tools = leg.payload.get("tools")
  107. if isinstance(tools, dict):
  108. return tools, None
  109. return {}, (UNREACHABLE if leg.payload else None)
  110. except Exception:
  111. logger.warning("connector describe merge failed (D32)", exc_info=True)
  112. return {}, UNREACHABLE