Use the flow builder to connect SDK requests, application tasks, and decisions on Node. This page explains how the operations compose and what their examples return. The API glossary has the full parameter and return tables. For your first definition, start with Flows from scratch.
defineJevFlow(name, build)#
Import from jev-devtools/flow in Node code. defineJevFlow<Input>() accepts a name and a synchronous builder callback, and returns a definition with run(input, options). The callback returns flow.output() or a terminal flow.branch().
flow.jev() creates future-answer references synchronously. The await triage.run(...) call inside routeMessage() performs the request and waits for the outcome. Call await routeMessage("I was charged twice.") from your Node handler.
import { choice } from "@typesafe-ai/sdk";
import { defineJevFlow } from "jev-devtools/flow";
import { inspector, jev } from "./server.js";
// 1. Define the steps; no SDK request is sent here.
export const triage = defineJevFlow<{ message: string }>("triage", (flow) => {
const classification = flow.jev("classify", {
state: flow.input,
questions: {
team: choice("Which team can help?", {
billing: "Charges and payments",
other: "Everything else",
}),
},
});
return flow.branch("route", {
when: classification.answer("team").choice.eq("billing"),
then: (path) => path.output("billing-team"),
else: (path) => path.output("support-team"),
});
});
// 2. Call routeMessage("I was charged twice.") from your Node handler.
export async function routeMessage(message: string) {
const result = await triage.run({ message }, { client: jev, devtools: inspector });
console.log(result.outcome); // "billing-team" or "support-team", chosen by the flow
return result;
}Describe a flow once, then run it with different inputs. Defining it does not send requests.
defineJevFlow<Input>(name, build)- name
- The flow name shown in DevTools.
- build
- Receives a builder. Return an output or a terminal branch.
Define a Jev request step synchronously. Returns future-answer references, not a promise. The request runs and is awaited when you call program.run().
flow.jev(id, { state, questions }, display?)- id
- A unique step ID within this flow.
- state
- Context sent to Jev for this request. Flow references are resolved when the step runs.
- questions
- Named questions to ask. Their keys identify the returned answers.
- display?
- Optional labels and display metadata for DevTools.
Context sent to Jev for this request. Flow references are resolved when the step runs.
stateA reference to the data passed to run(). Inside map/loop programs, it refers to the item or iteration input instead.
flow.input: FlowExpression<Input>Named questions to ask. Their keys identify the returned answers.
questionsFinish the flow by choosing one of two paths. Each path must return an output or another branch.
flow.branch(id, { when, then, else }, display?)- id
- A unique step ID within this flow.
- when
- A flow condition evaluated when the branch runs.
- then
- The path used when the condition is true.
- else
- The path used when the condition is false.
- display?
- Optional labels and display metadata for DevTools.
A flow condition evaluated when the branch runs.
whenReference the result of a named question. The name must match a key in questions; answer fields can be used in later steps.
request.answer(name)A reference to the label selected by a Choice question. Its allowed labels come from that question’s alternatives.
choice: FlowExpression<Label>Build an equality condition for strings, numbers, booleans, or null. TypeScript rejects non-scalar receivers; execution checks matching types and finite numbers.
expression.eq(value)The path used when the condition is true.
thenFinish this path with a named outcome and, optionally, a value for your application.
flow.output(name, value?, display?)- name
- The outcome string returned by run().
- value?
- Optional result data. Narrow its type in your application before using it.
- display?
- Optional labels and display metadata for DevTools.
The path used when the condition is false.
elseStart the flow with new input and await its steps, including SDK requests. Use await to read { runId, outcome, value? }.
program.run(input, { client, devtools?, signal? })- input
- Data matching the input type declared in defineJevFlow.
- client
- The TypeSafe-compatible client that executes Jev requests.
- devtools?
- An optional observer, such as the shared server adapter, that records execution.
- signal?
- An optional AbortSignal to cancel the run.
The TypeSafe-compatible client that executes Jev requests.
clientAn optional observer, such as the shared server adapter, that records execution.
devtoolsAPI reference: defineJevFlow() · flow.jev() · flow.input · flow.branch() · answer.choice · expression.eq() · flow.output() · program.run()
classify asks Jev which team should handle the message. The branch compares the returned Choice label with "billing" and returns one team name. routeMessage() supplies the message, instrumented client, and shared inspector, then awaits the outcome. The selected team depends on the SDK response.
Definition and run interfaces#
| Value / member | Type | Description |
|---|---|---|
triage.definition |
FlowDefinition |
The static recipe graph. |
triage.run(input, options) |
Promise<FlowOutcome & { runId: string }> |
Executes the recipe and resolves to the fields below. |
runId |
string |
Unique execution ID. |
outcome |
string |
"billing-team" or "support-team" for this example. |
value? |
unknown |
Optional output data; omitted by this example. |
run accepts a required client with systemOne, optional devtools: FlowObserver, and optional signal: AbortSignal. It resolves to { runId, outcome, value? }. definition exposes the static graph for inspection. Definitions make no SDK calls.
Requests and function steps#
| Method | Arguments and result |
|---|---|
jev(id, request, display?) |
A regular SDK request whose state can contain expressions. Returns .answer(name) field references and .answers. |
jev(id, { input, request }, display?) |
Resolve input, then synchronously construct an SDK request in the factory. Use a preceding task for async work. |
task(id, { input, run, timeoutMs? }, display?) |
Resolve nested input references and execute application code. Returns a typed expression for the result. |
run on a task receives (resolvedInput, context). TaskContext contains signal, runId, stepId, and executionId. Respond to the signal for cooperative cancellation. Timeouts cannot undo side effects or forcibly stop non-cooperating code. Tasks do not retry automatically.
display is optional static { title?, description? } metadata. Keep it public-safe. Use .answers when question keys are generated dynamically; individual .answer(name) references require the named question to exist at execution.
Return interfaces#
| Method / member | Type | Description |
|---|---|---|
jev(...).answer(name) |
Object of typed field expressions | References to the selected question's SDK result fields. |
jev(...).answers |
FlowExpression of the answer object |
Reference to all answers. Both Jev overloads return this interface. |
task(...) |
FlowExpression<Awaited<O>> |
Reference to the application callback's awaited result. |
Decisions and outcomes#
| Method | Behavior |
|---|---|
branch(id, { when, then, else }, display?) |
Terminal decision. Both paths must return output() or another terminal branch. |
if(id, { when, then, else }, display?) |
Return a typed value from one path, then continue shared work. |
output(name, value?, display?) |
Finish with a named outcome and optional resolved value. Does not execute a business action. |
Both path callbacks run during definition to describe the possible steps. During execution, the runner selects one path using the resolved condition.
Use flow expressions for that condition. A JavaScript if on a reference runs during definition and cannot select work based on its future value.
Return interfaces#
| Method | Type | Description |
|---|---|---|
branch(...) |
FlowExpression<FlowOutcome> |
Terminal result of the selected path. |
if(...) |
FlowExpression<A | B> |
Selected value, available to shared steps after the join. |
output(...) |
FlowExpression<FlowOutcome> |
Terminal outcome name and optional resolved value. |
Parallel lanes#
parallel(id, lanes, display?) takes a named object of lane callbacks. Each lane returns an expression. The result is an object of joined expressions with matching names. Work inside a lane is sequential; lanes are concurrent. A sibling lane's private intermediate references are not available to other lanes.
import { defineJevFlow } from "jev-devtools/flow";
const join = defineJevFlow<{}>("join", (flow) => {
const results = flow.parallel("load", {
left: (lane) => lane.task("left-value", { input: 2, run: (n) => n * 2 }),
right: (lane) => lane.task("right-value", { input: 3, run: (n) => n * 3 }),
});
return flow.output("joined", { left: results.left, right: results.right });
});
// Call tryJoin() from Node. This definition has no Jev requests.
export async function tryJoin() {
const noInference = {
systemOne() { throw new Error("This example does not call Jev"); },
};
const result = await join.run({}, { client: noInference });
console.log(result.outcome); // "joined"
console.log(result.value); // { left: 4, right: 9 }
return result;
}Run named lanes concurrently and join their returned expressions. Each lane's work stays sequential. Lane names belong to your application.
flow.parallel(id, lanes, display?)- lanes
- An object of builder callbacks, each returning an expression. Private sibling intermediates are not available across lanes.
Resolve input references, run application code, and return a reference to its result. Tasks do not retry automatically.
flow.task(id, { input, run, timeoutMs? }, display?)- input
- Values and nested flow references resolved before run is called.
- run
- Your callback receives the resolved input and task context, including a cancellation signal.
- timeoutMs?
- Optional time limit. The callback must cooperate with cancellation; a timeout cannot undo side effects.
Values and nested flow references resolved before run is called.
inputYour callback receives the resolved input and task context, including a cancellation signal.
runAPI reference: defineJevFlow() · flow.parallel() · flow.task() · flow.output() · program.run()
tryJoin() starts a run and waits for both lanes. The left lane doubles 2, and the right lane triples 3. output() resolves their joined references to { left: 4, right: 9 }. Neither lane reads the other's private steps.
| Returned member | Type | Resolved value in this example |
|---|---|---|
results.left |
FlowExpression<number> |
4. |
results.right |
FlowExpression<number> |
9. |
The builder describes both lanes synchronously; the task callbacks run when tryJoin() awaits execution. For a request-based example, see the full flow guide.
Collections and loops#
map() executes an item program for each array item. Results stay in input order even when items finish out of order. Set maxItems to bound the array size and concurrency to limit simultaneous item programs.
The item builder receives { item, index, context }. Pass shared context through the input option. Whole-array references and scalar arrays preserve Item; nested reference templates resolve each item to Resolved<Item>.
loop() tests a state condition before each iteration. Its builder receives { state, index, context } and must return the next state. Whole-state references and scalars preserve State; initial templates resolve nested references to Resolved<State>.
When the condition becomes false, the loop returns the current state. If the condition remains true at maxIterations, it rejects with FlowLoopLimitError. The error identifies the failing loop in stepId and its message. It does not return a successful partial result.
Call tryOperations() from Node to run both definitions below. Its two await calls execute application-owned tasks without inference and read the resolved results:
import { defineJevFlow, all, not } from "jev-devtools/flow";
export const summarize = defineJevFlow<{ values: number[] }>(
"summarize",
(flow) => {
const doubled = flow.map(
"double",
{
items: flow.input.get("values"),
input: {},
maxItems: 20,
concurrency: 2,
},
(item) =>
item.task("multiply", {
input: item.input.get("item"),
run: (value) => value * 2,
}),
);
const total = flow.task("total", {
input: doubled,
run: (values) => values.reduce((sum, value) => sum + value, 0),
});
const label = flow.if("label", {
when: all(total.gt(10), not(total.eq(20))),
then: (path) =>
path.task("large", { input: total, run: (n) => `Large: ${n}` }),
else: (path) =>
path.task("regular", { input: total, run: (n) => `Regular: ${n}` }),
});
return flow.output("summary", { total, label });
},
);
export const count = defineJevFlow<{ target: number }>("count", (flow) => {
const initial = { value: 0, target: flow.input.get("target") };
const finalState = flow.loop(
"advance",
{
initial,
input: {},
while: (state) => state.get("value").lt(state.get("target")),
maxIterations: 10,
},
(iteration) =>
iteration.task("increment", {
input: iteration.input.get("state"),
run: (state) => ({ ...state, value: state.value + 1 }),
}),
);
return flow.output("counted", finalState);
});
// This flow has no Jev steps: no API key or network access is needed.
const noInference = {
systemOne() { throw new Error("This example does not call Jev"); },
};
// Call tryOperations() from Node. Each await starts and waits for a fresh run.
export async function tryOperations() {
const summary = await summarize.run({ values: [2, 4] }, { client: noInference });
console.log(summary.outcome); // "summary"
console.log(summary.value); // { total: 12, label: "Large: 12" }
const counted = await count.run({ target: 3 }, { client: noInference });
console.log(counted.outcome); // "counted"
console.log(counted.value); // { value: 3, target: 3 }
return { summary, counted };
}Run a bounded item program for each item. Results preserve input order even when items execute concurrently.
flow.map(id, options, build, display?)- items
- An array or a reference to one. Each item is passed to the item program.
- input
- Explicit shared context for each item program, available as input.context.
- maxItems
- Required bound on the number of items.
- concurrency?
- Maximum number of item programs allowed to run concurrently.
- build
- Receives a builder with input { item, index, context }. Return an expression for the item result. Direct output() and branch() calls are rejected with map context.
An array or a reference to one. Each item is passed to the item program.
itemsSelect a field owned by an object, an array index, or array length. TypeScript rejects array methods. Missing fields and array bounds remain execution checks; use a task for string operations.
expression.get(key)Explicit shared context for each item program, available as input.context.
inputRequired bound on the number of items.
maxItemsMaximum number of item programs allowed to run concurrently.
concurrencyChoose one value-returning path, then continue with its result. Unlike branch(), this does not finish the flow.
flow.if(id, { when, then, else }, display?)- when
- A flow condition evaluated during the run.
- then
- Build the path selected when the condition is true. Return an expression for its value; direct output() and branch() calls are rejected with if path context.
- else
- Build the path selected when the condition is false. Return an expression for its value; direct output() and branch() calls are rejected with if path context.
A flow condition evaluated during the run.
whenCombine Boolean flow conditions: every condition must be true. All operands are evaluated; this does not short-circuit missing references.
all(...conditions)Build a strictly-greater-than condition. Compares finite numeric values when the flow runs.
expression.gt(value)Invert a Boolean flow condition, evaluated when the flow runs.
not(condition)Build the path selected when the condition is true. Return an expression for its value; direct output() and branch() calls are rejected with if path context.
thenBuild the path selected when the condition is false. Return an expression for its value; direct output() and branch() calls are rejected with if path context.
elseTest a condition before each iteration and return the final state. Rejects if the condition still holds at the iteration limit.
flow.loop(id, options, build, display?)- initial
- The starting JSON state, or a reference to it.
- input
- Explicit shared context, available to the iteration program as input.context.
- while
- Build a Boolean flow condition from the current state. Tested before each iteration.
- maxIterations
- Required bound. Exceeding it while the condition is still true rejects with FlowLoopLimitError identifying the loop in stepId and its message.
- build
- Receives a builder with input { state, index, context }. Return an expression for the next state. Direct output() and branch() calls are rejected with loop context.
The starting JSON state, or a reference to it.
initialExplicit shared context, available to the iteration program as input.context.
inputBuild a Boolean flow condition from the current state. Tested before each iteration.
whileBuild a strictly-less-than condition. Compares finite numeric values when the flow runs.
expression.lt(value)Required bound. Exceeding it while the condition is still true rejects with FlowLoopLimitError identifying the loop in stepId and its message.
maxIterationsAPI reference: defineJevFlow() · flow.map() · flow.input · expression.get() · flow.task() · flow.if() · all() · expression.gt() · not() · expression.eq() · flow.output() · flow.loop() · expression.lt() · program.run()
tryOperations() runs two definitions:
summarizedoubles[2, 4]withmap(), adds the results in a task, and usesif()to choose a label.all()andnot()express the label policy. The result is outcome"summary"with value{ total: 12, label: "Large: 12" }.countcarries{ value, target }through a loop, incrementingvalueuntil it reachestarget. With target3, the result is outcome"counted"with value{ value: 3, target: 3 }. The initial template contains a target reference, which resolves before the condition and task use it.
| Method | Return type | Resolved value |
|---|---|---|
map(...) |
FlowExpression<Output[]> |
Item results in input order. |
loop(...) |
FlowExpression<State> for whole-state references/scalars; FlowExpression<Resolved<State>> for templates |
Final state when its pre-iteration condition becomes false. |
A loop can contain a map. Nested maps, nested loops, and loops inside maps are unsupported. Shared context is explicit; use context inside map/loop item programs rather than reaching across scope boundaries.
Limits and errors#
Node IDs must be unique across the whole definition, including untaken branches and parallel lanes. Output names are node IDs too. Use 1–80 letters, digits, _, or -, starting with a letter or digit.
The limits apply at different levels:
| Level | Limit |
|---|---|
| Definition | 256 nodes and 16 nested blocks |
| Run | 1,024 expanded nodes |
| Map | maxItems 1–64; concurrency 1–16, default 4 |
| Loop | maxIterations 1–64 |
| Task timeout | Integer timeoutMs from 1 to 2,147,483,647 |
0.5.2: each expression is limited to depth 80 and 4,096 expanded nodes, counting reused children at every occurrence; an operation accepts at most 256 operands. These limits apply during construction, including composition. Legacy condition text is capped at 4,096 UTF-16 code units with an explicit truncation suffix; execution retains the full accepted expression. See expression limits.
Structural errors fail during definition. Direct output() and terminal branch() calls in map bodies, loop bodies, and if() paths report the containing step, body or path, and operation. Return a value expression instead. Parallel lanes still accept terminal outputs and branches as lane values, including lanes inside these bodies. The shared builder type still permits some unsupported calls; definition construction checks them. Missing input values and invalid comparisons fail when execution reaches them. FlowLoopLimitError is exported for bounded-loop handling. For exact collection/concurrency/timeout limits, execution errors, and cancellation behavior, consult the detailed guide and installed declarations.
Replay captured answers#
Since 0.5.0, replayClient(fixture) supplies stored Jev answers to a flow run. It needs no API key or inference-service connection. Application tasks still execute.
Call await replayExample() from Node. The definition is synchronous; await flow.run(...) executes it using the stored response.
// Requires jev-devtools 0.5.0 or later.
import { choice } from "@typesafe-ai/sdk";
import {
defineJevFlow,
replayClient,
type FlowFixture,
} from "jev-devtools/flow";
export async function replayExample() {
const flow = defineJevFlow<{ message: string }>("saved-triage", (flow) => {
const result = flow.jev("classify", {
state: flow.input,
questions: {
team: choice("Which team?", { billing: null, other: null }),
},
});
return flow.output("routed", result.answer("team").choice);
});
// A hand-written fixture; Copy fixture can produce this shape from a run.
const fixture: FlowFixture = {
version: 1,
flow: "saved-triage",
steps: {
classify: {
model: "jev-sample",
answers: {
team: {
type: "choice",
choice: "billing",
probabilities: { billing: 0.8, other: 0.2 },
confidence: 0.6,
},
},
},
},
};
const result = await flow.run(
{ message: "I was charged twice." },
{ client: replayClient(fixture) },
);
console.log(result.outcome); // "routed"
console.log(result.value); // "billing" from the fixture; no inference call
return result;
}Saved responses for replay. Does not contain the original input or the flow implementation.
{ version: 1, flow, steps }- version
- Fixture format version. Currently 1.
- flow
- The name of the flow this fixture can replay.
- steps
- Saved responses indexed by step ID. IDs inside map/loop programs include their execution path.
Fixture format version. Currently 1.
versionThe name of the flow this fixture can replay.
flowSaved responses indexed by step ID. IDs inside map/loop programs include their execution path.
stepsReturn an SDK-compatible client that serves saved answers without a key or inference call. Missing responses reject. Tasks and request factories still execute again.
replayClient(fixture)API reference: defineJevFlow() · flow.jev() · flow.input · flow.output() · answer.choice · FlowFixture · program.run() · replayClient()
The fixture stores a response for classify. replayClient() supplies it when that step executes, so the Choice reference resolves to "billing". replayExample() returns outcome "routed" with value "billing", without inference.
| API | Return type | Description |
|---|---|---|
replayClient(fixture) |
TypeSafeClient |
Client that supplies saved answers inside a flow run. |
replayExample() |
Promise of a flow run result | Resolves to { runId, outcome: "routed", value: "billing" }. |
A FlowFixture contains { version: 1, flow, steps }. It contains responses, not the original input or flow implementation. Replay checks the flow name, step IDs, and requested question names; it does not validate prompts or state. Missing responses reject rather than calling live inference. Map/loop steps use composed IDs such as rounds/0/batch/1/assess.
Tasks and request factories execute again. A task can still perform external calls or side effects, so use controlled tasks when replaying in tests. Failed or cancelled captures are partial fixtures, not replays of recorded failures. See the replay guide for fixture copying and the repository CLI.
Build a fixture#
Available since 0.5.0.
flowFixture(definition, run, traces) is available from both jev-devtools and jev-devtools/flow. Pass the captured flow definition/run and a ReadonlyMap<string, Trace> indexed by trace ID. It returns a BuiltFlowFixture with { fixture, missing, warnings }.
Check missing for completed steps whose response was unavailable, and warnings for capture limitations before saving fixture as JSON. Server redaction and truncation remain in the fixture. The drawer's Copy fixture also applies its current display masking; calling this helper directly does not add display masking.
Return interface#
| Property | Type | Description |
|---|---|---|
fixture |
FlowFixture |
Captured response bodies indexed by step ID. |
missing |
string[] |
Completed steps whose response captures were unavailable. |
warnings |
string[] |
Capture limitations to inspect before saving. |
Related types#
FlowBuilder, FlowRunOptions, FlowOutcome, FlowTask, FlowRequestFactory, TaskContext, and Resolved describe builder and execution contracts. FlowDefinition, FlowRun, FlowStep, FlowDisplay, and FlowObserver describe inspection data and callbacks. FlowFixture and BuiltFlowFixture describe replay data (new in 0.5.0). See recorder and types.
Compare a fixture with a live run#
Available since 0.5.0.
compareWithFixture() runs the same definition and input twice: first with saved answers, then with your supplied client. A live client makes inference calls during the second run. formatComparison() returns a text summary of the differences.
import { compareWithFixture, formatComparison } from "jev-devtools/flow";
// program, input, fixture, client, and inspector come from your app.
const comparison = await compareWithFixture(program, input, fixture, {
client,
devtools: inspector,
tolerance: 0.01,
});
console.log(formatComparison(comparison));The comparison uses the same program and input for both executions. tolerance: 0.01 permits small numeric answer-field differences. formatComparison() returns text, and console.log() prints it.
| Value / member | Type | Description |
|---|---|---|
compareWithFixture(...) |
Promise<FixtureComparison> |
Resolves to outcome, value, decisions, answers, step differences, and a live fixture. |
comparison.changed |
boolean |
Outcome, value, decision, or executed Jev-step differences. |
comparison.answers |
AnswerChange[] |
Individual scalar answer-field comparisons. |
comparison.liveFixture |
FlowFixture |
Unredacted, untruncated live response bodies. |
formatComparison(comparison) |
string |
Plain-text description of changes. |
Read changed and answers separately. The top-level flag tracks differences in outcome, value, decisions, and executed Jev steps. Answer drift alone does not set it. Numeric tolerance applies only to individual answer fields.
Tasks execute on both sides, so use controlled, repeatable tasks. Neither execution undoes side effects. liveFixture contains unredacted, untruncated responses from the supplied client. Review it before saving or sharing.
See FixtureComparison for every output field and the comparison contract for errors and cancellation.