Public declaration syntax from forge-rs/crates/forge-core/src/model.rs Original source SHA-256: 5c15069351107b972aac87130e3d8199c6ef6a30cd9da9113269a417f53d9d8e Function bodies and constant values are omitted. This is not the complete implementation. Source line 30 pub type StreamChunkResult = ForgeResult; Source line 39 pub type ChunkStream<'a> = Pin + Send + 'a>>; Source line 79 pub fn buffered_into_chunks(chunks: Vec) -> ChunkStream<'static>; Source line 109 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ModelCapabilities { /// Whether the model supports text generation. pub text_generation: bool, /// Whether the model supports structured output (JSON mode). pub structured_output: bool, /// Whether the model supports tool calling. pub tool_calling: bool, /// Whether the model supports vision (image input). pub vision: bool, /// Whether the model supports audio input/output. pub audio: bool, /// Whether the model supports embedding generation. pub embedding: bool, /// Maximum number of tokens in the context window. pub max_context_tokens: u32, /// Maximum number of tokens the model can generate in a single response. pub max_output_tokens: u32 } Source line 169 #[async_trait] pub trait LanguageModel: Send + Sync { /// Returns the model identifier (e.g., "gpt-4o", "claude-sonnet-4-5-20250929"). fn model_id(&self) -> &str; /// Returns the provider namespace (e.g., "openai", "anthropic"). fn provider(&self) -> &str; /// Generates a complete response from the model. /// /// # Arguments /// /// * `messages` - The conversation history. /// * `tools` - Available tool definitions for this inference call. /// * `options` - Generation options (temperature, max_tokens, etc.). /// /// # Returns /// /// A `GenerateResult` containing the model's response, usage statistics, /// and finish reason. /// /// # Errors /// /// Returns `ForgeError` if the provider call fails, times out, or returns /// an invalid response. async fn generate( &self, messages: &[ModelMessage], tools: &[ToolDefinition], options: &GenerateOptions, ) -> ForgeResult; /// Streams a response from the model as chunks. /// /// **Deprecated.** Returns the full `Vec` after the upstream /// provider has yielded its terminal `Done`. Despite the name, this /// method does not deliver chunks incrementally — use /// [`stream_chunks`](Self::stream_chunks) for real per-token streaming. /// /// Will be removed in v0.3.0 once every in-tree provider has a native /// [`stream_chunks`] implementation. /// /// # Errors /// /// Returns `ForgeError` if the provider call fails. #[deprecated( since = "0.2.0", note = "use `stream_chunks` for real per-token streaming; this method buffers the entire response before returning. Will be removed in 0.3.0." )] async fn stream( &self, messages: &[ModelMessage], tools: &[ToolDefinition], options: &GenerateOptions, ) -> ForgeResult>; /// **Real per-token streaming.** Returns a stream that yields each chunk /// as soon as the upstream provider produces it on the wire. /// /// Per RFC 0001 (`rfcs/0001-stream-chunks.md`): each item is itself a /// `Result` so mid-stream errors don't lose already-received chunks. The /// outer `Result` covers connect-time failures (DNS, TLS, auth, malformed /// request); per-item `Err` covers mid-stream failures. /// /// # Default implementation (TRANSITIONAL) /// /// The default impl calls the (deprecated) [`stream`](Self::stream) method /// and wraps the resulting `Vec` via [`buffered_into_chunks`]. /// **This is a transitional shim — it is NOT real streaming.** It exists /// only so existing providers compile against the new trait method while /// they are being rewritten one at a time for native streaming. /// /// **Providers MUST override this method with a native streaming /// implementation before 0.3.0.** Once every in-tree provider has a /// native `stream_chunks`, the default will be removed (the trait method /// becomes required) and `stream` will be deleted entirely. /// /// To find providers that still rely on the default impl, grep for /// `buffered_into_chunks` in their `stream_chunks` method body — that's /// the native-streaming migration checklist. /// /// # Arguments /// /// * `messages` - The conversation history. /// * `tools` - Available tool definitions for this inference call. /// * `options` - Generation options. /// /// # Returns /// /// A [`ChunkStream`] that yields [`StreamChunkResult`] items. /// /// # Errors /// /// - Outer `ForgeResult<...>` returns `Err` for connect-time failures. /// - Per-item `Err` for mid-stream failures (caller decides whether to /// abort the stream). async fn stream_chunks( &self, messages: &[ModelMessage], tools: &[ToolDefinition], options: &GenerateOptions, ) -> ForgeResult> ; /// Returns `true` if this model supports tool calling. fn supports_tool_calling(&self) -> bool ; /// Returns `true` if this model supports structured output (JSON mode). fn supports_structured_output(&self) -> bool ; /// Returns `true` if this model supports image input. fn supports_image_input(&self) -> bool ; /// Returns `true` if this model supports streaming. fn supports_streaming(&self) -> bool ; /// Returns the model's capabilities. /// /// The default implementation constructs a [`ModelCapabilities`] from the /// individual `supports_*` methods. Override this to provide accurate values /// for `max_context_tokens` and `max_output_tokens`. /// /// # ANVIL Spec Reference /// /// ANVIL Spec section 6.2 -- Model Capabilities. /// /// # Returns /// /// A [`ModelCapabilities`] describing what this model can do. fn capabilities(&self) -> ModelCapabilities ; }