Skip to navigation
AgentsTesting

CallSimulator

View as MarkdownOpen in Claude

CallSimulator runs an agent’s tools through a simulated call in a test. It reads the agent’s rendered SWML as the platform would and plays the model’s side: your test says what the caller says and which tools the model calls. Where swaig-test runs one tool call, the simulator runs a whole flow, such as a confirmation that spans several steps.

Each call is checked the way the platform checks it:

  • A tool the current step’s function list doesn’t offer doesn’t run, and the model gets the platform’s reply naming the tools it has.
  • A tool with semantic gates runs only when your gate callback passes every gate.
  • A result’s actions change the call: global data and semantic_state, the step and context, which functions are on, say, wait_for_user, back_to_back_functions, gate marks, and the end of the call. A post_process result’s actions run after the model’s next reply, given with agent_says().

The simulator is an approximation of the platform, for testing your code’s decisions. It doesn’t run a model, DataMap tools (give a stub), gather questions, consolidation, the sliding window, or the gates’ decision model (give a callback). model_view() and gate_conversation() raise NotImplementedError once the call has entered a step with a reset.

from signalwire.testing import CallSimulator

Parameters

agent
AgentBaseRequired

The agent to simulate a call to.

call_id
strDefaults to simulated-call

Keyword-only. The call’s id, as the agent’s tools see it in raw_data["call_id"].

gates
Callable[[dict, CallSimulator, dict], Any] | NoneDefaults to None

Keyword-only. Answers each semantic gate in place of the decision model, called as gates(gate, sim, args) with the gate’s dictionary, the simulator, and the call’s arguments. Return True or a probability at or above the gate’s threshold to pass, False or a probability below it to fail, or "unclear". A failed gate gives the model its on_fail; an unclear one gives its on_unclear. Without a callback, every gate passes.

stubs
Mapping[str, Callable[[dict], Any]] | NoneDefaults to None

Keyword-only. Results for tools that don’t run locally, such as DataMap tools, by name: fn(args) returns a FunctionResult, a dict, or text. Calling a DataMap tool without a stub raises ValueError.

global_data
Mapping[str, Any] | NoneDefaults to None

Keyword-only. Global data to start the call with, applied over the agent’s own.

check_tool_mentions
boolDefaults to False

Keyword-only. True makes call() raise ToolMentionError when a result, or the gate output that blocked the call, names a tool that the step the call ends up in doesn’t offer.

request_fields
Mapping[str, Any] | NoneDefaults to None

Keyword-only. More fields for every simulated tool request, as the platform sends them for a call, such as {"caller_id_num": "+15551234567"}.

Properties

step
str | None

The current step, or None when the agent has no contexts.

context
str | None

The current context, or None when the agent has no contexts.

global_data
dict[str, Any]

The call’s global data, as the results so far have left it.

semantic_state
Any

global_data["semantic_state"], or None.

params
dict[str, Any]

The ai verb’s params, as the agent rendered them.

functions
dict[str, dict[str, Any]]

The rendered function definitions, by name.

call_log
list[dict[str, Any]]

The conversation so far, as call log entries, each with a timestamp in microseconds. Step text is kept as it went in; model_view() shows it expanded.

calls
list[SimulatedCall]

Every tool call so far, as SimulatedCall objects.

marks
dict[str, int]

The semantic gate marks set so far, by name: the length of call_log when each was set, so call_log[marks[name]:] is everything since it. The call keeps its 16 newest marks.

chaining
bool | str

The back_to_back_functions setting, as the last result left it. Starts at "forever".

waiting_for_user
bool

Whether a result asked the model to wait for the caller. The caller’s next words clear it.

ended
bool

Whether a result ended the call with hangup or stop.

Methods

Example

A test of a confirm flow built with Proposal: the order tool runs only after a read-back, and a yes moves the call to the last step. To check changed code against real calls, record tool requests with RequestRecorder.

from signalwire import AgentBase, FunctionResult
from signalwire.patterns import Proposal
from signalwire.testing import CallSimulator
agent = AgentBase(name="tea", route="/tea")
agent.prompt_add_section("Role", "You take tea orders.")
order = Proposal("place_order", decline_tool="review_order")
def review_order(args, raw_data):
return order.propose(f"That's a {args['item']}. Is that right?",
raw_data=raw_data, details={"item": args["item"]})
def place_order(args, raw_data):
return order.complete("Order 4471 is placed",
FunctionResult("Order 4471 is placed.").swml_change_step("done"))
agent.define_tool("review_order", "Read the order back",
{"type": "object", "properties": {"item": {"type": "string"}}, "required": ["item"]},
review_order)
agent.define_tool("place_order", "Place the order the caller said yes to",
{"type": "object", "properties": {}}, place_order, gates=order.confirm_gates())
ctx = agent.define_contexts().add_context("default")
ctx.add_step("take").set_text("Take the order, then read it back.").set_functions(["review_order", "place_order"])
ctx.add_step("done").set_text("Thank the caller.").set_functions("none").set_history("spoken")
sim = CallSimulator(agent, gates=lambda gate, sim, args: True)
sim.caller("A large tea, please.")
sim.call("review_order", {"item": "large tea"})
sim.caller("Yes.")
assert sim.call("place_order").ran
assert sim.step == "done"