encodeModelResponse
KIT · function · Runtime value
Optional app-owned codec; validation and encoding consume the same snapshots.
Thin public wrapper over encodeModel: re-validates identity arguments and re-snapshots both the request and the model output, so callers that build replies themselves get identical validation and cannot mutate the mock's state afterwards.
Import
import { encodeModelResponse } from '@jangts/agent-core/mock';import { encodeModelResponse } from '@jangts/agent-core/mock';Other public entry points:@jangts/agent-core/mock.
Definition
Call
encodeModelResponse(request: ModelRequest, result: ModelOutput, id: string, created: number, signal: AbortSignal, finish: (error?: unknown, cancelled?: boolean) => void): ResponseOptional app-owned codec; validation and encoding consume the same snapshots.
Thin public wrapper over encodeModel: re-validates identity arguments and re-snapshots both the request and the model output, so callers that build replies themselves get identical validation and cannot mutate the mock's state afterwards.
| Parameter | Type | Passing | Description |
|---|---|---|---|
request | ModelRequest | Required | — |
result | ModelOutput | Required | — |
id | string | Required | — |
created | number | Required | — |
signal | AbortSignal | Required | — |
finish | (error?: unknown, cancelled?: boolean) => void | Required | — |
Fields of request
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Required | Read-only. Model identifier; required, non-empty, and compared verbatim by Rule.model. |
messages | readonly Readonly<{ [key: string]: unknown; role: string; content?: unknown; tool_calls?: unknown; tool_call_id?: string; }>[] | Required | Read-only. Non-empty conversation; the last user message supplies the default prompt. |
stream | boolean | Required | Read-only. Whether the codec should emit SSE; defaulted to false during normalization. |
tools | readonly unknown[] | undefined | Optional | Read-only. Tool schemas declared by the client; only the array shape is validated. |
stream_options | Readonly<{ include_usage?: boolean; }> | undefined | Optional | Read-only. include_usage: true appends a terminal usage-only chunk to the stream. |
Fields of result
| Field | Type | Required | Description |
|---|---|---|---|
content | string | null | undefined | Optional | Assistant text; null or omitted means no textual content (common for tool-call replies). |
tools | readonly ToolCall[] | undefined | Optional | Tool calls emitted by the assistant; their serialized size also counts toward limits. |
finishReason | "length" | "tool_calls" | "stop" | "content_filter" | undefined | Optional | Finish reason sent in the last content frame; defaults to tool_calls with tools, else stop. |
usage | Usage | undefined | Optional | Usage object included in non-stream replies and in the terminal usage chunk. |
delayMs | number | undefined | Optional | Artificial latency before the reply or first frame, in milliseconds; default 0. |
chunkSize | number | undefined | Optional | Maximum Unicode code points per content/tool-call delta; default 24; never splits a code point. |
intervalMs | number | undefined | Optional | Delay between SSE frames in milliseconds; default 0 (emit as fast as the consumer pulls). |
wireChunkSize | number | undefined | Optional | Optional transport-level byte chunks, independent of content/codepoint chunks. |
interruptAfter | number | undefined | Optional | Error the stream after this many data frames, without a success terminator. |
Returns: Response.
Examples
- MockGPT: response descriptions and scenario generators: Fixed results serve strict regression, input-dependent generators explore wiring, and explicit response descriptions cover failure and streaming scenarios.
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.
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());Generate an inspectable summary from the input
Beginner · 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);Reuse the same random source across field generators
Configuration · 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);