we een have builtin types for you to use

This commit is contained in:
lda
2026-05-06 22:12:55 +07:00 Verified
parent 05a0db63c8
commit f15f5289ef
3 changed files with 171 additions and 1 deletions
+18
View File
@@ -3,12 +3,21 @@ from .catalog import NodeCatalog, NodeCatalogEntry
from .conditions import context, exists, expr, input, state
from .mapping import bind_fields, bind_state, merge_maps
from .ops import (
BoolOutput,
CoalesceInput,
CountOutput,
ItemOutput,
MaybeItemOutput,
SequenceInput,
ValueOutput,
coalesce,
first_item,
first_item_maybe,
first_item_or_none,
is_empty,
last_item,
last_item_or_none,
length,
)
from .paths import GraphPath, context_path, graph_path, input_path, state_path
from .spec import (
@@ -25,6 +34,9 @@ from .subgraph import subgraph_node
__all__ = [
"NodeCatalog",
"NodeCatalogEntry",
"BoolOutput",
"CoalesceInput",
"CountOutput",
"GraphPath",
"ItemOutput",
"MaybeItemOutput",
@@ -33,11 +45,13 @@ __all__ = [
"AsyncRegistryHandler",
"SyncRegistryHandler",
"SequenceInput",
"ValueOutput",
"WorkflowBuilder",
"bind_fields",
"build_async_registry",
"build_registry",
"bind_state",
"coalesce",
"merge_maps",
"context",
"context_path",
@@ -49,6 +63,10 @@ __all__ = [
"graph_path",
"input",
"input_path",
"is_empty",
"last_item",
"last_item_or_none",
"length",
"node",
"state",
"state_path",
+69
View File
@@ -19,6 +19,23 @@ class MaybeItemOutput(BaseModel):
item: Any | None = None
class CountOutput(BaseModel):
count: int
class BoolOutput(BaseModel):
value: bool
class CoalesceInput(BaseModel):
value: Any | None = None
fallback: Any
class ValueOutput(BaseModel):
value: Any
@node(
name="authoring.first_item",
input_model=SequenceInput,
@@ -54,3 +71,55 @@ def first_item_maybe(input: SequenceInput) -> NodeReturn[MaybeItemOutput]:
if not input.items:
return NodeReturn(outcome="missing", output=MaybeItemOutput())
return NodeReturn(outcome="found", output=MaybeItemOutput(item=input.items[0]))
@node(
name="authoring.last_item",
input_model=SequenceInput,
output_model=ItemOutput,
description="Select the last item from a non-empty sequence.",
)
def last_item(input: SequenceInput) -> ItemOutput:
if not input.items:
raise ValueError("last_item requires at least one item")
return ItemOutput(item=input.items[-1])
@node(
name="authoring.last_item_or_none",
input_model=SequenceInput,
output_model=ItemOutput,
description="Select the last item from a sequence, or None when it is empty.",
)
def last_item_or_none(input: SequenceInput) -> ItemOutput:
return ItemOutput(item=input.items[-1] if input.items else None)
@node(
name="authoring.length",
input_model=SequenceInput,
output_model=CountOutput,
description="Count the items in a sequence.",
)
def length(input: SequenceInput) -> CountOutput:
return CountOutput(count=len(input.items))
@node(
name="authoring.is_empty",
input_model=SequenceInput,
output_model=BoolOutput,
description="Return whether a sequence is empty.",
)
def is_empty(input: SequenceInput) -> BoolOutput:
return BoolOutput(value=not input.items)
@node(
name="authoring.coalesce",
input_model=CoalesceInput,
output_model=ValueOutput,
description="Return value when it is not None, otherwise return fallback.",
)
def coalesce(input: CoalesceInput) -> ValueOutput:
return ValueOutput(value=input.value if input.value is not None else input.fallback)