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

feat: make node-plugin lazy-loaded for faster boot times #599

Merged
merged 2 commits into from
Nov 26, 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
7 changes: 6 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
import { confluxPlugin } from "@ai16z/plugin-conflux";
import { solanaPlugin } from "@ai16z/plugin-solana";
import { zgPlugin } from "@ai16z/plugin-0g";
import { nodePlugin } from "@ai16z/plugin-node";
import { type NodePlugin, createNodePlugin } from "@ai16z/plugin-node";
import {
coinbaseCommercePlugin,
coinbaseMassPaymentsPlugin,
Expand Down Expand Up @@ -239,6 +239,8 @@ function getSecret(character: Character, secret: string) {
return character.settings.secrets?.[secret] || process.env[secret];
}

let nodePlugin: NodePlugin | undefined;

export function createAgent(
character: Character,
db: IDatabaseAdapter,
Expand All @@ -250,6 +252,9 @@ export function createAgent(
"Creating runtime for character",
character.name
);

nodePlugin ??= createNodePlugin();

return new AgentRuntime({
databaseAdapter: db,
token,
Expand Down
31 changes: 17 additions & 14 deletions packages/plugin-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import {
VideoService,
} from "./services/index.ts";

export const nodePlugin: Plugin = {
name: "default",
description: "Default plugin, with basic actions and evaluators",
services: [
new BrowserService(),
new ImageDescriptionService(),
new LlamaService(),
new PdfService(),
new SpeechService(),
new TranscriptionService(),
new VideoService(),
],
};

export default nodePlugin;
export type NodePlugin = ReturnType<typeof createNodePlugin>

export function createNodePlugin() {
return {
name: "default",
description: "Default plugin, with basic actions and evaluators",
services: [
new BrowserService(),
new ImageDescriptionService(),
new LlamaService(),
new PdfService(),
new SpeechService(),
new TranscriptionService(),
new VideoService(),
],
} as const satisfies Plugin
};
8 changes: 5 additions & 3 deletions packages/plugin-node/src/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ 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"],
Expand Down Expand Up @@ -118,7 +120,7 @@ export class BrowserService extends Service implements IBrowserService {
url: string,
runtime: IAgentRuntime
): Promise<PageContent> {
await this.initialize();
await this.initializeBrowser();
this.queue.push(url);
this.processQueue(runtime);

Expand Down Expand Up @@ -181,7 +183,7 @@ export class BrowserService extends Service implements IBrowserService {
try {
if (!this.context) {
console.log(
"Browser context not initialized. Call initialize() first."
"Browser context not initialized. Call initializeBrowser() first."
);
}

Expand Down