← all modules

Std.Ai.Provider

Std.Ai.Provider

Types
  type alias CustomImpl
  type alias WireTransport
  type alias WireImpl = { post : String -> String -> String -> Task Error String , model : String , stream : Maybe StreamTransport , measured : Maybe MeasuredTransport }
  type alias MeasuredTransport
  type alias StreamTransport
  type Provider
  type alias Message = { role : String , content : String }
  type alias Usage = { promptTokens : Int , completionTokens : Int , totalTokens : Int }
  type alias ChatResponse = { content : String , usage : Usage }
  type alias Options
  type alias RawMessage = { content : String }
  type alias RawChoice = { message : RawMessage }
  type alias RawUsage = { prompt_tokens : Int , completion_tokens : Int , total_tokens : Int }
  type alias RawResponse = { choices : List RawChoice , usage : RawUsage }
  type AnswerStream
  type StreamStep = Piece String AnswerStream | Finished ChatResponse
  type StreamEvent
  type alias CallFacts
  type alias Measured
  type alias RuntimeFacts = { slot : Maybe String , tier : Maybe String , adapter : Maybe String , queueWaitMs : Maybe Int , startMs : Maybe Int , latencyMs : Maybe Int , firstTokenMs : Maybe Int , tokensPerSecond : Maybe Float , draftAcceptance : Maybe Float }
  type alias Clock = { wallMs : Int , firstMs : Maybe Int }
  type alias Timing a = { byRuntime : Bool , adapter : String , call : String -> Task Error ( a, Maybe Int ) , tokens : a -> Int }
  type alias StreamClock = { stream : AnswerStream , first : Maybe Int }
  type alias ToolSpec
  type alias ToolSchema
  type alias ToolRequest
  type alias ResponseFormat
  type ResponseShape a
  type alias Structured
  type alias ToolCall
  type alias ToolResponse
  type Turn = Say Message | Called (List ToolCall) | Returned ToolCall String
  type alias PriceBook

Values
  assistant : String -> Message
  chat : Provider -> List Message -> Task Error ChatResponse
      Send a chat and get the assistant text plus token usage. Fails with a
  chatStream : Options -> Provider -> List Message -> (String -> Task Error ()) -> Task Error ChatResponse
      `chatWith`, with each piece of the answer handed to `onPiece` as it is
  chatStructured : ResponseShape a -> Options -> Provider -> List Message -> Task Error (Structured a)
      `chatWith`, asking for an answer of a shape: the request carries the
  chatToolRequest : Provider -> List Turn -> ToolRequest -> Task Error ToolResponse
      `chatTools` with each tool's parameter schema and, optionally, the shape of
  chatTools : Provider -> List Turn -> List ToolSpec -> Task Error ToolResponse
      Like `chat`, but advertises `tools` and returns any `tool_calls` the model
  chatWith : Options -> Provider -> List Message -> Task Error ChatResponse
      `chat` with per-call `Options` (temperature, seed, max tokens, adapter).
  closeStream : AnswerStream -> Task Error ()
      Stop reading a stream before its end (the user left the page, the
  compatible : String -> Secret -> String -> Provider
      Any OpenAI-compatible endpoint — a gateway, vLLM, or a local llama.cpp server
  cost : PriceBook -> Usage -> Result Error Money
      The money cost of a usage under a price book: tokens/1000 × the per-1k rate,
  custom : (List Message -> Task Error ChatResponse) -> Provider
      Bring your own backend: wrap any `List Message -> Task Error ChatResponse`
  customToolRequest : (List Message -> Task Error ChatResponse) -> (List Turn -> ToolRequest -> Task Error ToolResponse) -> Provider
      Like `customTools`, but the provider receives the whole `ToolRequest`:
  customTools : (List Message -> Task Error ChatResponse) -> (List Turn -> List ToolSpec -> Task Error ToolResponse) -> Provider
      Like `custom`, but the provider ALSO implements native tool-calling: supply
  decodeStructured : ResponseShape a -> String -> Result Error a
      Decode a model's answer with the shape's codec. An answer that is not JSON
  defaultOptions : Options
      Every option unset: the server's defaults.
  embed : Provider -> List String -> Task Error (List (List Float))
      Embed each input text as a vector, in input order, through the server's
  freePriceBook : Money.Currency -> PriceBook
      A zero-cost book, for a local model or a test.
  measure : Options -> Provider -> List Message -> Task Error (Measured ChatResponse)
      `chatWith`, measured: the result (a failure is a value here, not a failed
  measureStream : Options -> Provider -> List Message -> (String -> Task Error ()) -> Task Error (Measured ChatResponse)
      `chatStream`, measured: each piece goes to `onPiece` as it is written,
  measureToolRequest : Provider -> List Turn -> ToolRequest -> Task Error (Measured ToolResponse)
      `chatToolRequest`, measured, for a native tool loop.
  measureTools : Provider -> List Turn -> List ToolSpec -> Task Error (Measured ToolResponse)
      `chatTools`, measured, for a native tool loop.
  openStream : Options -> Provider -> List Message -> Task Error AnswerStream
      Send a chat and start reading its answer as it is written.
  openai : Secret -> String -> Provider
      The hosted OpenAI chat-completions endpoint.
  priceBook : Money.Currency -> String -> String -> Result Error PriceBook
      Build a price book from decimal-string rates (e.g. "0.00015" per 1k input
  readStream : AnswerStream -> Task Error StreamStep
      Read the next piece of a stream.
  responseFormat : ResponseShape a -> ResponseFormat
      The `response_format` a shape sends.
  responseShape : String -> Codec a -> ResponseShape a
      The shape of an answer of type `a`, named `name` (letters, digits, `_`
  router : (List Message -> Provider) -> Provider
      A multi-provider ROUTER: a `Provider` that, per call, picks a concrete backend
  shapeCodec : ResponseShape a -> Codec a
      The codec that decodes a shape's answer.
  system : String -> Message
  user : String -> Message
  wire : WireTransport -> String -> Provider
      An OpenAI-compatible provider over your own transport. The provider builds
  wireMeasured : WireTransport -> StreamTransport -> MeasuredTransport -> String -> Provider
      `wireStreaming`, with a transport that also reports what each call did
  wireStreaming : WireTransport -> StreamTransport -> String -> Provider
      `wire`, with a transport that streams an answer as it is written