Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,71 @@ def audit_logs(limit: int = Query(default=100, ge=1, le=200), object_id: str | N
query = query.where(AuditLog.request_id == request_id)
return {"items": [audit_view(row) for row in session.scalars(query).all()]}

def _proposal_payload(row, replayed: bool):
payload = recommendation_view(row, None)
payload["replayed"] = replayed
payload["margin_check"] = (row.body or {}).get("proposal", {}).get("margin_check")
return payload

@app.get("/api/workspace/overview", tags=["workspace"])
def workspace_overview(start_date: date | None = None, end_date: date | None = None, store_id: str | None = None, actor: Actor = Depends(identity)):
from .workspace import build_overview
with factory() as session:
return build_overview(session, actor.tenant_id, start_date, end_date, store_id)

@app.get("/api/workspace/campaigns", tags=["workspace"])
def workspace_campaigns(start_date: date | None = None, end_date: date | None = None, store_id: str | None = None, actor: Actor = Depends(identity)):
from .workspace import list_campaigns
with factory() as session:
return list_campaigns(session, actor.tenant_id, start_date, end_date, store_id)

@app.get("/api/workspace/access", tags=["workspace"])
def workspace_access(actor: Actor = Depends(identity)):
from .workspace import access_view
return access_view(actor, llm_configured=bool(llm), llm_provider=settings.llm_provider, llm_model=settings.llm_model)

@app.get("/api/workspace/demo-csv/{kind}", tags=["workspace"])
def workspace_demo_csv(kind: str, actor: Actor = Depends(identity)):
from .workspace import demo_csv
with factory() as session:
body, filename = demo_csv(session, actor.tenant_id, kind)
return PlainTextResponse(body, media_type="text/csv; charset=utf-8", headers={"Content-Disposition": f'attachment; filename="{filename}"'})

@app.get("/api/evals", tags=["workspace"])
def evals(limit: int = Query(default=20, ge=1, le=50), actor: Actor = Depends(identity)):
from .workspace import list_evals
with factory() as session:
return list_evals(session, actor.tenant_id, limit)

@app.post("/api/evals/offline", tags=["workspace"])
def evals_offline(request: Request, actor: Actor = Depends(identity)):
from .workspace import run_offline_eval
with factory.begin() as session:
if engine.dialect.name == "sqlite":
session.execute(text("BEGIN IMMEDIATE"))
return run_offline_eval(session, actor, request.state.request_id)

def _run_proposal(builder, payload, request, actor):
with factory.begin() as session:
if engine.dialect.name == "sqlite":
session.execute(text("BEGIN IMMEDIATE"))
row, replayed = builder(session, actor, request.state.request_id, payload)
return _proposal_payload(row, replayed)

from .proposals import PriceProposalInput, ProposalScope, create_budget, create_price_review, create_replenishment

@app.post("/api/proposals/replenishment", tags=["workspace"])
def propose_replenishment(payload: ProposalScope, request: Request, actor: Actor = Depends(identity)):
return _run_proposal(create_replenishment, payload, request, actor)

@app.post("/api/proposals/budget", tags=["workspace"])
def propose_budget(payload: ProposalScope, request: Request, actor: Actor = Depends(identity)):
return _run_proposal(create_budget, payload, request, actor)

@app.post("/api/proposals/price", tags=["workspace"])
def propose_price(payload: PriceProposalInput, request: Request, actor: Actor = Depends(identity)):
return _run_proposal(create_price_review, payload, request, actor)

return app


Expand Down
364 changes: 364 additions & 0 deletions backend/app/proposals.py

Large diffs are not rendered by default.

77 changes: 74 additions & 3 deletions backend/app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@


class SimulationAction(StrictModel):
type: Literal["adjust_ad_budget", "create_review_task"]
type: Literal["adjust_ad_budget", "create_review_task", "simulate_replenishment", "simulate_price_review"]
simulation: Literal[True] = True
product_id: str
sku: str
adjustment_pct: int = Field(default=15, ge=1, le=30)
baseline_daily_budget: int = Field(ge=0)
proposed_daily_budget: int = Field(ge=0)
baseline_daily_budget: int = Field(default=0, ge=0)
proposed_daily_budget: int = Field(default=0, ge=0)
source_period: dict
replenish_quantity: int = Field(default=0, ge=0)
line_count: int = Field(default=1, ge=1, le=500)
proposed_price_cents: int = Field(default=0, ge=0)
margin_ok: bool = True
margin_pct: float | None = None
margin_floor_pct: float = Field(default=28, ge=0, le=100)


@dataclass
Expand Down Expand Up @@ -246,6 +252,39 @@ def create_recommendation(
return RecommendationOutput(recommendation_id=recommendation.id, status=recommendation.status)


def assert_margin_allows(action: dict, *, stage: str) -> None:
"""Server-side gross-margin floor. A missing flag is not a failure.

Older ad-budget actions have no margin fields. Only an explicit
``margin_ok: false`` blocks approval and simulation.
"""
if not isinstance(action, dict) or action.get("margin_ok") is not False:
return
raise DomainError(
"margin_redline",
"毛利硬校验未通过,提案不可批准或模拟执行",
409,
{
"stage": stage,
"margin_pct": action.get("margin_pct"),
"margin_floor_pct": action.get("margin_floor_pct", 28),
"sku": action.get("sku"),
"simulation": True,
},
audit_action="recommendation.margin_blocked",
)


def assert_simulation_only(action: dict) -> None:
if not isinstance(action, dict) or action.get("simulation") is False:
raise DomainError(
"simulation_required",
"只允许 simulation=true 的模拟动作,不会改价、改预算或采购",
409,
audit_action="execution.simulation_required",
)


def decide(session: Session, recommendation_id: str, decision: str, reason: str, actor: Actor, request_id: str):
require_role(actor, "approver")
status = "approved" if decision == "approve" else "rejected"
Expand All @@ -257,6 +296,9 @@ def decide(session: Session, recommendation_id: str, decision: str, reason: str,
raise DomainError("recommendation_not_found", "建议不存在", 404)
if recommendation.status != "pending_approval":
raise DomainError("invalid_transition", "只有待审批建议可以批准或拒绝", 409)
if decision == "approve":
assert_margin_allows(recommendation.action, stage="approve")
assert_simulation_only(recommendation.action)
body = recommendation.body if isinstance(recommendation.body, dict) else {}
snapshot = _approval_snapshot(session, recommendation, actor.tenant_id) if status == "approved" else None
if snapshot is not None:
Expand Down Expand Up @@ -292,6 +334,10 @@ def decide(session: Session, recommendation_id: str, decision: str, reason: str,


def simulate_action(action: SimulationAction) -> dict:
if action.simulation is not True:
raise ValueError("simulation 必须为 true")
if action.margin_ok is False:
raise ValueError("毛利红线未通过,拒绝模拟执行")
if action.type == "adjust_ad_budget":
if action.proposed_daily_budget > action.baseline_daily_budget:
raise ValueError("模拟预算不能高于参考预算")
Expand All @@ -302,6 +348,23 @@ def simulate_action(action: SimulationAction) -> dict:
"after": {"daily_budget": action.proposed_daily_budget}, "currency": "USD",
"side_effect_count": 1, "external_calls": 0,
}
if action.type == "simulate_replenishment":
return {
"simulation": True,
"message": f"模拟执行完成:已生成 {action.sku} 等 {action.line_count} 个 SKU 的补货预览,数量 {action.replenish_quantity}。未向供应商下单。",
"action_type": action.type, "sku": action.sku,
"replenish_quantity": action.replenish_quantity, "line_count": action.line_count,
"side_effect_count": 1, "external_calls": 0,
}
if action.type == "simulate_price_review":
return {
"simulation": True,
"message": "模拟执行完成:仅保存调价复核预览,售价未改变,未连接店铺。",
"action_type": action.type, "sku": action.sku,
"proposed_price_cents": action.proposed_price_cents,
"margin_pct": action.margin_pct, "currency": "USD",
"side_effect_count": 1, "external_calls": 0,
}
return {"simulation": True, "message": "模拟执行完成:已生成成本核查任务预览。", "sku": action.sku, "action_type": action.type, "side_effect_count": 1, "external_calls": 0}


Expand All @@ -316,6 +379,14 @@ def execution_output(execution: Execution, replayed=False):
def execute_once(session: Session, recommendation_id: str, key: str, actor: Actor, request_id: str) -> ExecuteOutput:
require_role(actor, "approver")
key = scoped_idempotency_key(actor.tenant_id, key)
guarded = session.scalar(select(Recommendation).where(
Recommendation.id == recommendation_id,
Recommendation.tenant_id == actor.tenant_id,
))
if guarded is None:
raise DomainError("recommendation_not_found", "建议不存在", 404)
assert_simulation_only(guarded.action)
assert_margin_allows(guarded.action, stage="execute")
# First write serializes competing transactions on SQLite and locks the row on PostgreSQL.
claimed = session.execute(update(Recommendation).where(
Recommendation.id == recommendation_id, Recommendation.status == "approved",
Expand Down
8 changes: 7 additions & 1 deletion backend/app/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ def tool_data(registry, name, arguments, context):
stale_codes = {
"approval_stale", "approval_snapshot_missing", "approval_scope_invalid",
}
status = 409 if error.code in {"not_approved", "idempotency_conflict", *stale_codes, "action_changed"} else 500
conflict_codes = {
"not_approved", "idempotency_conflict", "action_changed",
"margin_redline", "simulation_required", *stale_codes,
}
status = 409 if error.code in conflict_codes else 500
audit_action = {
"action_changed": "execution.action_changed",
"approval_stale": "execution.stale",
"approval_snapshot_missing": "execution.stale",
"approval_scope_invalid": "execution.stale",
"margin_redline": "recommendation.margin_blocked",
"simulation_required": "execution.simulation_required",
}.get(error.code)
raise DomainError(
error.code, error.message, status, error.details, audit_action=audit_action,
Expand Down
Loading
Loading