Event Model
The CloudEvents-style envelope and full subject catalog Anvio uses for internal event-driven communication.
4 min read
Anvio's runtime, channels, and Advanced Agent OS components communicate through a single, uniform event model — whether events travel over the in-process Local Event Bus (Level 1–2, default) or NATS JetStream (Level 3+). The envelope shape and subject names never change between the two — only the transport does.
Envelope: CloudEvents 1.0
Every event is wrapped in an AnvioEvent<T>, defined in packages/events/src/types.ts:
export interface AnvioEvent<T = unknown> {
specversion: '1.0';
type: string;
source: string;
id: string;
time: string;
subject?: string;
data: T;
}This matches the CloudEvents 1.0 spec shape (specversion, type, source, id, time, data), with subject used to carry the NATS/JetStream subject string separately from type (which is a shorter, human-readable event name like anvio.session.started).
Constructing an event (packages/events/src/envelope.ts):
export function createEvent<T>(type: string, source: string, data: T, subject?: string): AnvioEvent<T> {
return {
specversion: '1.0',
type,
source,
id: uuidv4(), // random UUID per event
time: new Date().toISOString(),
subject,
data,
};
}Events are serialized to Uint8Array via JSON.stringify / TextEncoder for transport (serializeEvent / deserializeEvent) — this matters for NATS (binary payloads) but is a no-op conceptually for the local bus.
Subject naming: anvio.<domain>.<action>.v1
Every subject is a versioned, dot-namespaced string defined once in EventSubjects (packages/events/src/types.ts) and referenced everywhere else by that constant — never as a raw string literal in application code. As of this writing there are 29 subjects across 8 domains:
| Domain | Subjects |
|---|---|
| Session | anvio.session.started.v1, anvio.session.ended.v1 |
| Agent run | anvio.agent.run.requested.v1, anvio.agent.run.completed.v1, anvio.agent.run.failed.v1, anvio.agent.run.chunk.v1, anvio.agent.run.progress.v1, anvio.agent.run.stop.v1 |
| Agent inbox | anvio.agent.inbox.injected.v1 |
| Approval | anvio.approval.requested.v1, anvio.approval.decided.v1 |
| Notification | anvio.notification.sent.v1 |
| Tool / memory / artifact | anvio.tool.executed.v1, anvio.memory.stored.v1, anvio.artifact.created.v1 |
| Orchestration | anvio.orchestration.started.v1, anvio.orchestration.completed.v1 |
| Goals (Advanced Agent OS) | anvio.goal.created.v1, anvio.goal.progress.updated.v1, anvio.goal.completed.v1, anvio.goal.paused.v1, anvio.goal.resumed.v1 |
| Soul | anvio.soul.updated.v1 |
| Automation | anvio.automation.started.v1, anvio.automation.completed.v1, anvio.automation.failed.v1, anvio.workflow.completed.v1 |
| Task delegation | anvio.task.assigned.v1, anvio.task.completed.v1, anvio.delegation.task.started.v1, anvio.delegation.task.completed.v1, anvio.delegation.task.failed.v1 |
// packages/events/src/types.ts (excerpt)
export const EventSubjects = {
SESSION_STARTED: 'anvio.session.started.v1',
AGENT_RUN_REQUESTED: 'anvio.agent.run.requested.v1',
AGENT_RUN_COMPLETED: 'anvio.agent.run.completed.v1',
AGENT_RUN_CHUNK: 'anvio.agent.run.chunk.v1',
TOOL_EXECUTED: 'anvio.tool.executed.v1',
MEMORY_STORED: 'anvio.memory.stored.v1',
// ...29 total
} as const;The .v1 suffix is a real versioning convention, not decoration — a breaking change to an event's data shape should ship as a new subject (.v2), not a silent mutation of .v1, so existing subscribers (durable NATS consumers, in particular) don't silently break.
The core request/response flow
The most important event pair to understand is AGENT_RUN_REQUESTED → AGENT_RUN_CHUNK* → AGENT_RUN_COMPLETED (or AGENT_RUN_FAILED), since it's how both the REST API and every channel adapter actually trigger and observe agent execution — see REST APIs and Channel Adapters.
SessionsController.sendMessage (apps/api/src/sessions.controller.ts) is a concrete example of the publish side:
await eventBus.publish(EventSubjects.AGENT_RUN_REQUESTED, 'anvio.agent.run.requested', {
sessionId: session.id,
userId: ctx.userId,
agentId: session.agentName,
content: body.content,
channel: session.channel,
detached: session.detached,
});Event payload reference
Selected data shapes from packages/events/src/types.ts (not exhaustive — see source for all 20 interfaces):
export interface SessionStartedData {
sessionId: string; userId: string; agentId: string; channel: string;
detached?: boolean; channelThreadId?: string;
}
export interface AgentRunRequestedData {
sessionId: string; userId: string; agentId: string; content: string;
channel: string; detached?: boolean; resumeApproval?: ApprovalDecidedData;
}
export interface AgentRunCompletedData {
sessionId: string; content: string;
usage: { inputTokens: number; outputTokens: number; totalTokens: number };
status: string; channel: string;
}
export interface AgentRunFailedData {
sessionId: string; error: string; channel: string;
}
export interface ApprovalRequestedData {
sessionId: string; requestId: string; toolName: string; reason: string; channel: string;
}Two publish paths: durable vs fire-and-forget
The NATS-backed EventBus (packages/events/src/event-bus.ts) exposes two publish methods with different durability guarantees — this distinction doesn't exist on the Local Event Bus, where everything is effectively synchronous and in-memory:
| Method | Transport | Durability |
|---|---|---|
publish() | JetStream (js.publish) | Persisted to the ANVIO stream; durable consumers replay on reconnect |
publishCore() | Core NATS (nc.publish) | Fire-and-forget, no persistence — used for high-frequency real-time streaming like AGENT_RUN_CHUNK |
See NATS JetStream for the full transport-level detail.
Last updated on