use crate::runtime::inference::{
ContextMessage, InferenceModelOverride, InferenceOverride, InferenceRequestTransform,
};
use crate::runtime::run::TerminationReason;
use crate::runtime::state::AnyStateAction;
use crate::runtime::tool_call::gate::{SuspendTicket, ToolCallAction};
use crate::runtime::tool_call::ToolResult;
use std::sync::Arc;
#[derive(Default)]
pub struct ActionSet<A>(Vec<A>);
impl<A> ActionSet<A> {
pub fn empty() -> Self {
Self(Vec::new())
}
pub fn single(a: impl Into<A>) -> Self {
Self(vec![a.into()])
}
#[must_use]
pub fn and(mut self, other: impl Into<ActionSet<A>>) -> Self {
self.0.extend(other.into().0);
self
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn as_slice(&self) -> &[A] {
&self.0
}
pub fn into_vec(self) -> Vec<A> {
self.0
}
}
impl<A> IntoIterator for ActionSet<A> {
type Item = A;
type IntoIter = std::vec::IntoIter<A>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<A> From<Vec<A>> for ActionSet<A> {
fn from(v: Vec<A>) -> Self {
Self(v)
}
}
impl<A> Extend<A> for ActionSet<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
self.0.extend(iter);
}
}
pub enum LifecycleAction {
State(AnyStateAction),
}
impl From<AnyStateAction> for LifecycleAction {
fn from(sa: AnyStateAction) -> Self {
Self::State(sa)
}
}
impl From<LifecycleAction> for ActionSet<LifecycleAction> {
fn from(a: LifecycleAction) -> Self {
ActionSet::single(a)
}
}
impl From<AnyStateAction> for ActionSet<LifecycleAction> {
fn from(sa: AnyStateAction) -> Self {
ActionSet::single(LifecycleAction::State(sa))
}
}
pub enum BeforeInferenceAction {
AddContextMessage(ContextMessage),
ExcludeTool(String),
IncludeOnlyTools(Vec<String>),
AddRequestTransform(Arc<dyn InferenceRequestTransform>),
OverrideModel(InferenceModelOverride),
OverrideInference(InferenceOverride),
Terminate(TerminationReason),
State(AnyStateAction),
}
impl From<AnyStateAction> for BeforeInferenceAction {
fn from(sa: AnyStateAction) -> Self {
Self::State(sa)
}
}
impl From<BeforeInferenceAction> for ActionSet<BeforeInferenceAction> {
fn from(a: BeforeInferenceAction) -> Self {
ActionSet::single(a)
}
}
impl From<AnyStateAction> for ActionSet<BeforeInferenceAction> {
fn from(sa: AnyStateAction) -> Self {
ActionSet::single(BeforeInferenceAction::State(sa))
}
}
pub enum AfterInferenceAction {
Terminate(TerminationReason),
State(AnyStateAction),
}
impl From<AnyStateAction> for AfterInferenceAction {
fn from(sa: AnyStateAction) -> Self {
Self::State(sa)
}
}
impl From<AfterInferenceAction> for ActionSet<AfterInferenceAction> {
fn from(a: AfterInferenceAction) -> Self {
ActionSet::single(a)
}
}
impl From<AnyStateAction> for ActionSet<AfterInferenceAction> {
fn from(sa: AnyStateAction) -> Self {
ActionSet::single(AfterInferenceAction::State(sa))
}
}
pub enum BeforeToolExecuteAction {
Block(String),
Suspend(SuspendTicket),
SetToolResult(ToolResult),
State(AnyStateAction),
}
impl BeforeToolExecuteAction {
pub fn from_decision(decision: ToolCallAction) -> Self {
match decision {
ToolCallAction::Block { reason } => Self::Block(reason),
ToolCallAction::Suspend(ticket) => Self::Suspend(*ticket),
ToolCallAction::Proceed => {
unreachable!("Proceed is not emitted as a BeforeToolExecuteAction")
}
}
}
}
impl From<AnyStateAction> for BeforeToolExecuteAction {
fn from(sa: AnyStateAction) -> Self {
Self::State(sa)
}
}
impl From<BeforeToolExecuteAction> for ActionSet<BeforeToolExecuteAction> {
fn from(a: BeforeToolExecuteAction) -> Self {
ActionSet::single(a)
}
}
impl From<AnyStateAction> for ActionSet<BeforeToolExecuteAction> {
fn from(sa: AnyStateAction) -> Self {
ActionSet::single(BeforeToolExecuteAction::State(sa))
}
}
pub enum AfterToolExecuteAction {
AddMessage(ContextMessage),
State(AnyStateAction),
}
impl AfterToolExecuteAction {
pub fn label(&self) -> &'static str {
match self {
Self::AddMessage(_) => "add_message",
Self::State(_) => "state_action",
}
}
}
impl From<AnyStateAction> for AfterToolExecuteAction {
fn from(sa: AnyStateAction) -> Self {
Self::State(sa)
}
}
impl From<AfterToolExecuteAction> for ActionSet<AfterToolExecuteAction> {
fn from(a: AfterToolExecuteAction) -> Self {
ActionSet::single(a)
}
}
impl From<AnyStateAction> for ActionSet<AfterToolExecuteAction> {
fn from(sa: AnyStateAction) -> Self {
ActionSet::single(AfterToolExecuteAction::State(sa))
}
}