Skip to content
PricingBlog
✨ Markdown

EntityDefinition

Defines an entity type's schema, state, and handler. Passed to registry.define() or defineEntity().

Source: @electric-ax/agents-runtime

ts
interface EntityDefinition {
  description?: string
  state?: Record<string, CollectionDefinition>
  actions?: (
    collections: Record<string, unknown>
  ) => Record<string, (...args: unknown[]) => void>
  creationSchema?: StandardJSONSchemaV1
  inboxSchemas?: Record<string, StandardJSONSchemaV1>
  stateSchemas?: Record<string, StandardJSONSchemaV1>
  permissionGrants?: EntityTypePermissionGrantDefinition[]
  slashCommands?: SlashCommandDefinition[]
  handler(ctx: HandlerContext, wake: WakeEvent): void | Promise<void>
}

Fields

FieldTypeRequiredDescription
descriptionstringNoHuman-readable description of the entity type. Used in type registration.
stateRecord<string, CollectionDefinition>NoCustom state collections exposed via ctx.db.actions (writes) and ctx.db.collections (reads).
actions(collections) => Record<string, (...args) => void>NoFactory for custom non-CRUD actions. Receives TanStack DB collections, returns named action functions exposed on ctx.actions.
creationSchemaStandardJSONSchemaV1NoJSON Schema for spawn arguments validation.
inboxSchemasRecord<string, StandardJSONSchemaV1>NoJSON Schemas for inbound message types, keyed by message type.
stateSchemasRecord<string, StandardJSONSchemaV1>NoAdditional JSON Schemas included in the registered entity type's state schema map.
permissionGrantsEntityTypePermissionGrantDefinition[]NoInitial permission grants applied when this entity type is registered.
slashCommandsSlashCommandDefinition[]NoStatic slash commands exposed to structured composers and available through ctx.slashCommands.
handler(ctx, wake) => void | Promise<void>YesThe function invoked on each wake. Receives HandlerContext and WakeEvent.

CollectionDefinition

Defines a custom state collection.

ts
interface CollectionDefinition {
  schema?: StandardSchemaV1
  type?: string
  primaryKey?: string
  externallyWritable?: boolean
  contract?: string
  operations?: Array<"insert" | "update" | "delete">
}
FieldTypeDefaultDescription
schemaStandardSchemaV1-Zod or Standard Schema validator for the row type.
typestring"state:{name}"Event type string used in the durable stream.
primaryKeystring"key"Primary key field name on the row.
externallyWritablebooleanfalseOpt in to HTTP writes for this collection.
contractstring-Well-known contract implemented by the collection.
operationsArray<"insert" | "update" | "delete">["insert"] for external writesAllowlist of external write operations when externallyWritable is enabled.

Permission grants

permissionGrants lets an entity type declare the initial access grants that the server stores for entities of that type.

ts
registry.define("worker", {
  description: "Internal worker agent",
  permissionGrants: [
    {
      subject_kind: "principal_kind",
      subject_value: "user",
      permission: "spawn",
    },
  ],
  async handler(ctx, wake) {
    // ...
  },
})

The server currently recognizes read, write, delete, signal, fork, schedule, spawn, and manage permissions. Grants can target a specific principal or a principal kind, and may include propagation options depending on the server route that creates them.