Skip to main content

Crate infr_engine

Crate infr_engine 

Source
Expand description

Chat text logic re-exports shared by infr run and infr serve (prompt rendering, channel/tool-call parsing, the reasoning/content delta splitter). The load-pipeline/session orchestration this crate originally stubbed out (the Engine struct) is now the unified transformer runner in infr-llama (SeamModel + the per-backend ChatModel impls, see docs/diffusion-gemma.md and docs/qwen35.md) — every arch, including DiffusionGemma, is built on it, so there is no separate “engine” seam left to implement here.

Reference for chat/channel/tool behavior: ~/Projects/scratch/dgemma-openai-server.py.

Structs§

ChatMessage
One chat message (OpenAI-shaped; tool fields preserved for the agentic round-trip).
ChatStream
A streamed piece of a response (re-exported from the single splitter in infr-chat). Incremental splitter for the streaming server path. Accumulates the raw decoded text and, on each piece, emits the newly-stable Reasoning (<think>…</think>) and Content deltas — holding back a marker-length tail so a partial <think>/</think>/<tool_call> marker is never emitted. Once a tool-call opener appears (and tool calls are allowed) it stops streaming content — matching every dialect finish() parses (Hermes <tool_call>, the pipe form <|tool_call>, and a Llama-3 bare-JSON {…} body), so non-Hermes calls can’t leak as content while also firing as a ToolCall. finish() flushes the held-back tails and parses the buffered tool call(s) into ToolCall deltas.
TokenLogprob
One generated token’s log probability and, when the request asked for them, its most likely alternatives. text/bytes are filled in by the backend that owns the tokenizer; the decode loop only knows the id and the probabilities.
TokenLogprobAlt
One alternative in TokenLogprob::top.
ToolCall
One parsed tool invocation from the model’s <|tool_call>…<tool_call|> block.

Enums§

Delta
A streamed piece of a response (re-exported from the single splitter in infr-chat). A streamed piece of a response.

Functions§

parse_hermes_tool_calls
Parse the Hermes / Qwen tool-call format — <tool_call>{"name":..,"arguments":{..}}</tool_call> (the format Qwen3 and most OSS function-calling models emit, and what the GGUF chat templates render). Returns (clean, calls): clean is the text with every block that parsed removed; calls the parsed invocations. The body is real JSON, so it’s parsed with serde (no hand-rolled scanner); arguments is accepted as either an object or a JSON string and normalised to a Value.
parse_tool_calls
Find all <|tool_call>…<tool_call|> blocks, parse them, and return:
prompt_prefills_think
A streamed piece of a response (re-exported from the single splitter in infr-chat). Does this rendered prompt end with a <think> PREFILL (the template opened the reasoning block itself — Qwen3.5 style — so the model’s output starts MID-reasoning with only the close marker)? Callers inject a synthetic "<think>" piece at the head of the stream when this is true, so the streaming splitter, the display styler and the history stripper all see a well-formed block through the one shared grammar.
split_channels
Split cumulative model output into (reasoning, content).
split_reasoning
Format-aware reasoning split: channel-marker output (E2B/gpt-oss <|channel>thought…<channel|>) goes through split_channels, everything else through split_think. THE one entry point for “what part of this reply was reasoning” — history stripping and batch consumers use this.
split_think
Split output into (reasoning, content) on the <think>…</think> markers Qwen3/DeepSeek-R1 emit — stripping EVERY reasoning span, not just the first (a constrained/looped turn can emit several). Handles the prefilled-<think> case where the open marker was added by the template (output then starts mid-reasoning and only the close marker appears) and an unterminated open (truncated turn — the tail is reasoning). No marker ⇒ all content, empty reasoning. This is THE reasoning grammar: infr run’s display, infr serve’s deltas and the chat history stripper all resolve reasoning-vs-content through this module.