Inputs
How Daydreams agents receive information and trigger processing.
Inputs are the mechanism by which Daydreams agents receive information from the
outside world. They act as the triggers that initiate or contribute to an
agent's processing cycle (agent.run
). An input could represent a user message,
a blockchain event, an API webhook, a sensor reading, or any other data source
relevant to the agent's task.
Defining an Input
Input sources are defined using the input
helper function exported from
@daydreamsai/core
. Each input definition connects an external data source to
the agent's core processing loop.
Key Parameters:
type
(string): A unique name identifying this input source (e.g., "discord:message", "webhook:github").schema
(Zod Schema, optional): Defines the expected structure of thedata
payload passed to thesend
function withinsubscribe
. This ensures data consistency before it enters the agent's run cycle.subscribe
(Function): This is the core connection point.- It receives a
send
function and theagent
instance. - Its responsibility is to listen to the external source (e.g., setup webhook listeners, connect to websockets, poll APIs, listen to event emitters).
- When new data arrives, it calls
send(context, args, data)
:context
: The targetContext
definition object.args
: An object matching the targetcontext
'sschema
, identifying the specific instance to run.data
: The payload matching thisinput
'sschema
.
- It should return a cleanup function that disconnects from the source when the agent stops.
- It receives a
handler
(Function, optional): A pre-processing step that runs aftersend
is called but before theInputRef
log is finalized and added to theWorkingMemory
. It receives the raw data fromsend
and the targetContextState
. It can transform the data or addparams
to the resultingInputRef
.format
(Function, optional): Customizes the XML representation of theInputRef
log.install
(Function, optional): Logic executed once when the agent starts (e.g., initializing SDKs).enabled
(Function, optional): Dynamically control if this input source is active.context
(Context, optional): Associates this input definition with a specific context (less common for inputs, usually defined globally or in extensions).
Execution Flow
- External Event: An event occurs in the external system connected via
subscribe
. - Listener Triggered: The listener function set up in
subscribe
is called with the raw event data. send
Called: The listener determines the targetcontext
andargs
, formats thedata
payload according to theinput
schema, and callssend(targetContext, contextArgs, inputData)
.agent.send
: Thesend
function (provided by the framework) internally callsagent.send({ context: targetContext, args: contextArgs, input: { type: thisInputType, data: inputData } })
.InputRef
Creation:agent.send
creates the initialInputRef
log object withprocessed: false
.agent.run
Initiated:agent.send
callsagent.run
for the target context instance, passing the newInputRef
in thechain
. (See Agent Lifecycle).handleInput
Called: During the first step of theagent.run
cycle, when theInputRef
is processed byhandlePushLog
, thehandleInput
function (handlers.ts
) is invoked.- Pre-processing & Validation:
handleInput
runs the optionalinput.handler
(if defined) for any final data transformation or addingparams
to theInputRef
. It also validates the finalInputRef.data
againstinput.schema
. - Memory Query:
handleInput
may perform a query against the vector store using the input data to retrieve relevant episodic memories, adding them to theWorkingMemory
. - Processing Continues: The
InputRef
is marked asprocessed: true
(usually), and the agent proceeds with the rest of the step (prompt generation, LLM call, etc.).
Inputs are the entry points for external information, initiating the agent's
perception-reasoning-action cycle. The subscribe
method bridges external
systems with the agent's core logic via the send
function.