refactor(prompts): share workspace override handling
This commit is contained in:
1 parent
63a6d5d07d
commit
c1fd76add3
5 files changed
+133
-58
No files matched your search
+17
-18
@@ -6,33 +6,34 @@ LLM call to decide whether the result warrants notifying the user.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.utils.helpers import truncate_text
|
||||
from nanobot.utils.prompt_templates import render_template
|
||||
from nanobot.utils.workspace_prompts import (
|
||||
WORKSPACE_PROMPT_MAX_CHARS,
|
||||
has_workspace_prompt_override,
|
||||
load_workspace_prompt_override,
|
||||
workspace_prompt_file,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.providers.base import LLMProvider
|
||||
|
||||
# Cap for a workspace-local heartbeat evaluator prompt override.
|
||||
EVALUATOR_PROMPT_MAX_CHARS = 32_000
|
||||
EVALUATOR_PROMPT_MAX_CHARS = WORKSPACE_PROMPT_MAX_CHARS
|
||||
|
||||
|
||||
def evaluator_prompt_file(workspace: Path) -> Path:
|
||||
"""Path to the workspace-local heartbeat evaluator prompt override."""
|
||||
return workspace / "prompts" / "evaluator.md"
|
||||
return workspace_prompt_file(workspace, "evaluator")
|
||||
|
||||
|
||||
def has_evaluator_prompt_override(workspace: Path) -> bool:
|
||||
"""True when the workspace defines a non-empty evaluator prompt override."""
|
||||
with suppress(OSError):
|
||||
path = evaluator_prompt_file(workspace)
|
||||
return path.is_file() and bool(path.read_text(encoding="utf-8").strip())
|
||||
return False
|
||||
return has_workspace_prompt_override(evaluator_prompt_file(workspace))
|
||||
|
||||
|
||||
def default_evaluator_prompt() -> str:
|
||||
@@ -46,16 +47,14 @@ def resolve_evaluator_prompt(workspace: Path) -> str:
|
||||
Oversized overrides are truncated so a runaway file cannot blow up the
|
||||
evaluator call.
|
||||
"""
|
||||
with suppress(OSError):
|
||||
text = evaluator_prompt_file(workspace).read_text(encoding="utf-8").rstrip()
|
||||
if text:
|
||||
if len(text) > EVALUATOR_PROMPT_MAX_CHARS:
|
||||
logger.warning(
|
||||
"Workspace heartbeat evaluator prompt exceeds {} chars ({}); truncating.",
|
||||
EVALUATOR_PROMPT_MAX_CHARS, len(text),
|
||||
)
|
||||
return truncate_text(text, EVALUATOR_PROMPT_MAX_CHARS)
|
||||
return text
|
||||
text, original_chars = load_workspace_prompt_override(evaluator_prompt_file(workspace))
|
||||
if text is not None:
|
||||
if original_chars > EVALUATOR_PROMPT_MAX_CHARS:
|
||||
logger.warning(
|
||||
"Workspace heartbeat evaluator prompt exceeds {} chars ({}); truncating.",
|
||||
EVALUATOR_PROMPT_MAX_CHARS, original_chars,
|
||||
)
|
||||
return text
|
||||
return default_evaluator_prompt()
|
||||
|
||||
_EVALUATE_TOOL = [
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Shared file handling for workspace-local prompt overrides."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.utils.helpers import truncate_text
|
||||
|
||||
WORKSPACE_PROMPT_MAX_CHARS = 32_000
|
||||
|
||||
|
||||
def workspace_prompt_file(workspace: Path, name: str) -> Path:
|
||||
"""Return the conventional path for a named workspace prompt override."""
|
||||
return workspace / "prompts" / f"{name}.md"
|
||||
|
||||
|
||||
def load_workspace_prompt_override(
|
||||
path: Path,
|
||||
*,
|
||||
max_chars: int = WORKSPACE_PROMPT_MAX_CHARS,
|
||||
) -> tuple[str | None, int]:
|
||||
"""Load and cap a non-empty UTF-8 prompt override.
|
||||
|
||||
Returns the loaded text and its original length. Missing, unreadable, and
|
||||
empty files return ``(None, 0)`` so callers can fall back to their default.
|
||||
"""
|
||||
with suppress(OSError):
|
||||
text = path.read_text(encoding="utf-8").rstrip()
|
||||
if text:
|
||||
original_chars = len(text)
|
||||
return truncate_text(text, max_chars), original_chars
|
||||
return None, 0
|
||||
|
||||
|
||||
def has_workspace_prompt_override(path: Path) -> bool:
|
||||
"""Return whether a path contains a non-empty workspace prompt override."""
|
||||
text, _original_chars = load_workspace_prompt_override(path)
|
||||
return text is not None
|
||||
|
||||
|
||||
def initialize_workspace_prompt(path: Path, default_prompt: str) -> bool:
|
||||
"""Create a default prompt copy when the target is missing or empty.
|
||||
|
||||
Returns ``False`` without overwriting a non-empty file, non-file path, or
|
||||
path whose current state cannot be read safely.
|
||||
"""
|
||||
try:
|
||||
if path.exists() and (
|
||||
not path.is_file() or bool(path.read_text(encoding="utf-8").strip())
|
||||
):
|
||||
return False
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(default_prompt + "\n", encoding="utf-8")
|
||||
return True
|
||||
Reference in new issue
Block a user