MingaAgent.Providers.Native.ReqLLMAdapter (Minga v0.1.0)

Copy Markdown View Source

ReqLLM-specific adapter helpers for the native provider.

MingaAgent.Providers.Native owns orchestration policy: turn flow, retry, cost, compaction, approvals, tool coordination, context updates, and event normalization. This module owns the ReqLLM-shaped details needed to make one provider request and decode one provider response.

Summary

Types

Streaming LLM client compatible with ReqLLM.stream_text/3.

Raw ReqLLM usage payload before Native normalizes it into TurnUsage.

Callbacks used while streaming a provider response.

Neutralized tool-call payload emitted by ReqLLM streaming.

Decoded result from one provider response.

Functions

Returns true for Anthropic-compatible models.

Creates a ReqLLM tool-call value for assistant messages.

Runs a non-streaming text request through ReqLLM stream processing.

Returns the default ReqLLM streaming client.

Sets the provider API key env var when credentials are file-backed.

Returns true for OpenAI Codex OAuth-backed models.

Processes a ReqLLM stream response into a neutral turn result.

Runs one ReqLLM streaming request attempt. Retry ownership stays in Native.

Builds ReqLLM stream options for one native provider request.

Builds the summary callback expected by the compaction subsystem.

Validates the model string before ReqLLM sees it.

Types

llm_client()

@type llm_client() :: (String.t(), [ReqLLM.Message.t()], keyword() ->
                   {:ok, ReqLLM.StreamResponse.t()} | {:error, term()})

Streaming LLM client compatible with ReqLLM.stream_text/3.

raw_usage()

@type raw_usage() :: %{
  optional(:input_tokens) => non_neg_integer(),
  optional(:output_tokens) => non_neg_integer(),
  optional(:input) => non_neg_integer(),
  optional(:output) => non_neg_integer(),
  optional(:cache_read_input_tokens) => non_neg_integer(),
  optional(:cache_creation_input_tokens) => non_neg_integer(),
  optional(:cache_read) => non_neg_integer(),
  optional(:cache_write) => non_neg_integer(),
  optional(:total_cost) => number()
}

Raw ReqLLM usage payload before Native normalizes it into TurnUsage.

stream_callbacks()

@type stream_callbacks() :: [
  on_text: (String.t() -> term()),
  on_thinking: (String.t() -> term()),
  on_tool_call: (tool_call() -> term())
]

Callbacks used while streaming a provider response.

tool_call()

Neutralized tool-call payload emitted by ReqLLM streaming.

turn_result()

Decoded result from one provider response.

Functions

anthropic_model?(model)

@spec anthropic_model?(String.t()) :: boolean()

Returns true for Anthropic-compatible models.

assistant_tool_call(id, name, arguments)

@spec assistant_tool_call(String.t(), String.t(), map()) :: ReqLLM.ToolCall.t()

Creates a ReqLLM tool-call value for assistant messages.

call_sync(llm_client, model, messages, opts, config)

@spec call_sync(
  llm_client(),
  String.t(),
  [ReqLLM.Message.t()],
  keyword(),
  MingaAgent.Config.t()
) ::
  {:ok, String.t()} | {:error, term()}

Runs a non-streaming text request through ReqLLM stream processing.

default_client()

@spec default_client() :: llm_client()

Returns the default ReqLLM streaming client.

ensure_api_key_in_env(model)

@spec ensure_api_key_in_env(String.t()) :: :ok

Sets the provider API key env var when credentials are file-backed.

openai_codex_model?(model)

@spec openai_codex_model?(String.t()) :: boolean()

Returns true for OpenAI Codex OAuth-backed models.

process_stream(stream_response, callbacks \\ [])

@spec process_stream(ReqLLM.StreamResponse.t(), stream_callbacks()) ::
  {:ok, turn_result()} | {:error, term(), String.t()}

Processes a ReqLLM stream response into a neutral turn result.

stream(llm_client, model, messages, opts)

@spec stream(llm_client(), String.t(), [ReqLLM.Message.t()], keyword()) ::
  {:ok, ReqLLM.StreamResponse.t()} | {:error, term()}

Runs one ReqLLM streaming request attempt. Retry ownership stays in Native.

stream_opts(model, tools, thinking_level, max_tokens, config)

@spec stream_opts(
  String.t(),
  [ReqLLM.Tool.t()],
  String.t(),
  pos_integer(),
  MingaAgent.Config.t()
) ::
  keyword()

Builds ReqLLM stream options for one native provider request.

summary_client(llm_client, config)

Builds the summary callback expected by the compaction subsystem.

validate_model(model)

@spec validate_model(String.t()) :: :ok | {:error, String.t(), :invalid_format}

Validates the model string before ReqLLM sees it.