SystemOneClient.Provider (SystemOneClient v0.3.0)

Copy Markdown View Source

Resolves where a request goes and how its response is shaped.

providerendpointauthdefault modelenvelope
:typesafe (default)https://api.typesafe.ai/v1/systemoneTYPESAFE_API_KEYjev-latesttop level
:cloudflare.../accounts/{account_id}/ai/run/@cf/cloudflare/clefCLOUDFLARE_API_TOKEN@cf/cloudflare/clefresult
:openrouterhttps://openrouter.ai/api/alpha/decisionsOPENROUTER_API_KEYtypesafe/jev-1.13top level
:openaihttps://api.openai.com/v1/decisionsOPENAI_API_KEYgpt-6-lunatranslated (see SystemOneClient.Wire.OpenAIDecisions)
:compatibleurl: (required)optionalnonetop level

url: and model: options always win over provider defaults. Keys come from api_key: or the provider's environment variable; env: can replace the environment in tests.

Summary

Functions

Resolves the endpoint, auth, default model, envelope and wire format for a request.

Extracts the System One body from a provider's response envelope.

Types

envelope()

@type envelope() :: :top_level | :cloudflare_result

name()

@type name() :: :typesafe | :cloudflare | :openai | :openrouter | :compatible

t()

@type t() :: %{
  provider: name(),
  url: String.t(),
  auth: {:bearer, String.t()} | nil,
  model: String.t() | nil,
  envelope: envelope(),
  wire: wire()
}

wire()

@type wire() :: :system_one | :openai_decisions

Functions

resolve(opts)

@spec resolve(SystemOneClient.options()) ::
  {:ok, t()}
  | {:error,
     :missing_api_key
     | :missing_account_id
     | :missing_url
     | {:unknown_provider, term()}}

Resolves the endpoint, auth, default model, envelope and wire format for a request.

iex> {:ok, p} = SystemOneClient.Provider.resolve(provider: :openai, api_key: "sk-test")
iex> {p.url, p.model, p.wire}
{"https://api.openai.com/v1/decisions", "gpt-6-luna", :openai_decisions}

iex> SystemOneClient.Provider.resolve(provider: :typesafe, env: %{})
{:error, :missing_api_key}

unwrap(atom, body)

@spec unwrap(envelope(), map()) :: {:ok, map()} | {:error, term()}

Extracts the System One body from a provider's response envelope.