Nous.ToolCall (nous v0.17.2)

Copy Markdown View Source

Field access for tool-call maps whose keys may be atoms or strings.

Providers return tool calls with string keys (%{"name" => ...}) while the internal format uses atom keys (%{name: ...}). These helpers resolve a field across both key styles in one place.

Unlike the call[:name] || call["name"] idiom, field/3 preserves legitimately-falsy values: arguments: false / 0 / "" are returned as-is instead of being coalesced into the fallback. Only a missing key or an explicit nil falls through to the next key style and then the default.

Summary

Functions

The tool name a model actually meant, with provider noise stripped.

Get key from a tool-call map, checking the atom key then its string form.

Put value under key, preserving the map's existing key style.

Functions

clean_name(name)

@spec clean_name(term()) :: String.t()

The tool name a model actually meant, with provider noise stripped.

Some models wrap the name in XML-ish syntax (search"…); some providers emit a malformed call with no name at all. Anything after a " is dropped and a missing or non-binary name becomes "", so a bad call fails lookup instead of crashing the run with a FunctionClauseError.

Examples

iex> Nous.ToolCall.clean_name("search")
"search"

iex> Nous.ToolCall.clean_name(~s(search" foo))
"search"

iex> Nous.ToolCall.clean_name(nil)
""

field(call, key, default \\ nil)

@spec field(map(), atom(), term()) :: term()

Get key from a tool-call map, checking the atom key then its string form.

Returns default when the field is absent or nil under both key styles.

Examples

iex> Nous.ToolCall.field(%{"name" => "search"}, :name)
"search"

iex> Nous.ToolCall.field(%{arguments: false}, :arguments)
false

iex> Nous.ToolCall.field(%{}, :arguments, %{})
%{}

put_field(call, key, value)

@spec put_field(map(), atom(), term()) :: map()

Put value under key, preserving the map's existing key style.

Writes to the atom key when present, otherwise to the existing string key, and defaults to the string form when the field is new (matching the wire format providers use).