Skip to content

Commit

Permalink
fix browser and video handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Sep 8, 2024
1 parent 96f00b8 commit 6819a4d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 42 deletions.
10 changes: 5 additions & 5 deletions src/clients/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ export class DiscordClient extends EventEmitter {
}

private async onReady() {
const guilds = await this.client.guilds.fetch();
for (const [, guild] of guilds) {
const fullGuild = await guild.fetch();
this.voiceManager.scanGuild(fullGuild);
}
// const guilds = await this.client.guilds.fetch();
// for (const [, guild] of guilds) {
// const fullGuild = await guild.fetch();
// this.voiceManager.scanGuild(fullGuild);
// }
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/clients/discord/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { InterestChannels } from "./types.ts";

import { TextChannel } from "discord.js";
import { AgentRuntime } from "../../core/runtime.ts";
import { VoiceManager } from "./voice.ts";
import { stringToUuid } from "../../core/uuid.ts";
import { SpeechService } from "../../services/speech.ts";
import { VoiceManager } from "./voice.ts";

const MAX_MESSAGE_LENGTH = 1990;

Expand Down Expand Up @@ -83,8 +83,6 @@ export class MessageManager {
const name = message.author.displayName;
const channelId = message.channel.id;

await this.runtime.browserService.initialize();

try {
const { processedContent, attachments } =
await this.processMessageMedia(message);
Expand Down
2 changes: 1 addition & 1 deletion src/clients/discord/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class VoiceManager extends EventEmitter {

async handleGuildCreate(guild: Guild) {
console.log(`Joined guild ${guild.name}`);
this.scanGuild(guild);
// this.scanGuild(guild);
}

async handleUserStream(
Expand Down
15 changes: 9 additions & 6 deletions src/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,23 @@ export class BrowserService {
async getPageContent(
url: string,
): Promise<{ title: string; description: string; bodyContent: string }> {
await this.initialize();
this.queue.push(url);
this.processQueue();

return new Promise((resolve, reject) => {
const checkQueue = () => {
const checkQueue = async () => {
console.log("***** CHECKING QUEUE", this.queue);
const index = this.queue.indexOf(url);
if (index !== -1) {
setTimeout(checkQueue, 100);
} else {
resolve(
(async () =>
await this.fetchPageContent(url)) as unknown as Promise<any>,
);
try {
const result = await this.fetchPageContent(url);
resolve(result);
} catch (error) {
reject(error);
}
}
};
checkQueue();
Expand Down
5 changes: 5 additions & 0 deletions src/services/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export async function generateSummary(
runtime: AgentRuntime,
text: string,
): Promise<{ title: string; description: string }> {


// make sure text is under 128k characters
text = runtime.trimTokens(text, 100000, "gpt-4o-mini");

const prompt = `Please generate a concise summary for the following text:
Text: """
Expand Down
40 changes: 14 additions & 26 deletions src/services/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,26 @@ export class VideoService {
}

public isVideoUrl(url: string): boolean {
return (
url.includes("youtube.com") ||
url.includes("youtu.be") ||
url.includes("vimeo.com")
);
return url.includes("youtube.com") || url.includes("youtu.be") || url.includes("vimeo.com");
}

public async processVideo(url: string): Promise<Media> {
this.queue.push(url);
this.processQueue();

return new Promise((resolve, reject) => {
const checkQueue = () => {
console.log("***** CHECKING QUEUE", this.queue);
const checkQueue = async () => {
console.log("***** CHECKING VIDEO QUEUE", this.queue);
const index = this.queue.indexOf(url);
if (index !== -1) {
setTimeout(checkQueue, 100);
} else {
// ??? Might be a bug here.
resolve(
(async () =>
await this.processVideoFromUrl(url)) as unknown as Promise<Media>,
);
try {
const result = await this.processVideoFromUrl(url);
resolve(result);
} catch (error) {
reject(error);
}
}
};
checkQueue();
Expand All @@ -70,28 +67,20 @@ export class VideoService {
this.processing = true;

while (this.queue.length > 0) {
const videoUrl = this.queue.shift();
await this.processVideoFromUrl(videoUrl);
const url = this.queue.shift()!;
await this.processVideoFromUrl(url);
}

this.processing = false;
}

private async processVideoFromUrl(url: string): Promise<Media> {
// Extract YouTube ID from URL
const videoId =
url.match(
/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/watch\?.+&v=))([^\/&?]+)/,
)?.[1] || "";
const videoId = url.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/watch\?.+&v=))([^\/&?]+)/)?.[1] || "";
const videoUuid = this.getVideoId(videoId);
const cacheFilePath = path.join(
this.CONTENT_CACHE_DIR,
`${videoUuid}.json`,
);
const cacheFilePath = path.join(this.CONTENT_CACHE_DIR, `${videoUuid}.json`);

// Check if the result is already cached
if (fs.existsSync(cacheFilePath)) {
console.log("Returning cached file");
console.log("Returning cached video file");
return JSON.parse(fs.readFileSync(cacheFilePath, "utf-8")) as Media;
}

Expand All @@ -110,7 +99,6 @@ export class VideoService {
text: transcript,
};

// Cache the result
fs.writeFileSync(cacheFilePath, JSON.stringify(result));
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"lib": ["es2023"],
"lib": ["es2023", "dom"],
"moduleResolution": "node",
"outDir": "./dist",
"rootDir": "./src",
Expand Down

0 comments on commit 6819a4d

Please sign in to comment.