ExTypesafe.Client (ExTypesafe v0.1.0)

Copy Markdown View Source

A configured TypeSafe API client.

Wraps Req to handle authentication, retries, and JSON encoding/decoding. Create a client with new/1 and pass it to ExTypesafe.system_one/3.

Example

client = ExTypesafe.Client.new(api_key: "ts-...")
# or read from env:
client = ExTypesafe.Client.new()

The client is a plain struct — it's safe to create once and reuse across requests.

Summary

Types

t()

An opaque client struct. Treat as read-only.

Functions

Evaluates typed questions against a state.

Creates a new client.

Types

t()

@type t() :: %ExTypesafe.Client{config: ExTypesafe.Config.t(), req: Req.Request.t()}

An opaque client struct. Treat as read-only.

Functions

evaluate(client, state, questions, opts \\ [])

@spec evaluate(t(), ExTypesafe.state(), ExTypesafe.questions(), keyword()) ::
  {:ok, ExTypesafe.Response.t()} | {:error, ExTypesafe.Error.t()}

Evaluates typed questions against a state.

Returns {:ok, ExTypesafe.Response.t()} on success or {:error, ExTypesafe.Error.t()} on failure (including exhausted retries).

This is the low-level function. Prefer ExTypesafe.system_one/3 for the public API.

Parameters

  • client — A client built with new/1.
  • state — The content to evaluate: a string, map, or list.
  • questions — A map of string/atom keys to ExTypesafe.Question structs.
  • opts — Optional keyword list:
    • :model — Override the client's default model.

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new client.

Accepts the same options as ExTypesafe.Config.new/1. Falls back to environment variables. Raises ArgumentError if no API key is configured.

Options

  • :api_key — TypeSafe API key. Falls back to TYPESAFE_API_KEY env var. Required.
  • :base_url — API root. Falls back to TYPESAFE_BASE_URL env var (default: https://api.typesafe.ai).
  • :model — Default model. Falls back to TYPESAFE_DEFAULT_MODEL env var (default: jev-latest).
  • :max_retries — Retry attempts on 429/529 (default: 3).
  • :retry_delay_ms — Initial delay in ms, doubles each attempt (default: 500).
  • :plug — Inject a Req plug for testing (e.g. {Req.Test, :typesafe}).