use serde::{Deserialize, Serialize};
use rig_core::message::{CallId, ToolCall, ToolName};
use rig_core::tool::ToolResult;
use super::InvalidToolCallAction;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PendingToolCall {
Execute(ExecCall),
Malformed(MalformedCall),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecCall {
pub(super) turn: usize,
pub(super) index: usize,
pub(super) tool_call: ToolCall,
}
impl ExecCall {
pub fn id(&self) -> &CallId {
&self.tool_call.id
}
pub fn name(&self) -> &ToolName {
&self.tool_call.function.name
}
pub fn tool_call(&self) -> &ToolCall {
&self.tool_call
}
pub fn arguments(&self) -> serde_json::Value {
self.tool_call.function.arguments_value()
}
pub fn answer(self, result: ToolResult) -> ToolAnswer {
ToolAnswer {
turn: self.turn,
index: self.index,
call: self.tool_call.id,
kind: AnswerKind::Executed(result),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MalformedCall {
pub(super) turn: usize,
pub(super) index: usize,
pub(super) tool_call: ToolCall,
}
impl MalformedCall {
pub fn id(&self) -> &CallId {
&self.tool_call.id
}
pub fn name(&self) -> &ToolName {
&self.tool_call.function.name
}
pub fn raw_arguments(&self) -> &str {
self.tool_call
.function
.invalid_arguments
.as_deref()
.unwrap_or_default()
}
pub(crate) fn tool_call(&self) -> &ToolCall {
&self.tool_call
}
pub fn answer(self, action: Option<InvalidToolCallAction>) -> ToolAnswer {
ToolAnswer {
turn: self.turn,
index: self.index,
call: self.tool_call.id,
kind: AnswerKind::Malformed(action),
}
}
}
#[derive(Debug)]
pub struct ToolAnswer {
pub(crate) turn: usize,
pub(crate) index: usize,
pub(crate) call: CallId,
pub(crate) kind: AnswerKind,
}
impl ToolAnswer {
pub(crate) fn ends_run(&self) -> bool {
matches!(
self.kind,
AnswerKind::Malformed(Some(
InvalidToolCallAction::Stop { .. }
| InvalidToolCallAction::Fail
| InvalidToolCallAction::Repair { .. }
))
)
}
}
#[derive(Debug)]
pub(crate) enum AnswerKind {
Executed(ToolResult),
Malformed(Option<InvalidToolCallAction>),
}