diff --git a/agent/agent_executors.py b/agent/agent_executors.py index 1ed61c6..a4fd98f 100644 --- a/agent/agent_executors.py +++ b/agent/agent_executors.py @@ -1,7 +1,8 @@ import os import httpx +import logging -from openai import OpenAI +from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.language_models.chat_models import BaseChatModel @@ -10,13 +11,35 @@ from onchain.tokens.metadata import TokenMetadataRepo from server import config from web3 import Web3 -from x402.clients.base import x402Client -from x402.types import x402PaymentRequiredResponse -from langchain_openai import ChatOpenAI -from .x402 import X402Auth + +from x402v2 import x402Client as x402Clientv2 +from x402v2.http.clients import x402HttpxClient as x402HttpxClientv2 +from x402v2.mechanisms.evm import EthAccountSigner as EthAccountSignerv2 +from x402v2.mechanisms.evm.exact.register import ( + register_exact_evm_client as register_exact_evm_clientv2, +) +from x402v2.mechanisms.evm.upto.register import ( + register_upto_evm_client as register_upto_evm_clientv2, +) + +logging.getLogger("x402.httpx").setLevel(logging.DEBUG) WEB3_CONFIG = Web3(Web3.HTTPProvider(config.OG_RPC_URL)) WALLET_ACCOUNT = WEB3_CONFIG.eth.account.from_key(config.WALLET_PRIV_KEY) +BASE_TESTNET_NETWORK = "eip155:84532" + +x402_client = x402Clientv2() +register_exact_evm_clientv2( + x402_client, + EthAccountSignerv2(WALLET_ACCOUNT), + networks=[BASE_TESTNET_NETWORK], +) +register_upto_evm_clientv2( + x402_client, + EthAccountSignerv2(WALLET_ACCOUNT), + networks=[BASE_TESTNET_NETWORK], +) + TIMEOUT = httpx.Timeout( timeout=90.0, @@ -48,14 +71,12 @@ ) GROK_MODEL = "x-ai/grok-2-1212" # $2/M input tokens; $10/M output tokens -x402_http_client = httpx.AsyncClient( - base_url=config.LLM_SERVER_URL, - headers={"Authorization": f"Bearer {config.DUMMY_X402_API_KEY}"}, +x402_http_client = x402HttpxClientv2( + x402_client, timeout=TIMEOUT, limits=LIMITS, http2=False, follow_redirects=False, - auth=X402Auth(account=WALLET_ACCOUNT), # type: ignore ) # Select model based on configuration diff --git a/agent/x402.py b/agent/x402.py deleted file mode 100644 index 4fc69c2..0000000 --- a/agent/x402.py +++ /dev/null @@ -1,60 +0,0 @@ -import httpx -import typing -import logging - -from x402.clients.base import x402Client -from x402.types import x402PaymentRequiredResponse, PaymentRequirements - - -class X402Auth(httpx.Auth): - """Auth class for handling x402 payment requirements.""" - - def __init__( - self, - account: typing.Any, - max_value: typing.Optional[int] = None, - payment_requirements_selector: typing.Optional[ - typing.Callable[ - [ - list[PaymentRequirements], - typing.Optional[str], - typing.Optional[str], - typing.Optional[int], - ], - PaymentRequirements, - ] - ] = None, - ): - self.x402_client = x402Client( - account, - max_value=max_value, - payment_requirements_selector=payment_requirements_selector, # type: ignore - ) - - async def async_auth_flow( - self, request: httpx.Request - ) -> typing.AsyncGenerator[httpx.Request, httpx.Response]: - response = yield request - - if response.status_code == 402: - try: - await response.aread() - data = response.json() - - payment_response = x402PaymentRequiredResponse(**data) - - selected_requirements = self.x402_client.select_payment_requirements( - payment_response.accepts - ) - - payment_header = self.x402_client.create_payment_header( - selected_requirements, payment_response.x402_version - ) - - request.headers["X-Payment"] = payment_header - request.headers["Access-Control-Expose-Headers"] = "X-Payment-Response" - yield request - - except Exception as e: - logging.error(f"X402Auth: Error handling payment: {e}") - return diff --git a/requirements.txt b/requirements.txt index 0cd445f..af9e6a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,7 +27,7 @@ aioboto3>=1.38.0 async_lru>=2.0.0 aiolimiter>=1.2.0 pytest-asyncio>=0.21.0 -og-test-x402==0.0.9 +og-test-v2-x402==0.0.9 eth-account>=0.13.4 web3>=7.3.0 cachetools>=6.2.4 \ No newline at end of file diff --git a/server/config.py b/server/config.py index c188b7b..14bffb9 100644 --- a/server/config.py +++ b/server/config.py @@ -4,7 +4,7 @@ SKIP_TOKEN_AUTH_KEY = os.getenv("SKIP_TOKEN_AUTH_KEY") DUMMY_X402_API_KEY = os.getenv("DUMMY_X402_API_KEY", "dummy") -LLM_SERVER_URL: str = os.getenv("LLM_SERVER_URL", "https://llmogevm.opengradient.ai/v1") +LLM_SERVER_URL: str = os.getenv("LLM_SERVER_URL", "https://llm.opengradient.ai/v1") OG_RPC_URL: str = os.getenv("OG_RPC_URL", "https://ogevmdevnet.opengradient.ai") WALLET_PRIV_KEY: str = os.getenv("WALLET_PRIV_KEY")