Skip to content
English

generators ​

KIT · constant · 运行时值

导入 ​

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

其他公开入口:@jangts/agent-core/mock。

定义 ​

成员目录 ​

成员用途
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.

成员详情 ​

summary ​

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

ts
generators.summary(options?: TextOptions): Generator
参数类型传递说明
optionsTextOptions可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.

返回: Generator。

bullets ​

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

ts
generators.bullets(options?: TextOptions): Generator
参数类型传递说明
optionsTextOptions可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.

返回: Generator。

keywords ​

Most frequent non-stopword terms, most frequent first.

ts
generators.keywords(options?: TextOptions): Generator
参数类型传递说明
optionsTextOptions可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.

返回: Generator。

title ​

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

ts
generators.title(options?: TextOptions): Generator
参数类型传递说明
optionsTextOptions可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.

返回: 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
参数类型传递说明
optionsTextOptions & { template?: (input: { text: string; previous: string; }) => string; }可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.
template((input: { text: string; previous: string; }) => string) | undefined可省略见类型定义及调用场景。

返回: 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
参数类型传递说明
optionsTextOptions & { fields?: Record<string, RegExp>; }可省略—
options 的字段
字段类型必需说明
sourceTextSource | undefined可省略Explicit text or async provider; defaults to the request prompt.
maxCharsnumber | undefined可省略Maximum characters in the returned text (1-30 000); generator-specific default.
countnumber | undefined可省略How many items to select (1-100), e.g. bullets, keywords or summary sentences.
fieldsRecord<string, RegExp> | undefined可省略见类型定义及调用场景。

返回: Generator。

integer ​

Uniform integer in an inclusive range.

ts
generators.integer(min?: number, max?: number): ValueGenerator<number>
参数类型传递说明
minnumber可省略—
maxnumber可省略—

返回: 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>
参数类型传递说明
minnumber可省略—
maxnumber可省略—

返回: ValueGenerator<number>。

boolean ​

Fair coin derived from the call PRNG.

ts
generators.boolean(): ValueGenerator<boolean>

无调用参数。

返回: 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>
类型参数约束默认类型
TJsonValue—
参数类型传递说明
itemsreadonly T[]必需—

返回: 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>
参数类型传递说明
prefixstring可省略—

返回: 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>

无调用参数。

返回: 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>

无调用参数。

返回: 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>
参数类型传递说明
__0{ width?: number; height?: number; }可省略—
__0 的字段
字段类型必需说明
widthnumber | undefined可省略见类型定义及调用场景。
heightnumber | undefined可省略见类型定义及调用场景。

返回: 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
参数类型传递说明
itemsreadonly Generator[]必需—

返回: Generator。

toolCall ​

Shorthand for a reply that only emits one tool call.

ts
generators.toolCall(name: string, args: JsonValue, options?: OutputOptions): Generator
参数类型传递说明
namestring必需—
argsJsonValue必需—
optionsOutputOptions可省略—
options 的字段
字段类型必需说明
finishReason"length" | "tool_calls" | "stop" | "content_filter" | undefined可省略Finish reason sent in the last content frame; defaults to tool_calls with tools, else stop.
usageUsage | undefined可省略Usage object included in non-stream replies and in the terminal usage chunk.
delayMsnumber | undefined可省略Artificial latency before the reply or first frame, in milliseconds; default 0.
chunkSizenumber | undefined可省略Maximum Unicode code points per content/tool-call delta; default 24; never splits a code point.
intervalMsnumber | undefined可省略Delay between SSE frames in milliseconds; default 0 (emit as fast as the consumer pulls).
wireChunkSizenumber | undefined可省略Optional transport-level byte chunks, independent of content/codepoint chunks.
interruptAfternumber | undefined可省略Error the stream after this many data frames, without a success terminator.

返回: Generator。

使用示例 ​

编码已校验的模型结果 ​

扩展 · Node.js

适配器负责请求校验、首包前延迟和生命周期;编码器不执行规则或修改 MockGPT 调用记录。

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());

从输入生成可检查的摘要 ​

入门 · 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);

字段生成器复用同一随机源 ​

配置 · 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);

相关定义 ​

接口参考、使用示例与开发指南。