walkRules
CORE · function · Runtime value
Walk every rule in order, mutating the shared input.
Later rules observe the mutations of earlier ones, which is why traversal is strictly sequential and every participant must be synchronous. Throwing stops the walk immediately; remaining rules are skipped. Walk every rule in order, mutating the shared input.
Import
import { walkRules } from '@jangts/agent-core/support';import { walkRules } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/support.
Definition
Overload 1
walkRules(rules: readonly WalkRule[]): voidWalk every rule in order, mutating the shared input.
Later rules observe the mutations of earlier ones, which is why traversal is strictly sequential and every participant must be synchronous. Throwing stops the walk immediately; remaining rules are skipped.
| Parameter | Type | Passing | Description |
|---|---|---|---|
rules | readonly WalkRule<void>[] | Required | — |
Returns: void.
Overload 2
walkRules<Input>(rules: readonly WalkRule<Input>[], input: Input): voidWalk every rule in order, mutating the shared input.
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
rules | readonly WalkRule<Input>[] | Required | — |
input | Input | Required | — |
Returns: void.
Examples
- Rejection checks, first match, and sequential processing: Use real lists to express multiple checks and enumerated dispatch; a single ordinary condition can still use if.
One explicit rejection
Beginner · Node.js
import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);Checks with dependencies evaluated in order
Configuration · Node.js
import { Support } from '@jangts/agent-core';
const input: unknown = { name: 'Ivan' };
Support.breakWhile([
{ occur: !input || typeof input !== 'object', error: '需要对象' },
{ occur: () => !('name' in (input as object)), error: '缺少 name' },
]);
console.log(input);import { Support } from '@jangts/agent-core';
const input = { name: 'Ivan' };
Support.breakWhile([
{ occur: !input || typeof input !== 'object', error: '需要对象' },
{ occur: () => !('name' in input), error: '缺少 name' },
]);
console.log(input);Declarative priority dispatch and sequential transformation
Extension · Node.js
import { Support } from '@jangts/agent-core';
const routeName = Support.matchRule([
{ id: 'status', description: '状态意图', pattern: (input: string) => input === 'status', handle: () => 'service' },
{ id: 'fallback', description: '其他意图', handle: () => 'reply' },
], 'status');
const state = { answer: ' 服务正常 ' };
Support.walkRules([
{ handle: (value: typeof state) => { value.answer = value.answer.trim(); } },
{ pattern: (value: typeof state) => !!value.answer, handle: value => { value.answer += '。'; } },
], state);
console.log(routeName, state.answer);import { Support } from '@jangts/agent-core';
const routeName = Support.matchRule([
{ id: 'status', description: '状态意图', pattern: (input) => input === 'status', handle: () => 'service' },
{ id: 'fallback', description: '其他意图', handle: () => 'reply' },
], 'status');
const state = { answer: ' 服务正常 ' };
Support.walkRules([
{ handle: (value) => { value.answer = value.answer.trim(); } },
{ pattern: (value) => !!value.answer, handle: value => { value.answer += '。'; } },
], state);
console.log(routeName, state.answer);