mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-09 12:17:24 -04:00
Compare commits
4 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 96f6b78a93 | |||
| 6c395f740d | |||
| 618943b278 | |||
| 1c19eea29a |
@@ -232,17 +232,24 @@ class BaseAgent:
|
|||||||
context_injector: Configured ContextInjector instance with
|
context_injector: Configured ContextInjector instance with
|
||||||
correct signature, today_date, job_id, session_id
|
correct signature, today_date, job_id, session_id
|
||||||
"""
|
"""
|
||||||
|
print(f"[DEBUG] set_context() ENTRY: Received context_injector with signature={context_injector.signature}, date={context_injector.today_date}, job_id={context_injector.job_id}, session_id={context_injector.session_id}")
|
||||||
|
|
||||||
self.context_injector = context_injector
|
self.context_injector = context_injector
|
||||||
|
print(f"[DEBUG] set_context(): Set self.context_injector, id={id(self.context_injector)}")
|
||||||
|
|
||||||
# Recreate MCP client with the interceptor
|
# Recreate MCP client with the interceptor
|
||||||
# Note: We need to recreate because MultiServerMCPClient doesn't have add_interceptor()
|
# Note: We need to recreate because MultiServerMCPClient doesn't have add_interceptor()
|
||||||
|
print(f"[DEBUG] set_context(): Creating new MCP client with interceptor, id={id(context_injector)}")
|
||||||
self.client = MultiServerMCPClient(
|
self.client = MultiServerMCPClient(
|
||||||
self.mcp_config,
|
self.mcp_config,
|
||||||
tool_interceptors=[context_injector]
|
tool_interceptors=[context_injector]
|
||||||
)
|
)
|
||||||
|
print(f"[DEBUG] set_context(): MCP client created")
|
||||||
|
|
||||||
# CRITICAL: Reload tools from new client so they use the interceptor
|
# CRITICAL: Reload tools from new client so they use the interceptor
|
||||||
|
print(f"[DEBUG] set_context(): Reloading tools...")
|
||||||
self.tools = await self.client.get_tools()
|
self.tools = await self.client.get_tools()
|
||||||
|
print(f"[DEBUG] set_context(): Tools reloaded, count={len(self.tools)}")
|
||||||
|
|
||||||
print(f"✅ Context injected: signature={context_injector.signature}, "
|
print(f"✅ Context injected: signature={context_injector.signature}, "
|
||||||
f"date={context_injector.today_date}, job_id={context_injector.job_id}, "
|
f"date={context_injector.today_date}, job_id={context_injector.job_id}, "
|
||||||
|
|||||||
@@ -49,14 +49,16 @@ class ContextInjector:
|
|||||||
"""
|
"""
|
||||||
# Inject context parameters for trade tools
|
# Inject context parameters for trade tools
|
||||||
if request.name in ["buy", "sell"]:
|
if request.name in ["buy", "sell"]:
|
||||||
# Add signature and today_date to args if not present
|
# Debug: Log self attributes BEFORE injection
|
||||||
if "signature" not in request.args:
|
print(f"[ContextInjector.__call__] ENTRY: id={id(self)}, self.signature={self.signature}, self.today_date={self.today_date}, self.job_id={self.job_id}, self.session_id={self.session_id}")
|
||||||
request.args["signature"] = self.signature
|
print(f"[ContextInjector.__call__] Args BEFORE injection: {request.args}")
|
||||||
if "today_date" not in request.args:
|
|
||||||
request.args["today_date"] = self.today_date
|
# ALWAYS inject/override context parameters (don't trust AI-provided values)
|
||||||
if "job_id" not in request.args and self.job_id:
|
request.args["signature"] = self.signature
|
||||||
|
request.args["today_date"] = self.today_date
|
||||||
|
if self.job_id:
|
||||||
request.args["job_id"] = self.job_id
|
request.args["job_id"] = self.job_id
|
||||||
if "session_id" not in request.args and self.session_id:
|
if self.session_id:
|
||||||
request.args["session_id"] = self.session_id
|
request.args["session_id"] = self.session_id
|
||||||
|
|
||||||
# Debug logging
|
# Debug logging
|
||||||
|
|||||||
@@ -82,24 +82,13 @@ def get_current_position_from_db(job_id: str, model: str, date: str) -> Tuple[Di
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||||
def buy(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
||||||
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
|
||||||
"""
|
"""
|
||||||
Buy stock function - writes to SQLite database.
|
Internal buy implementation - accepts injected context parameters.
|
||||||
|
|
||||||
Args:
|
This function is not exposed to the AI model. It receives runtime context
|
||||||
symbol: Stock symbol (e.g., "AAPL", "MSFT")
|
(signature, today_date, job_id, session_id) from the ContextInjector.
|
||||||
amount: Number of shares to buy (positive integer)
|
|
||||||
signature: Model signature (injected by ContextInjector)
|
|
||||||
today_date: Trading date YYYY-MM-DD (injected by ContextInjector)
|
|
||||||
job_id: Job UUID (injected by ContextInjector)
|
|
||||||
session_id: Trading session ID (injected by ContextInjector)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict[str, Any]:
|
|
||||||
- Success: {"CASH": amount, symbol: quantity, ...}
|
|
||||||
- Failure: {"error": message, ...}
|
|
||||||
"""
|
"""
|
||||||
# Validate required parameters
|
# Validate required parameters
|
||||||
if not job_id:
|
if not job_id:
|
||||||
@@ -206,8 +195,31 @@ def buy(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
|||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def sell(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
def buy(symbol: str, amount: int, **kwargs) -> Dict[str, Any]:
|
||||||
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
"""
|
||||||
|
Buy stock shares.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
symbol: Stock symbol (e.g., "AAPL", "MSFT", "GOOGL")
|
||||||
|
amount: Number of shares to buy (positive integer)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict[str, Any]:
|
||||||
|
- Success: {"CASH": remaining_cash, "SYMBOL": shares, ...}
|
||||||
|
- Failure: {"error": error_message, ...}
|
||||||
|
"""
|
||||||
|
# Extract injected parameters (added by ContextInjector, hidden from AI)
|
||||||
|
signature = kwargs.get("signature")
|
||||||
|
today_date = kwargs.get("today_date")
|
||||||
|
job_id = kwargs.get("job_id")
|
||||||
|
session_id = kwargs.get("session_id")
|
||||||
|
|
||||||
|
# Delegate to internal implementation
|
||||||
|
return _buy_impl(symbol, amount, signature, today_date, job_id, session_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _sell_impl(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||||
|
job_id: str = None, session_id: int = None) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Sell stock function - writes to SQLite database.
|
Sell stock function - writes to SQLite database.
|
||||||
|
|
||||||
@@ -327,6 +339,30 @@ def sell(symbol: str, amount: int, signature: str = None, today_date: str = None
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def sell(symbol: str, amount: int, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Sell stock shares.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
symbol: Stock symbol (e.g., "AAPL", "MSFT", "GOOGL")
|
||||||
|
amount: Number of shares to sell (positive integer)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict[str, Any]:
|
||||||
|
- Success: {"CASH": remaining_cash, "SYMBOL": shares, ...}
|
||||||
|
- Failure: {"error": error_message, ...}
|
||||||
|
"""
|
||||||
|
# Extract injected parameters (added by ContextInjector, hidden from AI)
|
||||||
|
signature = kwargs.get("signature")
|
||||||
|
today_date = kwargs.get("today_date")
|
||||||
|
job_id = kwargs.get("job_id")
|
||||||
|
session_id = kwargs.get("session_id")
|
||||||
|
|
||||||
|
# Delegate to internal implementation
|
||||||
|
return _sell_impl(symbol, amount, signature, today_date, job_id, session_id)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
port = int(os.getenv("TRADE_HTTP_PORT", "8002"))
|
port = int(os.getenv("TRADE_HTTP_PORT", "8002"))
|
||||||
mcp.run(transport="streamable-http", port=port)
|
mcp.run(transport="streamable-http", port=port)
|
||||||
|
|||||||
@@ -140,7 +140,10 @@ class ModelDayExecutor:
|
|||||||
job_id=self.job_id,
|
job_id=self.job_id,
|
||||||
session_id=session_id
|
session_id=session_id
|
||||||
)
|
)
|
||||||
|
logger.info(f"[DEBUG] ModelDayExecutor: Created ContextInjector with signature={self.model_sig}, date={self.date}, job_id={self.job_id}, session_id={session_id}")
|
||||||
|
logger.info(f"[DEBUG] ModelDayExecutor: Calling await agent.set_context()")
|
||||||
await agent.set_context(context_injector)
|
await agent.set_context(context_injector)
|
||||||
|
logger.info(f"[DEBUG] ModelDayExecutor: set_context() completed")
|
||||||
|
|
||||||
# Run trading session
|
# Run trading session
|
||||||
logger.info(f"Running trading session for {self.model_sig} on {self.date}")
|
logger.info(f"Running trading session for {self.model_sig} on {self.date}")
|
||||||
|
|||||||
Reference in New Issue
Block a user