synaptic_models/
bound_tools.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use synaptic_core::{
5 ChatModel, ChatRequest, ChatResponse, ChatStream, SynapticError, ToolDefinition,
6};
7
8pub struct BoundToolsChatModel {
13 inner: Arc<dyn ChatModel>,
14 tools: Vec<ToolDefinition>,
15}
16
17impl BoundToolsChatModel {
18 pub fn new(inner: Arc<dyn ChatModel>, tools: Vec<ToolDefinition>) -> Self {
19 Self { inner, tools }
20 }
21
22 fn inject_tools(&self, mut request: ChatRequest) -> ChatRequest {
23 if request.tools.is_empty() {
24 request.tools = self.tools.clone();
25 } else {
26 for tool in &self.tools {
28 if !request.tools.iter().any(|t| t.name == tool.name) {
29 request.tools.push(tool.clone());
30 }
31 }
32 }
33 request
34 }
35}
36
37#[async_trait]
38impl ChatModel for BoundToolsChatModel {
39 async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, SynapticError> {
40 self.inner.chat(self.inject_tools(request)).await
41 }
42
43 fn stream_chat(&self, request: ChatRequest) -> ChatStream<'_> {
44 self.inner.stream_chat(self.inject_tools(request))
45 }
46}