Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Jan 19, 2025
1 parent 06671f2 commit 66623eb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 23 deletions.
71 changes: 68 additions & 3 deletions examples/example-twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LogLevel } from "../packages/core/src/types";
import chalk from "chalk";
import { defaultCharacter } from "../packages/core/src/core/character";
import { JSONSchemaType } from "ajv";
import { Consciousness } from "../packages/core/src/core/consciousness";

async function main() {
// Initialize core dependencies
Expand Down Expand Up @@ -88,6 +89,68 @@ async function main() {
// });
// }

// Initialize consciousness
const consciousness = new Consciousness(llmClient, roomManager, {
intervalMs: 300000, // Think every 5 minutes
minConfidence: 0.7,
logLevel: LogLevel.DEBUG,
});

// Register consciousness input
core.registerInput({
name: "consciousness_thoughts",
handler: async () => {
console.log(chalk.blue("🧠 Generating thoughts..."));
const thought = await consciousness.start();

// Only return thought if it's not an error and has sufficient confidence
if (thought.type !== "error" && thought.type !== "low_confidence") {
// If it's a social_share type thought, format it for Twitter
if (thought.metadata?.type === "social_share") {
return {
type: "thought",
content: thought.content,
metadata: {
...thought.metadata,
suggestedPlatform: "twitter",
thoughtType: "social_share",
},
};
}
}
return null;
},
response: {
type: "string",
content: "string",
metadata: "object",
},
interval: 300000, // Check for new thoughts every 5 minutes
});

// Register Twitter output for thoughts
core.registerOutput({
name: "twitter_thought",
handler: async (data: unknown) => {
const thoughtData = data as { content: string };
return twitter.createTweetOutput().handler({
content: thoughtData.content,
});
},
response: {
success: "boolean",
tweetId: "string",
},
schema: {
type: "object" as const,
properties: {
content: { type: "string" as const, nullable: false },
},
required: ["content"],
additionalProperties: false,
} as any,
});

// Register Twitter output for auto-replies
core.registerOutput({
name: "twitter_reply",
Expand Down Expand Up @@ -118,12 +181,14 @@ async function main() {
process.on("SIGINT", async () => {
console.log(chalk.yellow("\n\nShutting down..."));

// Stop consciousness
await consciousness.stop();

// Remove all inputs and outputs
core.removeInput("twitter_mentions");
// for (const account of accountsToMonitor) {
// core.removeInput(`twitter_timeline_${account}`);
// }
core.removeInput("consciousness_thoughts");
core.removeOutput("twitter_reply");
core.removeOutput("twitter_thought");

console.log(chalk.green("✅ Shutdown complete"));
process.exit(0);
Expand Down
38 changes: 20 additions & 18 deletions packages/core/src/core/consciousness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface Thought {
confidence: number;
context?: Record<string, any>;
timestamp: Date;
type: string;
source: string;
metadata?: Record<string, any>;
roomId?: string;
}

export type ThoughtType =
Expand Down Expand Up @@ -166,15 +170,8 @@ Focus on:
});
}

public async start(): Promise<void> {
if (this.isThinking) return;

this.isThinking = true;
const intervalMs = this.config.intervalMs || 60000; // Default to 1 minute

this.thoughtInterval = setInterval(() => this.think(), intervalMs);

this.logger.info("Consciousness.start", "Internal thought process started");
public async start(): Promise<Thought> {
return this.think();
}

public async stop(): Promise<void> {
Expand All @@ -186,22 +183,21 @@ Focus on:
this.logger.info("Consciousness.stop", "Internal thought process stopped");
}

private async think(): Promise<{
type: string;
source: string;
content: string;
timestamp: Date;
metadata: Record<string, any>;
}> {
private async think(): Promise<Thought> {
console.log("thinking");
try {
console.log("generating thought");
const thought = await this.generateThought();

console.log("thought", thought);

if (thought.confidence >= (this.config.minConfidence || 0.7)) {
return {
type: "internal_thought",
source: "consciousness",
content: thought.content,
timestamp: thought.timestamp,
confidence: thought.confidence,
metadata: {
...thought.context,
confidence: thought.confidence,
Expand All @@ -224,6 +220,7 @@ Focus on:
source: "consciousness",
content: "Thought was below confidence threshold",
timestamp: new Date(),
confidence: thought.confidence,
metadata: {
confidence: thought.confidence,
threshold: this.config.minConfidence || 0.7,
Expand All @@ -240,6 +237,7 @@ Focus on:
source: "consciousness",
content: "Error occurred during thought process",
timestamp: new Date(),
confidence: 0,
metadata: {
error: error instanceof Error ? error.message : String(error),
},
Expand All @@ -249,8 +247,10 @@ Focus on:

private async generateThought(): Promise<Thought> {
// Get all rooms and their recent memories
const rooms = await this.roomManager.listRooms();
const recentMemories = this.getRecentMemories(rooms);

const recentMemories = this.getRecentMemories(
await this.roomManager.listRooms()
);

// Randomly select a thought type based on context
const thoughtType = await this.determineThoughtType(recentMemories);
Expand Down Expand Up @@ -283,6 +283,8 @@ Return only the JSON object, no other text or formatting.`;
return {
content: result.thought,
confidence: result.confidence,
type: thoughtType,
source: "consciousness",
context: {
reasoning: result.reasoning,
...result.context,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/core/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ ${availableOutputs
)
.join("\n\n")}
If the output is for a message user the personality of the character to determine if the output was successful.
${JSON.stringify(this.character, null, 2)}
Based on the content and context, determine which outputs should be triggered.
For each appropriate output, provide:
1. The output name
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core/vector-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ export class ChromaVectorDB implements VectorDB {
public async listRooms(): Promise<string[]> {
const collections = await this.client.listCollections();
return collections
.filter((c: any) => c.name.startsWith("room_"))
.map((c: any) => c.name.replace("room_", ""));
.filter((c: any) => c.startsWith("room_"))
.map((c: any) => c.replace("room_", ""));
}

public async getRoomMemoryCount(roomId: string): Promise<number> {
Expand Down

0 comments on commit 66623eb

Please sign in to comment.