Skip to content

Instantly share code, notes, and snippets.

@sarvagnakadiya
Created April 21, 2025 13:47
Show Gist options
  • Save sarvagnakadiya/3234787040faec686da194625d89c4a1 to your computer and use it in GitHub Desktop.
Save sarvagnakadiya/3234787040faec686da194625d89c4a1 to your computer and use it in GitHub Desktop.
/Users/sarvagnakadiya/projects/ek-leaf/ekcore/packages/tests/src/index.ts
import dotenv from "dotenv";
dotenv.config();
import { anthropic } from "@ek/anthropic";
import {
agent,
AgentStream,
AgentToolCall,
AgentToolCallResult,
StopEvent,
} from "ek";
import { blockchainTools } from "./tools/blockchain";
import { farcasterTools } from "./tools/farcaster";
import { account } from "./constants";
import chalk from "chalk";
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
console.log(chalk.green("WebSocket server started on ws://localhost:8080"));
const createAgent = () =>
agent({
tools: [...farcasterTools, ...blockchainTools],
llm: anthropic({
model: "claude-3-7-sonnet-20250219",
temperature: 0.5,
}),
systemPrompt: `Your name is Earnkit (Agent).
- Behavioral Guidelines:
1. NEVER be rude to user
2. NEVER try to be over profess̃ional
3. ALWAYS be friendly to the user
4. NEVER act over politely
4. ALWAYS be concise and to the point
Response Formatting:
- Use proper line breaks between different sections of your response for better readability
- Utilize markdown features effectively to enhance the structure of your response
- Keep responses concise and well-organized
- Use emojis sparingly and only when appropriate for the context
- Use an abbreviated format for transaction signatures
Common knowledge:
- Your are hyperoptimized for base blockchain
- Your wallet address: ${account.address || ""}
- Chain currently Operating on: Base Sepolia Testnet
Realtime knowledge:
- { approximateCurrentTime: ${new Date().toISOString()}}
IMPORTANT POINTS:
- You are in your developement phase
- The development team will update you with more features
- Don't use tools when it is not necessary
- **Always try to provide short, clear and concise responses**`,
});
wss.on("connection", (ws) => {
console.log(chalk.blue("New client connected"));
// Initialize agent once per connection
const mathAgent = createAgent();
ws.on("message", async (message) => {
try {
const userMessage = message.toString();
console.log(chalk.cyan("Received message:", userMessage));
const response = mathAgent.run(userMessage);
for await (const event of response) {
if (event instanceof AgentStream) {
ws.send(JSON.stringify({ type: "stream", data: event.data.delta }));
}
if (event instanceof AgentToolCall) {
const toolCall = {
type: "tool_call",
data: {
toolName: event.data.toolName,
toolKwargs: event.data.toolKwargs,
},
};
ws.send(JSON.stringify(toolCall));
console.log(
chalk.blue(
"Tool Call: ",
event.data.toolName,
event.data.toolKwargs
)
);
}
if (event instanceof AgentToolCallResult) {
const toolResult = {
type: "tool_result",
data: event.data.toolOutput,
};
ws.send(JSON.stringify(toolResult));
console.log(
chalk.yellow(
"Tool Result: ",
JSON.stringify(event.data.toolOutput, null, 2)
)
);
}
if (event instanceof StopEvent) {
ws.send(JSON.stringify({ type: "stop", data: event.data.result }));
console.log(chalk.green("Response: ", event.data.result));
}
}
} catch (error: unknown) {
console.error(chalk.red("Error:", error));
ws.send(
JSON.stringify({
type: "error",
data: error instanceof Error ? error.message : String(error),
})
);
}
});
ws.on("close", () => {
console.log(chalk.red("Client disconnected"));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment