Skip to content

Commit 1670600

Browse files
authored
Merge pull request #599 from bmgalego/node-plugin-lazy
feat: make node-plugin lazy-loaded for faster boot times
2 parents f0f31c2 + f45f6fe commit 1670600

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

agent/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
2626
import { confluxPlugin } from "@ai16z/plugin-conflux";
2727
import { solanaPlugin } from "@ai16z/plugin-solana";
2828
import { zgPlugin } from "@ai16z/plugin-0g";
29-
import { nodePlugin } from "@ai16z/plugin-node";
29+
import { type NodePlugin, createNodePlugin } from "@ai16z/plugin-node";
3030
import {
3131
coinbaseCommercePlugin,
3232
coinbaseMassPaymentsPlugin,
@@ -238,6 +238,8 @@ function getSecret(character: Character, secret: string) {
238238
return character.settings.secrets?.[secret] || process.env[secret];
239239
}
240240

241+
let nodePlugin: NodePlugin | undefined;
242+
241243
export function createAgent(
242244
character: Character,
243245
db: IDatabaseAdapter,
@@ -249,6 +251,9 @@ export function createAgent(
249251
"Creating runtime for character",
250252
character.name
251253
);
254+
255+
nodePlugin ??= createNodePlugin();
256+
252257
return new AgentRuntime({
253258
databaseAdapter: db,
254259
token,

packages/plugin-node/src/index.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ import {
1212
VideoService,
1313
} from "./services/index.ts";
1414

15-
export const nodePlugin: Plugin = {
16-
name: "default",
17-
description: "Default plugin, with basic actions and evaluators",
18-
services: [
19-
new BrowserService(),
20-
new ImageDescriptionService(),
21-
new LlamaService(),
22-
new PdfService(),
23-
new SpeechService(),
24-
new TranscriptionService(),
25-
new VideoService(),
26-
],
27-
};
2815

29-
export default nodePlugin;
16+
export type NodePlugin = ReturnType<typeof createNodePlugin>
17+
18+
export function createNodePlugin() {
19+
return {
20+
name: "default",
21+
description: "Default plugin, with basic actions and evaluators",
22+
services: [
23+
new BrowserService(),
24+
new ImageDescriptionService(),
25+
new LlamaService(),
26+
new PdfService(),
27+
new SpeechService(),
28+
new TranscriptionService(),
29+
new VideoService(),
30+
],
31+
} as const satisfies Plugin
32+
};

packages/plugin-node/src/services/browser.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ export class BrowserService extends Service implements IBrowserService {
8787
);
8888
}
8989

90-
async initialize() {
90+
async initialize() { }
91+
92+
async initializeBrowser() {
9193
if (!this.browser) {
9294
this.browser = await chromium.launch({
9395
args: ["--no-sandbox", "--disable-setuid-sandbox"],
@@ -118,7 +120,7 @@ export class BrowserService extends Service implements IBrowserService {
118120
url: string,
119121
runtime: IAgentRuntime
120122
): Promise<PageContent> {
121-
await this.initialize();
123+
await this.initializeBrowser();
122124
this.queue.push(url);
123125
this.processQueue(runtime);
124126

@@ -181,7 +183,7 @@ export class BrowserService extends Service implements IBrowserService {
181183
try {
182184
if (!this.context) {
183185
console.log(
184-
"Browser context not initialized. Call initialize() first."
186+
"Browser context not initialized. Call initializeBrowser() first."
185187
);
186188
}
187189

0 commit comments

Comments
 (0)