-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathagent.ts
More file actions
91 lines (74 loc) · 2.88 KB
/
Copy pathagent.ts
File metadata and controls
91 lines (74 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bun
/**
* Farcaster example agent entrypoint that validates Neynar credentials, loads
* workspace plugins, and keeps the polling service alive.
*/
import { AgentRuntime, type Plugin } from "@elizaos/core";
import { config as loadDotEnv } from "dotenv";
import { character } from "./character";
export function requireEnv(key: string): string {
const value = process.env[key];
if (typeof value !== "string" || value.trim().length === 0) {
throw new Error(`Missing required environment variable: ${key}`);
}
return value;
}
export function validateEnvironment(): void {
// OpenAI is the model provider for this example.
requireEnv("OPENAI_API_KEY");
// Farcaster / Neynar credentials
requireEnv("FARCASTER_FID");
requireEnv("FARCASTER_SIGNER_UUID");
requireEnv("FARCASTER_NEYNAR_API_KEY");
}
export async function main(): Promise<void> {
// Load environment variables from parent directory and current directory.
loadDotEnv({ path: "../.env" });
loadDotEnv();
console.log("🟣 Starting Farcaster Agent...\n");
try {
validateEnvironment();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.error(`❌ ${message}`);
console.error(
" Copy examples/farcaster/env.example to examples/farcaster/.env and fill in credentials.",
);
process.exit(1);
}
// Dynamically import workspace plugins (matches other examples).
const sqlPlugin = (await import("@elizaos/plugin-sql")).default;
const openaiPlugin = (await import("@elizaos/plugin-openai")).default;
const farcasterPlugin = (await import("@elizaos/plugin-farcaster")).default;
const runtime = new AgentRuntime({
character,
plugins: [sqlPlugin, openaiPlugin, farcasterPlugin] as Plugin[],
});
console.log("⏳ Initializing runtime...");
await runtime.initialize();
// Fail fast if the Farcaster service did not start.
await runtime.getServiceLoadPromise("farcaster");
console.log(`\n✅ Agent "${character.name}" is now running on Farcaster.`);
console.log(` Dry run mode: ${process.env.FARCASTER_DRY_RUN === "true"}`);
console.log(` Casting enabled: ${process.env.ENABLE_CAST === "true"}`);
console.log(
` Polling interval: ${process.env.FARCASTER_POLL_INTERVAL ?? "120"}s`,
);
console.log("\n Press Ctrl+C to stop.\n");
const shutdown = async (signal: string): Promise<void> => {
console.log(`\n${signal} received. Shutting down...`);
await runtime.stop();
process.exit(0);
};
process.on("SIGINT", () => void shutdown("SIGINT"));
process.on("SIGTERM", () => void shutdown("SIGTERM"));
// Keep process alive; the Farcaster service runs polling loops internally.
await new Promise(() => {});
}
if (import.meta.main) {
main().catch((err) => {
const message = err instanceof Error ? err.message : String(err);
console.error(`Fatal error: ${message}`);
process.exit(1);
});
}