blob: e6f14a2f62cea6af52fd5e9b002cd1a5feb61c81 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
use super::*;
use maka_tools::request::{BindingRequest, BoundTool, RequestBinding};
use std::collections::BTreeMap;
pub(super) struct Binding {
pub services: Vec<Service>,
pub workers: tokio_util::task::TaskTracker,
}
impl RequestBinding for Binding {
fn bind(
&self,
input: BindingRequest,
) -> futures_util::future::BoxFuture<'static, Result<BTreeMap<String, BoundTool>, ToolError>>
{
let services: Vec<_> = self
.services
.iter()
.filter(|service| input.tools.contains(&service.selection.name))
.cloned()
.collect();
let service = services
.first()
.expect("selected request binding group")
.clone();
let workers = self.workers.clone();
Box::pin(async move {
let access = service.access.clone();
access.fresh().await.map_err(ToolError::Failed)?;
let request = service
.services
.request_with_work(
Identity::Agent {
execution: access.execution().clone(),
operation_id: None,
},
input.cancellation.clone(),
access.custody().clone(),
)
.map_err(ToolError::Failed)?;
let request = access.track_binding_request(request).await;
let target = service.selection.target.clone();
let capture = workers
.spawn(async move {
let result = AssertUnwindSafe(async {
request
.call_target(
&target,
"capture".into(),
serde_json::json!({}),
Attachments::default(),
)?
.await
})
.catch_unwind()
.await;
access.settle_binding_request(&request).await?;
result
.map_err(|_| ToolError::Failed("Web capture panicked".into()))?
.map_err(ToolError::Failed)
})
.await
.map_err(|_| {
ToolError::CleanupUnconfirmed("Web capture worker disappeared".into())
})??;
if input.cancellation.is_cancelled() {
return Err(ToolError::Failed("Web capture cancelled".into()));
}
let capture: maka_web_api::contract::Capture = serde_json::from_value(capture)
.map_err(|_| ToolError::Failed("Web capture invalid".into()))?;
Ok(services
.into_iter()
.map(|service| {
let name = service.selection.name.clone();
let projection = if !capture.available {
BoundTool::Unavailable
} else {
let search = service.selection.method == "search";
let prepared = || {
BoundTool::Prepared(Arc::new(Service {
shell: None,
web_basis: Some(capture.basis.clone()),
..service
}))
};
if search {
match maka_web::search::select(&capture, input.model.as_ref()) {
Some(maka_web::search::Selection::Provider(tool)) => {
BoundTool::Provider(tool)
}
Some(maka_web::search::Selection::Custom) => prepared(),
None => BoundTool::Unavailable,
}
} else {
prepared()
}
};
(name, projection)
})
.collect())
})
}
}