generators
KIT · constant · 运行时值
导入
import { generators } from '@jangts/agent-core/mock';import { generators } from '@jangts/agent-core/mock';其他公开入口:@jangts/agent-core/mock。
定义
成员目录
| 成员 | 用途 |
|---|---|
summary | Extractive summary, one paragraph, at most maxChars (default 240) characters. |
bullets | Same selection as summary, rendered as - bullet lines (default budget 500). |
keywords | Most frequent non-stopword terms, most frequent first. |
title | First sentence of the source with leading Markdown heading marks removed. |
reply | Short conversational answer that echoes the topic. Continuation prompts ("继续", "换个说法") reuse the previous user message so a demo conversation stays coherent without a model. |
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. |
integer | Uniform integer in an inclusive range. |
float | Continuous value in [min, max), or exactly min when both bounds are equal. |
boolean | Fair coin derived from the call PRNG. |
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. |
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. |
date | ISO-8601 timestamp read from the injected clock. With a fixed clock option the value is constant, which keeps snapshot-style tests stable. |
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. |
imageUrl | Inline SVG data URL, so no network or filesystem access is needed. |
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. |
toolCall | Shorthand for a reply that only emits one tool call. |
成员详情
summary
Extractive summary, one paragraph, at most maxChars (default 240) characters.
generators.summary(options?: TextOptions): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | 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).
generators.bullets(options?: TextOptions): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | 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.
generators.keywords(options?: TextOptions): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | 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.
generators.title(options?: TextOptions): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | 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.
generators.reply(options?: TextOptions & { template?: (input: { text: string; previous: string; }) => string; }): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions & { template?: (input: { text: string; previous: string; }) => string; } | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | 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.
generators.extract(options?: TextOptions & { fields?: Record<string, RegExp>; }): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | TextOptions & { fields?: Record<string, RegExp>; } | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
source | TextSource | undefined | 可省略 | Explicit text or async provider; defaults to the request prompt. |
maxChars | number | undefined | 可省略 | Maximum characters in the returned text (1-30 000); generator-specific default. |
count | number | undefined | 可省略 | How many items to select (1-100), e.g. bullets, keywords or summary sentences. |
fields | Record<string, RegExp> | undefined | 可省略 | 见类型定义及调用场景。 |
返回: Generator。
integer
Uniform integer in an inclusive range.
generators.integer(min?: number, max?: number): ValueGenerator<number>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
min | number | 可省略 | — |
max | number | 可省略 | — |
返回: ValueGenerator<number>。
float
Continuous value in [min, max), or exactly min when both bounds are equal.
generators.float(min?: number, max?: number): ValueGenerator<number>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
min | number | 可省略 | — |
max | number | 可省略 | — |
返回: ValueGenerator<number>。
boolean
Fair coin derived from the call PRNG.
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.
generators.pick<T extends JsonValue>(items: readonly T[]): ValueGenerator<T>| 类型参数 | 约束 | 默认类型 |
|---|---|---|
T | JsonValue | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
items | readonly 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.
generators.id(prefix?: string): ValueGenerator<string>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
prefix | string | 可省略 | — |
返回: 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.
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.
generators.sentence(): ValueGenerator<string>无调用参数。
返回: ValueGenerator<string>。
imageUrl
Inline SVG data URL, so no network or filesystem access is needed.
generators.imageUrl({ width, height, }?: { width?: number; height?: number; }): ValueGenerator<string>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
__0 | { width?: number; height?: number; } | 可省略 | — |
__0 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
width | number | undefined | 可省略 | 见类型定义及调用场景。 |
height | number | 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.
generators.sequence(items: readonly Generator[]): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
items | readonly Generator[] | 必需 | — |
返回: Generator。
toolCall
Shorthand for a reply that only emits one tool call.
generators.toolCall(name: string, args: JsonValue, options?: OutputOptions): Generator| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
name | string | 必需 | — |
args | JsonValue | 必需 | — |
options | OutputOptions | 可省略 | — |
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. |
usage | Usage | undefined | 可省略 | Usage object included in non-stream replies and in the terminal usage chunk. |
delayMs | number | undefined | 可省略 | Artificial latency before the reply or first frame, in milliseconds; default 0. |
chunkSize | number | undefined | 可省略 | Maximum Unicode code points per content/tool-call delta; default 24; never splits a code point. |
intervalMs | number | undefined | 可省略 | Delay between SSE frames in milliseconds; default 0 (emit as fast as the consumer pulls). |
wireChunkSize | number | undefined | 可省略 | Optional transport-level byte chunks, independent of content/codepoint chunks. |
interruptAfter | number | undefined | 可省略 | Error the stream after this many data frames, without a success terminator. |
返回: Generator。
使用示例
- MockGPT:响应描述与场景生成器:固定结果用于严格回归,输入相关生成器用于探索接线,显式响应描述用于故障和流式场景。
编码已校验的模型结果
扩展 · Node.js
适配器负责请求校验、首包前延迟和生命周期;编码器不执行规则或修改 MockGPT 调用记录。
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());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
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);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
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);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);