synthex
Version:
Type-safe LLM response simulation with streaming & error injection
105 lines (78 loc) • 2.71 kB
Markdown
Synthex is a type-safe, streaming-friendly mock data generator for LLM and structured API workflows. It helps you test, prototype, and validate your code without waiting for slow LLMs or burning API tokens.
Schemas define the structure and constraints of your mock data. Use the `s` or `m` builder API:
```ts
import { s, MockGenerator } from 'synthex';
const userSchema = s.object({
id: s.uuid().required(),
name: s.string().min(2).max(50).required(),
email: s.email().required(),
age: s.number().min(18).max(99),
isActive: s.boolean(),
role: s.enum(['user', 'admin', 'moderator']),
profile: s.object({
bio: s.string().max(160),
website: s.url(),
}).optional(),
}).build('UserSchema');
```
```ts
const generator = new MockGenerator({ seed: 42 });
const mock = generator.generate(userSchema);
console.log(mock.data);
```
```ts
for await (const chunk of generator.streamGenerate(userSchema, 2, 100)) {
console.log('Chunk:', chunk);
}
```
```ts
const schemaWithError = s.object({
field: s.string().simulateError(true).errorType('SimulatedError'),
}).build('ErrorSchema');
try {
const result = generator.generate(schemaWithError);
console.log(result.data);
} catch (err) {
console.error('Simulated error:', err);
}
```
```ts
const schema = s.object({
always: s.string().required(),
maybe: s.string().probability(0.3),
onlyIf: s.string().when('always', 'yes'),
}).build('ConditionalSchema');
```
```ts
const customSchema = s.object({
custom: s.string().generate((ctx, data, rng) => `custom-${rng.randomInt(1, 100)}`),
}).build('CustomSchema');
```
```ts
import { TypeInference, SchemaUtils } from 'synthex';
console.log(TypeInference.inferType(userSchema));
const isValid = SchemaUtils.validateData({ id: '...', name: '...' }, userSchema);
```
- `node examples/form-demo.cjs` — Interactive form mocker
- `node examples/llm-demo.cjs` — LLM-style streaming and error demo
See the source code or TypeScript types for full API details. Key exports:
- `MockGenerator`
- `SchemaForm`, `SchemaField`
- `s` / `m` (schema builders)
- `TypeInference`, `SchemaUtils`, `DocGenerator`
- Use `SchemaUtils.createCollection` for managing multiple schemas
- Use `s.reference('OtherSchema')` for nested/linked schemas
- Use `SnapshotUtils` for snapshot testing
For more, see the README and example scripts.