use async_trait::async_trait;
use causa_kernel::{
CallControl, ModelGateway, ModelInvokeError, ModelItem, ModelOutput, ModelRequest,
ModelResponse, ModelStopReason, ProcessorContext, ProcessorData, ProcessorError, TextPayload,
Tool, ToolBatch, ToolBatchProcessor, ToolCallContext, ToolCallDraft, ToolDefinition,
ToolMetadata, ToolOutput, ToolResultPayload, ToolResultStatus, TurnId,
};
use causa_runtime::{
ToolExecutor, ToolExecutorOptions, TurnResult, TurnRunOptions, TurnRunner, new_block_id,
};
use std::sync::{Arc, Mutex as StdMutex};
use tokio::sync::{Mutex, mpsc, oneshot};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Effect {
ReadOnly,
Write,
}
#[derive(Debug, Clone)]
struct ApprovalGrant {
approved_path: String,
}
struct ScriptedGateway(Mutex<Vec<ModelOutput>>);
impl ScriptedGateway {
fn new(outputs: Vec<ModelOutput>) -> Arc<Self> {
Arc::new(Self(Mutex::new(outputs)))
}
}
#[async_trait]
impl ModelGateway for ScriptedGateway {
async fn invoke(
&self,
_request: &ModelRequest,
_control: &causa_kernel::CallControl,
) -> Result<ModelOutput, ModelInvokeError> {
let mut outputs = self.0.lock().await;
if outputs.is_empty() {
return Err(ModelInvokeError::new(
causa_kernel::ModelInvokeErrorKind::Permanent,
"script exhausted",
));
}
Ok(outputs.remove(0))
}
}
fn path_schema() -> serde_json::Value {
serde_json::json!({
"type": "object",
"properties": { "path": { "type": "string" } },
"required": ["path"]
})
}
struct ReadFile;
#[async_trait]
impl Tool for ReadFile {
fn definition(&self) -> ToolDefinition {
ToolDefinition::new("read_file", "Read a file.", path_schema())
}
fn metadata(&self) -> ToolMetadata {
ToolMetadata::new().with(Effect::ReadOnly)
}
async fn execute(
&self,
ctx: &ToolCallContext,
_control: &causa_kernel::CallControl,
_store: Option<&dyn causa_kernel::ArtifactStore>,
) -> ToolResultPayload {
ToolResultPayload::new(
ctx.call_block_id,
ToolResultStatus::Succeeded,
ToolOutput::new(serde_json::json!({ "contents": "the promised file contents" })),
)
}
}
struct WriteFile;
#[async_trait]
impl Tool for WriteFile {
fn definition(&self) -> ToolDefinition {
ToolDefinition::new("write_file", "Replace a file's contents.", path_schema())
}
fn metadata(&self) -> ToolMetadata {
ToolMetadata::new().with(Effect::Write)
}
async fn execute(
&self,
ctx: &ToolCallContext,
_control: &causa_kernel::CallControl,
_store: Option<&dyn causa_kernel::ArtifactStore>,
) -> ToolResultPayload {
let Some(grant) = ctx.attachment::<ApprovalGrant>() else {
return ToolResultPayload::new(
ctx.call_block_id,
ToolResultStatus::Failed,
ToolOutput::new(serde_json::json!({ "error": "missing approval grant" })),
);
};
ToolResultPayload::new(
ctx.call_block_id,
ToolResultStatus::Succeeded,
ToolOutput::new(serde_json::json!({
"written": true,
"approved_path": grant.approved_path,
})),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Ask,
ReadOnly,
}
type ModeHandle = Arc<StdMutex<Mode>>;
struct ApprovalRequest {
tool: String,
path: String,
reply: oneshot::Sender<bool>,
}
struct ApprovalProcessor(mpsc::Sender<ApprovalRequest>);
#[async_trait]
impl ToolBatchProcessor for ApprovalProcessor {
async fn process(
&self,
batch: &mut ToolBatch,
ctx: &ProcessorContext<'_>,
) -> Result<(), ProcessorError> {
let modes = ctx
.data::<ModeHandle>()
.ok_or_else(|| ProcessorError::Failed("the run sets no permission mode".into()))?;
let mut offset = 0;
while offset < batch.calls().len() {
let call = batch.calls()[offset].call();
let id = call.call_block_id;
let tool = call.input.tool_name.clone();
if ctx.tool_metadata::<Effect>(&tool) == Some(&Effect::ReadOnly) {
offset += 1;
continue;
}
let path = call.input.arguments["path"]
.as_str()
.unwrap_or("")
.to_owned();
let mode = *modes.lock().expect("mode lock");
let refusal = match mode {
Mode::ReadOnly => Some("the session is read-only"),
Mode::Ask => {
let (reply, decision) = oneshot::channel();
self.0
.send(ApprovalRequest {
tool,
path: path.clone(),
reply,
})
.await
.map_err(|error| ProcessorError::Failed(error.to_string()))?;
let approved = decision
.await
.map_err(|error| ProcessorError::Failed(error.to_string()))?;
(!approved).then_some("the reviewer declined this call")
}
};
match refusal {
None => {
let entry = &mut batch.calls_mut()[offset];
entry.attach(ApprovalGrant {
approved_path: path,
});
entry.push_note(TextPayload::new("Approved by the host policy."));
offset += 1;
}
Some(reason) => {
batch
.resolve_at(
batch.completed_len() + offset,
new_block_id(),
ToolResultPayload::new(
id,
ToolResultStatus::Rejected,
ToolOutput::new(serde_json::json!({ "error": reason })),
),
)
.map_err(|error| ProcessorError::Failed(error.to_string()))?;
}
}
}
Ok(())
}
}
fn call(tool: &str, path: &str) -> ModelItem {
ModelItem::tool_call(ToolCallDraft::new(
tool,
serde_json::json!({ "path": path }),
))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let gateway = ScriptedGateway::new(vec![
ModelOutput::new(
ModelResponse {
items: vec![
ModelItem::text("Read the notes, then update three files."),
call("read_file", "notes.txt"),
call("write_file", "notes.txt"),
call("write_file", "private.txt"),
call("write_file", "secrets.txt"),
],
},
ModelStopReason::ToolUse,
),
ModelOutput::new(
ModelResponse {
items: vec![ModelItem::text(
"Read and updated notes.txt; the other two writes were refused.",
)],
},
ModelStopReason::EndTurn,
),
]);
let mode: ModeHandle = Arc::new(StdMutex::new(Mode::Ask));
let (requests, mut receiver) = mpsc::channel::<ApprovalRequest>(1);
let reviewer = {
let mode = mode.clone();
tokio::spawn(async move {
while let Some(request) = receiver.recv().await {
let approved = request.path == "notes.txt";
println!(
"approval request: {} {} -> {}",
request.tool,
request.path,
if approved { "approved" } else { "declined" }
);
if !approved {
*mode.lock().expect("mode lock") = Mode::ReadOnly;
println!("mode switched to {:?}", Mode::ReadOnly);
}
let _ = request.reply.send(approved);
}
})
};
let executor = ToolExecutor::new(
vec![Arc::new(ReadFile), Arc::new(WriteFile)],
ToolExecutorOptions::default().with_before(vec![Arc::new(ApprovalProcessor(requests))]),
)?;
let runner = TurnRunner::new(gateway, Arc::new(executor));
let mut context = causa_kernel::Context::new();
context
.edit()
.append([causa_kernel::ContextBlock::text(
new_block_id(),
causa_kernel::Role::User,
"Read the notes and update the files.",
)])
.commit()?;
let outcome = runner
.run(
TurnId::new("approval_processor"),
context,
TurnRunOptions::new(causa_kernel::ModelRef::new("offline-demo"))
.with_processor_data(ProcessorData::new(mode.clone())),
CallControl::new(Default::default(), None),
)
.await;
for block in outcome.context.blocks() {
if let causa_kernel::BlockContent::ToolResult(result) = block.content() {
println!("result: {:?} {}", result.status, result.output.content);
}
}
match outcome.result {
TurnResult::Completed { final_output } => println!("{}", final_output.response.text()),
other => return Err(format!("turn did not complete: {other:?}").into()),
}
drop(runner);
reviewer.await?;
Ok(())
}