-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript_builder.py
More file actions
536 lines (431 loc) · 20.3 KB
/
Copy pathscript_builder.py
File metadata and controls
536 lines (431 loc) · 20.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import json, os, datetime, re
from typing import List, Optional, TypedDict, Dict, Any, Annotated
import numpy as np
from glob import glob
from pydantic import BaseModel, Field
from langgraph.graph import StateGraph, START, END
from collections import Counter
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser, StrOutputParser
from dotenv import load_dotenv
from pathlib import Path
from developer_prompt import SELECT_MAP_PROMPT, PLACE_UNITS_PROMPT, RULE_CONFIG_PROMPT, GET_CONDITION_PROMPT
from db_call import DBCall
import common
load_dotenv()
class ScriptDeveloperState(TypedDict):
user_intent: str
gdd: Dict[str, Any]
selected_map: Dict
unit_config: Dict[str, Dict[str, Any]]
rule_config: Dict[str, Dict[str, Any]]
end_config: Dict[str, Dict[str, Any]]
final_json: Dict
is_script_valid: bool
script_feedback: str
loop_count: int
refined_diffs: list
class ScriptDeveloperAgent:
def __init__(self, session_timestamp, seed: int = None):
self.current_dir = Path(__file__).resolve().parent
INFO_PATH = self.current_dir / "info"
self.unit_summary = common.load_unit_summary(INFO_PATH / "units_info.json")
self.session_timestamp = session_timestamp
self.log_dir = self.current_dir / "log" / session_timestamp / "developer"
os.makedirs(self.log_dir, exist_ok=True)
# Feedback log for tracking all feedback
self.feedback_log_path = self.log_dir / f"script_feedback_{session_timestamp}.json"
self.feedback_logs = []
# Reasoning log for tracking design decisions
self.reasoning_logs = []
self.client = common.get_client(seed=seed)
self.parser = JsonOutputParser()
self.db_caller = DBCall(seed=seed)
# NOTE: Analyst and the verify/refine loop are intentionally dropped in
# this simplified build (removes the game_simulation/psutil dependency).
builder = StateGraph(ScriptDeveloperState)
builder.add_node("select_map", self.select_map)
builder.add_node("place_units", self.place_units)
builder.add_node("generate_rule_config", self.generate_rule_config)
builder.add_node("get_condition", self.get_condition)
builder.add_node("assemble_draft", self.assemble_draft)
builder.add_edge(START, "select_map")
builder.add_edge("select_map", "place_units")
builder.add_edge("place_units", "generate_rule_config")
builder.add_edge("generate_rule_config", "get_condition")
builder.add_edge("get_condition", "assemble_draft")
builder.add_edge("assemble_draft", END)
self.graph = builder.compile()
def select_map(self, state: ScriptDeveloperState) -> ScriptDeveloperState:
user_intent = state.get("user_intent", "")
gdd = state.get("gdd", {})
current_map_list = []
print("\n[Developer] 1. Selecting Map...")
for _ in range(3):
prompt = ChatPromptTemplate.from_messages([
("system", SELECT_MAP_PROMPT),
("user", """
[User intent]
{user_intent}
[GDD]
{gdd}
[Available Map List]
{map_list}
Select the best map.
""")
])
chain = prompt | self.client | self.parser
result = chain.invoke({
"user_intent": user_intent,
"gdd": json.dumps(gdd, ensure_ascii=False),
"map_list": current_map_list
})
action = result.get("action")
if action == "call_db":
query = f"Find maps matching the query: {result.get('query')}"
print(f"[Map Selector] Searching DB for: '{query}'")
found_paths = self.db_caller.call(query)
new_maps_str = common.load_from_db(found_paths)
if new_maps_str:
print(f" Found maps. Updating context...")
if isinstance(new_maps_str, list):
current_map_list.extend(new_maps_str)
else:
current_map_list.append(new_maps_str)
else:
print("No maps found in DB. Asking LLM to pick from whatever creates.")
elif action == "finish":
selected_map = result.get("selected_map", {})
reason = result.get("reason", "")
print(f"Selected Map: {list(selected_map.keys())[0]} ({reason})")
if reason:
self.reasoning_logs.append({
"agent": "script_developer",
"step": "map_selection",
"reasoning": reason
})
common.log_node_result("step1_map_selection", result, self.session_timestamp, self.log_dir)
return {"selected_map": selected_map}
print("⚠️ Map selection failed. Falling back to default map (BarR 1.1).")
default_map_path = ["map/files/barr_1.1.json"]
default_map_data = common.load_json_from_db(default_map_path)
if default_map_data:
common.log_node_result("step1_map_selection", {"selected_map": default_map_data[0], "reason": "fallback_default"}, self.session_timestamp, self.log_dir)
return {"selected_map": default_map_data[0]}
return {"selected_map": {}}
def place_units(self, state: ScriptDeveloperState) -> ScriptDeveloperState:
print("\n[DeveloperS] 2. Placing Units...")
map_data = state.get("selected_map", {})
map_name = list(map_data.keys())[0] if map_data else "Unknown"
user_intent = state.get("user_intent", {})
gdd = state.get("gdd", {})
terrain_search_query = f"Find the terrain information for {map_name}"
current_terrain_info = self.db_caller.call(terrain_search_query)
current_unit_info = []
for i in range(5):
prompt = ChatPromptTemplate.from_messages([
("system", PLACE_UNITS_PROMPT),
("user", """
[GDD]
{gdd}
[Map Name]
{map_name}
[User Intent]
{user_intent}
[Available Unit List]
{unit_info}
[Terrain Info]
{terrain_info}
Calculate the map dimensions (Size * 512) and generate the scenario.
If data is missing, search for it. Otherwise, generate placements.
""")
])
chain = prompt | self.client | self.parser
result = chain.invoke({
"gdd": json.dumps(gdd, ensure_ascii=False),
"map_name": map_name,
"unit_info": current_unit_info,
"user_intent": user_intent,
"terrain_info": current_terrain_info
})
action = result.get("action")
if action == "call_db":
query = f"Find units mathcing the query: {result.get('query', '').lower()}"
print(f" [Unit Placement] Searching DB: '{query}'")
found_paths = self.db_caller.call(query)
retrieved_data = common.load_from_db(found_paths)
if not retrieved_data:
print("⚠️ DB returned nothing. Retrying with different query might be needed.")
continue
else:
if isinstance(retrieved_data, list):
current_unit_info.extend(retrieved_data)
else:
current_unit_info.append(retrieved_data)
elif action == "finish":
print("✅ All data present. Placement Generated.")
target_keys = ["match_format", "unit_placement", "spawn_waves"]
single_config = {k: result[k] for k in target_keys if k in result}
reasoning = result.get("reasoning", "")
if reasoning:
self.reasoning_logs.append({
"agent": "script_developer",
"step": "unit_placement",
"reasoning": reasoning
})
final_unit_config = {
"unit_config": {
"normal": single_config
}
}
common.log_node_result("step2_unit_placement", result, self.session_timestamp, self.log_dir)
return final_unit_config
print("❌ Failed to place units within loop limit.")
return {"unit_config": {}}
def generate_rule_config(self, state: ScriptDeveloperState) -> ScriptDeveloperState:
print("\n[Developer] 3. Generating Rule Configurations...")
gdd = state.get("gdd", {})
map_data = state.get("selected_map", {})
unit_config = state.get("unit_config", {})
user_intent = state.get("user_intent", {})
rule_list = gdd.get("rules", [])
if not rule_list:
print(" ⚠️ No rules found in gdd. Skipping config generation.")
return {"rule_config": {}}
rule_schemas = []
for g in rule_list:
rule_schemas.append({
"name": g.get("name"),
"description": g.get("role", ""),
"config_schema": g.get("config_fmt") or g.get("config_format", {})
})
prompt = ChatPromptTemplate.from_messages([
("system", RULE_CONFIG_PROMPT),
("user", """
[User Intent]
{user_intent}
[Active Rules & Schemas]:
{rule_schemas}
[Initial Unit Placement (Team Reference)]
{unit_placement}
[Map Info]
{map_info}
Based on the schemas above, generate the `customize` configuration for this scenario.
""")
])
chain = prompt | self.client | self.parser
try:
result = chain.invoke({
"user_intent": user_intent,
"rule_schemas": json.dumps(rule_schemas, indent=2),
"unit_placement": json.dumps(unit_config, indent=2),
"map_info": json.dumps(map_data)
})
config_reasoning = result.get("reasoning", "")
if config_reasoning:
self.reasoning_logs.append({
"agent": "script_developer",
"step": "rule_config",
"reasoning": config_reasoning if isinstance(config_reasoning, str) else str(config_reasoning)
})
common.log_node_result("step3_rule_config", result, self.session_timestamp, self.log_dir)
# Wrap rule_config under "normal" key
rule_cfg = result.get("rule_config", None) or result.get("customize", None) or {}
return {"rule_config": {"normal": rule_cfg}}
except Exception as e:
print(f"[Error] Rule Config Generation Failed: {e}")
return {"rule_config": {}}
def get_condition(self, state: ScriptDeveloperState) -> ScriptDeveloperState:
print("\n[Developer] 4. Generating Victory/Defeat Conditions...")
user_intent = state.get("user_intent", {})
gdd = state.get("gdd", {})
map_data = state.get("selected_map", {})
unit_config = state.get("unit_config", {})
rule_config = state.get("rule_config", {})
prompt = ChatPromptTemplate.from_messages([
("system", GET_CONDITION_PROMPT),
("user","""
[User Intent]
{user_intent}
[GDD]
{gdd}
[Rule Configuration]
{rule_config}
[Unit Config]
{unit_config}
[Map Info]
{map_info}
Generate the `end_condition` and descriptions for the scenario.
""")
])
chain = prompt | self.client | self.parser
result = chain.invoke({
"user_intent": user_intent,
"map_info": json.dumps(map_data, ensure_ascii=False),
"gdd": json.dumps(gdd, ensure_ascii=False),
"rule_config": json.dumps(rule_config, ensure_ascii=False),
"unit_config": json.dumps(unit_config, ensure_ascii=False)
})
condition_reasoning = result.get("reasoning", "")
if condition_reasoning:
self.reasoning_logs.append({
"agent": "script_developer",
"step": "end_condition",
"reasoning": condition_reasoning
})
common.log_node_result("step4_conditions", result, self.session_timestamp, self.log_dir)
# Wrap under "normal" key
return {
"end_config": {"normal": result}
}
def assemble_draft(self, state: ScriptDeveloperState) -> Dict:
print("\n[Developer] 5. Finalizing Scenario JSON...")
# 1. Map Data extraction
selected_map_data = state.get("selected_map", {})
map_name = list(selected_map_data.keys())[0] if selected_map_data else "Unknown Map"
# 2. Global Data extraction
unit_config_all = state.get("unit_config", {})
rule_config_all = state.get("rule_config", {})
end_config_all = state.get("end_config", {})
# 3. Prepare Final Output
final_output = {}
difficulties = ["normal"]
for diff in difficulties:
u_conf = unit_config_all.get(diff, {})
g_conf = rule_config_all.get(diff, {})
e_conf = end_config_all.get(diff, {})
# Build 'information' block
info_block = {
"map_name": map_name,
"gdd": state.get("gdd", {}).get("gdd", "Custom Mode"),
"description": e_conf.get("game_description", "No description available."),
"match_format": u_conf.get("match_format", "1v1"),
"decision": state.get("gdd", {}).get("decision", ["GroupMovement"]),
"difficulty": diff,
"fog_of_war": e_conf.get("fog_of_war", True)
}
# Build 'end_condition' block (Step 4 already structured this)
end_cond = e_conf.get("end_condition", {
"victory": "Destroy all enemies",
"defeat": "Commander destroyed"
})
placement = u_conf.get("unit_placement", {})
# g_conf may be the config directly, or wrapped in "customize"/"rule_config" key
if g_conf and isinstance(g_conf, dict):
customize_block = g_conf.get("customize", None) or g_conf.get("rule_config", None) or g_conf
else:
customize_block = {}
scenario_json = {
"information": info_block,
"end_condition": end_cond,
"unit_placement": placement,
"customize": customize_block
}
final_output[diff] = scenario_json
final_result = {
"final_json": final_output
}
common.log_node_result("step5_final_script", final_result, self.session_timestamp, self.log_dir)
return {
"final_json": final_output,
"is_script_valid": False,
"loop_count": 1 # Start at 1 so initial verify + MAX_LOOPS-1 refines = 3 total
}
def run(self, user_intent: str, gdd: Dict, meta_feedback: str = None, step_num: int = None) -> Dict[str, Any]:
self.current_step_num = step_num # Store for use in other methods
print(f"\n[ScriptDev] 🚀 Starting Script Generation Workflow for: '{user_intent}' (Step {step_num})")
# If meta_feedback is provided, enhance the user_intent
enhanced_intent = user_intent
if meta_feedback:
enhanced_intent = f"{user_intent}\n\n[Meta-Feedback from Previous Workflow]\n{meta_feedback}"
print(f"[ScriptDev] 📝 Using meta-feedback to guide script generation")
initial_state = {
"user_intent": enhanced_intent,
"gdd": gdd,
"is_script_valid": False,
"script_feedback": None,
"loop_count": 0,
"final_json": {},
"selected_map": {},
"unit_config": {},
"rule_config": {},
"end_config": {}
}
try:
result_state = self.graph.invoke(initial_state)
final_json = result_state.get("final_json", {})
is_valid = result_state.get("is_script_valid", False)
feedback = result_state.get("script_feedback", None)
if final_json:
output_dir = self.current_dir / "log" / self.session_timestamp / "result"
os.makedirs(output_dir, exist_ok=True)
saved_files = []
for diff, data in final_json.items():
filename = f"scenario_{diff}.json"
file_path = output_dir / filename
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
saved_files.append(str(file_path))
print(f" 💾 Saved: {file_path}")
print("\n[ScriptDev] Script generation completed.")
else:
print(" ❌ Final JSON is empty.")
is_valid = False
feedback = "Script generation resulted in empty JSON."
print(f"[System] Feedback logs saved to: {self.feedback_log_path}")
return {
"final_json": final_json,
"is_valid": is_valid,
"feedback": feedback,
"feedback_history": self.feedback_logs, # Include all feedback history
"reasoning_log": self.reasoning_logs
}
except Exception as e:
print(f" ❌ Critical Error in ScriptDeveloper: {e}")
import traceback
traceback.print_exc()
return {
"final_json": {},
"is_valid": False,
"feedback": f"Critical Exception: {str(e)}"
}
if __name__ == "__main__":
import sys, glob
if not os.getenv("OPENAI_API_KEY"):
print("\nOPENAI_API_KEY is missing. Check .env file.\n")
sys.exit(1)
# Find the latest session or use arg
current_dir = Path(__file__).resolve().parent.parent
if len(sys.argv) > 1:
TEST_SESSION = sys.argv[1]
else:
log_dirs = sorted(Path(current_dir / "log").glob("*_q0"))
if not log_dirs:
print("No session logs found.")
sys.exit(1)
TEST_SESSION = log_dirs[-1].name
print(f"Using latest session: {TEST_SESSION}")
designer_log_dir = current_dir / "log" / TEST_SESSION / "designer"
gdd_files = list(designer_log_dir.glob("gdd_*.json"))
if not gdd_files:
print(f"No gdd files found in {designer_log_dir}")
sys.exit(1)
gdd_file = gdd_files[0]
print(f"Loading gdd from: {gdd_file}")
with open(gdd_file, "r", encoding="utf-8") as f:
mock_gdd = json.load(f)
mock_user_intent = mock_gdd.get("game_description", "")[:200]
if not mock_user_intent:
mock_user_intent = f"Create a {mock_gdd.get('gdd', 'custom')} GDD"
print(f"GDD: {mock_gdd.get('gdd')}")
print(f"Rules: {[g.get('name') for g in mock_gdd.get('rules', [])]}")
print(f"Decision: {mock_gdd.get('decision', [])}")
agent = ScriptDeveloperAgent(session_timestamp=TEST_SESSION)
result = agent.run(user_intent=mock_user_intent, gdd=mock_gdd)
final_script = result.get("final_json", {})
if final_script:
print("\nScript Generated Successfully!")
for diff_key, diff_data in final_script.items():
print(f" {diff_key}: decision={diff_data.get('information',{}).get('decision','N/A')}")
else:
print(f"\nScript Generation Failed: {result.get('feedback', 'No feedback')}")