Files
colibri-strix/c
RDouglas 0f9f99cc29 OpenAI tool calling for GLM-5.2: gated tool declaration + parse_tool_calls, non-tool path preserved, opt-in salvage (#96)
* openai_server: parse GLM-5.2 tool calls into OpenAI tool_calls

Fills the 'tools not supported yet' stub. Upstream declared nothing and
rejected tools/functions; this makes them real end to end:

  render_chat:  emit a tools-declaration <|system|> block; replay assistant
                tool_calls as <tool_call>NAME<arg_key>K</arg_key><arg_value>V
                </arg_value></tool_call>; render tool-result messages as
                <|observation|><tool_response>...</tool_response> (one
                observation per consecutive tool run).
  parse_tool_calls: turn the model's <tool_call> markers back into OpenAI
                tool_calls; strip <think> from surfaced content.
  generation:   non-stream returns message.tool_calls + finish_reason
                'tool_calls'; stream suppresses the markers from content
                deltas (marker-length hold-back so a split <tool_call> is
                caught) and emits tool_calls deltas after generation.

Default-safe: with no tools in the request, render and streaming take the
exact upstream path (byte-identical); tool handling is gated on tools present.
Marker format is authoritative (GLM-5.2 chat_template.jinja).

Optional de-mangler (COLI_TOOL_SALVAGE=1, default OFF) recovers malformed
calls from heavily-quantized models by mapping a lone payload onto the tool's
primary parameter; never rewrites well-formed output, never on by default.
A [api] tool-calls telemetry line reports strict vs de-mangled.

Pure stdlib; no new deps.

* openai_server: COLI_THINK global thinking default (opt-in)

COLI_THINK=1 makes thinking the default when a request sends neither
reasoning_effort nor enable_thinking (a launch-time global switch equivalent
to the old server's --think, useful because reasoningEffort propagation from
openai-compatible front-ends is unreliable). An explicit client value always
wins. Default off => exact OpenAI-standard behavior, so this is inert unless
enabled.

* openai_server: keepalive during long prefill (SSE reasoning pings)

engine.generate() blocks silently through the cold prefill (minutes for a
large prompt), and OpenCode/undici drop the socket after their idle timeout
-> the stream is 'terminated' before the first token. Upstream's SSE path
emits nothing until generation produces text, so it has no heartbeat.

A background pump emits a reasoning_content '.' delta whenever no event has
been written for KA_GAP (10s): the channel that reliably resets the client
timer and lands in the thinking panel, so answer content stays clean. All
wfile writes share a lock so the pump and event() never interleave; a
last-write timestamp gates the pump so it's silent while real tokens flow
(decode). The pump stops as soon as generation returns.

Inert for fast responses (nothing sent if events flow within 10s). No new
deps.

* openai_server: COLI_DEBUG echoes decoded tokens to stderr

Upstream sends decoded tokens only to the client (stdout->SSE), so the
console shows no generation text -- painful when the client has disconnected
or for local debugging. COLI_DEBUG=1 tees each decoded chunk to stderr at the
engine-callback boundary (both the plain and tool-call streaming paths).
Default off; no effect unless enabled.

* openai_server: use GLM-5.2 authoritative tool-declaration block

The tools were declared with a hand-written preamble; GLM-5.2 was trained on
the '# Tools' + <tools></tools> XML structure from chat_template.jinja. With
the wrong preamble the model doesn't recognize the tools as native and
hallucinates other frameworks' syntax (observed: repetitive 'end_action' and
key+value concatenated into one arg). Switch render_chat to the byte-exact
trained format so the model emits well-formed <tool_call> blocks.
2026-07-12 22:14:41 +02:00
..