A Daydreams agent is composed of several core building blocks that work in a continuous loop:
- Inputs: Listen for external events (e.g., a Discord message) and trigger the agent.
- Contexts: Provide the persistent memory and state (e.g., chat history) that the agent uses to maintain continuity.
- Actions: Perform specific tasks or fetch data (e.g., getting weather) based on the agent's reasoning.
- Outputs: Send information or perform actions back to the external world (e.g., sending a Discord message).
These are all tied together using createDreams, which accepts a model, contexts, inputs, outputs, and actions.
import { createDreams, context, action, input, output } from "@daydreamsai/core";
import { openai } from "@ai-sdk/openai";
import * as z from "zod";
// 1. INPUT
const discordInput = input({
type: "discord:message",
schema: z.object({ content: z.string(), userId: z.string(), channelId: z.string() }),
subscribe: (send, agent) => {
// ... subscription logic
return () => discord.removeAllListeners("messageCreate");
},
});
// 2. ACTION
const getWeather = action({
name: "get-weather",
description: "Gets current weather for a city",
schema: z.object({ city: z.string().describe("City name") }),
handler: async ({ city }) => {
// ... logic
return { temperature: 72, condition: "sunny", city };
},
});
// 3. OUTPUT
const discordOutput = output({
type: "discord:message",
description: "Sends a message to Discord",
schema: z.string(),
attributes: z.object({ channelId: z.string() }),
handler: async (message, ctx) => {
const { channelId } = ctx.outputRef.params;
await discord.send(channelId, message);
return { sent: true };
},
});
// 4. CONTEXT
const chatContext = context({
type: "chat",
schema: z.object({ userId: z.string() }),
create: () => ({ messages: [] }),
});
// 5. AGENT
const agent = createDreams({
model: openai("gpt-4o"),
contexts: [chatContext],
inputs: [discordInput],
outputs: [discordOutput],
actions: [getWeather],
});