Getting Started
Starting Agent
Agent that manages goals and tasks with simple memory and actions.
1. Environment setup and imports
import { createDreams, context, action, validateEnv } from "@daydreamsai/core";
import { cliExtension } from "@daydreamsai/cli";
import { anthropic } from "@ai-sdk/anthropic";
import * as z from "zod";
validateEnv(
z.object({
ANTHROPIC_API_KEY: z.string().min(1, "ANTHROPIC_API_KEY is required"),
})
);
The agent requires an Anthropic API key for the Claude language model. Set this environment variable before running the agent.
2. Define the goal context and memory structure
type GoalMemory = {
goal: string;
tasks: string[];
currentTask: string;
};
const goalContext = context({
type: "goal",
schema: z.object({
id: z.string(),
}),
key: ({ id }) => id,
create: () => ({
goal: "",
tasks: [],
currentTask: "",
}),
render: ({ memory }) => `
Current Goal: ${memory.goal || "No goal set"}
Tasks: ${memory.tasks.length > 0 ? memory.tasks.join(", ") : "No tasks"}
Current Task: ${memory.currentTask || "None"}
`,
});
The context maintains the agent's current goal, list of tasks, and which task is currently active. Memory persists between conversations.
3. Define task management actions
const taskActions = [
action({
name: "setGoal",
description: "Set a new goal for the agent",
schema: z.object({
goal: z.string().describe("The goal to work towards"),
}),
handler: ({ goal }, ctx) => {
const memory = ctx.agentMemory as GoalMemory;
memory.goal = goal;
memory.tasks = [];
memory.currentTask = "";
return { success: true, message: `Goal set to: ${goal}` };
},
}),
action({
name: "addTask",
description: "Add a task to accomplish the goal",
schema: z.object({
task: z.string().describe("The task to add"),
}),
handler: ({ task }, ctx) => {
const memory = ctx.agentMemory as GoalMemory;
memory.tasks.push(task);
if (!memory.currentTask && memory.tasks.length === 1) {
memory.currentTask = task;
}
return { success: true, message: `Added task: ${task}` };
},
}),
action({
name: "completeTask",
description: "Mark the current task as complete",
schema: z.object({
task: z.string().describe("The task to complete"),
}),
handler: ({ task }, ctx) => {
const memory = ctx.agentMemory as GoalMemory;
memory.tasks = memory.tasks.filter((t) => t !== task);
if (memory.currentTask === task) {
memory.currentTask = memory.tasks[0] || "";
}
return {
success: true,
message: `Completed task: ${task}`,
remainingTasks: memory.tasks.length,
};
},
}),
];
4. Create and start the agent
createDreams({
model: anthropic("claude-3-7-sonnet-latest"),
extensions: [cliExtension],
context: goalContext,
actions: taskActions,
}).start({ id: "basic-agent" });