Skip to content
中文

selectVariant ​

KIT · function · Runtime value

Pick one variant, avoiding an immediate repeat when alternatives exist.

When every candidate equals previous, the full list is used again, so a single-value set still yields a result instead of failing.

Import ​

ts
import { selectVariant } from '@jangts/agent-core/kits/presentation';
js
import { selectVariant } from '@jangts/agent-core/kits/presentation';

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

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Value——

Call ​

ts
selectVariant<Value>(values: readonly Value[], options: { previous?: Value; randomIndex(limit: number): number; }): Value | undefined

Pick one variant, avoiding an immediate repeat when alternatives exist.

When every candidate equals previous, the full list is used again, so a single-value set still yields a result instead of failing.

Type parameterConstraintDefault type
Value——
ParameterTypePassingDescription
valuesreadonly Value[]Required—
options{ previous?: Value; randomIndex(limit: number): number; }Required—
Fields of options
FieldTypeRequiredDescription
previousValue | undefinedOptionalSee the type definition and usage context.
randomIndex(limit: number) => numberRequiredSee the type definition and usage context.

Returns: Value \| undefined.

Awaited value: Awaited<Value> \| undefined.

Examples ​

First compose an explanation with a program template ​

Beginner · Node.js

ts
import { Presentation } from '@jangts/agent-core/kits/presentation';
const template = Presentation.defineTextTemplate('你好,{{name}}。{{body}}');
console.log(template.render({ name: '开发者', body: '普通对象也能实现接口。' }));
js
import { Presentation } from '@jangts/agent-core/kits/presentation';
const template = Presentation.defineTextTemplate('你好,{{name}}。{{body}}');
console.log(template.render({ name: '开发者', body: '普通对象也能实现接口。' }));

Add semantic key validation and a non-repeating variant to the same template ​

Configuration · Node.js

ts
import { Presentation } from '@jangts/agent-core/kits/presentation';
const slots = Presentation.defineSemanticSlots({ namespace: 'tone', keys: ['brief', 'detailed'], maxOccurrences: 1 });
console.log(slots.isValid('{{tone:brief}}'));
const greeting = Presentation.selectVariant(['你好', '欢迎'], { previous: '你好', randomIndex: () => 0 });
const tone = Presentation.resolvePreference([{ value: 'brief', priority: 1, order: 0 }, { value: 'detailed', priority: 2, order: 1 }]);
const template = Presentation.defineTextTemplate('{{greeting}},{{body}}');
console.log(template.render({ greeting: greeting ?? '你好', body: tone === 'detailed' ? '下面逐步说明接口。' : '普通对象即可。' }));
js
import { Presentation } from '@jangts/agent-core/kits/presentation';
const slots = Presentation.defineSemanticSlots({ namespace: 'tone', keys: ['brief', 'detailed'], maxOccurrences: 1 });
console.log(slots.isValid('{{tone:brief}}'));
const greeting = Presentation.selectVariant(['你好', '欢迎'], { previous: '你好', randomIndex: () => 0 });
const tone = Presentation.resolvePreference([{ value: 'brief', priority: 1, order: 0 }, { value: 'detailed', priority: 2, order: 1 }]);
const template = Presentation.defineTextTemplate('{{greeting}},{{body}}');
console.log(template.render({ greeting: greeting ?? '你好', body: tone === 'detailed' ? '下面逐步说明接口。' : '普通对象即可。' }));

API reference, usage examples and developer guides.