Skip to content

Commit

Permalink
Merge pull request #653 from cygaar/playwright_improvements
Browse files Browse the repository at this point in the history
feat: improve browser service
  • Loading branch information
lalalune authored Nov 28, 2024
2 parents bc80654 + d274ef7 commit dbec9fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 82 deletions.
18 changes: 5 additions & 13 deletions packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ import {
discordShouldRespondTemplate,
discordMessageHandlerTemplate,
} from "./templates.ts";
import {
generateSummary,
sendMessageInChunks,
canSendMessage,
} from "./utils.ts";
import { sendMessageInChunks, canSendMessage } from "./utils.ts";

export type InterestChannels = {
[key: string]: {
Expand Down Expand Up @@ -410,20 +406,16 @@ export class MessageManager {
throw new Error("Browser service not found");
}

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

const { title: newTitle, description } = await generateSummary(
this.runtime,
title + "\n" + bodyContent
);
attachments.push({
id: `webpage-${Date.now()}`,
url: url,
title: newTitle || "Web Page",
title: title || "Web Page",
source: "Web",
description,
text: bodyContent,
description: summary,
text: summary,
});
}
}
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,13 +953,9 @@ Text: ${attachment.text}
.join(" ");
}


const knowledegeData = await knowledge.get(this, message);

const formattedKnowledge = formatKnowledge(
knowledegeData
);

const formattedKnowledge = formatKnowledge(knowledegeData);

const initialState = {
agentId: this.agentId,
Expand Down Expand Up @@ -1242,5 +1238,7 @@ Text: ${attachment.text}
}

const formatKnowledge = (knowledge: KnowledgeItem[]) => {
return knowledge.map((knowledge) => `- ${knowledge.content.text}`).join("\n");
return knowledge
.map((knowledge) => `- ${knowledge.content.text}`)
.join("\n");
};
1 change: 1 addition & 0 deletions packages/plugin-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"youtube-dl-exec": "3.0.10"
},
"devDependencies": {
"@types/node": "22.8.4",
"eslint": "9.13.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.2.1",
Expand Down
83 changes: 35 additions & 48 deletions packages/plugin-node/src/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export class BrowserService extends Service implements IBrowserService {
private captchaSolver: CaptchaSolver;
private cacheKey = "content/browser";

private queue: string[] = [];
private processing: boolean = false;

static serviceType: ServiceType = ServiceType.BROWSER;

static register(runtime: IAgentRuntime): IAgentRuntime {
Expand All @@ -87,17 +84,43 @@ export class BrowserService extends Service implements IBrowserService {
);
}

async initialize() { }
async initialize() {}

async initializeBrowser() {
if (!this.browser) {
this.browser = await chromium.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: true,
args: [
"--disable-dev-shm-usage", // Uses /tmp instead of /dev/shm. Prevents memory issues on low-memory systems
"--block-new-web-contents", // Prevents creation of new windows/tabs
],
});

const platform = process.platform;
let userAgent = "";

// Change the user agent to match the platform to reduce bot detection
switch (platform) {
case "darwin":
userAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
break;
case "win32":
userAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
break;
case "linux":
userAgent =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
break;
default:
userAgent =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
}

this.context = await this.browser.newContext({
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
userAgent,
acceptDownloads: false,
});

this.blocker =
Expand All @@ -121,49 +144,13 @@ export class BrowserService extends Service implements IBrowserService {
runtime: IAgentRuntime
): Promise<PageContent> {
await this.initializeBrowser();
this.queue.push(url);
this.processQueue(runtime);

return new Promise((resolve, reject) => {
const checkQueue = async () => {
const index = this.queue.indexOf(url);
if (index !== -1) {
setTimeout(checkQueue, 100);
} else {
try {
const result = await this.fetchPageContent(
url,
runtime
);
resolve(result);
} catch (error) {
reject(error);
}
}
};
checkQueue();
});
return await this.fetchPageContent(url, runtime);
}

private getCacheKey(url: string): string {
return stringToUuid(url);
}

private async processQueue(runtime: IAgentRuntime): Promise<void> {
if (this.processing || this.queue.length === 0) {
return;
}

this.processing = true;

while (this.queue.length > 0) {
const url = this.queue.shift();
await this.fetchPageContent(url, runtime);
}

this.processing = false;
}

private async fetchPageContent(
url: string,
runtime: IAgentRuntime
Expand Down Expand Up @@ -214,15 +201,15 @@ export class BrowserService extends Service implements IBrowserService {
if (captchaDetected) {
await this.solveCaptcha(page, url);
}
const title = await page.evaluate(() => document.title);
const documentTitle = await page.evaluate(() => document.title);
const bodyContent = await page.evaluate(
() => document.body.innerText
);
const { description } = await generateSummary(
const { title: parsedTitle, description } = await generateSummary(
runtime,
title + "\n" + bodyContent
documentTitle + "\n" + bodyContent
);
const content = { title, description, bodyContent };
const content = { title: parsedTitle, description, bodyContent };
await runtime.cacheManager.set(`${this.cacheKey}/${cacheKey}`, {
url,
content,
Expand Down
33 changes: 18 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbec9fd

Please sign in to comment.