|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { ChatOpenAI } from "@langchain/openai"; |
| 3 | +import { createReactAgent } from "@langchain/langgraph/prebuilt"; |
| 4 | +import { SolanaAgentKit } from "solana-agent-kit"; |
| 5 | +import { |
| 6 | + CreateWrappedTokenTool, |
| 7 | + CctpTransferTool, |
| 8 | + GetWormholeSupportedChainsTool, |
| 9 | + TokenTransferTool, |
| 10 | +} from "solana-agent-kit/dist/langchain"; |
| 11 | + |
| 12 | +const llm = new ChatOpenAI({ |
| 13 | + temperature: 0.7, |
| 14 | + model: "gpt-4o-mini", |
| 15 | + cache: false, |
| 16 | +}); |
| 17 | + |
| 18 | +const solanaAgent = new SolanaAgentKit( |
| 19 | + process.env.SOLANA_PRIVATE_KEY!, |
| 20 | + process.env.RPC_URL!, |
| 21 | + process.env.OPENAI_API_KEY!, |
| 22 | +); |
| 23 | + |
| 24 | +const tools = [ |
| 25 | + new GetWormholeSupportedChainsTool(solanaAgent), |
| 26 | + new TokenTransferTool(solanaAgent), |
| 27 | + new CctpTransferTool(solanaAgent), |
| 28 | + new CreateWrappedTokenTool(solanaAgent), |
| 29 | +]; |
| 30 | + |
| 31 | +const agent = createReactAgent({ |
| 32 | + llm, |
| 33 | + tools, |
| 34 | + messageModifier: ` |
| 35 | + You are a helpful agent that specializes in cross-chain operations using Wormhole Protocol on Solana. |
| 36 | + You can help users transfer tokens between different blockchains, create wrapped tokens, and perform CCTP transfers. |
| 37 | + If you ever need funds, you can request them from the faucet. If not, you can provide your wallet details and request funds from the user. |
| 38 | + If there is a 5XX (internal) HTTP error code, ask the user to try again later. |
| 39 | + 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 |
| 40 | + themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. |
| 41 | + Be concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested. |
| 42 | + IMPORTANT: Never repeat the user's question in your response. |
| 43 | + `, |
| 44 | +}); |
| 45 | + |
| 46 | +export async function POST(req: NextRequest) { |
| 47 | + const { messages } = await req.json(); |
| 48 | + |
| 49 | + console.log("messages", messages); |
| 50 | + |
| 51 | + // Create a streaming response |
| 52 | + const encoder = new TextEncoder(); |
| 53 | + const stream = new ReadableStream({ |
| 54 | + async start(controller) { |
| 55 | + try { |
| 56 | + const agentStream = await agent.stream( |
| 57 | + { messages }, |
| 58 | + { |
| 59 | + streamMode: "values", |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + for await (const { messages: agentMessages } of agentStream) { |
| 64 | + const latestMsg = agentMessages[agentMessages.length - 1]; |
| 65 | + let chunk; |
| 66 | + |
| 67 | + if (latestMsg?.content) { |
| 68 | + // Remove any instances where the agent repeats the user's last question |
| 69 | + const lastUserMessage = messages[messages.length - 1].content; |
| 70 | + let content = latestMsg.content; |
| 71 | + |
| 72 | + // Check if the response starts with the user's question |
| 73 | + if (content.startsWith(lastUserMessage)) { |
| 74 | + content = content.substring(lastUserMessage.length).trim(); |
| 75 | + } |
| 76 | + |
| 77 | + // Check if the response contains "what is wormhole?" followed by the answer |
| 78 | + const questionPattern = new RegExp(`^${lastUserMessage}\\s*`, "i"); |
| 79 | + content = content.replace(questionPattern, ""); |
| 80 | + |
| 81 | + chunk = content; |
| 82 | + } else if (latestMsg?.tool_calls?.length > 0) { |
| 83 | + chunk = JSON.stringify(latestMsg.tool_calls); |
| 84 | + } else { |
| 85 | + chunk = JSON.stringify(latestMsg); |
| 86 | + } |
| 87 | + |
| 88 | + controller.enqueue( |
| 89 | + encoder.encode(`data: ${JSON.stringify({ chunk })}\n\n`), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + controller.enqueue(encoder.encode("data: [DONE]\n\n")); |
| 94 | + controller.close(); |
| 95 | + } catch (error) { |
| 96 | + controller.error(error); |
| 97 | + } |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + return new NextResponse(stream, { |
| 102 | + headers: { |
| 103 | + "Content-Type": "text/event-stream", |
| 104 | + "Cache-Control": "no-cache", |
| 105 | + Connection: "keep-alive", |
| 106 | + }, |
| 107 | + }); |
| 108 | +} |
0 commit comments