-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.py
More file actions
237 lines (191 loc) · 9.56 KB
/
Copy pathorchestrator.py
File metadata and controls
237 lines (191 loc) · 9.56 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
import logging
import time
import json
import os
import requests
from typing import Dict, Any, Optional
from urllib.parse import urlparse
from world_client import WorldLabsClient
import semantic_analyzer
logger = logging.getLogger("BloomPath.Orchestrator")
class BloomPathOrchestrator:
"""
Manages the Project World Model (PWM) loop:
Intent -> Spatial Synthesis (Marble) -> Semantic Analysis (Gemini) -> UE5
"""
def __init__(self):
self.world_client = WorldLabsClient()
self.mechanics_map = self._load_mechanics_config()
def _load_mechanics_config(self) -> Dict[str, str]:
"""Load mechanics mapping from JSON config."""
try:
config_path = os.path.join(os.getcwd(), "config", "mechanics.json")
if os.path.exists(config_path):
with open(config_path, 'r') as f:
return json.load(f)
except Exception as e:
logger.warning(f"Failed to load mechanics config: {e}")
return {}
def parse_intent(self, ticket: Any) -> Dict[str, Any]:
"""Extracts generation prompt and mechanics from UnifiedTicket."""
summary = getattr(ticket, 'title', '')
labels = getattr(ticket, 'labels', [])
prompt = f"A 3D game level: {summary}"
mechanics_list = ["Standard movement, jump, walk"]
label_set = {str(l).lower() for l in labels}
for key, value in self.mechanics_map.items():
if key in label_set:
mechanics_list.append(value)
if mechanics_list:
prompt += f" Support mechanics: {', '.join(mechanics_list)}."
return {
"prompt": prompt,
"mechanics": ", ".join(mechanics_list),
"issue_key": getattr(ticket, 'id', 'UNKNOWN'),
"attachments": getattr(ticket, 'attachments', []),
"is_ue5_interactive": "ue5" in label_set,
}
def process_ticket(self, ticket: Any) -> Dict[str, Any]:
"""
Main entry point: Process a UnifiedTicket through the pipeline.
Intent -> Spatial Synthesis (Marble) -> Semantic Analysis (Gemini) -> UE5
"""
start_time = time.time()
intent = self.parse_intent(ticket)
issue_key = intent['issue_key']
is_ue5_interactive = intent.get('is_ue5_interactive', False)
logger.info(f"🎹 Orchestrator (Direct Mode) for {issue_key}: {intent['prompt']}")
current_prompt = intent['prompt']
attachments = intent.get('attachments', [])
video_attachment = None
# Look for the first video attachment
for att in attachments:
url = att.get("url", "")
title = att.get("title", "")
# Check both URL and Title (Linear upload URLs don't always have extensions)
target_strings = [url.lower(), title.lower()]
if any(ext in text for text in target_strings for ext in [".mp4", ".mov", ".webm"]):
video_attachment = att
break
# 1. Spatial Synthesis OR Interactive Placement
output_filename = f"{issue_key}_{int(time.time())}.gltf"
output_path = os.path.join(os.getcwd(), "content", "generated", output_filename)
mesh_path = None
manifest = None
if is_ue5_interactive:
# OPTION A: UE5 Interactive placement workflow
logger.info(" > UE5 Label detected. Executing Procedural Interactive Placement.")
# Use intent title as the item description/class
item_description = getattr(ticket, 'title', 'UnknownItem')
# Ideally, we look up the latest generated manifest to find a surface.
# In a full system, you'd track the current loaded manifest in state.
# For now, we simulate finding a surface target.
semantic_target = "CounterTop"
try:
from ue5_interface import trigger_ue5_spawn_interactive
trigger_ue5_spawn_interactive(item_description, semantic_target)
logger.info(f" > Spawned Interactive Blueprint '{item_description}' at '{semantic_target}'")
except ImportError:
logger.error(" > trigger_ue5_spawn_interactive not found in ue5_interface.py")
except Exception as e:
logger.error(f" > Failed to spawn interactive item: {e}")
else:
# OPTION B: Standard World Labs Mesh Generation workflow
if video_attachment:
video_url = video_attachment["url"]
logger.info(f" > Found Video Attachment: {video_url}")
# Download the video locally first
temp_video_path = os.path.join(os.getcwd(), "content", "generated", f"temp_{issue_key}.mp4")
try:
logger.info(f" > Downloading Video from Linear...")
v_resp = requests.get(video_url, stream=True, timeout=60)
v_resp.raise_for_status()
with open(temp_video_path, 'wb') as f:
for chunk in v_resp.iter_content(chunk_size=8192):
f.write(chunk)
logger.info(" > Generating World via Video Prompt...")
generation_result = self.world_client.generate_world_from_video(
video_path=temp_video_path,
text_prompt=current_prompt,
output_path=output_path
)
# Cleanup the temp downloaded video
if os.path.exists(temp_video_path):
os.remove(temp_video_path)
except Exception as e:
logger.error(f"Failed to process video attachment: {e}")
logger.info(" > Falling back to text-only generation...")
generation_result = self.world_client.generate_world(current_prompt, output_path)
else:
logger.info(" > Generating World via Text Prompt...")
generation_result = self.world_client.generate_world(current_prompt, output_path)
if not generation_result:
logger.error(" > World Generation Failed.")
return {"status": "error", "reason": "World Generation Failed"}
mesh_path = generation_result.get('mesh_path')
image_path = generation_result.get('image_path')
# 2. Semantic Tagging & UE5 Injection
logger.info(" > Running Semantic Tagging...")
if image_path:
manifest = semantic_analyzer.analyze_world(image_path)
if manifest:
manifest_path = output_path.replace(".gltf", "_manifest.json")
semantic_analyzer.save_manifest(manifest, manifest_path)
self._inject_tags_into_ue5(manifest)
# 3. Trigger Physical Load in UE5
if mesh_path and os.path.exists(mesh_path):
logger.info(" > Commanding UE5 to load the generated world...")
try:
from ue5_interface import trigger_ue5_load_level
trigger_ue5_load_level(mesh_path)
except Exception as e:
logger.error(f"Failed to trigger UE5 load: {e}")
elapsed = time.time() - start_time
return {
"status": "success",
"mesh_path": mesh_path,
"manifest": manifest,
"duration": elapsed
}
def _inject_tags_into_ue5(self, manifest: Dict[str, Any]):
"""Injects tags from an analyzed manifest into UE5 via Remote Control."""
from ue5_interface import trigger_ue5_set_tag, map_semantic_type_to_actor
objects = manifest.get("objects", [])
if not objects:
return
for obj in objects:
semantic_type = obj.get("semantic_type", "").lower()
tags = obj.get("tags", [])
target_actor = map_semantic_type_to_actor(semantic_type)
if target_actor:
for tag in tags:
try:
trigger_ue5_set_tag(target_actor, tag)
logger.info(f"Injected tag '{tag}' to actor '{target_actor}'")
except Exception as e:
logger.error(f"Failed to inject tag: {e}")
def dream_scenario(
self,
scenario_type: str,
sprint_data: dict,
params: dict = None,
visualize: bool = True
) -> dict:
"""
Run a what-if simulation through the Dreaming Engine.
Args:
scenario_type: 'resource_stress', 'scope_creep', or 'priority_shift'
sprint_data: Current sprint state dict
params: Optional overrides for scenario defaults
visualize: If True, send ghost overlay to UE5
Returns:
DreamResult as dict with projected outcomes
"""
from dreaming_engine import dreaming_engine
from dataclasses import asdict
logger.info(f"🌙 Orchestrator: Dreaming '{scenario_type}'...")
result = dreaming_engine.dream(scenario_type, sprint_data, params)
if visualize:
viz = dreaming_engine.visualize_dream(result)
logger.info(f"🌙 Ghost visualization: {viz}")
return asdict(result)