Skip to navigation
Agents

Operations

View as MarkdownOpen in Claude

The signalwire.operations package builds an agent from the things it can change for a caller, such as cancelling an order or moving a booking. You write each operation once: its fields, a docstring the model reads, and prepare(), which checks the request and returns a Change or raises Need or Refuse. The SDK generates the tools, the steps, the confirm gate, and the call’s record in global data, and runs every change the same way.

from signalwire.operations import (
Change, Field, Need, Operation, Operations, Refuse, ServiceAgent, Write, WriteFailed,
)

Change lifecycle

Every change goes through the same steps:

  1. The model calls an operation’s tool with everything the caller said. The tool of an operation that takes fields has a required data_supplied_by_user argument, and nothing runs unless the model sets it to true. An operation with no fields has no such argument, and lookups don’t take it either.
  2. A required Field with ask that the model sent empty, null, or as "not said" is asked for before prepare() runs.
  3. prepare() returns a Change, or raises Need (something wasn’t said) or Refuse (the request breaks a rule). Nothing is written yet.
  4. The change becomes the one waiting for the caller’s answer, the call moves to the confirm step, and the platform says the change’s read_back word for word.
  5. The model calls confirm_change or decline_change. confirm_change carries one semantic gate: did the caller agree to the change read back. A yes said while the read-back was still playing counts.
  6. The change’s writes run in order. A write that raises WriteFailed stops the change, and the writes before it stay made.
  7. The change is added to the call’s record, with any money it moved. A change set aside for after this one is offered next.

When the caller asks for something else while a change waits, the SDK compares the two by what both touch. It merges them, sets one aside for after the other, replaces part of the waiting change, or refuses. The hooks for this are on Operation.

What the SDK generates

  • Steps: identify (when the service identifies callers), main, confirm, a step for each operation with guidance, each declared phase, and transferred (when the service offers a transfer). Tool handlers move between them.
  • Prompt sections: Role, from role=, and Rules, from DEFAULT_RULES or rules=.
  • Tools: one per operation and lookup, confirm_change and decline_change when any operation confirms, find_account with identify(), and transfer_to_human with transfer_with().
  • The call’s record, in global_data.ops, whose now line the conscience param shows on every request.

Every generated tool’s result starts with back_to_back_functions set to "forever", so a refused gate doesn’t stop the model from chaining tools for the rest of the call.

Example

from signalwire.operations import Change, Field, Operation, Refuse, ServiceAgent, Write
ORDERS = {"#W1": {"status": "pending", "total": 54.04}}
agent = ServiceAgent("store", role="You answer the phone for an online store.", currency="USD")
agent.identify(
any_of=[["email"]],
find=lambda call, keys: "u1" if keys["email"] == "ann@example.com" else None,
)
agent.writes_with(lambda call, write: print("write", write.name, write.args))
@agent.operation(tool="cancel_order")
class Cancel(Operation):
"""Cancel a pending order."""
order_id = Field(str, "The order to cancel.")
def prepare(self, call, f):
order = ORDERS.get(f.order_id)
if order is None or order["status"] != "pending":
raise Refuse(f"{f.order_id} can't be cancelled: only pending orders can.")
return Change(
read_back=f"Cancel order {f.order_id}, refunding ${order['total']:.2f}. Shall I go ahead?",
touches=[f"order:{f.order_id}"],
writes=[Write("cancel_order", order_id=f.order_id)],
money=[-order["total"]],
done=f"Order {f.order_id} is cancelled.",
)
if __name__ == "__main__":
print(agent.preview())
agent.run()

preview() prints each generated step’s text and tools as the model gets them. The CallSimulator in signalwire.testing runs an operations agent’s tools offline, including the steps they move to and the gates.

Replacing deprecated prefabs

ConciergeAgent, ReceptionistAgent, and FAQBotAgent are deprecated:

  • For bookings, use FrontDeskAgent with the Appointments pack in place of ConciergeAgent, and InfoBotKnowledge for amenities and hours.
  • For transfers to departments, use FrontDeskAgent in place of ReceptionistAgent.
  • For answers from a knowledge base, use InfoBotKnowledge in place of FAQBotAgent.

Classes