Define Purple Flea Bee Tools
import { BeeAgent } from 'bee-agent-framework/agents/bee/agent';
import { DynamicTool, StringToolOutput } from 'bee-agent-framework/tools/base';
import { z } from 'zod';
const PURPLEFLEA_KEY = process.env.PURPLEFLEA_API_KEY!;
const BASE = 'https://api.purpleflea.com/v1';
async function pfRequest(path: string, body?: object) {
const res = await fetch(`${BASE}${path}`, {
method: body ? 'POST' : 'GET',
headers: {
'X-API-Key': PURPLEFLEA_KEY,
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : undefined
});
return res.json();
}
const getPriceTool = new DynamicTool({
name: "get_crypto_price",
description: "Get current price for any cryptocurrency. Pass 'BTC', 'ETH', etc.",
inputSchema: z.object({
symbol: z.string().describe("Cryptocurrency symbol e.g. BTC")
}),
async handler({ symbol }) {
const data = await pfRequest(`/trading/price/${symbol}`);
return new StringToolOutput(
`${symbol}: $${data.price.toLocaleString()} (${data.change_24h > 0 ? '+' : ''}${data.change_24h.toFixed(2)}% 24h)`
);
}
});
const openTradeTool = new DynamicTool({
name: "open_trade",
description: "Open a perpetual futures position on Purple Flea",
inputSchema: z.object({
symbol: z.string(),
side: z.enum(["long", "short"]),
size: z.number().positive().max(100),
leverage: z.number().int().min(1).max(10).default(1)
}),
async handler(params) {
const trade = await pfRequest('/trading/open', params);
return new StringToolOutput(
`Trade opened: ${trade.position_id} | ${params.side} ${params.symbol} ${params.size}x${params.leverage}`
);
}
});
Create Trading Bee Agent
import { BeeAgent } from 'bee-agent-framework/agents/bee/agent';
import { OpenAIChatLLM } from 'bee-agent-framework/adapters/openai/chat';
import { UnconstrainedMemory } from 'bee-agent-framework/memory/unconstrainedMemory';
const llm = new OpenAIChatLLM({ modelId: 'gpt-4o' });
const agent = new BeeAgent({
llm,
memory: new UnconstrainedMemory(),
tools: [getPriceTool, openTradeTool, getBalanceTool]
});
const response = await agent
.run({
prompt: `Check BTC and ETH prices. If BTC is up more than ETH
in the last 24h, open a small long on BTC (size 10, leverage 2).
If ETH is stronger, go long ETH instead.`
})
.observe((emitter) => {
emitter.on('update', ({ data }) => {
console.log(`[${data.type}] ${JSON.stringify(data)}`);
});
});
console.log(`Agent result: ${response.result.text}`);