Chat

The Chat module contains the functions and types needed to use the ChatML formatted chat completion API. It includes the Message type, ChatRequestBody and ChatResponseBody types, and various functions for creating and handling API requests.

Client : Client.Client

Message

ChatResponseBody

The structure of the JSON response body received from the OpenRouter API.

build_http_request : Client, { tool_choice ? ToolChoice } -> RequestObject

Create a request object to be sent with basic-cli's Http.send using ChatML messages.

new_client

Initialize the OpenRouter API client with the required API key. All parameters besides apiKey are completely optional, and may be set during initialization, assigned later, or left as their defaults.

client = Chat.new_client { apiKey: "your_openrouter_api_key" }

Same as Client.new.

decode_response : List U8 -> Result ChatResponseBody [TooShort]

Decode the JSON response body to a ChatML style request.

decode_top_message_choice : List U8 -> Result Message [ NoChoices, ApiError ApiError, BadJson Str ]

Decode the JSON response body to the first message in the list of choices.

decode_error_response

Decode the JSON response body of an API error message.

update_messages : Client, HttpResponse -> Result Client [ NoChoices, ApiError ApiError, BadJson Str, HttpError { body : Str, status : U16 } ]

Decode the response from the OpenRouter API and append the first message choice to the list of messages. Any errors encountered will be appended as system messages.

add_system_message : Client, Str, { cached ? Bool } -> Client

Append a system message to the list of messages.

add_user_message : Client, Str, { cached ? Bool } -> Client

Append a user message to the list of messages.

add_assistant_message : Client, Str, { cached ? Bool } -> Client

Append an assistant message to the list of messages.