5
5
broadcast ,
6
6
get_agent_for_character ,
7
7
get_character_agent_for_name ,
8
+ get_prompt ,
8
9
world_context ,
9
10
)
10
11
from taleweave .errors import ActionError
11
12
from taleweave .utils .conversation import loop_conversation
13
+ from taleweave .utils .prompt import format_prompt
12
14
from taleweave .utils .search import (
13
15
find_character_in_room ,
14
16
find_item_in_character ,
17
19
find_room ,
18
20
)
19
21
from taleweave .utils .string import normalize_name
20
- from taleweave .utils .world import describe_entity
21
22
22
23
logger = getLogger (__name__ )
23
24
@@ -33,34 +34,65 @@ def action_examine(target: str) -> str:
33
34
"""
34
35
35
36
with action_context () as (action_room , action_character ):
36
- broadcast (f"{ action_character .name } looks at { target } " )
37
+ broadcast (
38
+ format_prompt (
39
+ "action_examine_broadcast_action" ,
40
+ action_character = action_character ,
41
+ target = target ,
42
+ )
43
+ )
37
44
38
45
if normalize_name (target ) == normalize_name (action_room .name ):
39
- broadcast (f"{ action_character .name } saw the { action_room .name } room" )
40
- return describe_entity (action_room )
46
+ broadcast (
47
+ format_prompt (
48
+ "action_examine_broadcast_room" ,
49
+ action_character = action_character ,
50
+ action_room = action_room ,
51
+ )
52
+ )
53
+ return format_prompt ("action_examine_result_room" , action_room = action_room )
41
54
42
55
target_character = find_character_in_room (action_room , target )
43
56
if target_character :
44
57
broadcast (
45
- f"{ action_character .name } saw the { target_character .name } character in the { action_room .name } room"
58
+ format_prompt (
59
+ "action_examine_broadcast_character" ,
60
+ action_character = action_character ,
61
+ action_room = action_room ,
62
+ target_character = target_character ,
63
+ )
64
+ )
65
+ return format_prompt (
66
+ "action_examine_result_character" , target_character = target_character
46
67
)
47
- return describe_entity (target_character )
48
68
49
69
target_item = find_item_in_room (action_room , target )
50
70
if target_item :
51
71
broadcast (
52
- f"{ action_character .name } saw the { target_item .name } item in the { action_room .name } room"
72
+ format_prompt (
73
+ "action_examine_broadcast_item" ,
74
+ action_character = action_character ,
75
+ action_room = action_room ,
76
+ target_item = target_item ,
77
+ )
53
78
)
54
- return describe_entity ( target_item )
79
+ return format_prompt ( "action_examine_result_item" , target_item = target_item )
55
80
56
81
target_item = find_item_in_character (action_character , target )
57
82
if target_item :
58
83
broadcast (
59
- f"{ action_character .name } saw the { target_item .name } item in their inventory"
84
+ format_prompt (
85
+ "action_examine_broadcast_inventory" ,
86
+ action_character = action_character ,
87
+ action_room = action_room ,
88
+ target_item = target_item ,
89
+ )
90
+ )
91
+ return format_prompt (
92
+ "action_examine_result_inventory" , target_item = target_item
60
93
)
61
- return describe_entity (target_item )
62
94
63
- return "You do not see that item or character in the room."
95
+ return format_prompt ( "action_examine_error_target" , target = target )
64
96
65
97
66
98
def action_move (direction : str ) -> str :
@@ -74,20 +106,36 @@ def action_move(direction: str) -> str:
74
106
with world_context () as (action_world , action_room , action_character ):
75
107
portal = find_portal_in_room (action_room , direction )
76
108
if not portal :
77
- raise ActionError (f"You cannot move { direction } from here." )
109
+ portals = [p .name for p in action_room .portals ]
110
+ raise ActionError (
111
+ format_prompt (
112
+ "action_move_error_direction" , direction = direction , portals = portals
113
+ )
114
+ )
78
115
79
- destination_room = find_room (action_world , portal .destination )
80
- if not destination_room :
81
- raise ActionError (f"The { portal .destination } room does not exist." )
116
+ dest_room = find_room (action_world , portal .destination )
117
+ if not dest_room :
118
+ raise ActionError (
119
+ format_prompt (
120
+ "action_move_error_room" ,
121
+ direction = direction ,
122
+ destination = portal .destination ,
123
+ )
124
+ )
82
125
83
126
broadcast (
84
- f"{ action_character .name } moves through { direction } to { destination_room .name } "
127
+ format_prompt (
128
+ "action_move_broadcast" ,
129
+ action_character = action_character ,
130
+ dest_room = dest_room ,
131
+ direction = direction ,
132
+ )
85
133
)
86
134
action_room .characters .remove (action_character )
87
- destination_room .characters .append (action_character )
135
+ dest_room .characters .append (action_character )
88
136
89
- return (
90
- f"You move through the { direction } and arrive at { destination_room . name } ."
137
+ return format_prompt (
138
+ "action_move_result" , direction = direction , dest_room = dest_room
91
139
)
92
140
93
141
@@ -101,12 +149,20 @@ def action_take(item: str) -> str:
101
149
with action_context () as (action_room , action_character ):
102
150
action_item = find_item_in_room (action_room , item )
103
151
if not action_item :
104
- raise ActionError (f"The { item } item is not in the room." )
152
+ raise ActionError (format_prompt ( "action_take_error_item" , item = item ) )
105
153
106
- broadcast (f"{ action_character .name } takes the { item } item" )
154
+ broadcast (
155
+ format_prompt (
156
+ "action_take_broadcast" ,
157
+ action_character = action_character ,
158
+ action_room = action_room ,
159
+ item = item ,
160
+ )
161
+ )
107
162
action_room .items .remove (action_item )
108
163
action_character .items .append (action_item )
109
- return f"You take the { item } item and put it in your inventory."
164
+
165
+ return format_prompt ("action_take_result" , item = item )
110
166
111
167
112
168
def action_ask (character : str , question : str ) -> str :
@@ -122,27 +178,31 @@ def action_ask(character: str, question: str) -> str:
122
178
# sanity checks
123
179
question_character , question_agent = get_character_agent_for_name (character )
124
180
if question_character == action_character :
125
- raise ActionError (
126
- "You cannot ask yourself a question. Stop talking to yourself. Try another action."
127
- )
181
+ raise ActionError (format_prompt ("action_ask_error_self" ))
128
182
129
183
if not question_character :
130
- raise ActionError (f"The { character } character is not in the room." )
184
+ raise ActionError (
185
+ format_prompt ("action_ask_error_target" , character = character )
186
+ )
131
187
132
188
if not question_agent :
133
- raise ActionError (f"The { character } character does not exist." )
189
+ raise ActionError (
190
+ format_prompt ("action_ask_error_agent" , character = character )
191
+ )
134
192
135
- broadcast (f"{ action_character .name } asks { character } : { question } " )
136
- first_prompt = (
137
- "{last_character.name} asks you: {response}\n "
138
- "Reply with your response to them. Reply with 'END' to end the conversation. "
139
- "Do not include the question or any JSON. Only include your answer for {last_character.name}."
140
- )
141
- reply_prompt = (
142
- "{last_character.name} continues the conversation with you. They reply: {response}\n "
143
- "Reply with your response to them. Reply with 'END' to end the conversation. "
144
- "Do not include the question or any JSON. Only include your answer for {last_character.name}."
193
+ # TODO: make sure they are in the same room
194
+
195
+ broadcast (
196
+ format_prompt (
197
+ "action_ask_broadcast" ,
198
+ action_character = action_character ,
199
+ character = character ,
200
+ question = question ,
201
+ )
145
202
)
203
+ first_prompt = get_prompt ("action_ask_conversation_first" )
204
+ reply_prompt = get_prompt ("action_ask_conversation_reply" )
205
+ end_prompt = get_prompt ("action_ask_conversation_end" )
146
206
147
207
action_agent = get_agent_for_character (action_character )
148
208
result = loop_conversation (
@@ -153,7 +213,7 @@ def action_ask(character: str, question: str) -> str:
153
213
first_prompt ,
154
214
reply_prompt ,
155
215
question ,
156
- "Goodbye" ,
216
+ end_prompt ,
157
217
echo_function = action_tell .__name__ ,
158
218
echo_parameter = "message" ,
159
219
max_length = MAX_CONVERSATION_STEPS ,
@@ -162,7 +222,7 @@ def action_ask(character: str, question: str) -> str:
162
222
if result :
163
223
return result
164
224
165
- return f" { character } does not respond."
225
+ return format_prompt ( "action_ask_ignore" , character = character )
166
226
167
227
168
228
def action_tell (character : str , message : str ) -> str :
@@ -179,27 +239,22 @@ def action_tell(character: str, message: str) -> str:
179
239
# sanity checks
180
240
question_character , question_agent = get_character_agent_for_name (character )
181
241
if question_character == action_character :
182
- raise ActionError (
183
- "You cannot tell yourself a message. Stop talking to yourself. Try another action."
184
- )
242
+ raise ActionError (format_prompt ("action_tell_error_self" ))
185
243
186
244
if not question_character :
187
- raise ActionError (f"The { character } character is not in the room." )
245
+ raise ActionError (
246
+ format_prompt ("action_tell_error_target" , character = character )
247
+ )
188
248
189
249
if not question_agent :
190
- raise ActionError (f"The { character } character does not exist." )
250
+ raise ActionError (
251
+ format_prompt ("action_tell_error_agent" , character = character )
252
+ )
191
253
192
254
broadcast (f"{ action_character .name } tells { character } : { message } " )
193
- first_prompt = (
194
- "{last_character.name} starts a conversation with you. They say: {response}\n "
195
- "Reply with your response to them. "
196
- "Do not include the message or any JSON. Only include your reply to {last_character.name}."
197
- )
198
- reply_prompt = (
199
- "{last_character.name} continues the conversation with you. They reply: {response}\n "
200
- "Reply with your response to them. "
201
- "Do not include the message or any JSON. Only include your reply to {last_character.name}."
202
- )
255
+ first_prompt = get_prompt ("action_tell_conversation_first" )
256
+ reply_prompt = get_prompt ("action_tell_conversation_reply" )
257
+ end_prompt = get_prompt ("action_tell_conversation_end" )
203
258
204
259
action_agent = get_agent_for_character (action_character )
205
260
result = loop_conversation (
@@ -210,7 +265,7 @@ def action_tell(character: str, message: str) -> str:
210
265
first_prompt ,
211
266
reply_prompt ,
212
267
message ,
213
- "Goodbye" ,
268
+ end_prompt ,
214
269
echo_function = action_tell .__name__ ,
215
270
echo_parameter = "message" ,
216
271
max_length = MAX_CONVERSATION_STEPS ,
@@ -219,7 +274,7 @@ def action_tell(character: str, message: str) -> str:
219
274
if result :
220
275
return result
221
276
222
- return f" { character } does not respond."
277
+ return format_prompt ( "action_tell_ignore" , character = character )
223
278
224
279
225
280
def action_give (character : str , item : str ) -> str :
@@ -233,22 +288,29 @@ def action_give(character: str, item: str) -> str:
233
288
with action_context () as (action_room , action_character ):
234
289
destination_character = find_character_in_room (action_room , character )
235
290
if not destination_character :
236
- raise ActionError (f"The { character } character is not in the room." )
237
-
238
- if destination_character == action_character :
239
291
raise ActionError (
240
- "You cannot give an item to yourself. Try another action."
292
+ format_prompt ( "action_give_error_target" , character = character )
241
293
)
242
294
295
+ if destination_character == action_character :
296
+ raise ActionError (format_prompt ("action_give_error_self" ))
297
+
243
298
action_item = find_item_in_character (action_character , item )
244
299
if not action_item :
245
- raise ActionError (f"You do not have the { item } item in your inventory." )
300
+ raise ActionError (format_prompt ( "action_give_error_item" , item = item ) )
246
301
247
- broadcast (f"{ action_character .name } gives { character } the { item } item." )
302
+ broadcast (
303
+ format_prompt (
304
+ "action_give_broadcast" ,
305
+ action_character = action_character ,
306
+ character = character ,
307
+ item = item ,
308
+ )
309
+ )
248
310
action_character .items .remove (action_item )
249
311
destination_character .items .append (action_item )
250
312
251
- return f"You give the { item } item to { character } ."
313
+ return format_prompt ( "action_give_result" , character = character , item = item )
252
314
253
315
254
316
def action_drop (item : str ) -> str :
@@ -262,10 +324,14 @@ def action_drop(item: str) -> str:
262
324
with action_context () as (action_room , action_character ):
263
325
action_item = find_item_in_character (action_character , item )
264
326
if not action_item :
265
- raise ActionError (f"You do not have the { item } item in your inventory." )
327
+ raise ActionError (format_prompt ( "action_drop_error_item" , item = item ) )
266
328
267
- broadcast (f"{ action_character .name } drops the { item } item" )
329
+ broadcast (
330
+ format_prompt (
331
+ "action_drop_broadcast" , action_character = action_character , item = item
332
+ )
333
+ )
268
334
action_character .items .remove (action_item )
269
335
action_room .items .append (action_item )
270
336
271
- return f"You drop the { item } item."
337
+ return format_prompt ( "action_drop_result" , item = item )
0 commit comments