OpenrouterSdk.Middleware behaviour (OpenRouter SDK v0.1.0)

Copy Markdown View Source

extension hook for upstream consumers.

this package intentionally ships zero retry / backoff / rotation policy. instead, consumers compose their own by implementing this behaviour and registering it in :openrouter_sdk, :middleware:

config :openrouter_sdk,
  middleware: [
    {MyApp.Retry, max: 3, base: 200},
    {MyApp.Rotate, models: ["openai/gpt-4o", "anthropic/claude-sonnet-4-6"]}
  ]

the next callback runs the rest of the pipeline (eventually reaching the finch call). a middleware that wants to retry simply re-invokes next.(request); a rotator can swap fields on the request before passing it on.

Summary

Functions

fold a list of middleware over the terminal runner function, producing a single function that runs the whole chain.

Types

next()

@type next() :: (request() -> response())

request()

@type request() :: OpenrouterSdk.Client.Request.t()

response()

@type response() :: {:ok, term()} | {:error, OpenrouterSdk.Error.t()}

Callbacks

call(request, next, opts)

@callback call(request(), next(), opts :: keyword()) :: response()

Functions

build(middleware, runner)

@spec build([module() | {module(), keyword()}], next()) :: next()

fold a list of middleware over the terminal runner function, producing a single function that runs the whole chain.