update
This commit is contained in:
@@ -8,6 +8,7 @@ from core.db import SessionLocal
|
||||
from apps.ai_core.ollama_client import get_llm
|
||||
from apps.ai_core.tools import describe_for_prompt, get_tool, validate_args
|
||||
from apps.catalog.models import Category, Product
|
||||
from apps.orders.models import Order
|
||||
|
||||
|
||||
SYSTEM_PROMPT = """You are an admin assistant for an e-commerce shop.
|
||||
@@ -44,7 +45,11 @@ def _shop_state_snapshot() -> dict:
|
||||
{"id": c.id, "slug": c.slug, "name_de": (c.name or {}).get("de", "")}
|
||||
for c in db.query(Category).order_by(Category.id).all()
|
||||
]
|
||||
return {"products": products, "categories": categories}
|
||||
orders = [
|
||||
{"id": o.id, "status": o.status, "total": float(o.total)}
|
||||
for o in db.query(Order).order_by(Order.id.desc()).limit(20).all()
|
||||
]
|
||||
return {"products": products, "categories": categories, "orders": orders}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@@ -54,7 +59,7 @@ def build_plan(user_prompt: str) -> list[dict]:
|
||||
state = _shop_state_snapshot()
|
||||
user_msg = (
|
||||
f"TOOL CATALOG (JSON):\n{json.dumps(tools, ensure_ascii=False)}\n\n"
|
||||
f"SHOP STATE (current products & categories):\n{json.dumps(state, ensure_ascii=False)}\n\n"
|
||||
f"SHOP STATE (current products, categories, recent orders):\n{json.dumps(state, ensure_ascii=False)}\n\n"
|
||||
f"USER REQUEST:\n{user_prompt}\n\n"
|
||||
"Reply with ONLY the JSON object described in the rules."
|
||||
)
|
||||
|
||||
@@ -216,10 +216,49 @@ CATEGORY_CREATE = ToolSpec(
|
||||
)
|
||||
|
||||
|
||||
# ---- orders.update_status -----------------------------------------
|
||||
|
||||
|
||||
def _handler_order_status(args: dict, db: Session) -> dict:
|
||||
from apps.orders.models import Order, OrderStatusHistory
|
||||
|
||||
order_id = int(args["id"])
|
||||
o = db.get(Order, order_id)
|
||||
if not o:
|
||||
raise ValueError(f"Order {order_id} not found")
|
||||
new_status = args["status"]
|
||||
note = _coalesce(args.get("note"), "")
|
||||
o.status = new_status
|
||||
db.add(OrderStatusHistory(order_id=order_id, status=new_status, note=note))
|
||||
db.commit()
|
||||
event_bus.publish("order.status_changed", {"id": order_id, "status": new_status}, db=db)
|
||||
return {"id": order_id, "status": new_status}
|
||||
|
||||
|
||||
ORDER_STATUS = ToolSpec(
|
||||
name="orders.update_status",
|
||||
description="Change the status of an order (e.g. paid, packed, shipped, delivered, cancelled).",
|
||||
args_schema={
|
||||
"type": "object",
|
||||
"required": ["id", "status"],
|
||||
"properties": {
|
||||
"id": {"type": "integer", "description": "Order ID"},
|
||||
"status": {
|
||||
"type": "string",
|
||||
"description": "New status: paid, packed, shipped, delivered, cancelled",
|
||||
},
|
||||
"note": {"type": "string", "description": "Optional note"},
|
||||
},
|
||||
},
|
||||
handler=_handler_order_status,
|
||||
examples=[{"id": 1, "status": "shipped", "note": "DHL versendet"}],
|
||||
)
|
||||
|
||||
|
||||
# ---- registration -------------------------------------------------
|
||||
|
||||
|
||||
ALL_TOOLS = [SETTINGS_UPDATE, PRODUCT_CREATE, PRODUCT_UPDATE, CATEGORY_CREATE]
|
||||
ALL_TOOLS = [SETTINGS_UPDATE, PRODUCT_CREATE, PRODUCT_UPDATE, CATEGORY_CREATE, ORDER_STATUS]
|
||||
|
||||
|
||||
def register_all() -> None:
|
||||
|
||||
Reference in New Issue
Block a user