MingaClaudeBridge.Status (minga_claude_bridge v0.1.0)

Copy Markdown View Source

Holds the route's last known readiness and plan limits, and reports changes to Minga.

  • Availability results are cached per credential for a short time, so opening /model repeatedly does not start Claude Code every time.
  • Sessions send their rate_limit_events here. A rejected status reports the route as {:unavailable, :rate_limited, remedy} with the reset time; the next allowed status reports it ready again with the last plan label.
  • Plan windows (MingaAgent.InferenceRoute.PlanWindow) come from get_usage answers (put_usage/2) and rate-limit events, merged by MingaClaudeBridge.Route.PlanUsage. Every change is reported with Minga.Extension.ModelRoutes.report_usage/2.
  • The models of the last probe and the measured context windows feed MingaClaudeBridge.Route.models/1. Windows are cached per credential and model with their measurement time.
  • Background measurements run under a Task.Supervisor, at most one per credential at a time (start_measurement/3).

Reports go through the reporter MingaClaudeBridge received when it registered the route. The reporter and both report functions are options so tests can observe reports.

Summary

Functions

Returns a cached availability for key when it is younger than ttl milliseconds.

Returns a specification to start this module under a supervisor.

Returns the most recently measured context window in tokens per model id, across credentials. Empty before the first measurement.

Returns the latest rate-limit event, or nil.

Returns the remedy text for a reached plan limit.

Returns the model entries of Claude Code's last initialize answer, or nil before the first probe or when the server is not running.

Returns the current plan windows in display order.

Caches an availability result for key.

Records the context windows measured for credential key. Models it could not measure keep their earlier value.

Records the model entries of a probe.

Stores the reporter from route registration for every later status server.

Records a get_usage answer read with credential key: refreshes every plan window and reports them. An answer without windows reports an empty list, so Minga clears stale windows.

Records a rate-limit event, as sessions do with their :on_rate_limit pid.

Returns the ids among model_ids whose window for credential key is missing or older than ttl milliseconds.

Starts the status server.

Starts fun as the background measurement for credential key under the task supervisor. Returns :busy while a measurement for key runs and :unavailable without a task supervisor.

Returns the plan label of the last get_usage answer for credential key, for example "Claude Max", or nil.

Types

availability()

@type availability() :: {:ready, String.t()} | {:unavailable, atom(), String.t()}

report_fun()

@type report_fun() :: (term(), availability() -> term())

Functions

cached(server, key, ttl)

@spec cached(GenServer.server(), term(), non_neg_integer()) ::
  {:ok, availability()} | :miss

Returns a cached availability for key when it is younger than ttl milliseconds.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

context_windows(server)

@spec context_windows(GenServer.server()) :: %{required(String.t()) => pos_integer()}

Returns the most recently measured context window in tokens per model id, across credentials. Empty before the first measurement.

latest_rate_limit(server)

@spec latest_rate_limit(GenServer.server()) ::
  MingaClaudeBridge.Wire.RateLimit.t() | nil

Returns the latest rate-limit event, or nil.

limit_remedy(resets_at)

@spec limit_remedy(number() | nil) :: String.t()

Returns the remedy text for a reached plan limit.

models(server)

@spec models(GenServer.server()) :: [map()] | nil

Returns the model entries of Claude Code's last initialize answer, or nil before the first probe or when the server is not running.

plan_windows(server)

Returns the current plan windows in display order.

put(server, key, availability)

@spec put(GenServer.server(), term(), availability()) :: :ok

Caches an availability result for key.

put_context_windows(server, key, windows)

@spec put_context_windows(GenServer.server(), term(), %{
  required(String.t()) => pos_integer()
}) :: :ok

Records the context windows measured for credential key. Models it could not measure keep their earlier value.

put_models(server, models)

@spec put_models(GenServer.server(), [map()]) :: :ok

Records the model entries of a probe.

put_reporter(reporter)

@spec put_reporter(term()) :: :ok

Stores the reporter from route registration for every later status server.

put_usage(server, key, answer)

@spec put_usage(GenServer.server(), term(), map()) :: :ok

Records a get_usage answer read with credential key: refreshes every plan window and reports them. An answer without windows reports an empty list, so Minga clears stale windows.

When the account's own answer named no plan and this credential's last check was ready, the plan the usage names is reported as the new ready label.

rate_limit(server, rate_limit)

Records a rate-limit event, as sessions do with their :on_rate_limit pid.

stale_models(server, key, model_ids, ttl)

@spec stale_models(GenServer.server(), term(), [String.t()], non_neg_integer()) :: [
  String.t()
]

Returns the ids among model_ids whose window for credential key is missing or older than ttl milliseconds.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the status server.

Options: :name, :reporter (defaults to the stored reporter), :report (a two-argument function, defaults to Minga.Extension.ModelRoutes.report_status/2), :report_usage (a two-argument function, defaults to Minga.Extension.ModelRoutes.report_usage/2), :tasks (the Task.Supervisor for measurements; without it no measurement starts), and :notify (a pid that receives {:claude_bridge_measurement, :started | :finished, key}, for tests).

start_measurement(server, key, fun)

@spec start_measurement(GenServer.server(), term(), (-> term())) ::
  :ok | :busy | :unavailable

Starts fun as the background measurement for credential key under the task supervisor. Returns :busy while a measurement for key runs and :unavailable without a task supervisor.

usage_label(server, key)

@spec usage_label(GenServer.server(), term()) :: String.t() | nil

Returns the plan label of the last get_usage answer for credential key, for example "Claude Max", or nil.