Restore character facts in H3 ref context

This commit is contained in:
2026-08-27 07:58:05 +00:00
parent 32fc9b39df
commit a660919425
2 changed files with 82 additions and 1 deletions
+55
View File
@@ -4239,6 +4239,58 @@ def _reference_sentence(value):
return text return text
def _reference_positive_int(value):
text = _reference_text(value)
if not text:
return None
try:
parsed = int(text)
except (TypeError, ValueError):
return None
return parsed if parsed > 0 else None
def _reference_height_text(facts):
feet = _reference_text((facts or {}).get("height_feet"))
inches = _reference_text((facts or {}).get("height_inches"))
if feet and inches:
return f"{feet} foot {inches}"
if feet:
return f"{feet} foot"
if inches:
return f"{inches} inch"
return ""
def _reference_fact_sentence(ref, label):
facts = dict(ref.get("facts") or {})
bits = []
gender = _reference_text(facts.get("gender"))
age = _reference_positive_int(facts.get("age"))
nationality = _reference_text(facts.get("nationality"))
occupation = _reference_text(facts.get("occupation"))
accent = _reference_text(facts.get("accent"))
height = _reference_height_text(facts)
aliases = [_reference_text(alias) for alias in (ref.get("aliases") or []) if _reference_text(alias)]
if aliases:
bits.append(f"also known as {aliases[0]}")
if gender:
bits.append(gender)
if age is not None:
bits.append(f"{age} years old")
if nationality:
bits.append(nationality)
if occupation:
bits.append(f"works as {occupation}")
if height:
bits.append(f"{height} tall")
if accent:
bits.append(f"speaks with a {accent} accent")
if not bits:
return ""
return f"Character facts for {label}: " + ", ".join(bits) + "."
def _reference_name_keys(ref): def _reference_name_keys(ref):
names = [] names = []
for key in ("name", "id"): for key in ("name", "id"):
@@ -4305,12 +4357,15 @@ def _reference_context_for_text(text, ref_slots):
description = _reference_sentence(ref.get("description")) description = _reference_sentence(ref.get("description"))
wardrobe = _reference_sentence(ref.get("wardrobe")) wardrobe = _reference_sentence(ref.get("wardrobe"))
general = _reference_sentence(ref.get("general")) general = _reference_sentence(ref.get("general"))
facts = _reference_fact_sentence(ref, label)
if ref.get("kind") == "location": if ref.get("kind") == "location":
if description: if description:
parts.append(f"Location context for {label}: {description}") parts.append(f"Location context for {label}: {description}")
if general: if general:
parts.append(f"Location notes for {label}: {general}") parts.append(f"Location notes for {label}: {general}")
continue continue
if facts:
parts.append(facts)
if description: if description:
parts.append(f"Persistent appearance for {label}: {description}") parts.append(f"Persistent appearance for {label}: {description}")
if wardrobe: if wardrobe:
+27 -1
View File
@@ -288,7 +288,24 @@ class DumasH3LongVideosHelperTests(unittest.TestCase):
def test_reference_context_matches_character_names_and_location_tags(self): def test_reference_context_matches_character_names_and_location_tags(self):
refs = [ refs = [
{"kind": "character", "image": "img1", "name": "Mara", "description": "silver hair", "wardrobe": "red jacket"}, {
"kind": "character",
"image": "img1",
"name": "Mara",
"aliases": ["Xtina"],
"description": "silver hair",
"wardrobe": "red jacket",
"general": "wears a long grey coat",
"facts": {
"gender": "female",
"age": "41",
"nationality": "English",
"occupation": "a detective",
"height_feet": "6",
"height_inches": "2",
"accent": "English",
},
},
{"kind": "location", "image": "img2", "name": "Hangar", "description": "wet concrete floor"}, {"kind": "location", "image": "img2", "name": "Hangar", "description": "wet concrete floor"},
] ]
@@ -297,8 +314,17 @@ class DumasH3LongVideosHelperTests(unittest.TestCase):
refs, refs,
) )
self.assertIn("Character facts for Mara:", context)
self.assertIn("also known as Xtina", context)
self.assertIn("female", context)
self.assertIn("41 years old", context)
self.assertIn("English", context)
self.assertIn("works as a detective", context)
self.assertIn("6 foot 2 tall", context)
self.assertIn("speaks with a English accent", context)
self.assertIn("Persistent appearance for Mara: silver hair.", context) self.assertIn("Persistent appearance for Mara: silver hair.", context)
self.assertIn("Persistent wardrobe/style for Mara: red jacket.", context) self.assertIn("Persistent wardrobe/style for Mara: red jacket.", context)
self.assertIn("Character notes for Mara: wears a long grey coat.", context)
self.assertIn("Location context for Hangar: wet concrete floor.", context) self.assertIn("Location context for Hangar: wet concrete floor.", context)
def test_reference_context_matches_tagged_character_without_name_in_text(self): def test_reference_context_matches_tagged_character_without_name_in_text(self):