Skip to content

Commit 2ca3ee7

Browse files
committed
refactor(writer): remove trigger_type parameter from run_blueprint_via_api and streamline trigger selection logic - AB-881
1 parent 1fe0e42 commit 2ca3ee7

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

src/writer/blueprints.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,13 @@ def _gather_cron_blueprints(self):
232232
def run_blueprint_via_api(
233233
self,
234234
blueprint_id: str,
235-
trigger_type: Literal["API", "Cron"],
236235
branch_id: Optional[str] = None,
237236
execution_environment: Optional[Dict[str, Any]] = None
238237
):
239238
"""
240239
Executes a blueprint by its key via the API.
241240
242241
:param blueprint_id: The blueprint identifier.
243-
:param trigger_type: The type of trigger ("API" or "Cron").
244242
:param branch_id: Optional branch ID to start execution from.
245243
:param execution_environment: The execution environment for
246244
the blueprint.
@@ -250,13 +248,24 @@ def run_blueprint_via_api(
250248
execution_environment = {}
251249

252250
trigger_id = branch_id
253-
if trigger_id is None:
254-
if trigger_type == "Cron" or not self.is_blueprint_api_available(blueprint_id):
255-
trigger_id = self.get_blueprint_cron_trigger(blueprint_id)
251+
if trigger_id is not None:
252+
# Determine trigger type from the component
253+
component = self.session.session_component_tree.get_component(trigger_id)
254+
if component and component.type == "blueprints_apitrigger":
255+
trigger_type = "API"
256+
elif component and component.type == "blueprints_crontrigger":
256257
trigger_type = "Cron"
257258
else:
258-
trigger_id = self.get_blueprint_api_trigger(blueprint_id)
259-
259+
trigger_type = "Branch"
260+
elif self.is_blueprint_api_available(blueprint_id):
261+
# Prioritize API trigger over Cron if both exist
262+
trigger_id = self.get_blueprint_api_trigger(blueprint_id)
263+
trigger_type = "API"
264+
elif self.is_blueprint_cron_available(blueprint_id):
265+
trigger_id = self.get_blueprint_cron_trigger(blueprint_id)
266+
trigger_type = "Cron"
267+
else:
268+
raise ValueError(f'No trigger found for blueprint "{blueprint_id}".')
260269
return self.run_branch(
261270
trigger_id,
262271
None,

src/writer/core.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,15 +1283,11 @@ def run_blueprint_via_api(
12831283
blueprint_id = payload.pop("blueprint_id", None)
12841284
if not blueprint_id:
12851285
raise ValueError("Missing blueprint_id in payload")
1286-
trigger_type = payload.pop("trigger_type", None)
1287-
if not trigger_type:
1288-
raise ValueError("Missing trigger_type in payload")
12891286
execution_environment = EventHandler._get_blueprint_execution_environment(
12901287
payload, context, session, vault
12911288
)
12921289
return blueprint_runner.run_blueprint_via_api(
12931290
blueprint_id=blueprint_id,
1294-
trigger_type=trigger_type,
12951291
branch_id=payload.pop("branch_id", None),
12961292
execution_environment=execution_environment,
12971293
)

src/writer/serve.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -539,39 +539,21 @@ async def event_logic(queue: asyncio.Queue):
539539

540540
await queue.put(await format_event("status", {"status": "executing", "msg": (f"Executing branch: {branch_id}..." if branch_id else f"Executing blueprint: {blueprint_id}...")}))
541541

542-
if branch_id:
543-
task = asyncio.create_task(
544-
app_runner.handle_event(
545-
session_id,
546-
WriterEvent(
547-
type="wf-run-blueprint-via-api",
548-
isSafe=True,
549-
handler="run_blueprint_via_api",
550-
payload={
551-
"blueprint_id": blueprint_id,
552-
"trigger_type": "Cron",
553-
"branch_id": branch_id,
554-
**(payload or {})
555-
},
556-
)
557-
)
558-
)
559-
else:
560-
task = asyncio.create_task(
561-
app_runner.handle_event(
562-
session_id,
563-
WriterEvent(
564-
type="wf-run-blueprint-via-api",
565-
isSafe=True,
566-
handler="run_blueprint_via_api",
567-
payload={
568-
"blueprint_id": blueprint_id,
569-
"trigger_type": "API",
570-
**(payload or {})
571-
},
572-
)
542+
task = asyncio.create_task(
543+
app_runner.handle_event(
544+
session_id,
545+
WriterEvent(
546+
type="wf-run-blueprint-via-api",
547+
isSafe=True,
548+
handler="run_blueprint_via_api",
549+
payload={
550+
"blueprint_id": blueprint_id,
551+
"branch_id": branch_id,
552+
**(payload or {})
553+
},
573554
)
574555
)
556+
)
575557

576558
await queue.put(await format_event("status", {"status": "running", "msg": ("Branch is running. Awaiting output..." if branch_id else "Blueprint is running. Awaiting output...")}))
577559

0 commit comments

Comments
 (0)