Daydreams Logo
Advanced

Extensions vs Services

Understanding the difference between extensions and services in Daydreams.

Overview

The Daydreams framework uses both Services and Extensions as core architectural components, but they serve different purposes. Let's explore the differences:

Service (Provider)

Services are primarily focused on Dependency Injection (DI) and Lifecycle Management for specific functionality, often managing external clients or shared utilities.

  • Definition: Created using service({...})
  • Key Methods:
    • register(container): Adds instances or factories to the DI container. This runs before booting.
    • boot(container): Performs asynchronous initialization after all services have been registered. This runs during agent.start().
  • Role: Manages how a dependency is created, configured, initialized, and made available via the container.
  • Analogy: Think of a service provider as the blueprint and setup instructions for a specific tool or utility.

Extension

Extensions act as modular packages or bundles that group related Daydreams features together for easy integration into an agent.

  • Definition: Created using extension({...})
  • Contents:
    • name (required): Unique identifier
    • services: Array of Service Provider definitions this extension requires
    • contexts: Definitions for specific Context types
    • actions: Action definitions
    • inputs: Input definitions
    • outputs: Output definitions
    • events: Zod schemas for events the extension might emit
    • install(agent): Optional hook that runs once when the extension is added during agent.start()
  • Role: Organizes and encapsulates a set of related functionalities and declares the services those functionalities need.
  • Analogy: Think of an extension as a toolbox for a specific domain (e.g., a "Discord Toolbox").

Key Differences

FeatureService (Provider)Extension
Primary FocusDependency Injection & LifecycleBundling & Organizing Features
Defined Byservice({...})extension({...})
Containsregister, boot methodsactions, contexts, inputs, outputs, services, install, etc.
PurposeManages how a dependency is providedPackages what features are provided
RelationshipOften included within an ExtensionUses Services to manage its dependencies

In Simple Terms

  • You define a service to tell the system how to create and initialize something like an API client.
  • You define an extension to bundle together all the actions, contexts, inputs, and outputs related to a feature, and you list the services that those features depend on.

When you add an extension to your agent via createDreams, the framework automatically registers the services listed within that extension, making them available via the container for the extension's components to use.

On this page