mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-06-01 15:11:31 +00:00
chore: lint
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const fetchCache = 'force-no-store';
|
||||
export const dynamic = "force-dynamic";
|
||||
export const fetchCache = "force-no-store";
|
||||
export const maxDuration = 300;
|
||||
|
||||
import { Bot, webhookCallback } from 'grammy';
|
||||
import { Bot, webhookCallback } from "grammy";
|
||||
import { SolanaAgentKit, createSolanaTools } from "solana-agent-kit";
|
||||
import { ChatOpenAI } from "@langchain/openai";
|
||||
import { MemorySaver } from "@langchain/langgraph";
|
||||
@@ -10,10 +10,11 @@ import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
||||
import { HumanMessage } from "@langchain/core/messages";
|
||||
|
||||
const token = process.env.TELEGRAM_BOT_TOKEN;
|
||||
if (!token) throw new Error('TELEGRAM_BOT_TOKEN environment variable not found.');
|
||||
if (!token) {
|
||||
throw new Error("TELEGRAM_BOT_TOKEN environment variable not found.");
|
||||
}
|
||||
const bot = new Bot(token);
|
||||
|
||||
|
||||
async function initializeAgent(userId: string) {
|
||||
try {
|
||||
const llm = new ChatOpenAI({
|
||||
@@ -24,7 +25,7 @@ async function initializeAgent(userId: string) {
|
||||
const solanaKit = new SolanaAgentKit(
|
||||
process.env.SOLANA_PRIVATE_KEY!,
|
||||
process.env.RPC_URL,
|
||||
process.env.OPENAI_API_KEY!
|
||||
process.env.OPENAI_API_KEY!,
|
||||
);
|
||||
|
||||
const tools = createSolanaTools(solanaKit);
|
||||
@@ -51,24 +52,40 @@ async function initializeAgent(userId: string) {
|
||||
}
|
||||
}
|
||||
// Telegram bot handler
|
||||
bot.on('message:text', async (ctx:any) => {
|
||||
bot.on("message:text", async (ctx: any) => {
|
||||
const userId = ctx.from?.id.toString();
|
||||
if (!userId) return;
|
||||
const {agent, config} = await initializeAgent(userId);
|
||||
const stream = await agent.stream({ messages: [new HumanMessage(ctx.message.text)] }, config);
|
||||
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 20000));
|
||||
if (!userId) {
|
||||
return;
|
||||
}
|
||||
const { agent, config } = await initializeAgent(userId);
|
||||
const stream = await agent.stream(
|
||||
{ messages: [new HumanMessage(ctx.message.text)] },
|
||||
config,
|
||||
);
|
||||
const timeoutPromise = new Promise((_, reject) =>
|
||||
setTimeout(() => reject(new Error("Timeout")), 20000),
|
||||
);
|
||||
try {
|
||||
for await (const chunk of await Promise.race([stream, timeoutPromise]) as AsyncIterable<{ agent?: any; tools?: any }>) {
|
||||
for await (const chunk of (await Promise.race([
|
||||
stream,
|
||||
timeoutPromise,
|
||||
])) as AsyncIterable<{ agent?: any; tools?: any }>) {
|
||||
if ("agent" in chunk) {
|
||||
if (chunk.agent.messages[0].content) await ctx.reply(String(chunk.agent.messages[0].content));
|
||||
}
|
||||
if (chunk.agent.messages[0].content) {
|
||||
await ctx.reply(String(chunk.agent.messages[0].content));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Timeout') {
|
||||
await ctx.reply("I'm sorry, the operation took too long and timed out. Please try again.");
|
||||
if (error.message === "Timeout") {
|
||||
await ctx.reply(
|
||||
"I'm sorry, the operation took too long and timed out. Please try again.",
|
||||
);
|
||||
} else {
|
||||
console.error("Error processing stream:", error);
|
||||
await ctx.reply("I'm sorry, an error occurred while processing your request.");
|
||||
await ctx.reply(
|
||||
"I'm sorry, an error occurred while processing your request.",
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -77,10 +94,10 @@ bot.on('message:text', async (ctx:any) => {
|
||||
export const POST = async (req: Request) => {
|
||||
// Mark the function as a background function for Vercel
|
||||
const headers = new Headers();
|
||||
headers.set('x-vercel-background', 'true');
|
||||
headers.set("x-vercel-background", "true");
|
||||
|
||||
const handler = webhookCallback(bot, 'std/http'); // Use the correct callback
|
||||
const handler = webhookCallback(bot, "std/http"); // Use the correct callback
|
||||
|
||||
// Handle the incoming webhook request
|
||||
return handler(req);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user