1
1
from logging import getLogger
2
- from typing import Dict , List
2
+ from typing import Any , Dict , List
3
3
4
4
from taleweave .context import get_current_world , get_prompt_library , subscribe
5
5
from taleweave .game_system import FormatPerspective , GameSystem
6
6
from taleweave .models .entity import Character , Room , World , WorldEntity
7
7
from taleweave .models .event import ActionEvent , GameEvent
8
8
from taleweave .utils .prompt import format_str
9
- from taleweave .utils .search import find_containing_room
9
+ from taleweave .utils .search import find_containing_room , find_portal , find_room
10
10
11
11
logger = getLogger (__name__ )
12
12
13
13
14
+ def create_move_digest (
15
+ world : World ,
16
+ active_room : Room ,
17
+ active_character : Character ,
18
+ event : ActionEvent ,
19
+ ) -> str :
20
+ source_room = event .room
21
+ direction = str (event .parameters .get ("direction" ))
22
+ destination_portal = find_portal (world , direction )
23
+ if not destination_portal :
24
+ raise ValueError (f"Could not find portal for direction { direction } " )
25
+
26
+ destination_room = find_room (world , destination_portal .destination )
27
+ if not destination_room :
28
+ raise ValueError (
29
+ f"Could not find destination room { destination_portal .destination } "
30
+ )
31
+
32
+ # look up the source portal
33
+ source_portal = next (
34
+ (
35
+ portal
36
+ for portal in destination_room .portals
37
+ if portal .destination == source_room .name
38
+ ),
39
+ None ,
40
+ )
41
+ if not source_portal :
42
+ raise ValueError (f"Could not find source portal for { destination_portal .name } " )
43
+
44
+ mode = "self" if (event .character == active_character ) else "other"
45
+ mood = "enter" if (destination_room == active_room ) else "exit"
46
+
47
+ message = format_str (
48
+ f"digest_move_{ mode } _{ mood } " ,
49
+ destination_portal = destination_portal ,
50
+ destination_room = destination_room ,
51
+ direction = direction ,
52
+ source_portal = source_portal ,
53
+ source_room = source_room ,
54
+ )
55
+ return message
56
+
57
+
14
58
def create_turn_digest (
15
- active_room : Room , active_character : Character , turn_events : List [GameEvent ]
59
+ world : World ,
60
+ active_room : Room ,
61
+ active_character : Character ,
62
+ turn_events : List [GameEvent ],
16
63
) -> List [str ]:
17
64
library = get_prompt_library ()
18
65
messages = []
19
66
for event in turn_events :
20
67
if isinstance (event , ActionEvent ):
21
- if event .character == active_character or event .room == active_room :
68
+ # special handling for move actions
69
+ if event .action == "action_move" :
70
+ message = create_move_digest (
71
+ world , active_room , active_character , event
72
+ )
73
+ messages .append (message )
74
+ elif event .character == active_character or event .room == active_room :
22
75
prompt_key = f"digest_{ event .action } "
23
76
if prompt_key in library .prompts :
24
77
try :
25
78
template = library .prompts [prompt_key ]
26
- message = format_str (template , event = event )
79
+ message = format_str (
80
+ template ,
81
+ active_character = active_character ,
82
+ active_room = active_room ,
83
+ event = event ,
84
+ )
27
85
messages .append (message )
28
86
except Exception :
29
87
logger .exception ("error formatting digest event: %s" , event )
@@ -65,10 +123,16 @@ def format_digest(
65
123
if not room :
66
124
raise ValueError ("Character not found in any room" )
67
125
68
- digest = create_turn_digest (room , entity , buffer )
126
+ digest = create_turn_digest (world , room , entity , buffer )
69
127
return "\n " .join (digest )
70
128
71
129
130
+ def generate_digest (agent : Any , theme : str , entity : WorldEntity ):
131
+ if isinstance (entity , Character ):
132
+ if entity .name not in character_buffers :
133
+ character_buffers [entity .name ] = []
134
+
135
+
72
136
def initialize_digest (world : World ):
73
137
for room in world .rooms :
74
138
for character in room .characters :
@@ -77,4 +141,11 @@ def initialize_digest(world: World):
77
141
78
142
def init ():
79
143
subscribe (GameEvent , digest_listener )
80
- return [GameSystem ("digest" , format = format_digest , initialize = initialize_digest )]
144
+ return [
145
+ GameSystem (
146
+ "digest" ,
147
+ format = format_digest ,
148
+ generate = generate_digest ,
149
+ initialize = initialize_digest ,
150
+ )
151
+ ]
0 commit comments