Skip to content

Commit

Permalink
Merge pull request #412 from ai16z/fix-services
Browse files Browse the repository at this point in the history
feat: services
  • Loading branch information
sirkitree authored Nov 19, 2024
2 parents 743aa78 + 48d6fdb commit 3a033b4
Show file tree
Hide file tree
Showing 28 changed files with 402 additions and 318 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</div>

## 🌍 README Translations

[中文说明](./README_CN.md) | [日本語の説明](./README_JA.md) | [한국어 설명](./README_KOR.md) | [Français](./README_FR.md) | [Português](./README_PTBR.md) | [Türkçe](./README_TR.md) | [Русский](./README_RU.md) | [Español](./README_ES.md) | [Italiano](./README_IT.md)

## ✨ Features
Expand Down
20 changes: 12 additions & 8 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { DiscordClientInterface } from "@ai16z/client-discord";
import { AutoClientInterface } from "@ai16z/client-auto";
import { TelegramClientInterface } from "@ai16z/client-telegram";
import { TwitterClientInterface } from "@ai16z/client-twitter";
import { defaultCharacter } from "@ai16z/eliza";
import { AgentRuntime } from "@ai16z/eliza";
import { settings } from "@ai16z/eliza";
import {
defaultCharacter,
AgentRuntime,
settings,
Character,
IAgentRuntime,
IDatabaseAdapter,
ModelProviderName,
elizaLogger,
} from "@ai16z/eliza";
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
import { solanaPlugin } from "@ai16z/plugin-solana";
Expand Down Expand Up @@ -219,7 +219,11 @@ export async function createAgent(
db: any,
token: string
) {
console.log("Creating runtime for character", character.name);
elizaLogger.success(
elizaLogger.successesTitle,
"Creating runtime for character",
character.name
);
return new AgentRuntime({
databaseAdapter: db,
token,
Expand Down Expand Up @@ -279,7 +283,7 @@ const startAgents = async () => {
await startAgent(character, directClient);
}
} catch (error) {
console.error("Error starting agents:", error);
elizaLogger.error("Error starting agents:", error);
}

function chat() {
Expand All @@ -292,12 +296,12 @@ const startAgents = async () => {
});
}

console.log("Chat started. Type 'exit' to quit.");
elizaLogger.log("Chat started. Type 'exit' to quit.");
chat();
};

startAgents().catch((error) => {
console.error("Unhandled error in startAgents:", error);
elizaLogger.error("Unhandled error in startAgents:", error);
process.exit(1); // Exit the process after logging
});

Expand Down
2 changes: 1 addition & 1 deletion packages/client-direct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DirectClient {
private agents: Map<string, AgentRuntime>;

constructor() {
console.log("DirectClient constructor");
elizaLogger.log("DirectClient constructor");
this.app = express();
this.app.use(cors());
this.agents = new Map();
Expand Down
4 changes: 2 additions & 2 deletions packages/client-discord/src/actions/download_media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export default {
callback: HandlerCallback
) => {
const videoService = runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>();
.getService<IVideoService>(ServiceType.VIDEO)
.getInstance();
if (!state) {
state = (await runtime.composeState(message)) as State;
}
Expand Down
45 changes: 25 additions & 20 deletions packages/client-discord/src/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export class AttachmentManager {
} else if (
attachment.contentType?.startsWith("video/") ||
this.runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>()
.getService<IVideoService>(ServiceType.VIDEO)
.isVideoUrl(attachment.url)
) {
media = await this.processVideoAttachment(attachment);
Expand Down Expand Up @@ -137,10 +136,16 @@ export class AttachmentManager {
throw new Error("Unsupported audio/video format");
}

const transcription = await this.runtime
.getService(ServiceType.TRANSCRIPTION)
.getInstance<ITranscriptionService>()
.transcribeAttachment(audioBuffer);
const transcriptionService =
this.runtime.getService<ITranscriptionService>(
ServiceType.TRANSCRIPTION
);
if (!transcriptionService) {
throw new Error("Transcription service not found");
}

const transcription =
await transcriptionService.transcribeAttachment(audioBuffer);
const { title, description } = await generateSummary(
this.runtime,
transcription
Expand Down Expand Up @@ -220,8 +225,7 @@ export class AttachmentManager {
const response = await fetch(attachment.url);
const pdfBuffer = await response.arrayBuffer();
const text = await this.runtime
.getService(ServiceType.PDF)
.getInstance<IPdfService>()
.getService<IPdfService>(ServiceType.PDF)
.convertPdfToText(Buffer.from(pdfBuffer));
const { title, description } = await generateSummary(
this.runtime,
Expand Down Expand Up @@ -289,8 +293,9 @@ export class AttachmentManager {
): Promise<Media> {
try {
const { description, title } = await this.runtime
.getService(ServiceType.IMAGE_DESCRIPTION)
.getInstance<IImageDescriptionService>()
.getService<IImageDescriptionService>(
ServiceType.IMAGE_DESCRIPTION
)
.describeImage(attachment.url);
return {
id: attachment.id,
Expand Down Expand Up @@ -322,16 +327,16 @@ export class AttachmentManager {
private async processVideoAttachment(
attachment: Attachment
): Promise<Media> {
if (
this.runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>()
.isVideoUrl(attachment.url)
) {
const videoInfo = await this.runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>()
.processVideo(attachment.url);
const videoService = this.runtime.getService<IVideoService>(
ServiceType.VIDEO
);

if (!videoService) {
throw new Error("Video service not found");
}

if (videoService.isVideoUrl(attachment.url)) {
const videoInfo = await videoService.processVideo(attachment.url);
return {
id: attachment.id,
url: attachment.url,
Expand Down
6 changes: 3 additions & 3 deletions packages/client-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import { VoiceManager } from "./voice.ts";

export class DiscordClient extends EventEmitter {
apiToken: string;
private client: Client;
private runtime: IAgentRuntime;
client: Client;
runtime: IAgentRuntime;
character: Character;
private messageManager: MessageManager;
private voiceManager: VoiceManager;
Expand Down Expand Up @@ -193,7 +193,7 @@ export class DiscordClient extends EventEmitter {
}

async handleReactionRemove(reaction: MessageReaction, user: User) {
console.log("Reaction removed");
elizaLogger.log("Reaction removed");
// if (user.bot) return;

let emoji = reaction.emoji.name;
Expand Down
66 changes: 48 additions & 18 deletions packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,23 @@ export class MessageManager {
}
if (message.channel.type === ChannelType.GuildVoice) {
// For voice channels, use text-to-speech
const audioStream = await this.runtime
.getService(ServiceType.SPEECH_GENERATION)
.getInstance<ISpeechService>()
.generate(this.runtime, content.text);

const speechService =
this.runtime.getService<ISpeechService>(
ServiceType.SPEECH_GENERATION
);

if (!speechService) {
throw new Error(
"Speech generation service not found"
);
}

const audioStream = await speechService.generate(
this.runtime,
content.text
);

await this.voiceManager.playAudioStream(
userId,
audioStream
Expand Down Expand Up @@ -603,10 +616,18 @@ export class MessageManager {
if (message.channel.type === ChannelType.GuildVoice) {
// For voice channels, use text-to-speech for the error message
const errorMessage = "Sorry, I had a glitch. What was that?";
const audioStream = await this.runtime
.getService(ServiceType.SPEECH_GENERATION)
.getInstance<ISpeechService>()
.generate(this.runtime, errorMessage);

const speechService = this.runtime.getService<ISpeechService>(
ServiceType.SPEECH_GENERATION
);
if (!speechService) {
throw new Error("Speech generation service not found");
}

const audioStream = await speechService.generate(
this.runtime,
errorMessage
);
await this.voiceManager.playAudioStream(userId, audioStream);
} else {
// For text channels, send the error message
Expand Down Expand Up @@ -670,14 +691,17 @@ export class MessageManager {
for (const url of urls) {
if (
this.runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>()
.getService<IVideoService>(ServiceType.VIDEO)
.isVideoUrl(url)
) {
const videoInfo = await this.runtime
.getService(ServiceType.VIDEO)
.getInstance<IVideoService>()
.processVideo(url);
const videoService = this.runtime.getService<IVideoService>(
ServiceType.VIDEO
);
if (!videoService) {
throw new Error("Video service not found");
}
const videoInfo = await videoService.processVideo(url);

attachments.push({
id: `youtube-${Date.now()}`,
url: url,
Expand All @@ -687,10 +711,16 @@ export class MessageManager {
text: videoInfo.text,
});
} else {
const { title, bodyContent } = await this.runtime
.getService(ServiceType.BROWSER)
.getInstance<IBrowserService>()
.getPageContent(url, this.runtime);
const browserService = this.runtime.getService<IBrowserService>(
ServiceType.BROWSER
);
if (!browserService) {
throw new Error("Browser service not found");
}

const { title, bodyContent } =
await browserService.getPageContent(url, this.runtime);

const { title: newTitle, description } = await generateSummary(
this.runtime,
title + "\n" + bodyContent
Expand Down
Loading

0 comments on commit 3a033b4

Please sign in to comment.