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: implements lifecycle hooks #79

Merged
merged 11 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion examples/example-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { Orchestrator } from "../packages/core/src/core/orchestrator";
import { HandlerRole } from "../packages/core/src/core/types";
import { RoomManager } from "../packages/core/src/core/room-manager";
import { RoomManager } from "../packages/core/src/core/conversation-manager";
import { ChromaVectorDB } from "../packages/core/src/core/vector-db";
import { MessageProcessor } from "../packages/core/src/core/processors/message-processor";
import { ResearchQuantProcessor } from "../packages/core/src/core/processors/research-processor";
Expand Down
8 changes: 6 additions & 2 deletions examples/example-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ async function main() {
role: HandlerRole.OUTPUT,
execute: async (data: any) => {
const result = await starknetChain.write(data.payload);
return `Transaction: ${JSON.stringify(result, null, 2)}`;
return {
content: `Transaction: ${JSON.stringify(result, null, 2)}`,
};
},
outputSchema: z
.object({
Expand Down Expand Up @@ -123,7 +125,9 @@ async function main() {
`query: ${query}`,
`result: ${JSON.stringify(result, null, 2)}`,
].join("\n\n");
return `GraphQL data fetched successfully: ${resultStr}`;
return {
content: `GraphQL data fetched successfully: ${resultStr}`,
};
},
outputSchema: z
.object({
Expand Down
20 changes: 7 additions & 13 deletions examples/example-discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Orchestrator } from "../packages/core/src/core/orchestrator";
import { HandlerRole, LogLevel } from "../packages/core/src/core/types";
import { DiscordClient } from "../packages/core/src/core/io/discord";
import { RoomManager } from "../packages/core/src/core/room-manager";
import { ConversationManager } from "../packages/core/src/core/conversation-manager";
import { ChromaVectorDB } from "../packages/core/src/core/vector-db";
import { MessageProcessor } from "../packages/core/src/core/processors/message-processor";
import { LLMClient } from "../packages/core/src/core/llm-client";
Expand All @@ -19,6 +19,7 @@ import readline from "readline";
import { MongoDb } from "../packages/core/src/core/db/mongo-db";
import { Message } from "discord.js";
import { MasterProcessor } from "../packages/core/src/core/processors/master-processor";
import { makeFlowLifecycle } from "../packages/core/src/core/life-cycle";

async function main() {
// Set logging level as you see fit
Expand All @@ -33,10 +34,10 @@ async function main() {
// Optional: Purge previous session data if you want a fresh start
await vectorDb.purge();

const roomManager = new RoomManager(vectorDb);
const conversationManager = new ConversationManager(vectorDb);

const llmClient = new LLMClient({
model: "anthropic/claude-3-5-sonnet-latest", // Example model
model: "anthropic/claude-3-5-sonnet-latest",
temperature: 0.3,
});

Expand All @@ -46,15 +47,10 @@ async function main() {
loglevel
);

// Initialize processor with default character personality
const messageProcessor = new MessageProcessor(
llmClient,
defaultCharacter,
loglevel
masterProcessor.addProcessor(
new MessageProcessor(llmClient, defaultCharacter, loglevel)
);

masterProcessor.addProcessor(messageProcessor);

// Connect to MongoDB (for scheduled tasks, if you use them)
const scheduledTaskDb = new MongoDb(
"mongodb://localhost:27017",
Expand All @@ -69,10 +65,8 @@ async function main() {

// Create the Orchestrator
const core = new Orchestrator(
roomManager,
vectorDb,
masterProcessor,
scheduledTaskDb,
makeFlowLifecycle(scheduledTaskDb, conversationManager),
{
level: loglevel,
enableColors: true,
Expand Down
16 changes: 10 additions & 6 deletions examples/example-goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ async function main() {
role: HandlerRole.OUTPUT,
execute: async (data: any) => {
const result = await starknetChain.write(data.payload);
return `Transaction executed successfully: ${JSON.stringify(
result,
null,
2
)}`;
return {
content: `Transaction executed successfully: ${JSON.stringify(
result,
null,
2
)}`,
};
},
outputSchema: z
.object({
Expand Down Expand Up @@ -145,7 +147,9 @@ async function main() {
`query: ${query}`,
`result: ${JSON.stringify(result, null, 2)}`,
].join("\n\n");
return `GraphQL data fetched successfully: ${resultStr}`;
return {
content: `GraphQL data fetched successfully: ${resultStr}`,
};
},
outputSchema: z
.object({
Expand Down
26 changes: 14 additions & 12 deletions examples/example-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ import { ChromaVectorDB } from "../packages/core/src/core/vector-db";

import { Orchestrator } from "../packages/core/src/core/orchestrator";
import { HandlerRole } from "../packages/core/src/core/types";
import { RoomManager } from "../packages/core/src/core/room-manager";
import { ConversationManager } from "../packages/core/src/core/conversation-manager";
import { MessageProcessor } from "../packages/core/src/core/processors/message-processor";
import { defaultCharacter } from "../packages/core/src/core/character";

import { LogLevel } from "../packages/core/src/core/types";
import { MongoDb } from "../packages/core/src/core/db/mongo-db";
import { MasterProcessor } from "../packages/core/src/core/processors/master-processor";
import { makeFlowLifecycle } from "../packages/core/src/core/life-cycle";

const scheduledTaskDb = new MongoDb(
const kvDb = new MongoDb(
"mongodb://localhost:27017",
"myApp",
"scheduled_tasks"
);

await scheduledTaskDb.connect();
await kvDb.connect();
console.log(chalk.green("✅ Scheduled task database connected"));

await scheduledTaskDb.deleteAll();
await kvDb.deleteAll();

// ------------------------------------------------------
// 1) CREATE DAYDREAMS AGENT
Expand All @@ -50,7 +51,7 @@ async function createDaydreamsAgent() {
});

// 1.3. Room manager initialization
const roomManager = new RoomManager(vectorDb);
const conversationManager = new ConversationManager(vectorDb);

const masterProcessor = new MasterProcessor(
llmClient,
Expand All @@ -69,10 +70,8 @@ async function createDaydreamsAgent() {

// 1.5. Initialize core system
const orchestrator = new Orchestrator(
roomManager,
vectorDb,
masterProcessor,
scheduledTaskDb,
makeFlowLifecycle(kvDb, conversationManager),
{
level: loglevel,
enableColors: true,
Expand Down Expand Up @@ -102,6 +101,10 @@ async function createDaydreamsAgent() {
message: string;
};
console.log(`Reply to user ${userId ?? "??"}: ${message}`);
return {
userId,
message,
};
},
});

Expand Down Expand Up @@ -158,7 +161,7 @@ wss.on("connection", (ws) => {
"x-user-id": userId,
},
},
userMessage,
{ content: userMessage },
orchestratorId ? orchestratorId : undefined
);

Expand Down Expand Up @@ -211,8 +214,7 @@ app.get("/api/history/:userId", async (req, res) => {
console.log("Fetching history for userId:", userId);

// Get all orchestrator records for this user
const histories =
await scheduledTaskDb.getOrchestratorsByUserId(userId);
const histories = await kvDb.getOrchestratorsByUserId(userId);

if (!histories || histories.length === 0) {
console.log("No histories found");
Expand Down Expand Up @@ -241,7 +243,7 @@ app.get("/api/history/:userId/:chatId", async (req, res) => {
return res.status(400).json({ error: "Invalid chat ID format" });
}

const history = await scheduledTaskDb.getOrchestratorById(objectId);
const history = await kvDb.getOrchestratorById(objectId);

if (!history) {
return res.status(404).json({ error: "History not found" });
Expand Down
Loading