#![allow(dead_code)]
pub mod coordinator;
use async_trait::async_trait;
use causa_kernel::{
AttemptControl, CallControl, ContentPart, ContextFrame, ConversationId, ModelGateway,
ModelInvokeError, ModelInvokeErrorKind, ModelOutput, ModelRequest, ModelResponse,
ModelStopReason, ModelStream, RoundId, StreamDelta, TextPayload, Tool, ToolCallContext,
ToolCallDraft, ToolCallId, ToolCallPayload, ToolDefinition, ToolOutput, ToolResultPayload,
ToolResultStatus, Truncation, TurnContext, TurnId,
};
use causa_runtime::{
BatchDecision, Compaction, CompactionError, CompactionInput, CompactionOutput,
ConversationState, HookOutcome, ResumeRequest, RunControl, SealedResult, Session,
SessionConfig, SessionHandle, SubmitRequest, ToolExecutor, TurnInteraction, TurnLimits,
TurnPolicy, TurnRunOptions, TurnRunner, WaitEnd, WorkObservation, WorkReceipt, WorkRef,
WorkState,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
pub fn turn_id(s: &str) -> TurnId {
TurnId::new(s)
}
pub fn ctx(s: &str) -> TurnContext {
TurnContext::new(turn_id(s))
}
pub fn commit_sealed(state: &mut ConversationState, turn_label: &str, result: SealedResult) {
state.begin_turn(TurnId::new(turn_label)).unwrap();
state
.active_turn_mut()
.unwrap()
.append_input(TextPayload::new("hi"), "user")
.unwrap();
state.seal_turn(TurnId::new(turn_label), result).unwrap();
state.commit(TurnId::new(turn_label)).unwrap();
}
pub fn endturn_output(text: &str) -> ModelOutput {
ModelOutput {
response: ModelResponse {
text: TextPayload::new(text),
tool_calls: vec![],
},
usage: None,
stop_reason: ModelStopReason::EndTurn,
reasoning: None,
}
}
pub fn tooluse_output(text: &str, tool_name: &str, args: serde_json::Value) -> ModelOutput {
tooluse_calls_output(text, vec![draft(tool_name, args)])
}
pub fn tooluse_calls_output(text: &str, calls: Vec<ToolCallDraft>) -> ModelOutput {
ModelOutput {
response: ModelResponse {
text: TextPayload::new(text),
tool_calls: calls,
},
usage: None,
stop_reason: ModelStopReason::ToolUse,
reasoning: None,
}
}
pub fn draft(tool_name: &str, args: serde_json::Value) -> ToolCallDraft {
ToolCallDraft {
tool_name: tool_name.into(),
arguments: args,
provider_call_id: None,
}
}
pub fn ctrl() -> RunControl {
RunControl::new(tokio_util::sync::CancellationToken::new(), None)
}
pub fn limits(r: u32, t: u32) -> TurnLimits {
TurnLimits {
max_model_rounds: r,
max_tool_calls: t,
}
}
pub fn options_with_limits(r: u32, t: u32) -> TurnRunOptions {
TurnRunOptions {
policy: TurnPolicy {
limits: limits(r, t),
..Default::default()
},
..Default::default()
}
}
pub fn runner_with(gateway: Arc<dyn ModelGateway>, tools: Vec<Arc<dyn Tool>>) -> TurnRunner {
TurnRunner::new(gateway, Arc::new(ToolExecutor::from_vec(tools)))
}
pub struct PausingInteraction;
#[async_trait]
impl TurnInteraction for PausingInteraction {
async fn decide_batch(&self, _calls: &[ToolCallPayload]) -> BatchDecision {
BatchDecision::Pause { deadline: None }
}
}
pub struct GatedPauseInteraction {
entered: tokio::sync::Semaphore,
release: tokio::sync::Semaphore,
}
impl GatedPauseInteraction {
pub fn new() -> Arc<Self> {
Arc::new(Self {
entered: tokio::sync::Semaphore::new(0),
release: tokio::sync::Semaphore::new(0),
})
}
pub async fn wait_entered(&self) {
tokio::time::timeout(Duration::from_secs(5), self.entered.acquire())
.await
.expect("the gated interaction is entered within 5s")
.expect("the entry semaphore stays open")
.forget();
}
pub fn release(&self) {
self.release.add_permits(1);
}
}
#[async_trait]
impl TurnInteraction for GatedPauseInteraction {
async fn decide_batch(&self, _calls: &[ToolCallPayload]) -> BatchDecision {
self.entered.add_permits(1);
let permit = self
.release
.acquire()
.await
.expect("the release semaphore stays open");
permit.forget();
BatchDecision::Pause { deadline: None }
}
}
pub fn idle_session(id: &str, gateway: Arc<dyn ModelGateway>) -> Session {
Session::new(
ConversationState::new(ConversationId(id.into())),
Arc::new(runner_with(gateway, vec![])),
TurnRunOptions::default(),
SessionConfig::default(),
)
.expect("an empty state is an idle session base")
}
pub fn pausing_session(
id: &str,
gateway: Arc<dyn ModelGateway>,
tools: Vec<Arc<dyn Tool>>,
config: SessionConfig,
) -> Session {
let options = TurnRunOptions {
interaction: Arc::new(PausingInteraction),
..Default::default()
};
Session::new(
ConversationState::new(ConversationId(id.into())),
Arc::new(runner_with(gateway, tools)),
options,
config,
)
.expect("an empty state is an idle session base")
}
pub fn session_req(key: &str, text: &str) -> SubmitRequest {
SubmitRequest {
request_key: key.into(),
parts: vec![ContentPart::Text(TextPayload::new(text))],
}
}
pub async fn submit_to_pause(handle: &SessionHandle, key: &str) -> (WorkReceipt, WorkObservation) {
let receipt = handle
.submit(session_req(key, "go"))
.expect("an idle session accepts the work");
let waited = handle
.wait(&receipt.work, Duration::from_secs(5))
.await
.expect("the accepted work is observable");
assert_eq!(waited.end, WaitEnd::ReachedState);
assert_eq!(waited.observation.state, WorkState::Paused);
(receipt, waited.observation)
}
pub async fn paused_work(
id: &str,
gateway: Arc<RecordingGateway>,
) -> (Session, SessionHandle, WorkRef) {
let session = pausing_session(
id,
gateway,
vec![Arc::new(EchoTool)],
SessionConfig::default(),
);
let handle = session.handle();
let (receipt, _) = submit_to_pause(&handle, "pause").await;
(session, handle, receipt.work)
}
pub fn awaiting_echo() -> ToolCallPayload {
ToolCallPayload {
call_id: ToolCallId::generate(RoundId(0), "echo", &serde_json::json!({}), 0),
tool_name: "echo".into(),
arguments: serde_json::json!({}),
}
}
pub fn approve(calls: Vec<ToolCallPayload>) -> ResumeRequest {
ResumeRequest {
decision: Some(HookOutcome::passthrough(calls)),
inject: Vec::new(),
}
}
pub struct RecordingGateway {
outputs: Mutex<Vec<Result<ModelOutput, ModelInvokeErrorKind>>>,
recorded: Mutex<Vec<ModelRequest>>,
repeat_last: bool,
}
impl RecordingGateway {
pub fn scripted(outputs: Vec<Result<ModelOutput, ModelInvokeErrorKind>>) -> Arc<Self> {
Arc::new(Self {
outputs: Mutex::new(outputs),
recorded: Mutex::new(vec![]),
repeat_last: false,
})
}
pub fn repeating_last(outputs: Vec<Result<ModelOutput, ModelInvokeErrorKind>>) -> Arc<Self> {
Arc::new(Self {
outputs: Mutex::new(outputs),
recorded: Mutex::new(vec![]),
repeat_last: true,
})
}
pub fn recorded(&self) -> std::sync::MutexGuard<'_, Vec<ModelRequest>> {
self.recorded.lock().unwrap()
}
pub fn frames(&self) -> Vec<ContextFrame> {
self.recorded
.lock()
.unwrap()
.iter()
.map(|r| r.frame.clone())
.collect()
}
}
#[async_trait]
impl ModelGateway for RecordingGateway {
async fn invoke(
&self,
req: &ModelRequest,
_ctrl: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
self.recorded.lock().unwrap().push(req.clone());
let mut g = self.outputs.lock().unwrap();
let out = if self.repeat_last {
if g.len() > 1 {
g.remove(0)
} else {
g[0].clone()
}
} else if g.is_empty() {
Err(ModelInvokeErrorKind::Permanent)
} else {
g.remove(0)
};
out.map_err(|kind| ModelInvokeError::new(kind, "scripted"))
}
}
pub struct GatedGateway {
text: String,
calls: AtomicUsize,
entered: tokio::sync::Semaphore,
release: tokio::sync::Semaphore,
honour_cancel: bool,
}
impl GatedGateway {
pub fn new(text: &str, honour_cancel: bool) -> Arc<Self> {
Arc::new(Self {
text: text.to_string(),
calls: AtomicUsize::new(0),
entered: tokio::sync::Semaphore::new(0),
release: tokio::sync::Semaphore::new(0),
honour_cancel,
})
}
pub fn calls(&self) -> usize {
self.calls.load(Ordering::SeqCst)
}
pub async fn wait_entered(&self) {
tokio::time::timeout(Duration::from_secs(5), self.entered.acquire())
.await
.expect("the gated gateway is entered within 5s")
.expect("the entry semaphore stays open")
.forget();
}
pub fn release(&self) {
self.release.add_permits(1);
}
}
#[async_trait]
impl ModelGateway for GatedGateway {
async fn invoke(
&self,
_req: &ModelRequest,
ctrl: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
self.calls.fetch_add(1, Ordering::SeqCst);
self.entered.add_permits(1);
if self.honour_cancel {
tokio::select! {
biased;
_ = ctrl.cancellation_token().cancelled() => Err(ModelInvokeError::new(
ModelInvokeErrorKind::Cancelled,
"gated gateway: cancelled by the session",
)),
permit = self.release.acquire() => {
permit.expect("the release semaphore stays open").forget();
Ok(endturn_output(&self.text))
}
}
} else {
let permit = self
.release
.acquire()
.await
.expect("the release semaphore stays open");
permit.forget();
Ok(endturn_output(&self.text))
}
}
}
pub struct PanickingGateway;
#[async_trait]
impl ModelGateway for PanickingGateway {
async fn invoke(
&self,
_req: &ModelRequest,
_ctrl: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
panic!("boom: panicking gateway");
}
}
pub struct SlowGateway;
#[async_trait]
impl ModelGateway for SlowGateway {
async fn invoke(
&self,
_req: &ModelRequest,
_ctrl: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
tokio::time::sleep(Duration::from_millis(250)).await;
Ok(tooluse_output("late", "echo", serde_json::json!({})))
}
}
pub struct FakeGateway {
pub outputs: Mutex<Vec<Result<ModelOutput, ModelInvokeErrorKind>>>,
}
impl FakeGateway {
pub fn new(outputs: Vec<Result<ModelOutput, ModelInvokeErrorKind>>) -> Self {
Self {
outputs: Mutex::new(outputs),
}
}
}
#[async_trait]
impl ModelGateway for FakeGateway {
async fn invoke(
&self,
_req: &ModelRequest,
_control: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
let mut guard = self.outputs.lock().unwrap();
if guard.is_empty() {
return Err(ModelInvokeError::new(
ModelInvokeErrorKind::Permanent,
"no more fake outputs",
));
}
match guard.remove(0) {
Ok(o) => Ok(o),
Err(k) => Err(ModelInvokeError::new(k, "fake error")),
}
}
}
pub struct TestDedupHook;
#[async_trait]
impl causa_runtime::ToolUseHook for TestDedupHook {
async fn apply(
&self,
calls: Vec<causa_kernel::ToolCallPayload>,
_ctx: &causa_runtime::HookCtx<'_>,
) -> causa_runtime::HookOutcome {
use std::collections::HashMap;
let mut seen: HashMap<(String, serde_json::Value), ()> = HashMap::new();
let mut to_execute = Vec::new();
let mut rejected = Vec::new();
for payload in calls {
let key = (payload.tool_name.clone(), payload.arguments.clone());
if seen.insert(key, ()).is_some() {
rejected.push(ToolResultPayload {
call_id: payload.call_id.clone(),
status: ToolResultStatus::Rejected,
output: ToolOutput::new(serde_json::json!({"error": "duplicate tool call"})),
media: Vec::new(),
});
} else {
to_execute.push(payload);
}
}
causa_runtime::HookOutcome {
to_execute,
rejected,
unknown_decisions: Vec::new(),
}
}
}
pub fn runner_with_dedup(gateway: Arc<dyn ModelGateway>, tools: Vec<Arc<dyn Tool>>) -> TurnRunner {
TurnRunner::with_hook(
gateway,
Arc::new(ToolExecutor::from_vec(tools)),
Arc::new(TestDedupHook),
)
}
pub struct EchoTool;
#[async_trait]
impl Tool for EchoTool {
fn definition(&self) -> ToolDefinition {
ToolDefinition {
name: "echo".into(),
description: "echo".into(),
parameters: serde_json::json!({"type":"object"}),
}
}
async fn execute(&self, ctx: &ToolCallContext, _c: &CallControl) -> ToolResultPayload {
ToolResultPayload {
call_id: ctx.call_id.clone(),
status: ToolResultStatus::Succeeded,
output: ToolOutput {
content: serde_json::json!({"echo": ctx.arguments}),
truncation: Truncation::None,
meta: None,
artifact: None,
},
media: Vec::new(),
}
}
}
pub struct FailTool;
#[async_trait]
impl Tool for FailTool {
fn definition(&self) -> ToolDefinition {
ToolDefinition {
name: "fail".into(),
description: "fail".into(),
parameters: serde_json::json!({"type":"object"}),
}
}
async fn execute(&self, ctx: &ToolCallContext, _c: &CallControl) -> ToolResultPayload {
ToolResultPayload {
call_id: ctx.call_id.clone(),
status: ToolResultStatus::Failed,
output: ToolOutput::new(serde_json::json!({"err": "fail"})),
media: Vec::new(),
}
}
}
pub struct UnknownStopTool;
#[async_trait]
impl Tool for UnknownStopTool {
fn definition(&self) -> ToolDefinition {
ToolDefinition {
name: "unk".into(),
description: "unk".into(),
parameters: serde_json::json!({"type":"object"}),
}
}
async fn execute(&self, ctx: &ToolCallContext, _c: &CallControl) -> ToolResultPayload {
ToolResultPayload {
call_id: ctx.call_id.clone(),
status: ToolResultStatus::UnknownOutcome,
output: ToolOutput::new(serde_json::json!({"unk": true})),
media: Vec::new(),
}
}
}
pub struct DropAllCompaction;
#[async_trait]
impl Compaction for DropAllCompaction {
async fn compact(&self, _input: CompactionInput) -> Result<CompactionOutput, CompactionError> {
Ok(CompactionOutput {
blocks: Vec::new(),
summary: None,
truncated: true,
})
}
}
pub type StreamScript = Result<Vec<StreamDelta>, ModelInvokeErrorKind>;
pub struct RecordingStreamingGateway {
scripts: Mutex<Vec<StreamScript>>,
recorded: Mutex<Vec<ModelRequest>>,
}
impl RecordingStreamingGateway {
pub fn scripted(scripts: Vec<StreamScript>) -> Arc<Self> {
Arc::new(Self {
scripts: Mutex::new(scripts),
recorded: Mutex::new(vec![]),
})
}
pub fn attempts(&self) -> usize {
self.recorded.lock().unwrap().len()
}
}
#[async_trait]
impl ModelGateway for RecordingStreamingGateway {
async fn invoke(
&self,
_req: &ModelRequest,
_ctrl: &AttemptControl,
) -> Result<ModelOutput, ModelInvokeError> {
Err(ModelInvokeError::new(
ModelInvokeErrorKind::Permanent,
"streaming fixture has no invoke script",
))
}
async fn stream(
&self,
req: &ModelRequest,
_ctrl: &AttemptControl,
) -> Result<ModelStream, ModelInvokeError> {
self.recorded.lock().unwrap().push(req.clone());
let mut scripts = self.scripts.lock().unwrap();
match (!scripts.is_empty()).then(|| scripts.remove(0)) {
None => Err(ModelInvokeError::new(
ModelInvokeErrorKind::Permanent,
"no more streaming scripts",
)),
Some(Err(kind)) => Err(ModelInvokeError::new(kind, "scripted")),
Some(Ok(deltas)) => Ok(Box::pin(futures_util::stream::iter(deltas))),
}
}
}
pub fn text_script(text: &str) -> StreamScript {
Ok(vec![
StreamDelta::TextDelta {
delta: text.to_string(),
},
StreamDelta::Done {
stop_reason: ModelStopReason::EndTurn,
final_output: endturn_output(text),
},
])
}
pub fn tooluse_script(text: &str, tool: &str, args: serde_json::Value) -> StreamScript {
Ok(vec![
StreamDelta::TextDelta {
delta: text.to_string(),
},
StreamDelta::ToolCallDelta {
call_index: 0,
provider_call_id: Some("prov-1".into()),
name_delta: Some(tool.to_string()),
arguments_delta: None,
},
StreamDelta::ToolCallDelta {
call_index: 0,
provider_call_id: None,
name_delta: None,
arguments_delta: Some(args.to_string()),
},
StreamDelta::Done {
stop_reason: ModelStopReason::ToolUse,
final_output: tooluse_output(text, tool, args),
},
])
}