Skip to content

Commit

Permalink
add in basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Jan 14, 2025
1 parent e56bcae commit 9158d3e
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
134 changes: 134 additions & 0 deletions examples/example-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// This is a basic example of how to use the Daydreams package
// It runs a simple agent that can execute tasks directly using the think method

import { ChromaVectorDB } from "../packages/core/src/core/vector-db";
import { env } from "../packages/core/src/core/env";
import { LLMClient } from "../packages/core/src/core/llm-client";
import { ChainOfThought } from "../packages/core/src/core/chain-of-thought";
import { ETERNUM_CONTEXT } from "./eternum-context";
import * as readline from "readline";
import chalk from "chalk";
import { LogLevel } from "../packages/core/src/types";
import { starknetTransactionAction } from "../packages/core/src/core/actions/starknet-transaction";
import { graphqlAction } from "../packages/core/src/core/actions/graphql";
import {
graphqlFetchSchema,
starknetTransactionSchema,
} from "../packages/core/src/core/validation";
import type { JSONSchemaType } from "ajv";

async function getCliInput(prompt: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

return new Promise((resolve) => {
rl.question(prompt, (answer) => {
rl.close();
resolve(answer);
});
});
}

async function main() {
// Initialize LLM client
const llmClient = new LLMClient({
provider: "anthropic",
apiKey: env.ANTHROPIC_API_KEY,
});

// Initialize ChainOfThought
const dreams = new ChainOfThought(llmClient, {
worldState: ETERNUM_CONTEXT,
queriesAvailable: "",
availableActions: "",
});

// Register available actions
dreams.registerAction(
"EXECUTE_TRANSACTION",
starknetTransactionAction,
{
description: "Execute a transaction on the Starknet blockchain",
example: JSON.stringify({
contractAddress: "0x1234567890abcdef",
entrypoint: "execute",
calldata: [1, 2, 3],
}),
},
starknetTransactionSchema as JSONSchemaType<any>
);

dreams.registerAction(
"GRAPHQL_FETCH",
graphqlAction,
{
description: "Fetch data from the Eternum GraphQL API",
example: JSON.stringify({
query:
"query GetRealmInfo { eternumRealmModels(where: { realm_id: 42 }) { edges { node { ... on eternum_Realm { entity_id level } } } }",
}),
},
graphqlFetchSchema as JSONSchemaType<any>
);

// Add basic event handlers
dreams.on("think:start", ({ query }) => {
console.log(chalk.blue("\n🧠 Thinking about:"), query);
});

dreams.on("action:start", (action) => {
console.log(chalk.yellow("\n🎬 Executing action:"), {
type: action.type,
payload: action.payload,
});
});

dreams.on("action:complete", ({ action, result }) => {
console.log(chalk.green("\n✅ Action completed:"), {
type: action.type,
result,
});
});

dreams.on("action:error", ({ action, error }) => {
console.log(chalk.red("\n❌ Action failed:"), {
type: action.type,
error,
});
});

// Main interaction loop
while (true) {
console.log(
chalk.cyan("\n🤖 What would you like me to do? (type 'exit' to quit):")
);
const userInput = await getCliInput("> ");

if (userInput.toLowerCase() === "exit") {
console.log(chalk.yellow("Goodbye! 👋"));
break;
}

try {
console.log(chalk.cyan("\n🎯 Processing your request..."));
const result = await dreams.think(userInput);
console.log(chalk.green("\n✨ Task completed successfully!"));
console.log("Result:", result);
} catch (error) {
console.error(chalk.red("Error processing request:"), error);
}
}

// Handle shutdown
process.on("SIGINT", async () => {
console.log(chalk.yellow("\nShutting down..."));
process.exit(0);
});
}

main().catch((error) => {
console.error(chalk.red("Fatal error:"), error);
process.exit(1);
});
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "daydreams",
"version": "0.0.1",
"scripts": {
"dev": "bun run examples/example.ts"
"goal": "bun run examples/example-goal.ts",
"task": "bun run examples/example-basic.ts"
},
"dependencies": {
"@types/node": "^22.10.5",
Expand Down

0 comments on commit 9158d3e

Please sign in to comment.