Skip to content

Mock LLM Responder — Go source

Generate deterministic mock LLM API responses - chat completion JSON, SSE event streams with chunk timing, and a replay curl - for testing clients without an API key.

This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.

// mock-llm-responder — the authoritative Go source is the live CLI twin.
//
// This tool's Go implementation ships in the `cosmodev` CLI as
// cli/mock-llm-responder/mockllmresponder.go (package mockllmresponder): a
// pure, deterministic port of src/lib/mockLlmResponder.ts held in lock-step
// with the TypeScript lib by shared test vectors
// (mockllmresponder_test.go ↔ src/lib/mockLlmResponder.test.ts). Same spec
// in, same bytes out — no clock, no unseeded randomness; the only entropy is
// a mulberry32 PRNG seeded from an FNV-1a hash of the spec.
//
// Core entry points, repeated here for orientation (see the twin for the
// full surface — TokenCount, TruncateToTokens, NormalizeSpec, BuildContent,
// BuildCompletion, ChunkContent, BuildSse, BuildCurl):
//
//	// BuildCompletion returns the OpenAI-style chat completion (or the
//	// 429/500 error envelope) for the spec — a pure function of the spec.
//	func BuildCompletion(spec MockSpec) MockResponse
//
//	// BuildSse renders the SSE event stream: `data:` lines, timing
//	// comment markers, [DONE].
//	func BuildSse(spec MockSpec) string
//
// Run the twin's tests from the repo root:
//
//	go test ./cli/mock-llm-responder/
package mockllmresponder

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →