from polos import Agent, workflow, WorkflowContext
order_validation_agent = Agent(
provider="openai",
model="gpt-4o",
tools=[check_inventory, calculate_shipping]
)
@workflow
async def process_order(ctx: WorkflowContext, order: ProcessOrderInput):
# Agent validates order and checks inventory
validation = await ctx.step.agent_invoke_and_wait(
"validate_order",
order_validation_agent.with_input(f"Validate this order: {order}")
)
if not validation.result.valid:
return ProcessOrderOutput(
status="invalid",
reason=validation.result.reason
)
# High-value orders need approval
if order.amount > 1000:
# Suspend execution until the order is approved or rejected
decision = await ctx.step.suspend(
"approval",
data={
"id": order.id,
"amount": order.amount,
"items": order.items,
"user", order.user
}
)
if not decision.data["approved"]:
return ProcessOrderOutput(
status="rejected",
reason=decision.data.get("reason")
)
# Charge customer (exactly-once guarantee)
payment = await ctx.step.run("charge", charge_stripe, order)
# Wait for warehouse pickup (could be hours or days)
await ctx.step.wait_for_event(
"wait_pickup",
topic=f"warehouse.pickup/{order.id}"
)
# Send shipping notification
await ctx.step.run("notify", send_shipping_email, order)
return ProcessOrderOutput(status="completed", payment_id=payment.id)