A2aEngine.Codec.JsonRpc (a2a_engine v0.1.0)

Copy Markdown View Source

JSON-RPC 2.0 envelope codec with A2A method registry.

Keeps the envelope layer thin: params and result are passed through as plain maps. Callers handle A2A type encoding/decoding (via the A2aEngine.Types.* modules) before/after.

A2A methods

The known method strings are registered in @methods. Use method/1 to resolve an atom key to a string, and atom_for_method/1 to parse a string back to an atom.

Summary

Functions

Look up the atom for a wire method string. Returns :unknown if the method isn't in the A2A registry (still valid JSON-RPC, just not A2A).

Decode a JSON-RPC 2.0 request envelope.

Decode a JSON-RPC 2.0 response envelope.

Build a JSON-RPC 2.0 error response envelope.

Build a JSON-RPC 2.0 request envelope.

Build a JSON-RPC 2.0 success response envelope.

Whether a wire method string is one of the known A2A methods.

Look up the wire string for a known A2A method atom.

All known A2A method atoms → wire strings.

Types

decoded_request()

@type decoded_request() :: %{id: id(), method: String.t(), params: map() | nil}

decoded_response()

@type decoded_response() ::
  {:success, id(), term()}
  | {:error, id(), %{code: integer(), message: String.t(), data: map() | nil}}

id()

@type id() :: String.t() | integer() | nil

request()

@type request() :: %{required(String.t()) => term()}

response()

@type response() :: %{required(String.t()) => term()}

Functions

atom_for_method(str)

@spec atom_for_method(String.t()) :: atom() | :unknown

Look up the atom for a wire method string. Returns :unknown if the method isn't in the A2A registry (still valid JSON-RPC, just not A2A).

decode_request(m)

@spec decode_request(map()) :: {:ok, decoded_request()} | {:error, term()}

Decode a JSON-RPC 2.0 request envelope.

Returns {:ok, %{id, method, params}} or {:error, reason}.

decode_response(arg1)

@spec decode_response(map()) :: {:ok, decoded_response()} | {:error, term()}

Decode a JSON-RPC 2.0 response envelope.

Returns {:ok, {:success, id, result}}, {:ok, {:error, id, error_map}}, or {:error, reason}.

encode_error(id, code_or_error, message \\ nil, data \\ nil)

@spec encode_error(id(), integer() | map(), String.t() | nil, map() | nil) ::
  response()

Build a JSON-RPC 2.0 error response envelope.

Accepts either an %{code, message, data} map or a code integer (in which case the default message for that code is used).

encode_request(method, params, id)

@spec encode_request(String.t(), map() | nil, id()) :: request()

Build a JSON-RPC 2.0 request envelope.

encode_success(id, result)

@spec encode_success(id(), term()) :: response()

Build a JSON-RPC 2.0 success response envelope.

known_method?(str)

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

Whether a wire method string is one of the known A2A methods.

method(atom)

@spec method(atom()) :: String.t()

Look up the wire string for a known A2A method atom.

methods()

@spec methods() :: %{required(atom()) => String.t()}

All known A2A method atoms → wire strings.