-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.ts
420 lines (371 loc) · 12.4 KB
/
chatbot.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import { CdpAgentkit } from "@coinbase/cdp-agentkit-core";
import { CdpTool, CdpToolkit } from "@coinbase/cdp-langchain";
import { HumanMessage } from "@langchain/core/messages";
import { MemorySaver } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";
import * as fs from "fs";
import * as readline from "readline";
import { Wallet, Webhook } from "@coinbase/coinbase-sdk";
import { z } from "zod";
dotenv.config();
/**
* Validates that required environment variables are set
*
* @throws {Error} - If required environment variables are missing
* @returns {void}
*/
function validateEnvironment(): void {
const missingVars: string[] = [];
// Check required variables
const requiredVars = ["CDP_API_KEY_NAME", "CDP_API_KEY_PRIVATE_KEY"];
requiredVars.forEach((varName) => {
if (!process.env[varName]) {
missingVars.push(varName);
}
});
// Exit if any required variables are missing
if (missingVars.length > 0) {
console.error("Error: Required environment variables are not set");
missingVars.forEach((varName) => {
console.error(`${varName}=your_${varName.toLowerCase()}_here`);
});
process.exit(1);
}
// Warn about optional NETWORK_ID
if (!process.env.NETWORK_ID) {
console.warn(
"Warning: NETWORK_ID not set, defaulting to base-sepolia testnet",
);
}
}
// Add this right after imports and before any other code
validateEnvironment();
// Configure a file to persist the agent's CDP MPC Wallet Data
const WALLET_DATA_FILE = "wallet_data.txt";
// Define the webhook event types
const WebhookEventType = z.enum([
"wallet_activity",
"smart_contract_event_activity",
"erc20_transfer",
"erc721_transfer",
]);
// Create a flexible event filters schema
const EventFilters = z
.object({
addresses: z
.array(z.string())
.optional()
.describe("List of wallet or contract addresses to monitor"),
from_address: z
.string()
.optional()
.describe("Sender address for token transfers"),
to_address: z
.string()
.optional()
.describe("Recipient address for token transfers"),
contract_address: z
.string()
.optional()
.describe("Contract address for token transfers"),
})
.refine(
(data) => {
// Ensure at least one filter is provided
return Object.keys(data).length > 0;
},
{ message: "At least one filter must be provided" },
);
const CreateWebhookInput = z.object({
notificationUri: z
.string()
.url()
.describe("The callback URL where webhook events will be sent"),
eventType: WebhookEventType,
eventTypeFilter: z.object({
addresses: z
.array(z.string())
.describe("List of wallet or contract addresses to monitor"),
}),
eventFilters: EventFilters.optional(),
});
/**
* Creates a new webhook for monitoring on-chain events
*
* @param wallet - The wallet used to create the webhook
* @param args - Object with arguments needed
* @returns Details of the created webhook
*/
async function createWebhook(
wallet: Wallet,
args: {
notificationUri: string;
eventType: z.infer<typeof WebhookEventType>;
eventTypeFilter: { addresses: string[] };
eventFilters?: z.infer<typeof EventFilters>;
},
): Promise<string> {
try {
const { notificationUri, eventType, eventTypeFilter, eventFilters } = args;
// Use the environment variable for networkId
const networkId = process.env.NETWORK_ID;
if (!networkId) {
throw new Error("Network ID is not configured in environment variables");
}
const webhookOptions: any = {
networkId,
notificationUri,
eventType,
};
// Handle different event types with appropriate filtering
switch (eventType) {
case "wallet_activity":
webhookOptions.eventTypeFilter = {
addresses: eventTypeFilter.addresses || [],
wallet_id: "", // this is required by SDK, but can be an empty value
};
break;
case "smart_contract_event_activity":
webhookOptions.eventTypeFilter = {
contract_addresses: eventTypeFilter.addresses || [],
};
break;
case "erc20_transfer":
case "erc721_transfer":
webhookOptions.eventFilters = [
{
...(eventFilters?.contract_address
? { contract_address: eventFilters.contract_address }
: {}),
...(eventFilters?.from_address
? { from_address: eventFilters.from_address }
: {}),
...(eventFilters?.to_address
? { to_address: eventFilters.to_address }
: {}),
},
];
break;
default:
throw new Error(`Unsupported event type: ${eventType}`);
}
// Create webhook
const webhook = await Webhook.create(webhookOptions);
return `The webhook was successfully created: ${webhook?.toString()}\n\n`;
// return webhook
} catch (error) {
console.error("Failed to create webhook:", error);
return `Error: ${error}`;
}
}
/**
* Initialize the agent with CDP Agentkit
*
* @returns Agent executor and config
*/
async function initializeAgent() {
try {
// Initialize LLM with xAI configuration
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY,
});
let walletDataStr: string | null = null;
// Read existing wallet data if available
if (fs.existsSync(WALLET_DATA_FILE)) {
try {
walletDataStr = fs.readFileSync(WALLET_DATA_FILE, "utf8");
} catch (error) {
console.error("Error reading wallet data:", error);
// Continue without wallet data
}
}
// Configure CDP Agentkit
const config = {
cdpWalletData: walletDataStr || undefined,
networkId: process.env.NETWORK_ID || "base-sepolia",
};
// Initialize CDP agentkit
const agentkit = await CdpAgentkit.configureWithWallet(config);
// Initialize CDP Agentkit Toolkit and get tools
const cdpToolkit = new CdpToolkit(agentkit);
const tools = cdpToolkit.getTools();
// Define the descriptive prompt for the action
const CREATE_WEBHOOK_PROMPT = `
Create a new webhook to receive real-time updates for on-chain events.
Supports monitoring wallet activity or smart contract events by specifying:
- Callback URL for receiving events
- Event type (wallet_activity, smart_contract_event_activity, erc20_transfer or erc721_transfer)
- Addresses to monitor
When event type is erc20_transfer or erc721_transfer at least one of these filters needs to be provided (only one of them is required):
- Contract address to listen for token transfers
- Sender address for erc20_transfer and erc721_transfer events (listen on transfers originating from this address))
- Recipient address for erc20_transfer and erc721_transfer events (listen on transfers being made to this address))
`;
// Add the webhook tool
const createWebhookTool = new CdpTool(
{
name: "create_webhook",
description: CREATE_WEBHOOK_PROMPT,
argsSchema: CreateWebhookInput,
func: createWebhook,
},
agentkit, // Assumes this is the CdpWrapper instance
);
tools.push(createWebhookTool);
// Store buffered conversation history in memory
const memory = new MemorySaver();
const agentConfig = {
configurable: { thread_id: "CDP Agentkit Chatbot Example!" },
};
// Create React Agent using the LLM and CDP Agentkit tools
const agent = createReactAgent({
llm,
tools,
checkpointSaver: memory,
messageModifier:
"You are a helpful agent that can interact onchain using the Coinbase Developer Platform Agentkit. You are empowered to interact onchain using your tools. If you ever need funds, you can request them from the faucet if you are on network ID `base-sepolia`. If not, you can provide your wallet details and request funds from the user. If someone asks you to do something you can't do with your currently available tools, you must say so, and encourage them to implement it themselves using the CDP SDK + Agentkit, recommend they go to docs.cdp.coinbase.com for more informaton. Be concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.",
});
// Save wallet data
const exportedWallet = await agentkit.exportWallet();
fs.writeFileSync(WALLET_DATA_FILE, exportedWallet);
return { agent, config: agentConfig };
} catch (error) {
console.error("Failed to initialize agent:", error);
throw error; // Re-throw to be handled by caller
}
}
/**
* Run the agent autonomously with specified intervals
*
* @param agent - The agent executor
* @param config - Agent configuration
* @param interval - Time interval between actions in seconds
*/
async function runAutonomousMode(agent: any, config: any, interval = 10) {
console.log("Starting autonomous mode...");
while (true) {
try {
const thought =
"Be creative and do something interesting on the blockchain. " +
"Choose an action or set of actions and execute it that highlights your abilities.";
const stream = await agent.stream(
{ messages: [new HumanMessage(thought)] },
config,
);
for await (const chunk of stream) {
if ("agent" in chunk) {
console.log(chunk.agent.messages[0].content);
} else if ("tools" in chunk) {
console.log(chunk.tools.messages[0].content);
}
console.log("-------------------");
}
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
} catch (error) {
if (error instanceof Error) {
console.error("Error:", error.message);
}
process.exit(1);
}
}
}
/**
* Run the agent interactively based on user input
*
* @param agent - The agent executor
* @param config - Agent configuration
*/
async function runChatMode(agent: any, config: any) {
console.log("Starting chat mode... Type 'exit' to end.");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (prompt: string): Promise<string> =>
new Promise((resolve) => rl.question(prompt, resolve));
try {
while (true) {
const userInput = await question("\nPrompt: ");
if (userInput.toLowerCase() === "exit") {
break;
}
const stream = await agent.stream(
{ messages: [new HumanMessage(userInput)] },
config,
);
for await (const chunk of stream) {
if ("agent" in chunk) {
console.log(chunk.agent.messages[0].content);
} else if ("tools" in chunk) {
console.log(chunk.tools.messages[0].content);
}
console.log("-------------------");
}
}
} catch (error) {
if (error instanceof Error) {
console.error("Error:", error.message);
}
process.exit(1);
} finally {
rl.close();
}
}
/**
* Choose whether to run in autonomous or chat mode based on user input
*
* @returns Selected mode
*/
async function chooseMode(): Promise<"chat" | "auto"> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (prompt: string): Promise<string> =>
new Promise((resolve) => rl.question(prompt, resolve));
while (true) {
console.log("\nAvailable modes:");
console.log("1. chat - Interactive chat mode");
console.log("2. auto - Autonomous action mode");
const choice = (await question("\nChoose a mode (enter number or name): "))
.toLowerCase()
.trim();
if (choice === "1" || choice === "chat") {
rl.close();
return "chat";
} else if (choice === "2" || choice === "auto") {
rl.close();
return "auto";
}
console.log("Invalid choice. Please try again.");
}
}
/**
* Start the chatbot agent
*/
async function main() {
try {
const { agent, config } = await initializeAgent();
const mode = await chooseMode();
if (mode === "chat") {
await runChatMode(agent, config);
} else {
await runAutonomousMode(agent, config);
}
} catch (error) {
if (error instanceof Error) {
console.error("Error:", error.message);
}
process.exit(1);
}
}
if (require.main === module) {
console.log("Starting Agent...");
main().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});
}