Skip to content
中文

defineCandidateSelection ​

KIT · function · Runtime value

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.

Import ​

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

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

Definition ​

Call ​

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.

ParameterTypePassingDescription
definitionCandidateSelectionDefinitionRequired—
Fields of definition
FieldTypeRequiredDescription
fieldstringRequiredRead-only. Reply field that carries the alternatives.
minCandidatesnumber | undefinedOptionalRead-only. Minimum number of distinct candidates once the envelope is present; defaults to 1.
maxCandidatesnumberRequiredRead-only. Maximum number of candidates; must be a safe integer not below the minimum.
errorBreakReason | undefinedOptionalRead-only. Rejection wording, either fixed, a pre-built error or a lazy factory.
tagsreadonly string[] | undefinedOptionalRead-only. Diagnostic tags attached to every rejection from this contract.

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

Return value structure
FieldTypeRequiredDescription
fieldstringRequiredSee the type definition and usage context.
minCandidatesnumberRequiredSee the type definition and usage context.
maxCandidatesnumberRequiredSee the type definition and usage context.
parse({ value, allowed }: { value: unknown; allowed: boolean; }) => unknown[]RequiredSee the type definition and usage context.

Examples ​

Accept a single proposal or up to three candidates ​

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

Add a lower bound and an application-stage constraint to the same parse ​

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

API reference, usage examples and developer guides.