Skip to content
中文

route ​

CORE · function · Runtime value

Import ​

ts
import { route } from '@jangts/agent-core/engine';
js
import { route } from '@jangts/agent-core/engine';

Other public entry points:@jangts/agent-core, @jangts/agent-core/engine, @jangts/agent-core/types.

Definition ​

Call ​

ts
route(target: string, id?: string): { id: string; targetNodeId: string; }
ParameterTypePassingDescription
targetstringRequired—
idstring | undefinedOptional—

Returns: { id: string; targetNodeId: string; }.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.

Member index ​

MemberPurpose
toSee the overload below for full inputs and outputs.
toIfEqualSee the overload below for full inputs and outputs.
toIfEqualWithSee the overload below for full inputs and outputs.
toIfNotEmptySee the overload below for full inputs and outputs.
toIfDefinedSee the overload below for full inputs and outputs.
toIfSee the overload below for full inputs and outputs.
toIfErrorSee the overload below for full inputs and outputs.
deliverSee the overload below for full inputs and outputs.
deliverIfReadySee the overload below for full inputs and outputs.
deliverWhenCompleteSee the overload below for full inputs and outputs.

Member details ​

to ​

ts
route.to(target: string, id?: string): { id: string; targetNodeId: string; }
ParameterTypePassingDescription
targetstringRequired—
idstring | undefinedOptional—

Returns: { id: string; targetNodeId: string; }.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.

toIfEqual ​

ts
route.toIfEqual<Key extends PropertyKey, Value>(target: string, path: Key, value: Value, options?: RouteOptions): { id: string | undefined; conditionId: string | undefined; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; targetNodeId: string; description?: string; }

One conditional jump, using the same strict equality as a handwritten route.

A derived route id is only produced for symbol-free paths and primitive values, because those are the cases where String(value) is stable and unique enough to name the condition.

Type parameterConstraintDefault type
KeyPropertyKey—
Value——
ParameterTypePassingDescription
targetstringRequired—
pathKeyRequired—
valueValueRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { id: string \| undefined; conditionId: string \| undefined; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
idstring | undefinedRequiredSee the type definition and usage context.
conditionIdstring | undefinedRequiredSee the type definition and usage context.
when(state: Partial<Record<PathRoot<Key>, unknown>>) => booleanRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

toIfEqualWith ​

ts
route.toIfEqualWith<Left extends PropertyKey, Right extends PropertyKey>(target: string, leftPath: Left, rightPath: Right, options?: RouteOptions): { id: string | undefined; conditionId: string | undefined; when: (state: Partial<Record<PathRoot<Left> | PathRoot<Right>, unknown>>) => boolean; targetNodeId: string; description?: string; }

Jump when two state paths resolve to strictly equal values.

No derived route id is produced for symbol paths, because a symbol cannot be embedded in a stable name.

Type parameterConstraintDefault type
LeftPropertyKey—
RightPropertyKey—
ParameterTypePassingDescription
targetstringRequired—
leftPathLeftRequired—
rightPathRightRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { id: string \| undefined; conditionId: string \| undefined; when: (state: Partial<Record<PathRoot<Left> \| PathRoot<Right>, unknown>>) => boolean; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
idstring | undefinedRequiredSee the type definition and usage context.
conditionIdstring | undefinedRequiredSee the type definition and usage context.
when(state: Partial<Record<PathRoot<Left> | PathRoot<Right>, unknown>>) => booleanRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

toIfNotEmpty ​

ts
route.toIfNotEmpty<Key extends PropertyKey>(target: string, key: Key, options?: RouteOptions): { conditionId: string; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }

Jump when a state array contains at least one entry.

A non-array value never matches, so a mistyped field cannot be treated as populated.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
targetstringRequired—
keyKeyRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { conditionId: string; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<PathRoot<Key>, unknown>>) => booleanRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

toIfDefined ​

ts
route.toIfDefined<Key extends PropertyKey>(target: string, field: Key, options?: RouteOptions): { conditionId: string; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }

Jump once a state field has a value.

null counts as present: only undefined means the field is still unset.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
targetstringRequired—
fieldKeyRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { conditionId: string; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<PathRoot<Key>, unknown>>) => booleanRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

toIf ​

ts
route.toIf<Key extends string>(target: Key, options?: RouteOptions): { when: (state: Partial<Record<Key, unknown>>) => boolean; conditionId: string; id: string; targetNodeId: string; description?: string; }

Jump to the node named by a truthy state field.

The same field is used as the condition and as the destination, which is the usual shape of a dispatcher that stored its own next node.

Type parameterConstraintDefault type
Keystring—
ParameterTypePassingDescription
targetKeyRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { when: (state: Partial<Record<Key, unknown>>) => boolean; conditionId: string; id: string; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
when(state: Partial<Record<Key, unknown>>) => booleanRequiredSee the type definition and usage context.
conditionIdstringRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

toIfError ​

ts
route.toIfError<Key extends PropertyKey>(target: string, options: RouteOptions & { readonly errorKey: Key; }): ReturnType<typeof toIfDefined<Key>>

Route a recorded state error to its recovery node.

Defaults to the error field because that is the convention the engine relies on when reporting recoverable failures.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
targetstringRequired—
optionsRouteOptions & { readonly errorKey: Key; }Required—
Fields of options
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().
errorKeyKeyRequiredRead-only. See the type definition and usage context.

Returns: { conditionId: string; when: (state: Partial<Record<PathRoot<Key>, unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<PathRoot<Key>, unknown>>) => booleanRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().
ts
route.toIfError(target: string, options?: RouteOptions & { readonly errorKey?: undefined; }): ReturnType<typeof toIfDefined<"error">>
ParameterTypePassingDescription
targetstringRequired—
options(RouteOptions & { readonly errorKey?: undefined; }) | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().
errorKeyundefinedOptionalRead-only. See the type definition and usage context.

Returns: { conditionId: string; when: (state: Partial<Record<"error", unknown>>) => boolean; id: string; targetNodeId: string; description?: string; }.

Return value structure
FieldTypeRequiredDescription
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<"error", unknown>>) => booleanRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
targetNodeIdstringRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

deliver ​

ts
route.deliver<Key extends PropertyKey>(field: Key, options?: Omit<RouteOptions, "conditionId">): { id: string; complete: <State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>; description?: string; }

At a terminal node, the result must already exist.

Completing reads the field with requireWorkflowValue, so a node that forgot to produce its result fails instead of returning undefined.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
fieldKeyRequired—
optionsOmit<RouteOptions, "conditionId"> | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { id: string; complete: <State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>; description?: string; }.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
complete<State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>RequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

deliverIfReady ​

ts
route.deliverIfReady<Key extends PropertyKey>(field: Key, options?: RouteOptions): { conditionId: string; when: (state: Partial<Record<Key, unknown>>) => boolean; id: string; complete: <State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>; description?: string; }

Deliver an available result, or let the next route decide.

Unlike deliver, this route carries a condition so a following route still has a chance to match when the field is still unset.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
fieldKeyRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { conditionId: string; when: (state: Partial<Record<Key, unknown>>) => boolean; id: string; complete: <State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>; description?: string; }.

Return value structure
FieldTypeRequiredDescription
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<Key, unknown>>) => booleanRequiredSee the type definition and usage context.
idstringRequiredSee the type definition and usage context.
complete<State extends Partial<Record<Key, unknown>>>(state: State) => Exclude<State[Key], undefined>RequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

deliverWhenComplete ​

ts
route.deliverWhenComplete<Key extends PropertyKey>(field: Key, options?: RouteOptions): { id: string; conditionId: string; when: (state: Partial<Record<Key, StepOutcome<unknown>>>) => boolean; complete<Value>(state: Partial<Record<Key, StepOutcome<Value>>>): Value; description?: string; }

Apply the shared continue/complete outcome contract.

The field holds a StepOutcome; only the complete variant ends the workflow, and the completion asserts the variant instead of trusting the condition.

Type parameterConstraintDefault type
KeyPropertyKey—
ParameterTypePassingDescription
fieldKeyRequired—
optionsRouteOptions | undefinedOptional—

Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idstring | undefinedOptionalRead-only. Explicit route id; otherwise derived from the target so it stays stable.
conditionIdstring | undefinedOptionalRead-only. Condition identity template; {nid} and {rid} are replaced when the node registers.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Returns: { id: string; conditionId: string; when: (state: Partial<Record<Key, StepOutcome<unknown>>>) => boolean; complete<Value>(state: Partial<Record<Key, StepOutcome<Value>>>): Value; description?: string; }.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
conditionIdstringRequiredSee the type definition and usage context.
when(state: Partial<Record<Key, StepOutcome<unknown>>>) => booleanRequiredSee the type definition and usage context.
complete<Value>(state: Partial<Record<Key, StepOutcome<Value>>>) => ValueRequiredSee the type definition and usage context.
descriptionstring | undefinedOptionalRead-only. Human-readable description surfaced by describe().

Examples ​

Minimal jump and end ​

Beginner · Node.js

ts
import { route } from '@jangts/agent-core/engine';
const jump = route('reply');
const sameJump = route.to('reply');
const finish = route.deliver('answer');
console.log(jump, sameJump, finish);
js
import { route } from '@jangts/agent-core/engine';
const jump = route('reply');
const sameJump = route.to('reply');
const finish = route.deliver('answer');
console.log(jump, sameJump, finish);

Common branches for the same business ​

Configuration · Node.js

ts
import { route } from '@jangts/agent-core/engine';
const reviewRoutes = [
  route.toIfError('failure'),
  route.toIf('recovered'),
  route.toIfNotEmpty('candidate', 'candidates'),
  route.toIfDefined('reply', 'answer'),
  route.toIfEqual('status', 'intent.id', 'status'),
  route.toIfEqualWith('reuse', 'cache.0.id', 'request.id'),
  route.to('fallback'),
];
const completionRoutes = [route.deliverIfReady('answer'), route.to('retry')];
const outcomeRoutes = [route.deliverWhenComplete('outcome'), route.to('retry')];
console.log(reviewRoutes, completionRoutes, outcomeRoutes);
js
import { route } from '@jangts/agent-core/engine';
const reviewRoutes = [
    route.toIfError('failure'),
    route.toIf('recovered'),
    route.toIfNotEmpty('candidate', 'candidates'),
    route.toIfDefined('reply', 'answer'),
    route.toIfEqual('status', 'intent.id', 'status'),
    route.toIfEqualWith('reuse', 'cache.0.id', 'request.id'),
    route.to('fallback'),
];
const completionRoutes = [route.deliverIfReady('answer'), route.to('retry')];
const outcomeRoutes = [route.deliverWhenComplete('outcome'), route.to('retry')];
console.log(reviewRoutes, completionRoutes, outcomeRoutes);

Override identity as the business requires; hand-written objects also support placeholders ​

Extension · Node.js

conditionId carries no authorization. It helps logs explain which condition matched.

ts
import { defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { answer?: string; reviewError?: string };
const workflow = defineWorkflow<State, string>({ id: 'review', nodes: [
  { id: 'check', description: '检查既有结果', startsRound: true, routes: [
    route.toIfError('failure', { errorKey: 'reviewError', id: 'review-error' }),
    { id: 'ready', conditionId: '{nid}.{rid}', when: state => state.answer !== undefined,
      complete: state => state.answer! },
    route.to('failure'),
  ] },
  { id: 'failure', description: '给出当前缺失信息',
    run: () => ({ answer: '当前没有可交付结果' }), routes: [route.deliver('answer')] },
] });
console.log(workflow.describe());
js
import { defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'review', nodes: [
        { id: 'check', description: '检查既有结果', startsRound: true, routes: [
                route.toIfError('failure', { errorKey: 'reviewError', id: 'review-error' }),
                { id: 'ready', conditionId: '{nid}.{rid}', when: state => state.answer !== undefined,
                    complete: state => state.answer },
                route.to('failure'),
            ] },
        { id: 'failure', description: '给出当前缺失信息',
            run: () => ({ answer: '当前没有可交付结果' }), routes: [route.deliver('answer')] },
    ] });
console.log(workflow.describe());

API reference, usage examples and developer guides.