-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext_optimizer.py
More file actions
189 lines (159 loc) · 7.14 KB
/
context_optimizer.py
File metadata and controls
189 lines (159 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Context Optimizer (T7 — MEMO)
==============================
Prepends a compact "State of the Game" (SOTG) block to every AI prompt so
the LLM always has the physical scene anchor at the very top of context,
regardless of how much memory, lore, and emotional data follows.
Based on arXiv:2603.09022 — MEMO: Memory-Augmented Model Context Optimization.
Architecture:
SceneAnchor — dataclass snapshot of the current physical scene
ContextOptimizer — builds and prepends the SOTG block each turn
Integration point:
YggdrasilAIRouter.route_call() calls prepend_sotg() after prompts are
assembled and before the LLM call.
"""
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
@dataclass
class SceneAnchor:
"""Compact physical-scene snapshot injected at prompt top."""
location_id: str
location_name: str
time_of_day: str
weather: Optional[str]
npcs_present: List[str] # display names
player_name: str
turn_number: int
chaos_factor: int
active_quest_titles: List[str] = field(default_factory=list)
ambient_notes: Optional[str] = None
def render(self, max_npcs: int = 6, include_weather: bool = True,
include_quests: bool = True) -> str:
npc_names = self.npcs_present[:max_npcs]
npc_line = ", ".join(npc_names) if npc_names else "none"
if len(self.npcs_present) > max_npcs:
npc_line += f" (+{len(self.npcs_present) - max_npcs} more)"
parts = [
"=== STATE OF THE GAME ===",
f"Turn {self.turn_number} | Chaos {self.chaos_factor}/10",
f"Location: {self.location_name} ({self.location_id})",
f"Time: {self.time_of_day}",
]
if include_weather and self.weather:
parts.append(f"Weather: {self.weather}")
if self.ambient_notes:
parts.append(f"Ambient: {self.ambient_notes}")
parts.append(f"Present: {self.player_name} + {npc_line}")
if include_quests and self.active_quest_titles:
quests = "; ".join(self.active_quest_titles[:3])
parts.append(f"Active quests: {quests}")
parts.append("=== END SOTG ===")
return "\n".join(parts)
class ContextOptimizer:
"""
Builds a SceneAnchor each turn and prepends its rendered block to the
assembled prompt. Safe to call even if game_state is incomplete — all
field access uses getattr with defaults.
Never raises — on any exception the original prompt is returned unchanged.
"""
def __init__(self, config: Optional[Dict[str, Any]] = None):
cfg = (config or {}).get("context_optimizer", {})
self._enabled = cfg.get("enabled", True)
self._max_npcs = cfg.get("sotg_max_npcs", 6)
self._include_weather = cfg.get("sotg_include_weather", True)
self._include_quests = cfg.get("sotg_include_quests", True)
self._last_anchor: Optional[SceneAnchor] = None
# Per-turn SOTG cache: (turn_number, rendered_sotg_block)
self._sotg_cache: Optional[tuple] = None
def build_anchor(self, game_state: Any) -> SceneAnchor:
"""
Construct a SceneAnchor from a GameState (dataclass or dict).
Accepts both attribute-style (GameState dataclass) and dict access.
"""
def _get(obj: Any, key: str, default: Any = "") -> Any:
if isinstance(obj, dict):
return obj.get(key, default)
return getattr(obj, key, default)
# NPC display names
npcs_raw = _get(game_state, "npcs_present", []) or []
npc_names: List[str] = []
for npc in npcs_raw:
if isinstance(npc, dict):
name = (
npc.get("identity", {}).get("name")
or npc.get("name")
or npc.get("id", "Unknown")
)
else:
name = str(npc)
npc_names.append(name)
# Active quest titles
quest_titles: List[str] = []
quest_data = _get(game_state, "quest_data", {}) or {}
for quest_id, quest in quest_data.items():
if isinstance(quest, dict) and quest.get("status") == "active":
quest_titles.append(quest.get("title") or quest_id)
# Also check active_quests list
for quest_id in (_get(game_state, "active_quests", []) or []):
if isinstance(quest_id, str) and quest_id not in quest_titles:
quest_titles.append(quest_id)
# Player name
pc = _get(game_state, "player_character", {}) or {}
if isinstance(pc, dict):
player_name = (
pc.get("identity", {}).get("name")
or pc.get("name")
or "Player"
)
else:
player_name = "Player"
# Location name — try sub-location first for specificity
location_id = _get(game_state, "current_sub_location_id", "") or \
_get(game_state, "current_location_id", "unknown")
location_name = _get(game_state, "current_location_name", "") or \
location_id.replace("_", " ").title()
anchor = SceneAnchor(
location_id=str(location_id),
location_name=str(location_name),
time_of_day=str(_get(game_state, "time_of_day", "unknown")),
weather=_get(game_state, "current_weather", None) or None,
npcs_present=npc_names,
player_name=player_name,
turn_number=int(_get(game_state, "turn_count", 0)),
chaos_factor=int(_get(game_state, "chaos_factor", 5)),
active_quest_titles=quest_titles,
ambient_notes=_get(game_state, "ambient_scene_notes", None) or None,
)
self._last_anchor = anchor
return anchor
def prepend_sotg(self, prompt: str, game_state: Any) -> str:
"""
Build SceneAnchor and prepend its rendered block to prompt.
Returns prompt unchanged on any exception.
The rendered SOTG block is cached for the current turn: multiple
route_call() invocations within the same turn hit the cache and skip
the anchor-build + render work entirely.
"""
if not self._enabled:
return prompt
try:
turn = int(getattr(game_state, "turn_count", None)
or (game_state.get("turn_count", 0) if isinstance(game_state, dict) else 0))
if self._sotg_cache is not None and self._sotg_cache[0] == turn:
return self._sotg_cache[1] + "\n\n" + prompt
anchor = self.build_anchor(game_state)
sotg = anchor.render(
max_npcs=self._max_npcs,
include_weather=self._include_weather,
include_quests=self._include_quests,
)
self._sotg_cache = (turn, sotg)
return sotg + "\n\n" + prompt
except Exception as exc:
logger.warning("ContextOptimizer.prepend_sotg failed: %s", exc)
return prompt
def get_last_anchor(self) -> Optional[SceneAnchor]:
return self._last_anchor