defineSemanticSlots
KIT · function · Runtime value
Models select public semantic keys; hosts retain private values and selection policy.
A text is valid when it uses at most maxOccurrences namespaced slots, every name is a declared key, and no malformed {{namespace remnant is left behind — the last check stops partial or misspelled placeholders from reaching the template engine.
Import
import { defineSemanticSlots } from '@jangts/agent-core/kits/presentation';import { defineSemanticSlots } from '@jangts/agent-core/kits/presentation';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/kits/presentation, @jangts/agent-core/kits.
Definition
Call
defineSemanticSlots(definition: SemanticSlotDefinition): Readonly<{ namespace: string; isValid(text: string): boolean; }>Models select public semantic keys; hosts retain private values and selection policy.
A text is valid when it uses at most maxOccurrences namespaced slots, every name is a declared key, and no malformed {{namespace remnant is left behind — the last check stops partial or misspelled placeholders from reaching the template engine.
| Parameter | Type | Passing | Description |
|---|---|---|---|
definition | SemanticSlotDefinition | Required | — |
Fields of definition
| Field | Type | Required | Description |
|---|---|---|---|
namespace | string | Required | Read-only. Slot namespace; must match ^[a-z][a-z0-9-]*$. |
keys | readonly string[] | Required | Read-only. Allowed keys after the namespace; any other key makes the text invalid. |
maxOccurrences | number | Required | Read-only. Maximum total occurrences across the text; must be a non-negative integer. |
Returns: Readonly<{ namespace: string; isValid(text: string): boolean; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
namespace | string | Required | See the type definition and usage context. |
isValid | (text: string) => boolean | Required | See the type definition and usage context. |
Examples
- Templates, semantic slots, and expression selection: The program owns templates and preference order, inserted values are replaced only once, and the App supplies the random source for variants.
First compose an explanation with a program template
Beginner · Node.js
import { Presentation } from '@jangts/agent-core/kits/presentation';
const template = Presentation.defineTextTemplate('你好,{{name}}。{{body}}');
console.log(template.render({ name: '开发者', body: '普通对象也能实现接口。' }));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
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' ? '下面逐步说明接口。' : '普通对象即可。' }));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' ? '下面逐步说明接口。' : '普通对象即可。' }));