-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipeline.py
More file actions
224 lines (187 loc) · 8.04 KB
/
Copy pathpipeline.py
File metadata and controls
224 lines (187 loc) · 8.04 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
"""
Simplified pipeline: DB match -> script.
user query
-> (1) find the most similar scenario in db/scenario (DBCall, LLM match)
-> (2) build a gdd from that existing scenario (load_existing_mode)
-> (3) generate the final script (ScriptDeveloperAgent)
No new GDD generation, no rule development (existing validated rules only),
no analyst / verify loop.
"""
import os
import re
import json
import shutil
import datetime
from pathlib import Path
from dotenv import load_dotenv
from db_call import DBCall
from script_builder import ScriptDeveloperAgent
load_dotenv()
CURRENT_DIR = Path(__file__).resolve().parent
DB_DIR = CURRENT_DIR / "db"
# --------------------------------------------------------------------------- #
# (1) Find the most similar existing scenario in the DB
# --------------------------------------------------------------------------- #
def _scenario_details() -> dict:
try:
with open(DB_DIR / "scenario" / "meta.json", "r", encoding="utf-8") as f:
return json.load(f).get("details", {})
except Exception:
return {}
def _keyword_fallback(query: str, details: dict) -> str:
"""Pick the scenario whose name+description shares the most words with the
query. No LLM — used when the DBCall match is empty or errors out."""
q = set(re.findall(r"[a-z0-9가-힣]+", query.lower()))
best, best_score = None, -1
for name, info in details.items():
text = f"{name} {info.get('description', '')}".lower()
score = len(q & set(re.findall(r"[a-z0-9가-힣]+", text)))
if score > best_score:
best, best_score = name, score
return best if best_score > 0 else None
def find_scenario(query: str, seed: int = None) -> str:
"""Return the best-matching scenario name from db/scenario.
Resilient: LLM match → keyword fallback → default to the first scenario.
Returns None only when the scenario DB is empty/unreadable.
"""
details = _scenario_details()
names = []
try:
db = DBCall(seed=seed)
_, names = db.call_with_names(query, folder="scenario")
except Exception as e:
print(f" ⚠️ DBCall match failed ({e}); falling back.")
if names:
print(f" 🔎 Matched scenario(s): {names} -> using '{names[0]}'")
return names[0]
fb = _keyword_fallback(query, details)
if fb:
print(f" ↩️ Keyword fallback -> '{fb}'")
return fb
if details:
first = next(iter(details))
print(f" ↩️ No overlap; defaulting to first scenario -> '{first}'")
return first
print(" ⚠️ Scenario DB is empty.")
return None
# --------------------------------------------------------------------------- #
# (2) Build a gdd from an existing DB scenario (existing rules only)
# --------------------------------------------------------------------------- #
def _normalize_name(name: str) -> str:
name = re.sub(r'\s*\(.*?\)\s*', '', name)
name = name.lower().strip()
name = re.sub(r'[-_ ]+', '_', name)
return name
def _resolve_db_name(llm_name: str, candidates: list) -> str:
if not llm_name or not candidates:
return llm_name
if llm_name in candidates:
return llm_name
norm = _normalize_name(llm_name)
for key in candidates:
if _normalize_name(key) == norm:
return key
for key in candidates:
nk = _normalize_name(key)
if nk in norm or norm in nk:
return key
return llm_name
def load_existing_mode(gdd_name: str, session_timestamp: str) -> dict:
"""Load an existing scenario from db/scenario and build a gdd dict using
only existing, validated rules referenced by the scenario."""
scenario_meta_path = DB_DIR / "scenario" / "meta.json"
try:
with open(scenario_meta_path, "r", encoding="utf-8") as f:
scenario_meta = json.load(f)
except Exception as e:
print(f" ⚠️ Failed to load scenario meta: {e}")
return {}
details = scenario_meta.get("details", {})
if gdd_name not in details:
resolved = _resolve_db_name(gdd_name, list(details.keys()))
if resolved != gdd_name and resolved in details:
print(f" 🔗 Fuzzy-matched '{gdd_name}' -> '{resolved}'")
gdd_name = resolved
else:
print(f" ⚠️ Scenario '{gdd_name}' not found in DB.")
return {}
scenario_path = DB_DIR / details[gdd_name].get("file", "")
try:
with open(scenario_path, "r", encoding="utf-8") as f:
scenario_data = json.load(f)
except Exception as e:
print(f" ⚠️ Failed to load scenario file: {e}")
return {}
rules_temp = CURRENT_DIR / "log" / session_timestamp / "developer" / "rules_temp"
os.makedirs(rules_temp, exist_ok=True)
db_codes_dir = DB_DIR / "rule" / "codes"
rules = []
for rule_ref in scenario_data.get("rule", []):
rule_path = DB_DIR / rule_ref
try:
with open(rule_path, "r", encoding="utf-8") as f:
rule_data = json.load(f)
code_info = rule_data.get("code", {})
rule_name = code_info.get("name", "")
rules.append({
"action": "existing",
"name": rule_name,
"logic_description": code_info.get("description", ""),
"config_format": code_info.get("config_format", {}),
"config_example": code_info.get("config_example", {}),
"validated": True
})
src_lua = db_codes_dir / f"{rule_name}.lua"
dst_lua = rules_temp / f"{rule_name}.lua"
if src_lua.exists() and not dst_lua.exists():
shutil.copy2(str(src_lua), str(dst_lua))
except Exception as e:
print(f" ⚠️ Failed to load rule {rule_ref}: {e}")
gdd = {
"gdd": gdd_name,
"game_description": scenario_data.get("specification", ""),
"decision": scenario_data.get("decision", []),
"rules": rules,
"reasoning": f"Loaded existing mode '{gdd_name}' from DB.",
"assessing_ability": ""
}
print(f" 📦 Loaded existing mode: {gdd_name} ({len(rules)} existing rule(s))")
return gdd
# --------------------------------------------------------------------------- #
# (3) + orchestration
# --------------------------------------------------------------------------- #
def generate(query: str, seed: int = None) -> dict:
"""Run the full simplified pipeline and return:
{
"scenario": <matched scenario name>,
"config": <the generated scenario config>, # normal difficulty
"raw": <full developer result>
}
"""
session_timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
print(f"\n{'='*60}\n SIMPLIFIED PIPELINE (DB match -> script)\n Query: {query}\n{'='*60}")
print("\n[1/3] Searching DB for a similar scenario...")
scenario_name = find_scenario(query, seed=seed)
if not scenario_name:
return {"error": "no_matching_scenario", "config": None}
print("\n[2/3] Building gdd from existing scenario...")
gdd = load_existing_mode(scenario_name, session_timestamp)
if not gdd:
return {"error": "failed_to_load_scenario", "config": None}
print("\n[3/3] Generating script from gdd...")
developer = ScriptDeveloperAgent(session_timestamp=session_timestamp, seed=seed)
result = developer.run(user_intent=query, gdd=gdd)
final_json = result.get("final_json", {}) or {}
# difficulties default to ["normal"]; take the first available config.
config = None
if final_json:
config = final_json.get("normal") or next(iter(final_json.values()), None)
return {"scenario": scenario_name, "config": config, "raw": result}
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print('Usage: python pipeline.py "<game mode description>"')
sys.exit(1)
out = generate(sys.argv[1])
print("\n" + "=" * 60 + "\n RESULT\n" + "=" * 60)
print(json.dumps(out.get("config"), ensure_ascii=False, indent=2))