node

fun node(name: String, action: NodeAction<State>): NodeRef

Adds a node named name that runs action and returns a reference to it for use with then.

Throws

if the name is blank, reserved (START, END), holds a / or is already used.


fun node(name: String, retry: Retry? = null, timeout: Duration? = null, action: NodeAction<State>): NodeRef

Adds a node that is tried again when it fails, that has a time limit, or both.

val fetch = node("fetch", retry = Retry(attempts = 3), timeout = 30.seconds) { order ->
order.copy(stock = warehouse.stock(order.item))
}

With a retry, a node that fails runs again on the same state, in the same step: see Retry for how often and with which pause. Only when its last try fails does the run fail, with a NodeExecutionException. Nothing is saved between two tries, and the other nodes of the step go on while this one waits for its next try. A stream gets a GraphEvent.NodeRetry for each try that failed.

With a timeout, a try that takes longer is cancelled and fails with a NodeTimeoutException, which a retry treats like any other failure. The limit stops code that suspends, such as a call with a Ktor client. Code that blocks its thread goes on until it returns, and the node fails then.

A node that is tried again starts from its first line, so make its side effects safe to repeat.

Parameters

retry

how the node is tried again after a failure. null runs it once.

timeout

how long one try may take. null is no limit.

Throws

if the name is blank, reserved (START, END), holds a / or is already used, or timeout is not positive.


fun <Result> node(name: String, work: suspend (State) -> Result, update: suspend (State, Result) -> State): NodeRef

Adds a node whose job is split in two, so that it can run next to other nodes without a Reducer. work does the slow part, such as a call to a model or a service, and returns a result. update writes that result into the state.

val web = node("web", work = { searchWeb(it.question) }) { state, found ->
state.copy(findings = state.findings + found)
}

When several nodes run in the same step, their work runs in parallel on the same state. Their updates are then applied one after another, in the order the nodes were added to the graph, each to the state that the previous one produced. No change is lost, and if two nodes write the same property, the node that was added later wins. A graph compiled with mergeRules, given as rules or as reducer, works differently: each update gets the state from before the step, and the rules merge the results (see compile).

update must only build the new state. The engine may call it more than once for one run of the node.

Throws

if the name is blank, reserved (START, END), holds a / or is already used.


fun <Result> node(name: String, work: suspend (State) -> Result, retry: Retry? = null, timeout: Duration? = null, update: suspend (State, Result) -> State): NodeRef

Adds a node with a work and an update that is tried again when it fails, that has a time limit, or both. See the overload of a node that returns a whole state for what retry and timeout do.

val classify = node(
"classify",
work = { email -> model.ask<Verdict>("Classify this email: ${email.body}") },
retry = Retry(attempts = 3),
) { email, verdict -> email.copy(category = verdict.category) }

A try is the work of the node. In a step with several nodes, the update runs once, after the work of every node of the step has succeeded.

Parameters

retry

how the node is tried again after a failure. null runs it once.

timeout

how long one try may take. null is no limit.

Throws

if the name is blank, reserved (START, END), holds a / or is already used, or timeout is not positive.


fun <Item, Result> node(name: String, forEach: suspend (State) -> List<Item>, work: suspend (State, Item) -> Result, serializer: StateSerializer<Result>? = null, concurrency: Int = Int.MAX_VALUE, retry: Retry? = null, timeout: Duration? = null, update: suspend (State, List<Result>) -> State): NodeRef

Adds a node that runs its work once for each item of a list, all at the same time, when the number of items is only known at run time: summarize every document a search found, or ask a model about each line of an order. forEach reads the list from the state, and update writes the results into the state, in the order of the items.

val summarize = node(
"summarize",
forEach = { search -> search.documents },
work = { search, document -> model.ask<Summary>("Summarize: ${document.text}") },
serializer = KotlinxStateSerializer(),
) { search, summaries -> search.copy(summaries = summaries) }

Every item gets the state from before the step, and update runs once, after the work of every item has succeeded, with an empty list when forEach returns none. To the rest of the graph it is a node with a work and an update: it can run next to other nodes without a Reducer, and GraphConfig.interruptBefore and GraphConfig.interruptAfter name the whole node.

What to know:

  • Each item is tried on its own. retry and timeout hold for one item: an item that fails is tried again while the others go on. When its last try fails, the run fails with a NodeExecutionException whose item is the position of the item, and the items still running are cancelled.

  • With a serializer, a finished item is not run again. Its result is saved in the checkpoint as soon as it is there, as an effect is. When the step runs again, after a failure, a crash or an interrupt from the work of an item, only the items without a result run. The results belong to the positions in the list, so a CompiledGraph.resume that changes the list should not keep them: change the list in a later step instead. Without a serializer, every item runs again, as any node does. With telar-serialization, KotlinxStateSerializer() stores a @Serializable result.

  • An effect in work belongs to its item, so effect("charge") { ... } runs once for each item without a key of its own for each.

  • A stream shows the items. The node has one GraphEvent.NodeStarted and one GraphEvent.NodeCompleted, and a GraphEvent.ItemCompleted for each item, in between. What an item passes to reportProgress and the retries of an item say which item it is.

  • concurrency limits how many items run at the same time. There is no limit by default, so 500 items are 500 calls to a model at once: set one for a service with a rate limit.

forEach, work and update must only read and build. The engine may call update more than once for one run of the node.

Parameters

forEach

reads the items from the state.

work

does the work of one item, with the state of the node.

serializer

stores the result of an item as a string and reads it back. null saves no results.

concurrency

how many items may run at the same time.

retry

how an item is tried again after a failure. null runs it once.

timeout

how long one try of an item may take. null is no limit.

Throws

if the name is blank, reserved (START, END), holds a / or is already used, concurrency is not positive, or timeout is not positive.