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 duringagent.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 identifierservices
: Array of Service Provider definitions this extension requirescontexts
: Definitions for specific Context typesactions
: Action definitionsinputs
: Input definitionsoutputs
: Output definitionsevents
: Zod schemas for events the extension might emitinstall(agent)
: Optional hook that runs once when the extension is added duringagent.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
Feature | Service (Provider) | Extension |
---|---|---|
Primary Focus | Dependency Injection & Lifecycle | Bundling & Organizing Features |
Defined By | service({...}) | extension({...}) |
Contains | register , boot methods | actions , contexts , inputs , outputs , services , install , etc. |
Purpose | Manages how a dependency is provided | Packages what features are provided |
Relationship | Often included within an Extension | Uses 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 theactions
,contexts
,inputs
, andoutputs
related to a feature, and you list theservices
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.