Skip to main content

Module api

Module api 

Source
Expand description

OpenAI-compatible /chat/completions client with a tool-calling loop.

Builds the request body, applies bearer auth, parses the first choice, and when the assistant returns tool_calls runs them via crate::tools::execute_tool and feeds the results back until it returns plain content. Prompt-cache markers and token-budget trimming are intentionally omitted for this first cut.

StructsΒ§

ApiStatusError
HTTP status failure returned by the OpenAI-compatible endpoint.
PendingToolCall πŸ”’
Spinner πŸ”’
A stderr spinner shown until the first streamed response chunk arrives. Active only on a TTY; a no-op otherwise so logs and redirected output stay clean.
StreamTextPrinter πŸ”’
StreamedChat πŸ”’
ToolLoopConfig
Read/edit sandbox + iteration cap for the tool loop.

EnumsΒ§

StreamPrintState πŸ”’

ConstantsΒ§

DUMP_MAX_CHARS πŸ”’
Max characters of any single field printed in a verbose transcript dump.
SLOW_TOOL_LOG_AFTER πŸ”’
TOOL_CALL_CLOSE πŸ”’
TOOL_CALL_OPEN πŸ”’

FunctionsΒ§

append_json_text πŸ”’
apply_bearer πŸ”’
apply_stream_delta πŸ”’
arguments_to_string πŸ”’
arguments may arrive as a JSON string (OpenAI) or an inline object; normalize to a string.
chat
Run a chat turn with an optional tool loop. Returns the final assistant text. Every request’s token usage is summed into usage (the tool loop makes one request per iteration).
error_summary
One-line provider/request error for logs and reports.
find_sse_delimiter πŸ”’
floor_char_boundary πŸ”’
is_api_error
True if an error came from the provider/request layer rather than local tool execution or response parsing. The controller can skip a round for these.
message_content_to_string πŸ”’
normalize_tool_call_arguments πŸ”’
OpenAI-compatible chat history requires function.arguments to be a string.
normalize_tool_calls πŸ”’
parse_one_text_call πŸ”’
Parse one <tool_call> block body ({"name":..., "arguments"/"parameters":...}) into a synthetic OpenAI-shaped tool call, or None if it isn’t a valid call.
parse_text_tool_calls πŸ”’
Fallback for models that emit tool calls as <tool_call>{...}</tool_call> text in the message content instead of the structured tool_calls field (the Hermes/Qwen convention). Returns the parsed calls (in OpenAI shape) plus the content with the blocks stripped out.
path_with_line_range πŸ”’
preview πŸ”’
Truncate s to max chars with an elision marker, for verbose dumps.
read_streamed_chat πŸ”’
sanitize_tool_calls_for_history πŸ”’
Return a copy of calls safe to store in the chat history that gets re-sent on the next request.
sse_data πŸ”’
system_message πŸ”’
Build the system message. With cache=true the content is an Anthropic-style block carrying cache_control: ephemeral so the (large, constant) system prompt can be cached across requests; with cache=false it’s a plain string, byte-identical to a no-caching request.
tool_call_label πŸ”’
A short, human-friendly label for a tool call (shown in non-verbose output): prefers the most salient argument (path/pattern) over the raw JSON.
tool_loop
Convenience: a tool loop config rooted at sandbox.