ALL FOR DX or maybe this could harm dx

This commit is contained in:
lda
2026-05-07 05:36:38 +07:00 Verified
parent b2b8cf1edd
commit d479af7e67
3 changed files with 47 additions and 21 deletions
+15 -2
View File
@@ -237,12 +237,25 @@ class WorkflowBuilder:
self.nodes.append(node) self.nodes.append(node)
return node return node
def connect(self, from_: StepRef, outcome: str, to: StepRef) -> None: def connect(
self,
from_: BranchRef,
outcome: str,
to: BranchRef,
) -> tuple[StepRef, StepRef]:
"""Connect one outcome, auto-using NodeSpec endpoints as fresh node uses."""
source = self.use(from_) if _is_node_spec(from_) else from_
target = self.use(to) if _is_node_spec(to) else to
self.edges.append( self.edges.append(
Edge.model_validate( Edge.model_validate(
{"from": _step_id(from_), "outcome": outcome, "to": _step_id(to)} {
"from": _step_id(cast(StepRef, source)),
"outcome": outcome,
"to": _step_id(cast(StepRef, target)),
}
) )
) )
return cast(StepRef, source), cast(StepRef, target)
def branch( def branch(
self, self,
+13 -19
View File
@@ -160,15 +160,6 @@ class State(
# context: Context # context: Context
gacha = WorkflowBuilder(
name="im not hiding it no more",
input_schema=Input,
output_schema=Storage, # could be State, since the OG doesnt care, ill probably dump out the list.
state_schema=State,
start="init",
)
class Nothing(BaseModel): ... # variance shit IDC class Nothing(BaseModel): ... # variance shit IDC
@@ -410,25 +401,26 @@ def keep_rolling(state: Countdown) -> NodeReturn[Nothing]:
return s("tick") if (state.countdown or 0) > 0 else s(END) return s("tick") if (state.countdown or 0) > 0 else s(END)
# could be @graph.(something combining node and use)... gacha = WorkflowBuilder(
name="im not hiding it no more",
gacha.use(init) input_schema=Input,
gacha.use(tick, id="tick") # itd use main output_schema=Storage, # could be State, since the OG doesnt care, ill probably dump out the list.
state_schema=State,
)
gacha.use(tick, id="tick") # itd use main if we dont have id
counter_up = gacha.use(CounterUp.c1, id="counter_up") # 0 base to 1 base probably counter_up = gacha.use(CounterUp.c1, id="counter_up") # 0 base to 1 base probably
rate_up = gacha.use(RateChange.r65, id="rate_up") rate_up = gacha.use(RateChange.r65, id="rate_up")
rate_same = gacha.use(RateChange.r0, id="rate_same") rate_same = gacha.use(RateChange.r0, id="rate_same")
r_10 = gacha.use(RateChange.r10, id="r_g10") r_10 = gacha.use(RateChange.r10, id="r_g10")
gacha.use(RateChange.r80, id="r_g80") gacha.use(RateChange.r80, id="r_g80")
gacha.use(RateChange.r240, id="r_gs") gacha.use(RateChange.r240, id="r_gs")
prepare_pool = gacha.use(prep)
gacha.use(roll)
c_80 = gacha.use(CounterUp.c80, id="c_80") c_80 = gacha.use(CounterUp.c80, id="c_80")
gacha.use(CounterUp.c10, id="c_10") gacha.use(CounterUp.c10, id="c_10")
gacha.condition( # condition dont ignore id; you can... gacha.condition( # condition dont ignore id; you can...
id="keep_rolling", check=expr(state("countdown")) > 0 id="keep_rolling", check=expr(state("countdown")) > 0
) # replaces keep_rolling ) # replaces keep_rolling
# Outcome is currently hidden from the docs (there is none), outcome_map is insane, should we have it # Outcome is currently hidden from the docs (there is none), outcome_map is insane, should we have it
gacha.connect("init", "ok", "keep_rolling") init_ref, _ = gacha.connect(init, "ok", "keep_rolling")
gacha.connect("keep_rolling", "true", "tick") gacha.connect("keep_rolling", "true", "tick")
gacha.connect("keep_rolling", "false", END) gacha.connect("keep_rolling", "false", END)
@@ -448,11 +440,11 @@ preroll_routes = gacha.branch(
"240": "r_gs", "240": "r_gs",
"80": "r_g80", "80": "r_g80",
"10": r_10, "10": r_10,
"1": "prepare_pool", "1": prep,
}, },
) )
print(preroll_routes) prepare_pool = preroll_routes["1"]
gacha.connect(prepare_pool, "ok", "roll") _, roll_ref = gacha.connect(prepare_pool, "ok", roll)
gacha.connect("r_gs", "ok", "prepare_pool") gacha.connect("r_gs", "ok", "prepare_pool")
gacha.connect(preroll_routes["80"], "ok", prepare_pool) gacha.connect(preroll_routes["80"], "ok", prepare_pool)
gacha.connect("r_g10", "ok", prepare_pool) gacha.connect("r_g10", "ok", prepare_pool)
@@ -475,6 +467,7 @@ gacha.branch(
gacha.connect(c_80, "ok", "keep_rolling") gacha.connect(c_80, "ok", "keep_rolling")
gacha.connect("c_10", "ok", "keep_rolling") gacha.connect("c_10", "ok", "keep_rolling")
gacha.set_entry_point(init_ref)
# there is like no general uses for the nodes; idk tho # there is like no general uses for the nodes; idk tho
@@ -570,6 +563,7 @@ def execute(graph: WorkflowBuilder, input: Input):
def test(): def test():
assert 240 - 135 + 20 >= 120, "my math!"
d = execute( d = execute(
gacha, gacha,
build_input(context)( build_input(context)(
+19
View File
@@ -303,3 +303,22 @@ def test_builder_can_auto_id_condition_foreach_and_interrupt() -> None:
assert second_condition.id == "condition_2" assert second_condition.id == "condition_2"
assert foreach.id == "foreach_tag" assert foreach.id == "foreach_tag"
assert interrupt.id == "interrupt_approval" assert interrupt.id == "interrupt_approval"
def test_builder_connect_can_use_node_specs_and_returns_resolved_refs() -> None:
builder = WorkflowBuilder(
name="connect_specs_demo",
input_schema=AutoBindInput,
state_schema=AutoBindState,
output_schema=AutoBindOutput,
)
source, target = builder.connect(auto_bind_node, "ok", auto_bind_node)
assert not isinstance(source, str)
assert not isinstance(target, str)
assert source.id == "test_auto_bind"
assert target.id == "test_auto_bind_2"
assert builder.edges[0].from_ == "test_auto_bind"
assert builder.edges[0].outcome == "ok"
assert builder.edges[0].to == "test_auto_bind_2"