Skip to content
中文

defineTextTemplate ​

KIT · function · Runtime value

The program owns the master; inserted model text is a value and is substituted once only.

Substitution is single-pass String.replace: a value that itself contains {{slot}} is left as literal text, so a slot value cannot inject further substitutions.

Import ​

ts
import { defineTextTemplate } from '@jangts/agent-core/kits/presentation';
js
import { defineTextTemplate } 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 ​

ts
defineTextTemplate(master: string, options?: TextTemplateOptions): Readonly<{ slots: readonly string[]; render(values: Readonly<Record<string, string>>): string; }>

The program owns the master; inserted model text is a value and is substituted once only.

Substitution is single-pass String.replace: a value that itself contains {{slot}} is left as literal text, so a slot value cannot inject further substitutions.

ParameterTypePassingDescription
masterstringRequired—
optionsTextTemplateOptionsOptional—
Fields of options
FieldTypeRequiredDescription
observe((result: string) => void | Promise<void>) | undefinedOptionalRead-only. Observer invoked with every rendered text.
errors{ readonly missingField?: (key: string) => Error | string; } | undefinedOptionalRead-only. Builds the error for a slot the caller did not supply; receives the slot name.

Returns: Readonly<{ slots: readonly string[]; render(values: Readonly<Record<string, string>>): string; }>.

Return value structure
FieldTypeRequiredDescription
slotsreadonly string[]RequiredSee the type definition and usage context.
render(values: Readonly<Record<string, string>>) => stringRequiredSee the type definition and usage context.

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.