Skip to content
中文

generators ​

KIT · constant · Runtime value

Import ​

ts
import { generators } from '@jangts/agent-core/mock';
js
import { generators } from '@jangts/agent-core/mock';

Other public entry points:@jangts/agent-core/mock.

Definition ​

Member index ​

MemberPurpose
summaryExtractive summary, one paragraph, at most maxChars (default 240) characters.
bulletsSame selection as summary, rendered as - bullet lines (default budget 500).
keywordsMost frequent non-stopword terms, most frequent first.
titleFirst sentence of the source with leading Markdown heading marks removed.
replyShort conversational answer that echoes the topic. Continuation prompts ("继续", "换个说法") reuse the previous user message so a demo conversation stays coherent without a model.
extractExtract the first match of each configured pattern into a JSON object. A capture group is preferred over the whole match, so id:(\d+) yields just the digits. A field with no match is null rather than omitted, keeping the output shape stable.
integerUniform integer in an inclusive range.
floatContinuous value in [min, max), or exactly min when both bounds are equal.
booleanFair coin derived from the call PRNG.
pickUniform choice from a fixed pool. The pool is copied at construction and each result is copied again, so a consumer mutating a returned object cannot corrupt later selections.
idIdentifier that includes the call index plus a random suffix. The suffix is 32 random bits rendered in hex; it is a fixture value, not a UUID and not guaranteed unique or unguessable.
dateISO-8601 timestamp read from the injected clock. With a fixed clock option the value is constant, which keeps snapshot-style tests stable.
sentenceOne of three fixed Chinese sample sentences. The sentences are placeholders that read naturally in demos; selection still consumes the per-call PRNG so the value changes with the seed.
imageUrlInline SVG data URL, so no network or filesystem access is needed.
sequencePlay a fixed list one item per accepted call. Selection is driven by context.ruleIndex (1-based), so it advances with the rule's own hit count and is unaffected by other rules or by concurrent calls.
toolCallShorthand for a reply that only emits one tool call.

Member details ​

summary ​

Extractive summary, one paragraph, at most maxChars (default 240) characters.

ts
generators.summary(options?: TextOptions): Generator
ParameterTypePassingDescription
optionsTextOptionsOptional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.

Returns: Generator.

bullets ​

Same selection as summary, rendered as - bullet lines (default budget 500).

ts
generators.bullets(options?: TextOptions): Generator
ParameterTypePassingDescription
optionsTextOptionsOptional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.

Returns: Generator.

keywords ​

Most frequent non-stopword terms, most frequent first.

ts
generators.keywords(options?: TextOptions): Generator
ParameterTypePassingDescription
optionsTextOptionsOptional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.

Returns: Generator.

title ​

First sentence of the source with leading Markdown heading marks removed.

ts
generators.title(options?: TextOptions): Generator
ParameterTypePassingDescription
optionsTextOptionsOptional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.

Returns: Generator.

reply ​

Short conversational answer that echoes the topic.

Continuation prompts ("继续", "换个说法") reuse the previous user message so a demo conversation stays coherent without a model.

ts
generators.reply(options?: TextOptions & { template?: (input: { text: string; previous: string; }) => string; }): Generator
ParameterTypePassingDescription
optionsTextOptions & { template?: (input: { text: string; previous: string; }) => string; }Optional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.
template((input: { text: string; previous: string; }) => string) | undefinedOptionalSee the type definition and usage context.

Returns: Generator.

extract ​

Extract the first match of each configured pattern into a JSON object.

A capture group is preferred over the whole match, so id:(\d+) yields just the digits. A field with no match is null rather than omitted, keeping the output shape stable.

ts
generators.extract(options?: TextOptions & { fields?: Record<string, RegExp>; }): Generator
ParameterTypePassingDescription
optionsTextOptions & { fields?: Record<string, RegExp>; }Optional—
Fields of options
FieldTypeRequiredDescription
sourceTextSource | undefinedOptionalExplicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefinedOptionalMaximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefinedOptionalHow many items to select (1-100), e.g. bullets, keywords or summary sentences.
fieldsRecord<string, RegExp> | undefinedOptionalSee the type definition and usage context.

Returns: Generator.

integer ​

Uniform integer in an inclusive range.

ts
generators.integer(min?: number, max?: number): ValueGenerator<number>
ParameterTypePassingDescription
minnumberOptional—
maxnumberOptional—

Returns: ValueGenerator<number>.

float ​

Continuous value in [min, max), or exactly min when both bounds are equal.

ts
generators.float(min?: number, max?: number): ValueGenerator<number>
ParameterTypePassingDescription
minnumberOptional—
maxnumberOptional—

Returns: ValueGenerator<number>.

boolean ​

Fair coin derived from the call PRNG.

ts
generators.boolean(): ValueGenerator<boolean>

No call parameters.

Returns: ValueGenerator<boolean>.

pick ​

Uniform choice from a fixed pool.

The pool is copied at construction and each result is copied again, so a consumer mutating a returned object cannot corrupt later selections.

ts
generators.pick<T extends JsonValue>(items: readonly T[]): ValueGenerator<T>
Type parameterConstraintDefault type
TJsonValue—
ParameterTypePassingDescription
itemsreadonly T[]Required—

Returns: ValueGenerator<T>.

id ​

Identifier that includes the call index plus a random suffix.

The suffix is 32 random bits rendered in hex; it is a fixture value, not a UUID and not guaranteed unique or unguessable.

ts
generators.id(prefix?: string): ValueGenerator<string>
ParameterTypePassingDescription
prefixstringOptional—

Returns: ValueGenerator<string>.

date ​

ISO-8601 timestamp read from the injected clock.

With a fixed clock option the value is constant, which keeps snapshot-style tests stable.

ts
generators.date(): ValueGenerator<string>

No call parameters.

Returns: ValueGenerator<string>.

sentence ​

One of three fixed Chinese sample sentences.

The sentences are placeholders that read naturally in demos; selection still consumes the per-call PRNG so the value changes with the seed.

ts
generators.sentence(): ValueGenerator<string>

No call parameters.

Returns: ValueGenerator<string>.

imageUrl ​

Inline SVG data URL, so no network or filesystem access is needed.

ts
generators.imageUrl({ width, height, }?: { width?: number; height?: number; }): ValueGenerator<string>
ParameterTypePassingDescription
__0{ width?: number; height?: number; }Optional—
Fields of __0
FieldTypeRequiredDescription
widthnumber | undefinedOptionalSee the type definition and usage context.
heightnumber | undefinedOptionalSee the type definition and usage context.

Returns: ValueGenerator<string>.

sequence ​

Play a fixed list one item per accepted call.

Selection is driven by context.ruleIndex (1-based), so it advances with the rule's own hit count and is unaffected by other rules or by concurrent calls.

ts
generators.sequence(items: readonly Generator[]): Generator
ParameterTypePassingDescription
itemsreadonly Generator[]Required—

Returns: Generator.

toolCall ​

Shorthand for a reply that only emits one tool call.

ts
generators.toolCall(name: string, args: JsonValue, options?: OutputOptions): Generator
ParameterTypePassingDescription
namestringRequired—
argsJsonValueRequired—
optionsOutputOptionsOptional—
Fields of options
FieldTypeRequiredDescription
finishReason"length" | "tool_calls" | "stop" | "content_filter" | undefinedOptionalFinish reason sent in the last content frame; defaults to tool_calls with tools, else stop.
usageUsage | undefinedOptionalUsage object included in non-stream replies and in the terminal usage chunk.
delayMsnumber | undefinedOptionalArtificial latency before the reply or first frame, in milliseconds; default 0.
chunkSizenumber | undefinedOptionalMaximum Unicode code points per content/tool-call delta; default 24; never splits a code point.
intervalMsnumber | undefinedOptionalDelay between SSE frames in milliseconds; default 0 (emit as fast as the consumer pulls).
wireChunkSizenumber | undefinedOptionalOptional transport-level byte chunks, independent of content/codepoint chunks.
interruptAfternumber | undefinedOptionalError the stream after this many data frames, without a success terminator.

Returns: Generator.

Examples ​

Encode a validated model result ​

Extension · Node.js

The adapter handles request validation, pre-first-byte delay, and lifecycle; the encoder does not execute rules or modify MockGPT call records.

ts
import { encodeModelResponse } from '@jangts/agent-core/mock';
const controller = new AbortController();
const response = encodeModelResponse(
  { model: 'demo', messages: [{ role: 'user', content: '状态' }], stream: false },
  { content: '服务在线。' }, 'mock-1', 1700000000, controller.signal,
  (error, cancelled) => console.log({ error, cancelled }),
);
console.log(await response.json());
js
import { encodeModelResponse } from '@jangts/agent-core/mock';
const controller = new AbortController();
const response = encodeModelResponse({ model: 'demo', messages: [{ role: 'user', content: '状态' }], stream: false }, { content: '服务在线。' }, 'mock-1', 1700000000, controller.signal, (error, cancelled) => console.log({ error, cancelled }));
console.log(await response.json());

Generate an inspectable summary from the input ​

Beginner · Node.js

ts
import { MockGPT, generators } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ generator: generators.summary({ maxChars: 120, count: 2 }) }] });
console.log(await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '服务刚完成维护。健康检查通过。用户可以查询状态。' }] }));
console.log(mock.records);
js
import { MockGPT, generators } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ generator: generators.summary({ maxChars: 120, count: 2 }) }] });
console.log(await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '服务刚完成维护。健康检查通过。用户可以查询状态。' }] }));
console.log(mock.records);

Reuse the same random source across field generators ​

Configuration · Node.js

ts
import { MockGPT, generators, generatorNames } from '@jangts/agent-core/mock';
const id = generators.id('user');
const age = generators.integer(18, 60);
const mock = new MockGPT({ seed: 42, clock: () => 1700000000000, rules: [{
  generator: context => ({ id: id(context), age: age(context) }),
}] });
console.log(await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '生成资料' }] }));
console.log(generatorNames);
js
import { MockGPT, generators, generatorNames } from '@jangts/agent-core/mock';
const id = generators.id('user');
const age = generators.integer(18, 60);
const mock = new MockGPT({ seed: 42, clock: () => 1700000000000, rules: [{
            generator: context => ({ id: id(context), age: age(context) }),
        }] });
console.log(await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '生成资料' }] }));
console.log(generatorNames);

API reference, usage examples and developer guides.