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

fix: Fix/telegram #530

Merged
merged 3 commits into from
Nov 23, 2024
Merged
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
11 changes: 9 additions & 2 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import readline from "readline";
import yargs from "yargs";
import path from "path";
import { fileURLToPath } from "url";
import blobert from "./blobert.ts";
import { character } from "./character.ts";
import type { DirectClient } from "@ai16z/client-direct";

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
Expand Down Expand Up @@ -263,6 +263,13 @@ export function createAgent(
});
}

function intializeFsCache(baseDir: string, character: Character) {
const cacheDir = path.resolve(baseDir, character.id, "cache");

const cache = new CacheManager(new FsCacheAdapter(cacheDir));
return cache;
}

function intializeDbCache(character: Character, db: IDatabaseCacheAdapter) {
const cache = new CacheManager(new DbCacheAdapter(db, character.id));
return cache;
Expand Down Expand Up @@ -310,7 +317,7 @@ const startAgents = async () => {

let charactersArg = args.characters || args.character;

let characters = [blobert];
let characters = [character];

if (charactersArg) {
characters = await loadCharacters(charactersArg);
Expand Down
12 changes: 8 additions & 4 deletions packages/adapter-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ export class PostgresDatabaseAdapter
);

if (existingParticipant.rows.length > 0) {
console.log(`Participant with userId ${userId} already exists in room ${roomId}.`);
console.log(
`Participant with userId ${userId} already exists in room ${roomId}.`
);
return; // Exit early if the participant already exists
}

Expand All @@ -750,11 +752,13 @@ export class PostgresDatabaseAdapter
} catch (error) {
// This is to prevent duplicate participant error in case of a race condition
// Handle unique constraint violation error (code 23505)
if (error.code === '23505') {
console.warn(`Participant with userId ${userId} already exists in room ${roomId}.`); // Optionally, you can log this or handle it differently
if (error.code === "23505") {
console.warn(
`Participant with userId ${userId} already exists in room ${roomId}.`
); // Optionally, you can log this or handle it differently
} else {
// Handle other errors
console.error('Error adding participant:', error);
console.error("Error adding participant:", error);
return false;
}
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-telegram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const TelegramClientInterface: Client = {
return tg;
},
stop: async (_runtime: IAgentRuntime) => {
console.warn("Telegram client does not support stopping yet");
elizaLogger.warn("Telegram client does not support stopping yet");
},
};

Expand Down
71 changes: 37 additions & 34 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Message } from "@telegraf/types";
import { Context, Telegraf } from "telegraf";

import { composeContext } from "@ai16z/eliza";
import { composeContext, elizaLogger, ServiceType } from "@ai16z/eliza";
import { embeddingZeroVector } from "@ai16z/eliza";
import {
Content,
Expand All @@ -17,7 +17,6 @@ import { stringToUuid } from "@ai16z/eliza";

import { generateMessageResponse, generateShouldRespond } from "@ai16z/eliza";
import { messageCompletionFooter, shouldRespondFooter } from "@ai16z/eliza";
import { ImageDescriptionService } from "@ai16z/plugin-node";

const MAX_MESSAGE_LENGTH = 4096; // Telegram's max message length

Expand Down Expand Up @@ -137,57 +136,49 @@ Thread of Tweets You Are Replying To:
export class MessageManager {
public bot: Telegraf<Context>;
private runtime: IAgentRuntime;
private imageService: IImageDescriptionService;

constructor(bot: Telegraf<Context>, runtime: IAgentRuntime) {
this.bot = bot;
this.runtime = runtime;
this.imageService = ImageDescriptionService.getInstance();
}

// Process image messages and generate descriptions
private async processImage(
message: Message
): Promise<{ description: string } | null> {
// console.log(
// "🖼️ Processing image message:",
// JSON.stringify(message, null, 2)
// );

try {
let imageUrl: string | null = null;

// Handle photo messages
if ("photo" in message && message.photo?.length > 0) {
const photo = message.photo[message.photo.length - 1];
const fileLink = await this.bot.telegram.getFileLink(
photo.file_id
);
imageUrl = fileLink.toString();
}
// Handle image documents
else if (
} else if (
"document" in message &&
message.document?.mime_type?.startsWith("image/")
) {
const doc = message.document;
const fileLink = await this.bot.telegram.getFileLink(
doc.file_id
message.document.file_id
);
imageUrl = fileLink.toString();
}

if (imageUrl) {
const imageDescriptionService =
this.runtime.getService<IImageDescriptionService>(
ServiceType.IMAGE_DESCRIPTION
);
const { title, description } =
await this.imageService.describeImage(imageUrl);
const fullDescription = `[Image: ${title}\n${description}]`;
return { description: fullDescription };
await imageDescriptionService.describeImage(imageUrl);
return { description: `[Image: ${title}\n${description}]` };
}
} catch (error) {
console.error("❌ Error processing image:", error);
}

return null; // No image found
return null;
}

// Decide if the bot should respond to the message
Expand All @@ -196,7 +187,6 @@ export class MessageManager {
state: State
): Promise<boolean> {
// Respond if bot is mentioned

if (
"text" in message &&
message.text?.includes(`@${this.bot.botInfo?.username}`)
Expand All @@ -209,7 +199,7 @@ export class MessageManager {
return true;
}

// Respond to images in group chats
// Don't respond to images in group chats
if (
"photo" in message ||
("document" in message &&
Expand Down Expand Up @@ -238,7 +228,7 @@ export class MessageManager {
return response === "RESPOND";
}

return false; // No criteria met
return false;
}

// Send long messages in chunks
Expand Down Expand Up @@ -291,7 +281,7 @@ export class MessageManager {
// Generate a response using AI
private async _generateResponse(
message: Memory,
state: State,
_state: State,
context: string
): Promise<Content> {
const { userId, roomId } = message;
Expand All @@ -306,9 +296,10 @@ export class MessageManager {
console.error("❌ No response from generateMessageResponse");
return null;
}

await this.runtime.databaseAdapter.log({
body: { message, context, response },
userId: userId,
userId,
roomId,
type: "response",
});
Expand Down Expand Up @@ -342,14 +333,23 @@ export class MessageManager {
try {
// Convert IDs to UUIDs
const userId = stringToUuid(ctx.from.id.toString()) as UUID;

// Get user name
const userName =
ctx.from.username || ctx.from.first_name || "Unknown User";

// Get chat ID
const chatId = stringToUuid(
ctx.chat?.id.toString() + "-" + this.runtime.agentId
) as UUID;

// Get agent ID
const agentId = this.runtime.agentId;

// Get room ID
const roomId = chatId;

// Ensure connection
await this.runtime.ensureConnection(
userId,
roomId,
Expand All @@ -358,6 +358,7 @@ export class MessageManager {
"telegram"
);

// Get message ID
const messageId = stringToUuid(
message.message_id.toString() + "-" + this.runtime.agentId
) as UUID;
Expand All @@ -382,17 +383,18 @@ export class MessageManager {
return; // Skip if no content
}

// Create content
const content: Content = {
text: fullText,
source: "telegram",
// inReplyTo:
// "reply_to_message" in message && message.reply_to_message
// ? stringToUuid(
// message.reply_to_message.message_id.toString() +
// "-" +
// this.runtime.agentId
// )
// : undefined,
inReplyTo:
"reply_to_message" in message && message.reply_to_message
? stringToUuid(
message.reply_to_message.message_id.toString() +
"-" +
this.runtime.agentId
)
: undefined,
};

// Create memory for the message
Expand All @@ -406,6 +408,7 @@ export class MessageManager {
embedding: embeddingZeroVector,
};

// Create memory
await this.runtime.messageManager.createMemory(memory);

// Update state with the new memory
Expand Down Expand Up @@ -498,8 +501,8 @@ export class MessageManager {

await this.runtime.evaluate(memory, state, shouldRespond);
} catch (error) {
console.error("❌ Error handling message:", error);
console.error("Error sending message:", error);
elizaLogger.error("❌ Error handling message:", error);
elizaLogger.error("Error sending message:", error);
}
}
}
Loading