diff --git a/examples/agent-kit-langgraph/README.json b/examples/agent-kit-langgraph/README.json deleted file mode 100644 index e69de29..0000000 diff --git a/examples/agent-kit-langgraph/README.md b/examples/agent-kit-langgraph/README.md new file mode 100644 index 0000000..cd4ea37 --- /dev/null +++ b/examples/agent-kit-langgraph/README.md @@ -0,0 +1,83 @@ +# Agent Kit LangGraph Example + +This example demonstrates how to build an advanced Solana agent using LangGraph and the Solana Agent Kit. It showcases a multi-agent system that can handle various Solana-related tasks through a directed workflow. + +## Features + +- Multi-agent architecture using LangGraph's StateGraph +- Specialized agents for different tasks: + - General purpose agent for basic queries + - Transfer/Swap agent for transaction operations + - Read agent for blockchain data queries + - Manager agent for routing and orchestration +- Environment-based configuration +- TypeScript implementation with full type safety + +## Prerequisites + +- Node.js (v16 or higher) +- pnpm package manager +- Solana development environment + +## Installation + +1. Clone the repository and navigate to the example directory: +```bash +cd examples/agent-kit-langgraph +``` + +2. Install dependencies: +```bash +pnpm install +``` + +3. Configure environment variables: +```bash +cp .env.example .env +``` + +Edit the `.env` file with your configuration: +- Add your OpenAI API key +- Configure any other required environment variables + +## Project Structure + +``` +src/ +├── agents/ # Individual agent implementations +├── helper/ # Helper utilities and examples +├── prompts/ # Agent prompts and templates +├── tools/ # Custom tools for agents +└── utils/ # Utility functions and configurations +``` + +## Usage + +To run the example: + +```bash +pnpm dev src/index.ts +``` + +The example demonstrates a workflow where: +1. The manager agent receives the initial query +2. Based on the query type, it routes to the appropriate specialized agent: + - General queries → Generalist Agent + - Transfer/Swap operations → TransferSwap Agent + - Blockchain data queries → Read Agent + +## Dependencies + +- `@langchain/community`: LangChain community tools and utilities +- `@langchain/core`: Core LangChain functionality +- `@langchain/langgraph`: Graph-based agent workflows +- `solana-agent-kit`: Solana Agent Kit for blockchain interactions +- `zod`: Runtime type checking + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +## License + +ISC License diff --git a/examples/agent-kit-langgraph/langgraph.json b/examples/agent-kit-langgraph/langgraph.json index 1e630f6..59a83cf 100644 --- a/examples/agent-kit-langgraph/langgraph.json +++ b/examples/agent-kit-langgraph/langgraph.json @@ -1,7 +1,9 @@ { + "node_version": "20", + "dockerfile_lines": [], + "dependencies": ["."], "graphs": { - "solana-agent": "./index.ts:graph" + "solanaAgent": "./src/index.ts:graph" }, - "env": ".env", - "node_version": "20" -} \ No newline at end of file + "env": ".env" + } \ No newline at end of file diff --git a/examples/agent-kit-langgraph/src/agents/readAgent.ts b/examples/agent-kit-langgraph/src/agents/readAgent.ts new file mode 100644 index 0000000..7875c54 --- /dev/null +++ b/examples/agent-kit-langgraph/src/agents/readAgent.ts @@ -0,0 +1,22 @@ +import { createReactAgent } from "@langchain/langgraph/prebuilt"; +import { gpt4o } from "../utils/model"; +import { solanaAgentState } from "../utils/state"; +import { HumanMessage } from "@langchain/core/messages"; +import { agentKit } from "../utils/solanaAgent"; +import { + SolanaBalanceTool, + SolanaFetchPriceTool, +} from "solana-agent-kit/dist/langchain"; + +const readAgent = createReactAgent({ + llm: gpt4o, + tools: [new SolanaBalanceTool(agentKit), new SolanaFetchPriceTool(agentKit)], +}); + +export const readNode = async (state: typeof solanaAgentState.State) => { + const { messages } = state; + + const result = await readAgent.invoke({ messages }); + + return { messages: [...result.messages] }; +}; diff --git a/examples/agent-kit-langgraph/src/helper/examples.ts b/examples/agent-kit-langgraph/src/helper/examples.ts index d88dcd2..5aa86ea 100644 --- a/examples/agent-kit-langgraph/src/helper/examples.ts +++ b/examples/agent-kit-langgraph/src/helper/examples.ts @@ -3,3 +3,7 @@ import { HumanMessage } from "@langchain/core/messages"; export const generalQuestion = [ new HumanMessage("Who is the president of Ecuador?"), ]; + +export const solanaReadQuery = [new HumanMessage("what is the price of SOL")]; + +export const solanaWriteQuery = [new HumanMessage("swap 0.1 usdc to sol")]; diff --git a/examples/agent-kit-langgraph/src/index.ts b/examples/agent-kit-langgraph/src/index.ts index 5ac5302..b57c0c2 100644 --- a/examples/agent-kit-langgraph/src/index.ts +++ b/examples/agent-kit-langgraph/src/index.ts @@ -3,6 +3,7 @@ import { solanaAgentState } from "./utils/state"; import { generalistNode } from "./agents/generalAgent"; import { transferSwapNode } from "./agents/transferOrSwap"; import { managerNode } from "./agents/manager"; +import { readNode } from "./agents/readAgent"; import { START, END } from "@langchain/langgraph"; import { managerRouter } from "./utils/route"; import { HumanMessage } from "@langchain/core/messages"; @@ -11,15 +12,17 @@ const workflow = new StateGraph(solanaAgentState) .addNode("generalist", generalistNode) .addNode("manager", managerNode) .addNode("transferSwap", transferSwapNode) + .addNode("read", readNode) .addEdge(START, "manager") .addConditionalEdges("manager", managerRouter) .addEdge("generalist", END) - .addEdge("transferSwap", END); + .addEdge("transferSwap", END) + .addEdge("read", END); export const graph = workflow.compile(); const result = await graph.invoke({ - messages: [new HumanMessage("swap 0.1 usdc to sol")], + messages: [new HumanMessage("what is the price of SOL")], }); console.log(result); diff --git a/examples/agent-kit-langgraph/src/utils/route.ts b/examples/agent-kit-langgraph/src/utils/route.ts index bb45fae..31aefb0 100644 --- a/examples/agent-kit-langgraph/src/utils/route.ts +++ b/examples/agent-kit-langgraph/src/utils/route.ts @@ -8,6 +8,8 @@ export const managerRouter = (state: typeof solanaAgentState.State) => { return "generalist"; } else if (isSolanaWriteQuery) { return "transferSwap"; + } else if (isSolanaReadQuery) { + return "read"; } else { return END; }