Skip to content
English

GenerationContext ​

KIT · interface · 类型合同

Everything a rule or generator may read for one accepted call.

The context is frozen; the only mutable side channel is report, and only while the call is still pending.

导入 ​

ts
import type { GenerationContext } from '@jangts/agent-core/mock';
js
/** @typedef {import('@jangts/agent-core/mock').GenerationContext} GenerationContext */

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

定义 ​

ts
interface GenerationContext

字段结构 ​

字段类型必需说明
requestModelRequest必需只读。Frozen snapshot of the normalized request that matched.
promptstring必需只读。Prompt selected by selectPrompt; default is the text of the last user message.
matchRegExpMatchArray | null必需只读。Match of the matched rule's regular expression, or null for string/fallback rules.
callIndexnumber必需只读。1-based sequence number of this call across the instance lifetime; reset by reset.
ruleIndexnumber必需只读。1-based hit count of the matched rule, or the fallback hit count for fallback calls.
signalAbortSignal必需只读。Signal for the current call; also aborts when the transport is cancelled.
random() => number必需只读。Per-call deterministic PRNG in [0, 1), derived from the instance seed and call order.
clock() => number必需只读。Millisecond timestamp from the configured clock; throws if the clock is not synchronous.
report(metadata: Record<string, JsonValue>) => void必需只读。Bounded diagnostic data; never inserted into model content.

成员目录 ​

成员用途
randomPer-call deterministic PRNG in [0, 1), derived from the instance seed and call order.
clockMillisecond timestamp from the configured clock; throws if the clock is not synchronous.
reportBounded diagnostic data; never inserted into model content.

成员详情 ​

random ​

Per-call deterministic PRNG in [0, 1), derived from the instance seed and call order.

ts
GenerationContext.random(): number

无调用参数。

返回: number。

clock ​

Millisecond timestamp from the configured clock; throws if the clock is not synchronous.

ts
GenerationContext.clock(): number

无调用参数。

返回: number。

report ​

Bounded diagnostic data; never inserted into model content.

ts
GenerationContext.report(metadata: Record<string, JsonValue>): void
参数类型传递说明
metadataRecord<string, JsonValue>必需—

返回: void。

使用示例 ​

编码已校验的模型结果 ​

扩展 · 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);

相关定义 ​

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