causa-protocol 0.1.5

Causa — a minimal context kernel for Rust agents (facts, ports, adapters)
Documentation
//! Shared test fixtures for the three kernel-face renderers. `#[cfg(test)]`
//! only: one copy of the block/text/call/result/frame builders so the
//! per-protocol test modules cannot drift apart.

#![cfg(test)]

use serde_json::Value;

use causa_kernel::{
    BlockContent, BlockId, BlockMeta, ContentPart, ContextBlock, ContextFrame, MediaRef, Role,
    TextPayload, ToolCallPayload, ToolChange, ToolDefinition, ToolOutput, ToolResultPayload,
    ToolResultStatus,
};

pub(crate) fn label_id(label: &str) -> BlockId {
    let mut value = 0_u128;
    for byte in label.bytes() {
        value = value.wrapping_mul(257).wrapping_add(u128::from(byte));
    }
    BlockId::new(uuid::Uuid::from_u128(value))
}

pub(crate) fn block(
    seq: u64,
    content: BlockContent,
    source: Option<&str>,
    provider_call_id: Option<&str>,
) -> ContextBlock {
    ContextBlock::new(
        BlockId::new(uuid::Uuid::from_u128(u128::from(seq) + 1)),
        content,
        {
            let mut meta = BlockMeta::default();
            meta.provider_call_id = provider_call_id.map(String::from);
            meta.source = source.map(String::from);
            meta
        },
    )
}

pub(crate) fn text(seq: u64, text: &str, role: Role) -> ContextBlock {
    parts(seq, vec![ContentPart::Text(TextPayload::new(text))], role)
}

pub(crate) fn text_with_source(
    seq: u64,
    text: &str,
    role: Role,
    source: Option<&str>,
) -> ContextBlock {
    parts_with_source(
        seq,
        vec![ContentPart::Text(TextPayload::new(text))],
        role,
        source,
    )
}

/// A Message block from explicit content parts — the general message shape.
pub(crate) fn parts(seq: u64, parts: Vec<ContentPart>, role: Role) -> ContextBlock {
    parts_with_source(seq, parts, role, None)
}

/// A Message block from explicit content parts with explicit provenance.
pub(crate) fn parts_with_source(
    seq: u64,
    parts: Vec<ContentPart>,
    role: Role,
    source: Option<&str>,
) -> ContextBlock {
    block(seq, BlockContent::Message { role, parts }, source, None)
}

/// A single-text part convenience over [`parts`].
pub(crate) fn text_part(text: &str) -> ContentPart {
    ContentPart::Text(TextPayload::new(text))
}

/// A media part pointing at a host-side asset reference.
pub(crate) fn media_part(media_type: &str, reference: &str) -> ContentPart {
    ContentPart::Media(MediaRef::new(media_type, reference))
}

pub(crate) fn call(
    _seq: u64,
    call_id: &str,
    provider: Option<&str>,
    name: &str,
    arguments: Value,
) -> ContextBlock {
    ContextBlock::new(
        label_id(call_id),
        BlockContent::ToolCall(ToolCallPayload::new(name, arguments)),
        {
            let mut meta = BlockMeta::default();
            meta.provider_call_id = provider.map(String::from);
            meta
        },
    )
}

pub(crate) fn result(
    seq: u64,
    call_id: &str,
    status: ToolResultStatus,
    content: Value,
) -> ContextBlock {
    result_with_media(seq, call_id, status, content, Vec::new())
}

/// A tool result block with media attachments (references only).
pub(crate) fn result_with_media(
    seq: u64,
    call_id: &str,
    status: ToolResultStatus,
    content: Value,
    media: Vec<MediaRef>,
) -> ContextBlock {
    block(
        seq,
        BlockContent::ToolResult(
            ToolResultPayload::new(label_id(call_id), status, ToolOutput::new(content))
                .with_media(media),
        ),
        None,
        None,
    )
}

pub(crate) fn frame(blocks: Vec<ContextBlock>) -> ContextFrame {
    ContextFrame { blocks }
}

/// A tool change block adding `added` after withdrawing `removed`.
fn tool_change(label: &str, added: Vec<ToolDefinition>, removed: &[&str]) -> ContextBlock {
    ContextBlock::new(
        label_id(label),
        BlockContent::ToolChange(ToolChange::new(
            added,
            removed.iter().map(|name| (*name).to_owned()).collect(),
        )),
        BlockMeta::default(),
    )
}

/// `frame` with a declaration of `tools` in front, so it offers them.
pub(crate) fn with_tools(frame: &ContextFrame, tools: &[ToolDefinition]) -> ContextFrame {
    let mut blocks = vec![tool_change("declaration", tools.to_vec(), &[])];
    blocks.extend(frame.blocks.iter().cloned());
    ContextFrame { blocks }
}