walkRules
CORE · function · 运行时值
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 { walkRules } from '@jangts/agent-core/support';import { walkRules } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support。
定义
重载 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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
rules | readonly WalkRule<void>[] | 必需 | — |
返回: void。
重载 2
walkRules<Input>(rules: readonly WalkRule<Input>[], input: Input): voidWalk every rule in order, mutating the shared input.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
rules | readonly WalkRule<Input>[] | 必需 | — |
input | Input | 必需 | — |
返回: void。
使用示例
- 拒绝检查、首个匹配与顺序处理:使用真实清单表达多项检查和枚举分发;单个普通判断仍可直接使用 if。
一个明确的拒绝
入门 · 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);有依赖关系的检查按序求值
配置 · 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);声明式优先分发与顺序转换
扩展 · 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);