Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiple contexts #114

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 112 additions & 30 deletions examples/v1/example-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,66 @@ import {
render,
action,
LogLevel,
evaluator,
output,
createContainer,
type InferContextMemory,
} from "@daydreamsai/core";
import { cli } from "@daydreamsai/core/extensions";
import { deepResearch } from "./deep-research/research";
import { string, z } from "zod";
import { tavily } from "@tavily/core";
import { ETERNUM_CONTEXT } from "../v0/eternum-context";

export const goalSchema = z.object({
id: z.string(),
description: z.string(),
success_criteria: z.array(z.string()),
dependencies: z.array(z.string()),
priority: z.number().min(1).max(10),
required_resources: z.array(z.string()),
estimated_difficulty: z.number().min(1).max(10),
const taskSchema = z.object({
plan: z.string().optional(),
meta: z.any().optional(),
actions: z.array(
z.object({
type: z.string(),
context: z.string(),
payload: z.any(),
})
),
});

export const goalSchema = z
.object({
id: z.string(),
description: z.string().describe("A description of the goal"),
success_criteria: z.array(z.string()).describe("The criteria for success"),
dependencies: z.array(z.string()).describe("The dependencies of the goal"),
priority: z.number().min(1).max(10).describe("The priority of the goal"),
required_resources: z
.array(z.string())
.describe("The resources needed to achieve the goal"),
estimated_difficulty: z
.number()
.min(1)
.max(10)
.describe("The estimated difficulty of the goal"),
tasks: z
.array(taskSchema)
.describe(
"The tasks to achieve the goal. This is where you build potential tasks you need todo, based on your understanding of what you can do. These are actions."
),
})
.describe("A goal to be achieved");

export const goalPlanningSchema = z.object({
long_term: z.array(goalSchema),
medium_term: z.array(goalSchema),
short_term: z.array(goalSchema),
long_term: z
.array(goalSchema)
.describe("Strategic goals that are the main goals you want to achieve"),
medium_term: z
.array(goalSchema)
.describe(
"Tactical goals that will require many short term goals to achieve"
),
short_term: z
.array(goalSchema)
.describe(
"Immediate actionable goals that will require a few tasks to achieve"
),
});

// Initialize Groq client
Expand All @@ -52,11 +92,6 @@ Current Task: {{currentTask}}
7. Consider past experiences when setting goals
8. Use available game state information to inform strategy

# Return a JSON structure with three arrays:
- long_term: Strategic goals that might take multiple sessions
- medium_term: Tactical goals achievable in one session
- short_term: Immediate actionable goals

# Each goal must include:
- id: Unique temporary ID used in dependencies
- description: Clear goal statement
Expand Down Expand Up @@ -99,6 +134,14 @@ const goalContexts = context({
},
});

const container = createContainer();

container.singleton("tavily", () => {
return tavily({
apiKey: process.env.TAVILY_API_KEY!,
});
});

type GoalContextMemory = InferContextMemory<typeof goalContexts>;

// Create Dreams agent instance
Expand All @@ -111,6 +154,7 @@ const agent = createDreams({
model: groq("deepseek-r1-distill-llama-70b"),
extensions: [cli, deepResearch],
context: goalContexts,
container,
actions: [
// action({
// name: "addTask",
Expand Down Expand Up @@ -173,24 +217,62 @@ const agent = createDreams({
goal,
};
},
}),
action({
name: "queryEternum",
description:
"This will tell you everything you need to know about Eternum for how to win the game",
schema: z.object({ query: z.string() }),
handler(call, ctx, agent) {
return {
data: {
result: ETERNUM_CONTEXT,
},
timestamp: Date.now(),
};
},
}),
action({
name: "Query:Eternum:Graphql",
description: "Search Eternum GraphQL API",
schema: z.object({ query: z.string() }),
handler(call, ctx, agent) {
console.log(call.data.query);
return {
data: {
result: ETERNUM_CONTEXT,
},
timestamp: Date.now(),
};
},
}),
],
outputs: {
"goal-manager:state": output({
description:
"Use this when you need to update the goals. Use the goal id to update the goal. You should attempt the goal then call this to update the goal.",
instructions: "Increment the state of the goal manager",
schema: z.object({
type: z
.enum(["SET", "UPDATE"])
.describe("SET to set the goals. UPDATE to update a goal."),
goal: goalSchema,
}),
handler: async (call, ctx, agent) => {
// get goal id
// update state of the goal id and the changes

evaluator: {
name: "validateFetchData",
prompt: "Ensure the goal is achievable",
description: "Ensures fetched data meets requirements",

handler: async (result, ctx, agent) => {
console.log({ result, ctx, agent });
const isValid = true;
return isValid;
},
console.log("handler", { call, ctx, agent });

onFailure: async (ctx, agent) => {
console.log({ ctx, agent });
},
return {
data: {
goal: "",
},
timestamp: Date.now(),
};
},
}),
],
},
}).start({
id: "game",
});
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.1",
"version": "0.1.2-alpha.1",
"packages": ["packages/*"],
"npmClient": "pnpm"
}
20 changes: 12 additions & 8 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
{
"name": "@daydreamsai/core",
"version": "0.1.1",
"version": "0.1.2-alpha.1",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": {
"@daydreamsai/core": "./src/index.ts",
"types": "./src/index.ts",
"default": "./src/index.ts"
"development": "./src/index.ts",
"default": "./dist/index.js"
}
},
"./extensions": {
"types": "./dist/extensions/index.d.ts",
"import": {
"@daydreamsai/core": "./src/extensions/index.ts",
"types": "./src/extensions/index.ts",
"default": "./src/extensions/index.ts"
"development": "./src/extensions/index.ts",
"default": "./dist/extensions/index.js"
}
},
"./io/*": {
"default": "./src/io/*.ts"
"types": "./dist/io/*.d.ts",
"import": {
"development": "./src/io/*.ts",
"default": "./dist/io/*.js"
}
}
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/dreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
type ActionCall,
type ActionResult,
type Agent,
type AgentContext,
type AnyAction,
type AnyAgent,
type AnyContext,
type Config,
type Context,
type ContextState,
type Debugger,
type Evaluator,
type Handlers,
type Log,
type Output,
Expand All @@ -30,6 +32,7 @@ import { createMemory } from "./memory";
import { createVectorStore } from "./memory/base";
import { v7 as randomUUIDv7 } from "uuid";
import { runAction, runGenerate, runGenerateResults } from "./tasks";
import { generateObject } from "ai";

export function createDreams<
Memory = any,
Expand Down Expand Up @@ -896,3 +899,27 @@ function createContextStreamHandler({
handler,
};
}

// Update runEvaluation to handle the enhanced evaluation context
// @dev: TODO: make a render context function for the evaluation prompt
async function runEvaluation<Data = any>(
evaluator: Evaluator<Data>,
data: Data,
context: AgentContext<any>,
agent: AnyAgent
): Promise<boolean> {
const evaluationResult = await generateObject({
model: agent.reasoningModel ?? agent.model,
schema: evaluator.schema,
prompt:
evaluator.prompt +
{
data: JSON.stringify(data),
context: JSON.stringify(context),
workingMemory: JSON.stringify(context.workingMemory),
timestamp: Date.now(),
},
});

return evaluator.handler(data, evaluationResult.object, context, agent);
}
12 changes: 7 additions & 5 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,20 @@ type InferAgentMemory<TAgent extends AnyAgent> =
*/
export type Evaluator<
Data = any,
Schema extends z.AnyZodObject = z.AnyZodObject,
Context extends AgentContext<any, any> = AgentContext<any, any>,
TAgent extends AnyAgent = AnyAgent,
> = {
name: string;
description?: string;
/** Schema for the evaluation result */
schema?: z.ZodType<any>;
schema: Schema;
/** Custom prompt template for LLM-based evaluation */
prompt?: string;
prompt: string;
/** Custom handler for evaluation logic */
handler?: (
handler: (
data: Data,
result: z.infer<Schema>,
ctx: Context,
agent: TAgent
) => Promise<boolean> | boolean;
Expand Down Expand Up @@ -191,7 +193,7 @@ export type Action<
) => Promise<Result> | Result;
format?: (result: ActionResult<Result>) => string | string[];
/** Optional evaluator for this specific action */
evaluator?: Evaluator<Result, Context, TAgent>;
evaluator?: Evaluator;
};

export type OutputSchema = z.AnyZodObject | z.ZodString;
Expand Down Expand Up @@ -224,7 +226,7 @@ export type Output<
format?: (res: Response) => string | string[];
examples?: z.infer<Schema>[];
/** Optional evaluator for this specific output */
evaluator?: Evaluator<Response, Context, TAgent>;
evaluator?: Evaluator<Response, z.AnyZodObject, Context, TAgent>;
required?: boolean;
};

Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
AnyContext,
Config,
Context,
Evaluator,
ExpertConfig,
Extension,
InferMemoryData,
Expand Down Expand Up @@ -84,6 +85,22 @@ export function action<
return action;
}

/**
* Creates an evaluator configuration
* @template Schema - Zod schema type for evaluator validation
* @template Context - Context type for evaluator execution
* @param config - Evaluator configuration object
* @returns Typed evaluator configuration
*/
export function evaluator<
Data = any,
Schema extends z.AnyZodObject = z.AnyZodObject,
Context extends AgentContext<any, any> = AgentContext<any, any>,
TAgent extends AnyAgent = AnyAgent,
>(config: Evaluator<Data, Schema, Context, TAgent>) {
return config;
}

/**
* Creates an output configuration
* @template Schema - Zod schema type for output validation
Expand Down
Loading