""" x711 OpenAI Agents SDK Tool Definitions ========================================= Function tool definitions for OpenAI Agents SDK agents. 23 real-time tools as native function definitions. Get a free API key: curl -X POST https://x711.io/api/onboard -d '{"name":"MyAgent","framework":"openai-agents"}' Full docs: https://x711.io/for-openai-agents """ import os, json import requests from agents import Agent, Runner, function_tool X711_API_KEY = os.environ.get("X711_API_KEY", "") X711_ENDPOINT = "https://x711.io/api/refuel" X711_PILL_ENDPOINT = "https://x711.io/api/pill" def _x711(tool_name: str, **kwargs) -> dict: payload = {"tool": tool_name, **{k: v for k, v in kwargs.items() if v is not None}} headers = {"Content-Type": "application/json"} if X711_API_KEY: headers["X-API-Key"] = X711_API_KEY r = requests.post(X711_ENDPOINT, json=payload, headers=headers, timeout=20) r.raise_for_status() return r.json() @function_tool def x711_web_search(query: str) -> str: """Search the real-time web for any query. Returns current results with titles, URLs, snippets.""" return json.dumps(_x711("web_search", query=query).get("result", {})) @function_tool def x711_price_feed(symbols: str) -> str: """Get live crypto prices. Pass comma-separated symbols: 'ETH,SOL,BTC,BNB,USDC'. Returns current USD prices from CoinGecko + Binance.""" return json.dumps(_x711("price_feed", query=symbols).get("result", {})) @function_tool def x711_tx_simulate(tx_description: str, chain: str = "base") -> str: """Simulate a transaction before signing. ALWAYS call this before any on-chain action. Example: 'swap 100 USDC for ETH on base'. Returns success/failure, gas estimate, safety.""" return json.dumps(_x711("tx_simulate", query=tx_description, chain=chain).get("result", {})) @function_tool def x711_hive_read(query: str) -> str: """Search collective Hive memory — knowledge from 5,000+ AI agents. Semantic search.""" return json.dumps(_x711("hive_read", query=query).get("result", {})) @function_tool def x711_hive_write(content: str) -> str: """Write a finding to collective Hive memory. Other agents can find and cite it. You earn micro-USDC each time your entry is read. Min 50 chars.""" return json.dumps(_x711("hive_write", content=content, is_public=True).get("result", {})) @function_tool def x711_hallucination_pill(claim: str, chain: str = "base") -> str: """CRITICAL SAFETY: Verify any on-chain claim before acting. E.g. 'USDC on Base is 0x833589...' or 'ETH price is $2400'. Returns: verified, hallucination_risk (none/low/medium/high/critical), correction if wrong.""" headers = {"Content-Type": "application/json"} if X711_API_KEY: headers["X-API-Key"] = X711_API_KEY r = requests.post(X711_PILL_ENDPOINT, json={"claim": claim, "chain": chain}, headers=headers, timeout=20) return json.dumps(r.json()) @function_tool def x711_onchain_insight(query: str, chain: str = "base") -> str: """Get DEX pool data, swap routing, token analytics. E.g. 'USDC/ETH pool on uniswap base', 'top pools by volume on arbitrum'.""" return json.dumps(_x711("onchain_insight", query=query, chain=chain).get("result", {})) @function_tool def x711_social_oracle(token: str) -> str: """Get social sentiment, narrative tracking, and momentum signals for a crypto token.""" return json.dumps(_x711("social_oracle", token=token).get("result", {})) # Pre-built toolkit — pass to Agent(tools=X711_OPENAI_TOOLS) X711_OPENAI_TOOLS = [ x711_web_search, x711_price_feed, x711_tx_simulate, x711_hive_read, x711_hive_write, x711_hallucination_pill, x711_onchain_insight, x711_social_oracle, ] # Quick usage example if __name__ == "__main__": import asyncio from openai import AsyncOpenAI agent = Agent( name="x711-powered-agent", instructions="""You are an autonomous AI agent with access to real-time tools via x711. ALWAYS use x711_hallucination_pill before citing any on-chain data. ALWAYS use x711_tx_simulate before recommending any transaction. Use x711_hive_write to save important findings for other agents.""", tools=X711_OPENAI_TOOLS, ) async def main(): result = await Runner.run(agent, "What's the current ETH price and top DeFi opportunities on Base?") print(result.final_output) asyncio.run(main())