Skip to content
English

defineCandidateSelection ​

KIT · function · 运行时值

Compile an alternative-proposal envelope contract.

Alternative proposals are bounded and shallow; accepting a task closes this capability. A value that is not an object, or that does not carry the candidate field, is treated as a single implicit candidate and returned as-is. Once the envelope is present it must contain only that field, and its array must hold between minCandidates (default 1) and maxCandidates distinct candidate objects that do not themselves repeat the field.

导入 ​

ts
import { defineCandidateSelection } from '@jangts/agent-core/kits/candidates';
js
import { defineCandidateSelection } from '@jangts/agent-core/kits/candidates';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/kits/candidates、@jangts/agent-core/kits。

定义 ​

调用 ​

ts
defineCandidateSelection(definition: CandidateSelectionDefinition): Readonly<{ field: string; minCandidates: number; maxCandidates: number; parse: ({ value, allowed }: { value: unknown; allowed: boolean; }) => unknown[]; }>

Compile an alternative-proposal envelope contract.

Alternative proposals are bounded and shallow; accepting a task closes this capability. A value that is not an object, or that does not carry the candidate field, is treated as a single implicit candidate and returned as-is. Once the envelope is present it must contain only that field, and its array must hold between minCandidates (default 1) and maxCandidates distinct candidate objects that do not themselves repeat the field.

参数类型传递说明
definitionCandidateSelectionDefinition必需—
definition 的字段
字段类型必需说明
fieldstring必需只读。Reply field that carries the alternatives.
minCandidatesnumber | undefined可省略只读。Minimum number of distinct candidates once the envelope is present; defaults to 1.
maxCandidatesnumber必需只读。Maximum number of candidates; must be a safe integer not below the minimum.
errorBreakReason | undefined可省略只读。Rejection wording, either fixed, a pre-built error or a lazy factory.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to every rejection from this contract.

返回: Readonly<{ field: string; minCandidates: number; maxCandidates: number; parse: ({ value, allowed }: { value: unknown; allowed: boolean; }) => unknown[]; }>。

返回值结构
字段类型必需说明
fieldstring必需见类型定义及调用场景。
minCandidatesnumber必需见类型定义及调用场景。
maxCandidatesnumber必需见类型定义及调用场景。
parse({ value, allowed }: { value: unknown; allowed: boolean; }) => unknown[]必需见类型定义及调用场景。

使用示例 ​

接受单一提议或最多三个候选 ​

入门 · 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);
}

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。