const Orders = agentDefinition('orders')
.id({ customerId: z.string() })
.config(z.object({ systemPrompt: z.string() }))
.method('handle', m => m
.input(z.object({ request: z.string(), orderId: z.string() }))
.returns(z.object({ resolved: z.boolean() })))
export default Orders.implement({
init: () => ({ history: [] as Message[] }),
methods: {
async handle({ request, orderId }) {
// No DB writes — this push survives crashes, deploys, host migrations
this.history.push({ role: 'user', content: request })
// LLM sees full conversation; system prompt comes from typed config
const outcome = await llm.run({
prompt: this.config.systemPrompt, history: this.history,
tools: [cancelOrder, changeAddress], context: { orderId },
})
this.history.push({ role: 'assistant', content: outcome.message })
// Refunds aren't in the LLM's toolset — agent code gates them via HITL
if (outcome.needsRefund) {
// This await can sit for days at zero cost — no queue, no cron, no state table
const { approved } = await webhooks.awaitApproval(outcome)
if (approved) {
// Crash, retry, restart — still one charge. No silent double-charges.
const result = await refundOrder({ orderId, amount: outcome.refundAmount })
this.history.push({ role: 'tool', content: JSON.stringify(result) })
}
}
return { resolved: true }
},
},
})
#[derive(ConfigSchema)]
pub struct OrdersConfig { pub system_prompt: String }
#[agent_definition]
pub trait Orders {
async fn handle(&mut self, request: String, order_id: String) -> bool;
}
struct OrdersImpl { config: Config<OrdersConfig>, history: Vec<Message> }
#[agent_implementation]
impl Orders for OrdersImpl {
async fn handle(&mut self, request: String, order_id: String) -> bool {
// No DB writes — this push survives crashes, deploys, host migrations
self.history.push(Message::user(request));
// LLM sees full conversation; system prompt comes from typed config
let outcome = llm::run(
&self.config.get().system_prompt, &self.history,
vec![cancel_order(), change_address()], order_id.clone(),
).await;
self.history.push(Message::assistant(outcome.message.clone()));
// Refunds aren't in the LLM's toolset — agent code gates them via HITL
if outcome.needs_refund {
// This await can sit for days at zero cost — no queue, no cron, no state table
let approval = create_webhook().await.json::<Approval>();
if approval.approved {
// Crash, retry, restart — still one charge. No silent double-charges.
let result = refund_order(order_id, outcome.refund_amount).await;
self.history.push(Message::tool(result.to_string()));
}
}
true
}
}
final case class OrdersConfig(systemPrompt: String)
@agentDefinition()
trait Orders extends BaseAgent with AgentConfig[OrdersConfig]:
def handle(request: String, orderId: String): Future[Boolean]
@agentImplementation()
final class OrdersImpl(customerId: String, config: Config[OrdersConfig]) extends Orders:
private var history: Vector[Message] = Vector.empty
override def handle(request: String, orderId: String): Future[Boolean] = Future:
// No DB writes — this push survives crashes, deploys, host migrations
history = history :+ Message("user", request)
// LLM sees full conversation; system prompt comes from typed config
val outcome = llm.run(
prompt = config.value.systemPrompt, history = history,
tools = Seq(cancelOrder, changeAddress), context = Map("orderId" -> orderId)).await
history = history :+ Message("assistant", outcome.message)
// Refunds aren't in the LLM's toolset — agent code gates them via HITL
if outcome.needsRefund then
// This await can sit for days at zero cost — no queue, no cron, no state table
val approval = HostApi.createWebhook().await.json[Approval]()
if approval.approved then
// Crash, retry, restart — still one charge. No silent double-charges.
val result = refundOrder(orderId, outcome.refundAmount).await
history = history :+ Message("tool", result.toString)
true
#derive.config
pub(all) struct OrdersConfig { system_prompt : String }
#derive.agent
struct Orders {
config : @config.Config[OrdersConfig]
mut history : Array[Message]
}
pub fn Orders::handle(self : Self, request : String, order_id : String) -> Bool {
// No DB writes — this push survives crashes, deploys, host migrations
self.history.push({ role: "user", content: request })
// LLM sees full conversation; system prompt comes from typed config
let outcome = @llm.run(
prompt = self.config.value().system_prompt, history = self.history,
tools = [cancel_order(), change_address()], context = { "order_id": order_id },
)
self.history.push({ role: "assistant", content: outcome.message })
// Refunds aren't in the LLM's toolset — agent code gates them via HITL
if outcome.needs_refund {
// This await can sit for days at zero cost — no queue, no cron, no state table
let approval : Approval = @webhook.create().wait().json()
if approval.approved {
// Crash, retry, restart — still one charge. No silent double-charges.
let result = refund_order(order_id, outcome.refund_amount)
self.history.push({ role: "tool", content: result.to_json().stringify() })
}
}
true
}