defineSemanticSlots
KIT · function · 运行时值
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 { defineSemanticSlots } from '@jangts/agent-core/kits/presentation';import { defineSemanticSlots } from '@jangts/agent-core/kits/presentation';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/kits/presentation、@jangts/agent-core/kits。
定义
调用
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definition | SemanticSlotDefinition | 必需 | — |
definition 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
namespace | string | 必需 | 只读。Slot namespace; must match ^[a-z][a-z0-9-]*$. |
keys | readonly string[] | 必需 | 只读。Allowed keys after the namespace; any other key makes the text invalid. |
maxOccurrences | number | 必需 | 只读。Maximum total occurrences across the text; must be a non-negative integer. |
返回: Readonly<{ namespace: string; isValid(text: string): boolean; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
namespace | string | 必需 | 见类型定义及调用场景。 |
isValid | (text: string) => boolean | 必需 | 见类型定义及调用场景。 |
使用示例
- 模板、语义槽与表达选择:程序拥有模板和偏好顺序,插入值只替换一次,变体随机源由 App 提供。
先用程序模板组合说明
入门 · 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: '普通对象也能实现接口。' }));相同模板加语义键校验与不重复变体
配置 · 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' ? '下面逐步说明接口。' : '普通对象即可。' }));