Guard against duplicate narrative entries
This commit is contained in:
@@ -112,8 +112,6 @@ def validate_update_size(name: str, new_content: str, path: Path) -> bool:
|
||||
|
||||
def apply_state(result: TurnResult) -> None:
|
||||
"""Write state changes from a TurnResult to disk."""
|
||||
if result.ambience:
|
||||
AMBIENCE_PATH.write_text(result.ambience.strip().lower() + "\n")
|
||||
if result.changes:
|
||||
CHANGES_PATH.write_text("\n".join(result.changes) + "\n")
|
||||
else:
|
||||
|
||||
@@ -4,8 +4,8 @@ import json
|
||||
import random
|
||||
import re
|
||||
|
||||
from .paths import CHAR_PATH, WORLD_PATH
|
||||
from .state import read_file, validate_update_size, update_journal, append_llm_log
|
||||
from .paths import AMBIENCE_PATH, CHAR_PATH, WORLD_PATH
|
||||
from .state import read_file, validate_update_size, update_journal, append_llm_log, get_valid_ambiences
|
||||
|
||||
|
||||
TOOL_REGISTRY: dict[str, dict] = {
|
||||
@@ -168,6 +168,20 @@ def tool_journal_update(args: dict) -> str:
|
||||
return "Journal updated."
|
||||
|
||||
|
||||
def tool_finalize_turn(args: dict) -> str:
|
||||
"""Validate ambience and write to AMBIENCE_PATH."""
|
||||
raw = (args or {}).get("ambience", "").strip().lower()
|
||||
if not raw:
|
||||
AMBIENCE_PATH.write_text("silence\n")
|
||||
return "Ambience set to silence."
|
||||
valid = get_valid_ambiences()
|
||||
if raw not in valid:
|
||||
append_llm_log(f"\n[WARN] invalid ambience '{raw}', allowed: {sorted(valid)}")
|
||||
return f"**Error:** invalid ambience '{raw}'. Allowed: {', '.join(sorted(valid))}."
|
||||
AMBIENCE_PATH.write_text(raw + "\n")
|
||||
return f"Ambience set to {raw}."
|
||||
|
||||
|
||||
def execute_tool(tool_name: str, args: dict) -> str:
|
||||
"""Execute a tool by name. Returns result string."""
|
||||
fn_map = {
|
||||
@@ -181,6 +195,7 @@ def execute_tool(tool_name: str, args: dict) -> str:
|
||||
"replace_note": tool_replace_note,
|
||||
"world_update": tool_world_update,
|
||||
"journal_update": tool_journal_update,
|
||||
"finalize_turn": tool_finalize_turn,
|
||||
}
|
||||
fn = fn_map.get(tool_name)
|
||||
if not fn:
|
||||
@@ -227,6 +242,9 @@ def describe_change(tool_name: str, args: dict) -> str:
|
||||
for d in args.get("done", []):
|
||||
parts.append(f"✅ {d}")
|
||||
return "; ".join(parts) if parts else ""
|
||||
elif tool_name == "finalize_turn":
|
||||
a = args.get("ambience", "")
|
||||
return f"♫ {a}" if a else ""
|
||||
return ""
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user