Hermes Bot commited on
feat(tinybard): improve pacing with five-sentence directive and step awareness
Browse files- .agents/tasks/TASK.2026-06-10_pacing.md +16 -0
- projects/tinybard/app.py +16 -15
.agents/tasks/TASK.2026-06-10_pacing.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Task: TinyBard Pacing and Narrative Space Enhancements
|
| 2 |
+
|
| 3 |
+
- Branch: feat/tinybard-five-sentence-pacing
|
| 4 |
+
- Date: 2026-06-10
|
| 5 |
+
|
| 6 |
+
## Chain-of-Draft
|
| 7 |
+
|
| 8 |
+
1. Initialize task file.
|
| 9 |
+
2. Read _parse_messages and generate_llm_story.
|
| 10 |
+
3. Update instructions to 5-sentences.
|
| 11 |
+
4. Pass step context.
|
| 12 |
+
5. Increase max_new_tokens.
|
| 13 |
+
6. Verify and commit.
|
| 14 |
+
|
| 15 |
+
####
|
| 16 |
+
Proceeding with implementation.
|
projects/tinybard/app.py
CHANGED
|
@@ -385,16 +385,16 @@ def generate_procedural_step(genre: str, step: int, health: int, choice: str = "
|
|
| 385 |
# ---------------------------------------------------------------------------
|
| 386 |
# LLM Generation Logic (HF Inference API + cooldown)
|
| 387 |
# ---------------------------------------------------------------------------
|
| 388 |
-
def _parse_messages(genre: str, history: List[Dict[str, str]], next_instruction: str) -> list[Dict[str, str]]:
|
| 389 |
"""Translate internal history into OpenAI-style chat messages.
|
| 390 |
|
| 391 |
-
Includes the last
|
| 392 |
"""
|
| 393 |
system = (
|
| 394 |
"You are Nanaboozhoo, the trickster storyteller of Anishinaabe tradition. "
|
| 395 |
"You spin interactive text adventures with wit, mischief, and wonder. "
|
| 396 |
f"Genre: {genre}. Write in the second person ('You...'). "
|
| 397 |
-
"Keep descriptions atmospheric
|
| 398 |
"Be unpredictable — every story beat should surprise. "
|
| 399 |
"Never repeat the same scene twice. "
|
| 400 |
"Focus on action, mystery, and choice. Do not offer numbered choices unless asked. "
|
|
@@ -404,15 +404,16 @@ def _parse_messages(genre: str, history: List[Dict[str, str]], next_instruction:
|
|
| 404 |
)
|
| 405 |
msgs: List[Dict[str, str]] = [{"role": "system", "content": system}]
|
| 406 |
|
| 407 |
-
# Include last
|
| 408 |
-
recent_history = (history or [])[-
|
| 409 |
for h in recent_history:
|
| 410 |
if h.get("role") == "player":
|
| 411 |
msgs.append({"role": "user", "content": h["text"]})
|
| 412 |
elif h.get("role") == "narrator":
|
| 413 |
msgs.append({"role": "assistant", "content": h["text"]})
|
| 414 |
|
| 415 |
-
|
|
|
|
| 416 |
return msgs
|
| 417 |
|
| 418 |
|
|
@@ -420,13 +421,14 @@ def generate_llm_story(
|
|
| 420 |
genre: str,
|
| 421 |
history: List[Dict[str, str]],
|
| 422 |
next_instruction: str,
|
| 423 |
-
max_tokens: int =
|
|
|
|
| 424 |
) -> str:
|
| 425 |
"""Generate story text via HF Inference API (with cooldown)."""
|
| 426 |
from shared.inference_client import force_clear_cooldown
|
| 427 |
force_clear_cooldown("tinybard")
|
| 428 |
try:
|
| 429 |
-
msgs = _parse_messages(genre, history, next_instruction)
|
| 430 |
result = inference_generate(
|
| 431 |
project="tinybard",
|
| 432 |
messages=msgs,
|
|
@@ -440,7 +442,6 @@ def generate_llm_story(
|
|
| 440 |
log.warning(f"HF Inference error (fallback to procedural): {e}")
|
| 441 |
return ""
|
| 442 |
|
| 443 |
-
|
| 444 |
def generate_llm_choices(genre: str, story_context: str, history: List[Dict[str, str]] | None = None) -> List[str]:
|
| 445 |
"""Ask the LLM to produce 3 verb-based action choices for the player."""
|
| 446 |
from shared.inference_client import force_clear_cooldown
|
|
@@ -1228,8 +1229,8 @@ def _run_turn(choice: str, genre: str, step: int, health: int, history: List[Dic
|
|
| 1228 |
prefixes = ["Option one", "Option two", "Option three"]
|
| 1229 |
|
| 1230 |
if step == 0:
|
| 1231 |
-
instruction = "Narrate the beginning of the adventure. What happens first? Do not offer choices yet."
|
| 1232 |
-
story = generate_llm_story(genre, [], instruction)
|
| 1233 |
if not story:
|
| 1234 |
res = generate_procedural_step(genre, 0, 100)
|
| 1235 |
formatted_choices = [f"{prefixes[i]}: {strip_option_prefix(ch)}" for i, ch in enumerate(res["choices"][:3])]
|
|
@@ -1275,8 +1276,8 @@ def _run_turn(choice: str, genre: str, step: int, health: int, history: List[Dic
|
|
| 1275 |
new_health = max(0, min(100, health + health_delta))
|
| 1276 |
|
| 1277 |
if new_health <= 0:
|
| 1278 |
-
instruction = "The player has run out of health. Narrate a quick, dramatic end. Game Over."
|
| 1279 |
-
story = generate_llm_story(genre, history, instruction)
|
| 1280 |
final_story = story or "Your strength fails. The adventure ends in darkness."
|
| 1281 |
audio_url, audio_path = generate_tts_for_turn(final_story, [], genre)
|
| 1282 |
clean_story = re.sub(r"\[MOOD:\s*[a-zA-Z0-9_\-]+\]", "", final_story).strip()
|
|
@@ -1294,10 +1295,10 @@ def _run_turn(choice: str, genre: str, step: int, health: int, history: List[Dic
|
|
| 1294 |
|
| 1295 |
instruction = (
|
| 1296 |
f"The player chose: '{clean_choice}'. "
|
| 1297 |
-
"Narrate what happens next as a direct consequence of this action. "
|
| 1298 |
"Be specific to their choice — reference what they did and its immediate result."
|
| 1299 |
)
|
| 1300 |
-
story = generate_llm_story(genre, history, instruction)
|
| 1301 |
if not story:
|
| 1302 |
res = generate_procedural_step(genre, step, health, clean_choice)
|
| 1303 |
formatted_choices = [f"{prefixes[i]}: {strip_option_prefix(ch)}" for i, ch in enumerate(res["choices"][:3])]
|
|
|
|
| 385 |
# ---------------------------------------------------------------------------
|
| 386 |
# LLM Generation Logic (HF Inference API + cooldown)
|
| 387 |
# ---------------------------------------------------------------------------
|
| 388 |
+
def _parse_messages(genre: str, history: List[Dict[str, str]], next_instruction: str, step: int = 0) -> list[Dict[str, str]]:
|
| 389 |
"""Translate internal history into OpenAI-style chat messages.
|
| 390 |
|
| 391 |
+
Includes the last 5 story beats as context for coherent narrative flow.
|
| 392 |
"""
|
| 393 |
system = (
|
| 394 |
"You are Nanaboozhoo, the trickster storyteller of Anishinaabe tradition. "
|
| 395 |
"You spin interactive text adventures with wit, mischief, and wonder. "
|
| 396 |
f"Genre: {genre}. Write in the second person ('You...'). "
|
| 397 |
+
"Write exactly five sentences for the story narrative. Keep descriptions atmospheric, engaging, and complete. "
|
| 398 |
"Be unpredictable — every story beat should surprise. "
|
| 399 |
"Never repeat the same scene twice. "
|
| 400 |
"Focus on action, mystery, and choice. Do not offer numbered choices unless asked. "
|
|
|
|
| 404 |
)
|
| 405 |
msgs: List[Dict[str, str]] = [{"role": "system", "content": system}]
|
| 406 |
|
| 407 |
+
# Include last 10 history entries as context (most recent first for relevance)
|
| 408 |
+
recent_history = (history or [])[-10:] # Last 10 entries = ~5 story beats (player + narrator pairs)
|
| 409 |
for h in recent_history:
|
| 410 |
if h.get("role") == "player":
|
| 411 |
msgs.append({"role": "user", "content": h["text"]})
|
| 412 |
elif h.get("role") == "narrator":
|
| 413 |
msgs.append({"role": "assistant", "content": h["text"]})
|
| 414 |
|
| 415 |
+
progress_info = f"[Adventure Progress - Step {step}]"
|
| 416 |
+
msgs.append({"role": "user", "content": f"{progress_info}\nInstruction: {next_instruction}"})
|
| 417 |
return msgs
|
| 418 |
|
| 419 |
|
|
|
|
| 421 |
genre: str,
|
| 422 |
history: List[Dict[str, str]],
|
| 423 |
next_instruction: str,
|
| 424 |
+
max_tokens: int = 350,
|
| 425 |
+
step: int = 0,
|
| 426 |
) -> str:
|
| 427 |
"""Generate story text via HF Inference API (with cooldown)."""
|
| 428 |
from shared.inference_client import force_clear_cooldown
|
| 429 |
force_clear_cooldown("tinybard")
|
| 430 |
try:
|
| 431 |
+
msgs = _parse_messages(genre, history, next_instruction, step=step)
|
| 432 |
result = inference_generate(
|
| 433 |
project="tinybard",
|
| 434 |
messages=msgs,
|
|
|
|
| 442 |
log.warning(f"HF Inference error (fallback to procedural): {e}")
|
| 443 |
return ""
|
| 444 |
|
|
|
|
| 445 |
def generate_llm_choices(genre: str, story_context: str, history: List[Dict[str, str]] | None = None) -> List[str]:
|
| 446 |
"""Ask the LLM to produce 3 verb-based action choices for the player."""
|
| 447 |
from shared.inference_client import force_clear_cooldown
|
|
|
|
| 1229 |
prefixes = ["Option one", "Option two", "Option three"]
|
| 1230 |
|
| 1231 |
if step == 0:
|
| 1232 |
+
instruction = "Narrate the beginning of the adventure. What happens first? Write exactly five sentences. Do not offer choices yet."
|
| 1233 |
+
story = generate_llm_story(genre, [], instruction, step=0)
|
| 1234 |
if not story:
|
| 1235 |
res = generate_procedural_step(genre, 0, 100)
|
| 1236 |
formatted_choices = [f"{prefixes[i]}: {strip_option_prefix(ch)}" for i, ch in enumerate(res["choices"][:3])]
|
|
|
|
| 1276 |
new_health = max(0, min(100, health + health_delta))
|
| 1277 |
|
| 1278 |
if new_health <= 0:
|
| 1279 |
+
instruction = "The player has run out of health. Narrate a quick, dramatic end in exactly five sentences. Game Over."
|
| 1280 |
+
story = generate_llm_story(genre, history, instruction, step=step)
|
| 1281 |
final_story = story or "Your strength fails. The adventure ends in darkness."
|
| 1282 |
audio_url, audio_path = generate_tts_for_turn(final_story, [], genre)
|
| 1283 |
clean_story = re.sub(r"\[MOOD:\s*[a-zA-Z0-9_\-]+\]", "", final_story).strip()
|
|
|
|
| 1295 |
|
| 1296 |
instruction = (
|
| 1297 |
f"The player chose: '{clean_choice}'. "
|
| 1298 |
+
"Narrate what happens next as a direct consequence of this action in exactly five sentences. "
|
| 1299 |
"Be specific to their choice — reference what they did and its immediate result."
|
| 1300 |
)
|
| 1301 |
+
story = generate_llm_story(genre, history, instruction, step=step)
|
| 1302 |
if not story:
|
| 1303 |
res = generate_procedural_step(genre, step, health, clean_choice)
|
| 1304 |
formatted_choices = [f"{prefixes[i]}: {strip_option_prefix(ch)}" for i, ch in enumerate(res["choices"][:3])]
|