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), 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 and envelope),
11//! and 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;
19use crate::context::tool_data::ToolResultPayload;
20
21/// Envelope provenance. Fields are serde-additive so older records
22/// without them still deserialize.
23#[derive(Debug, Clone, Default, PartialEq, Eq, 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. Its containing block ID is the declaration
46/// identity; provider-issued identifiers ride on BlockMeta::provider_call_id.
47#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
48pub struct ToolCallPayload {
49 /// Name of the invoked tool — the key the executor dispatches on.
50 pub tool_name: String,
51 /// The call's arguments as a JSON value.
52 pub arguments: serde_json::Value,
53}
54
55/// A typed fact. Three axes: identity, content, envelope provenance.
56#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
57pub struct ContextBlock {
58 /// Identity axis: the UUID assigned to this fact block.
59 id: BlockId,
60 /// Content axis: the typed fact payload.
61 content: BlockContent,
62 /// Envelope provenance axis; serde-additive, defaulting when absent.
63 meta: BlockMeta,
64}
65
66impl ContextBlock {
67 /// Creates a context fact with an explicit identity, content, and metadata.
68 ///
69 /// Callers that change a block's content or metadata must supply a new
70 /// identity. Construction does not validate identity reuse against any
71 /// other block or context.
72 pub fn new(id: BlockId, content: BlockContent, meta: BlockMeta) -> Self {
73 Self { id, content, meta }
74 }
75
76 /// Returns this block's stable identity.
77 pub const fn id(&self) -> BlockId {
78 self.id
79 }
80
81 /// Returns this block's content by shared reference.
82 pub const fn content(&self) -> &BlockContent {
83 &self.content
84 }
85
86 /// Returns this block's metadata by shared reference.
87 pub const fn meta(&self) -> &BlockMeta {
88 &self.meta
89 }
90}
91
92/// A media fact: a cheap, durable reference. Bytes never enter facts —
93/// they appear only in resolved render payloads (provider side).
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95pub struct MediaRef {
96 /// IANA media type; only `image/*` renders to the wire, anything else
97 /// degrades to a deterministic text placeholder.
98 pub media_type: String,
99 /// Host-side asset reference (workspace asset id / content-addressed
100 /// hash). The kernel never interprets it; the host's resolver does.
101 pub reference: String,
102}
103
104impl MediaRef {
105 /// Wraps a media type and a host-side asset reference.
106 pub fn new(media_type: impl Into<String>, reference: impl Into<String>) -> Self {
107 Self {
108 media_type: media_type.into(),
109 reference: reference.into(),
110 }
111 }
112}
113
114/// The content shape of a block. Three shapes: one message's ordered
115/// parts (any role), a tool call, a tool result.
116#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
117#[serde(tag = "shape", content = "value", rename_all = "snake_case")]
118pub enum BlockContent {
119 /// One logical message's ordered content parts (text and media
120 /// references) — atomic at the block: identity, one
121 /// content identity; execution pairing is enforced by specialized append
122 /// operations rather than by importing arbitrary facts.
123 Parts(Vec<ContentPart>),
124 /// A model-issued tool invocation.
125 ToolCall(ToolCallPayload),
126 /// The recorded outcome of a prior call, paired by
127 /// `ToolResultPayload::call_block_id`.
128 ToolResult(ToolResultPayload),
129}
130
131/// One content atom inside a [`BlockContent::Parts`] block: ordered,
132/// identity-free, sharing the block's envelope. New modalities arrive
133/// as new variants (additive); media bytes stay out of facts forever.
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(tag = "part", content = "value", rename_all = "snake_case")]
136pub enum ContentPart {
137 /// Plain text; provider role assignment stays the renderer's job.
138 Text(TextPayload),
139 /// A media reference; bytes never enter facts.
140 Media(MediaRef),
141}