oneshot.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Shared one-off LLM requests for non-conversational helpers.
  2. A "one-shot" is a single stateless model call outside any conversation (commit
  3. messages, rename suggestions, summaries): it never touches session history or
  4. prompt caching. Call with explicit ``instructions``/``user_input`` or a registered
  5. ``template`` + ``variables`` so prompt engineering stays consistent across
  6. CLI/TUI/desktop. Model selection rides :func:`agent.auxiliary_client.call_llm`:
  7. ``main_runtime`` inherits the live session's provider/model, else ``task``
  8. resolves a cheap backend.
  9. """
  10. import logging
  11. from typing import Any, Callable, Dict, Optional, Tuple
  12. from agent.auxiliary_client import call_llm, extract_content_or_reasoning
  13. logger = logging.getLogger(__name__)
  14. # Templates are plain callables (not str.format) so diff/code payloads with
  15. # literal "{" / "}" pass through untouched.
  16. PromptTemplate = Callable[[dict[str, Any]], tuple[str, str]]
  17. def _truncate(text: str, limit: int) -> str:
  18. text = text or ""
  19. return text if len(text) <= limit else text[:limit].rstrip() + "\n…(truncated)"
  20. _COMMIT_INSTRUCTIONS = (
  21. "You write git commit messages. Given a diff of staged changes, write ONE concise Conventional Commits "
  22. "message describing what the change does and why.\n"
  23. "Rules:\n"
  24. "- Subject line: type(scope): summary — imperative mood, lower-case, no trailing period, ≤ 72 "
  25. "characters. Types: feat, fix, refactor, perf, docs, test, build, chore, style, ci.\n"
  26. "- Omit the scope if it isn't obvious.\n"
  27. "- Add a short body (wrapped at ~72 cols) ONLY when the change needs explanation; skip it for "
  28. "small/obvious changes.\n"
  29. "- Describe the actual change, never restate the diff line-by-line.\n"
  30. "- Return ONLY the commit message text — no quotes, no markdown fences, no preamble."
  31. )
  32. def _commit_message_template(variables: dict[str, Any]) -> tuple[str, str]:
  33. diff = _truncate(str(variables.get("diff") or ""), 12000)
  34. recent = _truncate(str(variables.get("recent_commits") or ""), 1500)
  35. parts = []
  36. if recent.strip():
  37. parts.append(
  38. "Recent commit subjects from this repo (match their style/conventions):\n"
  39. f"{recent}"
  40. )
  41. parts.append("Diff to describe:\n" + (diff or "(no textual diff available)"))
  42. # "Regenerate" must yield something new even on greedy/server-pinned
  43. # temperature models; a nonce isn't enough, so hand back the previous
  44. # message and require a genuinely different one.
  45. avoid = _truncate(str(variables.get("avoid") or "").strip(), 1000)
  46. if avoid:
  47. parts.append(
  48. "You already proposed the message below and the user wants a different one. Write a NEW message with "
  49. "different wording (and, if reasonable, a different emphasis or scope framing) — do not repeat "
  50. f"it:\n{avoid}"
  51. )
  52. return _COMMIT_INSTRUCTIONS, "\n\n".join(parts)
  53. # Registry of named templates; add an entry to give a new surface a reusable prompt.
  54. PROMPT_TEMPLATES: dict[str, PromptTemplate] = {
  55. "commit_message": _commit_message_template,
  56. }
  57. def render_template(name: str, variables: Optional[dict[str, Any]] = None) -> tuple[str, str]:
  58. """Resolve a registered template into (instructions, user_input); KeyError if unknown."""
  59. template = PROMPT_TEMPLATES.get(name)
  60. if template is None:
  61. raise KeyError(f"unknown one-shot template: {name}")
  62. return template(variables or {})
  63. def run_oneshot(
  64. *,
  65. instructions: str = "",
  66. user_input: str = "",
  67. template: Optional[str] = None,
  68. variables: Optional[dict[str, Any]] = None,
  69. task: str = "title_generation",
  70. max_tokens: int = 1024,
  71. temperature: Optional[float] = 0.3,
  72. timeout: float = 60.0,
  73. main_runtime: Optional[dict[str, Any]] = None,
  74. ) -> str:
  75. """Run a single stateless LLM request and return its text (fence-stripped).
  76. Raises RuntimeError when no provider is configured (from :func:`call_llm`),
  77. KeyError for an unknown template, ValueError when the prompt is empty.
  78. """
  79. if template:
  80. instructions, user_input = render_template(template, variables)
  81. has_instructions = bool((instructions or "").strip())
  82. if not has_instructions and not (user_input or "").strip():
  83. raise ValueError("run_oneshot requires a template or instructions/user_input")
  84. messages = [{"role": "system", "content": instructions}] if has_instructions else []
  85. messages.append({"role": "user", "content": user_input or ""})
  86. response = call_llm(
  87. task=task,
  88. messages=messages,
  89. max_tokens=max_tokens,
  90. temperature=temperature,
  91. timeout=timeout,
  92. main_runtime=main_runtime,
  93. )
  94. return _strip_code_fence((extract_content_or_reasoning(response) or "").strip())
  95. def _strip_code_fence(text: str) -> str:
  96. """Drop a single wrapping ``` fence the model may have added."""
  97. if not text.startswith("```"):
  98. return text
  99. lines = text.splitlines()
  100. if len(lines) >= 2 and lines[-1].strip() == "```":
  101. return "\n".join(lines[1:-1]).strip()
  102. return text