Daydreams Logo
Extra reading

utils.ts

This file provides essential "factory" functions that you use to define the building blocks of your Daydreams agent, such as its tools (Actions), how it receives information (Inputs), how it responds (Outputs), how it remembers things specifically for an action (Memory), and how you bundle features together (Extensions).

How to Use

You'll import these functions directly from @daydreamsai/core when defining your agent's components, typically in separate files.

  • action({...}): Use this to define a specific capability or tool for your agent. You give it a name, description, expected arguments (schema using Zod), and the handler code that runs when the AI decides to use this tool. (See Actions for details).

    import { action } from "@daydreamsai/core";
    import { z } from "zod";
     
    export const myAction = action({
      name: "myTool",
      description: "Does something cool.",
      schema: z.object({ param: z.string() }),
      handler: async (args, ctx, agent) => {
        /* ... */
      },
    });
  • input({...}): Use this to define how your agent receives information from the outside world (like a chat message or an API event). You specify how to subscribe to the source and how to send incoming data into the agent for processing. (See Inputs).

  • output({...}): Use this to define how your agent sends information out (like replying to a chat). You give it a type, expected content structure (schema), and the handler code that performs the sending. (See Outputs).

  • extension({...}): Use this to package related actions, inputs, outputs, contexts, and services together into a reusable module. You provide a name and arrays/objects containing the components this extension provides. (See Services & Extensions).

  • memory({...}): A specialized helper used within an action definition if that specific action needs its own persistent memory across different calls (less common than context memory). You provide a key and a create function for its initial state.

Benefit

These functions provide a standardized way to define the different parts of your agent. They ensure all the necessary configuration details are provided and integrate smoothly with the agent's lifecycle and the AI model. They abstract away the internal wiring, letting you focus on the logic of your agent's capabilities.

Anticipated Questions

  • "Do I use these functions inside createDreams?" No, you typically use these functions in separate files to define your actions, inputs, etc., and then you import those definitions and pass them to createDreams in its configuration object (e.g., in the actions: [...] or extensions: [...] arrays).
  • "What's the difference between action and output?" Use action when the agent needs to perform a task and get a result back to continue thinking (like looking up information). Use output when the agent just needs to send information out (like sending a final reply message).

On this page