anthropic4s
GitHub

Reference Integrations

ScriptedModel

A Messages implementation that plays back the replies you script, from the anthropic4s-testkit module. Agents run against it with no network and no API key.

Reference#

ScriptedModel.of(replies*)#

testkit · ScriptedModel.scala
def of[F[_]: Concurrent](replies: Message*): F[ScriptedModel[F]]
def scripted[F[_]: Concurrent](steps: Either[Throwable, Message]*): F[ScriptedModel[F]]

ScriptedModel[F]#

testkit · ScriptedModel.scala
final class ScriptedModel[F[_]] private (
    script: Ref[F, List[Either[Throwable, Message]]],
    seen: Ref[F, Vector[MessageRequest]]
)(implicit F: Concurrent[F])
    extends Messages[F]
def requests: F[Vector[MessageRequest]]

requests returns every request it received, to assert on what your code sent.

Caveats

Replies#

testkit · ScriptedModel.scala
def text(text: String): Message
def toolUse(name: String, input: Json, id: String = "toolu_1"): Message
def toolUses(calls: (String, String, Json)*): Message
def stopped(reason: StopReason): Message
def structured[A](value: A)(implicit schema: anthropic4s.schema.Schema[A]): Message
testkit · ScriptedModel.scala
def of(message: Message): List[StreamEvent]

Ready-made replies: text, tool calls, a given stop reason, or a structured value. StreamEvents.of turns a reply into the events of its stream.

Usage#

Testing an agent#

testkit · ScriptedModel.scala
for {
  model  <- ScriptedModel.of[IO](Replies.toolUse("get_weather", json"""{"city":"Brussels"}"""), Replies.text("Sunny."))
  result <- model.toolRunner(List(weatherTool)).run(request)
  sent   <- model.requests
} yield assertEquals(result.text, "Sunny.")
README.md
for {
  model  <- ScriptedModel.of[IO](Replies.toolUse("get_weather", Json.obj("city" -> "Ghent".asJson)), Replies.text("Rainy."))
  result <- model.toolRunner(List(weather)).run(request)
  sent   <- model.requests            // inspect what your code sent
} yield assertEquals(result.text, "Rainy.")

Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.