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 Concepts:
type
: A unique string identifying your input source.schema
: (Recommended) A Zod schema defining the structure of the data payload you intend to pass viasend
. Validation happens automatically if provided.subscribe(send, agent)
: The core function where you connect to your external data source (API, websocket, event emitter, polling mechanism, etc.).- When data arrives, you call the provided
send
function. - You must return a cleanup function to disconnect/unsubscribe when the agent stops.
- When data arrives, you call the provided
send(context, args, data)
: This framework-provided function is your gateway into the agent.context
: The context definition object that should handle this input.args
: An object matching thecontext.schema
, identifying the specific instance of the context.data
: The payload containing the actual input information, ideally matching thisinput
'sschema
.
handler(data, ctx, agent)
: (Optional) Pre-processing function executed aftersend
but before the input log entry is finalized. Allows data transformation or adding metadata (params
) based on the target context's state (ctx
).format(ref)
: (Optional) Customize the string/XML representation of the input log entry (InputRef
).
How Inputs Trigger the Agent
- External Event: Your
subscribe
function detects an event or receives data. send
Called: Your code callssend(context, args, data)
.- Agent Invoked: The framework receives the call, creates a basic log
entry (
InputRef
) for this input, and starts or queues anagent.run
cycle for the specifiedcontext
instance identified byargs
. - Pre-processing (
handler
): If you defined aninput.handler
, it runs, potentially modifying the data or adding parameters to theInputRef
. - Run Cycle: The agent proceeds with its run cycle
(Agent Lifecycle), processing this new
InputRef
along with other state information to generate a response or perform actions.
Inputs within Extensions
Inputs are commonly bundled within Extensions to
package integrations cleanly. The structure is the same, but the input
definitions live inside the inputs
property of the extension
definition.
Examples
(Keep the existing CLI, Telegram, and Twitter examples here, perhaps slightly
simplifying the code snippets to focus on the subscribe
and send
pattern.)
CLI Input (from @daydreamsai/cli
)
Telegram Message Input (from @daydreamsai/telegram
)
Inputs are the crucial link between your agent and the information it needs to react to, enabling dynamic and event-driven behavior.