Skip to content
中文

MockGPT: direct generation and Provider injection ​

Get a string or JSON directly; inject the same instance into a Provider when you need to exercise model communication.

Import from @jangts/agent-core/mock; no separate simulator installation is needed. new MockGPT({rules,fallback?,seed?,clock?,recordLimit?,maxBodyBytes?,maxOutputChars?,stream?,selectPrompt?}) fixes the configuration. Rules match in order on prompt, model, and the synchronous when, while times limits the hit count; strings match exactly, and RegExp can capture groups. Function generators receive a frozen request, prompt, match, signal, callIndex, ruleIndex, random, clock, and report.

await mock.generate({model,messages,...fields},{signal?}) returns an independent frozen snapshot of a string, native JSON, or an explicit ResponseDescription. A direct call creates no HTTP objects and does not execute the description's delay, SSE, or HTTP failure; the caller interprets the description. Optional object fields with undefined are omitted on input, while empty array slots, undefined array entries, circular references, and non-JSON data are rejected; an output undefined is not omitted. JSON snapshots are limited to 64 levels and 100000 value nodes; exceeding request limits yields MOCK_REQUEST (400), and exceeding generation limits yields MOCK_GENERATION. Fields are read once, hidden toJSON is not executed, and prototype names are kept as ordinary own data fields. Failure or cancellation does not return the allocated rule hit count.

mock.fetch is a bound standard fetch adapter that can be injected into Support.CompatibleModelProvider. routes declares /v1/chat/completions and /chat/completions; it accepts POST only and supports JSON and stream=true SSE with one choice. It does not intercept requests globally and does not forward to a real model.

records is a bounded in-memory snapshot of metadata, 100 by default and disabled with recordLimit=0; counts is rule hit counts, and activeRequests includes unconsumed SSE. reset is rejected while active requests exist; when idle it clears records, counts, and seed without resetting custom closures. A fixed seed and clock suit deterministic tests and do not represent real model quality, latency, or capacity. Errors are distinguished by MockGPTError.code and details: configuration, request, no match, output, cancellation, and transport boundaries.

Progressive examples for the same business case ​

Generate native JSON directly ​

Beginner · Node.js

ts
import { MockGPT } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ prompt: '服务状态', generator: { online: true, source: 'fixture' } }] });
const value = await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '服务状态' }] });
console.log(value); // { online: true, source: 'fixture' }
console.log(mock.counts, mock.records);
js
import { MockGPT } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ prompt: '服务状态', generator: { online: true, source: 'fixture' } }] });
const value = await mock.generate({ model: 'demo', messages: [{ role: 'user', content: '服务状态' }] });
console.log(value); // { online: true, source: 'fixture' }
console.log(mock.counts, mock.records);

Inject the same instance into the real Core model boundary ​

Configuration · Node.js

ts
import { ModelRunner, Support } from '@jangts/agent-core';
import { MockGPT, responses } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ prompt: '服务状态', generator: responses.text('服务在线。', { chunkSize: 2, wireChunkSize: 3 }) }] });
const provider = new Support.CompatibleModelProvider({
  config: { baseUrl: 'https://mock.local/v1', apiKey: 'offline-example', model: 'demo' },
  ports: { request: mock.fetch },
});
const result = await new ModelRunner({ provider, maxCalls: 1 }).invoke('服务状态');
console.log(result.value, result.invocation.status);
js
import { ModelRunner, Support } from '@jangts/agent-core';
import { MockGPT, responses } from '@jangts/agent-core/mock';
const mock = new MockGPT({ rules: [{ prompt: '服务状态', generator: responses.text('服务在线。', { chunkSize: 2, wireChunkSize: 3 }) }] });
const provider = new Support.CompatibleModelProvider({
    config: { baseUrl: 'https://mock.local/v1', apiKey: 'offline-example', model: 'demo' },
    ports: { request: mock.fetch },
});
const result = await new ModelRunner({ provider, maxCalls: 1 }).invoke('服务状态');
console.log(result.value, result.invocation.status);

API reference, usage examples and developer guides.