use crate::{
CoreError, ToolAdapterId, ToolBindingName, ToolSourceFingerprint, ToolSourceId, ToolSpec,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExternalToolBinding {
adapter: ToolAdapterId,
source: ToolSourceId,
operation: ToolBindingName,
source_fingerprint: ToolSourceFingerprint,
}
impl ExternalToolBinding {
#[must_use]
pub fn new(
adapter: ToolAdapterId,
source: ToolSourceId,
operation: ToolBindingName,
source_fingerprint: ToolSourceFingerprint,
) -> Self {
Self {
adapter,
source,
operation,
source_fingerprint,
}
}
#[must_use]
pub fn adapter(&self) -> &ToolAdapterId {
&self.adapter
}
#[must_use]
pub fn source(&self) -> &ToolSourceId {
&self.source
}
#[must_use]
pub fn operation(&self) -> &ToolBindingName {
&self.operation
}
#[must_use]
pub fn source_fingerprint(&self) -> &ToolSourceFingerprint {
&self.source_fingerprint
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionToolCatalogEntry {
spec: ToolSpec,
binding: ExternalToolBinding,
}
impl SessionToolCatalogEntry {
#[must_use]
pub fn new(spec: ToolSpec, binding: ExternalToolBinding) -> Self {
Self { spec, binding }
}
#[must_use]
pub fn spec(&self) -> &ToolSpec {
&self.spec
}
#[must_use]
pub fn binding(&self) -> &ExternalToolBinding {
&self.binding
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StoredCatalog")]
pub struct SessionToolCatalog {
format_version: u32,
entries: Vec<SessionToolCatalogEntry>,
}
impl SessionToolCatalog {
pub fn new(entries: Vec<SessionToolCatalogEntry>) -> Result<Self, CoreError> {
let mut names = BTreeSet::new();
let mut bindings = BTreeSet::new();
for entry in &entries {
if !names.insert(entry.spec().name()) || !bindings.insert(entry.binding()) {
return Err(CoreError::InvalidToolSpec {
reason: "external tool catalog contains duplicate names or bindings",
});
}
}
Ok(Self {
format_version: 1,
entries,
})
}
#[must_use]
pub fn entries(&self) -> &[SessionToolCatalogEntry] {
&self.entries
}
}
impl Default for SessionToolCatalog {
fn default() -> Self {
Self {
format_version: 1,
entries: Vec::new(),
}
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StoredCatalog {
format_version: u32,
entries: Vec<SessionToolCatalogEntry>,
}
impl TryFrom<StoredCatalog> for SessionToolCatalog {
type Error = CoreError;
fn try_from(stored: StoredCatalog) -> Result<Self, Self::Error> {
if stored.format_version != 1 {
return Err(CoreError::InvalidToolSpec {
reason: "unsupported external tool catalog version",
});
}
Self::new(stored.entries)
}
}
#[cfg(test)]
#[path = "tool_catalog_tests.rs"]
mod tests;