Skip to main content

causa_kernel/context/
block.rs

1//! Block facts -- the kernel-side vocabulary of a conversation.
2//!
3//! A ContextBlock is a typed fact with three orthogonal axes:
4//! identity (id, sequence), content (BlockContent), and envelope
5//! provenance (BlockMeta). Provider-specific role assignment (system /
6//! user / assistant / tool) is the renderer's job, not the kernel's.
7//!
8//! Content vocabulary is **Parts**: one
9//! logical message's mixed content commits as one block of ordered
10//! [`ContentPart`]s — the block is the fact atom (identity, sequence,
11//! single version bump, pairing invariant), the part is the content
12//! atom (ordered, identity-free, shares the envelope). Media enters as
13//! a [`MediaRef`] — a cheap durable reference; bytes never enter facts,
14//! they appear only in resolved render payloads (provider side).
15
16use serde::{Deserialize, Serialize};
17
18use crate::context::ids::{BlockId, BlockSequence};
19use crate::context::tool_data::{ToolCallId, ToolResultPayload};
20
21/// Envelope provenance. Fields are serde-additive so legacy snapshots
22/// without them still deserialize.
23#[derive(Debug, Clone, Default, Serialize, Deserialize)]
24pub struct BlockMeta {
25    /// Provider-issued identifier (e.g. upstream call id).
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub provider_call_id: Option<String>,
28
29    /// Origin tag (e.g. "user", "provider:gpt-4o", "host"). Optional.
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub source: Option<String>,
32}
33
34/// A single string of text. Serializes transparently as the inner string.
35#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(transparent)]
37pub struct TextPayload(pub String);
38impl TextPayload {
39    /// Wrap any string-like value as a text payload.
40    pub fn new(s: impl Into<String>) -> Self {
41        Self(s.into())
42    }
43}
44
45/// A model-issued tool call. call_id is the kernel-generated causal key;
46/// any provider-issued identifier rides on BlockMeta::provider_call_id.
47#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
48pub struct ToolCallPayload {
49    /// Kernel-generated causal key pairing results to this call (unique
50    /// within a single turn); provider-issued identifiers ride on
51    /// [`BlockMeta::provider_call_id`].
52    pub call_id: ToolCallId,
53    /// Name of the invoked tool — the key the executor dispatches on.
54    pub tool_name: String,
55    /// The call's arguments as a JSON value.
56    pub arguments: serde_json::Value,
57}
58
59/// A typed fact. Three axes: identity, content, envelope provenance.
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct ContextBlock {
62    /// Identity axis: the owning turn's id plus the block's sequence
63    /// (always equal to `sequence`).
64    pub id: BlockId,
65    /// Position of the block within its turn: zero-based, increasing by
66    /// one per block; mirrors `id.sequence`.
67    pub sequence: BlockSequence,
68    /// Content axis: the typed fact payload.
69    pub content: BlockContent,
70    /// Envelope provenance axis; serde-additive, defaulting when absent.
71    pub meta: BlockMeta,
72}
73
74/// A media fact: a cheap, durable reference. Bytes never enter facts —
75/// they appear only in resolved render payloads (provider side).
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77pub struct MediaRef {
78    /// IANA media type; only `image/*` renders to the wire, anything else
79    /// degrades to a deterministic text placeholder.
80    pub media_type: String,
81    /// Host-side asset reference (workspace asset id / content-addressed
82    /// hash). The kernel never interprets it; the host's resolver does.
83    pub reference: String,
84}
85
86impl MediaRef {
87    /// Wraps a media type and a host-side asset reference.
88    pub fn new(media_type: impl Into<String>, reference: impl Into<String>) -> Self {
89        Self {
90            media_type: media_type.into(),
91            reference: reference.into(),
92        }
93    }
94}
95
96/// The content shape of a block. Three shapes: one message's ordered
97/// parts (any role), a tool call, a tool result.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99#[serde(tag = "shape", content = "value", rename_all = "snake_case")]
100pub enum BlockContent {
101    /// One logical message's ordered content parts (text and media
102    /// references) — atomic at the block: identity, sequence, one
103    /// version bump, and the pairing invariant live only here.
104    Parts(Vec<ContentPart>),
105    /// A model-issued tool invocation.
106    ToolCall(ToolCallPayload),
107    /// The recorded outcome of a prior call, paired by
108    /// `ToolResultPayload::call_id`.
109    ToolResult(ToolResultPayload),
110}
111
112/// One content atom inside a [`BlockContent::Parts`] block: ordered,
113/// identity-free, sharing the block's envelope. New modalities arrive
114/// as new variants (additive); media bytes stay out of facts forever.
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
116#[serde(tag = "part", content = "value", rename_all = "snake_case")]
117pub enum ContentPart {
118    /// Plain text; provider role assignment stays the renderer's job.
119    Text(TextPayload),
120    /// A media reference; bytes never enter facts.
121    Media(MediaRef),
122}