run_background.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """Finite background tasks launched by the gateway /bg command."""
  2. from contextlib import suppress
  3. import logging
  4. import os
  5. from typing import Optional
  6. from agent.i18n import t
  7. from gateway.media_repair import repair_explicit_computer_use_media_paths
  8. from gateway.platforms.base import BasePlatformAdapter
  9. from gateway.session import SessionSource
  10. logger = logging.getLogger("gateway.run")
  11. def _bg_prompt_preview(prompt: str, limit: int = 60) -> str:
  12. """Short single-line quote of a /bg prompt for its failure notice (the task id means nothing to the user)."""
  13. text = " ".join(str(prompt or "").split())
  14. return text if len(text) <= limit else text[: limit - 1].rstrip() + "…"
  15. class GatewayBackgroundMixin:
  16. async def _run_background_task(
  17. self, prompt: str, source: SessionSource, task_id: str,
  18. event_message_id: Optional[str] = None, media_urls: Optional[list[str]] = None,
  19. media_types: Optional[list[str]] = None,
  20. ) -> None:
  21. """Profile-scoping wrapper around the background agent task (mirrors ``_run_agent``)."""
  22. with self._profile_scope_for_source(source):
  23. return await self._run_background_task_inner(
  24. prompt, source, task_id, event_message_id, media_urls, media_types,
  25. )
  26. async def _run_background_task_inner(
  27. self, prompt: str, source: SessionSource, task_id: str,
  28. event_message_id: Optional[str] = None, media_urls: Optional[list[str]] = None,
  29. media_types: Optional[list[str]] = None,
  30. ) -> None:
  31. """Execute a background agent task and deliver the result to the chat."""
  32. from gateway.run import (
  33. _checkpoint_agent_kwargs, _current_max_iterations, _load_gateway_config,
  34. _platform_config_key,
  35. )
  36. from run_agent import AIAgent
  37. media_urls = media_urls or []
  38. media_types = media_types or []
  39. adapter = self._delivery_adapter_for(source)
  40. if not adapter:
  41. logger.warning("No adapter for platform %s in background task %s", source.platform, task_id)
  42. return
  43. _thread_metadata = self._thread_metadata_for_source(source, event_message_id)
  44. try:
  45. user_config = _load_gateway_config()
  46. model, runtime_kwargs = self._resolve_session_agent_runtime(source=source, user_config=user_config)
  47. if not runtime_kwargs.get("api_key"):
  48. await adapter.send(source.chat_id, t("gateway.background.no_credentials"), metadata=_thread_metadata)
  49. return
  50. platform_key = _platform_config_key(source.platform)
  51. enabled_toolsets, disabled_toolsets = self._resolve_turn_toolsets(user_config, source, platform_key)
  52. pr = self._provider_routing
  53. max_iterations = _current_max_iterations()
  54. reasoning_config = self._resolve_session_reasoning_config(source=source, model=model)
  55. self._reasoning_config = reasoning_config
  56. self._service_tier = self._resolve_session_service_tier(source=source)
  57. turn_route = self._resolve_turn_agent_config(prompt, model, runtime_kwargs)
  58. # Enrich the prompt with image descriptions (same as the main flow).
  59. enriched_prompt = prompt
  60. image_paths = [
  61. path for i, path in enumerate(media_urls)
  62. if (media_types[i] if i < len(media_types) else "").startswith("image/")
  63. ]
  64. if image_paths:
  65. try:
  66. enriched_prompt = await self._enrich_message_with_vision(prompt, image_paths)
  67. except Exception as e:
  68. logger.warning("Background task vision enrichment failed: %s", e, exc_info=True)
  69. def run_sync():
  70. from gateway.session_context import declare_stateless_channel
  71. # This finite /bg session has no route for a completion after its final reply.
  72. declare_stateless_channel()
  73. agent = AIAgent(
  74. model=turn_route["model"],
  75. **turn_route["runtime"],
  76. **_checkpoint_agent_kwargs(user_config),
  77. max_iterations=max_iterations,
  78. quiet_mode=True,
  79. verbose_logging=False,
  80. enabled_toolsets=enabled_toolsets,
  81. disabled_toolsets=disabled_toolsets,
  82. reasoning_config=reasoning_config,
  83. service_tier=self._service_tier,
  84. request_overrides=turn_route.get("request_overrides"),
  85. providers_allowed=pr.get("only"),
  86. providers_ignored=pr.get("ignore"),
  87. providers_order=pr.get("order"),
  88. provider_sort=pr.get("sort"),
  89. provider_require_parameters=pr.get("require_parameters", False),
  90. provider_data_collection=pr.get("data_collection"),
  91. session_id=task_id,
  92. platform=platform_key,
  93. **{k: getattr(source, k) for k in (
  94. "user_id", "user_id_alt", "user_name", "chat_id", "chat_name", "chat_type", "thread_id",
  95. )},
  96. session_db=getattr(self._session_db, "_db", self._session_db),
  97. # Reload from disk — do not reuse the startup snapshot.
  98. # See #60955.
  99. fallback_model=self._refresh_fallback_model(),
  100. )
  101. try:
  102. return agent.run_conversation(user_message=enriched_prompt, task_id=task_id)
  103. finally:
  104. self._cleanup_agent_resources(agent)
  105. result = await self._run_in_executor_with_context(run_sync)
  106. response = result.get("final_response", "") if result else ""
  107. if not response and result and result.get("error"):
  108. response = t("gateway.shared.error_prefix", error=result["error"])
  109. # Fresh conversation, so history_offset=0: every message in the run belongs to this turn.
  110. if response:
  111. response = repair_explicit_computer_use_media_paths(response, result.get("messages", []))
  112. preview = prompt[:60] + ("..." if len(prompt) > 60 else "")
  113. header = t("gateway.background.complete_header", preview=preview)
  114. images, media_files, text_content = [], [], ""
  115. if response:
  116. media_files, response = adapter.extract_media(response)
  117. media_files = BasePlatformAdapter.filter_media_delivery_paths(media_files)
  118. images, text_content = adapter.extract_images(response)
  119. if text_content:
  120. await adapter.send(chat_id=source.chat_id, content=header + text_content, metadata=_thread_metadata)
  121. elif not images and not media_files:
  122. await adapter.send(
  123. chat_id=source.chat_id, content=header + t("gateway.background.no_response"), metadata=_thread_metadata,
  124. )
  125. for image_url, alt_text in (images or []):
  126. with suppress(Exception):
  127. await adapter.send_image(
  128. chat_id=source.chat_id, image_url=image_url, caption=alt_text, metadata=_thread_metadata,
  129. )
  130. # Route each media file by type (voice bubble / video / image / document), as the
  131. # streaming + kanban paths do.
  132. from gateway.platforms.base import should_send_media_as_audio as _should_send_media_as_audio
  133. from gateway.run_notifications import _IMAGE_EXTS, _VIDEO_EXTS
  134. for media_path, _is_voice in (media_files or []):
  135. _ext = os.path.splitext(media_path)[1].lower()
  136. with suppress(Exception):
  137. if _should_send_media_as_audio(source.platform, _ext, _is_voice):
  138. await adapter.send_voice(
  139. chat_id=source.chat_id, audio_path=media_path, metadata=_thread_metadata,
  140. is_voice=_is_voice,
  141. )
  142. else:
  143. sender, key = (
  144. (adapter.send_video, "video_path") if _ext in _VIDEO_EXTS
  145. else (adapter.send_image_file, "image_path") if _ext in _IMAGE_EXTS
  146. else (adapter.send_document, "file_path")
  147. )
  148. await sender(chat_id=source.chat_id, metadata=_thread_metadata, **{key: media_path})
  149. except Exception:
  150. logger.exception("Background task %s failed", task_id)
  151. # Automatic failure diagnostic (the task produced no requested result to deliver).
  152. from gateway.bot_peer_notices import automatic_notices_allowed
  153. with suppress(Exception):
  154. if automatic_notices_allowed(source):
  155. await adapter.emit_warning(
  156. source.chat_id,
  157. t("gateway.background.failed", preview=_bg_prompt_preview(prompt)),
  158. metadata=_thread_metadata, logical_platform=source.platform,
  159. )