causa_kernel/ports/tool.rs
1//! Tool behavior — the `Tool` trait and the `ArtifactStore` port, plus the
2//! execution vocabulary they share with drivers: definitions and call
3//! context. Recorded facts (results, outputs, artifacts) live in
4//! `crate::context::tool_data`; batch dispatch lives in `causa-runtime`'s
5//! executor. A tool returns its recorded result and nothing else —
6//! unknown-outcome continuation and output retention are runtime-component
7//! configuration, not tool declarations.
8
9use async_trait::async_trait;
10use serde::{Deserialize, Serialize};
11
12use crate::context::tool_data::{ArtifactKind, ArtifactRef, ToolCallId, ToolResultPayload};
13use crate::ports::control::CallControl;
14
15/// The model-facing description of one callable tool; renderers map it onto
16/// the protocol's tool entries (name, description, parameter schema).
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct ToolDefinition {
19 /// The name the model uses to invoke the tool.
20 pub name: String,
21 /// Short summary of what the tool does, shown to the model.
22 pub description: String,
23 /// Schema of the arguments the tool accepts, embedded verbatim in the
24 /// rendered request.
25 pub parameters: serde_json::Value,
26}
27
28/// Identity of one tool dispatch, handed to [`Tool::execute`].
29#[derive(Debug, Clone)]
30pub struct ToolCallContext {
31 /// The stable [`ToolCallId`] this call's result must pair against.
32 pub call_id: ToolCallId,
33 /// Name of the tool being invoked, as the model called it.
34 pub tool_name: String,
35 /// The model-emitted arguments, as a raw JSON value.
36 pub arguments: serde_json::Value,
37}
38
39/// Provenance a caller attaches to bytes handed to [`ArtifactStore::persist`],
40/// so the store can label what it receives.
41pub struct ArtifactHint {
42 /// The tool whose output produced the bytes.
43 pub tool_name: String,
44 /// The tool call the bytes belong to.
45 pub call_id: ToolCallId,
46 /// The [`ArtifactKind`] classification of the bytes.
47 pub kind: ArtifactKind,
48}
49
50/// Error surface of the [`ArtifactStore`] port.
51#[derive(Debug, thiserror::Error)]
52pub enum StoreError {
53 /// Persisting the artifact failed; carries the human-readable cause.
54 #[error("persist failed: {0}")]
55 Persist(String),
56 /// Reading an artifact back failed; carries the human-readable cause.
57 #[error("read failed: {0}")]
58 Read(String),
59}
60
61/// Persistence port for tool artifacts — where oversized tool outputs are
62/// spilled and read back. Callers persist raw bytes plus an [`ArtifactHint`]
63/// and receive an [`ArtifactRef`] to embed in the result; concrete stores
64/// are host concerns.
65#[async_trait]
66pub trait ArtifactStore: Send + Sync {
67 /// Stores `data`, tagged with `hint`, and returns the [`ArtifactRef`]
68 /// describing the persisted artifact.
69 async fn persist(&self, data: &[u8], hint: ArtifactHint) -> Result<ArtifactRef, StoreError>;
70 /// Reads back the bytes stored under `id`; an optional byte range bounds
71 /// the read.
72 async fn read(
73 &self,
74 id: &str,
75 range: Option<std::ops::Range<u64>>,
76 ) -> Result<Vec<u8>, StoreError>;
77}
78
79/// The tool port: one callable tool the model can invoke. Implementations
80/// live outside the kernel; the driver's executor dispatches them. A tool
81/// returns only its recorded result — whether an `UnknownOutcome` result
82/// may continue the turn and how output is retained are runtime-component
83/// configuration (`causa_runtime`), not tool declarations.
84#[async_trait]
85pub trait Tool: Send + Sync {
86 /// The model-facing [`ToolDefinition`] for this tool.
87 fn definition(&self) -> ToolDefinition;
88 /// Runs one call under the call's [`CallControl`], returning the
89 /// recorded result.
90 async fn execute(&self, ctx: &ToolCallContext, control: &CallControl) -> ToolResultPayload;
91 /// Extension point for tools that persist artifacts: receives the host's
92 /// [`ArtifactStore`] when one is configured (`None` otherwise). The
93 /// default ignores the store and delegates to [`Tool::execute`].
94 async fn execute_with_store(
95 &self,
96 ctx: &ToolCallContext,
97 control: &CallControl,
98 store: Option<&dyn ArtifactStore>,
99 ) -> ToolResultPayload {
100 let _ = store;
101 self.execute(ctx, control).await
102 }
103}