responses.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Copilot / GitHub Models wire quirks on the Responses route, reasoning payloads and reasoning carriers.
  2. Fills the generic ``ProviderProfile`` endpoint seams (providers/base.py, lane L3 block). Everything here
  3. is called per request or per replayed message, so it is pure and answers from static data / caches.
  4. """
  5. from __future__ import annotations
  6. from typing import Any
  7. from .helpers import base_url_host_matches
  8. _COPILOT_HOST = "githubcopilot.com" # api.githubcopilot.com plus the api.<plan>.githubcopilot.com variants
  9. # Constructor kwargs for the profile's L3 fields. ``github_responses`` is persisted on encrypted reasoning
  10. # items in session history: renaming it orphans every stored blob (one cold cache + dropped reasoning on resume).
  11. RESPONSES_PROFILE_FIELDS: dict[str, Any] = {
  12. # The backend binds replayed item ids to a connection; a stale id 401s even when short (#32716).
  13. "responses_replay_item_ids": False,
  14. "responses_prompt_cache_key": False,
  15. "responses_issuer_kind": "github_responses",
  16. "responses_trusts_completed_status": False,
  17. "responses_native_compaction": False,
  18. # Reasoning text only comes back when ``summary: auto`` is sent (#46527); ``include`` is never sent.
  19. "responses_profile_reasoning": True,
  20. "message_carrier_opaque": "reasoning_opaque",
  21. "message_carrier_text": "reasoning_text",
  22. # Copilot's Gemini models take tool-call ``extra_content`` signatures back like the native Gemini API.
  23. "replays_tool_call_signatures": True,
  24. }
  25. def _is_copilot_host(base_url: str) -> bool:
  26. return base_url_host_matches(str(base_url or ""), _COPILOT_HOST)
  27. class CopilotResponsesMixin:
  28. """Endpoint ownership, catalog-clamped reasoning payloads and the ``/chat/completions`` carriers."""
  29. def reasoning_payload(self, model: str | None, reasoning_config: dict | None, *, base_url: str) -> dict | None:
  30. from .catalog import clamp_github_reasoning_effort, github_model_reasoning_efforts
  31. supported = github_model_reasoning_efforts(model)
  32. if not supported:
  33. return None
  34. cfg = reasoning_config if isinstance(reasoning_config, dict) else {}
  35. if cfg.get("enabled") is False:
  36. return None
  37. return {"effort": clamp_github_reasoning_effort(cfg.get("effort"), supported)}
  38. def replay_message_carriers(self, values: dict[str, Any], *, model: str | None, base_url: str) -> dict[str, Any]:
  39. # The editor clients replay reasoning_text only next to the opaque blob it belongs to.
  40. if not _is_copilot_host(base_url) or not values.get("reasoning_opaque"):
  41. return {}
  42. out = {"reasoning_opaque": values["reasoning_opaque"]}
  43. if values.get("reasoning_text"):
  44. out["reasoning_text"] = values["reasoning_text"]
  45. return out
  46. def replays_function_thought_signature(self, *, model: str | None, base_url: str) -> bool:
  47. # Copilot's Gemini 3 variant signs tool calls inside ``function`` and expects them back.
  48. return _is_copilot_host(base_url)