infr_chat/lib.rs
1//! Shared chat text logic — turning conversations into model prompts and parsing model output back
2//! out. **No GPU, no model, no inference.** Used by `infr-llama` (to feed the forward pass) and by
3//! `infr-engine`/`infr-server` (the OpenAI-shaped API) so prompt rendering lives in ONE place instead
4//! of being re-implemented next to each backend.
5//!
6//! - [`render_chat_jinja`] / [`render_chat_user`] — render a GGUF's embedded `tokenizer.chat_template`
7//! (jinja, via minijinja) into a prompt string. Return `None` when the GGUF has no template (or it
8//! fails to render); callers fail loud rather than fabricate a default — infr only supports
9//! models that ship a chat template.
10//! - [`split_channels`] / [`parse_tool_calls`] — parse model output (reasoning vs answer,
11//! `<|tool_call>` blocks).
12
13mod stream;
14mod template;
15mod tools;
16
17pub use stream::{prompt_prefills_think, ChatStream, Delta};
18pub use template::{
19 render_chat_jinja, render_chat_oai, render_chat_user, render_template, TemplateError,
20};
21pub use tools::{
22 parse_any_tool_calls, parse_hermes_tool_calls, parse_tool_calls, split_channels,
23 split_reasoning, split_think, ToolCall,
24};
25
26/// One chat message (OpenAI-shaped; tool fields preserved for the agentic round-trip).
27#[derive(Clone, Debug, Default)]
28pub struct ChatMessage {
29 pub role: String,
30 pub content: String,
31 /// The assistant's OUTGOING tool calls (OpenAI `message.tool_calls`), replayed into the prompt on
32 /// the next turn so the model sees its own prior calls. Empty/None for non-assistant messages.
33 pub tool_calls: Option<Vec<ToolCall>>,
34 /// For a `tool`-role result message: which call it answers (OpenAI `tool_call_id`).
35 pub tool_call_id: Option<String>,
36 pub name: Option<String>,
37}