Skip to main content

tirea_contract/runtime/inference/
context.rs

1use crate::runtime::inference::transform::InferenceRequestTransform;
2use crate::runtime::tool_call::ToolDescriptor;
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6/// Context window management policy.
7///
8/// Pure data struct used by [`ContextWindowPlugin`] to configure the
9/// context window transform. Lives in the contract layer so plugin crates
10/// can construct it without depending on `tirea-agent-loop`.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ContextWindowPolicy {
13    /// Model's total context window size in tokens.
14    pub max_context_tokens: usize,
15    /// Tokens reserved for model output.
16    pub max_output_tokens: usize,
17    /// Minimum number of recent messages to always preserve (never truncated).
18    pub min_recent_messages: usize,
19    /// Whether to enable prompt caching (Anthropic `cache_control: ephemeral`).
20    pub enable_prompt_cache: bool,
21}
22
23impl Default for ContextWindowPolicy {
24    fn default() -> Self {
25        Self {
26            max_context_tokens: 200_000,
27            max_output_tokens: 16_384,
28            min_recent_messages: 10,
29            enable_prompt_cache: true,
30        }
31    }
32}
33
34/// Inference-phase extension: system/session context and tool descriptors.
35///
36/// Populated by `AddSystemContext`, `AddSessionContext`, `ExcludeTool`,
37/// `IncludeOnlyTools`, `AddRequestTransform` actions during `BeforeInference`.
38#[derive(Default, Clone)]
39pub struct InferenceContext {
40    /// System context lines appended to the system prompt.
41    pub system_context: Vec<String>,
42    /// Session context messages injected before user messages.
43    pub session_context: Vec<String>,
44    /// Available tool descriptors (can be filtered by actions).
45    pub tools: Vec<ToolDescriptor>,
46    /// Request transforms registered by plugins. Applied in order after
47    /// messages are assembled, before the request is sent to the LLM.
48    pub request_transforms: Vec<Arc<dyn InferenceRequestTransform>>,
49}
50
51impl std::fmt::Debug for InferenceContext {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_struct("InferenceContext")
54            .field("system_context", &self.system_context)
55            .field("session_context", &self.session_context)
56            .field("tools", &self.tools)
57            .field("request_transforms", &self.request_transforms.len())
58            .finish()
59    }
60}