jevtools.Documentation

API reference

Flow builder

Define requests, tasks, branches, parallel work, collections, and loops.

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.

ts · API help
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 = <{ message: string }>("triage", (flow) => {
  const classification = flow.("classify", {
    : flow.,
    : {
      team: choice("Which team can help?", {
        billing: "Charges and payments",
        other: "Everything else",
      }),
    },
  });

  return flow.("route", {
    : classification.("team")..("billing"),
    : (path) => path.("billing-team"),
    : (path) => path.("support-team"),
  });
});

// 2. Call routeMessage("I was charged twice.") from your Node handler.
export async function routeMessage(message: string) {
  const result = await triage.({ message }, { : jev, : inspector });
  console.log(result.outcome); // "billing-team" or "support-team", chosen by the flow
  return result;
}

API 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.

ts · API help
import { defineJevFlow } from "jev-devtools/flow";

const join = <{}>("join", (flow) => {
  const results = flow.("load", {
    left: (lane) => lane.("left-value", { : 2, : (n) => n * 2 }),
    right: (lane) => lane.("right-value", { : 3, : (n) => n * 3 }),
  });
  return flow.("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.({}, { : noInference });
  console.log(result.outcome); // "joined"
  console.log(result.value); // { left: 4, right: 9 }
  return result;
}

API 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:

ts · API help
import { defineJevFlow, all, not } from "jev-devtools/flow";

export const summarize = <{ values: number[] }>(
  "summarize",
  (flow) => {
    const doubled = flow.(
      "double",
      {
        : flow..("values"),
        : {},
        : 20,
        : 2,
      },
      (item) =>
        item.("multiply", {
          : item..("item"),
          : (value) => value * 2,
        }),
    );

    const total = flow.("total", {
      : doubled,
      : (values) => values.reduce((sum, value) => sum + value, 0),
    });

    const label = flow.("label", {
      : (total.(10), (total.(20))),
      : (path) =>
        path.("large", { : total, : (n) => `Large: ${n}` }),
      : (path) =>
        path.("regular", { : total, : (n) => `Regular: ${n}` }),
    });
    return flow.("summary", { total, label });
  },
);

export const count = <{ target: number }>("count", (flow) => {
  const initial = { value: 0, target: flow..("target") };
  const finalState = flow.(
    "advance",
    {
      ,
      : {},
      : (state) => state.("value").(state.("target")),
      : 10,
    },
    (iteration) =>
      iteration.("increment", {
        : iteration..("state"),
        : (state) => ({ ...state, value: state.value + 1 }),
      }),
  );
  return flow.("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.({ values: [2, 4] }, { : noInference });
  console.log(summary.outcome); // "summary"
  console.log(summary.value); // { total: 12, label: "Large: 12" }

  const counted = await count.({ target: 3 }, { : noInference });
  console.log(counted.outcome); // "counted"
  console.log(counted.value); // { value: 3, target: 3 }
  return { summary, counted };
}

API 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:

  • summarize doubles [2, 4] with map(), adds the results in a task, and uses if() to choose a label. all() and not() express the label policy. The result is outcome "summary" with value { total: 12, label: "Large: 12" }.
  • count carries { value, target } through a loop, incrementing value until it reaches target. With target 3, 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.

ts · API help
// 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 = <{ message: string }>("saved-triage", (flow) => {
    const result = flow.("classify", {
      : flow.,
      : {
        team: choice("Which team?", { billing: null, other: null }),
      },
    });
    return flow.("routed", result.("team").);
  });

  // A hand-written fixture; Copy fixture can produce this shape from a run.
  const fixture:  = {
    : 1,
    : "saved-triage",
    : {
      classify: {
        model: "jev-sample",
        answers: {
          team: {
            type: "choice",
            choice: "billing",
            probabilities: { billing: 0.8, other: 0.2 },
            confidence: 0.6,
          },
        },
      },
    },
  };
  const result = await flow.(
    { message: "I was charged twice." },
    { : (fixture) },
  );
  console.log(result.outcome); // "routed"
  console.log(result.value); // "billing" from the fixture; no inference call
  return result;
}

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.

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.

ts
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.

Jev DevTools is an independent, open-source community project. It is not affiliated with TypeSafe AI.