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Β§
- ApiStatus
Error - HTTP status failure returned by the OpenAI-compatible endpoint.
- Pending
Tool πCall - 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.
- Stream
Text πPrinter - Streamed
Chat π - Tool
Loop Config - Read/edit sandbox + iteration cap for the tool loop.
EnumsΒ§
- Stream
Print πState
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 argumentsmay 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.argumentsto 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 structuredtool_callsfield (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
stomaxchars with an elision marker, for verbose dumps. - read_
streamed_ πchat - sanitize_
tool_ πcalls_ for_ history - Return a copy of
callssafe to store in the chat history that gets re-sent on the next request. - sse_
data π - system_
message π - Build the system message. With
cache=truethe content is an Anthropic-style block carryingcache_control: ephemeralso the (large, constant) system prompt can be cached across requests; withcache=falseitβ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.