90 lines
3.4 KiB
Python
90 lines
3.4 KiB
Python
# database/ai_models.py
|
|
from sqlalchemy import Column, Integer, String, DateTime, Text, Float, ForeignKey, Index
|
|
from sqlalchemy.orm import relationship
|
|
from database.models import Base
|
|
from datetime import datetime, timezone
|
|
|
|
# helper for default timestamps
|
|
datetime_now = lambda: datetime.now(timezone.utc)
|
|
|
|
|
|
class AgentContext(Base):
|
|
"""
|
|
Shared context table (replaces hardcoded chain), supporting multi-agent access and TTL.
|
|
Index: (session_id, agent_name, context_key) for fast cross-agent queries.
|
|
"""
|
|
__tablename__ = 'agent_context'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
session_id = Column(String(64), nullable=False, index=True)
|
|
agent_name = Column(String(50), nullable=False, index=True)
|
|
context_key = Column(String(100), nullable=False)
|
|
context_val = Column(Text) # JSON string
|
|
created_at = Column(DateTime, default=datetime_now)
|
|
ttl_minutes = Column(Integer, default=60)
|
|
|
|
__table_args__ = (
|
|
Index('idx_agent_context_session_key', 'session_id', 'agent_name', 'context_key'),
|
|
Index('idx_agent_context_session_ttl', 'session_id', 'created_at'),
|
|
)
|
|
|
|
|
|
class ActionPlan(Base):
|
|
"""
|
|
Action plan table (NemoTron output, awaiting review/execution tracking).
|
|
"""
|
|
__tablename__ = 'action_plans'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
session_id = Column(String(64), nullable=True)
|
|
plan_type = Column(String(50), nullable=True) # price_adjust / restock / campaign
|
|
sku = Column(String(100), nullable=True, index=True)
|
|
payload = Column(Text) # JSON payload
|
|
status = Column(String(20), default='pending') # pending/approved/rejected/executed
|
|
created_by = Column(String(50)) # nemotron / openclaw
|
|
approved_by = Column(String(100), nullable=True) # Telegram user_id
|
|
created_at = Column(DateTime, default=datetime_now)
|
|
executed_at = Column(DateTime, nullable=True)
|
|
|
|
__table_args__ = (
|
|
Index('idx_action_plan_sku_status', 'sku', 'status'),
|
|
Index('idx_action_plan_created', 'created_at'),
|
|
)
|
|
|
|
|
|
class ActionOutcome(Base):
|
|
"""
|
|
Action outcome tracking (closed-loop learning core).
|
|
"""
|
|
__tablename__ = 'action_outcomes'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
plan_id = Column(Integer, ForeignKey('action_plans.id'), nullable=False)
|
|
metric_type = Column(String(50), nullable=True) # sales_7d / price_rank / conversion
|
|
before_val = Column(Float)
|
|
after_val = Column(Float)
|
|
measured_at = Column(DateTime)
|
|
verdict = Column(String(20)) # effective / neutral / backfired
|
|
created_at = Column(DateTime, default=datetime_now)
|
|
|
|
plan = relationship("ActionPlan", backref="outcomes")
|
|
|
|
|
|
class AgentStrategyWeights(Base):
|
|
"""
|
|
Agent strategy weights (OpenClaw learning accumulation).
|
|
Index: strategy_key for fast updates/query.
|
|
"""
|
|
__tablename__ = 'agent_strategy_weights'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
strategy_key = Column(String(100), unique=True, nullable=False) # e.g. price_cut_when_gap_gt_5pct
|
|
weight = Column(Float, default=1.0)
|
|
success_cnt = Column(Integer, default=0)
|
|
fail_cnt = Column(Integer, default=0)
|
|
updated_at = Column(DateTime, default=datetime_now)
|
|
|
|
__table_args__ = (
|
|
Index('idx_strategy_key', 'strategy_key'),
|
|
)
|