Candidates:有界候选封套
同一结果可以是单项提议或候选清单;候选权限与数量由应用控制。
parse({value,allowed}) 返回 unknown[]。没有配置字段的输入按单项返回;出现 field 时必须是只有该字段的对象,成员必须是非数组对象且不能再次嵌套候选字段。默认 minCandidates=1,maxCandidates 必填。allowed 只表示此处是否接受多候选,不是执行授权;模型不能通过候选封套绕过任务已接受后的限制。
由简到繁的同业务示例
接受单一提议或最多三个候选
入门 · Node.js
ts
import { Candidates } from '@jangts/agent-core/kits/candidates';
const selection = Candidates.defineCandidateSelection({ field: 'candidates', maxCandidates: 3 });
console.log(selection.parse({ value: { action: 'read' }, allowed: true }));
console.log(selection.parse({ value: { candidates: [{ action: 'read' }, { action: 'reply' }] }, allowed: true }));js
import { Candidates } from '@jangts/agent-core/kits/candidates';
const selection = Candidates.defineCandidateSelection({ field: 'candidates', maxCandidates: 3 });
console.log(selection.parse({ value: { action: 'read' }, allowed: true }));
console.log(selection.parse({ value: { candidates: [{ action: 'read' }, { action: 'reply' }] }, allowed: true }));相同 parse 增加下界和应用阶段约束
配置 · Node.js
ts
import { Candidates } from '@jangts/agent-core/kits/candidates';
const selection = Candidates.defineCandidateSelection({ field: 'candidates', minCandidates: 2, maxCandidates: 3, error: '当前阶段不接受这份候选' });
const taskAccepted = true;
try {
selection.parse({ value: { candidates: [{ action: 'read' }, { action: 'reply' }] }, allowed: !taskAccepted });
} catch (error) { console.log((error as Error).message); }js
import { Candidates } from '@jangts/agent-core/kits/candidates';
const selection = Candidates.defineCandidateSelection({ field: 'candidates', minCandidates: 2, maxCandidates: 3, error: '当前阶段不接受这份候选' });
const taskAccepted = true;
try {
selection.parse({ value: { candidates: [{ action: 'read' }, { action: 'reply' }] }, allowed: !taskAccepted });
}
catch (error) {
console.log(error.message);
}