> Documentation index for m4trix: https://m4trix.dev/llms.txt. Every page is available as Markdown by appending `.md` to its URL.

# Types / Advanced Generics

Source: https://m4trix.dev/docs/api-reference/types-advanced

m4trix provides full TypeScript inference from event schemas through agent logic to HTTP responses.

## Event Inference

When you define events with `AgentNetworkEvent.of()`, the payload type is inferred from the Effect schema:

```ts
const req = AgentNetworkEvent.of('req', S.Struct({ query: S.String }));
// req payload type: { query: string }
```

## Agent Logic Inference

In `.logic()`, `triggerEvent` is a union of all `listensTo` event envelopes:

```ts
AgentFactory.run()
  .listensTo([eventA, eventB])
  .emits([outEvent])
  .logic(async ({ triggerEvent, emit }) => {
    // triggerEvent: { name: 'a', meta, payload: { x: number } }
    //             | { name: 'b', meta, payload: { y: string } }
    if (triggerEvent.name === 'a') {
      emit({ name: 'out', payload: { result: String(triggerEvent.payload.x) } });
    } else {
      emit({ name: 'out', payload: { result: triggerEvent.payload.y } });
    }
  });
```

## Emit Typing

The `emit()` function only accepts events declared in `.emits()`. TypeScript will error if you emit an undeclared event or wrong payload shape. The same check runs at runtime (payloads built from model output bypass the types): an undeclared event or a payload that fails the schema throws `EmitValidationError` and fails the agent's invocation. See [`.emits()`](https://m4trix.dev/docs/api-reference/agent-factory.md#emitsevents).

## Channel Names

Channel names use a branded type (`ChannelName`) and must be kebab-case. This is enforced at runtime.

## Effect Integration

m4trix uses [Effect](https://effect.website/) for schema validation and concurrency. The event plane runs inside an Effect scope. For advanced use, you can compose with Effect's `Effect` type and use `.makeEffect()` / `.makeBoundEffect()` for event creation.
