UNPKG

sei-agent-kit

Version:

A package for building AI agents on the SEI blockchain

157 lines 7.84 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.setupAgent = setupAgent; exports.startInteractiveSession = startInteractiveSession; const src_1 = require("../src"); const langchain_1 = require("../src/langchain"); const messages_1 = require("@langchain/core/messages"); const langgraph_1 = require("@langchain/langgraph"); const prebuilt_1 = require("@langchain/langgraph/prebuilt"); const openai_1 = require("@langchain/openai"); const dotenv = __importStar(require("dotenv")); const readline = __importStar(require("readline")); const types_1 = require("../src/types"); dotenv.config(); function checkRequiredEnvVars() { const missingVars = []; const requiredVars = ["OPENAI_API_KEY", "SEI_PRIVATE_KEY", "RPC_URL"]; requiredVars.forEach((varName) => { if (!process.env[varName]) { missingVars.push(varName); } }); 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); } } async function setupAgent() { try { const llm = new openai_1.ChatOpenAI({ model: "gpt-4o", temperature: 0, }); const agentInstance = new src_1.SeiAgentKit(process.env.SEI_PRIVATE_KEY, types_1.ModelProviderName.OPENAI); const agentTools = (0, langchain_1.createSeiTools)(agentInstance); const memory = new langgraph_1.MemorySaver(); const agentConfig = { configurable: { thread_id: "Sei Agent Kit!" } }; const agent = (0, prebuilt_1.createReactAgent)({ llm, tools: agentTools, checkpointSaver: memory, messageModifier: ` You are a lively and witty agent created by Cambrian AI, designed to interact onchain using the Sei Agent Kit. You have a knack for humor and enjoy making the interaction enjoyable while being efficient. If there is a 5XX (internal) HTTP error code, humorously suggest the user try again later. All users' wallet infos are already provided on the tool kit. If someone asks you to do something you can't do with your currently available tools, respond with a playful apology and encourage them to implement it themselves using the Sei Agent Kit repository that they can find on https://github.com/CambrianAgents/sei-agent-kit. Suggest they visit the Twitter account https://x.com/cambrian_ai or the website https://www.cambrian.wtf/ for more information, perhaps with a light-hearted comment about the wonders of the internet. Be concise, helpful, and sprinkle in some humor with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested. If the user tries to exit the conversation, cheerfully inform them that by typing "bye" they can end the conversation, maybe with a friendly farewell message. `, }); return { agent, config: agentConfig }; } catch (error) { console.error("Failed to initialize agent:", error); throw error; } } async function startInteractiveSession(agent, config) { console.log("\nStarting chat with the Cambrian Agent... Type 'bye' to end."); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const question = (prompt) => new Promise((resolve) => rl.question(prompt, resolve)); try { while (true) { const userInput = await question("\nYou: "); if (userInput.toLowerCase() === "bye") { break; } const responseStream = await agent.stream({ messages: [new messages_1.HumanMessage(userInput)] }, config); for await (const responseChunk of responseStream) { if ("agent" in responseChunk) { console.log("\nCambrian Agent:", responseChunk.agent.messages[0].content); } else if ("tools" in responseChunk) { console.log("\nCambrian Agent:", responseChunk.tools.messages[0].content); } console.log("\n-----------------------------------\n"); } } } catch (error) { if (error instanceof Error) { console.error("Error:", error.message); } process.exit(1); } finally { rl.close(); } } async function main() { try { console.log('\x1b[38;2;201;235;52m%s\x1b[0m', ` ███████╗███████╗██╗ █████╗ ██████╗ ███████╗███╗ ██╗████████╗ ██╗ ██╗██╗████████╗ ██╔════╝██╔════╝██║ ██╔══██╗██╔════╝ ██╔════╝████╗ ██║╚══██╔══╝ ██║ ██╔╝██║╚══██╔══╝ ███████╗█████╗ ██║ ███████║██║ ███╗█████╗ ██╔██╗ ██║ ██║ █████╔╝ ██║ ██║ ╚════██║██╔══╝ ██║ ██╔══██║██║ ██║██╔══╝ ██║╚██╗██║ ██║ ██╔═██╗ ██║ ██║ ███████║███████╗██║ ██║ ██║╚██████╔╝███████╗██║ ╚████║ ██║ ██║ ██╗██║ ██║ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ `); const { agent, config } = await setupAgent(); await startInteractiveSession(agent, config); } catch (error) { if (error instanceof Error) { console.error("Error:", error.message); } process.exit(1); } } if (require.main === module) { checkRequiredEnvVars(); main().catch((error) => { console.error("Fatal error:", error); process.exit(1); }); } //# sourceMappingURL=index.js.map