causa_kernel/ports/prepare.rs
1//! Context preparation contract for each logical model round.
2//!
3//! Exclusive material preparation and its actual invocation configuration.
4
5use crate::{
6 CallControl, Context, ContextFrame, GenerationOptions, InvocationId, ModelRef, ToolSurface,
7};
8use async_trait::async_trait;
9
10/// Configuration visible while preparing this invocation's material.
11#[non_exhaustive]
12pub struct RoundInfo<'a> {
13 /// The actual logical model invocation being prepared.
14 pub invocation_id: &'a InvocationId,
15 /// The actual model selected by the caller.
16 pub model: &'a ModelRef,
17 /// The bound tools offered for this invocation.
18 pub tool_surface: &'a ToolSurface,
19 /// The generation settings selected by the caller.
20 pub generation: &'a GenerationOptions,
21}
22impl<'a> RoundInfo<'a> {
23 /// The facts of the round about to be invoked.
24 pub fn new(
25 invocation_id: &'a InvocationId,
26 model: &'a ModelRef,
27 tool_surface: &'a ToolSurface,
28 generation: &'a GenerationOptions,
29 ) -> Self {
30 Self {
31 invocation_id,
32 model,
33 tool_surface,
34 generation,
35 }
36 }
37}
38
39/// Failure to prepare a request's material.
40#[derive(Debug, thiserror::Error)]
41#[error("{message}")]
42#[non_exhaustive]
43pub struct PrepareError {
44 /// Human-readable failure detail.
45 pub message: String,
46}
47impl PrepareError {
48 /// A preparation failure with the given message.
49 pub fn new(message: impl Into<String>) -> Self {
50 Self {
51 message: message.into(),
52 }
53 }
54}
55
56/// Prepares an independent request frame with exclusive access to context.
57///
58/// Each context edit commits independently; edits already committed remain if
59/// preparation subsequently fails. The returned frame is not written back to
60/// context. Input consumption and recovery belong to the implementation.
61#[async_trait]
62pub trait ContextPreparer: Send + Sync {
63 /// Selects and optionally edits material using actual invocation settings.
64 async fn prepare(
65 &self,
66 context: &mut Context,
67 round: &RoundInfo<'_>,
68 control: &CallControl,
69 ) -> Result<ContextFrame, PrepareError>;
70}