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字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
request | ModelRequest | 必需 | 只读。Frozen snapshot of the normalized request that matched. |
prompt | string | 必需 | 只读。Prompt selected by selectPrompt; default is the text of the last user message. |
match | RegExpMatchArray | null | 必需 | 只读。Match of the matched rule's regular expression, or null for string/fallback rules. |
callIndex | number | 必需 | 只读。1-based sequence number of this call across the instance lifetime; reset by reset. |
ruleIndex | number | 必需 | 只读。1-based hit count of the matched rule, or the fallback hit count for fallback calls. |
signal | AbortSignal | 必需 | 只读。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. |
成员目录
| 成员 | 用途 |
|---|---|
random | Per-call deterministic PRNG in [0, 1), derived from the instance seed and call order. |
clock | Millisecond timestamp from the configured clock; throws if the clock is not synchronous. |
report | Bounded 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| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
metadata | Record<string, JsonValue> | 必需 | — |
返回: void。
使用示例
- MockGPT:响应描述与场景生成器:固定结果用于严格回归,输入相关生成器用于探索接线,显式响应描述用于故障和流式场景。
- MockGPT Web:可信来源与本地摘要:从真实网页或可信 load 取得正文,再交给输入相关生成器。
编码已校验的模型结果
扩展 · 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);